Action.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/Function.h>
  10. #include <AK/HashTable.h>
  11. #include <AK/NonnullRefPtr.h>
  12. #include <AK/RefCounted.h>
  13. #include <AK/String.h>
  14. #include <AK/WeakPtr.h>
  15. #include <AK/Weakable.h>
  16. #include <LibCore/EventReceiver.h>
  17. #include <LibCore/Timer.h>
  18. #include <LibGUI/Forward.h>
  19. #include <LibGUI/Shortcut.h>
  20. #include <LibGfx/Forward.h>
  21. namespace GUI {
  22. namespace CommonActions {
  23. NonnullRefPtr<Action> make_about_action(String const& app_name, Icon const& app_icon, Window* parent = nullptr);
  24. NonnullRefPtr<Action> make_open_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  25. NonnullRefPtr<Action> make_save_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  26. NonnullRefPtr<Action> make_save_as_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  27. NonnullRefPtr<Action> make_undo_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  28. NonnullRefPtr<Action> make_redo_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  29. NonnullRefPtr<Action> make_cut_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  30. NonnullRefPtr<Action> make_copy_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  31. NonnullRefPtr<Action> make_paste_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  32. NonnullRefPtr<Action> make_delete_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  33. NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  34. NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  35. NonnullRefPtr<Action> make_insert_emoji_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  36. NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  37. NonnullRefPtr<Action> make_quit_action(Function<void(Action&)>);
  38. NonnullRefPtr<Action> make_help_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  39. NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  40. NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  41. NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  42. NonnullRefPtr<Action> make_close_tab_action(Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  43. NonnullRefPtr<Action> make_reload_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  44. NonnullRefPtr<Action> make_select_all_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  45. NonnullRefPtr<Action> make_rename_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  46. NonnullRefPtr<Action> make_properties_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  47. NonnullRefPtr<Action> make_zoom_in_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  48. NonnullRefPtr<Action> make_zoom_out_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  49. NonnullRefPtr<Action> make_reset_zoom_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  50. NonnullRefPtr<Action> make_rotate_clockwise_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  51. NonnullRefPtr<Action> make_rotate_counterclockwise_action(Function<void(Action&)>, Core::EventReceiver* parent = nullptr);
  52. NonnullRefPtr<Action> make_command_palette_action(Window* window = nullptr);
  53. };
  54. class Action final : public Core::EventReceiver {
  55. C_OBJECT(Action)
  56. public:
  57. enum class ShortcutScope {
  58. None,
  59. WidgetLocal,
  60. WindowLocal,
  61. ApplicationGlobal,
  62. };
  63. static NonnullRefPtr<Action> create(DeprecatedString text, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  64. static NonnullRefPtr<Action> create(DeprecatedString text, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  65. static NonnullRefPtr<Action> create(DeprecatedString text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  66. static NonnullRefPtr<Action> create(DeprecatedString text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  67. static NonnullRefPtr<Action> create(DeprecatedString text, Shortcut const& shortcut, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  68. static NonnullRefPtr<Action> create(DeprecatedString text, Shortcut const& shortcut, Shortcut const& alternate_shortcut, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  69. static NonnullRefPtr<Action> create_checkable(DeprecatedString text, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  70. static NonnullRefPtr<Action> create_checkable(DeprecatedString text, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  71. static NonnullRefPtr<Action> create_checkable(DeprecatedString text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  72. static NonnullRefPtr<Action> create_checkable(DeprecatedString text, Shortcut const& shortcut, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  73. static ErrorOr<NonnullRefPtr<Action>> try_create_checkable(DeprecatedString text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::EventReceiver* parent = nullptr);
  74. static RefPtr<Action> find_action_for_shortcut(Core::EventReceiver& object, Shortcut const& shortcut);
  75. virtual ~Action() override;
  76. DeprecatedString text() const { return m_text; }
  77. void set_text(DeprecatedString);
  78. DeprecatedString tooltip() const { return m_tooltip.value_or(m_text); }
  79. void set_tooltip(DeprecatedString);
  80. Optional<String> status_tip() const;
  81. void set_status_tip(String status_tip) { m_status_tip = move(status_tip); }
  82. Shortcut const& shortcut() const { return m_shortcut; }
  83. Shortcut const& alternate_shortcut() const { return m_alternate_shortcut; }
  84. Gfx::Bitmap const* icon() const { return m_icon.ptr(); }
  85. void set_icon(Gfx::Bitmap const*);
  86. Core::EventReceiver const* activator() const { return m_activator.ptr(); }
  87. Core::EventReceiver* activator() { return m_activator.ptr(); }
  88. Function<void(Action&)> on_activation;
  89. void activate(Core::EventReceiver* activator = nullptr);
  90. void process_event(Window& window, Event& event);
  91. void flash_menubar_menu(GUI::Window& window);
  92. bool is_enabled() const { return m_enabled; }
  93. void set_enabled(bool);
  94. bool is_visible() const { return m_visible; }
  95. void set_visible(bool);
  96. bool is_checkable() const { return m_checkable; }
  97. void set_checkable(bool checkable) { m_checkable = checkable; }
  98. bool is_checked() const
  99. {
  100. VERIFY(is_checkable());
  101. return m_checked;
  102. }
  103. void set_checked(bool);
  104. bool is_activating() const { return m_activating; }
  105. bool swallow_key_event_when_disabled() const { return m_swallow_key_event_when_disabled; }
  106. void set_swallow_key_event_when_disabled(bool swallow) { m_swallow_key_event_when_disabled = swallow; }
  107. void register_button(Badge<Button>, Button&);
  108. void unregister_button(Badge<Button>, Button&);
  109. void register_menu_item(Badge<MenuItem>, MenuItem&);
  110. void unregister_menu_item(Badge<MenuItem>, MenuItem&);
  111. ActionGroup const* group() const { return m_action_group.ptr(); }
  112. void set_group(Badge<ActionGroup>, ActionGroup*);
  113. HashTable<MenuItem*> const& menu_items() const { return m_menu_items; }
  114. private:
  115. Action(DeprecatedString, Function<void(Action&)> = nullptr, Core::EventReceiver* = nullptr, bool checkable = false);
  116. Action(DeprecatedString, Shortcut const&, Function<void(Action&)> = nullptr, Core::EventReceiver* = nullptr, bool checkable = false);
  117. Action(DeprecatedString, Shortcut const&, Shortcut const&, Function<void(Action&)> = nullptr, Core::EventReceiver* = nullptr, bool checkable = false);
  118. Action(DeprecatedString, Shortcut const&, Shortcut const&, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> = nullptr, Core::EventReceiver* = nullptr, bool checkable = false);
  119. Action(DeprecatedString, RefPtr<Gfx::Bitmap const> icon, Function<void(Action&)> = nullptr, Core::EventReceiver* = nullptr, bool checkable = false);
  120. template<typename Callback>
  121. void for_each_toolbar_button(Callback);
  122. template<typename Callback>
  123. void for_each_menu_item(Callback);
  124. DeprecatedString m_text;
  125. Optional<DeprecatedString> m_tooltip;
  126. String m_status_tip;
  127. RefPtr<Gfx::Bitmap const> m_icon;
  128. Shortcut m_shortcut;
  129. Shortcut m_alternate_shortcut;
  130. bool m_enabled { true };
  131. bool m_visible { true };
  132. bool m_checkable { false };
  133. bool m_checked { false };
  134. bool m_swallow_key_event_when_disabled { false };
  135. bool m_activating { false };
  136. ShortcutScope m_scope { ShortcutScope::None };
  137. HashTable<Button*> m_buttons;
  138. HashTable<MenuItem*> m_menu_items;
  139. WeakPtr<ActionGroup> m_action_group;
  140. WeakPtr<Core::EventReceiver> m_activator;
  141. };
  142. }