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/editor_file_system.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_paths.h"
  36. #include "editor/editor_resource_preview.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/filesystem_dock.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/margin_container.h"
  45. #include "scene/gui/panel_container.h"
  46. #include "scene/gui/separator.h"
  47. #include "scene/gui/texture_rect.h"
  48. #include "scene/gui/tree.h"
  49. void HighlightedLabel::draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing) {
  50. for (int i = get_lines_skipped(); i < p_line_limit; i++) {
  51. RID line = get_line_rid(i);
  52. Vector<Vector2> ranges = TS->shaped_text_get_selection(line, p_substr.x, p_substr.x + p_substr.y);
  53. Rect2 line_rect = get_line_rect(i);
  54. for (const Vector2 &range : ranges) {
  55. Rect2 rect = Rect2(Point2(range.x, 0) + line_rect.position, Size2(range.y - range.x, line_rect.size.y));
  56. rect.position = p_offset + line_rect.position;
  57. rect.position.x += range.x;
  58. rect.size = Size2(range.y - range.x, line_rect.size.y);
  59. rect.size.x = MIN(rect.size.x, line_rect.size.x - range.x);
  60. if (rect.size.x > 0) {
  61. draw_rect(rect, Color(1, 1, 1, 0.07), true);
  62. draw_rect(rect, Color(0.5, 0.7, 1.0, 0.4), false, 1);
  63. }
  64. }
  65. p_offset.y += line_spacing + TS->shaped_text_get_ascent(line) + TS->shaped_text_get_descent(line);
  66. }
  67. }
  68. void HighlightedLabel::add_highlight(const Vector2i &p_interval) {
  69. if (p_interval.y > 0) {
  70. highlights.append(p_interval);
  71. queue_redraw();
  72. }
  73. }
  74. void HighlightedLabel::reset_highlights() {
  75. highlights.clear();
  76. queue_redraw();
  77. }
  78. void HighlightedLabel::_notification(int p_notification) {
  79. if (p_notification == NOTIFICATION_DRAW) {
  80. if (highlights.is_empty()) {
  81. return;
  82. }
  83. Vector2 offset;
  84. int line_limit;
  85. int line_spacing;
  86. get_layout_data(offset, line_limit, line_spacing);
  87. for (const Vector2i &substr : highlights) {
  88. draw_substr_rects(substr, offset, line_limit, line_spacing);
  89. }
  90. }
  91. }
  92. EditorQuickOpenDialog::EditorQuickOpenDialog() {
  93. VBoxContainer *vbc = memnew(VBoxContainer);
  94. vbc->add_theme_constant_override("separation", 0);
  95. add_child(vbc);
  96. {
  97. // Search bar
  98. MarginContainer *mc = memnew(MarginContainer);
  99. mc->add_theme_constant_override("margin_top", 6);
  100. mc->add_theme_constant_override("margin_bottom", 6);
  101. mc->add_theme_constant_override("margin_left", 1);
  102. mc->add_theme_constant_override("margin_right", 1);
  103. vbc->add_child(mc);
  104. search_box = memnew(LineEdit);
  105. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  106. search_box->set_placeholder(TTR("Search files..."));
  107. search_box->set_accessibility_name(TTRC("Search"));
  108. search_box->set_clear_button_enabled(true);
  109. mc->add_child(search_box);
  110. }
  111. {
  112. container = memnew(QuickOpenResultContainer);
  113. container->connect("result_clicked", callable_mp(this, &EditorQuickOpenDialog::ok_pressed));
  114. vbc->add_child(container);
  115. }
  116. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorQuickOpenDialog::_search_box_text_changed));
  117. search_box->connect(SceneStringName(gui_input), callable_mp(container, &QuickOpenResultContainer::handle_search_box_input));
  118. register_text_enter(search_box);
  119. get_ok_button()->hide();
  120. }
  121. String EditorQuickOpenDialog::get_dialog_title(const Vector<StringName> &p_base_types) {
  122. if (p_base_types.size() > 1) {
  123. return TTR("Select Resource");
  124. }
  125. if (p_base_types[0] == SNAME("PackedScene")) {
  126. return TTR("Select Scene");
  127. }
  128. return TTR("Select") + " " + p_base_types[0];
  129. }
  130. void EditorQuickOpenDialog::popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback) {
  131. ERR_FAIL_COND(p_base_types.is_empty());
  132. ERR_FAIL_COND(!p_item_selected_callback.is_valid());
  133. item_selected_callback = p_item_selected_callback;
  134. container->init(p_base_types);
  135. get_ok_button()->set_disabled(container->has_nothing_selected());
  136. set_title(get_dialog_title(p_base_types));
  137. popup_centered_clamped(Size2(780, 650) * EDSCALE, 0.8f);
  138. search_box->grab_focus();
  139. }
  140. void EditorQuickOpenDialog::ok_pressed() {
  141. item_selected_callback.call(container->get_selected());
  142. container->save_selected_item();
  143. container->cleanup();
  144. search_box->clear();
  145. hide();
  146. }
  147. void EditorQuickOpenDialog::cancel_pressed() {
  148. container->cleanup();
  149. search_box->clear();
  150. }
  151. void EditorQuickOpenDialog::_search_box_text_changed(const String &p_query) {
  152. container->set_query_and_update(p_query);
  153. get_ok_button()->set_disabled(container->has_nothing_selected());
  154. }
  155. //------------------------- Result Container
  156. void style_button(Button *p_button) {
  157. p_button->set_flat(true);
  158. p_button->set_focus_mode(Control::FOCUS_NONE);
  159. p_button->set_default_cursor_shape(Control::CURSOR_POINTING_HAND);
  160. }
  161. QuickOpenResultContainer::QuickOpenResultContainer() {
  162. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  163. set_v_size_flags(Control::SIZE_EXPAND_FILL);
  164. add_theme_constant_override("separation", 0);
  165. history_file.instantiate();
  166. {
  167. // Results section
  168. panel_container = memnew(PanelContainer);
  169. panel_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  170. add_child(panel_container);
  171. {
  172. // No search results
  173. no_results_container = memnew(CenterContainer);
  174. no_results_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  175. no_results_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  176. panel_container->add_child(no_results_container);
  177. no_results_label = memnew(Label);
  178. no_results_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  179. no_results_label->add_theme_font_size_override(SceneStringName(font_size), 24 * EDSCALE);
  180. no_results_container->add_child(no_results_label);
  181. no_results_container->hide();
  182. }
  183. {
  184. // Search results
  185. scroll_container = memnew(ScrollContainer);
  186. scroll_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  187. scroll_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  188. scroll_container->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  189. scroll_container->hide();
  190. panel_container->add_child(scroll_container);
  191. list = memnew(VBoxContainer);
  192. list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  193. list->add_theme_constant_override(SNAME("separation"), 0);
  194. list->hide();
  195. scroll_container->add_child(list);
  196. grid = memnew(HFlowContainer);
  197. grid->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  198. grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  199. grid->add_theme_constant_override(SNAME("v_separation"), 0);
  200. grid->add_theme_constant_override(SNAME("h_separation"), 0);
  201. grid->hide();
  202. scroll_container->add_child(grid);
  203. file_context_menu = memnew(PopupMenu);
  204. file_context_menu->add_item(TTR("Show in FileSystem"), FILE_SHOW_IN_FILESYSTEM);
  205. file_context_menu->add_item(TTR("Show in File Manager"), FILE_SHOW_IN_FILE_MANAGER);
  206. file_context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &QuickOpenResultContainer::_menu_option));
  207. file_context_menu->hide();
  208. scroll_container->add_child(file_context_menu);
  209. }
  210. }
  211. {
  212. // Selected filepath
  213. file_details_path = memnew(Label);
  214. file_details_path->set_focus_mode(FOCUS_ACCESSIBILITY);
  215. file_details_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  216. file_details_path->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  217. file_details_path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  218. add_child(file_details_path);
  219. }
  220. {
  221. // Bottom bar
  222. HBoxContainer *bottom_bar = memnew(HBoxContainer);
  223. bottom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  224. bottom_bar->set_alignment(ALIGNMENT_END);
  225. bottom_bar->add_theme_constant_override("separation", 3);
  226. add_child(bottom_bar);
  227. fuzzy_search_toggle = memnew(CheckButton);
  228. style_button(fuzzy_search_toggle);
  229. fuzzy_search_toggle->set_text(TTR("Fuzzy Search"));
  230. fuzzy_search_toggle->set_tooltip_text(TTR("Enable fuzzy matching"));
  231. fuzzy_search_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_fuzzy_search));
  232. bottom_bar->add_child(fuzzy_search_toggle);
  233. include_addons_toggle = memnew(CheckButton);
  234. style_button(include_addons_toggle);
  235. include_addons_toggle->set_text(TTR("Addons"));
  236. include_addons_toggle->set_tooltip_text(TTR("Include files from addons"));
  237. include_addons_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_include_addons));
  238. bottom_bar->add_child(include_addons_toggle);
  239. VSeparator *vsep = memnew(VSeparator);
  240. vsep->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  241. vsep->set_custom_minimum_size(Size2i(0, 14 * EDSCALE));
  242. bottom_bar->add_child(vsep);
  243. display_mode_toggle = memnew(Button);
  244. display_mode_toggle->set_accessibility_name(TTRC("Display Mode"));
  245. style_button(display_mode_toggle);
  246. display_mode_toggle->connect(SceneStringName(pressed), callable_mp(this, &QuickOpenResultContainer::_toggle_display_mode));
  247. bottom_bar->add_child(display_mode_toggle);
  248. }
  249. }
  250. void QuickOpenResultContainer::_menu_option(int p_option) {
  251. switch (p_option) {
  252. case FILE_SHOW_IN_FILESYSTEM: {
  253. FileSystemDock::get_singleton()->navigate_to_path(get_selected());
  254. } break;
  255. case FILE_SHOW_IN_FILE_MANAGER: {
  256. String dir = ProjectSettings::get_singleton()->globalize_path(get_selected());
  257. OS::get_singleton()->shell_show_in_file_manager(dir, true);
  258. } break;
  259. }
  260. }
  261. void QuickOpenResultContainer::_ensure_result_vector_capacity() {
  262. int target_size = EDITOR_GET("filesystem/quick_open_dialog/max_results");
  263. int initial_size = result_items.size();
  264. for (int i = target_size; i < initial_size; i++) {
  265. result_items[i]->queue_free();
  266. }
  267. result_items.resize(target_size);
  268. for (int i = initial_size; i < target_size; i++) {
  269. QuickOpenResultItem *item = memnew(QuickOpenResultItem);
  270. item->connect(SceneStringName(gui_input), callable_mp(this, &QuickOpenResultContainer::_item_input).bind(i));
  271. result_items.write[i] = item;
  272. if (!never_opened) {
  273. _layout_result_item(item);
  274. }
  275. }
  276. }
  277. void QuickOpenResultContainer::init(const Vector<StringName> &p_base_types) {
  278. _ensure_result_vector_capacity();
  279. base_types = p_base_types;
  280. const int display_mode_behavior = EDITOR_GET("filesystem/quick_open_dialog/default_display_mode");
  281. const bool adaptive_display_mode = (display_mode_behavior == 0);
  282. const bool first_open = never_opened;
  283. if (adaptive_display_mode) {
  284. _set_display_mode(get_adaptive_display_mode(p_base_types));
  285. } else if (never_opened) {
  286. int last = EditorSettings::get_singleton()->get_project_metadata("quick_open_dialog", "last_mode", (int)QuickOpenDisplayMode::LIST);
  287. _set_display_mode((QuickOpenDisplayMode)last);
  288. }
  289. const bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching");
  290. const bool include_addons = EDITOR_GET("filesystem/quick_open_dialog/include_addons");
  291. fuzzy_search_toggle->set_pressed_no_signal(fuzzy_matching);
  292. include_addons_toggle->set_pressed_no_signal(include_addons);
  293. never_opened = false;
  294. const bool enable_highlights = EDITOR_GET("filesystem/quick_open_dialog/show_search_highlight");
  295. for (QuickOpenResultItem *E : result_items) {
  296. E->enable_highlights = enable_highlights;
  297. }
  298. if (first_open && history_file->load(_get_cache_file_path()) == OK) {
  299. // Load history when opening for the first time.
  300. file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object")));
  301. bool history_modified = false;
  302. Vector<String> history_keys = history_file->get_section_keys("selected_history");
  303. for (const String &type : history_keys) {
  304. const StringName type_name = type;
  305. const PackedStringArray paths = history_file->get_value("selected_history", type);
  306. PackedStringArray cleaned_paths;
  307. cleaned_paths.resize(paths.size());
  308. Vector<QuickOpenResultCandidate> loaded_candidates;
  309. loaded_candidates.resize(paths.size());
  310. {
  311. QuickOpenResultCandidate *candidates_write = loaded_candidates.ptrw();
  312. String *cleanup_write = cleaned_paths.ptrw();
  313. int i = 0;
  314. for (String path : paths) {
  315. if (path.begins_with("uid://")) {
  316. ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(path);
  317. if (!ResourceUID::get_singleton()->has_id(id)) {
  318. continue;
  319. }
  320. path = ResourceUID::get_singleton()->get_id_path(id);
  321. }
  322. if (!ResourceLoader::exists(path)) {
  323. continue;
  324. }
  325. filetypes.insert(path, type_name);
  326. QuickOpenResultCandidate candidate;
  327. _setup_candidate(candidate, path);
  328. candidates_write[i] = candidate;
  329. cleanup_write[i] = path;
  330. i++;
  331. }
  332. loaded_candidates.resize(i);
  333. cleaned_paths.resize(i);
  334. selected_history.insert(type, loaded_candidates);
  335. if (i < paths.size()) {
  336. // Some paths removed, need to update history.
  337. if (i == 0) {
  338. history_file->erase_section_key("selected_history", type);
  339. } else {
  340. history_file->set_value("selected_history", type, cleaned_paths);
  341. }
  342. history_modified = true;
  343. }
  344. }
  345. }
  346. if (history_modified) {
  347. history_file->save(_get_cache_file_path());
  348. }
  349. }
  350. _create_initial_results();
  351. }
  352. void QuickOpenResultContainer::_sort_filepaths(int p_max_results) {
  353. struct FilepathComparator {
  354. bool operator()(const String &p_lhs, const String &p_rhs) const {
  355. // Sort on (length, alphanumeric) to prioritize shorter filepaths
  356. return p_lhs.length() == p_rhs.length() ? p_lhs < p_rhs : p_lhs.length() < p_rhs.length();
  357. }
  358. };
  359. SortArray<String, FilepathComparator> sorter;
  360. if (filepaths.size() > p_max_results) {
  361. sorter.partial_sort(0, filepaths.size(), p_max_results, filepaths.ptrw());
  362. } else {
  363. sorter.sort(filepaths.ptrw(), filepaths.size());
  364. }
  365. }
  366. void QuickOpenResultContainer::_create_initial_results() {
  367. file_type_icons.clear();
  368. file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object")));
  369. filepaths.clear();
  370. filetypes.clear();
  371. history_set.clear();
  372. Vector<QuickOpenResultCandidate> *history = _get_history();
  373. if (history) {
  374. for (const QuickOpenResultCandidate &candidate : *history) {
  375. history_set.insert(candidate.file_path);
  376. }
  377. }
  378. _find_filepaths_in_folder(EditorFileSystem::get_singleton()->get_filesystem(), include_addons_toggle->is_pressed());
  379. _sort_filepaths(result_items.size());
  380. max_total_results = MIN(filepaths.size(), result_items.size());
  381. update_results();
  382. }
  383. void QuickOpenResultContainer::_find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons) {
  384. for (int i = 0; i < p_directory->get_subdir_count(); i++) {
  385. if (p_include_addons || p_directory->get_name() != "addons") {
  386. _find_filepaths_in_folder(p_directory->get_subdir(i), p_include_addons);
  387. }
  388. }
  389. for (int i = 0; i < p_directory->get_file_count(); i++) {
  390. String file_path = p_directory->get_file_path(i);
  391. const StringName engine_type = p_directory->get_file_type(i);
  392. const StringName script_type = p_directory->get_file_resource_script_class(i);
  393. const bool is_engine_type = script_type == StringName();
  394. const StringName &actual_type = is_engine_type ? engine_type : script_type;
  395. for (const StringName &parent_type : base_types) {
  396. 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));
  397. if (is_valid) {
  398. filepaths.append(file_path);
  399. filetypes.insert(file_path, actual_type);
  400. break; // Stop testing base types as soon as we get a match.
  401. }
  402. }
  403. }
  404. }
  405. void QuickOpenResultContainer::set_query_and_update(const String &p_query) {
  406. query = p_query;
  407. update_results();
  408. }
  409. Vector<QuickOpenResultCandidate> *QuickOpenResultContainer::_get_history() {
  410. if (base_types.size() == 1) {
  411. return selected_history.lookup_ptr(base_types[0]);
  412. }
  413. return nullptr;
  414. }
  415. void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &p_candidate, const String &p_filepath) {
  416. p_candidate.file_path = ResourceUID::ensure_path(p_filepath);
  417. p_candidate.result = nullptr;
  418. StringName actual_type;
  419. {
  420. StringName *actual_type_ptr = filetypes.lookup_ptr(p_filepath);
  421. if (actual_type_ptr) {
  422. actual_type = *actual_type_ptr;
  423. } else {
  424. ERR_PRINT(vformat("EditorQuickOpenDialog: No type for path %s.", p_candidate.file_path));
  425. }
  426. }
  427. EditorResourcePreview::PreviewItem item = EditorResourcePreview::get_singleton()->get_resource_preview_if_available(p_candidate.file_path);
  428. if (item.preview.is_valid()) {
  429. p_candidate.thumbnail = item.preview;
  430. } else if (file_type_icons.has(actual_type)) {
  431. p_candidate.thumbnail = *file_type_icons.lookup_ptr(actual_type);
  432. } else if (has_theme_icon(actual_type, EditorStringName(EditorIcons))) {
  433. p_candidate.thumbnail = get_editor_theme_icon(actual_type);
  434. file_type_icons.insert(actual_type, p_candidate.thumbnail);
  435. } else {
  436. p_candidate.thumbnail = *file_type_icons.lookup_ptr(SNAME("__default_icon"));
  437. }
  438. }
  439. void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result) {
  440. _setup_candidate(p_candidate, p_result.target);
  441. p_candidate.result = &p_result;
  442. }
  443. void QuickOpenResultContainer::update_results() {
  444. candidates.clear();
  445. if (query.is_empty()) {
  446. _use_default_candidates();
  447. } else {
  448. _score_and_sort_candidates();
  449. }
  450. _update_result_items(MIN(candidates.size(), max_total_results), 0);
  451. }
  452. void QuickOpenResultContainer::_use_default_candidates() {
  453. Vector<QuickOpenResultCandidate> *history = _get_history();
  454. if (history) {
  455. candidates.append_array(*history);
  456. }
  457. int count = candidates.size();
  458. candidates.resize(MIN(max_total_results, filepaths.size()));
  459. for (const String &filepath : filepaths) {
  460. if (count >= max_total_results) {
  461. break;
  462. }
  463. if (!history || !history_set.has(filepath)) {
  464. _setup_candidate(candidates.write[count++], filepath);
  465. }
  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. "Font",
  680. "Texture2D",
  681. "Material",
  682. "Mesh"
  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.lookup_ptr(base_type);
  706. if (!type_history) {
  707. selected_history.insert(base_type, Vector<QuickOpenResultCandidate>());
  708. type_history = selected_history.lookup_ptr(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. }