Locator.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "Locator.h"
  2. #include "Project.h"
  3. #include <LibGUI/GBoxLayout.h>
  4. #include <LibGUI/GTableView.h>
  5. #include <LibGUI/GTextBox.h>
  6. #include <LibGUI/GWindow.h>
  7. extern RefPtr<Project> g_project;
  8. extern void open_file(const String&);
  9. static RefPtr<GraphicsBitmap> s_file_icon;
  10. static RefPtr<GraphicsBitmap> s_cplusplus_icon;
  11. static RefPtr<GraphicsBitmap> s_header_icon;
  12. class LocatorSuggestionModel final : public GModel {
  13. public:
  14. explicit LocatorSuggestionModel(Vector<String>&& suggestions)
  15. : m_suggestions(move(suggestions))
  16. {
  17. }
  18. enum Column {
  19. Icon,
  20. Name,
  21. __Column_Count,
  22. };
  23. virtual int row_count(const GModelIndex& = GModelIndex()) const override { return m_suggestions.size(); }
  24. virtual int column_count(const GModelIndex& = GModelIndex()) const override { return Column::__Column_Count; }
  25. virtual GVariant data(const GModelIndex& index, Role role = Role::Display) const override
  26. {
  27. auto& suggestion = m_suggestions.at(index.row());
  28. if (role == Role::Display) {
  29. if (index.column() == Column::Name)
  30. return suggestion;
  31. if (index.column() == Column::Icon) {
  32. if (suggestion.ends_with(".cpp"))
  33. return *s_cplusplus_icon;
  34. if (suggestion.ends_with(".h"))
  35. return *s_header_icon;
  36. return *s_file_icon;
  37. }
  38. }
  39. return {};
  40. }
  41. virtual void update() override {};
  42. private:
  43. Vector<String> m_suggestions;
  44. };
  45. class LocatorTextBox final : public GTextBox {
  46. C_OBJECT(LocatorTextBox)
  47. public:
  48. virtual ~LocatorTextBox() override {}
  49. Function<void()> on_up;
  50. Function<void()> on_down;
  51. virtual void keydown_event(GKeyEvent& event) override
  52. {
  53. if (event.key() == Key_Up)
  54. on_up();
  55. else if (event.key() == Key_Down)
  56. on_down();
  57. GTextBox::keydown_event(event);
  58. }
  59. private:
  60. LocatorTextBox(GWidget* parent)
  61. : GTextBox(parent)
  62. {
  63. }
  64. };
  65. Locator::Locator(GWidget* parent)
  66. : GWidget(parent)
  67. {
  68. if (!s_cplusplus_icon) {
  69. s_file_icon = GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-unknown.png");
  70. s_cplusplus_icon = GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png");
  71. s_header_icon = GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-header.png");
  72. }
  73. set_layout(make<GBoxLayout>(Orientation::Vertical));
  74. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  75. set_preferred_size(0, 20);
  76. m_textbox = LocatorTextBox::construct(this);
  77. m_textbox->on_change = [this] {
  78. update_suggestions();
  79. };
  80. m_textbox->on_escape_pressed = [this] {
  81. m_popup_window->hide();
  82. };
  83. m_textbox->on_up = [this] {
  84. GModelIndex new_index = m_suggestion_view->selection().first();
  85. if (new_index.is_valid())
  86. new_index = m_suggestion_view->model()->index(new_index.row() - 1);
  87. else
  88. new_index = m_suggestion_view->model()->index(0);
  89. if (new_index.is_valid()) {
  90. m_suggestion_view->selection().set(new_index);
  91. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  92. }
  93. };
  94. m_textbox->on_down = [this] {
  95. GModelIndex new_index = m_suggestion_view->selection().first();
  96. if (new_index.is_valid())
  97. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  98. else
  99. new_index = m_suggestion_view->model()->index(0);
  100. if (new_index.is_valid()) {
  101. m_suggestion_view->selection().set(new_index);
  102. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  103. }
  104. };
  105. m_textbox->on_return_pressed = [this] {
  106. auto selected_index = m_suggestion_view->selection().first();
  107. if (!selected_index.is_valid())
  108. return;
  109. open_suggestion(selected_index);
  110. };
  111. m_popup_window = GWindow::construct();
  112. // FIXME: This is obviously not a tooltip window, but it's the closest thing to what we want atm.
  113. m_popup_window->set_window_type(GWindowType::Tooltip);
  114. m_popup_window->set_rect(0, 0, 500, 200);
  115. m_suggestion_view = GTableView::construct(nullptr);
  116. m_suggestion_view->set_size_columns_to_fit_content(true);
  117. m_suggestion_view->set_headers_visible(false);
  118. m_popup_window->set_main_widget(m_suggestion_view);
  119. m_suggestion_view->on_activation = [this](auto& index) {
  120. open_suggestion(index);
  121. };
  122. }
  123. Locator::~Locator()
  124. {
  125. }
  126. void Locator::open_suggestion(const GModelIndex& index)
  127. {
  128. auto filename_index = m_suggestion_view->model()->index(index.row(), LocatorSuggestionModel::Column::Name);
  129. auto filename = m_suggestion_view->model()->data(filename_index, GModel::Role::Display).to_string();
  130. open_file(filename);
  131. close();
  132. }
  133. void Locator::open()
  134. {
  135. m_textbox->set_focus(true);
  136. if (!m_textbox->text().is_empty()) {
  137. m_textbox->select_all();
  138. m_popup_window->show();
  139. }
  140. }
  141. void Locator::close()
  142. {
  143. m_popup_window->hide();
  144. }
  145. void Locator::update_suggestions()
  146. {
  147. auto typed_text = m_textbox->text();
  148. Vector<String> suggestions;
  149. g_project->for_each_text_file([&](auto& file) {
  150. if (file.name().contains(typed_text))
  151. suggestions.append(file.name());
  152. });
  153. dbg() << "I have " << suggestions.size() << " suggestion(s):";
  154. for (auto& s : suggestions) {
  155. dbg() << " " << s;
  156. }
  157. m_suggestion_view->set_model(adopt(*new LocatorSuggestionModel(move(suggestions))));
  158. m_popup_window->move_to(screen_relative_rect().top_left().translated(0, -m_popup_window->height()));
  159. dbg() << "Popup rect: " << m_popup_window->rect();
  160. m_popup_window->show();
  161. }