CommonActions.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/DeprecatedString.h>
  7. #include <AK/Function.h>
  8. #include <AK/WeakPtr.h>
  9. #include <LibCore/Version.h>
  10. #include <LibGUI/AboutDialog.h>
  11. #include <LibGUI/Action.h>
  12. #include <LibGUI/CommandPalette.h>
  13. #include <LibGUI/Icon.h>
  14. namespace GUI {
  15. namespace CommonActions {
  16. NonnullRefPtr<Action> make_about_action(DeprecatedString const& app_name, Icon const& app_icon, Window* parent)
  17. {
  18. auto weak_parent = AK::make_weak_ptr_if_nonnull<Window>(parent);
  19. auto action = Action::create(DeprecatedString::formatted("&About {}", app_name), app_icon.bitmap_for_size(16), [=](auto&) {
  20. AboutDialog::show(
  21. String::from_deprecated_string(app_name).release_value_but_fixme_should_propagate_errors(),
  22. Core::Version::read_long_version_string().release_value_but_fixme_should_propagate_errors(),
  23. app_icon.bitmap_for_size(32),
  24. weak_parent)
  25. .release_value_but_fixme_should_propagate_errors();
  26. });
  27. action->set_status_tip("Show application about box"_string.release_value_but_fixme_should_propagate_errors());
  28. return action;
  29. }
  30. NonnullRefPtr<Action> make_open_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  31. {
  32. auto action = Action::create("&Open...", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  33. action->set_status_tip("Open an existing file"_string.release_value_but_fixme_should_propagate_errors());
  34. return action;
  35. }
  36. NonnullRefPtr<Action> make_save_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  37. {
  38. auto action = Action::create("&Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  39. action->set_status_tip("Save the current file"_string.release_value_but_fixme_should_propagate_errors());
  40. return action;
  41. }
  42. NonnullRefPtr<Action> make_save_as_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  43. {
  44. auto action = Action::create("Save &As...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save-as.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  45. action->set_status_tip("Save the current file with a new name"_string.release_value_but_fixme_should_propagate_errors());
  46. return action;
  47. }
  48. NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  49. {
  50. 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"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  51. action->set_status_tip("Move to the top of the stack"_string.release_value_but_fixme_should_propagate_errors());
  52. return action;
  53. }
  54. NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  55. {
  56. 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"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  57. action->set_status_tip("Move to the bottom of the stack"_string.release_value_but_fixme_should_propagate_errors());
  58. return action;
  59. }
  60. NonnullRefPtr<Action> make_undo_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  61. {
  62. return Action::create("&Undo", { Mod_Ctrl, Key_Z }, Gfx::Bitmap::load_from_file("/res/icons/16x16/undo.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  63. }
  64. NonnullRefPtr<Action> make_redo_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  65. {
  66. return Action::create("&Redo", { Mod_Ctrl | Mod_Shift, Key_Z }, { Mod_Ctrl, Key_Y }, Gfx::Bitmap::load_from_file("/res/icons/16x16/redo.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  67. }
  68. NonnullRefPtr<Action> make_delete_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  69. {
  70. return Action::create("&Delete", { Mod_None, Key_Delete }, Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  71. }
  72. NonnullRefPtr<Action> make_cut_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  73. {
  74. auto action = Action::create("Cu&t", { Mod_Ctrl, Key_X }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-cut.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  75. action->set_status_tip("Cut to clipboard"_string.release_value_but_fixme_should_propagate_errors());
  76. return action;
  77. }
  78. NonnullRefPtr<Action> make_copy_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  79. {
  80. auto action = Action::create("&Copy", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  81. action->set_status_tip("Copy to clipboard"_string.release_value_but_fixme_should_propagate_errors());
  82. return action;
  83. }
  84. NonnullRefPtr<Action> make_paste_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  85. {
  86. auto action = Action::create("&Paste", { Mod_Ctrl, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/16x16/paste.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  87. action->set_status_tip("Paste from clipboard"_string.release_value_but_fixme_should_propagate_errors());
  88. return action;
  89. }
  90. NonnullRefPtr<Action> make_insert_emoji_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  91. {
  92. auto action = Action::create("&Insert Emoji...", { Mod_Ctrl | Mod_Alt, Key_Space }, Gfx::Bitmap::load_from_file("/res/icons/16x16/emoji.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  93. action->set_status_tip("Open the Emoji Picker"_string.release_value_but_fixme_should_propagate_errors());
  94. return action;
  95. }
  96. NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  97. {
  98. auto action = Action::create("&Fullscreen", { Mod_None, Key_F11 }, move(callback), parent);
  99. action->set_status_tip("Enter fullscreen mode"_string.release_value_but_fixme_should_propagate_errors());
  100. action->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/fullscreen.png"sv).release_value_but_fixme_should_propagate_errors());
  101. return action;
  102. }
  103. NonnullRefPtr<Action> make_quit_action(Function<void(Action&)> callback)
  104. {
  105. auto action = Action::create("&Quit", { Mod_Alt, Key_F4 }, move(callback));
  106. action->set_status_tip("Quit the application"_string.release_value_but_fixme_should_propagate_errors());
  107. return action;
  108. }
  109. NonnullRefPtr<Action> make_help_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  110. {
  111. auto action = Action::create("&Manual", { Mod_None, Key_F1 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/app-help.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  112. action->set_status_tip("Show help contents"_string.release_value_but_fixme_should_propagate_errors());
  113. return action;
  114. }
  115. NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  116. {
  117. auto action = Action::create("Go &Back", { Mod_Alt, Key_Left }, { MouseButton::Backward }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  118. action->set_status_tip("Move one step backward in history"_string.release_value_but_fixme_should_propagate_errors());
  119. return action;
  120. }
  121. NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  122. {
  123. auto action = Action::create("Go &Forward", { Mod_Alt, Key_Right }, { MouseButton::Forward }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  124. action->set_status_tip("Move one step forward in history"_string.release_value_but_fixme_should_propagate_errors());
  125. return action;
  126. }
  127. NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  128. {
  129. return Action::create("Go &Home", { Mod_Alt, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-home.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  130. }
  131. NonnullRefPtr<Action> make_close_tab_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  132. {
  133. auto action = Action::create("&Close Tab", { Mod_Ctrl, Key_W }, Gfx::Bitmap::load_from_file("/res/icons/16x16/close-tab.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  134. action->set_status_tip("Close current tab"_string.release_value_but_fixme_should_propagate_errors());
  135. return action;
  136. }
  137. NonnullRefPtr<Action> make_reload_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  138. {
  139. return Action::create("&Reload", { Mod_Ctrl, Key_R }, Key_F5, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  140. }
  141. NonnullRefPtr<Action> make_select_all_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  142. {
  143. return Action::create("Select &All", { Mod_Ctrl, Key_A }, Gfx::Bitmap::load_from_file("/res/icons/16x16/select-all.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  144. }
  145. NonnullRefPtr<Action> make_rename_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  146. {
  147. return Action::create("Re&name...", Key_F2, Gfx::Bitmap::load_from_file("/res/icons/16x16/rename.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  148. }
  149. NonnullRefPtr<Action> make_properties_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  150. {
  151. return Action::create("P&roperties", { Mod_Alt, Key_Return }, Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  152. }
  153. NonnullRefPtr<Action> make_zoom_in_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  154. {
  155. return GUI::Action::create("Zoom &In", { Mod_Ctrl, Key_Equal }, Gfx::Bitmap::load_from_file("/res/icons/16x16/zoom-in.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  156. }
  157. NonnullRefPtr<Action> make_reset_zoom_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  158. {
  159. return GUI::Action::create("&Reset Zoom", { Mod_Ctrl, Key_0 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/zoom-reset.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  160. }
  161. NonnullRefPtr<Action> make_zoom_out_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  162. {
  163. return GUI::Action::create("Zoom &Out", { Mod_Ctrl, Key_Minus }, Gfx::Bitmap::load_from_file("/res/icons/16x16/zoom-out.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  164. }
  165. NonnullRefPtr<Action> make_rotate_clockwise_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  166. {
  167. return GUI::Action::create("Rotate Clock&wise", { Mod_Ctrl | Mod_Shift, Key_GreaterThan }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-rotate-cw.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  168. }
  169. NonnullRefPtr<Action> make_rotate_counterclockwise_action(Function<void(Action&)> callback, Core::EventReceiver* parent)
  170. {
  171. return GUI::Action::create("Rotate &Counterclockwise", { Mod_Ctrl | Mod_Shift, Key_LessThan }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-rotate-ccw.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent);
  172. }
  173. NonnullRefPtr<Action> make_command_palette_action(Window* window)
  174. {
  175. auto action = Action::create("Find &Command...", { Mod_Ctrl | Mod_Shift, Key_A }, MUST(Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"sv)), [=](auto&) {
  176. auto command_palette = CommandPalette::construct(*window);
  177. if (command_palette->exec() != GUI::Dialog::ExecResult::OK)
  178. return;
  179. auto* action = command_palette->selected_action();
  180. VERIFY(action);
  181. action->flash_menubar_menu(*window);
  182. action->activate();
  183. });
  184. action->set_status_tip("Open the command palette"_string.release_value_but_fixme_should_propagate_errors());
  185. return action;
  186. }
  187. }
  188. }