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();
auto line_box_borders_action = GUI::Action::create("Line box borders", [&](auto& action) {
action.set_checked(!action.is_checked());
auto line_box_borders_action = GUI::Action::create_checkable("Line box borders", [&](auto& action) {
html_widget.set_should_show_line_box_borders(action.is_checked());
html_widget.update();
});
line_box_borders_action->set_checkable(true);
line_box_borders_action->set_checked(false);
debug_menu.add_action(line_box_borders_action);
auto& bookmarks_menu = menubar->add_menu("Bookmarks");
auto show_bookmarksbar_action = GUI::Action::create("Show bookmarks bar", [&](auto& action) {
action.set_checked(!action.is_checked());
auto show_bookmarksbar_action = GUI::Action::create_checkable("Show bookmarks bar", [&](auto& action) {
bookmarksbar.set_visible(action.is_checked());
bookmarksbar.update();
});
show_bookmarksbar_action->set_checkable(true);
show_bookmarksbar_action->set_checked(bookmarksbar_enabled);
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_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&) {
directory_view.set_view_mode(DirectoryView::ViewMode::List);
view_as_table_action->set_checked(true);
config->write_entry("DirectoryView", "ViewMode", "List");
config->sync();
},
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&) {
directory_view.set_view_mode(DirectoryView::ViewMode::Icon);
view_as_icons_action->set_checked(true);
config->write_entry("DirectoryView", "ViewMode", "Icon");
config->sync();
},
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&) {
directory_view.set_view_mode(DirectoryView::ViewMode::Columns);
view_as_columns_action->set_checked(true);
config->write_entry("DirectoryView", "ViewMode", "Columns");
config->sync();
},
window);
view_as_columns_action->set_checkable(true);
auto view_type_action_group = make<GUI::ActionGroup>();
view_type_action_group->set_exclusive(true);

View file

@ -26,10 +26,10 @@
#include "EllipseTool.h"
#include "PaintableWidget.h"
#include <LibGfx/Rect.h>
#include <LibGUI/Action.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Rect.h>
#include <LibM/math.h>
EllipseTool::EllipseTool()
@ -117,11 +117,9 @@ void EllipseTool::on_contextmenu(GUI::ContextMenuEvent& event)
m_context_menu->add_separator();
m_thickness_actions.set_exclusive(true);
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;
action.set_checked(true);
});
action->set_checkable(true);
action->set_checked(checked);
m_thickness_actions.add_action(*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) {
m_context_menu = GUI::Menu::construct();
NonnullRefPtr<GUI::Action> eraser_color_toggler = GUI::Action::create("Use secondary color", [&](GUI::Action& action) {
bool toggled = !m_use_secondary_color;
m_use_secondary_color = toggled;
action.set_checked(toggled);
auto eraser_color_toggler = GUI::Action::create_checkable("Use secondary color", [&](auto& action) {
m_use_secondary_color = action.is_checked();
});
eraser_color_toggler->set_checkable(true);
eraser_color_toggler->set_checked(m_use_secondary_color);
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);
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;
action.set_checked(true);
});
action->set_checkable(true);
action->set_checked(checked);
m_thickness_actions.add_action(*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_thickness_actions.set_exclusive(true);
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;
action.set_checked(true);
});
action->set_checkable(true);
action->set_checked(checked);
m_thickness_actions.add_action(*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_thickness_actions.set_exclusive(true);
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;
action.set_checked(true);
});
action->set_checkable(true);
action->set_checked(checked);
m_thickness_actions.add_action(*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_thickness_actions.set_exclusive(true);
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;
action.set_checked(true);
});
action->set_checkable(true);
action->set_checked(checked);
m_thickness_actions.add_action(*action);
m_context_menu->add_action(move(action));

View file

