editor_quick_open_dialog.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "scene/gui/margin_container.h"
  35. class Button;
  36. class CenterContainer;
  37. class CheckButton;
  38. class ConfigFile;
  39. class EditorFileSystemDirectory;
  40. class LineEdit;
  41. class HFlowContainer;
  42. class MarginContainer;
  43. class PanelContainer;
  44. class PopupMenu;
  45. class ScrollContainer;
  46. class StringName;
  47. class Texture2D;
  48. class TextureRect;
  49. class VBoxContainer;
  50. class FuzzySearchResult;
  51. class QuickOpenResultItem;
  52. enum class QuickOpenDisplayMode {
  53. GRID,
  54. LIST,
  55. };
  56. struct QuickOpenResultCandidate {
  57. String file_path;
  58. Ref<Texture2D> thumbnail;
  59. const FuzzySearchResult *result = nullptr;
  60. };
  61. class HighlightedLabel : public Label {
  62. GDCLASS(HighlightedLabel, Label)
  63. Vector<Vector2i> highlights;
  64. void draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing);
  65. public:
  66. void add_highlight(const Vector2i &p_interval);
  67. void reset_highlights();
  68. protected:
  69. void _notification(int p_notification);
  70. };
  71. class QuickOpenResultContainer : public VBoxContainer {
  72. GDCLASS(QuickOpenResultContainer, VBoxContainer)
  73. enum {
  74. FILE_SHOW_IN_FILESYSTEM,
  75. FILE_SHOW_IN_FILE_MANAGER
  76. };
  77. public:
  78. void init(const Vector<StringName> &p_base_types);
  79. void handle_search_box_input(const Ref<InputEvent> &p_ie);
  80. void set_query_and_update(const String &p_query);
  81. void update_results();
  82. bool has_nothing_selected() const;
  83. String get_selected() const;
  84. void save_selected_item();
  85. void cleanup();
  86. QuickOpenResultContainer();
  87. protected:
  88. void _notification(int p_what);
  89. private:
  90. static constexpr int SHOW_ALL_FILES_THRESHOLD = 30;
  91. static constexpr int MAX_HISTORY_SIZE = 20;
  92. Vector<FuzzySearchResult> search_results;
  93. Vector<StringName> base_types;
  94. Vector<String> filepaths;
  95. OAHashMap<String, StringName> filetypes;
  96. Vector<QuickOpenResultCandidate> candidates;
  97. OAHashMap<StringName, Vector<QuickOpenResultCandidate>> selected_history;
  98. String query;
  99. int selection_index = -1;
  100. int num_visible_results = 0;
  101. int max_total_results = 0;
  102. bool showing_history = false;
  103. bool never_opened = true;
  104. Ref<ConfigFile> history_file;
  105. QuickOpenDisplayMode content_display_mode = QuickOpenDisplayMode::LIST;
  106. Vector<QuickOpenResultItem *> result_items;
  107. ScrollContainer *scroll_container = nullptr;
  108. VBoxContainer *list = nullptr;
  109. HFlowContainer *grid = nullptr;
  110. PopupMenu *file_context_menu = nullptr;
  111. PanelContainer *panel_container = nullptr;
  112. CenterContainer *no_results_container = nullptr;
  113. Label *no_results_label = nullptr;
  114. Label *file_details_path = nullptr;
  115. Button *display_mode_toggle = nullptr;
  116. CheckButton *include_addons_toggle = nullptr;
  117. CheckButton *fuzzy_search_toggle = nullptr;
  118. OAHashMap<StringName, Ref<Texture2D>> file_type_icons;
  119. static QuickOpenDisplayMode get_adaptive_display_mode(const Vector<StringName> &p_base_types);
  120. void _ensure_result_vector_capacity();
  121. void _create_initial_results();
  122. void _find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons);
  123. void _setup_candidate(QuickOpenResultCandidate &p_candidate, const String &p_filepath);
  124. void _setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result);
  125. void _update_fuzzy_search_results();
  126. void _use_default_candidates();
  127. void _score_and_sort_candidates();
  128. void _update_result_items(int p_new_visible_results_count, int p_new_selection_index);
  129. void _move_selection_index(Key p_key);
  130. void _select_item(int p_index);
  131. void _item_input(const Ref<InputEvent> &p_ev, int p_index);
  132. CanvasItem *_get_result_root();
  133. void _layout_result_item(QuickOpenResultItem *p_item);
  134. void _set_display_mode(QuickOpenDisplayMode p_display_mode);
  135. void _toggle_display_mode();
  136. void _toggle_include_addons(bool p_pressed);
  137. void _toggle_fuzzy_search(bool p_pressed);
  138. void _menu_option(int p_option);
  139. String _get_cache_file_path() const;
  140. static void _bind_methods();
  141. };
  142. class QuickOpenResultGridItem : public MarginContainer {
  143. GDCLASS(QuickOpenResultGridItem, MarginContainer)
  144. public:
  145. QuickOpenResultGridItem();
  146. void reset();
  147. void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);
  148. void highlight_item(const Color &p_color);
  149. void remove_highlight();
  150. private:
  151. VBoxContainer *vbc = nullptr;
  152. TextureRect *thumbnail = nullptr;
  153. HighlightedLabel *name = nullptr;
  154. };
  155. class QuickOpenResultListItem : public MarginContainer {
  156. GDCLASS(QuickOpenResultListItem, MarginContainer)
  157. public:
  158. QuickOpenResultListItem();
  159. void reset();
  160. void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);
  161. void highlight_item(const Color &p_color);
  162. void remove_highlight();
  163. protected:
  164. void _notification(int p_what);
  165. private:
  166. HBoxContainer *hbc = nullptr;
  167. VBoxContainer *text_container = nullptr;
  168. TextureRect *thumbnail = nullptr;
  169. HighlightedLabel *name = nullptr;
  170. HighlightedLabel *path = nullptr;
  171. };
  172. class QuickOpenResultItem : public HBoxContainer {
  173. GDCLASS(QuickOpenResultItem, HBoxContainer)
  174. public:
  175. QuickOpenResultItem();
  176. bool enable_highlights = true;
  177. void reset();
  178. void set_content(const QuickOpenResultCandidate &p_candidate);
  179. void set_display_mode(QuickOpenDisplayMode p_display_mode);
  180. void highlight_item(bool p_enabled);
  181. protected:
  182. void _notification(int p_what);
  183. private:
  184. QuickOpenResultListItem *list_item = nullptr;
  185. QuickOpenResultGridItem *grid_item = nullptr;
  186. Ref<StyleBox> selected_stylebox;
  187. Ref<StyleBox> hovering_stylebox;
  188. Color highlighted_font_color;
  189. bool is_hovering = false;
  190. bool is_selected = false;
  191. void _set_enabled(bool p_enabled);
  192. };
  193. class EditorQuickOpenDialog : public AcceptDialog {
  194. GDCLASS(EditorQuickOpenDialog, AcceptDialog);
  195. public:
  196. void popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback);
  197. EditorQuickOpenDialog();
  198. protected:
  199. virtual void cancel_pressed() override;
  200. virtual void ok_pressed() override;
  201. private:
  202. static String get_dialog_title(const Vector<StringName> &p_base_types);
  203. LineEdit *search_box = nullptr;
  204. QuickOpenResultContainer *container = nullptr;
  205. Callable item_selected_callback;
  206. void _search_box_text_changed(const String &p_query);
  207. };
  208. #endif // EDITOR_QUICK_OPEN_DIALOG_H