CommandPalette.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 ErrorOr<String> column_name(int) const override { return String {}; }
  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 GUI::Model::MatchResult 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. auto match_result = fuzzy_match(needle, haystack);
  121. if (match_result.score > 0)
  122. return { TriState::True, match_result.score };
  123. return { TriState::False };
  124. }
  125. static DeprecatedString action_text(ModelIndex const& index)
  126. {
  127. auto& action = *static_cast<GUI::Action*>(index.internal_data());
  128. return Gfx::parse_ampersand_string(action.text());
  129. }
  130. static DeprecatedString menu_name(ModelIndex const& index)
  131. {
  132. auto& action = *static_cast<GUI::Action*>(index.internal_data());
  133. if (action.menu_items().is_empty())
  134. return {};
  135. auto* menu_item = *action.menu_items().begin();
  136. auto* menu = Menu::from_menu_id(menu_item->menu_id());
  137. if (!menu)
  138. return {};
  139. return Gfx::parse_ampersand_string(menu->name());
  140. }
  141. private:
  142. Vector<NonnullRefPtr<GUI::Action>> const& m_actions;
  143. };
  144. CommandPalette::CommandPalette(GUI::Window& parent_window, ScreenPosition screen_position)
  145. : GUI::Dialog(&parent_window, screen_position)
  146. {
  147. set_window_type(GUI::WindowType::Popup);
  148. set_window_mode(GUI::WindowMode::Modeless);
  149. set_blocks_emoji_input(true);
  150. resize(450, 300);
  151. collect_actions(parent_window);
  152. auto main_widget = set_main_widget<GUI::Frame>().release_value_but_fixme_should_propagate_errors();
  153. main_widget->set_frame_style(Gfx::FrameStyle::Window);
  154. main_widget->set_fill_with_background_color(true);
  155. main_widget->set_layout<GUI::VerticalBoxLayout>(4);
  156. m_text_box = main_widget->add<GUI::TextBox>();
  157. m_table_view = main_widget->add<GUI::TableView>();
  158. m_model = adopt_ref(*new ActionModel(m_actions));
  159. m_table_view->set_column_headers_visible(false);
  160. m_filter_model = MUST(GUI::FilteringProxyModel::create(*m_model));
  161. m_filter_model->set_filter_term(""sv);
  162. m_table_view->set_column_painting_delegate(0, make<ActionIconDelegate>());
  163. m_table_view->set_model(*m_filter_model);
  164. m_table_view->set_focus_proxy(m_text_box);
  165. m_text_box->on_change = [this] {
  166. m_filter_model->set_filter_term(m_text_box->text());
  167. if (m_filter_model->row_count() != 0)
  168. m_table_view->set_cursor(m_filter_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
  169. };
  170. m_text_box->on_down_pressed = [this] {
  171. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  172. };
  173. m_text_box->on_up_pressed = [this] {
  174. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set);
  175. };
  176. m_text_box->on_return_pressed = [this] {
  177. if (!m_table_view->selection().is_empty())
  178. finish_with_index(m_table_view->selection().first());
  179. };
  180. m_table_view->on_activation = [this](GUI::ModelIndex const& filter_index) {
  181. finish_with_index(filter_index);
  182. };
  183. m_text_box->set_focus(true);
  184. }
  185. void CommandPalette::collect_actions(GUI::Window& parent_window)
  186. {
  187. OrderedHashTable<NonnullRefPtr<GUI::Action>> actions;
  188. auto collect_action_children = [&](Core::EventReceiver& action_parent) {
  189. action_parent.for_each_child_of_type<GUI::Action>([&](GUI::Action& action) {
  190. if (action.is_enabled() && action.is_visible())
  191. actions.set(action);
  192. return IterationDecision::Continue;
  193. });
  194. };
  195. Function<bool(GUI::Action*)> should_show_action = [&](GUI::Action* action) {
  196. return action && action->is_enabled() && action->is_visible() && action->shortcut() != Shortcut(Mod_Ctrl | Mod_Shift, Key_A);
  197. };
  198. Function<void(Menu&)> collect_actions_from_menu = [&](Menu& menu) {
  199. for (auto& menu_item : menu.items()) {
  200. if (menu_item->submenu())
  201. collect_actions_from_menu(*menu_item->submenu());
  202. auto* action = menu_item->action();
  203. if (should_show_action(action))
  204. actions.set(*action);
  205. }
  206. };
  207. for (auto* widget = parent_window.focused_widget(); widget; widget = widget->parent_widget())
  208. collect_action_children(*widget);
  209. collect_action_children(parent_window);
  210. parent_window.menubar().for_each_menu([&](Menu& menu) {
  211. collect_actions_from_menu(menu);
  212. return IterationDecision::Continue;
  213. });
  214. if (!parent_window.is_modal()) {
  215. for (auto const& it : GUI::Application::the()->global_shortcut_actions({})) {
  216. if (should_show_action(it.value))
  217. actions.set(*it.value);
  218. }
  219. }
  220. m_actions.clear();
  221. for (auto& action : actions)
  222. m_actions.append(action);
  223. quick_sort(m_actions, [&](auto& a, auto& b) {
  224. // FIXME: This is so awkward. Don't be so awkward.
  225. return Gfx::parse_ampersand_string(a->text()) < Gfx::parse_ampersand_string(b->text());
  226. });
  227. }
  228. void CommandPalette::finish_with_index(GUI::ModelIndex const& filter_index)
  229. {
  230. if (!filter_index.is_valid())
  231. return;
  232. auto action_index = m_filter_model->map(filter_index);
  233. auto* action = static_cast<GUI::Action*>(action_index.internal_data());
  234. VERIFY(action);
  235. m_selected_action = action;
  236. done(ExecResult::OK);
  237. }
  238. }