Action.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TemporaryChange.h>
  7. #include <LibGUI/Action.h>
  8. #include <LibGUI/ActionGroup.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/Button.h>
  11. #include <LibGUI/MenuItem.h>
  12. #include <LibGUI/Window.h>
  13. #include <LibGfx/Painter.h>
  14. namespace GUI {
  15. NonnullRefPtr<Action> Action::create(DeprecatedString text, Function<void(Action&)> callback, Core::EventReceiver* parent)
  16. {
  17. return adopt_ref(*new Action(move(text), move(callback), parent));
  18. }
  19. NonnullRefPtr<Action> Action::create(DeprecatedString text, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent)
  20. {
  21. return adopt_ref(*new Action(move(text), move(icon), move(callback), parent));
  22. }
  23. NonnullRefPtr<Action> Action::create(DeprecatedString text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::EventReceiver* parent)
  24. {
  25. return adopt_ref(*new Action(move(text), shortcut, move(callback), parent));
  26. }
  27. NonnullRefPtr<Action> Action::create(DeprecatedString text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, Function<void(Action&)> callback, Core::EventReceiver* parent)
  28. {
  29. return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(callback), parent));
  30. }
  31. NonnullRefPtr<Action> Action::create(DeprecatedString text, Shortcut const& shortcut, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent)
  32. {
  33. return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent));
  34. }
  35. NonnullRefPtr<Action> Action::create(DeprecatedString text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent)
  36. {
  37. return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(icon), move(callback), parent));
  38. }
  39. NonnullRefPtr<Action> Action::create_checkable(DeprecatedString text, Function<void(Action&)> callback, Core::EventReceiver* parent)
  40. {
  41. return adopt_ref(*new Action(move(text), move(callback), parent, true));
  42. }
  43. NonnullRefPtr<Action> Action::create_checkable(DeprecatedString text, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent)
  44. {
  45. return adopt_ref(*new Action(move(text), move(icon), move(callback), parent, true));
  46. }
  47. NonnullRefPtr<Action> Action::create_checkable(DeprecatedString text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::EventReceiver* parent)
  48. {
  49. return adopt_ref(*new Action(move(text), shortcut, move(callback), parent, true));
  50. }
  51. NonnullRefPtr<Action> Action::create_checkable(DeprecatedString text, Shortcut const& shortcut, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent)
  52. {
  53. return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent, true));
  54. }
  55. RefPtr<Action> Action::find_action_for_shortcut(Core::EventReceiver& object, Shortcut const& shortcut)
  56. {
  57. RefPtr<Action> found_action = nullptr;
  58. object.for_each_child_of_type<Action>([&](auto& action) {
  59. if (action.shortcut() == shortcut || action.alternate_shortcut() == shortcut) {
  60. found_action = &action;
  61. return IterationDecision::Break;
  62. }
  63. return IterationDecision::Continue;
  64. });
  65. return found_action;
  66. }
  67. Action::Action(DeprecatedString text, Function<void(Action&)> on_activation_callback, Core::EventReceiver* parent, bool checkable)
  68. : Action(move(text), Shortcut {}, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
  69. {
  70. }
  71. Action::Action(DeprecatedString text, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> on_activation_callback, Core::EventReceiver* parent, bool checkable)
  72. : Action(move(text), Shortcut {}, Shortcut {}, move(icon), move(on_activation_callback), parent, checkable)
  73. {
  74. }
  75. Action::Action(DeprecatedString text, Shortcut const& shortcut, Function<void(Action&)> on_activation_callback, Core::EventReceiver* parent, bool checkable)
  76. : Action(move(text), shortcut, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
  77. {
  78. }
  79. Action::Action(DeprecatedString text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, Function<void(Action&)> on_activation_callback, Core::EventReceiver* parent, bool checkable)
  80. : Action(move(text), shortcut, alternate_shortcut, nullptr, move(on_activation_callback), parent, checkable)
  81. {
  82. }
  83. Action::Action(DeprecatedString text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> on_activation_callback, Core::EventReceiver* parent, bool checkable)
  84. : Core::EventReceiver(parent)
  85. , on_activation(move(on_activation_callback))
  86. , m_text(move(text))
  87. , m_icon(move(icon))
  88. , m_shortcut(shortcut)
  89. , m_alternate_shortcut(alternate_shortcut)
  90. , m_checkable(checkable)
  91. {
  92. if (parent && is<Widget>(*parent)) {
  93. m_scope = ShortcutScope::WidgetLocal;
  94. } else if (parent && is<Window>(*parent)) {
  95. m_scope = ShortcutScope::WindowLocal;
  96. } else {
  97. m_scope = ShortcutScope::ApplicationGlobal;
  98. if (auto* app = Application::the()) {
  99. app->register_global_shortcut_action({}, *this);
  100. }
  101. }
  102. }
  103. Action::~Action()
  104. {
  105. if (m_scope == ShortcutScope::ApplicationGlobal) {
  106. if (auto* app = Application::the())
  107. app->unregister_global_shortcut_action({}, *this);
  108. }
  109. }
  110. void Action::process_event(Window& window, Event& event)
  111. {
  112. if (is_enabled() && is_visible() && !is_activating()) {
  113. flash_menubar_menu(window);
  114. activate();
  115. event.accept();
  116. return;
  117. }
  118. if (swallow_key_event_when_disabled()) {
  119. event.accept();
  120. return;
  121. }
  122. event.ignore();
  123. }
  124. void Action::activate(Core::EventReceiver* activator)
  125. {
  126. if (is_activating())
  127. return;
  128. TemporaryChange change { m_activating, true };
  129. if (!on_activation)
  130. return;
  131. if (activator)
  132. m_activator = activator->make_weak_ptr();
  133. if (is_checkable()) {
  134. if (m_action_group) {
  135. if (m_action_group->is_unchecking_allowed())
  136. set_checked(!is_checked());
  137. else
  138. set_checked(true);
  139. } else {
  140. set_checked(!is_checked());
  141. }
  142. }
  143. if (activator == nullptr) {
  144. for_each_toolbar_button([](auto& button) {
  145. button.mimic_pressed();
  146. });
  147. }
  148. on_activation(*this);
  149. m_activator = nullptr;
  150. }
  151. void Action::flash_menubar_menu(GUI::Window& window)
  152. {
  153. for (auto& menu_item : m_menu_items)
  154. window.flash_menubar_menu_for(*menu_item);
  155. }
  156. void Action::register_button(Badge<Button>, Button& button)
  157. {
  158. m_buttons.set(&button);
  159. }
  160. void Action::unregister_button(Badge<Button>, Button& button)
  161. {
  162. m_buttons.remove(&button);
  163. }
  164. void Action::register_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  165. {
  166. m_menu_items.set(&menu_item);
  167. }
  168. void Action::unregister_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  169. {
  170. m_menu_items.remove(&menu_item);
  171. }
  172. template<typename Callback>
  173. void Action::for_each_toolbar_button(Callback callback)
  174. {
  175. for (auto& it : m_buttons)
  176. callback(*it);
  177. }
  178. template<typename Callback>
  179. void Action::for_each_menu_item(Callback callback)
  180. {
  181. for (auto& it : m_menu_items)
  182. callback(*it);
  183. }
  184. void Action::set_enabled(bool enabled)
  185. {
  186. if (m_enabled == enabled)
  187. return;
  188. m_enabled = enabled;
  189. for_each_toolbar_button([enabled](auto& button) {
  190. button.set_enabled(enabled);
  191. });
  192. for_each_menu_item([enabled](auto& item) {
  193. item.set_enabled(enabled);
  194. });
  195. }
  196. void Action::set_visible(bool visible)
  197. {
  198. if (m_visible == visible)
  199. return;
  200. m_visible = visible;
  201. for_each_toolbar_button([visible](auto& button) {
  202. button.set_visible(visible);
  203. });
  204. for_each_menu_item([visible](auto& item) {
  205. item.set_visible(visible);
  206. });
  207. }
  208. void Action::set_checked(bool checked)
  209. {
  210. if (m_checked == checked)
  211. return;
  212. m_checked = checked;
  213. if (m_checked && m_action_group) {
  214. m_action_group->for_each_action([this](auto& other_action) {
  215. if (this == &other_action)
  216. return IterationDecision::Continue;
  217. if (other_action.is_checkable())
  218. other_action.set_checked(false);
  219. return IterationDecision::Continue;
  220. });
  221. }
  222. for_each_toolbar_button([checked](auto& button) {
  223. button.set_checked(checked);
  224. });
  225. for_each_menu_item([checked](MenuItem& item) {
  226. item.set_checked(checked);
  227. });
  228. }
  229. void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
  230. {
  231. m_action_group = AK::make_weak_ptr_if_nonnull(group);
  232. }
  233. void Action::set_icon(Gfx::Bitmap const* icon)
  234. {
  235. if (m_icon == icon)
  236. return;
  237. m_icon = icon;
  238. for_each_toolbar_button([icon](auto& button) {
  239. button.set_icon(icon);
  240. });
  241. for_each_menu_item([](auto& menu_item) {
  242. menu_item.update_from_action({});
  243. });
  244. }
  245. void Action::set_text(DeprecatedString text)
  246. {
  247. if (m_text == text)
  248. return;
  249. m_text = move(text);
  250. for_each_toolbar_button([&](auto& button) {
  251. button.set_text(String::from_deprecated_string(m_text).release_value_but_fixme_should_propagate_errors());
  252. });
  253. for_each_menu_item([&](auto& menu_item) {
  254. menu_item.update_from_action({});
  255. });
  256. }
  257. DeprecatedString Action::tooltip() const
  258. {
  259. return m_tooltip.value_or_lazy_evaluated([this] { return Gfx::parse_ampersand_string(m_text); });
  260. }
  261. void Action::set_tooltip(DeprecatedString tooltip)
  262. {
  263. if (m_tooltip == tooltip)
  264. return;
  265. m_tooltip = move(tooltip);
  266. for_each_toolbar_button([&](auto& button) {
  267. button.set_tooltip(MUST(String::from_deprecated_string(*m_tooltip)));
  268. });
  269. for_each_menu_item([&](auto& menu_item) {
  270. menu_item.update_from_action({});
  271. });
  272. }
  273. Optional<String> Action::status_tip() const
  274. {
  275. if (!m_status_tip.is_empty())
  276. return m_status_tip;
  277. auto maybe_parsed_action_text = String::from_deprecated_string(Gfx::parse_ampersand_string(m_text));
  278. if (maybe_parsed_action_text.is_error())
  279. return {};
  280. return maybe_parsed_action_text.release_value();
  281. }
  282. }