CommandPalette.cpp 6.5 KB

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