Action.cpp 9.3 KB

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