2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
#include <LibGUI/ActionGroup.h>
|
|
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
|
#include <LibGUI/MenuItem.h>
|
2020-07-23 19:52:18 +02:00
|
|
|
#include <LibGUI/Window.h>
|
2019-02-12 14:09:48 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
2019-09-14 22:10:44 +02:00
|
|
|
|
2021-02-20 12:45:55 +01:00
|
|
|
NonnullRefPtr<Action> Action::create(String text, Function<void(Action&)> callback, Core::Object* parent)
|
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*new Action(move(text), move(callback), parent));
|
2021-02-20 12:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NonnullRefPtr<Action> Action::create(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
|
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*new Action(move(text), move(icon), move(callback), parent));
|
2021-02-20 12:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
NonnullRefPtr<Action> Action::create(String text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::Object* parent)
|
2021-02-20 12:45:55 +01:00
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*new Action(move(text), shortcut, move(callback), parent));
|
2021-02-20 12:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
NonnullRefPtr<Action> Action::create(String text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, Function<void(Action&)> callback, Core::Object* parent)
|
2021-06-24 11:53:47 +03:00
|
|
|
{
|
|
|
|
|
return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(callback), parent));
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
NonnullRefPtr<Action> Action::create(String text, Shortcut const& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
|
2021-02-20 12:45:55 +01:00
|
|
|
{
|
2021-06-24 11:53:47 +03:00
|
|
|
return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent));
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
NonnullRefPtr<Action> Action::create(String text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
|
2021-06-24 11:53:47 +03:00
|
|
|
{
|
|
|
|
|
return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(icon), move(callback), parent));
|
2021-02-20 12:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NonnullRefPtr<Action> Action::create_checkable(String text, Function<void(Action&)> callback, Core::Object* parent)
|
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*new Action(move(text), move(callback), parent, true));
|
2021-02-20 12:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NonnullRefPtr<Action> Action::create_checkable(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
|
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*new Action(move(text), move(icon), move(callback), parent, true));
|
2021-02-20 12:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
NonnullRefPtr<Action> Action::create_checkable(String text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::Object* parent)
|
2021-02-20 12:45:55 +01:00
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*new Action(move(text), shortcut, move(callback), parent, true));
|
2021-02-20 12:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
NonnullRefPtr<Action> Action::create_checkable(String text, Shortcut const& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
|
2021-02-20 12:45:55 +01:00
|
|
|
{
|
2021-06-24 11:53:47 +03:00
|
|
|
return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent, true));
|
2021-02-20 12:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-10-24 19:04:32 -05:00
|
|
|
RefPtr<Action> Action::find_action_for_shortcut(Core::Object& object, Shortcut const& shortcut)
|
|
|
|
|
{
|
|
|
|
|
RefPtr<Action> found_action = nullptr;
|
|
|
|
|
object.for_each_child_of_type<Action>([&](auto& action) {
|
|
|
|
|
if (action.shortcut() == shortcut || action.alternate_shortcut() == shortcut) {
|
|
|
|
|
found_action = &action;
|
|
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
return found_action;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 12:45:55 +01:00
|
|
|
Action::Action(String text, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
|
2021-06-24 11:53:47 +03:00
|
|
|
: Action(move(text), Shortcut {}, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
|
2019-02-12 14:09:48 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 12:45:55 +01:00
|
|
|
Action::Action(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
|
2021-06-24 11:53:47 +03:00
|
|
|
: Action(move(text), Shortcut {}, Shortcut {}, move(icon), move(on_activation_callback), parent, checkable)
|
2019-02-20 02:39:46 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Action::Action(String text, Shortcut const& shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
|
2021-06-24 11:53:47 +03:00
|
|
|
: Action(move(text), shortcut, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Action::Action(String text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
|
2021-06-24 11:53:47 +03:00
|
|
|
: Action(move(text), shortcut, alternate_shortcut, nullptr, move(on_activation_callback), parent, checkable)
|
2019-03-03 02:52:22 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Action::Action(String text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
|
2020-02-02 12:34:39 +01:00
|
|
|
: Core::Object(parent)
|
2020-02-02 01:57:57 +01:00
|
|
|
, on_activation(move(on_activation_callback))
|
2021-02-20 12:45:55 +01:00
|
|
|
, m_text(move(text))
|
2019-03-02 10:04:49 +01:00
|
|
|
, m_icon(move(icon))
|
|
|
|
|
, m_shortcut(shortcut)
|
2021-06-24 11:53:47 +03:00
|
|
|
, m_alternate_shortcut(alternate_shortcut)
|
2020-04-21 17:19:27 +02:00
|
|
|
, m_checkable(checkable)
|
2019-03-02 10:04:49 +01:00
|
|
|
{
|
2020-07-26 17:16:35 +02:00
|
|
|
if (parent && is<Widget>(*parent)) {
|
2019-04-20 21:56:56 +02:00
|
|
|
m_scope = ShortcutScope::WidgetLocal;
|
2020-07-26 17:16:35 +02:00
|
|
|
} else if (parent && is<Window>(*parent)) {
|
2020-02-02 01:57:57 +01:00
|
|
|
m_scope = ShortcutScope::WindowLocal;
|
2019-04-20 21:56:56 +02:00
|
|
|
} else {
|
|
|
|
|
m_scope = ShortcutScope::ApplicationGlobal;
|
2020-07-23 19:52:18 +02:00
|
|
|
if (auto* app = Application::the()) {
|
2020-07-04 16:52:01 +02:00
|
|
|
app->register_global_shortcut_action({}, *this);
|
2020-07-23 19:52:18 +02:00
|
|
|
}
|
2019-04-20 21:56:56 +02:00
|
|
|
}
|
2019-03-02 10:04:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Action::~Action()
|
2019-02-12 14:09:48 +01:00
|
|
|
{
|
2020-07-04 16:52:01 +02:00
|
|
|
if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) {
|
|
|
|
|
if (auto* app = Application::the())
|
|
|
|
|
app->unregister_global_shortcut_action({}, *this);
|
|
|
|
|
}
|
2019-02-12 14:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::activate(Core::Object* activator)
|
2019-02-12 14:09:48 +01:00
|
|
|
{
|
2020-04-21 17:19:27 +02:00
|
|
|
if (!on_activation)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-12-09 21:25:48 +01:00
|
|
|
if (activator)
|
|
|
|
|
m_activator = activator->make_weak_ptr();
|
2020-04-21 17:19:27 +02:00
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 15:50:32 +10:00
|
|
|
if (activator == nullptr) {
|
|
|
|
|
for_each_toolbar_button([](auto& button) {
|
|
|
|
|
button.set_mimic_pressed(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 17:19:27 +02:00
|
|
|
on_activation(*this);
|
2019-12-09 21:25:48 +01:00
|
|
|
m_activator = nullptr;
|
2019-02-12 14:09:48 +01:00
|
|
|
}
|
2019-04-12 02:53:27 +02:00
|
|
|
|
2022-01-29 16:13:42 +01:00
|
|
|
void Action::flash_menubar_menu(GUI::Window& window)
|
2021-10-03 12:33:08 +02:00
|
|
|
{
|
2022-01-29 16:13:42 +01:00
|
|
|
for (auto& menu_item : m_menu_items)
|
|
|
|
|
window.flash_menubar_menu_for(*menu_item);
|
2021-10-03 12:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::register_button(Badge<Button>, Button& button)
|
2019-04-12 02:53:27 +02:00
|
|
|
{
|
|
|
|
|
m_buttons.set(&button);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::unregister_button(Badge<Button>, Button& button)
|
2019-04-12 02:53:27 +02:00
|
|
|
{
|
|
|
|
|
m_buttons.remove(&button);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::register_menu_item(Badge<MenuItem>, MenuItem& menu_item)
|
2019-04-12 02:53:27 +02:00
|
|
|
{
|
|
|
|
|
m_menu_items.set(&menu_item);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::unregister_menu_item(Badge<MenuItem>, MenuItem& menu_item)
|
2019-04-12 02:53:27 +02:00
|
|
|
{
|
|
|
|
|
m_menu_items.remove(&menu_item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Callback>
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::for_each_toolbar_button(Callback callback)
|
2019-04-12 02:53:27 +02:00
|
|
|
{
|
|
|
|
|
for (auto& it : m_buttons)
|
|
|
|
|
callback(*it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Callback>
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::for_each_menu_item(Callback callback)
|
2019-04-12 02:53:27 +02:00
|
|
|
{
|
|
|
|
|
for (auto& it : m_menu_items)
|
|
|
|
|
callback(*it);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::set_enabled(bool enabled)
|
2019-04-12 02:53:27 +02:00
|
|
|
{
|
|
|
|
|
if (m_enabled == enabled)
|
|
|
|
|
return;
|
|
|
|
|
m_enabled = enabled;
|
2020-02-02 15:07:41 +01:00
|
|
|
for_each_toolbar_button([enabled](auto& button) {
|
2019-04-12 02:53:27 +02:00
|
|
|
button.set_enabled(enabled);
|
|
|
|
|
});
|
2020-02-02 15:07:41 +01:00
|
|
|
for_each_menu_item([enabled](auto& item) {
|
2019-04-12 02:53:27 +02:00
|
|
|
item.set_enabled(enabled);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-04-26 21:09:56 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::set_checked(bool checked)
|
2019-04-26 21:09:56 +02:00
|
|
|
{
|
|
|
|
|
if (m_checked == checked)
|
|
|
|
|
return;
|
|
|
|
|
m_checked = checked;
|
2019-07-09 22:10:03 +02:00
|
|
|
|
|
|
|
|
if (m_checked && m_action_group) {
|
|
|
|
|
m_action_group->for_each_action([this](auto& other_action) {
|
|
|
|
|
if (this == &other_action)
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
if (other_action.is_checkable())
|
|
|
|
|
other_action.set_checked(false);
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
for_each_toolbar_button([checked](auto& button) {
|
2019-04-26 21:09:56 +02:00
|
|
|
button.set_checked(checked);
|
|
|
|
|
});
|
2020-02-02 15:07:41 +01:00
|
|
|
for_each_menu_item([checked](MenuItem& item) {
|
2019-04-26 21:09:56 +02:00
|
|
|
item.set_checked(checked);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-07-09 22:10:03 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
|
2019-07-09 22:10:03 +02:00
|
|
|
{
|
2022-02-13 20:47:36 +02:00
|
|
|
m_action_group = AK::make_weak_ptr_if_nonnull(group);
|
2019-07-09 22:10:03 +02:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void Action::set_icon(Gfx::Bitmap const* icon)
|
2020-02-14 23:02:47 +01:00
|
|
|
{
|
2022-06-01 08:49:01 +02:00
|
|
|
if (m_icon == icon)
|
|
|
|
|
return;
|
2020-02-14 23:02:47 +01:00
|
|
|
m_icon = icon;
|
2022-06-01 08:49:01 +02:00
|
|
|
for_each_toolbar_button([icon](auto& button) {
|
|
|
|
|
button.set_icon(icon);
|
|
|
|
|
});
|
|
|
|
|
for_each_menu_item([](auto& menu_item) {
|
|
|
|
|
menu_item.update_from_action({});
|
|
|
|
|
});
|
2020-02-14 23:02:47 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-08 21:15:38 +02:00
|
|
|
void Action::set_text(String text)
|
|
|
|
|
{
|
|
|
|
|
if (m_text == text)
|
|
|
|
|
return;
|
|
|
|
|
m_text = move(text);
|
2022-10-30 04:32:51 -05:00
|
|
|
for_each_toolbar_button([&](auto& button) {
|
|
|
|
|
button.set_text_from_action();
|
|
|
|
|
});
|
2021-05-08 21:15:38 +02:00
|
|
|
for_each_menu_item([&](auto& menu_item) {
|
|
|
|
|
menu_item.update_from_action({});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|