CommandPalette.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Action.h>
  7. #include <LibGUI/Application.h>
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/CommandPalette.h>
  10. #include <LibGUI/FilteringProxyModel.h>
  11. #include <LibGUI/Model.h>
  12. #include <LibGUI/TableView.h>
  13. #include <LibGUI/TextBox.h>
  14. #include <LibGUI/Widget.h>
  15. #include <LibGfx/Painter.h>
  16. namespace GUI {
  17. class ActionModel final : public GUI::Model {
  18. public:
  19. enum Column {
  20. Icon,
  21. Text,
  22. Shortcut,
  23. __Count,
  24. };
  25. ActionModel(NonnullRefPtrVector<GUI::Action>& actions)
  26. : m_actions(actions)
  27. {
  28. }
  29. virtual ~ActionModel() override { }
  30. virtual int row_count(ModelIndex const& parent_index) const override
  31. {
  32. if (!parent_index.is_valid())
  33. return m_actions.size();
  34. return 0;
  35. }
  36. virtual int column_count(ModelIndex const& = ModelIndex()) const override
  37. {
  38. return Column::__Count;
  39. }
  40. virtual String column_name(int) const override { return {}; }
  41. virtual ModelIndex index(int row, int column = 0, ModelIndex const& = ModelIndex()) const override
  42. {
  43. return create_index(row, column, m_actions.ptr_at(row).ptr());
  44. }
  45. virtual Variant data(ModelIndex const& index, ModelRole role = ModelRole::Display) const override
  46. {
  47. if (role != ModelRole::Display)
  48. return {};
  49. auto& action = *static_cast<GUI::Action*>(index.internal_data());
  50. switch (index.column()) {
  51. case Column::Icon:
  52. if (action.icon())
  53. return *action.icon();
  54. return "";
  55. case Column::Text:
  56. return Gfx::parse_ampersand_string(action.text());
  57. case Column::Shortcut:
  58. if (!action.shortcut().is_valid())
  59. return "";
  60. return action.shortcut().to_string();
  61. }
  62. VERIFY_NOT_REACHED();
  63. }
  64. virtual TriState data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override
  65. {
  66. auto& action = *static_cast<GUI::Action*>(index.internal_data());
  67. auto text = Gfx::parse_ampersand_string(action.text());
  68. if (text.contains(term.as_string(), CaseSensitivity::CaseInsensitive))
  69. return TriState::True;
  70. return TriState::False;
  71. }
  72. private:
  73. NonnullRefPtrVector<GUI::Action> const& m_actions;
  74. };
  75. CommandPalette::CommandPalette(GUI::Window& parent_window, ScreenPosition screen_position)
  76. : GUI::Dialog(&parent_window, screen_position)
  77. {
  78. set_frameless(true);
  79. resize(400, 300);
  80. collect_actions(parent_window);
  81. auto& main_widget = set_main_widget<GUI::Frame>();
  82. main_widget.set_frame_shadow(Gfx::FrameShadow::Raised);
  83. main_widget.set_fill_with_background_color(true);
  84. auto& layout = main_widget.set_layout<GUI::VerticalBoxLayout>();
  85. layout.set_margins(4);
  86. m_text_box = main_widget.add<GUI::TextBox>();
  87. m_table_view = main_widget.add<GUI::TableView>();
  88. m_model = adopt_ref(*new ActionModel(m_actions));
  89. m_table_view->set_column_headers_visible(false);
  90. m_filter_model = MUST(GUI::FilteringProxyModel::create(*m_model));
  91. m_filter_model->set_filter_term("");
  92. m_table_view->set_model(*m_filter_model);
  93. m_text_box->on_change = [this] {
  94. m_filter_model->set_filter_term(m_text_box->text());
  95. if (m_filter_model->row_count() != 0)
  96. m_table_view->set_cursor(m_filter_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
  97. };
  98. m_text_box->on_down_pressed = [this] {
  99. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  100. };
  101. m_text_box->on_up_pressed = [this] {
  102. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set);
  103. };
  104. m_text_box->on_return_pressed = [this] {
  105. if (!m_table_view->selection().is_empty())
  106. finish_with_index(m_table_view->selection().first());
  107. };
  108. m_table_view->on_activation = [this](GUI::ModelIndex const& filter_index) {
  109. finish_with_index(filter_index);
  110. };
  111. m_text_box->set_focus(true);
  112. }
  113. CommandPalette::~CommandPalette()
  114. {
  115. }
  116. void CommandPalette::collect_actions(GUI::Window& parent_window)
  117. {
  118. OrderedHashTable<NonnullRefPtr<GUI::Action>> actions;
  119. auto collect_action_children = [&](Core::Object& action_parent) {
  120. action_parent.for_each_child_of_type<GUI::Action>([&](GUI::Action& action) {
  121. if (action.is_enabled())
  122. actions.set(action);
  123. return IterationDecision::Continue;
  124. });
  125. };
  126. for (auto* widget = parent_window.focused_widget(); widget; widget = widget->parent_widget())
  127. collect_action_children(*widget);
  128. collect_action_children(parent_window);
  129. if (!parent_window.is_modal()) {
  130. for (auto const& it : GUI::Application::the()->global_shortcut_actions({})) {
  131. if (it.value->is_enabled())
  132. actions.set(*it.value);
  133. }
  134. }
  135. m_actions.clear();
  136. for (auto& action : actions)
  137. m_actions.append(action);
  138. }
  139. void CommandPalette::finish_with_index(GUI::ModelIndex const& filter_index)
  140. {
  141. if (!filter_index.is_valid())
  142. return;
  143. auto action_index = m_filter_model->map(filter_index);
  144. auto* action = static_cast<GUI::Action*>(action_index.internal_data());
  145. VERIFY(action);
  146. m_selected_action = action;
  147. done(ExecOK);
  148. }
  149. }