Locator.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/BoxLayout.h>
  29. #include <LibGUI/TableView.h>
  30. #include <LibGUI/TextBox.h>
  31. #include <LibGUI/Window.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() {}
  86. };
  87. Locator::Locator()
  88. {
  89. if (!s_cplusplus_icon) {
  90. s_file_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png");
  91. s_cplusplus_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png");
  92. s_header_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-header.png");
  93. }
  94. set_layout<GUI::VerticalBoxLayout>();
  95. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  96. set_preferred_size(0, 20);
  97. m_textbox = add<LocatorTextBox>();
  98. m_textbox->on_change = [this] {
  99. update_suggestions();
  100. };
  101. m_textbox->on_escape_pressed = [this] {
  102. m_popup_window->hide();
  103. };
  104. m_textbox->on_up = [this] {
  105. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  106. if (new_index.is_valid())
  107. new_index = m_suggestion_view->model()->index(new_index.row() - 1);
  108. else
  109. new_index = m_suggestion_view->model()->index(0);
  110. if (m_suggestion_view->model()->is_valid(new_index)) {
  111. m_suggestion_view->selection().set(new_index);
  112. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  113. }
  114. };
  115. m_textbox->on_down = [this] {
  116. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  117. if (new_index.is_valid())
  118. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  119. else
  120. new_index = m_suggestion_view->model()->index(0);
  121. if (m_suggestion_view->model()->is_valid(new_index)) {
  122. m_suggestion_view->selection().set(new_index);
  123. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  124. }
  125. };
  126. m_textbox->on_return_pressed = [this] {
  127. auto selected_index = m_suggestion_view->selection().first();
  128. if (!selected_index.is_valid())
  129. return;
  130. open_suggestion(selected_index);
  131. };
  132. m_popup_window = GUI::Window::construct();
  133. // FIXME: This is obviously not a tooltip window, but it's the closest thing to what we want atm.
  134. m_popup_window->set_window_type(GUI::WindowType::Tooltip);
  135. m_popup_window->set_rect(0, 0, 500, 200);
  136. m_suggestion_view = m_popup_window->set_main_widget<GUI::TableView>();
  137. m_suggestion_view->set_size_columns_to_fit_content(true);
  138. m_suggestion_view->set_headers_visible(false);
  139. m_suggestion_view->on_activation = [this](auto& index) {
  140. open_suggestion(index);
  141. };
  142. }
  143. Locator::~Locator()
  144. {
  145. }
  146. void Locator::open_suggestion(const GUI::ModelIndex& index)
  147. {
  148. auto filename_index = m_suggestion_view->model()->index(index.row(), LocatorSuggestionModel::Column::Name);
  149. auto filename = m_suggestion_view->model()->data(filename_index, GUI::Model::Role::Display).to_string();
  150. open_file(filename);
  151. close();
  152. }
  153. void Locator::open()
  154. {
  155. m_textbox->set_focus(true);
  156. if (!m_textbox->text().is_empty()) {
  157. m_textbox->select_all();
  158. m_popup_window->show();
  159. }
  160. }
  161. void Locator::close()
  162. {
  163. m_popup_window->hide();
  164. }
  165. void Locator::update_suggestions()
  166. {
  167. auto typed_text = m_textbox->text();
  168. Vector<String> suggestions;
  169. g_project->for_each_text_file([&](auto& file) {
  170. if (file.name().contains(typed_text))
  171. suggestions.append(file.name());
  172. });
  173. dbg() << "I have " << suggestions.size() << " suggestion(s):";
  174. for (auto& s : suggestions) {
  175. dbg() << " " << s;
  176. }
  177. bool has_suggestions = !suggestions.is_empty();
  178. m_suggestion_view->set_model(adopt(*new LocatorSuggestionModel(move(suggestions))));
  179. if (!has_suggestions)
  180. m_suggestion_view->selection().clear();
  181. else
  182. m_suggestion_view->selection().set(m_suggestion_view->model()->index(0));
  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. }