Action.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/WeakPtr.h>
  7. #include <LibGUI/AboutDialog.h>
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/ActionGroup.h>
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/Button.h>
  12. #include <LibGUI/Icon.h>
  13. #include <LibGUI/MenuItem.h>
  14. #include <LibGUI/Window.h>
  15. namespace GUI {
  16. namespace CommonActions {
  17. NonnullRefPtr<Action> make_about_action(const String& app_name, const Icon& app_icon, Window* parent)
  18. {
  19. auto weak_parent = AK::try_make_weak_ptr<Window>(parent);
  20. auto action = Action::create(String::formatted("&About {}", app_name), app_icon.bitmap_for_size(16), [=](auto&) {
  21. AboutDialog::show(app_name, app_icon.bitmap_for_size(32), weak_parent.ptr());
  22. });
  23. action->set_status_tip("Show application about box");
  24. return action;
  25. }
  26. NonnullRefPtr<Action> make_open_action(Function<void(Action&)> callback, Core::Object* parent)
  27. {
  28. auto action = Action::create("&Open...", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), move(callback), parent);
  29. action->set_status_tip("Open an existing file");
  30. return action;
  31. }
  32. NonnullRefPtr<Action> make_save_action(Function<void(Action&)> callback, Core::Object* parent)
  33. {
  34. auto action = Action::create("&Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), move(callback), parent);
  35. action->set_status_tip("Save the current file");
  36. return action;
  37. }
  38. NonnullRefPtr<Action> make_save_as_action(Function<void(Action&)> callback, Core::Object* parent)
  39. {
  40. auto action = Action::create("Save &As...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), move(callback), parent);
  41. action->set_status_tip("Save the current file with a new name");
  42. return action;
  43. }
  44. NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)> callback, Core::Object* parent)
  45. {
  46. auto action = 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);
  47. action->set_status_tip("Move to the top of the stack");
  48. return action;
  49. }
  50. NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)> callback, Core::Object* parent)
  51. {
  52. auto action = 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);
  53. action->set_status_tip("Move to the bottom of the stack");
  54. return action;
  55. }
  56. NonnullRefPtr<Action> make_undo_action(Function<void(Action&)> callback, Core::Object* parent)
  57. {
  58. return Action::create("&Undo", { Mod_Ctrl, Key_Z }, Gfx::Bitmap::load_from_file("/res/icons/16x16/undo.png"), move(callback), parent);
  59. }
  60. NonnullRefPtr<Action> make_redo_action(Function<void(Action&)> callback, Core::Object* parent)
  61. {
  62. return Action::create("&Redo", { Mod_Ctrl, Key_Y }, Gfx::Bitmap::load_from_file("/res/icons/16x16/redo.png"), move(callback), parent);
  63. }
  64. NonnullRefPtr<Action> make_delete_action(Function<void(Action&)> callback, Core::Object* parent)
  65. {
  66. return Action::create("&Delete", { Mod_None, Key_Delete }, Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"), move(callback), parent);
  67. }
  68. NonnullRefPtr<Action> make_cut_action(Function<void(Action&)> callback, Core::Object* parent)
  69. {
  70. auto action = Action::create("Cu&t", { Mod_Ctrl, Key_X }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-cut.png"), move(callback), parent);
  71. action->set_status_tip("Cut to clipboard");
  72. return action;
  73. }
  74. NonnullRefPtr<Action> make_copy_action(Function<void(Action&)> callback, Core::Object* parent)
  75. {
  76. auto action = Action::create("&Copy", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"), move(callback), parent);
  77. action->set_status_tip("Copy to clipboard");
  78. return action;
  79. }
  80. NonnullRefPtr<Action> make_paste_action(Function<void(Action&)> callback, Core::Object* parent)
  81. {
  82. auto action = Action::create("&Paste", { Mod_Ctrl, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/16x16/paste.png"), move(callback), parent);
  83. action->set_status_tip("Paste from clipboard");
  84. return action;
  85. }
  86. NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)> callback, Core::Object* parent)
  87. {
  88. auto action = Action::create("&Fullscreen", { Mod_None, Key_F11 }, move(callback), parent);
  89. action->set_status_tip("Enter fullscreen mode");
  90. return action;
  91. }
  92. NonnullRefPtr<Action> make_quit_action(Function<void(Action&)> callback)
  93. {
  94. auto action = Action::create("&Quit", { Mod_Alt, Key_F4 }, move(callback));
  95. action->set_status_tip("Quit the application");
  96. return action;
  97. }
  98. NonnullRefPtr<Action> make_help_action(Function<void(Action&)> callback, Core::Object* parent)
  99. {
  100. auto action = Action::create("&Contents", { Mod_None, Key_F1 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/app-help.png"), move(callback), parent);
  101. action->set_status_tip("Show help contents");
  102. return action;
  103. }
  104. NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)> callback, Core::Object* parent)
  105. {
  106. auto action = Action::create("Go &Back", { Mod_Alt, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"), move(callback), parent);
  107. action->set_status_tip("Move one step backward in history");
  108. return action;
  109. }
  110. NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)> callback, Core::Object* parent)
  111. {
  112. auto action = Action::create("Go &Forward", { Mod_Alt, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), move(callback), parent);
  113. action->set_status_tip("Move one step forward in history");
  114. return action;
  115. }
  116. NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::Object* parent)
  117. {
  118. return Action::create("Go &Home", { Mod_Alt, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-home.png"), move(callback), parent);
  119. }
  120. NonnullRefPtr<Action> make_reload_action(Function<void(Action&)> callback, Core::Object* parent)
  121. {
  122. return Action::create("&Reload", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"), move(callback), parent);
  123. }
  124. NonnullRefPtr<Action> make_select_all_action(Function<void(Action&)> callback, Core::Object* parent)
  125. {
  126. return Action::create("Select &All", { Mod_Ctrl, Key_A }, Gfx::Bitmap::load_from_file("/res/icons/16x16/select-all.png"), move(callback), parent);
  127. }
  128. NonnullRefPtr<Action> make_properties_action(Function<void(Action&)> callback, Core::Object* parent)
  129. {
  130. return Action::create("&Properties", { Mod_Alt, Key_Return }, Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"), move(callback), parent);
  131. }
  132. }
  133. NonnullRefPtr<Action> Action::create(String text, Function<void(Action&)> callback, Core::Object* parent)
  134. {
  135. return adopt_ref(*new Action(move(text), move(callback), parent));
  136. }
  137. NonnullRefPtr<Action> Action::create(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  138. {
  139. return adopt_ref(*new Action(move(text), move(icon), move(callback), parent));
  140. }
  141. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent)
  142. {
  143. return adopt_ref(*new Action(move(text), shortcut, move(callback), parent));
  144. }
  145. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  146. {
  147. return adopt_ref(*new Action(move(text), shortcut, move(icon), move(callback), parent));
  148. }
  149. NonnullRefPtr<Action> Action::create_checkable(String text, Function<void(Action&)> callback, Core::Object* parent)
  150. {
  151. return adopt_ref(*new Action(move(text), move(callback), parent, true));
  152. }
  153. NonnullRefPtr<Action> Action::create_checkable(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  154. {
  155. return adopt_ref(*new Action(move(text), move(icon), move(callback), parent, true));
  156. }
  157. NonnullRefPtr<Action> Action::create_checkable(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent)
  158. {
  159. return adopt_ref(*new Action(move(text), shortcut, move(callback), parent, true));
  160. }
  161. NonnullRefPtr<Action> Action::create_checkable(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  162. {
  163. return adopt_ref(*new Action(move(text), shortcut, move(icon), move(callback), parent, true));
  164. }
  165. Action::Action(String text, 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, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  170. : Action(move(text), Shortcut {}, move(icon), move(on_activation_callback), parent, checkable)
  171. {
  172. }
  173. Action::Action(String text, const Shortcut& shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  174. : Action(move(text), shortcut, nullptr, move(on_activation_callback), parent, checkable)
  175. {
  176. }
  177. Action::Action(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  178. : Core::Object(parent)
  179. , on_activation(move(on_activation_callback))
  180. , m_text(move(text))
  181. , m_icon(move(icon))
  182. , m_shortcut(shortcut)
  183. , m_checkable(checkable)
  184. {
  185. if (parent && is<Widget>(*parent)) {
  186. m_scope = ShortcutScope::WidgetLocal;
  187. } else if (parent && is<Window>(*parent)) {
  188. m_scope = ShortcutScope::WindowLocal;
  189. } else {
  190. m_scope = ShortcutScope::ApplicationGlobal;
  191. if (auto* app = Application::the()) {
  192. app->register_global_shortcut_action({}, *this);
  193. }
  194. }
  195. }
  196. Action::~Action()
  197. {
  198. if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) {
  199. if (auto* app = Application::the())
  200. app->unregister_global_shortcut_action({}, *this);
  201. }
  202. }
  203. void Action::activate(Core::Object* activator)
  204. {
  205. if (!on_activation)
  206. return;
  207. if (activator)
  208. m_activator = activator->make_weak_ptr();
  209. if (is_checkable()) {
  210. if (m_action_group) {
  211. if (m_action_group->is_unchecking_allowed())
  212. set_checked(!is_checked());
  213. else
  214. set_checked(true);
  215. } else {
  216. set_checked(!is_checked());
  217. }
  218. }
  219. on_activation(*this);
  220. m_activator = nullptr;
  221. }
  222. void Action::register_button(Badge<Button>, Button& button)
  223. {
  224. m_buttons.set(&button);
  225. }
  226. void Action::unregister_button(Badge<Button>, Button& button)
  227. {
  228. m_buttons.remove(&button);
  229. }
  230. void Action::register_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  231. {
  232. m_menu_items.set(&menu_item);
  233. }
  234. void Action::unregister_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  235. {
  236. m_menu_items.remove(&menu_item);
  237. }
  238. template<typename Callback>
  239. void Action::for_each_toolbar_button(Callback callback)
  240. {
  241. for (auto& it : m_buttons)
  242. callback(*it);
  243. }
  244. template<typename Callback>
  245. void Action::for_each_menu_item(Callback callback)
  246. {
  247. for (auto& it : m_menu_items)
  248. callback(*it);
  249. }
  250. void Action::set_enabled(bool enabled)
  251. {
  252. if (m_enabled == enabled)
  253. return;
  254. m_enabled = enabled;
  255. for_each_toolbar_button([enabled](auto& button) {
  256. button.set_enabled(enabled);
  257. });
  258. for_each_menu_item([enabled](auto& item) {
  259. item.set_enabled(enabled);
  260. });
  261. }
  262. void Action::set_checked(bool checked)
  263. {
  264. if (m_checked == checked)
  265. return;
  266. m_checked = checked;
  267. if (m_checked && m_action_group) {
  268. m_action_group->for_each_action([this](auto& other_action) {
  269. if (this == &other_action)
  270. return IterationDecision::Continue;
  271. if (other_action.is_checkable())
  272. other_action.set_checked(false);
  273. return IterationDecision::Continue;
  274. });
  275. }
  276. for_each_toolbar_button([checked](auto& button) {
  277. button.set_checked(checked);
  278. });
  279. for_each_menu_item([checked](MenuItem& item) {
  280. item.set_checked(checked);
  281. });
  282. }
  283. void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
  284. {
  285. m_action_group = AK::try_make_weak_ptr(group);
  286. }
  287. void Action::set_icon(const Gfx::Bitmap* icon)
  288. {
  289. m_icon = icon;
  290. }
  291. }