mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 17:47:36 +00:00
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:
parent
1032ae0140
commit
705cee528a
18 changed files with 135 additions and 158 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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...");
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue