Action.cpp 14 KB

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