CommandPalette.cpp 5.3 KB

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