CommandPalette.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/FuzzyMatch.h>
  9. #include <AK/QuickSort.h>
  10. #include <LibGUI/Action.h>
  11. #include <LibGUI/ActionGroup.h>
  12. #include <LibGUI/Application.h>
  13. #include <LibGUI/BoxLayout.h>
  14. #include <LibGUI/CommandPalette.h>
  15. #include <LibGUI/FilteringProxyModel.h>
  16. #include <LibGUI/Menu.h>
  17. #include <LibGUI/MenuItem.h>
  18. #include <LibGUI/Menubar.h>
  19. #include <LibGUI/Model.h>
  20. #include <LibGUI/Painter.h>
  21. #include <LibGUI/TableView.h>
  22. #include <LibGUI/TextBox.h>
  23. #include <LibGUI/Widget.h>
  24. #include <LibGfx/Painter.h>
  25. namespace GUI {
  26. enum class IconFlags : unsigned {
  27. Checkable = 0,
  28. Exclusive = 1,
  29. Checked = 2,
  30. };
  31. AK_ENUM_BITWISE_OPERATORS(IconFlags);
  32. class ActionIconDelegate final : public GUI::TableCellPaintingDelegate {
  33. public:
  34. virtual ~ActionIconDelegate() override = default;
  35. bool should_paint(ModelIndex const& index) override
  36. {
  37. return index.data().is_u32();
  38. }
  39. virtual void paint(GUI::Painter& painter, Gfx::IntRect const& cell_rect, Gfx::Palette const& palette, ModelIndex const& index) override
  40. {
  41. auto flags = static_cast<IconFlags>(index.data().as_u32());
  42. VERIFY(has_flag(flags, IconFlags::Checkable));
  43. auto exclusive = has_flag(flags, IconFlags::Exclusive);
  44. auto checked = has_flag(flags, IconFlags::Checked);
  45. if (exclusive) {
  46. Gfx::IntRect radio_rect { 0, 0, 12, 12 };
  47. radio_rect.center_within(cell_rect);
  48. Gfx::StylePainter::paint_radio_button(painter, radio_rect, palette, checked, false);
  49. } else {
  50. Gfx::IntRect radio_rect { 0, 0, 13, 13 };
  51. radio_rect.center_within(cell_rect);
  52. Gfx::StylePainter::paint_check_box(painter, radio_rect, palette, true, checked, false);
  53. }
  54. }
  55. };
  56. class ActionModel final : public GUI::Model {
  57. public:
  58. enum Column {
  59. Icon,
  60. Text,
  61. Menu,
  62. Shortcut,
  63. __Count,
  64. };
  65. ActionModel(Vector<NonnullRefPtr<GUI::Action>>& actions)
  66. : m_actions(actions)
  67. {
  68. }
  69. virtual ~ActionModel() override = default;
  70. virtual int row_count(ModelIndex const& parent_index) const override
  71. {
  72. if (!parent_index.is_valid())
  73. return m_actions.size();
  74. return 0;
  75. }
  76. virtual int column_count(ModelIndex const& = ModelIndex()) const override
  77. {
  78. return Column::__Count;
  79. }
  80. virtual DeprecatedString column_name(int) const override { return {}; }
  81. virtual ModelIndex index(int row, int column = 0, ModelIndex const& = ModelIndex()) const override
  82. {
  83. return create_index(row, column, m_actions.at(row).ptr());
  84. }
  85. virtual Variant data(ModelIndex const& index, ModelRole role = ModelRole::Display) const override
  86. {
  87. if (role != ModelRole::Display)
  88. return {};
  89. auto& action = *static_cast<GUI::Action*>(index.internal_data());
  90. switch (index.column()) {
  91. case Column::Icon:
  92. if (action.icon())
  93. return *action.icon();
  94. if (action.is_checkable()) {
  95. auto flags = IconFlags::Checkable;
  96. if (action.is_checked())
  97. flags |= IconFlags::Checked;
  98. if (action.group() && action.group()->is_exclusive())
  99. flags |= IconFlags::Exclusive;
  100. return (u32)flags;
  101. }
  102. return "";
  103. case Column::Text:
  104. return action_text(index);
  105. case Column::Menu:
  106. return menu_name(index);
  107. case Column::Shortcut:
  108. if (!action.shortcut().is_valid())
  109. return "";
  110. return action.shortcut().to_deprecated_string();
  111. }
  112. VERIFY_NOT_REACHED();
  113. }
  114. virtual TriState data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override
  115. {
  116. auto needle = term.as_string();
  117. if (needle.is_empty())
  118. return TriState::True;
  119. auto haystack = DeprecatedString::formatted("{} {}", menu_name(index), action_text(index));
  120. if (fuzzy_match(needle, haystack).score > 0)
  121. return TriState::True;
  122. return TriState::False;
  123. }
  124. static DeprecatedString action_text(ModelIndex const& index)
  125. {
  126. auto& action = *static_cast<GUI::Action*>(index.internal_data());
  127. return Gfx::parse_ampersand_string(action.text());
  128. }
  129. static DeprecatedString menu_name(ModelIndex const& index)
  130. {
  131. auto& action = *static_cast<GUI::Action*>(index.internal_data());
  132. if (action.menu_items().is_empty())
  133. return {};
  134. auto* menu_item = *action.menu_items().begin();
  135. auto* menu = Menu::from_menu_id(menu_item->menu_id());
  136. if (!menu)
  137. return {};
  138. return Gfx::parse_ampersand_string(menu->name());
  139. }
  140. private:
  141. Vector<NonnullRefPtr<GUI::Action>> const& m_actions;
  142. };
  143. CommandPalette::CommandPalette(GUI::Window& parent_window, ScreenPosition screen_position)
  144. : GUI::Dialog(&parent_window, screen_position)
  145. {
  146. set_window_type(GUI::WindowType::Popup);
  147. set_window_mode(GUI::WindowMode::Modeless);
  148. set_blocks_emoji_input(true);
  149. resize(450, 300);
  150. collect_actions(parent_window);
  151. auto main_widget = set_main_widget<GUI::Frame>().release_value_but_fixme_should_propagate_errors();
  152. main_widget->set_frame_shape(Gfx::FrameShape::Window);
  153. main_widget->set_fill_with_background_color(true);
  154. main_widget->set_layout<GUI::VerticalBoxLayout>(4);
  155. m_text_box = main_widget->add<GUI::TextBox>();
  156. m_table_view = main_widget->add<GUI::TableView>();
  157. m_model = adopt_ref(*new ActionModel(m_actions));
  158. m_table_view->set_column_headers_visible(false);
  159. m_filter_model = MUST(GUI::FilteringProxyModel::create(*m_model));
  160. m_filter_model->set_filter_term(""sv);
  161. m_table_view->set_column_painting_delegate(0, make<ActionIconDelegate>());
  162. m_table_view->set_model(*m_filter_model);
  163. m_table_view->set_focus_proxy(m_text_box);
  164. m_text_box->on_change = [this] {
  165. m_filter_model->set_filter_term(m_text_box->text());
  166. if (m_filter_model->row_count() != 0)
  167. m_table_view->set_cursor(m_filter_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
  168. };
  169. m_text_box->on_down_pressed = [this] {
  170. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  171. };
  172. m_text_box->on_up_pressed = [this] {
  173. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set);
  174. };
  175. m_text_box->on_return_pressed = [this] {
  176. if (!m_table_view->selection().is_empty())
  177. finish_with_index(m_table_view->selection().first());
  178. };
  179. m_table_view->on_activation = [this](GUI::ModelIndex const& filter_index) {
  180. finish_with_index(filter_index);
  181. };
  182. m_text_box->set_focus(true);
  183. }
  184. void CommandPalette::collect_actions(GUI::Window& parent_window)
  185. {
  186. OrderedHashTable<NonnullRefPtr<GUI::Action>> actions;
  187. auto collect_action_children = [&](Core::Object& action_parent) {
  188. action_parent.for_each_child_of_type<GUI::Action>([&](GUI::Action& action) {
  189. if (action.is_enabled() && action.is_visible())
  190. actions.set(action);
  191. return IterationDecision::Continue;
  192. });
  193. };
  194. Function<bool(GUI::Action*)> should_show_action = [&](GUI::Action* action) {
  195. return action && action->is_enabled() && action->is_visible() && action->shortcut() != Shortcut(Mod_Ctrl | Mod_Shift, Key_A);
  196. };
  197. Function<void(Menu&)> collect_actions_from_menu = [&](Menu& menu) {
  198. for (auto& menu_item : menu.items()) {
  199. if (menu_item->submenu())
  200. collect_actions_from_menu(*menu_item->submenu());
  201. auto* action = menu_item->action();
  202. if (should_show_action(action))
  203. actions.set(*action);
  204. }
  205. };
  206. for (auto* widget = parent_window.focused_widget(); widget; widget = widget->parent_widget())
  207. collect_action_children(*widget);
  208. collect_action_children(parent_window);
  209. parent_window.menubar().for_each_menu([&](Menu& menu) {
  210. collect_actions_from_menu(menu);
  211. return IterationDecision::Continue;
  212. });
  213. if (!parent_window.is_modal()) {
  214. for (auto const& it : GUI::Application::the()->global_shortcut_actions({})) {
  215. if (should_show_action(it.value))
  216. actions.set(*it.value);
  217. }
  218. }
  219. m_actions.clear();
  220. for (auto& action : actions)
  221. m_actions.append(action);
  222. quick_sort(m_actions, [&](auto& a, auto& b) {
  223. // FIXME: This is so awkward. Don't be so awkward.
  224. return Gfx::parse_ampersand_string(a->text()) < Gfx::parse_ampersand_string(b->text());
  225. });
  226. }
  227. void CommandPalette::finish_with_index(GUI::ModelIndex const& filter_index)
  228. {
  229. if (!filter_index.is_valid())
  230. return;
  231. auto action_index = m_filter_model->map(filter_index);
  232. auto* action = static_cast<GUI::Action*>(action_index.internal_data());
  233. VERIFY(action);
  234. m_selected_action = action;
  235. done(ExecResult::OK);
  236. }
  237. }