editor_quick_open_dialog.cpp 45 KB

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