Action.cpp 7.4 KB

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