CommandPalette.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 String 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_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 = String::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 String 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 String 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_frameless(true);
  147. set_blocks_emoji_input(true);
  148. resize(450, 300);
  149. collect_actions(parent_window);
  150. auto& main_widget = set_main_widget<GUI::Frame>();
  151. main_widget.set_frame_shape(Gfx::FrameShape::Window);
  152. main_widget.set_fill_with_background_color(true);
  153. auto& layout = main_widget.set_layout<GUI::VerticalBoxLayout>();
  154. layout.set_margins(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_text_box->on_change = [this] {
  164. m_filter_model->set_filter_term(m_text_box->text());
  165. if (m_filter_model->row_count() != 0)
  166. m_table_view->set_cursor(m_filter_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
  167. };
  168. m_text_box->on_down_pressed = [this] {
  169. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  170. };
  171. m_text_box->on_up_pressed = [this] {
  172. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set);
  173. };
  174. m_text_box->on_return_pressed = [this] {
  175. if (!m_table_view->selection().is_empty())
  176. finish_with_index(m_table_view->selection().first());
  177. };
  178. m_table_view->on_activation = [this](GUI::ModelIndex const& filter_index) {
  179. finish_with_index(filter_index);
  180. };
  181. m_text_box->set_focus(true);
  182. on_active_input_change = [this](bool is_active_input) {
  183. if (!is_active_input)
  184. close();
  185. };
  186. on_input_preemption = [this](InputPreemptor preemptor) {
  187. if (preemptor != InputPreemptor::ContextMenu)
  188. close();
  189. };
  190. }
  191. void CommandPalette::collect_actions(GUI::Window& parent_window)
  192. {
  193. OrderedHashTable<NonnullRefPtr<GUI::Action>> actions;
  194. auto collect_action_children = [&](Core::Object& action_parent) {
  195. action_parent.for_each_child_of_type<GUI::Action>([&](GUI::Action& action) {
  196. if (action.is_enabled())
  197. actions.set(action);
  198. return IterationDecision::Continue;
  199. });
  200. };
  201. Function<bool(GUI::Action*)> should_show_action = [&](GUI::Action* action) {
  202. return action && action->is_enabled() && action->shortcut() != Shortcut(Mod_Ctrl | Mod_Shift, Key_A);
  203. };
  204. Function<void(Menu&)> collect_actions_from_menu = [&](Menu& menu) {
  205. for (auto menu_item : menu.items()) {
  206. if (menu_item.submenu())
  207. collect_actions_from_menu(*menu_item.submenu());
  208. auto* action = menu_item.action();
  209. if (should_show_action(action))
  210. actions.set(*action);
  211. }
  212. };
  213. for (auto* widget = parent_window.focused_widget(); widget; widget = widget->parent_widget())
  214. collect_action_children(*widget);
  215. collect_action_children(parent_window);
  216. parent_window.menubar().for_each_menu([&](Menu& menu) {
  217. collect_actions_from_menu(menu);
  218. return IterationDecision::Continue;
  219. });
  220. if (!parent_window.is_modal()) {
  221. for (auto const& it : GUI::Application::the()->global_shortcut_actions({})) {
  222. if (should_show_action(it.value))
  223. actions.set(*it.value);
  224. }
  225. }
  226. m_actions.clear();
  227. for (auto& action : actions)
  228. m_actions.append(action);
  229. quick_sort(m_actions, [&](auto& a, auto& b) {
  230. // FIXME: This is so awkward. Don't be so awkward.
  231. return Gfx::parse_ampersand_string(a->text()) < Gfx::parse_ampersand_string(b->text());
  232. });
  233. }
  234. void CommandPalette::finish_with_index(GUI::ModelIndex const& filter_index)
  235. {
  236. if (!filter_index.is_valid())
  237. return;
  238. auto action_index = m_filter_model->map(filter_index);
  239. auto* action = static_cast<GUI::Action*>(action_index.internal_data());
  240. VERIFY(action);
  241. m_selected_action = action;
  242. done(ExecResult::OK);
  243. }
  244. }