Locator.cpp 7.1 KB

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