Locator.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Locator.h"
  27. #include "Project.h"
  28. #include <LibGUI/GBoxLayout.h>
  29. #include <LibGUI/GTableView.h>
  30. #include <LibGUI/GTextBox.h>
  31. #include <LibGUI/GWindow.h>
  32. extern RefPtr<Project> g_project;
  33. extern void open_file(const String&);
  34. static RefPtr<Gfx::Bitmap> s_file_icon;
  35. static RefPtr<Gfx::Bitmap> s_cplusplus_icon;
  36. static RefPtr<Gfx::Bitmap> s_header_icon;
  37. class LocatorSuggestionModel final : public GUI::Model {
  38. public:
  39. explicit LocatorSuggestionModel(Vector<String>&& suggestions)
  40. : m_suggestions(move(suggestions))
  41. {
  42. }
  43. enum Column {
  44. Icon,
  45. Name,
  46. __Column_Count,
  47. };
  48. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_suggestions.size(); }
  49. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Column_Count; }
  50. virtual GUI::Variant data(const GUI::ModelIndex& index, Role role = Role::Display) const override
  51. {
  52. auto& suggestion = m_suggestions.at(index.row());
  53. if (role == Role::Display) {
  54. if (index.column() == Column::Name)
  55. return suggestion;
  56. if (index.column() == Column::Icon) {
  57. if (suggestion.ends_with(".cpp"))
  58. return *s_cplusplus_icon;
  59. if (suggestion.ends_with(".h"))
  60. return *s_header_icon;
  61. return *s_file_icon;
  62. }
  63. }
  64. return {};
  65. }
  66. virtual void update() override {};
  67. private:
  68. Vector<String> m_suggestions;
  69. };
  70. class LocatorTextBox final : public GUI::TextBox {
  71. C_OBJECT(LocatorTextBox)
  72. public:
  73. virtual ~LocatorTextBox() override {}
  74. Function<void()> on_up;
  75. Function<void()> on_down;
  76. virtual void keydown_event(GUI::KeyEvent& event) override
  77. {
  78. if (event.key() == Key_Up)
  79. on_up();
  80. else if (event.key() == Key_Down)
  81. on_down();
  82. GUI::TextBox::keydown_event(event);
  83. }
  84. private:
  85. LocatorTextBox(GUI::Widget* parent)
  86. : GUI::TextBox(parent)
  87. {
  88. }
  89. };
  90. Locator::Locator(GUI::Widget* parent)
  91. : GUI::Widget(parent)
  92. {
  93. if (!s_cplusplus_icon) {
  94. s_file_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png");
  95. s_cplusplus_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png");
  96. s_header_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-header.png");
  97. }
  98. set_layout(make<GUI::VBoxLayout>());
  99. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  100. set_preferred_size(0, 20);
  101. m_textbox = LocatorTextBox::construct(this);
  102. m_textbox->on_change = [this] {
  103. update_suggestions();
  104. };
  105. m_textbox->on_escape_pressed = [this] {
  106. m_popup_window->hide();
  107. };
  108. m_textbox->on_up = [this] {
  109. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  110. if (new_index.is_valid())
  111. new_index = m_suggestion_view->model()->index(new_index.row() - 1);
  112. else
  113. new_index = m_suggestion_view->model()->index(0);
  114. if (new_index.is_valid()) {
  115. m_suggestion_view->selection().set(new_index);
  116. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  117. }
  118. };
  119. m_textbox->on_down = [this] {
  120. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  121. if (new_index.is_valid())
  122. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  123. else
  124. new_index = m_suggestion_view->model()->index(0);
  125. if (new_index.is_valid()) {
  126. m_suggestion_view->selection().set(new_index);
  127. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  128. }
  129. };
  130. m_textbox->on_return_pressed = [this] {
  131. auto selected_index = m_suggestion_view->selection().first();
  132. if (!selected_index.is_valid())
  133. return;
  134. open_suggestion(selected_index);
  135. };
  136. m_popup_window = GUI::Window::construct();
  137. // FIXME: This is obviously not a tooltip window, but it's the closest thing to what we want atm.
  138. m_popup_window->set_window_type(GUI::WindowType::Tooltip);
  139. m_popup_window->set_rect(0, 0, 500, 200);
  140. m_suggestion_view = GUI::TableView::construct(nullptr);
  141. m_suggestion_view->set_size_columns_to_fit_content(true);
  142. m_suggestion_view->set_headers_visible(false);
  143. m_popup_window->set_main_widget(m_suggestion_view);
  144. m_suggestion_view->on_activation = [this](auto& index) {
  145. open_suggestion(index);
  146. };
  147. }
  148. Locator::~Locator()
  149. {
  150. }
  151. void Locator::open_suggestion(const GUI::ModelIndex& index)
  152. {
  153. auto filename_index = m_suggestion_view->model()->index(index.row(), LocatorSuggestionModel::Column::Name);
  154. auto filename = m_suggestion_view->model()->data(filename_index, GUI::Model::Role::Display).to_string();
  155. open_file(filename);
  156. close();
  157. }
  158. void Locator::open()
  159. {
  160. m_textbox->set_focus(true);
  161. if (!m_textbox->text().is_empty()) {
  162. m_textbox->select_all();
  163. m_popup_window->show();
  164. }
  165. }
  166. void Locator::close()
  167. {
  168. m_popup_window->hide();
  169. }
  170. void Locator::update_suggestions()
  171. {
  172. auto typed_text = m_textbox->text();
  173. Vector<String> suggestions;
  174. g_project->for_each_text_file([&](auto& file) {
  175. if (file.name().contains(typed_text))
  176. suggestions.append(file.name());
  177. });
  178. dbg() << "I have " << suggestions.size() << " suggestion(s):";
  179. for (auto& s : suggestions) {
  180. dbg() << " " << s;
  181. }
  182. m_suggestion_view->set_model(adopt(*new LocatorSuggestionModel(move(suggestions))));
  183. m_popup_window->move_to(screen_relative_rect().top_left().translated(0, -m_popup_window->height()));
  184. dbg() << "Popup rect: " << m_popup_window->rect();
  185. m_popup_window->show();
  186. }