editor_quick_open_dialog.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /**************************************************************************/
  2. /* editor_quick_open_dialog.cpp */
  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. #include "editor_quick_open_dialog.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/string/fuzzy_search.h"
  33. #include "editor/docks/filesystem_dock.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_string_names.h"
  36. #include "editor/file_system/editor_file_system.h"
  37. #include "editor/file_system/editor_paths.h"
  38. #include "editor/inspector/editor_resource_preview.h"
  39. #include "editor/settings/editor_settings.h"
  40. #include "editor/themes/editor_scale.h"
  41. #include "scene/gui/center_container.h"
  42. #include "scene/gui/check_button.h"
  43. #include "scene/gui/flow_container.h"
  44. #include "scene/gui/line_edit.h"
  45. #include "scene/gui/margin_container.h"
  46. #include "scene/gui/panel_container.h"
  47. #include "scene/gui/separator.h"
  48. #include "scene/gui/texture_rect.h"
  49. #include "scene/gui/tree.h"
  50. void HighlightedLabel::draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing) {
  51. for (int i = get_lines_skipped(); i < p_line_limit; i++) {
  52. RID line = get_line_rid(i);
  53. Vector<Vector2> ranges = TS->shaped_text_get_selection(line, p_substr.x, p_substr.x + p_substr.y);
  54. Rect2 line_rect = get_line_rect(i);
  55. for (const Vector2 &range : ranges) {
  56. Rect2 rect = Rect2(Point2(range.x, 0) + line_rect.position, Size2(range.y - range.x, line_rect.size.y));
  57. rect.position = p_offset + line_rect.position;
  58. rect.position.x += range.x;
  59. rect.size = Size2(range.y - range.x, line_rect.size.y);
  60. rect.size.x = MIN(rect.size.x, line_rect.size.x - range.x);
  61. if (rect.size.x > 0) {
  62. draw_rect(rect, Color(1, 1, 1, 0.07), true);
  63. draw_rect(rect, Color(0.5, 0.7, 1.0, 0.4), false, 1);
  64. }
  65. }
  66. p_offset.y += line_spacing + TS->shaped_text_get_ascent(line) + TS->shaped_text_get_descent(line);
  67. }
  68. }
  69. void HighlightedLabel::add_highlight(const Vector2i &p_interval) {
  70. if (p_interval.y > 0) {
  71. highlights.append(p_interval);
  72. queue_redraw();
  73. }
  74. }
  75. void HighlightedLabel::reset_highlights() {
  76. highlights.clear();
  77. queue_redraw();
  78. }
  79. void HighlightedLabel::_notification(int p_notification) {
  80. if (p_notification == NOTIFICATION_DRAW) {
  81. if (highlights.is_empty()) {
  82. return;
  83. }
  84. Vector2 offset;
  85. int line_limit;
  86. int line_spacing;
  87. get_layout_data(offset, line_limit, line_spacing);
  88. for (const Vector2i &substr : highlights) {
  89. draw_substr_rects(substr, offset, line_limit, line_spacing);
  90. }
  91. }
  92. }
  93. EditorQuickOpenDialog::EditorQuickOpenDialog() {
  94. VBoxContainer *vbc = memnew(VBoxContainer);
  95. vbc->add_theme_constant_override("separation", 0);
  96. add_child(vbc);
  97. {
  98. // Search bar
  99. MarginContainer *mc = memnew(MarginContainer);
  100. mc->add_theme_constant_override("margin_top", 6);
  101. mc->add_theme_constant_override("margin_bottom", 6);
  102. mc->add_theme_constant_override("margin_left", 1);
  103. mc->add_theme_constant_override("margin_right", 1);
  104. vbc->add_child(mc);
  105. search_box = memnew(LineEdit);
  106. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  107. search_box->set_placeholder(TTR("Search files..."));
  108. search_box->set_accessibility_name(TTRC("Search"));
  109. search_box->set_clear_button_enabled(true);
  110. mc->add_child(search_box);
  111. }
  112. {
  113. container = memnew(QuickOpenResultContainer);
  114. container->connect("result_clicked", callable_mp(this, &EditorQuickOpenDialog::ok_pressed));
  115. vbc->add_child(container);
  116. }
  117. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorQuickOpenDialog::_search_box_text_changed));
  118. search_box->connect(SceneStringName(gui_input), callable_mp(container, &QuickOpenResultContainer::handle_search_box_input));
  119. register_text_enter(search_box);
  120. get_ok_button()->hide();
  121. }
  122. String EditorQuickOpenDialog::get_dialog_title(const Vector<StringName> &p_base_types) {
  123. if (p_base_types.size() > 1) {
  124. return TTR("Select Resource");
  125. }
  126. if (p_base_types[0] == SNAME("PackedScene")) {
  127. return TTR("Select Scene");
  128. }
  129. return TTR("Select") + " " + p_base_types[0];
  130. }
  131. void EditorQuickOpenDialog::popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback) {
  132. ERR_FAIL_COND(p_base_types.is_empty());
  133. ERR_FAIL_COND(!p_item_selected_callback.is_valid());
  134. item_selected_callback = p_item_selected_callback;
  135. container->init(p_base_types);
  136. get_ok_button()->set_disabled(container->has_nothing_selected());
  137. set_title(get_dialog_title(p_base_types));
  138. popup_centered_clamped(Size2(780, 650) * EDSCALE, 0.8f);
  139. search_box->grab_focus();
  140. }
  141. void EditorQuickOpenDialog::ok_pressed() {
  142. item_selected_callback.call(container->get_selected());
  143. container->save_selected_item();
  144. container->cleanup();
  145. search_box->clear();
  146. hide();
  147. }
  148. void EditorQuickOpenDialog::cancel_pressed() {
  149. container->cleanup();
  150. search_box->clear();
  151. }
  152. void EditorQuickOpenDialog::_search_box_text_changed(const String &p_query) {
  153. container->set_query_and_update(p_query);
  154. get_ok_button()->set_disabled(container->has_nothing_selected());
  155. }
  156. //------------------------- Result Container
  157. void style_button(Button *p_button) {
  158. p_button->set_flat(true);
  159. p_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  160. p_button->set_default_cursor_shape(Control::CURSOR_POINTING_HAND);
  161. }
  162. QuickOpenResultContainer::QuickOpenResultContainer() {
  163. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  164. set_v_size_flags(Control::SIZE_EXPAND_FILL);
  165. add_theme_constant_override("separation", 0);
  166. history_file.instantiate();
  167. {
  168. // Results section
  169. panel_container = memnew(PanelContainer);
  170. panel_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  171. add_child(panel_container);
  172. {
  173. // No search results
  174. no_results_container = memnew(CenterContainer);
  175. no_results_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  176. no_results_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  177. panel_container->add_child(no_results_container);
  178. no_results_label = memnew(Label);
  179. no_results_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  180. no_results_label->add_theme_font_size_override(SceneStringName(font_size), 24 * EDSCALE);
  181. no_results_container->add_child(no_results_label);
  182. no_results_container->hide();
  183. }
  184. {
  185. // Search results
  186. scroll_container = memnew(ScrollContainer);
  187. scroll_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  188. scroll_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  189. scroll_container->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  190. scroll_container->hide();
  191. panel_container->add_child(scroll_container);
  192. list = memnew(VBoxContainer);
  193. list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  194. list->add_theme_constant_override(SNAME("separation"), 0);
  195. list->hide();
  196. scroll_container->add_child(list);
  197. grid = memnew(HFlowContainer);
  198. grid->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  199. grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  200. grid->add_theme_constant_override(SNAME("v_separation"), 0);
  201. grid->add_theme_constant_override(SNAME("h_separation"), 0);
  202. grid->hide();
  203. scroll_container->add_child(grid);
  204. file_context_menu = memnew(PopupMenu);
  205. file_context_menu->add_item(TTR("Show in FileSystem"), FILE_SHOW_IN_FILESYSTEM);
  206. file_context_menu->add_item(TTR("Show in File Manager"), FILE_SHOW_IN_FILE_MANAGER);
  207. file_context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &QuickOpenResultContainer::_menu_option));
  208. file_context_menu->hide();
  209. scroll_container->add_child(file_context_menu);
  210. }
  211. }
  212. {
  213. // Selected filepath
  214. file_details_path = memnew(Label);
  215. file_details_path->set_focus_mode(FOCUS_ACCESSIBILITY);
  216. file_details_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  217. file_details_path->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  218. file_details_path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  219. add_child(file_details_path);
  220. }
  221. {
  222. // Bottom bar
  223. HBoxContainer *bottom_bar = memnew(HBoxContainer);
  224. bottom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  225. bottom_bar->set_alignment(ALIGNMENT_END);
  226. bottom_bar->add_theme_constant_override("separation", 3);
  227. add_child(bottom_bar);
  228. fuzzy_search_toggle = memnew(CheckButton);
  229. style_button(fuzzy_search_toggle);
  230. fuzzy_search_toggle->set_text(TTR("Fuzzy Search"));
  231. fuzzy_search_toggle->set_tooltip_text(TTRC("Include approximate matches."));
  232. fuzzy_search_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_fuzzy_search));
  233. bottom_bar->add_child(fuzzy_search_toggle);
  234. include_addons_toggle = memnew(CheckButton);
  235. style_button(include_addons_toggle);
  236. include_addons_toggle->set_text(TTR("Addons"));
  237. include_addons_toggle->set_tooltip_text(TTR("Include files from addons"));
  238. include_addons_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_include_addons));
  239. bottom_bar->add_child(include_addons_toggle);
  240. VSeparator *vsep = memnew(VSeparator);
  241. vsep->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  242. vsep->set_custom_minimum_size(Size2i(0, 14 * EDSCALE));
  243. bottom_bar->add_child(vsep);
  244. display_mode_toggle = memnew(Button);
  245. display_mode_toggle->set_accessibility_name(TTRC("Display Mode"));
  246. style_button(display_mode_toggle);
  247. display_mode_toggle->connect(SceneStringName(pressed), callable_mp(this, &QuickOpenResultContainer::_toggle_display_mode));
  248. bottom_bar->add_child(display_mode_toggle);
  249. }
  250. }
  251. void QuickOpenResultContainer::_menu_option(int p_option) {
  252. switch (p_option) {
  253. case FILE_SHOW_IN_FILESYSTEM: {
  254. FileSystemDock::get_singleton()->navigate_to_path(get_selected());
  255. } break;
  256. case FILE_SHOW_IN_FILE_MANAGER: {
  257. String dir = ProjectSettings::get_singleton()->globalize_path(get_selected());
  258. OS::get_singleton()->shell_show_in_file_manager(dir, true);
  259. } break;
  260. }
  261. }
  262. void QuickOpenResultContainer::_ensure_result_vector_capacity() {
  263. int target_size = EDITOR_GET("filesystem/quick_open_dialog/max_results");
  264. int initial_size = result_items.size();
  265. for (int i = target_size; i < initial_size; i++) {
  266. result_items[i]->queue_free();
  267. }
  268. result_items.resize(target_size);
  269. for (int i = initial_size; i < target_size; i++) {
  270. QuickOpenResultItem *item = memnew(QuickOpenResultItem);
  271. item->connect(SceneStringName(gui_input), callable_mp(this, &QuickOpenResultContainer::_item_input).bind(i));
  272. result_items.write[i] = item;
  273. if (!never_opened) {
  274. _layout_result_item(item);
  275. }
  276. }
  277. }
  278. void QuickOpenResultContainer::init(const Vector<StringName> &p_base_types) {
  279. _ensure_result_vector_capacity();
  280. base_types = p_base_types;
  281. const int display_mode_behavior = EDITOR_GET("filesystem/quick_open_dialog/default_display_mode");
  282. const bool adaptive_display_mode = (display_mode_behavior == 0);
  283. const bool first_open = never_opened;
  284. if (adaptive_display_mode) {
  285. _set_display_mode(get_adaptive_display_mode(p_base_types));
  286. } else if (never_opened) {
  287. int last = EditorSettings::get_singleton()->get_project_metadata("quick_open_dialog", "last_mode", (int)QuickOpenDisplayMode::LIST);
  288. _set_display_mode((QuickOpenDisplayMode)last);
  289. }
  290. const bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching");
  291. const bool include_addons = EDITOR_GET("filesystem/quick_open_dialog/include_addons");
  292. fuzzy_search_toggle->set_pressed_no_signal(fuzzy_matching);
  293. include_addons_toggle->set_pressed_no_signal(include_addons);
  294. never_opened = false;
  295. const bool enable_highlights = EDITOR_GET("filesystem/quick_open_dialog/show_search_highlight");
  296. for (QuickOpenResultItem *E : result_items) {
  297. E->enable_highlights = enable_highlights;
  298. }
  299. if (first_open && history_file->load(_get_cache_file_path()) == OK) {
  300. // Load history when opening for the first time.
  301. file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object")));
  302. bool history_modified = false;
  303. Vector<String> history_keys = history_file->get_section_keys("selected_history");
  304. for (const String &type : history_keys) {
  305. const StringName type_name = type;
  306. const PackedStringArray paths = history_file->get_value("selected_history", type);
  307. PackedStringArray cleaned_paths;
  308. cleaned_paths.resize(paths.size());
  309. Vector<QuickOpenResultCandidate> loaded_candidates;
  310. loaded_candidates.resize(paths.size());
  311. {
  312. QuickOpenResultCandidate *candidates_write = loaded_candidates.ptrw();
  313. String *cleanup_write = cleaned_paths.ptrw();
  314. int i = 0;
  315. for (String path : paths) {
  316. if (path.begins_with("uid://")) {
  317. ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(path);
  318. if (!ResourceUID::get_singleton()->has_id(id)) {
  319. continue;
  320. }
  321. path = ResourceUID::get_singleton()->get_id_path(id);
  322. }
  323. if (!ResourceLoader::exists(path)) {
  324. continue;
  325. }
  326. filetypes.insert(path, type_name);
  327. QuickOpenResultCandidate candidate;
  328. _setup_candidate(candidate, path);
  329. candidates_write[i] = candidate;
  330. cleanup_write[i] = path;
  331. i++;
  332. }
  333. loaded_candidates.resize(i);
  334. cleaned_paths.resize(i);
  335. selected_history.insert(type, loaded_candidates);
  336. if (i < paths.size()) {
  337. // Some paths removed, need to update history.
  338. if (i == 0) {
  339. history_file->erase_section_key("selected_history", type);
  340. } else {
  341. history_file->set_value("selected_history", type, cleaned_paths);
  342. }
  343. history_modified = true;
  344. }
  345. }
  346. }
  347. if (history_modified) {
  348. history_file->save(_get_cache_file_path());
  349. }
  350. }
  351. _create_initial_results();
  352. }
  353. void QuickOpenResultContainer::_sort_filepaths(int p_max_results) {
  354. struct FilepathComparator {
  355. bool operator()(const String &p_lhs, const String &p_rhs) const {
  356. // Sort on (length, alphanumeric) to prioritize shorter filepaths
  357. return p_lhs.length() == p_rhs.length() ? p_lhs < p_rhs : p_lhs.length() < p_rhs.length();
  358. }
  359. };
  360. SortArray<String, FilepathComparator> sorter;
  361. if (filepaths.size() > p_max_results) {
  362. sorter.partial_sort(0, filepaths.size(), p_max_results, filepaths.ptrw());
  363. } else {
  364. sorter.sort(filepaths.ptrw(), filepaths.size());
  365. }
  366. }
  367. void QuickOpenResultContainer::_create_initial_results() {
  368. file_type_icons.clear();
  369. file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object")));
  370. filepaths.clear();
  371. filetypes.clear();
  372. history_set.clear();
  373. Vector<QuickOpenResultCandidate> *history = _get_history();
  374. if (history) {
  375. for (const QuickOpenResultCandidate &candidate : *history) {
  376. history_set.insert(candidate.file_path);
  377. }
  378. }
  379. _find_filepaths_in_folder(EditorFileSystem::get_singleton()->get_filesystem(), include_addons_toggle->is_pressed());
  380. _sort_filepaths(result_items.size());
  381. max_total_results = MIN(filepaths.size(), result_items.size());
  382. update_results();
  383. }
  384. void QuickOpenResultContainer::_find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons) {
  385. for (int i = 0; i < p_directory->get_subdir_count(); i++) {
  386. if (p_include_addons || p_directory->get_name() != "addons") {
  387. _find_filepaths_in_folder(p_directory->get_subdir(i), p_include_addons);
  388. }
  389. }
  390. for (int i = 0; i < p_directory->get_file_count(); i++) {
  391. String file_path = p_directory->get_file_path(i);
  392. const StringName engine_type = p_directory->get_file_type(i);
  393. const StringName script_type = p_directory->get_file_resource_script_class(i);
  394. const bool is_engine_type = script_type == StringName();
  395. const StringName &actual_type = is_engine_type ? engine_type : script_type;
  396. for (const StringName &parent_type : base_types) {
  397. bool is_valid = ClassDB::is_parent_class(engine_type, parent_type) || (!is_engine_type && EditorNode::get_editor_data().script_class_is_parent(script_type, parent_type));
  398. if (is_valid) {
  399. filepaths.append(file_path);
  400. filetypes.insert(file_path, actual_type);
  401. break; // Stop testing base types as soon as we get a match.
  402. }
  403. }
  404. }
  405. }
  406. void QuickOpenResultContainer::set_query_and_update(const String &p_query) {
  407. query = p_query;
  408. update_results();
  409. }
  410. Vector<QuickOpenResultCandidate> *QuickOpenResultContainer::_get_history() {
  411. if (base_types.size() == 1) {
  412. return selected_history.getptr(base_types[0]);
  413. }
  414. return nullptr;
  415. }
  416. void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &p_candidate, const String &p_filepath) {
  417. p_candidate.file_path = ResourceUID::ensure_path(p_filepath);
  418. p_candidate.result = nullptr;
  419. StringName actual_type;
  420. {
  421. StringName *actual_type_ptr = filetypes.getptr(p_filepath);
  422. if (actual_type_ptr) {
  423. actual_type = *actual_type_ptr;
  424. } else {
  425. ERR_PRINT(vformat("EditorQuickOpenDialog: No type for path %s.", p_candidate.file_path));
  426. }
  427. }
  428. EditorResourcePreview::PreviewItem item = EditorResourcePreview::get_singleton()->get_resource_preview_if_available(p_candidate.file_path);
  429. if (item.preview.is_valid()) {
  430. p_candidate.thumbnail = item.preview;
  431. } else if (file_type_icons.has(actual_type)) {
  432. p_candidate.thumbnail = *file_type_icons.getptr(actual_type);
  433. } else if (has_theme_icon(actual_type, EditorStringName(EditorIcons))) {
  434. p_candidate.thumbnail = get_editor_theme_icon(actual_type);
  435. file_type_icons.insert(actual_type, p_candidate.thumbnail);
  436. } else {
  437. p_candidate.thumbnail = *file_type_icons.getptr(SNAME("__default_icon"));
  438. }
  439. }
  440. void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result) {
  441. _setup_candidate(p_candidate, p_result.target);
  442. p_candidate.result = &p_result;
  443. }
  444. void QuickOpenResultContainer::update_results() {
  445. candidates.clear();
  446. if (query.is_empty()) {
  447. _use_default_candidates();
  448. } else {
  449. _score_and_sort_candidates();
  450. }
  451. _update_result_items(MIN(candidates.size(), max_total_results), 0);
  452. }
  453. void QuickOpenResultContainer::_use_default_candidates() {
  454. Vector<QuickOpenResultCandidate> *history = _get_history();
  455. if (history) {
  456. candidates.append_array(*history);
  457. }
  458. candidates.resize(MIN(max_total_results, filepaths.size()));
  459. int count = candidates.size();
  460. int i = 0;
  461. for (const String &filepath : filepaths) {
  462. if (i >= count) {
  463. break;
  464. }
  465. _setup_candidate(candidates.write[i++], filepath);
  466. }
  467. }
  468. void QuickOpenResultContainer::_update_fuzzy_search_results() {
  469. FuzzySearch fuzzy_search;
  470. fuzzy_search.start_offset = 6; // Don't match against "res://" at the start of each filepath.
  471. fuzzy_search.set_query(query);
  472. fuzzy_search.max_results = max_total_results;
  473. bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching");
  474. int max_misses = EDITOR_GET("filesystem/quick_open_dialog/max_fuzzy_misses");
  475. fuzzy_search.allow_subsequences = fuzzy_matching;
  476. fuzzy_search.max_misses = fuzzy_matching ? max_misses : 0;
  477. fuzzy_search.search_all(filepaths, search_results);
  478. }
  479. void QuickOpenResultContainer::_score_and_sort_candidates() {
  480. _update_fuzzy_search_results();
  481. candidates.resize(search_results.size());
  482. QuickOpenResultCandidate *candidates_write = candidates.ptrw();
  483. for (const FuzzySearchResult &result : search_results) {
  484. _setup_candidate(*candidates_write++, result);
  485. }
  486. }
  487. void QuickOpenResultContainer::_update_result_items(int p_new_visible_results_count, int p_new_selection_index) {
  488. // Only need to update items that were not hidden in previous update.
  489. int num_items_needing_updates = MAX(num_visible_results, p_new_visible_results_count);
  490. num_visible_results = p_new_visible_results_count;
  491. for (int i = 0; i < num_items_needing_updates; i++) {
  492. QuickOpenResultItem *item = result_items[i];
  493. if (i < num_visible_results) {
  494. item->set_content(candidates[i]);
  495. } else {
  496. item->reset();
  497. }
  498. };
  499. const bool any_results = num_visible_results > 0;
  500. _select_item(any_results ? p_new_selection_index : -1);
  501. scroll_container->set_visible(any_results);
  502. no_results_container->set_visible(!any_results);
  503. if (!any_results) {
  504. if (filepaths.is_empty()) {
  505. no_results_label->set_text(TTR("No files found for this type"));
  506. } else {
  507. no_results_label->set_text(TTR("No results found"));
  508. }
  509. }
  510. }
  511. void QuickOpenResultContainer::handle_search_box_input(const Ref<InputEvent> &p_ie) {
  512. if (num_visible_results < 0) {
  513. return;
  514. }
  515. Ref<InputEventKey> key_event = p_ie;
  516. if (key_event.is_valid() && key_event->is_pressed()) {
  517. bool move_selection = false;
  518. switch (key_event->get_keycode()) {
  519. case Key::UP:
  520. case Key::DOWN:
  521. case Key::PAGEUP:
  522. case Key::PAGEDOWN: {
  523. move_selection = true;
  524. } break;
  525. case Key::LEFT:
  526. case Key::RIGHT: {
  527. if (content_display_mode == QuickOpenDisplayMode::GRID) {
  528. // Maybe strip off the shift modifier to allow non-selecting navigation by character?
  529. if (key_event->get_modifiers_mask().is_empty()) {
  530. move_selection = true;
  531. }
  532. }
  533. } break;
  534. default:
  535. break; // Let the event through so it will reach the search box.
  536. }
  537. if (move_selection) {
  538. _move_selection_index(key_event->get_keycode());
  539. queue_redraw();
  540. accept_event();
  541. }
  542. }
  543. }
  544. void QuickOpenResultContainer::_move_selection_index(Key p_key) {
  545. // Don't move selection if there are no results.
  546. if (num_visible_results <= 0) {
  547. return;
  548. }
  549. const int max_index = num_visible_results - 1;
  550. int idx = selection_index;
  551. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  552. if (p_key == Key::UP) {
  553. idx = (idx == 0) ? max_index : (idx - 1);
  554. } else if (p_key == Key::DOWN) {
  555. idx = (idx == max_index) ? 0 : (idx + 1);
  556. } else if (p_key == Key::PAGEUP) {
  557. idx = (idx == 0) ? idx : MAX(idx - 10, 0);
  558. } else if (p_key == Key::PAGEDOWN) {
  559. idx = (idx == max_index) ? idx : MIN(idx + 10, max_index);
  560. }
  561. } else {
  562. int column_count = grid->get_line_max_child_count();
  563. if (p_key == Key::LEFT) {
  564. idx = (idx == 0) ? max_index : (idx - 1);
  565. } else if (p_key == Key::RIGHT) {
  566. idx = (idx == max_index) ? 0 : (idx + 1);
  567. } else if (p_key == Key::UP) {
  568. idx = (idx == 0) ? max_index : MAX(idx - column_count, 0);
  569. } else if (p_key == Key::DOWN) {
  570. idx = (idx == max_index) ? 0 : MIN(idx + column_count, max_index);
  571. } else if (p_key == Key::PAGEUP) {
  572. idx = (idx == 0) ? idx : MAX(idx - (3 * column_count), 0);
  573. } else if (p_key == Key::PAGEDOWN) {
  574. idx = (idx == max_index) ? idx : MIN(idx + (3 * column_count), max_index);
  575. }
  576. }
  577. _select_item(idx);
  578. }
  579. void QuickOpenResultContainer::_select_item(int p_index) {
  580. if (!has_nothing_selected()) {
  581. result_items[selection_index]->highlight_item(false);
  582. }
  583. selection_index = p_index;
  584. if (has_nothing_selected()) {
  585. file_details_path->set_text("");
  586. return;
  587. }
  588. result_items[selection_index]->highlight_item(true);
  589. bool in_history = history_set.has(candidates[selection_index].file_path);
  590. file_details_path->set_text(get_selected() + (in_history ? TTR(" (recently opened)") : ""));
  591. const QuickOpenResultItem *item = result_items[selection_index];
  592. // Copied from Tree.
  593. const int selected_position = item->get_position().y;
  594. const int selected_size = item->get_size().y;
  595. const int scroll_window_size = scroll_container->get_size().y;
  596. const int scroll_position = scroll_container->get_v_scroll();
  597. if (selected_position <= scroll_position) {
  598. scroll_container->set_v_scroll(selected_position);
  599. } else if (selected_position + selected_size > scroll_position + scroll_window_size) {
  600. scroll_container->set_v_scroll(selected_position + selected_size - scroll_window_size);
  601. }
  602. }
  603. void QuickOpenResultContainer::_item_input(const Ref<InputEvent> &p_ev, int p_index) {
  604. Ref<InputEventMouseButton> mb = p_ev;
  605. if (mb.is_valid() && mb->is_pressed()) {
  606. if (mb->get_button_index() == MouseButton::LEFT) {
  607. _select_item(p_index);
  608. emit_signal(SNAME("result_clicked"));
  609. } else if (mb->get_button_index() == MouseButton::RIGHT) {
  610. _select_item(p_index);
  611. file_context_menu->set_position(result_items[p_index]->get_screen_position() + mb->get_position());
  612. file_context_menu->reset_size();
  613. file_context_menu->popup();
  614. }
  615. }
  616. }
  617. void QuickOpenResultContainer::_toggle_fuzzy_search(bool p_pressed) {
  618. EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/enable_fuzzy_matching", p_pressed);
  619. update_results();
  620. }
  621. String QuickOpenResultContainer::_get_cache_file_path() const {
  622. return EditorPaths::get_singleton()->get_project_settings_dir().path_join("quick_open_dialog_cache.cfg");
  623. }
  624. void QuickOpenResultContainer::_toggle_include_addons(bool p_pressed) {
  625. EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/include_addons", p_pressed);
  626. cleanup();
  627. _create_initial_results();
  628. }
  629. void QuickOpenResultContainer::_toggle_display_mode() {
  630. QuickOpenDisplayMode new_display_mode = (content_display_mode == QuickOpenDisplayMode::LIST) ? QuickOpenDisplayMode::GRID : QuickOpenDisplayMode::LIST;
  631. _set_display_mode(new_display_mode);
  632. }
  633. CanvasItem *QuickOpenResultContainer::_get_result_root() {
  634. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  635. return list;
  636. } else {
  637. return grid;
  638. }
  639. }
  640. void QuickOpenResultContainer::_layout_result_item(QuickOpenResultItem *item) {
  641. item->set_display_mode(content_display_mode);
  642. Node *parent = item->get_parent();
  643. if (parent) {
  644. parent->remove_child(item);
  645. }
  646. _get_result_root()->add_child(item);
  647. }
  648. void QuickOpenResultContainer::_set_display_mode(QuickOpenDisplayMode p_display_mode) {
  649. CanvasItem *prev_root = _get_result_root();
  650. if (prev_root->is_visible() && content_display_mode == p_display_mode) {
  651. return;
  652. }
  653. content_display_mode = p_display_mode;
  654. CanvasItem *next_root = _get_result_root();
  655. EditorSettings::get_singleton()->set_project_metadata("quick_open_dialog", "last_mode", (int)content_display_mode);
  656. prev_root->hide();
  657. next_root->show();
  658. for (QuickOpenResultItem *item : result_items) {
  659. _layout_result_item(item);
  660. }
  661. _update_result_items(num_visible_results, selection_index);
  662. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  663. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));
  664. display_mode_toggle->set_tooltip_text(TTR("Grid view"));
  665. } else {
  666. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));
  667. display_mode_toggle->set_tooltip_text(TTR("List view"));
  668. }
  669. }
  670. bool QuickOpenResultContainer::has_nothing_selected() const {
  671. return selection_index < 0;
  672. }
  673. String QuickOpenResultContainer::get_selected() const {
  674. ERR_FAIL_COND_V_MSG(has_nothing_selected(), String(), "Tried to get selected file, but nothing was selected.");
  675. return candidates[selection_index].file_path;
  676. }
  677. QuickOpenDisplayMode QuickOpenResultContainer::get_adaptive_display_mode(const Vector<StringName> &p_base_types) {
  678. static const Vector<StringName> grid_preferred_types = {
  679. StringName("Font", true),
  680. StringName("Texture2D", true),
  681. StringName("Material", true),
  682. StringName("Mesh", true),
  683. };
  684. for (const StringName &type : grid_preferred_types) {
  685. for (const StringName &base_type : p_base_types) {
  686. if (base_type == type || ClassDB::is_parent_class(base_type, type)) {
  687. return QuickOpenDisplayMode::GRID;
  688. }
  689. }
  690. }
  691. return QuickOpenDisplayMode::LIST;
  692. }
  693. String _get_uid_string(const String &p_filepath) {
  694. ResourceUID::ID id = EditorFileSystem::get_singleton()->get_file_uid(p_filepath);
  695. return id == ResourceUID::INVALID_ID ? p_filepath : ResourceUID::get_singleton()->id_to_text(id);
  696. }
  697. void QuickOpenResultContainer::save_selected_item() {
  698. if (base_types.size() > 1) {
  699. // Getting the type of the file and checking which base type it belongs to should be possible.
  700. // However, for now these are not supported, and we don't record this.
  701. return;
  702. }
  703. const StringName &base_type = base_types[0];
  704. QuickOpenResultCandidate &selected = candidates.write[selection_index];
  705. Vector<QuickOpenResultCandidate> *type_history = selected_history.getptr(base_type);
  706. if (!type_history) {
  707. selected_history.insert(base_type, Vector<QuickOpenResultCandidate>());
  708. type_history = selected_history.getptr(base_type);
  709. } else {
  710. for (int i = 0; i < type_history->size(); i++) {
  711. if (selected.file_path == type_history->get(i).file_path) {
  712. type_history->remove_at(i);
  713. break;
  714. }
  715. }
  716. }
  717. selected.result = nullptr;
  718. history_set.insert(selected.file_path);
  719. type_history->insert(0, selected);
  720. if (type_history->size() > MAX_HISTORY_SIZE) {
  721. type_history->resize(MAX_HISTORY_SIZE);
  722. }
  723. PackedStringArray paths;
  724. paths.resize(type_history->size());
  725. {
  726. String *paths_write = paths.ptrw();
  727. int i = 0;
  728. for (const QuickOpenResultCandidate &candidate : *type_history) {
  729. paths_write[i] = _get_uid_string(candidate.file_path);
  730. i++;
  731. }
  732. }
  733. history_file->set_value("selected_history", base_type, paths);
  734. history_file->save(_get_cache_file_path());
  735. }
  736. void QuickOpenResultContainer::cleanup() {
  737. num_visible_results = 0;
  738. candidates.clear();
  739. history_set.clear();
  740. _select_item(-1);
  741. for (QuickOpenResultItem *item : result_items) {
  742. item->reset();
  743. }
  744. }
  745. void QuickOpenResultContainer::_notification(int p_what) {
  746. switch (p_what) {
  747. case NOTIFICATION_THEME_CHANGED: {
  748. Color text_color = get_theme_color("font_readonly_color", EditorStringName(Editor));
  749. file_details_path->add_theme_color_override(SceneStringName(font_color), text_color);
  750. no_results_label->add_theme_color_override(SceneStringName(font_color), text_color);
  751. panel_container->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  752. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  753. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));
  754. } else {
  755. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));
  756. }
  757. } break;
  758. }
  759. }
  760. void QuickOpenResultContainer::_bind_methods() {
  761. ADD_SIGNAL(MethodInfo("result_clicked"));
  762. }
  763. //------------------------- Result Item
  764. QuickOpenResultItem::QuickOpenResultItem() {
  765. set_focus_mode(FocusMode::FOCUS_ALL);
  766. _set_enabled(false);
  767. set_default_cursor_shape(CURSOR_POINTING_HAND);
  768. list_item = memnew(QuickOpenResultListItem);
  769. list_item->hide();
  770. add_child(list_item);
  771. grid_item = memnew(QuickOpenResultGridItem);
  772. grid_item->hide();
  773. add_child(grid_item);
  774. }
  775. void QuickOpenResultItem::set_display_mode(QuickOpenDisplayMode p_display_mode) {
  776. if (p_display_mode == QuickOpenDisplayMode::LIST) {
  777. grid_item->hide();
  778. grid_item->reset();
  779. list_item->show();
  780. } else {
  781. list_item->hide();
  782. list_item->reset();
  783. grid_item->show();
  784. }
  785. queue_redraw();
  786. }
  787. void QuickOpenResultItem::set_content(const QuickOpenResultCandidate &p_candidate) {
  788. _set_enabled(true);
  789. if (list_item->is_visible()) {
  790. list_item->set_content(p_candidate, enable_highlights);
  791. } else {
  792. grid_item->set_content(p_candidate, enable_highlights);
  793. }
  794. queue_redraw();
  795. }
  796. void QuickOpenResultItem::reset() {
  797. _set_enabled(false);
  798. is_hovering = false;
  799. is_selected = false;
  800. list_item->reset();
  801. grid_item->reset();
  802. }
  803. void QuickOpenResultItem::highlight_item(bool p_enabled) {
  804. is_selected = p_enabled;
  805. if (list_item->is_visible()) {
  806. if (p_enabled) {
  807. list_item->highlight_item(highlighted_font_color);
  808. } else {
  809. list_item->remove_highlight();
  810. }
  811. } else {
  812. if (p_enabled) {
  813. grid_item->highlight_item(highlighted_font_color);
  814. } else {
  815. grid_item->remove_highlight();
  816. }
  817. }
  818. queue_redraw();
  819. }
  820. void QuickOpenResultItem::_set_enabled(bool p_enabled) {
  821. set_visible(p_enabled);
  822. set_process(p_enabled);
  823. set_process_input(p_enabled);
  824. }
  825. void QuickOpenResultItem::_notification(int p_what) {
  826. switch (p_what) {
  827. case NOTIFICATION_MOUSE_ENTER:
  828. case NOTIFICATION_MOUSE_EXIT: {
  829. is_hovering = is_visible() && p_what == NOTIFICATION_MOUSE_ENTER;
  830. queue_redraw();
  831. } break;
  832. case NOTIFICATION_THEME_CHANGED: {
  833. selected_stylebox = get_theme_stylebox("selected", "Tree");
  834. hovering_stylebox = get_theme_stylebox(SNAME("hovered"), "Tree");
  835. highlighted_font_color = get_theme_color("font_focus_color", EditorStringName(Editor));
  836. } break;
  837. case NOTIFICATION_DRAW: {
  838. if (is_selected) {
  839. draw_style_box(selected_stylebox, Rect2(Point2(), get_size()));
  840. } else if (is_hovering) {
  841. draw_style_box(hovering_stylebox, Rect2(Point2(), get_size()));
  842. }
  843. } break;
  844. }
  845. }
  846. //----------------- List item
  847. static Vector2i _get_path_interval(const Vector2i &p_interval, int p_dir_index) {
  848. if (p_interval.x >= p_dir_index || p_interval.y < 1) {
  849. return { -1, -1 };
  850. }
  851. return { p_interval.x, MIN(p_interval.x + p_interval.y, p_dir_index) - p_interval.x };
  852. }
  853. static Vector2i _get_name_interval(const Vector2i &p_interval, int p_dir_index) {
  854. if (p_interval.x + p_interval.y <= p_dir_index || p_interval.y < 1) {
  855. return { -1, -1 };
  856. }
  857. int first_name_idx = p_dir_index + 1;
  858. int start = MAX(p_interval.x, first_name_idx);
  859. return { start - first_name_idx, p_interval.y - start + p_interval.x };
  860. }
  861. QuickOpenResultListItem::QuickOpenResultListItem() {
  862. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  863. add_theme_constant_override("margin_left", 6 * EDSCALE);
  864. add_theme_constant_override("margin_right", 6 * EDSCALE);
  865. hbc = memnew(HBoxContainer);
  866. hbc->add_theme_constant_override(SNAME("separation"), 4 * EDSCALE);
  867. add_child(hbc);
  868. const int max_size = 36 * EDSCALE;
  869. thumbnail = memnew(TextureRect);
  870. thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  871. thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  872. thumbnail->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  873. thumbnail->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  874. thumbnail->set_custom_minimum_size(Size2i(max_size, max_size));
  875. hbc->add_child(thumbnail);
  876. text_container = memnew(VBoxContainer);
  877. text_container->add_theme_constant_override(SNAME("separation"), -7 * EDSCALE);
  878. text_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  879. text_container->set_v_size_flags(Control::SIZE_FILL);
  880. hbc->add_child(text_container);
  881. name = memnew(HighlightedLabel);
  882. name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  883. name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  884. name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT);
  885. text_container->add_child(name);
  886. path = memnew(HighlightedLabel);
  887. path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  888. path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  889. path->add_theme_font_size_override(SceneStringName(font_size), 12 * EDSCALE);
  890. text_container->add_child(path);
  891. }
  892. void QuickOpenResultListItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) {
  893. thumbnail->set_texture(p_candidate.thumbnail);
  894. name->set_text(p_candidate.file_path.get_file());
  895. path->set_text(p_candidate.file_path.get_base_dir());
  896. name->reset_highlights();
  897. path->reset_highlights();
  898. if (p_highlight && p_candidate.result != nullptr) {
  899. for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) {
  900. for (const Vector2i &interval : match.substrings) {
  901. path->add_highlight(_get_path_interval(interval, p_candidate.result->dir_index));
  902. name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index));
  903. }
  904. }
  905. }
  906. }
  907. void QuickOpenResultListItem::reset() {
  908. thumbnail->set_texture(nullptr);
  909. name->set_text("");
  910. path->set_text("");
  911. name->reset_highlights();
  912. path->reset_highlights();
  913. }
  914. void QuickOpenResultListItem::highlight_item(const Color &p_color) {
  915. name->add_theme_color_override(SceneStringName(font_color), p_color);
  916. }
  917. void QuickOpenResultListItem::remove_highlight() {
  918. name->remove_theme_color_override(SceneStringName(font_color));
  919. }
  920. void QuickOpenResultListItem::_notification(int p_what) {
  921. switch (p_what) {
  922. case NOTIFICATION_THEME_CHANGED: {
  923. path->add_theme_color_override(SceneStringName(font_color), get_theme_color("font_disabled_color", EditorStringName(Editor)));
  924. } break;
  925. }
  926. }
  927. //--------------- Grid Item
  928. QuickOpenResultGridItem::QuickOpenResultGridItem() {
  929. set_custom_minimum_size(Size2i(120 * EDSCALE, 0));
  930. add_theme_constant_override("margin_top", 6 * EDSCALE);
  931. add_theme_constant_override("margin_left", 2 * EDSCALE);
  932. add_theme_constant_override("margin_right", 2 * EDSCALE);
  933. vbc = memnew(VBoxContainer);
  934. vbc->set_h_size_flags(Control::SIZE_FILL);
  935. vbc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  936. vbc->add_theme_constant_override(SNAME("separation"), 0);
  937. add_child(vbc);
  938. const int max_size = 64 * EDSCALE;
  939. thumbnail = memnew(TextureRect);
  940. thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  941. thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  942. thumbnail->set_custom_minimum_size(Size2i(max_size, max_size));
  943. vbc->add_child(thumbnail);
  944. name = memnew(HighlightedLabel);
  945. name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  946. name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  947. name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  948. name->add_theme_font_size_override(SceneStringName(font_size), 13 * EDSCALE);
  949. vbc->add_child(name);
  950. }
  951. void QuickOpenResultGridItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) {
  952. thumbnail->set_texture(p_candidate.thumbnail);
  953. name->set_text(p_candidate.file_path.get_file());
  954. name->set_tooltip_text(p_candidate.file_path);
  955. name->reset_highlights();
  956. if (p_highlight && p_candidate.result != nullptr) {
  957. for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) {
  958. for (const Vector2i &interval : match.substrings) {
  959. name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index));
  960. }
  961. }
  962. }
  963. bool uses_icon = p_candidate.thumbnail->get_width() < (32 * EDSCALE);
  964. if (uses_icon || p_candidate.thumbnail->get_height() <= thumbnail->get_custom_minimum_size().y) {
  965. thumbnail->set_expand_mode(TextureRect::EXPAND_KEEP_SIZE);
  966. thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);
  967. } else {
  968. thumbnail->set_expand_mode(TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL);
  969. thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_SCALE);
  970. }
  971. }
  972. void QuickOpenResultGridItem::reset() {
  973. thumbnail->set_texture(nullptr);
  974. name->set_text("");
  975. name->reset_highlights();
  976. }
  977. void QuickOpenResultGridItem::highlight_item(const Color &p_color) {
  978. name->add_theme_color_override(SceneStringName(font_color), p_color);
  979. }
  980. void QuickOpenResultGridItem::remove_highlight() {
  981. name->remove_theme_color_override(SceneStringName(font_color));
  982. }