Action.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/WeakPtr.h>
  27. #include <LibGUI/AboutDialog.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/ActionGroup.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/Icon.h>
  33. #include <LibGUI/MenuItem.h>
  34. #include <LibGUI/Window.h>
  35. namespace GUI {
  36. namespace CommonActions {
  37. NonnullRefPtr<Action> make_about_action(const String& app_name, const Icon& app_icon, Window* parent)
  38. {
  39. auto weak_parent = AK::try_make_weak_ptr<Window>(parent);
  40. return Action::create(String::formatted("About {}", app_name), app_icon.bitmap_for_size(16), [=](auto&) {
  41. AboutDialog::show(app_name, app_icon.bitmap_for_size(32), weak_parent.ptr());
  42. });
  43. }
  44. NonnullRefPtr<Action> make_open_action(Function<void(Action&)> callback, Core::Object* parent)
  45. {
  46. return Action::create("Open...", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), move(callback), parent);
  47. }
  48. NonnullRefPtr<Action> make_save_action(Function<void(Action&)> callback, Core::Object* parent)
  49. {
  50. return Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), move(callback), parent);
  51. }
  52. NonnullRefPtr<Action> make_save_as_action(Function<void(Action&)> callback, Core::Object* parent)
  53. {
  54. return Action::create("Save As...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), move(callback), parent);
  55. }
  56. NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)> callback, Core::Object* parent)
  57. {
  58. return Action::create("Move to front", { Mod_Ctrl | Mod_Shift, Key_Up }, Gfx::Bitmap::load_from_file("/res/icons/16x16/move-to-front.png"), move(callback), parent);
  59. }
  60. NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)> callback, Core::Object* parent)
  61. {
  62. return Action::create("Move to back", { Mod_Ctrl | Mod_Shift, Key_Down }, Gfx::Bitmap::load_from_file("/res/icons/16x16/move-to-back.png"), move(callback), parent);
  63. }
  64. NonnullRefPtr<Action> make_undo_action(Function<void(Action&)> callback, Core::Object* parent)
  65. {
  66. return Action::create("Undo", { Mod_Ctrl, Key_Z }, Gfx::Bitmap::load_from_file("/res/icons/16x16/undo.png"), move(callback), parent);
  67. }
  68. NonnullRefPtr<Action> make_redo_action(Function<void(Action&)> callback, Core::Object* parent)
  69. {
  70. return Action::create("Redo", { Mod_Ctrl, Key_Y }, Gfx::Bitmap::load_from_file("/res/icons/16x16/redo.png"), move(callback), parent);
  71. }
  72. NonnullRefPtr<Action> make_delete_action(Function<void(Action&)> callback, Core::Object* parent)
  73. {
  74. return Action::create("Delete", { Mod_None, Key_Delete }, Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"), move(callback), parent);
  75. }
  76. NonnullRefPtr<Action> make_cut_action(Function<void(Action&)> callback, Core::Object* parent)
  77. {
  78. return Action::create("Cut", { Mod_Ctrl, Key_X }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-cut.png"), move(callback), parent);
  79. }
  80. NonnullRefPtr<Action> make_copy_action(Function<void(Action&)> callback, Core::Object* parent)
  81. {
  82. return Action::create("Copy", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"), move(callback), parent);
  83. }
  84. NonnullRefPtr<Action> make_paste_action(Function<void(Action&)> callback, Core::Object* parent)
  85. {
  86. return Action::create("Paste", { Mod_Ctrl, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/16x16/paste.png"), move(callback), parent);
  87. }
  88. NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)> callback, Core::Object* parent)
  89. {
  90. return Action::create("Fullscreen", { Mod_None, Key_F11 }, move(callback), parent);
  91. }
  92. NonnullRefPtr<Action> make_quit_action(Function<void(Action&)> callback)
  93. {
  94. return Action::create("Quit", { Mod_Alt, Key_F4 }, move(callback));
  95. }
  96. NonnullRefPtr<Action> make_help_action(Function<void(Action&)> callback, Core::Object* parent)
  97. {
  98. return Action::create("Contents", { Mod_None, Key_F1 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/app-help.png"), move(callback), parent);
  99. }
  100. NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)> callback, Core::Object* parent)
  101. {
  102. return Action::create("Go back", { Mod_Alt, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"), move(callback), parent);
  103. }
  104. NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)> callback, Core::Object* parent)
  105. {
  106. return Action::create("Go forward", { Mod_Alt, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), move(callback), parent);
  107. }
  108. NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::Object* parent)
  109. {
  110. return Action::create("Go home", { Mod_Alt, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-home.png"), move(callback), parent);
  111. }
  112. NonnullRefPtr<Action> make_reload_action(Function<void(Action&)> callback, Core::Object* parent)
  113. {
  114. return Action::create("Reload", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"), move(callback), parent);
  115. }
  116. NonnullRefPtr<Action> make_select_all_action(Function<void(Action&)> callback, Core::Object* parent)
  117. {
  118. return Action::create("Select all", { Mod_Ctrl, Key_A }, Gfx::Bitmap::load_from_file("/res/icons/16x16/select-all.png"), move(callback), parent);
  119. }
  120. NonnullRefPtr<Action> make_properties_action(Function<void(Action&)> callback, Core::Object* parent)
  121. {
  122. return Action::create("Properties", { Mod_Alt, Key_Return }, Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"), move(callback), parent);
  123. }
  124. }
  125. NonnullRefPtr<Action> Action::create(String text, Function<void(Action&)> callback, Core::Object* parent)
  126. {
  127. return adopt(*new Action(move(text), move(callback), parent));
  128. }
  129. NonnullRefPtr<Action> Action::create(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  130. {
  131. return adopt(*new Action(move(text), move(icon), move(callback), parent));
  132. }
  133. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent)
  134. {
  135. return adopt(*new Action(move(text), shortcut, move(callback), parent));
  136. }
  137. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  138. {
  139. return adopt(*new Action(move(text), shortcut, move(icon), move(callback), parent));
  140. }
  141. NonnullRefPtr<Action> Action::create_checkable(String text, Function<void(Action&)> callback, Core::Object* parent)
  142. {
  143. return adopt(*new Action(move(text), move(callback), parent, true));
  144. }
  145. NonnullRefPtr<Action> Action::create_checkable(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  146. {
  147. return adopt(*new Action(move(text), move(icon), move(callback), parent, true));
  148. }
  149. NonnullRefPtr<Action> Action::create_checkable(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent)
  150. {
  151. return adopt(*new Action(move(text), shortcut, move(callback), parent, true));
  152. }
  153. NonnullRefPtr<Action> Action::create_checkable(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  154. {
  155. return adopt(*new Action(move(text), shortcut, move(icon), move(callback), parent, true));
  156. }
  157. Action::Action(String text, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  158. : Action(move(text), Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
  159. {
  160. }
  161. Action::Action(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  162. : Action(move(text), Shortcut {}, move(icon), move(on_activation_callback), parent, checkable)
  163. {
  164. }
  165. Action::Action(String text, const Shortcut& shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  166. : Action(move(text), shortcut, nullptr, move(on_activation_callback), parent, checkable)
  167. {
  168. }
  169. Action::Action(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  170. : Core::Object(parent)
  171. , on_activation(move(on_activation_callback))
  172. , m_text(move(text))
  173. , m_icon(move(icon))
  174. , m_shortcut(shortcut)
  175. , m_checkable(checkable)
  176. {
  177. if (parent && is<Widget>(*parent)) {
  178. m_scope = ShortcutScope::WidgetLocal;
  179. } else if (parent && is<Window>(*parent)) {
  180. m_scope = ShortcutScope::WindowLocal;
  181. } else {
  182. m_scope = ShortcutScope::ApplicationGlobal;
  183. if (auto* app = Application::the()) {
  184. app->register_global_shortcut_action({}, *this);
  185. }
  186. }
  187. }
  188. Action::~Action()
  189. {
  190. if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) {
  191. if (auto* app = Application::the())
  192. app->unregister_global_shortcut_action({}, *this);
  193. }
  194. }
  195. void Action::activate(Core::Object* activator)
  196. {
  197. if (!on_activation)
  198. return;
  199. if (activator)
  200. m_activator = activator->make_weak_ptr();
  201. if (is_checkable()) {
  202. if (m_action_group) {
  203. if (m_action_group->is_unchecking_allowed())
  204. set_checked(!is_checked());
  205. else
  206. set_checked(true);
  207. } else {
  208. set_checked(!is_checked());
  209. }
  210. }
  211. on_activation(*this);
  212. m_activator = nullptr;
  213. }
  214. void Action::register_button(Badge<Button>, Button& button)
  215. {
  216. m_buttons.set(&button);
  217. }
  218. void Action::unregister_button(Badge<Button>, Button& button)
  219. {
  220. m_buttons.remove(&button);
  221. }
  222. void Action::register_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  223. {
  224. m_menu_items.set(&menu_item);
  225. }
  226. void Action::unregister_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  227. {
  228. m_menu_items.remove(&menu_item);
  229. }
  230. template<typename Callback>
  231. void Action::for_each_toolbar_button(Callback callback)
  232. {
  233. for (auto& it : m_buttons)
  234. callback(*it);
  235. }
  236. template<typename Callback>
  237. void Action::for_each_menu_item(Callback callback)
  238. {
  239. for (auto& it : m_menu_items)
  240. callback(*it);
  241. }
  242. void Action::set_enabled(bool enabled)
  243. {
  244. if (m_enabled == enabled)
  245. return;
  246. m_enabled = enabled;
  247. for_each_toolbar_button([enabled](auto& button) {
  248. button.set_enabled(enabled);
  249. });
  250. for_each_menu_item([enabled](auto& item) {
  251. item.set_enabled(enabled);
  252. });
  253. }
  254. void Action::set_checked(bool checked)
  255. {
  256. if (m_checked == checked)
  257. return;
  258. m_checked = checked;
  259. if (m_checked && m_action_group) {
  260. m_action_group->for_each_action([this](auto& other_action) {
  261. if (this == &other_action)
  262. return IterationDecision::Continue;
  263. if (other_action.is_checkable())
  264. other_action.set_checked(false);
  265. return IterationDecision::Continue;
  266. });
  267. }
  268. for_each_toolbar_button([checked](auto& button) {
  269. button.set_checked(checked);
  270. });
  271. for_each_menu_item([checked](MenuItem& item) {
  272. item.set_checked(checked);
  273. });
  274. }
  275. void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
  276. {
  277. m_action_group = AK::try_make_weak_ptr(group);
  278. }
  279. void Action::set_icon(const Gfx::Bitmap* icon)
  280. {
  281. m_icon = icon;
  282. }
  283. }