Action.cpp 10 KB

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