Action.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Action.h>
  7. #include <LibGUI/ActionGroup.h>
  8. #include <LibGUI/Application.h>
  9. #include <LibGUI/Button.h>
  10. #include <LibGUI/MenuItem.h>
  11. #include <LibGUI/Window.h>
  12. namespace GUI {
  13. NonnullRefPtr<Action> Action::create(String text, Function<void(Action&)> callback, Core::Object* parent)
  14. {
  15. return adopt_ref(*new Action(move(text), move(callback), parent));
  16. }
  17. NonnullRefPtr<Action> Action::create(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  18. {
  19. return adopt_ref(*new Action(move(text), move(icon), move(callback), parent));
  20. }
  21. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent)
  22. {
  23. return adopt_ref(*new Action(move(text), shortcut, move(callback), parent));
  24. }
  25. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, Function<void(Action&)> callback, Core::Object* parent)
  26. {
  27. return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(callback), parent));
  28. }
  29. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  30. {
  31. return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent));
  32. }
  33. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  34. {
  35. return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(icon), move(callback), parent));
  36. }
  37. NonnullRefPtr<Action> Action::create_checkable(String text, Function<void(Action&)> callback, Core::Object* parent)
  38. {
  39. return adopt_ref(*new Action(move(text), move(callback), parent, true));
  40. }
  41. NonnullRefPtr<Action> Action::create_checkable(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  42. {
  43. return adopt_ref(*new Action(move(text), move(icon), move(callback), parent, true));
  44. }
  45. NonnullRefPtr<Action> Action::create_checkable(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent)
  46. {
  47. return adopt_ref(*new Action(move(text), shortcut, move(callback), parent, true));
  48. }
  49. NonnullRefPtr<Action> Action::create_checkable(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  50. {
  51. return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent, true));
  52. }
  53. Action::Action(String text, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  54. : Action(move(text), Shortcut {}, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
  55. {
  56. }
  57. Action::Action(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  58. : Action(move(text), Shortcut {}, Shortcut {}, move(icon), move(on_activation_callback), parent, checkable)
  59. {
  60. }
  61. Action::Action(String text, const Shortcut& shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  62. : Action(move(text), shortcut, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
  63. {
  64. }
  65. Action::Action(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  66. : Action(move(text), shortcut, alternate_shortcut, nullptr, move(on_activation_callback), parent, checkable)
  67. {
  68. }
  69. Action::Action(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  70. : Core::Object(parent)
  71. , on_activation(move(on_activation_callback))
  72. , m_text(move(text))
  73. , m_icon(move(icon))
  74. , m_shortcut(shortcut)
  75. , m_alternate_shortcut(alternate_shortcut)
  76. , m_checkable(checkable)
  77. {
  78. if (parent && is<Widget>(*parent)) {
  79. m_scope = ShortcutScope::WidgetLocal;
  80. } else if (parent && is<Window>(*parent)) {
  81. m_scope = ShortcutScope::WindowLocal;
  82. } else {
  83. m_scope = ShortcutScope::ApplicationGlobal;
  84. if (auto* app = Application::the()) {
  85. app->register_global_shortcut_action({}, *this);
  86. }
  87. }
  88. }
  89. Action::~Action()
  90. {
  91. if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) {
  92. if (auto* app = Application::the())
  93. app->unregister_global_shortcut_action({}, *this);
  94. }
  95. }
  96. void Action::activate(Core::Object* activator)
  97. {
  98. if (!on_activation)
  99. return;
  100. if (activator)
  101. m_activator = activator->make_weak_ptr();
  102. if (is_checkable()) {
  103. if (m_action_group) {
  104. if (m_action_group->is_unchecking_allowed())
  105. set_checked(!is_checked());
  106. else
  107. set_checked(true);
  108. } else {
  109. set_checked(!is_checked());
  110. }
  111. }
  112. on_activation(*this);
  113. m_activator = nullptr;
  114. }
  115. void Action::register_button(Badge<Button>, Button& button)
  116. {
  117. m_buttons.set(&button);
  118. }
  119. void Action::unregister_button(Badge<Button>, Button& button)
  120. {
  121. m_buttons.remove(&button);
  122. }
  123. void Action::register_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  124. {
  125. m_menu_items.set(&menu_item);
  126. }
  127. void Action::unregister_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  128. {
  129. m_menu_items.remove(&menu_item);
  130. }
  131. template<typename Callback>
  132. void Action::for_each_toolbar_button(Callback callback)
  133. {
  134. for (auto& it : m_buttons)
  135. callback(*it);
  136. }
  137. template<typename Callback>
  138. void Action::for_each_menu_item(Callback callback)
  139. {
  140. for (auto& it : m_menu_items)
  141. callback(*it);
  142. }
  143. void Action::set_enabled(bool enabled)
  144. {
  145. if (m_enabled == enabled)
  146. return;
  147. m_enabled = enabled;
  148. for_each_toolbar_button([enabled](auto& button) {
  149. button.set_enabled(enabled);
  150. });
  151. for_each_menu_item([enabled](auto& item) {
  152. item.set_enabled(enabled);
  153. });
  154. }
  155. void Action::set_checked(bool checked)
  156. {
  157. if (m_checked == checked)
  158. return;
  159. m_checked = checked;
  160. if (m_checked && m_action_group) {
  161. m_action_group->for_each_action([this](auto& other_action) {
  162. if (this == &other_action)
  163. return IterationDecision::Continue;
  164. if (other_action.is_checkable())
  165. other_action.set_checked(false);
  166. return IterationDecision::Continue;
  167. });
  168. }
  169. for_each_toolbar_button([checked](auto& button) {
  170. button.set_checked(checked);
  171. });
  172. for_each_menu_item([checked](MenuItem& item) {
  173. item.set_checked(checked);
  174. });
  175. }
  176. void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
  177. {
  178. m_action_group = AK::try_make_weak_ptr(group);
  179. }
  180. void Action::set_icon(const Gfx::Bitmap* icon)
  181. {
  182. m_icon = icon;
  183. }
  184. void Action::set_text(String text)
  185. {
  186. if (m_text == text)
  187. return;
  188. m_text = move(text);
  189. for_each_menu_item([&](auto& menu_item) {
  190. menu_item.update_from_action({});
  191. });
  192. }
  193. }