Locator.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "HackStudio.h"
  28. #include "Project.h"
  29. #include <LibGUI/BoxLayout.h>
  30. #include <LibGUI/TableView.h>
  31. #include <LibGUI/TextBox.h>
  32. #include <LibGUI/Window.h>
  33. static RefPtr<Gfx::Bitmap> s_file_icon;
  34. static RefPtr<Gfx::Bitmap> s_cplusplus_icon;
  35. static RefPtr<Gfx::Bitmap> s_header_icon;
  36. class LocatorSuggestionModel final : public GUI::Model {
  37. public:
  38. explicit LocatorSuggestionModel(Vector<String>&& suggestions)
  39. : m_suggestions(move(suggestions))
  40. {
  41. }
  42. enum Column {
  43. Icon,
  44. Name,
  45. __Column_Count,
  46. };
  47. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_suggestions.size(); }
  48. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Column_Count; }
  49. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
  50. {
  51. auto& suggestion = m_suggestions.at(index.row());
  52. if (role == GUI::ModelRole::Display) {
  53. if (index.column() == Column::Name)
  54. return suggestion;
  55. if (index.column() == Column::Icon) {
  56. if (suggestion.ends_with(".cpp"))
  57. return *s_cplusplus_icon;
  58. if (suggestion.ends_with(".h"))
  59. return *s_header_icon;
  60. return *s_file_icon;
  61. }
  62. }
  63. return {};
  64. }
  65. virtual void update() override {};
  66. private:
  67. Vector<String> m_suggestions;
  68. };
  69. Locator::Locator()
  70. {
  71. if (!s_cplusplus_icon) {
  72. s_file_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png");
  73. s_cplusplus_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png");
  74. s_header_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-header.png");
  75. }
  76. set_layout<GUI::VerticalBoxLayout>();
  77. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  78. set_preferred_size(0, 20);
  79. m_textbox = add<GUI::TextBox>();
  80. m_textbox->on_change = [this] {
  81. update_suggestions();
  82. };
  83. m_textbox->on_escape_pressed = [this] {
  84. m_popup_window->hide();
  85. };
  86. m_textbox->on_up_pressed = [this] {
  87. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  88. if (new_index.is_valid())
  89. new_index = m_suggestion_view->model()->index(new_index.row() - 1);
  90. else
  91. new_index = m_suggestion_view->model()->index(0);
  92. if (m_suggestion_view->model()->is_valid(new_index)) {
  93. m_suggestion_view->selection().set(new_index);
  94. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  95. }
  96. };
  97. m_textbox->on_down_pressed = [this] {
  98. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  99. if (new_index.is_valid())
  100. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  101. else
  102. new_index = m_suggestion_view->model()->index(0);
  103. if (m_suggestion_view->model()->is_valid(new_index)) {
  104. m_suggestion_view->selection().set(new_index);
  105. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  106. }
  107. };
  108. m_textbox->on_return_pressed = [this] {
  109. auto selected_index = m_suggestion_view->selection().first();
  110. if (!selected_index.is_valid())
  111. return;
  112. open_suggestion(selected_index);
  113. };
  114. m_popup_window = GUI::Window::construct();
  115. // FIXME: This is obviously not a tooltip window, but it's the closest thing to what we want atm.
  116. m_popup_window->set_window_type(GUI::WindowType::Tooltip);
  117. m_popup_window->set_rect(0, 0, 500, 200);
  118. m_suggestion_view = m_popup_window->set_main_widget<GUI::TableView>();
  119. m_suggestion_view->set_headers_visible(false);
  120. m_suggestion_view->on_activation = [this](auto& index) {
  121. open_suggestion(index);
  122. };
  123. }
  124. Locator::~Locator()
  125. {
  126. }
  127. void Locator::open_suggestion(const GUI::ModelIndex& index)
  128. {
  129. auto filename_index = m_suggestion_view->model()->index(index.row(), LocatorSuggestionModel::Column::Name);
  130. auto filename = filename_index.data().to_string();
  131. open_file(filename);
  132. close();
  133. }
  134. void Locator::open()
  135. {
  136. m_textbox->set_focus(true);
  137. if (!m_textbox->text().is_empty()) {
  138. m_textbox->select_all();
  139. m_popup_window->show();
  140. }
  141. }
  142. void Locator::close()
  143. {
  144. m_popup_window->hide();
  145. }
  146. void Locator::update_suggestions()
  147. {
  148. auto typed_text = m_textbox->text();
  149. Vector<String> suggestions;
  150. g_project->for_each_text_file([&](auto& file) {
  151. if (file.name().contains(typed_text))
  152. suggestions.append(file.name());
  153. });
  154. dbg() << "I have " << suggestions.size() << " suggestion(s):";
  155. for (auto& s : suggestions) {
  156. dbg() << " " << s;
  157. }
  158. bool has_suggestions = !suggestions.is_empty();
  159. m_suggestion_view->set_model(adopt(*new LocatorSuggestionModel(move(suggestions))));
  160. if (!has_suggestions)
  161. m_suggestion_view->selection().clear();
  162. else
  163. m_suggestion_view->selection().set(m_suggestion_view->model()->index(0));
  164. m_popup_window->move_to(screen_relative_rect().top_left().translated(0, -m_popup_window->height()));
  165. dbg() << "Popup rect: " << m_popup_window->rect();
  166. m_popup_window->show();
  167. }