mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:57:46 +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
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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>();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue