AutoCompleteBox.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  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 "AutoCompleteBox.h"
  27. #include "Editor.h"
  28. #include <LibGUI/Model.h>
  29. #include <LibGUI/TableView.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibGfx/Bitmap.h>
  32. namespace HackStudio {
  33. static RefPtr<Gfx::Bitmap> s_cplusplus_icon;
  34. class AutoCompleteSuggestionModel final : public GUI::Model {
  35. public:
  36. explicit AutoCompleteSuggestionModel(Vector<String>&& suggestions)
  37. : m_suggestions(move(suggestions))
  38. {
  39. }
  40. enum Column {
  41. Icon,
  42. Name,
  43. __Column_Count,
  44. };
  45. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_suggestions.size(); }
  46. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Column_Count; }
  47. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
  48. {
  49. auto& suggestion = m_suggestions.at(index.row());
  50. if (role == GUI::ModelRole::Display) {
  51. if (index.column() == Column::Name) {
  52. return suggestion;
  53. }
  54. if (index.column() == Column::Icon) {
  55. // TODO: Have separate icons for fields, functions, methods etc
  56. if (suggestion.ends_with(".cpp"))
  57. return *s_cplusplus_icon;
  58. return *s_cplusplus_icon;
  59. }
  60. }
  61. return {};
  62. }
  63. virtual void update() override {};
  64. private:
  65. Vector<String> m_suggestions;
  66. };
  67. AutoCompleteBox::~AutoCompleteBox() { }
  68. AutoCompleteBox::AutoCompleteBox(WeakPtr<Editor> editor)
  69. : m_editor(move(editor))
  70. {
  71. if (!s_cplusplus_icon) {
  72. s_cplusplus_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png");
  73. }
  74. m_popup_window = GUI::Window::construct();
  75. m_popup_window->set_window_type(GUI::WindowType::Tooltip);
  76. m_popup_window->set_rect(0, 0, 200, 100);
  77. m_suggestion_view = m_popup_window->set_main_widget<GUI::TableView>();
  78. m_suggestion_view->set_column_headers_visible(false);
  79. }
  80. void AutoCompleteBox::update_suggestions(String partial_input, Vector<String>&& suggestions)
  81. {
  82. m_partial_input = partial_input;
  83. if (suggestions.is_empty())
  84. return;
  85. bool has_suggestions = !suggestions.is_empty();
  86. m_suggestion_view->set_model(adopt(*new AutoCompleteSuggestionModel(move(suggestions))));
  87. if (!has_suggestions)
  88. m_suggestion_view->selection().clear();
  89. else
  90. m_suggestion_view->selection().set(m_suggestion_view->model()->index(0));
  91. }
  92. void AutoCompleteBox::show(Gfx::IntPoint suggstion_box_location)
  93. {
  94. m_popup_window->move_to(suggstion_box_location);
  95. m_popup_window->show();
  96. }
  97. void AutoCompleteBox::close()
  98. {
  99. m_partial_input.empty();
  100. m_popup_window->hide();
  101. }
  102. void AutoCompleteBox::next_suggestion()
  103. {
  104. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  105. if (new_index.is_valid())
  106. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  107. else
  108. new_index = m_suggestion_view->model()->index(0);
  109. if (m_suggestion_view->model()->is_valid(new_index)) {
  110. m_suggestion_view->selection().set(new_index);
  111. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  112. }
  113. }
  114. void AutoCompleteBox::previous_suggestion()
  115. {
  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. void AutoCompleteBox::apply_suggestion()
  127. {
  128. if (m_editor.is_null())
  129. return;
  130. auto selected_index = m_suggestion_view->selection().first();
  131. if (!selected_index.is_valid())
  132. return;
  133. auto suggestion_index = m_suggestion_view->model()->index(selected_index.row(), AutoCompleteSuggestionModel::Column::Name);
  134. auto suggestion = suggestion_index.data().to_string();
  135. ASSERT(!m_partial_input.is_null());
  136. ASSERT(suggestion.starts_with(m_partial_input));
  137. auto completion = suggestion.substring(m_partial_input.length(), suggestion.length() - m_partial_input.length());
  138. m_editor->insert_at_cursor_or_replace_selection(completion);
  139. }
  140. };