Action.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. RefPtr<Action> Action::find_action_for_shortcut(Core::Object& object, Shortcut const& shortcut)
  54. {
  55. RefPtr<Action> found_action = nullptr;
  56. object.for_each_child_of_type<Action>([&](auto& action) {
  57. if (action.shortcut() == shortcut || action.alternate_shortcut() == shortcut) {
  58. found_action = &action;
  59. return IterationDecision::Break;
  60. }
  61. return IterationDecision::Continue;
  62. });
  63. return found_action;
  64. }
  65. Action::Action(String text, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  66. : Action(move(text), Shortcut {}, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
  67. {
  68. }
  69. Action::Action(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  70. : Action(move(text), Shortcut {}, Shortcut {}, move(icon), move(on_activation_callback), parent, checkable)
  71. {
  72. }
  73. Action::Action(String text, Shortcut const& shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  74. : Action(move(text), shortcut, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
  75. {
  76. }
  77. Action::Action(String text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  78. : Action(move(text), shortcut, alternate_shortcut, nullptr, move(on_activation_callback), parent, checkable)
  79. {
  80. }
  81. 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)
  82. : Core::Object(parent)
  83. , on_activation(move(on_activation_callback))
  84. , m_text(move(text))
  85. , m_icon(move(icon))
  86. , m_shortcut(shortcut)
  87. , m_alternate_shortcut(alternate_shortcut)
  88. , m_checkable(checkable)
  89. {
  90. if (parent && is<Widget>(*parent)) {
  91. m_scope = ShortcutScope::WidgetLocal;
  92. } else if (parent && is<Window>(*parent)) {
  93. m_scope = ShortcutScope::WindowLocal;
  94. } else {
  95. m_scope = ShortcutScope::ApplicationGlobal;
  96. if (auto* app = Application::the()) {
  97. app->register_global_shortcut_action({}, *this);
  98. }
  99. }
  100. }
  101. Action::~Action()
  102. {
  103. if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) {
  104. if (auto* app = Application::the())
  105. app->unregister_global_shortcut_action({}, *this);
  106. }
  107. }
  108. void Action::process_event(Window& window, Event& event)
  109. {
  110. if (is_enabled()) {
  111. flash_menubar_menu(window);
  112. activate();
  113. event.accept();
  114. return;
  115. }
  116. if (swallow_key_event_when_disabled()) {
  117. event.accept();
  118. return;
  119. }
  120. event.ignore();
  121. }
  122. void Action::activate(Core::Object* activator)
  123. {
  124. if (!on_activation)
  125. return;
  126. if (activator)
  127. m_activator = activator->make_weak_ptr();
  128. if (is_checkable()) {
  129. if (m_action_group) {
  130. if (m_action_group->is_unchecking_allowed())
  131. set_checked(!is_checked());
  132. else
  133. set_checked(true);
  134. } else {
  135. set_checked(!is_checked());
  136. }
  137. }
  138. if (activator == nullptr) {
  139. for_each_toolbar_button([](auto& button) {
  140. button.set_mimic_pressed(true);
  141. });
  142. }
  143. on_activation(*this);
  144. m_activator = nullptr;
  145. }
  146. void Action::flash_menubar_menu(GUI::Window& window)
  147. {
  148. for (auto& menu_item : m_menu_items)
  149. window.flash_menubar_menu_for(*menu_item);
  150. }
  151. void Action::register_button(Badge<Button>, Button& button)
  152. {
  153. m_buttons.set(&button);
  154. }
  155. void Action::unregister_button(Badge<Button>, Button& button)
  156. {
  157. m_buttons.remove(&button);
  158. }
  159. void Action::register_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  160. {
  161. m_menu_items.set(&menu_item);
  162. }
  163. void Action::unregister_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  164. {
  165. m_menu_items.remove(&menu_item);
  166. }
  167. template<typename Callback>
  168. void Action::for_each_toolbar_button(Callback callback)
  169. {
  170. for (auto& it : m_buttons)
  171. callback(*it);
  172. }
  173. template<typename Callback>
  174. void Action::for_each_menu_item(Callback callback)
  175. {
  176. for (auto& it : m_menu_items)
  177. callback(*it);
  178. }
  179. void Action::set_enabled(bool enabled)
  180. {
  181. if (m_enabled == enabled)
  182. return;
  183. m_enabled = enabled;
  184. for_each_toolbar_button([enabled](auto& button) {
  185. button.set_enabled(enabled);
  186. });
  187. for_each_menu_item([enabled](auto& item) {
  188. item.set_enabled(enabled);
  189. });
  190. }
  191. void Action::set_checked(bool checked)
  192. {
  193. if (m_checked == checked)
  194. return;
  195. m_checked = checked;
  196. if (m_checked && m_action_group) {
  197. m_action_group->for_each_action([this](auto& other_action) {
  198. if (this == &other_action)
  199. return IterationDecision::Continue;
  200. if (other_action.is_checkable())
  201. other_action.set_checked(false);
  202. return IterationDecision::Continue;
  203. });
  204. }
  205. for_each_toolbar_button([checked](auto& button) {
  206. button.set_checked(checked);
  207. });
  208. for_each_menu_item([checked](MenuItem& item) {
  209. item.set_checked(checked);
  210. });
  211. }
  212. void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
  213. {
  214. m_action_group = AK::make_weak_ptr_if_nonnull(group);
  215. }
  216. void Action::set_icon(Gfx::Bitmap const* icon)
  217. {
  218. if (m_icon == icon)
  219. return;
  220. m_icon = icon;
  221. for_each_toolbar_button([icon](auto& button) {
  222. button.set_icon(icon);
  223. });
  224. for_each_menu_item([](auto& menu_item) {
  225. menu_item.update_from_action({});
  226. });
  227. }
  228. void Action::set_text(String text)
  229. {
  230. if (m_text == text)
  231. return;
  232. m_text = move(text);
  233. for_each_toolbar_button([&](auto& button) {
  234. button.set_text_from_action();
  235. });
  236. for_each_menu_item([&](auto& menu_item) {
  237. menu_item.update_from_action({});
  238. });
  239. }
  240. }