@ -74,11 +74,9 @@ int main(int argc, char** argv)
player.manager().play();
}
auto hide_scope = GUI::Action::create("Hide scope", { Mod_Ctrl, Key_H }, [&](GUI::Action& action) {
action.set_checked(!action.is_checked());
auto hide_scope = GUI::Action::create_checkable("Hide scope", { Mod_Ctrl, Key_H }, [&](auto& action) {
player.hide_scope(action.is_checked());
});
hide_scope->set_checkable(true);
app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
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);
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);
action.set_checked(true);
});
action->set_checkable(true);
action->set_checked(checked);
frequency_action_group.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);
auto& font_menu = menubar->add_menu("Font");
GUI::FontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
auto action = GUI::Action::create(font_name, [&](GUI::Action& action) {
action.set_checked(true);
auto action = GUI::Action::create_checkable(font_name, [&](auto& action) {
terminal.set_font(GUI::FontDatabase::the().get_by_name(action.text()));
auto metadata = GUI::FontDatabase::the().get_metadata_by_name(action.text());
ASSERT(metadata.has_value());
@ -288,7 +287,6 @@ int main(int argc, char** argv)
terminal.force_repaint();
});
font_action_group.add_action(*action);
action->set_checkable(true);
if (terminal.font().name() == font_name)
action->set_checked(true);
font_menu.add_action(*action);

View file

@ -334,11 +334,9 @@ TextEditorWidget::TextEditorWidget()
m_save_as_action->activate();
});
m_line_wrapping_setting_action = GUI::Action::create("Line wrapping", [&](GUI::Action& action) {
action.set_checked(!action.is_checked());
m_line_wrapping_setting_action = GUI::Action::create_checkable("Line wrapping", [&](auto& action) {
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());
auto menubar = GUI::MenuBar::construct();
@ -382,31 +380,25 @@ TextEditorWidget::TextEditorWidget()
syntax_actions.set_exclusive(true);
auto& syntax_menu = menubar->add_menu("Syntax");
m_plain_text_highlight = GUI::Action::create("Plain Text", [&](GUI::Action& action) {
action.set_checked(true);
m_plain_text_highlight = GUI::Action::create_checkable("Plain text", [&](auto&) {
m_editor->set_syntax_highlighter(nullptr);
m_editor->update();
});
m_plain_text_highlight->set_checkable(true);
m_plain_text_highlight->set_checked(true);
syntax_actions.add_action(*m_plain_text_highlight);
syntax_menu.add_action(*m_plain_text_highlight);
m_cpp_highlight = GUI::Action::create("C++", [&](GUI::Action& action) {
action.set_checked(true);
m_cpp_highlight = GUI::Action::create_checkable("C++", [&](auto&) {
m_editor->set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
m_editor->update();
});
m_cpp_highlight->set_checkable(true);
syntax_actions.add_action(*m_cpp_highlight);
syntax_menu.add_action(*m_cpp_highlight);
m_js_highlight = GUI::Action::create("Javascript", [&](GUI::Action& action) {
action.set_checked(true);
m_js_highlight = GUI::Action::create_checkable("JavaScript", [&](auto&) {
m_editor->set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
m_editor->update();
});
m_js_highlight->set_checkable(true);
syntax_actions.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;
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));
});
cursor_tool_action->set_checkable(true);
cursor_tool_action->set_checked(true);
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) {
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));
auto widget = reg.construct();
g_form_editor_widget->form_widget().add_child(widget);
widget->set_relative_rect(30, 30, 30, 30);
g_form_editor_widget->model().update();
});
action->set_checkable(true);
action->set_checked(false);
tool_actions.add_action(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(); }));
auto& view_menu = menubar->add_menu("View");
auto invert_action = GUI::Action::create("Invert tree", { Mod_Ctrl, Key_I }, [&](auto& action) {
action.set_checked(!action.is_checked());
auto invert_action = GUI::Action::create_checkable("Invert tree", { Mod_Ctrl, Key_I }, [&](auto& action) {
profile->set_inverted(action.is_checked());
});
invert_action->set_checkable(true);
invert_action->set_checked(false);
view_menu.add_action(invert_action);
auto percent_action = GUI::Action::create("Show percentages", { Mod_Ctrl, Key_P }, [&](auto& action) {
action.set_checked(!action.is_checked());
auto percent_action = GUI::Action::create_checkable("Show percentages", { Mod_Ctrl, Key_P }, [&](auto& action) {
profile->set_show_percentages(action.is_checked());
tree_view.update();
disassembly_view.update();
});
percent_action->set_checkable(true);
percent_action->set_checked(false);
view_menu.add_action(percent_action);

View file

@ -90,12 +90,9 @@ int main(int argc, char** argv)
app_menu.add_separator();
NonnullRefPtr<GUI::Action> chord_toggler_action = GUI::Action::create("Single-click chording", [&](const GUI::Action&) {
bool toggled = !field.is_single_chording();
field.set_single_chording(toggled);
chord_toggler_action->set_checked(toggled);
auto chord_toggler_action = GUI::Action::create_checkable("Single-click chording", [&](auto& action) {
field.set_single_chording(!action.is_checked());
});
chord_toggler_action->set_checkable(true);
chord_toggler_action->set_checked(field.is_single_chording());
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) {
auto& column_data = this->column_data(column);
auto name = model()->column_name(column);
column_data.visibility_action = Action::create(name, [this, column](Action& action) {
action.set_checked(!action.is_checked());
column_data.visibility_action = Action::create_checkable(name, [this, column](auto& action) {
set_column_hidden(column, !action.is_checked());
});
column_data.visibility_action->set_checkable(true);
column_data.visibility_action->set_checked(true);
m_header_context_menu->add_action(*column_data.visibility_action);

View file

@ -34,109 +34,112 @@ namespace GUI {
namespace CommonActions {
NonnullRefPtr<Action> make_open_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Open...", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_open_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Open...", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Move to front", { Mod_Ctrl | Mod_Shift, Key_Up }, Gfx::Bitmap::load_from_file("/res/icons/16x16/move-to-front.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Move to front", { Mod_Ctrl | Mod_Shift, Key_Up }, Gfx::Bitmap::load_from_file("/res/icons/16x16/move-to-front.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Move to back", { Mod_Ctrl | Mod_Shift, Key_Down }, Gfx::Bitmap::load_from_file("/res/icons/16x16/move-to-back.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Move to back", { Mod_Ctrl | Mod_Shift, Key_Down }, Gfx::Bitmap::load_from_file("/res/icons/16x16/move-to-back.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_undo_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Undo", { Mod_Ctrl, Key_Z }, Gfx::Bitmap::load_from_file("/res/icons/16x16/undo.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_undo_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Undo", { Mod_Ctrl, Key_Z }, Gfx::Bitmap::load_from_file("/res/icons/16x16/undo.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_redo_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Redo", { Mod_Ctrl, Key_Y }, Gfx::Bitmap::load_from_file("/res/icons/16x16/redo.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_redo_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Redo", { Mod_Ctrl, Key_Y }, Gfx::Bitmap::load_from_file("/res/icons/16x16/redo.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_delete_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Delete", { Mod_None, Key_Delete }, Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_delete_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Delete", { Mod_None, Key_Delete }, Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_cut_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Cut", { Mod_Ctrl, Key_X }, Gfx::Bitmap::load_from_file("/res/icons/cut16.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_cut_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Cut", { Mod_Ctrl, Key_X }, Gfx::Bitmap::load_from_file("/res/icons/cut16.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_copy_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Copy", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_copy_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Copy", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_paste_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Paste", { Mod_Ctrl, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/paste16.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_paste_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Paste", { Mod_Ctrl, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/paste16.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Fullscreen", { Mod_None, Key_F11 }, move(callback), parent);
}
NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Fullscreen", { Mod_None, Key_F11 }, move(callback), parent);
}
NonnullRefPtr<Action> make_quit_action(Function<void(Action&)> callback)
{
return Action::create("Quit", { Mod_Alt, Key_F4 }, move(callback));
}
NonnullRefPtr<Action> make_quit_action(Function<void(Action&)> callback)
{
return Action::create("Quit", { Mod_Alt, Key_F4 }, move(callback));
}
NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Go back", { Mod_Alt, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Go back", { Mod_Alt, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Go forward", { Mod_Alt, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Go forward", { Mod_Alt, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Go home", { Mod_Alt, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-home.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Go home", { Mod_Alt, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-home.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_reload_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Reload", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"), move(callback), parent);
}
NonnullRefPtr<Action> make_reload_action(Function<void(Action&)> callback, Core::Object* parent)
{
return Action::create("Reload", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"), move(callback), parent);
}
}
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)
, on_activation(move(on_activation_callback))
, 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)
, on_activation(move(on_activation_callback))
, m_text(text)
, 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(text, shortcut, nullptr, move(on_activation_callback), 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, 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)
, on_activation(move(on_activation_callback))
, m_text(text)
, m_icon(move(icon))
, m_shortcut(shortcut)
, m_checkable(checkable)
{
if (parent && Core::is<Widget>(*parent)) {
m_scope = ShortcutScope::WidgetLocal;
@ -156,10 +159,24 @@ Action::~Action()
void Action::activate(Core::Object* activator)
{
if (!on_activation)
return;
if (activator)
m_activator = activator->make_weak_ptr();
if (on_activation)
on_activation(*this);
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);
m_activator = nullptr;
}

View file

@ -84,6 +84,23 @@ public:
{
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;
String text() const { return m_text; }
@ -122,10 +139,10 @@ public:
private:
virtual bool is_action() const override { return true; }
Action(const StringView& text, Function<void(Action&)> = nullptr, Core::Object* = nullptr);
Action(const StringView& text, const Shortcut&, Function<void(Action&)> = nullptr, Core::Object* = nullptr);
Action(const StringView& text, const Shortcut&, 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);
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, bool checkable = false);
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, bool checkable = false);
template<typename 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()
{
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&) {
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&) {
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
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&) {
set_view_mode(ViewMode::Columns);
m_view_as_columns_action->set_checked(true);
});
m_view_as_columns_action->set_checkable(true);
#endif
m_view_type_action_group = make<ActionGroup>();