editor_quick_open_dialog.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**************************************************************************/
  2. /* editor_quick_open_dialog.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef EDITOR_QUICK_OPEN_DIALOG_H
  31. #define EDITOR_QUICK_OPEN_DIALOG_H
  32. #include "core/templates/oa_hash_map.h"
  33. #include "scene/gui/dialogs.h"
  34. class Button;
  35. class CenterContainer;
  36. class CheckButton;
  37. class EditorFileSystemDirectory;
  38. class LineEdit;
  39. class HFlowContainer;
  40. class MarginContainer;
  41. class PanelContainer;
  42. class ScrollContainer;
  43. class StringName;
  44. class Texture2D;
  45. class TextureRect;
  46. class VBoxContainer;
  47. class FuzzySearchResult;
  48. class QuickOpenResultItem;
  49. enum class QuickOpenDisplayMode {
  50. GRID,
  51. LIST,
  52. };
  53. struct QuickOpenResultCandidate {
  54. String file_path;
  55. Ref<Texture2D> thumbnail;
  56. const FuzzySearchResult *result = nullptr;
  57. };
  58. class HighlightedLabel : public Label {
  59. GDCLASS(HighlightedLabel, Label)
  60. Vector<Vector2i> highlights;
  61. void draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing);
  62. public:
  63. void add_highlight(const Vector2i &p_interval);
  64. void reset_highlights();
  65. protected:
  66. void _notification(int p_notification);
  67. };
  68. class QuickOpenResultContainer : public VBoxContainer {
  69. GDCLASS(QuickOpenResultContainer, VBoxContainer)
  70. public:
  71. void init(const Vector<StringName> &p_base_types);
  72. void handle_search_box_input(const Ref<InputEvent> &p_ie);
  73. void set_query_and_update(const String &p_query);
  74. void update_results();
  75. bool has_nothing_selected() const;
  76. String get_selected() const;
  77. void save_selected_item();
  78. void cleanup();
  79. QuickOpenResultContainer();
  80. protected:
  81. void _notification(int p_what);
  82. private:
  83. static constexpr int SHOW_ALL_FILES_THRESHOLD = 30;
  84. static constexpr int MAX_HISTORY_SIZE = 20;
  85. Vector<FuzzySearchResult> search_results;
  86. Vector<StringName> base_types;
  87. Vector<String> filepaths;
  88. OAHashMap<String, StringName> filetypes;
  89. Vector<QuickOpenResultCandidate> candidates;
  90. OAHashMap<StringName, Vector<QuickOpenResultCandidate>> selected_history;
  91. String query;
  92. int selection_index = -1;
  93. int num_visible_results = 0;
  94. int max_total_results = 0;
  95. bool showing_history = false;
  96. bool never_opened = true;
  97. QuickOpenDisplayMode content_display_mode = QuickOpenDisplayMode::LIST;
  98. Vector<QuickOpenResultItem *> result_items;
  99. ScrollContainer *scroll_container = nullptr;
  100. VBoxContainer *list = nullptr;
  101. HFlowContainer *grid = nullptr;
  102. PanelContainer *panel_container = nullptr;
  103. CenterContainer *no_results_container = nullptr;
  104. Label *no_results_label = nullptr;
  105. Label *file_details_path = nullptr;
  106. Button *display_mode_toggle = nullptr;
  107. CheckButton *include_addons_toggle = nullptr;
  108. CheckButton *fuzzy_search_toggle = nullptr;
  109. OAHashMap<StringName, Ref<Texture2D>> file_type_icons;
  110. static QuickOpenDisplayMode get_adaptive_display_mode(const Vector<StringName> &p_base_types);
  111. void _ensure_result_vector_capacity();
  112. void _create_initial_results();
  113. void _find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons);
  114. void _setup_candidate(QuickOpenResultCandidate &p_candidate, const String &p_filepath);
  115. void _setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result);
  116. void _update_fuzzy_search_results();
  117. void _use_default_candidates();
  118. void _score_and_sort_candidates();
  119. void _update_result_items(int p_new_visible_results_count, int p_new_selection_index);
  120. void _move_selection_index(Key p_key);
  121. void _select_item(int p_index);
  122. void _item_input(const Ref<InputEvent> &p_ev, int p_index);
  123. CanvasItem *_get_result_root();
  124. void _layout_result_item(QuickOpenResultItem *p_item);
  125. void _set_display_mode(QuickOpenDisplayMode p_display_mode);
  126. void _toggle_display_mode();
  127. void _toggle_include_addons(bool p_pressed);
  128. void _toggle_fuzzy_search(bool p_pressed);
  129. static void _bind_methods();
  130. };
  131. class QuickOpenResultGridItem : public VBoxContainer {
  132. GDCLASS(QuickOpenResultGridItem, VBoxContainer)
  133. public:
  134. QuickOpenResultGridItem();
  135. void reset();
  136. void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);
  137. void highlight_item(const Color &p_color);
  138. void remove_highlight();
  139. private:
  140. TextureRect *thumbnail = nullptr;
  141. HighlightedLabel *name = nullptr;
  142. };
  143. class QuickOpenResultListItem : public HBoxContainer {
  144. GDCLASS(QuickOpenResultListItem, HBoxContainer)
  145. public:
  146. QuickOpenResultListItem();
  147. void reset();
  148. void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);
  149. void highlight_item(const Color &p_color);
  150. void remove_highlight();
  151. protected:
  152. void _notification(int p_what);
  153. private:
  154. static const int CONTAINER_MARGIN = 8;
  155. MarginContainer *image_container = nullptr;
  156. VBoxContainer *text_container = nullptr;
  157. TextureRect *thumbnail = nullptr;
  158. HighlightedLabel *name = nullptr;
  159. HighlightedLabel *path = nullptr;
  160. };
  161. class QuickOpenResultItem : public HBoxContainer {
  162. GDCLASS(QuickOpenResultItem, HBoxContainer)
  163. public:
  164. QuickOpenResultItem();
  165. bool enable_highlights = true;
  166. void reset();
  167. void set_content(const QuickOpenResultCandidate &p_candidate);
  168. void set_display_mode(QuickOpenDisplayMode p_display_mode);
  169. void highlight_item(bool p_enabled);
  170. protected:
  171. void _notification(int p_what);
  172. private:
  173. QuickOpenResultListItem *list_item = nullptr;
  174. QuickOpenResultGridItem *grid_item = nullptr;
  175. Ref<StyleBox> selected_stylebox;
  176. Ref<StyleBox> hovering_stylebox;
  177. Color highlighted_font_color;
  178. bool is_hovering = false;
  179. bool is_selected = false;
  180. void _set_enabled(bool p_enabled);
  181. };
  182. class EditorQuickOpenDialog : public AcceptDialog {
  183. GDCLASS(EditorQuickOpenDialog, AcceptDialog);
  184. public:
  185. void popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback);
  186. EditorQuickOpenDialog();
  187. protected:
  188. virtual void cancel_pressed() override;
  189. virtual void ok_pressed() override;
  190. private:
  191. static String get_dialog_title(const Vector<StringName> &p_base_types);
  192. LineEdit *search_box = nullptr;
  193. QuickOpenResultContainer *container = nullptr;
  194. Callable item_selected_callback;
  195. void _search_box_text_changed(const String &p_query);
  196. };
  197. #endif // EDITOR_QUICK_OPEN_DIALOG_H