Action.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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::flash_menubar_menu(GUI::Window& window)
  116. {
  117. for (auto& menu_item : m_menu_items)
  118. window.flash_menubar_menu_for(*menu_item);
  119. }
  120. void Action::register_button(Badge<Button>, Button& button)
  121. {
  122. m_buttons.set(&button);
  123. }
  124. void Action::unregister_button(Badge<Button>, Button& button)
  125. {
  126. m_buttons.remove(&button);
  127. }
  128. void Action::register_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  129. {
  130. m_menu_items.set(&menu_item);
  131. }
  132. void Action::unregister_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  133. {
  134. m_menu_items.remove(&menu_item);
  135. }
  136. template<typename Callback>
  137. void Action::for_each_toolbar_button(Callback callback)
  138. {
  139. for (auto& it : m_buttons)
  140. callback(*it);
  141. }
  142. template<typename Callback>
  143. void Action::for_each_menu_item(Callback callback)
  144. {
  145. for (auto& it : m_menu_items)
  146. callback(*it);
  147. }
  148. void Action::set_enabled(bool enabled)
  149. {
  150. if (m_enabled == enabled)
  151. return;
  152. m_enabled = enabled;
  153. for_each_toolbar_button([enabled](auto& button) {
  154. button.set_enabled(enabled);
  155. });
  156. for_each_menu_item([enabled](auto& item) {
  157. item.set_enabled(enabled);
  158. });
  159. }
  160. void Action::set_checked(bool checked)
  161. {
  162. if (m_checked == checked)
  163. return;
  164. m_checked = checked;
  165. if (m_checked && m_action_group) {
  166. m_action_group->for_each_action([this](auto& other_action) {
  167. if (this == &other_action)
  168. return IterationDecision::Continue;
  169. if (other_action.is_checkable())
  170. other_action.set_checked(false);
  171. return IterationDecision::Continue;
  172. });
  173. }
  174. for_each_toolbar_button([checked](auto& button) {
  175. button.set_checked(checked);
  176. });
  177. for_each_menu_item([checked](MenuItem& item) {
  178. item.set_checked(checked);
  179. });
  180. }
  181. void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
  182. {
  183. m_action_group = AK::try_make_weak_ptr(group);
  184. }
  185. void Action::set_icon(const Gfx::Bitmap* icon)
  186. {
  187. m_icon = icon;
  188. }
  189. void Action::set_text(String text)
  190. {
  191. if (m_text == text)
  192. return;
  193. m_text = move(text);
  194. for_each_menu_item([&](auto& menu_item) {
  195. menu_item.update_from_action({});
  196. });
  197. }
  198. }