mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:17:45 +00:00
LibGUI: Allow finding the source of a GAction activation
When a GAction is activated by a menu, or by a toolbar button, you can now use GAction::activator() to get a pointer to whomever activated it. This can be used to implement context-specific behaviors in situations where the same action is exposed through multiple paths. This addresses an issue that was brought up in #826.
This commit is contained in:
parent
fd5eb79d19
commit
571c4d3fb8
4 changed files with 11 additions and 4 deletions
|
@ -127,10 +127,13 @@ GAction::~GAction()
|
||||||
m_widget->unregister_local_shortcut_action({}, *this);
|
m_widget->unregister_local_shortcut_action({}, *this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GAction::activate()
|
void GAction::activate(CObject* activator)
|
||||||
{
|
{
|
||||||
|
if (activator)
|
||||||
|
m_activator = activator->make_weak_ptr();
|
||||||
if (on_activation)
|
if (on_activation)
|
||||||
on_activation(*this);
|
on_activation(*this);
|
||||||
|
m_activator = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GAction::register_button(Badge<GButton>, GButton& button)
|
void GAction::register_button(Badge<GButton>, GButton& button)
|
||||||
|
|
|
@ -69,9 +69,12 @@ public:
|
||||||
const GraphicsBitmap* icon() const { return m_icon.ptr(); }
|
const GraphicsBitmap* icon() const { return m_icon.ptr(); }
|
||||||
void set_icon(const GraphicsBitmap* icon) { m_icon = icon; }
|
void set_icon(const GraphicsBitmap* icon) { m_icon = icon; }
|
||||||
|
|
||||||
|
const CObject* activator() const { return m_activator.ptr(); }
|
||||||
|
CObject* activator() { return m_activator.ptr(); }
|
||||||
|
|
||||||
Function<void(GAction&)> on_activation;
|
Function<void(GAction&)> on_activation;
|
||||||
|
|
||||||
void activate();
|
void activate(CObject* activator = nullptr);
|
||||||
|
|
||||||
bool is_enabled() const { return m_enabled; }
|
bool is_enabled() const { return m_enabled; }
|
||||||
void set_enabled(bool);
|
void set_enabled(bool);
|
||||||
|
@ -117,4 +120,5 @@ private:
|
||||||
HashTable<GMenuItem*> m_menu_items;
|
HashTable<GMenuItem*> m_menu_items;
|
||||||
WeakPtr<GWidget> m_widget;
|
WeakPtr<GWidget> m_widget;
|
||||||
WeakPtr<GActionGroup> m_action_group;
|
WeakPtr<GActionGroup> m_action_group;
|
||||||
|
WeakPtr<CObject> m_activator;
|
||||||
};
|
};
|
||||||
|
|
|
@ -69,7 +69,7 @@ void GButton::click()
|
||||||
if (on_click)
|
if (on_click)
|
||||||
on_click(*this);
|
on_click(*this);
|
||||||
if (m_action)
|
if (m_action)
|
||||||
m_action->activate();
|
m_action->activate(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GButton::supports_keyboard_activation() const
|
bool GButton::supports_keyboard_activation() const
|
||||||
|
|
|
@ -209,7 +209,7 @@ void GWindowServerConnection::handle(const WindowClient::MenuItemActivated& mess
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (auto* action = menu->action_at(message.identifier()))
|
if (auto* action = menu->action_at(message.identifier()))
|
||||||
action->activate();
|
action->activate(menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GWindowServerConnection::handle(const WindowClient::WM_WindowStateChanged& message)
|
void GWindowServerConnection::handle(const WindowClient::WM_WindowStateChanged& message)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue