1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:48:12 +00:00

LibGUI: Allow GActions to be scoped either globally or widget-locally.

This makes it possible for e.g GTextEditor to create a bunch of actions
with popular shortcuts like Ctrl+C, etc, without polluting the global
shortcut namespace. Widget-local actions will only activate while their
corresponding widget has focus. :^)
This commit is contained in:
Andreas Kling 2019-04-20 21:56:56 +02:00
parent a56e1afb64
commit 5c5ce4f885
8 changed files with 102 additions and 41 deletions

View file

@ -3,44 +3,55 @@
#include <LibGUI/GButton.h>
#include <LibGUI/GMenuItem.h>
GAction::GAction(const String& text, const String& custom_data, Function<void(const GAction&)> on_activation_callback)
GAction::GAction(const String& text, const String& custom_data, Function<void(const GAction&)> on_activation_callback, GWidget* widget)
: on_activation(move(on_activation_callback))
, m_text(text)
, m_custom_data(custom_data)
, m_widget(widget ? widget->make_weak_ptr() : nullptr)
{
}
GAction::GAction(const String& text, Function<void(const GAction&)> on_activation_callback)
: GAction(text, String(), move(on_activation_callback))
GAction::GAction(const String& text, Function<void(const GAction&)> on_activation_callback, GWidget* widget)
: GAction(text, String(), move(on_activation_callback), widget)
{
}
GAction::GAction(const String& text, RetainPtr<GraphicsBitmap>&& icon, Function<void(const GAction&)> on_activation_callback)
GAction::GAction(const String& text, RetainPtr<GraphicsBitmap>&& icon, Function<void(const GAction&)> on_activation_callback, GWidget* widget)
: on_activation(move(on_activation_callback))
, m_text(text)
, m_icon(move(icon))
, m_widget(widget ? widget->make_weak_ptr() : nullptr)
{
}
GAction::GAction(const String& text, const GShortcut& shortcut, Function<void(const GAction&)> on_activation_callback)
: GAction(text, shortcut, nullptr, move(on_activation_callback))
GAction::GAction(const String& text, const GShortcut& shortcut, Function<void(const GAction&)> on_activation_callback, GWidget* widget)
: GAction(text, shortcut, nullptr, move(on_activation_callback), widget)
{
}
GAction::GAction(const String& text, const GShortcut& shortcut, RetainPtr<GraphicsBitmap>&& icon, Function<void(const GAction&)> on_activation_callback)
GAction::GAction(const String& text, const GShortcut& shortcut, RetainPtr<GraphicsBitmap>&& icon, Function<void(const GAction&)> on_activation_callback, GWidget* widget)
: on_activation(move(on_activation_callback))
, m_text(text)
, m_icon(move(icon))
, m_shortcut(shortcut)
, m_widget(widget ? widget->make_weak_ptr() : nullptr)
{
GApplication::the().register_shortcut_action(Badge<GAction>(), *this);
if (m_widget) {
m_scope = ShortcutScope::WidgetLocal;
m_widget->register_local_shortcut_action(Badge<GAction>(), *this);
} else {
m_scope = ShortcutScope::ApplicationGlobal;
GApplication::the().register_global_shortcut_action(Badge<GAction>(), *this);
}
}
GAction::~GAction()
{
if (m_shortcut.is_valid())
GApplication::the().unregister_shortcut_action(Badge<GAction>(), *this);
if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal)
GApplication::the().unregister_global_shortcut_action(Badge<GAction>(), *this);
if (m_widget && m_scope == ShortcutScope::WidgetLocal)
m_widget->unregister_local_shortcut_action(Badge<GAction>(), *this);
}
void GAction::activate()