Action.cpp 7.5 KB

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