/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include namespace GUI { NonnullRefPtr Action::create(String text, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), move(callback), parent)); } NonnullRefPtr Action::create(String text, RefPtr icon, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), move(icon), move(callback), parent)); } NonnullRefPtr Action::create(String text, const Shortcut& shortcut, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), shortcut, move(callback), parent)); } NonnullRefPtr Action::create(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(callback), parent)); } NonnullRefPtr Action::create(String text, const Shortcut& shortcut, RefPtr icon, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent)); } NonnullRefPtr Action::create(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, RefPtr icon, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(icon), move(callback), parent)); } NonnullRefPtr Action::create_checkable(String text, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), move(callback), parent, true)); } NonnullRefPtr Action::create_checkable(String text, RefPtr icon, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), move(icon), move(callback), parent, true)); } NonnullRefPtr Action::create_checkable(String text, const Shortcut& shortcut, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), shortcut, move(callback), parent, true)); } NonnullRefPtr Action::create_checkable(String text, const Shortcut& shortcut, RefPtr icon, Function callback, Core::Object* parent) { return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent, true)); } 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) { } Action::Action(String text, RefPtr icon, Function on_activation_callback, Core::Object* parent, bool checkable) : Action(move(text), Shortcut {}, Shortcut {}, move(icon), move(on_activation_callback), parent, checkable) { } Action::Action(String text, const Shortcut& shortcut, Function on_activation_callback, Core::Object* parent, bool checkable) : Action(move(text), shortcut, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable) { } Action::Action(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, Function on_activation_callback, Core::Object* parent, bool checkable) : Action(move(text), shortcut, alternate_shortcut, nullptr, move(on_activation_callback), parent, checkable) { } Action::Action(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, RefPtr icon, Function on_activation_callback, Core::Object* parent, bool checkable) : Core::Object(parent) , on_activation(move(on_activation_callback)) , m_text(move(text)) , m_icon(move(icon)) , m_shortcut(shortcut) , m_alternate_shortcut(alternate_shortcut) , m_checkable(checkable) { if (parent && is(*parent)) { m_scope = ShortcutScope::WidgetLocal; } else if (parent && is(*parent)) { m_scope = ShortcutScope::WindowLocal; } else { m_scope = ShortcutScope::ApplicationGlobal; if (auto* app = Application::the()) { app->register_global_shortcut_action({}, *this); } } } Action::~Action() { if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) { if (auto* app = Application::the()) app->unregister_global_shortcut_action({}, *this); } } void Action::activate(Core::Object* activator) { if (!on_activation) return; if (activator) m_activator = activator->make_weak_ptr(); 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()); } } on_activation(*this); m_activator = nullptr; } void Action::register_button(Badge