CommandPalette.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Jakob-Niklas See <git@nwex.de>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/QuickSort.h>
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/ActionGroup.h>
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/BoxLayout.h>
  12. #include <LibGUI/CommandPalette.h>
  13. #include <LibGUI/FilteringProxyModel.h>
  14. #include <LibGUI/Model.h>
  15. #include <LibGUI/Painter.h>
  16. #include <LibGUI/TableView.h>
  17. #include <LibGUI/TextBox.h>
  18. #include <LibGUI/Widget.h>
  19. #include <LibGfx/Painter.h>
  20. namespace GUI {
  21. enum class IconFlags : unsigned {
  22. Checkable = 0,
  23. Exclusive = 1,
  24. Checked = 2,
  25. };
  26. AK_ENUM_BITWISE_OPERATORS(IconFlags);
  27. class ActionIconDelegate final : public GUI::TableCellPaintingDelegate {
  28. public:
  29. virtual ~ActionIconDelegate() override { }
  30. bool should_paint(ModelIndex const& index) override
  31. {
  32. return index.data().is_u32();
  33. }
  34. virtual void paint(GUI::Painter& painter, Gfx::IntRect const& cell_rect, Gfx::Palette const& palette, ModelIndex const& index) override
  35. {
  36. auto flags = static_cast<IconFlags>(index.data().as_u32());
  37. VERIFY(has_flag(flags, IconFlags::Checkable));
  38. auto exclusive = has_flag(flags, IconFlags::Exclusive);
  39. auto checked = has_flag(flags, IconFlags::Checked);
  40. if (exclusive) {
  41. Gfx::IntRect radio_rect { 0, 0, 12, 12 };
  42. radio_rect.center_within(cell_rect);
  43. Gfx::StylePainter::paint_radio_button(painter, radio_rect, palette, checked, false);
  44. } else {
  45. Gfx::IntRect radio_rect { 0, 0, 13, 13 };
  46. radio_rect.center_within(cell_rect);
  47. Gfx::StylePainter::paint_check_box(painter, radio_rect, palette, true, checked, false);
  48. }
  49. }
  50. };
  51. class ActionModel final : public GUI::Model {
  52. public:
  53. enum Column {
  54. Icon,
  55. Text,
  56. Shortcut,
  57. __Count,
  58. };
  59. ActionModel(Vector<NonnullRefPtr<GUI::Action>>& actions)
  60. : m_actions(actions)
  61. {
  62. }
  63. virtual ~ActionModel() override { }
  64. virtual int row_count(ModelIndex const& parent_index) const override
  65. {
  66. if (!parent_index.is_valid())
  67. return m_actions.size();
  68. return 0;
  69. }
  70. virtual int column_count(ModelIndex const& = ModelIndex()) const override
  71. {
  72. return Column::__Count;
  73. }
  74. virtual String column_name(int) const override { return {}; }
  75. virtual ModelIndex index(int row, int column = 0, ModelIndex const& = ModelIndex()) const override
  76. {
  77. return create_index(row, column, m_actions.at(row).ptr());
  78. }
  79. virtual Variant data(ModelIndex const& index, ModelRole role = ModelRole::Display) const override
  80. {
  81. if (role != ModelRole::Display)
  82. return {};
  83. auto& action = *static_cast<GUI::Action*>(index.internal_data());
  84. switch (index.column()) {
  85. case Column::Icon:
  86. if (action.icon())
  87. return *action.icon();
  88. if (action.is_checkable()) {
  89. auto flags = IconFlags::Checkable;
  90. if (action.is_checked())
  91. flags |= IconFlags::Checked;
  92. if (action.group() && action.group()->is_exclusive())
  93. flags |= IconFlags::Exclusive;
  94. return (u32)flags;
  95. }
  96. return "";
  97. case Column::Text:
  98. return Gfx::parse_ampersand_string(action.text());
  99. case Column::Shortcut:
  100. if (!action.shortcut().is_valid())
  101. return "";
  102. return action.shortcut().to_string();
  103. }
  104. VERIFY_NOT_REACHED();
  105. }
  106. virtual TriState data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override
  107. {
  108. auto& action = *static_cast<GUI::Action*>(index.internal_data());
  109. auto text = Gfx::parse_ampersand_string(action.text());
  110. if (text.contains(term.as_string(), CaseSensitivity::CaseInsensitive))
  111. return TriState::True;
  112. return TriState::False;
  113. }
  114. private:
  115. Vector<NonnullRefPtr<GUI::Action>> const& m_actions;
  116. };
  117. CommandPalette::CommandPalette(GUI::Window& parent_window, ScreenPosition screen_position)
  118. : GUI::Dialog(&parent_window, screen_position)
  119. {
  120. set_frameless(true);
  121. resize(400, 300);
  122. collect_actions(parent_window);
  123. auto& main_widget = set_main_widget<GUI::Frame>();
  124. main_widget.set_frame_shadow(Gfx::FrameShadow::Raised);
  125. main_widget.set_fill_with_background_color(true);
  126. auto& layout = main_widget.set_layout<GUI::VerticalBoxLayout>();
  127. layout.set_margins(4);
  128. m_text_box = main_widget.add<GUI::TextBox>();
  129. m_table_view = main_widget.add<GUI::TableView>();
  130. m_model = adopt_ref(*new ActionModel(m_actions));
  131. m_table_view->set_column_headers_visible(false);
  132. m_filter_model = MUST(GUI::FilteringProxyModel::create(*m_model));
  133. m_filter_model->set_filter_term("");
  134. m_table_view->set_column_painting_delegate(0, make<ActionIconDelegate>());
  135. m_table_view->set_model(*m_filter_model);
  136. m_text_box->on_change = [this] {
  137. m_filter_model->set_filter_term(m_text_box->text());
  138. if (m_filter_model->row_count() != 0)
  139. m_table_view->set_cursor(m_filter_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
  140. };
  141. m_text_box->on_down_pressed = [this] {
  142. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  143. };
  144. m_text_box->on_up_pressed = [this] {
  145. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set);
  146. };
  147. m_text_box->on_return_pressed = [this] {
  148. if (!m_table_view->selection().is_empty())
  149. finish_with_index(m_table_view->selection().first());
  150. };
  151. m_table_view->on_activation = [this](GUI::ModelIndex const& filter_index) {
  152. finish_with_index(filter_index);
  153. };
  154. m_text_box->set_focus(true);
  155. }
  156. CommandPalette::~CommandPalette()
  157. {
  158. }
  159. void CommandPalette::collect_actions(GUI::Window& parent_window)
  160. {
  161. OrderedHashTable<NonnullRefPtr<GUI::Action>> actions;
  162. auto collect_action_children = [&](Core::Object& action_parent) {
  163. action_parent.for_each_child_of_type<GUI::Action>([&](GUI::Action& action) {
  164. if (action.is_enabled())
  165. actions.set(action);
  166. return IterationDecision::Continue;
  167. });
  168. };
  169. for (auto* widget = parent_window.focused_widget(); widget; widget = widget->parent_widget())
  170. collect_action_children(*widget);
  171. collect_action_children(parent_window);
  172. if (!parent_window.is_modal()) {
  173. for (auto const& it : GUI::Application::the()->global_shortcut_actions({})) {
  174. if (it.value->is_enabled())
  175. actions.set(*it.value);
  176. }
  177. }
  178. m_actions.clear();
  179. for (auto& action : actions)
  180. m_actions.append(action);
  181. quick_sort(m_actions, [&](auto& a, auto& b) {
  182. // FIXME: This is so awkward. Don't be so awkward.
  183. return Gfx::parse_ampersand_string(a->text()) < Gfx::parse_ampersand_string(b->text());
  184. });
  185. }
  186. void CommandPalette::finish_with_index(GUI::ModelIndex const& filter_index)
  187. {
  188. if (!filter_index.is_valid())
  189. return;
  190. auto action_index = m_filter_model->map(filter_index);
  191. auto* action = static_cast<GUI::Action*>(action_index.internal_data());
  192. VERIFY(action);
  193. m_selected_action = action;
  194. done(ExecOK);
  195. }
  196. }