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