LibGUI: Make it easier to create checkable GUI::Actions

This patch adds GUI::Action::create_checkable() helpers that work just
like the existing create() helpers, but the actions become checkable(!)

Clients are no longer required to manage the checked state of their
actions manually, but instead they will be checked/unchecked as needed
by GUI::Action itself before the activation hook is fired.
This commit is contained in:
Andreas Kling 2020-04-21 17:19:27 +02:00
parent 1032ae0140
commit 705cee528a
Notes: sideshowbarker 2024-07-19 07:24:49 +09:00
18 changed files with 135 additions and 158 deletions

View file

@ -288,22 +288,18 @@ int main(int argc, char** argv)
} }
})); }));
debug_menu.add_separator(); debug_menu.add_separator();
auto line_box_borders_action = GUI::Action::create("Line box borders", [&](auto& action) { auto line_box_borders_action = GUI::Action::create_checkable("Line box borders", [&](auto& action) {
action.set_checked(!action.is_checked());
html_widget.set_should_show_line_box_borders(action.is_checked()); html_widget.set_should_show_line_box_borders(action.is_checked());
html_widget.update(); html_widget.update();
}); });
line_box_borders_action->set_checkable(true);
line_box_borders_action->set_checked(false); line_box_borders_action->set_checked(false);
debug_menu.add_action(line_box_borders_action); debug_menu.add_action(line_box_borders_action);
auto& bookmarks_menu = menubar->add_menu("Bookmarks"); auto& bookmarks_menu = menubar->add_menu("Bookmarks");
auto show_bookmarksbar_action = GUI::Action::create("Show bookmarks bar", [&](auto& action) { auto show_bookmarksbar_action = GUI::Action::create_checkable("Show bookmarks bar", [&](auto& action) {
action.set_checked(!action.is_checked());
bookmarksbar.set_visible(action.is_checked()); bookmarksbar.set_visible(action.is_checked());
bookmarksbar.update(); bookmarksbar.update();
}); });
show_bookmarksbar_action->set_checkable(true);
show_bookmarksbar_action->set_checked(bookmarksbar_enabled); show_bookmarksbar_action->set_checked(bookmarksbar_enabled);
bookmarks_menu.add_action(show_bookmarksbar_action); bookmarks_menu.add_action(show_bookmarksbar_action);

View file

@ -283,38 +283,29 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
RefPtr<GUI::Action> view_as_icons_action; RefPtr<GUI::Action> view_as_icons_action;
RefPtr<GUI::Action> view_as_columns_action; RefPtr<GUI::Action> view_as_columns_action;
view_as_table_action = GUI::Action::create( view_as_table_action = GUI::Action::create_checkable(
"Table view", { Mod_Ctrl, KeyCode::Key_L }, Gfx::Bitmap::load_from_file("/res/icons/16x16/table-view.png"), [&](const GUI::Action&) { "Table view", { Mod_Ctrl, KeyCode::Key_L }, Gfx::Bitmap::load_from_file("/res/icons/16x16/table-view.png"), [&](const GUI::Action&) {
directory_view.set_view_mode(DirectoryView::ViewMode::List); directory_view.set_view_mode(DirectoryView::ViewMode::List);
view_as_table_action->set_checked(true);
config->write_entry("DirectoryView", "ViewMode", "List"); config->write_entry("DirectoryView", "ViewMode", "List");
config->sync(); config->sync();
}, },
window); window);
view_as_table_action->set_checkable(true);
view_as_icons_action = GUI::Action::create( view_as_icons_action = GUI::Action::create_checkable(
"Icon view", { Mod_Ctrl, KeyCode::Key_I }, Gfx::Bitmap::load_from_file("/res/icons/16x16/icon-view.png"), [&](const GUI::Action&) { "Icon view", { Mod_Ctrl, KeyCode::Key_I }, Gfx::Bitmap::load_from_file("/res/icons/16x16/icon-view.png"), [&](const GUI::Action&) {
directory_view.set_view_mode(DirectoryView::ViewMode::Icon); directory_view.set_view_mode(DirectoryView::ViewMode::Icon);
view_as_icons_action->set_checked(true);
config->write_entry("DirectoryView", "ViewMode", "Icon"); config->write_entry("DirectoryView", "ViewMode", "Icon");
config->sync(); config->sync();
}, },
window); window);
view_as_icons_action->set_checkable(true);
view_as_columns_action = GUI::Action::create( view_as_columns_action = GUI::Action::create_checkable(
"Columns view", Gfx::Bitmap::load_from_file("/res/icons/16x16/columns-view.png"), [&](const GUI::Action&) { "Columns view", Gfx::Bitmap::load_from_file("/res/icons/16x16/columns-view.png"), [&](const GUI::Action&) {
directory_view.set_view_mode(DirectoryView::ViewMode::Columns); directory_view.set_view_mode(DirectoryView::ViewMode::Columns);
view_as_columns_action->set_checked(true);
config->write_entry("DirectoryView", "ViewMode", "Columns"); config->write_entry("DirectoryView", "ViewMode", "Columns");
config->sync(); config->sync();
}, },
window); window);
view_as_columns_action->set_checkable(true);
auto view_type_action_group = make<GUI::ActionGroup>(); auto view_type_action_group = make<GUI::ActionGroup>();
view_type_action_group->set_exclusive(true); view_type_action_group->set_exclusive(true);

