Action.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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::try_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::try_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::try_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::try_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::try_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::try_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::try_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::try_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::try_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::try_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::try_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::try_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::try_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::try_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::try_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 }, Key_F5, Gfx::Bitmap::try_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::try_load_from_file("/res/icons/16x16/select-all.png"), move(callback), parent);
  127. }
  128. NonnullRefPtr<Action> make_rename_action(Function<void(Action&)> callback, Core::Object* parent)
  129. {
  130. return Action::create("Re&name", Key_F2, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/rename.png"), move(callback), parent);
  131. }
  132. NonnullRefPtr<Action> make_properties_action(Function<void(Action&)> callback, Core::Object* parent)
  133. {
  134. return Action::create("P&roperties", { Mod_Alt, Key_Return }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/properties.png"), move(callback), parent);
  135. }
  136. NonnullRefPtr<Action> make_zoom_in_action(Function<void(Action&)> callback, Core::Object* parent)
  137. {
  138. return GUI::Action::create("Zoom &In", { Mod_Ctrl, Key_Equal }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/zoom-in.png"), move(callback), parent);
  139. }
  140. NonnullRefPtr<Action> make_reset_zoom_action(Function<void(Action&)> callback, Core::Object* parent)
  141. {
  142. return GUI::Action::create("&Reset Zoom", { Mod_Ctrl, Key_0 }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/zoom-reset.png"), move(callback), parent);
  143. }
  144. NonnullRefPtr<Action> make_zoom_out_action(Function<void(Action&)> callback, Core::Object* parent)
  145. {
  146. return GUI::Action::create("Zoom &Out", { Mod_Ctrl, Key_Minus }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/zoom-out.png"), move(callback), parent);
  147. }
  148. }
  149. NonnullRefPtr<Action> Action::create(String text, Function<void(Action&)> callback, Core::Object* parent)
  150. {
  151. return adopt_ref(*new Action(move(text), move(callback), parent));
  152. }
  153. NonnullRefPtr<Action> Action::create(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));
  156. }
  157. NonnullRefPtr<Action> Action::create(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));
  160. }
  161. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, Function<void(Action&)> callback, Core::Object* parent)
  162. {
  163. return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(callback), parent));
  164. }
  165. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  166. {
  167. return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent));
  168. }
  169. NonnullRefPtr<Action> Action::create(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  170. {
  171. return adopt_ref(*new Action(move(text), shortcut, alternate_shortcut, move(icon), move(callback), parent));
  172. }
  173. NonnullRefPtr<Action> Action::create_checkable(String text, Function<void(Action&)> callback, Core::Object* parent)
  174. {
  175. return adopt_ref(*new Action(move(text), move(callback), parent, true));
  176. }
  177. NonnullRefPtr<Action> Action::create_checkable(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  178. {
  179. return adopt_ref(*new Action(move(text), move(icon), move(callback), parent, true));
  180. }
  181. NonnullRefPtr<Action> Action::create_checkable(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent)
  182. {
  183. return adopt_ref(*new Action(move(text), shortcut, move(callback), parent, true));
  184. }
  185. NonnullRefPtr<Action> Action::create_checkable(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent)
  186. {
  187. return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent, true));
  188. }
  189. Action::Action(String text, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  190. : Action(move(text), Shortcut {}, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
  191. {
  192. }
  193. Action::Action(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  194. : Action(move(text), Shortcut {}, Shortcut {}, move(icon), move(on_activation_callback), parent, checkable)
  195. {
  196. }
  197. Action::Action(String text, const Shortcut& shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  198. : Action(move(text), shortcut, Shortcut {}, nullptr, move(on_activation_callback), parent, checkable)
  199. {
  200. }
  201. Action::Action(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  202. : Action(move(text), shortcut, alternate_shortcut, nullptr, move(on_activation_callback), parent, checkable)
  203. {
  204. }
  205. Action::Action(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  206. : Core::Object(parent)
  207. , on_activation(move(on_activation_callback))
  208. , m_text(move(text))
  209. , m_icon(move(icon))
  210. , m_shortcut(shortcut)
  211. , m_alternate_shortcut(alternate_shortcut)
  212. , m_checkable(checkable)
  213. {
  214. if (parent && is<Widget>(*parent)) {
  215. m_scope = ShortcutScope::WidgetLocal;
  216. } else if (parent && is<Window>(*parent)) {
  217. m_scope = ShortcutScope::WindowLocal;
  218. } else {
  219. m_scope = ShortcutScope::ApplicationGlobal;
  220. if (auto* app = Application::the()) {
  221. app->register_global_shortcut_action({}, *this);
  222. }
  223. }
  224. }
  225. Action::~Action()
  226. {
  227. if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) {
  228. if (auto* app = Application::the())
  229. app->unregister_global_shortcut_action({}, *this);
  230. }
  231. }
  232. void Action::activate(Core::Object* activator)
  233. {
  234. if (!on_activation)
  235. return;
  236. if (activator)
  237. m_activator = activator->make_weak_ptr();
  238. if (is_checkable()) {
  239. if (m_action_group) {
  240. if (m_action_group->is_unchecking_allowed())
  241. set_checked(!is_checked());
  242. else
  243. set_checked(true);
  244. } else {
  245. set_checked(!is_checked());
  246. }
  247. }
  248. on_activation(*this);
  249. m_activator = nullptr;
  250. }
  251. void Action::register_button(Badge<Button>, Button& button)
  252. {
  253. m_buttons.set(&button);
  254. }
  255. void Action::unregister_button(Badge<Button>, Button& button)
  256. {
  257. m_buttons.remove(&button);
  258. }
  259. void Action::register_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  260. {
  261. m_menu_items.set(&menu_item);
  262. }
  263. void Action::unregister_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  264. {
  265. m_menu_items.remove(&menu_item);
  266. }
  267. template<typename Callback>
  268. void Action::for_each_toolbar_button(Callback callback)
  269. {
  270. for (auto& it : m_buttons)
  271. callback(*it);
  272. }
  273. template<typename Callback>
  274. void Action::for_each_menu_item(Callback callback)
  275. {
  276. for (auto& it : m_menu_items)
  277. callback(*it);
  278. }
  279. void Action::set_enabled(bool enabled)
  280. {
  281. if (m_enabled == enabled)
  282. return;
  283. m_enabled = enabled;
  284. for_each_toolbar_button([enabled](auto& button) {
  285. button.set_enabled(enabled);
  286. });
  287. for_each_menu_item([enabled](auto& item) {
  288. item.set_enabled(enabled);
  289. });
  290. }
  291. void Action::set_checked(bool checked)
  292. {
  293. if (m_checked == checked)
  294. return;
  295. m_checked = checked;
  296. if (m_checked && m_action_group) {
  297. m_action_group->for_each_action([this](auto& other_action) {
  298. if (this == &other_action)
  299. return IterationDecision::Continue;
  300. if (other_action.is_checkable())
  301. other_action.set_checked(false);
  302. return IterationDecision::Continue;
  303. });
  304. }
  305. for_each_toolbar_button([checked](auto& button) {
  306. button.set_checked(checked);
  307. });
  308. for_each_menu_item([checked](MenuItem& item) {
  309. item.set_checked(checked);
  310. });
  311. }
  312. void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
  313. {
  314. m_action_group = AK::try_make_weak_ptr(group);
  315. }
  316. void Action::set_icon(const Gfx::Bitmap* icon)
  317. {
  318. m_icon = icon;
  319. }
  320. void Action::set_text(String text)
  321. {
  322. if (m_text == text)
  323. return;
  324. m_text = move(text);
  325. for_each_menu_item([&](auto& menu_item) {
  326. menu_item.update_from_action({});
  327. });
  328. }
  329. }