From deb31645aa25db3cfd6b490a76ff7fdc900602ba Mon Sep 17 00:00:00 2001 From: rhin123 Date: Sun, 1 Sep 2019 15:28:07 -0500 Subject: [PATCH] GAction: Added GCommonActions as a template to create standard actions Instead of creating actions from the ground up, GCommonActions contains all related information to that common action. Such as the icon, keybind, ect. --- Libraries/LibGUI/GAction.cpp | 34 ++++++++++++++++++++++++++++++++++ Libraries/LibGUI/GAction.h | 14 +++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Libraries/LibGUI/GAction.cpp b/Libraries/LibGUI/GAction.cpp index b655c67f58..40228d9ba5 100644 --- a/Libraries/LibGUI/GAction.cpp +++ b/Libraries/LibGUI/GAction.cpp @@ -4,6 +4,40 @@ #include #include +NonnullRefPtr GCommonActions::make_cut_action(Function callback, GWidget* widget) +{ + return GAction::create( + "Cut", { Mod_Ctrl, Key_X }, GraphicsBitmap::load_from_file("/res/icons/cut16.png"), [callback = move(callback)](const GAction&) { + callback(); + }, + widget); +} + +NonnullRefPtr GCommonActions::make_copy_action(Function callback, GWidget* widget) +{ + return GAction::create( + "Copy", { Mod_Ctrl, Key_C }, GraphicsBitmap::load_from_file("/res/icons/16x16/edit-copy.png"), [callback = move(callback)](const GAction&) { + callback(); + }, + widget); +} + +NonnullRefPtr GCommonActions::make_paste_action(Function callback, GWidget* widget) +{ + return GAction::create( + "Paste", { Mod_Ctrl, Key_V }, GraphicsBitmap::load_from_file("/res/icons/paste16.png"), [callback = move(callback)](const GAction&) { + callback(); + }, + widget); +} + +NonnullRefPtr GCommonActions::make_quit_action(Function callback) +{ + return GAction::create("Quit", { Mod_Alt, Key_F4 }, [callback = move(callback)](const GAction&) { + callback(); + }); +} + GAction::GAction(const StringView& text, Function on_activation_callback, GWidget* widget) : on_activation(move(on_activation_callback)) , m_text(text) diff --git a/Libraries/LibGUI/GAction.h b/Libraries/LibGUI/GAction.h index c294d65de9..08be487d39 100644 --- a/Libraries/LibGUI/GAction.h +++ b/Libraries/LibGUI/GAction.h @@ -4,18 +4,27 @@ #include #include #include -#include #include +#include #include #include -#include #include +#include +#include +class GAction; class GActionGroup; class GButton; class GMenuItem; class GWidget; +namespace GCommonActions { +NonnullRefPtr make_cut_action(Function, GWidget* widget); +NonnullRefPtr make_copy_action(Function, GWidget* widget); +NonnullRefPtr make_paste_action(Function, GWidget* widget); +NonnullRefPtr make_quit_action(Function); +}; + class GAction : public RefCounted , public Weakable { public: @@ -41,7 +50,6 @@ public: return adopt(*new GAction(text, shortcut, move(icon), move(callback), widget)); } ~GAction(); - GWidget* widget() { return m_widget.ptr(); } const GWidget* widget() const { return m_widget.ptr(); }