View file

@ -26,10 +26,10 @@
#include "EllipseTool.h" #include "EllipseTool.h"
#include "PaintableWidget.h" #include "PaintableWidget.h"
#include <LibGfx/Rect.h>
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Menu.h> #include <LibGUI/Menu.h>
#include <LibGUI/Painter.h> #include <LibGUI/Painter.h>
#include <LibGfx/Rect.h>
#include <LibM/math.h> #include <LibM/math.h>
EllipseTool::EllipseTool() EllipseTool::EllipseTool()
@ -117,11 +117,9 @@ void EllipseTool::on_contextmenu(GUI::ContextMenuEvent& event)
m_context_menu->add_separator(); m_context_menu->add_separator();
m_thickness_actions.set_exclusive(true); m_thickness_actions.set_exclusive(true);
auto insert_action = [&](int size, bool checked = false) { auto insert_action = [&](int size, bool checked = false) {
auto action = GUI::Action::create(String::number(size), [this, size](auto& action) { auto action = GUI::Action::create_checkable(String::number(size), [this, size](auto&) {
m_thickness = size; m_thickness = size;
action.set_checked(true);
}); });
action->set_checkable(true);
action->set_checked(checked); action->set_checked(checked);
m_thickness_actions.add_action(*action); m_thickness_actions.add_action(*action);
m_context_menu->add_action(move(action)); m_context_menu->add_action(move(action));

View file

@ -77,12 +77,9 @@ void EraseTool::on_contextmenu(GUI::ContextMenuEvent& event)
if (!m_context_menu) { if (!m_context_menu) {
m_context_menu = GUI::Menu::construct(); m_context_menu = GUI::Menu::construct();
NonnullRefPtr<GUI::Action> eraser_color_toggler = GUI::Action::create("Use secondary color", [&](GUI::Action& action) { auto eraser_color_toggler = GUI::Action::create_checkable("Use secondary color", [&](auto& action) {
bool toggled = !m_use_secondary_color; m_use_secondary_color = action.is_checked();
m_use_secondary_color = toggled;
action.set_checked(toggled);
}); });
eraser_color_toggler->set_checkable(true);
eraser_color_toggler->set_checked(m_use_secondary_color); eraser_color_toggler->set_checked(m_use_secondary_color);
m_context_menu->add_action(eraser_color_toggler); m_context_menu->add_action(eraser_color_toggler);
@ -90,11 +87,9 @@ void EraseTool::on_contextmenu(GUI::ContextMenuEvent& event)
m_thickness_actions.set_exclusive(true); m_thickness_actions.set_exclusive(true);
auto insert_action = [&](int size, bool checked = false) { auto insert_action = [&](int size, bool checked = false) {
auto action = GUI::Action::create(String::number(size), [this, size](auto& action) { auto action = GUI::Action::create_checkable(String::number(size), [this, size](auto&) {
m_thickness = size; m_thickness = size;
action.set_checked(true);
}); });
action->set_checkable(true);
action->set_checked(checked); action->set_checked(checked);
m_thickness_actions.add_action(*action); m_thickness_actions.add_action(*action);
m_context_menu->add_action(move(action)); m_context_menu->add_action(move(action));

View file

@ -133,11 +133,9 @@ void LineTool::on_contextmenu(GUI::ContextMenuEvent& event)
m_context_menu = GUI::Menu::construct(); m_context_menu = GUI::Menu::construct();
m_thickness_actions.set_exclusive(true); m_thickness_actions.set_exclusive(true);
auto insert_action = [&](int size, bool checked = false) { auto insert_action = [&](int size, bool checked = false) {
auto action = GUI::Action::create(String::number(size), [this, size](auto& action) { auto action = GUI::Action::create_checkable(String::number(size), [this, size](auto&) {
m_thickness = size; m_thickness = size;
action.set_checked(true);
}); });
action->set_checkable(true);
action->set_checked(checked); action->set_checked(checked);
m_thickness_actions.add_action(*action); m_thickness_actions.add_action(*action);
m_context_menu->add_action(move(action)); m_context_menu->add_action(move(action));

View file

@ -79,11 +79,9 @@ void PenTool::on_contextmenu(GUI::ContextMenuEvent& event)
m_context_menu = GUI::Menu::construct(); m_context_menu = GUI::Menu::construct();
m_thickness_actions.set_exclusive(true); m_thickness_actions.set_exclusive(true);
auto insert_action = [&](int size, bool checked = false) { auto insert_action = [&](int size, bool checked = false) {
auto action = GUI::Action::create(String::number(size), [this, size](auto& action) { auto action = GUI::Action::create_checkable(String::number(size), [this, size](auto&) {
m_thickness = size; m_thickness = size;
action.set_checked(true);
}); });
action->set_checkable(true);
action->set_checked(checked); action->set_checked(checked);
m_thickness_actions.add_action(*action); m_thickness_actions.add_action(*action);
m_context_menu->add_action(move(action)); m_context_menu->add_action(move(action));

View file

@ -105,11 +105,9 @@ void SprayTool::on_contextmenu(GUI::ContextMenuEvent& event)
m_context_menu = GUI::Menu::construct(); m_context_menu = GUI::Menu::construct();
m_thickness_actions.set_exclusive(true); m_thickness_actions.set_exclusive(true);
auto insert_action = [&](int size, bool checked = false) { auto insert_action = [&](int size, bool checked = false) {
auto action = GUI::Action::create(String::number(size), [this, size](auto& action) { auto action = GUI::Action::create_checkable(String::number(size), [this, size](auto&) {
m_thickness = size; m_thickness = size;
action.set_checked(true);
}); });
action->set_checkable(true);
action->set_checked(checked); action->set_checked(checked);
m_thickness_actions.add_action(*action); m_thickness_actions.add_action(*action);
m_context_menu->add_action(move(action)); m_context_menu->add_action(move(action));

View file

@ -74,11 +74,9 @@ int main(int argc, char** argv)
player.manager().play(); player.manager().play();
} }
auto hide_scope = GUI::Action::create("Hide scope", { Mod_Ctrl, Key_H }, [&](GUI::Action& action) { auto hide_scope = GUI::Action::create_checkable("Hide scope", { Mod_Ctrl, Key_H }, [&](auto& action) {
action.set_checked(!action.is_checked());
player.hide_scope(action.is_checked()); player.hide_scope(action.is_checked());
}); });
hide_scope->set_checkable(true);
app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) { app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
Optional<String> path = GUI::FilePicker::get_open_filepath("Open wav file..."); Optional<String> path = GUI::FilePicker::get_open_filepath("Open wav file...");

View file

@ -200,11 +200,9 @@ int main(int argc, char** argv)
frequency_action_group.set_exclusive(true); frequency_action_group.set_exclusive(true);
auto make_frequency_action = [&](auto& title, int interval, bool checked = false) { auto make_frequency_action = [&](auto& title, int interval, bool checked = false) {
auto action = GUI::Action::create(title, [&refresh_timer, interval](auto& action) { auto action = GUI::Action::create_checkable(title, [&refresh_timer, interval](auto&) {
refresh_timer.restart(interval); refresh_timer.restart(interval);
action.set_checked(true);
}); });
action->set_checkable(true);
action->set_checked(checked); action->set_checked(checked);
frequency_action_group.add_action(*action); frequency_action_group.add_action(*action);
frequency_menu.add_action(*action); frequency_menu.add_action(*action);

View file

@ -278,8 +278,7 @@ int main(int argc, char** argv)
font_action_group.set_exclusive(true); font_action_group.set_exclusive(true);
auto& font_menu = menubar->add_menu("Font"); auto& font_menu = menubar->add_menu("Font");
GUI::FontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) { GUI::FontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
auto action = GUI::Action::create(font_name, [&](GUI::Action& action) { auto action = GUI::Action::create_checkable(font_name, [&](auto& action) {
action.set_checked(true);
terminal.set_font(GUI::FontDatabase::the().get_by_name(action.text())); terminal.set_font(GUI::FontDatabase::the().get_by_name(action.text()));
auto metadata = GUI::FontDatabase::the().get_metadata_by_name(action.text()); auto metadata = GUI::FontDatabase::the().get_metadata_by_name(action.text());
ASSERT(metadata.has_value()); ASSERT(metadata.has_value());
@ -288,7 +287,6 @@ int main(int argc, char** argv)
terminal.force_repaint(); terminal.force_repaint();
}); });
font_action_group.add_action(*action); font_action_group.add_action(*action);
action->set_checkable(true);
if (terminal.font().name() == font_name) if (terminal.font().name() == font_name)
action->set_checked(true); action->set_checked(true);
font_menu.add_action(*action); font_menu.add_action(*action);

View file

@ -334,11 +334,9 @@ TextEditorWidget::TextEditorWidget()
m_save_as_action->activate(); m_save_as_action->activate();
}); });
m_line_wrapping_setting_action = GUI::Action::create("Line wrapping", [&](GUI::Action& action) { m_line_wrapping_setting_action = GUI::Action::create_checkable("Line wrapping", [&](auto& action) {
action.set_checked(!action.is_checked());
m_editor->set_line_wrapping_enabled(action.is_checked()); m_editor->set_line_wrapping_enabled(action.is_checked());
}); });
m_line_wrapping_setting_action->set_checkable(true);
m_line_wrapping_setting_action->set_checked(m_editor->is_line_wrapping_enabled()); m_line_wrapping_setting_action->set_checked(m_editor->is_line_wrapping_enabled());
auto menubar = GUI::MenuBar::construct(); auto menubar = GUI::MenuBar::construct();
@ -382,31 +380,25 @@ TextEditorWidget::TextEditorWidget()
syntax_actions.set_exclusive(true); syntax_actions.set_exclusive(true);
auto& syntax_menu = menubar->add_menu("Syntax"); auto& syntax_menu = menubar->add_menu("Syntax");
m_plain_text_highlight = GUI::Action::create("Plain Text", [&](GUI::Action& action) { m_plain_text_highlight = GUI::Action::create_checkable("Plain text", [&](auto&) {
action.set_checked(true);
m_editor->set_syntax_highlighter(nullptr); m_editor->set_syntax_highlighter(nullptr);
m_editor->update(); m_editor->update();
}); });
m_plain_text_highlight->set_checkable(true);
m_plain_text_highlight->set_checked(true); m_plain_text_highlight->set_checked(true);
syntax_actions.add_action(*m_plain_text_highlight); syntax_actions.add_action(*m_plain_text_highlight);
syntax_menu.add_action(*m_plain_text_highlight); syntax_menu.add_action(*m_plain_text_highlight);
m_cpp_highlight = GUI::Action::create("C++", [&](GUI::Action& action) { m_cpp_highlight = GUI::Action::create_checkable("C++", [&](auto&) {
action.set_checked(true);
m_editor->set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>()); m_editor->set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
m_editor->update(); m_editor->update();
}); });
m_cpp_highlight->set_checkable(true);
syntax_actions.add_action(*m_cpp_highlight); syntax_actions.add_action(*m_cpp_highlight);
syntax_menu.add_action(*m_cpp_highlight); syntax_menu.add_action(*m_cpp_highlight);
m_js_highlight = GUI::Action::create("Javascript", [&](GUI::Action& action) { m_js_highlight = GUI::Action::create_checkable("JavaScript", [&](auto&) {
action.set_checked(true);
m_editor->set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>()); m_editor->set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
m_editor->update(); m_editor->update();
}); });
m_js_highlight->set_checkable(true);
syntax_actions.add_action(*m_js_highlight); syntax_actions.add_action(*m_js_highlight);
syntax_menu.add_action(*m_js_highlight); syntax_menu.add_action(*m_js_highlight);

View file

@ -274,10 +274,9 @@ int main(int argc, char** argv)
GUI::ActionGroup tool_actions; GUI::ActionGroup tool_actions;
tool_actions.set_exclusive(true); tool_actions.set_exclusive(true);
auto cursor_tool_action = GUI::Action::create("Cursor", Gfx::Bitmap::load_from_file("/res/icons/widgets/Cursor.png"), [&](auto&) { auto cursor_tool_action = GUI::Action::create_checkable("Cursor", Gfx::Bitmap::load_from_file("/res/icons/widgets/Cursor.png"), [&](auto&) {
g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget)); g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget));
}); });
cursor_tool_action->set_checkable(true);
cursor_tool_action->set_checked(true); cursor_tool_action->set_checked(true);
tool_actions.add_action(cursor_tool_action); tool_actions.add_action(cursor_tool_action);
@ -285,14 +284,13 @@ int main(int argc, char** argv)
GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& reg) { GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& reg) {
auto icon_path = String::format("/res/icons/widgets/G%s.png", reg.class_name().characters()); auto icon_path = String::format("/res/icons/widgets/G%s.png", reg.class_name().characters());
auto action = GUI::Action::create(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg](auto&) { auto action = GUI::Action::create_checkable(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg](auto&) {
g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg)); g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg));
auto widget = reg.construct(); auto widget = reg.construct();
g_form_editor_widget->form_widget().add_child(widget); g_form_editor_widget->form_widget().add_child(widget);
widget->set_relative_rect(30, 30, 30, 30); widget->set_relative_rect(30, 30, 30, 30);
g_form_editor_widget->model().update(); g_form_editor_widget->model().update();
}); });
action->set_checkable(true);
action->set_checked(false); action->set_checked(false);
tool_actions.add_action(action); tool_actions.add_action(action);
form_widgets_toolbar.add_action(move(action)); form_widgets_toolbar.add_action(move(action));

View file

@ -85,21 +85,17 @@ int main(int argc, char** argv)
app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app.quit(); })); app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app.quit(); }));
auto& view_menu = menubar->add_menu("View"); auto& view_menu = menubar->add_menu("View");
auto invert_action = GUI::Action::create("Invert tree", { Mod_Ctrl, Key_I }, [&](auto& action) { auto invert_action = GUI::Action::create_checkable("Invert tree", { Mod_Ctrl, Key_I }, [&](auto& action) {
action.set_checked(!action.is_checked());
profile->set_inverted(action.is_checked()); profile->set_inverted(action.is_checked());
}); });
invert_action->set_checkable(true);
invert_action->set_checked(false); invert_action->set_checked(false);
view_menu.add_action(invert_action); view_menu.add_action(invert_action);
auto percent_action = GUI::Action::create("Show percentages", { Mod_Ctrl, Key_P }, [&](auto& action) { auto percent_action = GUI::Action::create_checkable("Show percentages", { Mod_Ctrl, Key_P }, [&](auto& action) {
action.set_checked(!action.is_checked());
profile->set_show_percentages(action.is_checked()); profile->set_show_percentages(action.is_checked());
tree_view.update(); tree_view.update();
disassembly_view.update(); disassembly_view.update();
}); });
percent_action->set_checkable(true);
percent_action->set_checked(false); percent_action->set_checked(false);
view_menu.add_action(percent_action); view_menu.add_action(percent_action);

View file

@ -90,12 +90,9 @@ int main(int argc, char** argv)
app_menu.add_separator(); app_menu.add_separator();
NonnullRefPtr<GUI::Action> chord_toggler_action = GUI::Action::create("Single-click chording", [&](const GUI::Action&) { auto chord_toggler_action = GUI::Action::create_checkable("Single-click chording", [&](auto& action) {
bool toggled = !field.is_single_chording(); field.set_single_chording(!action.is_checked());
field.set_single_chording(toggled);
chord_toggler_action->set_checked(toggled);
}); });
chord_toggler_action->set_checkable(true);
chord_toggler_action->set_checked(field.is_single_chording()); chord_toggler_action->set_checked(field.is_single_chording());
app_menu.add_action(*chord_toggler_action); app_menu.add_action(*chord_toggler_action);

View file

@ -199,11 +199,9 @@ Menu& AbstractTableView::ensure_header_context_menu()
for (int column = 0; column < model()->column_count(); ++column) { for (int column = 0; column < model()->column_count(); ++column) {
auto& column_data = this->column_data(column); auto& column_data = this->column_data(column);
auto name = model()->column_name(column); auto name = model()->column_name(column);
column_data.visibility_action = Action::create(name, [this, column](Action& action) { column_data.visibility_action = Action::create_checkable(name, [this, column](auto& action) {
action.set_checked(!action.is_checked());
set_column_hidden(column, !action.is_checked()); set_column_hidden(column, !action.is_checked());
}); });
column_data.visibility_action->set_checkable(true);
column_data.visibility_action->set_checked(true); column_data.visibility_action->set_checked(true);
m_header_context_menu->add_action(*column_data.visibility_action); m_header_context_menu->add_action(*column_data.visibility_action);

View file

@ -111,32 +111,35 @@ namespace CommonActions {
} }
Action::Action(const StringView& text, Function<void(Action&)> on_activation_callback, Core::Object* parent) Action::Action(const StringView& text, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
: Core::Object(parent) : Core::Object(parent)
, on_activation(move(on_activation_callback)) , on_activation(move(on_activation_callback))
, m_text(text) , m_text(text)
, m_checkable(checkable)
{ {
} }
Action::Action(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> on_activation_callback, Core::Object* parent) Action::Action(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
: Core::Object(parent) : Core::Object(parent)
, on_activation(move(on_activation_callback)) , on_activation(move(on_activation_callback))
, m_text(text) , m_text(text)
, m_icon(move(icon)) , m_icon(move(icon))
, m_checkable(checkable)
{ {
} }
Action::Action(const StringView& text, const Shortcut& shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent) Action::Action(const StringView& text, const Shortcut& shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
: Action(text, shortcut, nullptr, move(on_activation_callback), parent) : Action(text, shortcut, nullptr, move(on_activation_callback), parent, checkable)
{ {
} }
Action::Action(const StringView& text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> on_activation_callback, Core::Object* parent) Action::Action(const StringView& text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
: Core::Object(parent) : Core::Object(parent)
, on_activation(move(on_activation_callback)) , on_activation(move(on_activation_callback))
, m_text(text) , m_text(text)
, m_icon(move(icon)) , m_icon(move(icon))
, m_shortcut(shortcut) , m_shortcut(shortcut)
, m_checkable(checkable)
{ {
if (parent && Core::is<Widget>(*parent)) { if (parent && Core::is<Widget>(*parent)) {
m_scope = ShortcutScope::WidgetLocal; m_scope = ShortcutScope::WidgetLocal;
@ -156,9 +159,23 @@ Action::~Action()
void Action::activate(Core::Object* activator) void Action::activate(Core::Object* activator)
{ {
if (!on_activation)
return;
if (activator) if (activator)
m_activator = activator->make_weak_ptr(); m_activator = activator->make_weak_ptr();
if (on_activation)
if (is_checkable()) {
if (m_action_group) {
if (m_action_group->is_unchecking_allowed())
set_checked(!is_checked());
else
set_checked(true);
} else {
set_checked(!is_checked());
}
}
on_activation(*this); on_activation(*this);
m_activator = nullptr; m_activator = nullptr;
} }

View file

@ -84,6 +84,23 @@ public:
{ {
return adopt(*new Action(text, shortcut, move(icon), move(callback), parent)); return adopt(*new Action(text, shortcut, move(icon), move(callback), parent));
} }
static NonnullRefPtr<Action> create_checkable(const StringView& text, Function<void(Action&)> callback, Core::Object* parent = nullptr)
{
return adopt(*new Action(text, move(callback), parent, true));
}
static NonnullRefPtr<Action> create_checkable(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> callback, Core::Object* parent = nullptr)
{
return adopt(*new Action(text, move(icon), move(callback), parent, true));
}
static NonnullRefPtr<Action> create_checkable(const StringView& text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr)
{
return adopt(*new Action(text, shortcut, move(callback), parent, true));
}
static NonnullRefPtr<Action> create_checkable(const StringView& text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> callback, Core::Object* parent = nullptr)
{
return adopt(*new Action(text, shortcut, move(icon), move(callback), parent, true));
}
virtual ~Action() override; virtual ~Action() override;
String text() const { return m_text; } String text() const { return m_text; }
@ -122,10 +139,10 @@ public:
private: private:
virtual bool is_action() const override { return true; } virtual bool is_action() const override { return true; }
Action(const StringView& text, Function<void(Action&)> = nullptr, Core::Object* = nullptr); Action(const StringView& text, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
Action(const StringView& text, const Shortcut&, Function<void(Action&)> = nullptr, Core::Object* = nullptr); Action(const StringView& text, const Shortcut&, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
Action(const StringView& text, const Shortcut&, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr); Action(const StringView& text, const Shortcut&, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
Action(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr); Action(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
template<typename Callback> template<typename Callback>
void for_each_toolbar_button(Callback); void for_each_toolbar_button(Callback);

View file

@ -165,27 +165,21 @@ void MultiView::set_column_hidden(int column_index, bool hidden)
void MultiView::build_actions() void MultiView::build_actions()
{ {
m_view_as_table_action = Action::create( m_view_as_table_action = Action::create_checkable(
"Table view", Gfx::Bitmap::load_from_file("/res/icons/16x16/table-view.png"), [this](auto&) { "Table view", Gfx::Bitmap::load_from_file("/res/icons/16x16/table-view.png"), [this](auto&) {
set_view_mode(ViewMode::List); set_view_mode(ViewMode::List);
m_view_as_table_action->set_checked(true);
}); });
m_view_as_table_action->set_checkable(true);
m_view_as_icons_action = Action::create( m_view_as_icons_action = Action::create_checkable(
"Icon view", Gfx::Bitmap::load_from_file("/res/icons/16x16/icon-view.png"), [this](auto&) { "Icon view", Gfx::Bitmap::load_from_file("/res/icons/16x16/icon-view.png"), [this](auto&) {
set_view_mode(ViewMode::Icon); set_view_mode(ViewMode::Icon);
m_view_as_icons_action->set_checked(true);
}); });
m_view_as_icons_action->set_checkable(true);
#ifdef MULTIVIEW_WITH_COLUMNSVIEW #ifdef MULTIVIEW_WITH_COLUMNSVIEW
m_view_as_columns_action = Action::create( m_view_as_columns_action = Action::create_checkable(
"Columns view", Gfx::Bitmap::load_from_file("/res/icons/16x16/columns-view.png"), [this](auto&) { "Columns view", Gfx::Bitmap::load_from_file("/res/icons/16x16/columns-view.png"), [this](auto&) {
set_view_mode(ViewMode::Columns); set_view_mode(ViewMode::Columns);
m_view_as_columns_action->set_checked(true);
}); });
m_view_as_columns_action->set_checkable(true);
#endif #endif
m_view_type_action_group = make<ActionGroup>(); m_view_type_action_group = make<ActionGroup>();