From 8e7c7e0a2a0279764c4aedd59e29943b7935039d Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Mon, 24 Oct 2022 19:04:32 -0500 Subject: [PATCH] LibGUI: Share code for finding an Action by Shortcut This moves logic for finding a shortcut on a Window or Widget to Action::find_action_for_shortcut instead. --- Userland/Libraries/LibGUI/Action.cpp | 13 +++++++++++++ Userland/Libraries/LibGUI/Action.h | 2 ++ Userland/Libraries/LibGUI/Widget.cpp | 10 +--------- Userland/Libraries/LibGUI/Window.cpp | 10 +--------- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Userland/Libraries/LibGUI/Action.cpp b/Userland/Libraries/LibGUI/Action.cpp index 6dbd04616d..1ac2417b75 100644 --- a/Userland/Libraries/LibGUI/Action.cpp +++ b/Userland/Libraries/LibGUI/Action.cpp @@ -63,6 +63,19 @@ NonnullRefPtr Action::create_checkable(String text, Shortcut const& shor return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent, true)); } +RefPtr Action::find_action_for_shortcut(Core::Object& object, Shortcut const& shortcut) +{ + RefPtr found_action = nullptr; + object.for_each_child_of_type([&](auto& action) { + if (action.shortcut() == shortcut || action.alternate_shortcut() == shortcut) { + found_action = &action; + return IterationDecision::Break; + } + return IterationDecision::Continue; + }); + return found_action; +} + Action::Action(String text, Function on_activation_callback, Core::Object* parent, bool checkable) : Action(move(text), Shortcut {}, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable) { diff --git a/Userland/Libraries/LibGUI/Action.h b/Userland/Libraries/LibGUI/Action.h index 398983d99e..87c5762b6b 100644 --- a/Userland/Libraries/LibGUI/Action.h +++ b/Userland/Libraries/LibGUI/Action.h @@ -76,6 +76,8 @@ public: static NonnullRefPtr create_checkable(String text, Shortcut const& shortcut, Function callback, Core::Object* parent = nullptr); static NonnullRefPtr create_checkable(String text, Shortcut const& shortcut, RefPtr icon, Function callback, Core::Object* parent = nullptr); + static RefPtr find_action_for_shortcut(Core::Object& object, Shortcut const& shortcut); + virtual ~Action() override; String text() const { return m_text; } diff --git a/Userland/Libraries/LibGUI/Widget.cpp b/Userland/Libraries/LibGUI/Widget.cpp index cc16c057f2..1def39c505 100644 --- a/Userland/Libraries/LibGUI/Widget.cpp +++ b/Userland/Libraries/LibGUI/Widget.cpp @@ -957,15 +957,7 @@ bool Widget::is_backmost() const Action* Widget::action_for_shortcut(Shortcut const& shortcut) { - Action* found_action = nullptr; - for_each_child_of_type([&](auto& action) { - if (action.shortcut() == shortcut || action.alternate_shortcut() == shortcut) { - found_action = &action; - return IterationDecision::Break; - } - return IterationDecision::Continue; - }); - return found_action; + return Action::find_action_for_shortcut(*this, shortcut); } void Widget::set_updates_enabled(bool enabled) diff --git a/Userland/Libraries/LibGUI/Window.cpp b/Userland/Libraries/LibGUI/Window.cpp index 9f6e671a1c..9d930f1cc2 100644 --- a/Userland/Libraries/LibGUI/Window.cpp +++ b/Userland/Libraries/LibGUI/Window.cpp @@ -1131,15 +1131,7 @@ void Window::notify_input_preempted(Badge, InputPreemp Action* Window::action_for_shortcut(Shortcut const& shortcut) { - Action* found_action = nullptr; - for_each_child_of_type([&](auto& action) { - if (action.shortcut() == shortcut || action.alternate_shortcut() == shortcut) { - found_action = &action; - return IterationDecision::Break; - } - return IterationDecision::Continue; - }); - return found_action; + return Action::find_action_for_shortcut(*this, shortcut); } void Window::set_base_size(Gfx::IntSize const& base_size)