Path: blob/master/editor/gui/editor_quick_open_dialog.cpp
20941 views
/**************************************************************************/1/* editor_quick_open_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_quick_open_dialog.h"3132#include "core/config/project_settings.h"33#include "core/string/fuzzy_search.h"34#include "editor/docks/filesystem_dock.h"35#include "editor/editor_node.h"36#include "editor/editor_string_names.h"37#include "editor/editor_undo_redo_manager.h"38#include "editor/file_system/editor_file_system.h"39#include "editor/file_system/editor_paths.h"40#include "editor/gui/editor_toaster.h"41#include "editor/inspector/editor_resource_preview.h"42#include "editor/inspector/multi_node_edit.h"43#include "editor/settings/editor_settings.h"44#include "editor/themes/editor_scale.h"45#include "scene/gui/center_container.h"46#include "scene/gui/check_button.h"47#include "scene/gui/flow_container.h"48#include "scene/gui/line_edit.h"49#include "scene/gui/margin_container.h"50#include "scene/gui/panel_container.h"51#include "scene/gui/separator.h"52#include "scene/gui/texture_rect.h"53#include "scene/gui/tree.h"5455void HighlightedLabel::draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing) {56for (int i = get_lines_skipped(); i < p_line_limit; i++) {57RID line = get_line_rid(i);58Vector<Vector2> ranges = TS->shaped_text_get_selection(line, p_substr.x, p_substr.x + p_substr.y);59Rect2 line_rect = get_line_rect(i);60for (const Vector2 &range : ranges) {61Rect2 rect = Rect2(Point2(range.x, 0) + line_rect.position, Size2(range.y - range.x, line_rect.size.y));62rect.position = p_offset + line_rect.position;63rect.position.x += range.x;64rect.size = Size2(range.y - range.x, line_rect.size.y);65rect.size.x = MIN(rect.size.x, line_rect.size.x - range.x);66if (rect.size.x > 0) {67draw_rect(rect, Color(1, 1, 1, 0.07), true);68draw_rect(rect, Color(0.5, 0.7, 1.0, 0.4), false, 1);69}70}71p_offset.y += line_spacing + TS->shaped_text_get_ascent(line) + TS->shaped_text_get_descent(line);72}73}7475void HighlightedLabel::add_highlight(const Vector2i &p_interval) {76if (p_interval.y > 0) {77highlights.append(p_interval);78queue_redraw();79}80}8182void HighlightedLabel::reset_highlights() {83highlights.clear();84queue_redraw();85}8687void HighlightedLabel::_notification(int p_notification) {88if (p_notification == NOTIFICATION_DRAW) {89if (highlights.is_empty()) {90return;91}9293Vector2 offset;94int line_limit;95int line_spacing;96get_layout_data(offset, line_limit, line_spacing);9798for (const Vector2i &substr : highlights) {99draw_substr_rects(substr, offset, line_limit, line_spacing);100}101}102}103104EditorQuickOpenDialog::EditorQuickOpenDialog() {105VBoxContainer *vbc = memnew(VBoxContainer);106vbc->add_theme_constant_override("separation", 0);107add_child(vbc);108109{110// Search bar111MarginContainer *mc = memnew(MarginContainer);112mc->add_theme_constant_override("margin_top", 6);113mc->add_theme_constant_override("margin_bottom", 6);114mc->add_theme_constant_override("margin_left", 1);115mc->add_theme_constant_override("margin_right", 1);116vbc->add_child(mc);117118search_box = memnew(LineEdit);119search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);120search_box->set_placeholder(TTR("Search files..."));121search_box->set_accessibility_name(TTRC("Search"));122search_box->set_clear_button_enabled(true);123mc->add_child(search_box);124}125126{127container = memnew(QuickOpenResultContainer);128container->connect("selection_changed", callable_mp(this, &EditorQuickOpenDialog::selection_changed));129container->connect("result_clicked", callable_mp(this, &EditorQuickOpenDialog::item_pressed));130vbc->add_child(container);131}132133search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorQuickOpenDialog::_search_box_text_changed));134search_box->connect(SceneStringName(gui_input), callable_mp(container, &QuickOpenResultContainer::handle_search_box_input));135register_text_enter(search_box);136get_ok_button()->hide();137}138139String EditorQuickOpenDialog::get_dialog_title(const Vector<StringName> &p_base_types) {140if (p_base_types.size() > 1) {141return TTR("Select Resource");142}143144if (p_base_types[0] == SNAME("PackedScene")) {145return TTR("Select Scene");146}147148return TTR("Select") + " " + p_base_types[0];149}150151void EditorQuickOpenDialog::popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback) {152ERR_FAIL_COND(p_base_types.is_empty());153ERR_FAIL_COND(!p_item_selected_callback.is_valid());154155property_object = nullptr;156property_path = "";157item_selected_callback = p_item_selected_callback;158159container->init(p_base_types);160container->set_instant_preview_toggle_visible(false);161_finish_dialog_setup(p_base_types);162}163164void EditorQuickOpenDialog::popup_dialog_for_property(const Vector<StringName> &p_base_types, Object *p_obj, const StringName &p_path, const Callable &p_item_selected_callback) {165ERR_FAIL_NULL(p_obj);166ERR_FAIL_COND(p_base_types.is_empty());167ERR_FAIL_COND(!p_item_selected_callback.is_valid());168169property_object = p_obj;170property_path = p_path;171item_selected_callback = p_item_selected_callback;172initial_property_value = property_object->get(property_path);173174// Reset this, so that the property isn't updated immediately upon opening175// the window.176initial_selection_performed = false;177178container->init(p_base_types);179container->set_instant_preview_toggle_visible(true);180_finish_dialog_setup(p_base_types);181}182183void EditorQuickOpenDialog::_finish_dialog_setup(const Vector<StringName> &p_base_types) {184get_ok_button()->set_disabled(container->has_nothing_selected());185set_title(get_dialog_title(p_base_types));186popup_centered_clamped(Size2(780, 650) * EDSCALE, 0.8f);187search_box->grab_focus();188}189190void EditorQuickOpenDialog::ok_pressed() {191container->save_selected_item();192193update_property();194container->cleanup();195search_box->clear();196hide();197}198199bool EditorQuickOpenDialog::_is_instant_preview_active() const {200return property_object != nullptr && container->is_instant_preview_enabled();201}202203void EditorQuickOpenDialog::selection_changed() {204if (!_is_instant_preview_active()) {205return;206}207208// This prevents the property from being changed the first time the Quick Open209// window is opened.210if (!initial_selection_performed) {211initial_selection_performed = true;212} else {213preview_property();214}215}216217void EditorQuickOpenDialog::item_pressed(bool p_double_click) {218// A double-click should always be taken as a "confirm" action.219if (p_double_click) {220ok_pressed();221return;222}223224// Single-clicks should be taken as a "confirm" action only if Instant Preview225// isn't currently enabled, or the property object is null for some reason.226if (!_is_instant_preview_active()) {227ok_pressed();228}229}230231void EditorQuickOpenDialog::preview_property() {232ERR_FAIL_COND(container->get_selected() == ResourceUID::INVALID_ID);233String path = container->get_selected_path();234235Ref<Resource> loaded_resource = ResourceLoader::load(path);236ERR_FAIL_COND_MSG(loaded_resource.is_null(), "Cannot load resource from path '" + path + "'.");237238Resource *res = Object::cast_to<Resource>(property_object);239if (res) {240HashSet<Resource *> resources_found;241resources_found.insert(res);242if (EditorNode::find_recursive_resources(loaded_resource, resources_found)) {243EditorToaster::get_singleton()->popup_str(TTR("Recursion detected, Instant Preview failed."), EditorToaster::SEVERITY_ERROR);244loaded_resource = Ref<Resource>();245}246}247248// MultiNodeEdit has adding to the undo/redo stack baked into its set function.249// As such, we have to specifically call a version of its setter that doesn't250// create undo/redo actions.251property_object->set_block_signals(true);252if (Object::cast_to<MultiNodeEdit>(property_object)) {253Object::cast_to<MultiNodeEdit>(property_object)->_set_impl(property_path, loaded_resource, "", false);254} else {255property_object->set(property_path, loaded_resource);256}257property_object->set_block_signals(false);258}259260void EditorQuickOpenDialog::update_property() {261// Set the property back to the initial value first, so that the undo action262// has the correct object.263if (property_object) {264if (Object::cast_to<MultiNodeEdit>(property_object)) {265Object::cast_to<MultiNodeEdit>(property_object)->_set_impl(property_path, initial_property_value, "", false);266} else {267property_object->set(property_path, initial_property_value);268}269}270271if (!item_selected_callback.is_valid()) {272String err_msg = "The callback provided to the Quick Open dialog was invalid.";273if (_is_instant_preview_active()) {274err_msg += " Try disabling \"Instant Preview\" as a workaround.";275}276ERR_FAIL_MSG(err_msg);277}278279item_selected_callback.call(container->get_selected_path());280}281282void EditorQuickOpenDialog::cancel_pressed() {283if (property_object) {284if (Object::cast_to<MultiNodeEdit>(property_object)) {285Object::cast_to<MultiNodeEdit>(property_object)->_set_impl(property_path, initial_property_value, "", false);286} else {287property_object->set(property_path, initial_property_value);288}289}290container->cleanup();291search_box->clear();292}293294void EditorQuickOpenDialog::_search_box_text_changed(const String &p_query) {295container->set_query_and_update(p_query);296get_ok_button()->set_disabled(container->has_nothing_selected());297}298299//------------------------- Result Container300301void style_button(Button *p_button) {302p_button->set_flat(true);303p_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);304}305306QuickOpenResultContainer::QuickOpenResultContainer() {307set_h_size_flags(Control::SIZE_EXPAND_FILL);308set_v_size_flags(Control::SIZE_EXPAND_FILL);309add_theme_constant_override("separation", 0);310history_file.instantiate();311312{313// Results section314panel_container = memnew(PanelContainer);315panel_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);316add_child(panel_container);317318{319// No search results320no_results_container = memnew(CenterContainer);321no_results_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);322no_results_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);323panel_container->add_child(no_results_container);324325no_results_label = memnew(Label);326no_results_label->set_focus_mode(FOCUS_ACCESSIBILITY);327no_results_label->add_theme_font_size_override(SceneStringName(font_size), 24 * EDSCALE);328no_results_container->add_child(no_results_label);329no_results_container->hide();330}331332{333MarginContainer *mc = memnew(MarginContainer);334mc->set_theme_type_variation("NoBorderHorizontalWindow");335mc->set_h_size_flags(Control::SIZE_EXPAND_FILL);336mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);337panel_container->add_child(mc);338339// Search results340scroll_container = memnew(ScrollContainer);341scroll_container->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);342scroll_container->set_scroll_hint_mode(ScrollContainer::SCROLL_HINT_MODE_ALL);343scroll_container->hide();344panel_container->add_child(scroll_container);345346list = memnew(VBoxContainer);347list->set_h_size_flags(Control::SIZE_EXPAND_FILL);348list->add_theme_constant_override(SNAME("separation"), 0);349list->hide();350scroll_container->add_child(list);351352grid = memnew(HFlowContainer);353grid->set_h_size_flags(Control::SIZE_EXPAND_FILL);354grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);355grid->add_theme_constant_override(SNAME("v_separation"), 0);356grid->add_theme_constant_override(SNAME("h_separation"), 0);357grid->hide();358scroll_container->add_child(grid);359360file_context_menu = memnew(PopupMenu);361file_context_menu->add_item(TTR("Show in FileSystem"), FILE_SHOW_IN_FILESYSTEM);362file_context_menu->add_item(TTR("Show in File Manager"), FILE_SHOW_IN_FILE_MANAGER);363file_context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &QuickOpenResultContainer::_menu_option));364file_context_menu->hide();365scroll_container->add_child(file_context_menu);366}367}368369{370// Selected filepath371file_details_path = memnew(Label);372file_details_path->set_focus_mode(FOCUS_ACCESSIBILITY);373file_details_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);374file_details_path->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);375file_details_path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);376add_child(file_details_path);377}378379{380// Bottom bar381HBoxContainer *bottom_bar = memnew(HBoxContainer);382bottom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);383bottom_bar->set_alignment(ALIGNMENT_END);384bottom_bar->add_theme_constant_override("separation", 3);385add_child(bottom_bar);386387instant_preview_toggle = memnew(CheckButton);388style_button(instant_preview_toggle);389instant_preview_toggle->set_text(TTRC("Instant Preview"));390instant_preview_toggle->set_tooltip_text(TTRC("Selected resource will be previewed in the editor before accepting."));391instant_preview_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_instant_preview));392bottom_bar->add_child(instant_preview_toggle);393394fuzzy_search_toggle = memnew(CheckButton);395style_button(fuzzy_search_toggle);396fuzzy_search_toggle->set_text(TTR("Fuzzy Search"));397fuzzy_search_toggle->set_tooltip_text(TTRC("Include approximate matches."));398fuzzy_search_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_fuzzy_search));399bottom_bar->add_child(fuzzy_search_toggle);400401include_addons_toggle = memnew(CheckButton);402style_button(include_addons_toggle);403include_addons_toggle->set_text(TTR("Addons"));404include_addons_toggle->set_tooltip_text(TTR("Include files from addons"));405include_addons_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_include_addons));406bottom_bar->add_child(include_addons_toggle);407408VSeparator *vsep = memnew(VSeparator);409vsep->set_v_size_flags(Control::SIZE_SHRINK_CENTER);410vsep->set_custom_minimum_size(Size2i(0, 14 * EDSCALE));411bottom_bar->add_child(vsep);412413display_mode_toggle = memnew(Button);414display_mode_toggle->set_accessibility_name(TTRC("Display Mode"));415style_button(display_mode_toggle);416display_mode_toggle->connect(SceneStringName(pressed), callable_mp(this, &QuickOpenResultContainer::_toggle_display_mode));417bottom_bar->add_child(display_mode_toggle);418}419}420421void QuickOpenResultContainer::_menu_option(int p_option) {422ERR_FAIL_COND(get_selected() == ResourceUID::INVALID_ID);423String selected_path = get_selected_path();424425switch (p_option) {426case FILE_SHOW_IN_FILESYSTEM: {427FileSystemDock::get_singleton()->navigate_to_path(selected_path);428} break;429case FILE_SHOW_IN_FILE_MANAGER: {430String dir = ProjectSettings::get_singleton()->globalize_path(selected_path);431OS::get_singleton()->shell_show_in_file_manager(dir, true);432} break;433}434}435436void QuickOpenResultContainer::_ensure_result_vector_capacity() {437int target_size = EDITOR_GET("filesystem/quick_open_dialog/max_results");438int initial_size = result_items.size();439for (int i = target_size; i < initial_size; i++) {440result_items[i]->queue_free();441}442result_items.resize(target_size);443for (int i = initial_size; i < target_size; i++) {444QuickOpenResultItem *item = memnew(QuickOpenResultItem);445item->connect(SceneStringName(gui_input), callable_mp(this, &QuickOpenResultContainer::_item_input).bind(i));446result_items.write[i] = item;447if (!never_opened) {448_layout_result_item(item);449}450}451}452453void QuickOpenResultContainer::init(const Vector<StringName> &p_base_types) {454_ensure_result_vector_capacity();455base_types = p_base_types;456457const int display_mode_behavior = EDITOR_GET("filesystem/quick_open_dialog/default_display_mode");458const bool adaptive_display_mode = (display_mode_behavior == 0);459const bool first_open = never_opened;460461if (adaptive_display_mode) {462_set_display_mode(get_adaptive_display_mode(p_base_types));463} else if (never_opened) {464int last = EditorSettings::get_singleton()->get_project_metadata("quick_open_dialog", "last_mode", (int)QuickOpenDisplayMode::LIST);465_set_display_mode((QuickOpenDisplayMode)last);466}467468const bool do_instant_preview = EDITOR_GET("filesystem/quick_open_dialog/instant_preview");469const bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching");470const bool include_addons = EDITOR_GET("filesystem/quick_open_dialog/include_addons");471instant_preview_toggle->set_pressed_no_signal(do_instant_preview);472fuzzy_search_toggle->set_pressed_no_signal(fuzzy_matching);473include_addons_toggle->set_pressed_no_signal(include_addons);474never_opened = false;475476const bool enable_highlights = EDITOR_GET("filesystem/quick_open_dialog/show_search_highlight");477for (QuickOpenResultItem *E : result_items) {478E->enable_highlights = enable_highlights;479}480481bool history_modified = false;482483if (first_open && history_file->load(_get_cache_file_path()) == OK) {484// Load history when opening for the first time.485file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object")));486487Vector<String> history_keys = history_file->get_section_keys("selected_history");488for (const String &type : history_keys) {489const StringName type_name = type;490const PackedStringArray history_uids = history_file->get_value("selected_history", type);491492PackedStringArray cleaned_text_uids;493cleaned_text_uids.resize(history_uids.size());494495Vector<ResourceUID::ID> cleaned_ids;496cleaned_ids.resize(history_uids.size());497498{499String *text_write = cleaned_text_uids.ptrw();500ResourceUID::ID *id_write = cleaned_ids.ptrw();501int i = 0;502for (String uid : history_uids) {503#ifndef DISABLE_DEPRECATED504if (!uid.begins_with("uid://")) {505// uid might be a path here, if config was written by older editor version506ResourceUID::ID id = EditorFileSystem::get_singleton()->get_file_uid(uid);507if (id == ResourceUID::INVALID_ID) {508continue;509}510uid = ResourceUID::get_singleton()->id_to_text(id);511}512#endif513514ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(uid);515if (id == ResourceUID::INVALID_ID || !ResourceUID::get_singleton()->has_id(id)) {516continue;517}518519filetypes.insert(id, type_name);520text_write[i] = uid;521id_write[i] = id;522i++;523}524525cleaned_text_uids.resize(i);526selected_history.insert(type, cleaned_ids);527528if (i < history_uids.size()) {529// Some paths removed, need to update history.530if (i == 0) {531history_file->erase_section_key("selected_history", type);532} else {533history_file->set_value("selected_history", type, cleaned_text_uids);534}535history_modified = true;536}537}538}539} else if (!first_open && base_types.size() == 1) {540const StringName &type = base_types[0];541Vector<ResourceUID::ID> *history = selected_history.getptr(type);542543if (history) {544Vector<ResourceUID::ID> clean_history;545546for (const ResourceUID::ID &uid : *history) {547if (ResourceUID::get_singleton()->has_id(uid)) {548clean_history.push_back(uid);549} else {550history_modified = true;551}552}553554if (clean_history.is_empty()) {555selected_history.erase(type);556} else if (history_modified) {557*history = clean_history;558}559}560}561562if (history_modified) {563history_file->save(_get_cache_file_path());564}565566_create_initial_results();567}568569void QuickOpenResultContainer::_sort_uids(int p_max_results) {570struct FilepathComparator {571bool operator()(const ResourceUID::ID &p_lhs, const ResourceUID::ID &p_rhs) const {572String lhs_path = ResourceUID::get_singleton()->get_id_path(p_lhs);573String rhs_path = ResourceUID::get_singleton()->get_id_path(p_rhs);574575// Sort on (length, alphanumeric) to prioritize shorter filepaths576return lhs_path.length() == rhs_path.length() ? lhs_path < rhs_path : lhs_path.length() < rhs_path.length();577}578};579580SortArray<ResourceUID::ID, FilepathComparator> sorter{};581582if ((int)uids.size() > p_max_results) {583sorter.partial_sort(0, uids.size(), p_max_results, uids.ptr());584} else {585sorter.sort(uids.ptr(), uids.size());586}587}588589void QuickOpenResultContainer::_create_initial_results() {590file_type_icons.clear();591file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object")));592uids.clear();593filetypes.clear();594history_set.clear();595596Vector<ResourceUID::ID> *history = _get_history();597if (history) {598for (const ResourceUID::ID &uid : *history) {599history_set.insert(uid);600}601}602603_find_uids_in_folder(EditorFileSystem::get_singleton()->get_filesystem(), include_addons_toggle->is_pressed());604_sort_uids(result_items.size());605max_total_results = MIN(uids.size(), result_items.size());606update_results();607}608609void QuickOpenResultContainer::_find_uids_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons) {610for (int i = 0; i < p_directory->get_subdir_count(); i++) {611if (p_include_addons || p_directory->get_name() != "addons") {612_find_uids_in_folder(p_directory->get_subdir(i), p_include_addons);613}614}615616for (int i = 0; i < p_directory->get_file_count(); i++) {617ResourceUID::ID uid = p_directory->get_file_uid(i);618if (uid == ResourceUID::INVALID_ID) {619continue;620}621622const StringName engine_type = p_directory->get_file_type(i);623const StringName script_type = p_directory->get_file_resource_script_class(i);624625const bool is_engine_type = script_type == StringName();626const StringName &actual_type = is_engine_type ? engine_type : script_type;627628for (const StringName &parent_type : base_types) {629bool 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));630631if (is_valid) {632uids.push_back(uid);633filetypes.insert(uid, actual_type);634break; // Stop testing base types as soon as we get a match.635}636}637}638}639640void QuickOpenResultContainer::set_query_and_update(const String &p_query) {641query = p_query;642update_results();643}644645Vector<ResourceUID::ID> *QuickOpenResultContainer::_get_history() {646if (base_types.size() == 1) {647return selected_history.getptr(base_types[0]);648}649return nullptr;650}651652QuickOpenResultCandidate QuickOpenResultCandidate::from_uid(const ResourceUID::ID &p_uid, bool &r_success) {653if (p_uid == ResourceUID::INVALID_ID || !ResourceUID::get_singleton()->has_id(p_uid)) {654r_success = false;655return QuickOpenResultCandidate();656}657658QuickOpenResultCandidate candidate;659candidate.uid = p_uid;660candidate.result = nullptr;661r_success = true;662return candidate;663}664665QuickOpenResultCandidate QuickOpenResultCandidate::from_result(const FuzzySearchResult &p_result, bool &r_success) {666ResourceUID::ID uid = EditorFileSystem::get_singleton()->get_file_uid(p_result.target);667668QuickOpenResultCandidate candidate = from_uid(uid, r_success);669if (!r_success) {670return QuickOpenResultCandidate();671}672673candidate.result = &p_result;674return candidate;675}676677void QuickOpenResultContainer::_add_candidate(QuickOpenResultCandidate &p_candidate) {678ERR_FAIL_COND(!ResourceUID::get_singleton()->has_id(p_candidate.uid));679680StringName actual_type;681{682StringName *actual_type_ptr = filetypes.getptr(p_candidate.uid);683if (actual_type_ptr) {684actual_type = *actual_type_ptr;685} else {686ERR_PRINT(vformat("EditorQuickOpenDialog: No type for path %s.", ResourceUID::get_singleton()->get_id_path(p_candidate.uid)));687}688}689690String file_path = ResourceUID::get_singleton()->get_id_path(p_candidate.uid);691EditorResourcePreview::PreviewItem item = EditorResourcePreview::get_singleton()->get_resource_preview_if_available(file_path);692if (item.preview.is_valid()) {693p_candidate.thumbnail = item.preview;694} else if (file_type_icons.has(actual_type)) {695p_candidate.thumbnail = *file_type_icons.getptr(actual_type);696} else if (has_theme_icon(actual_type, EditorStringName(EditorIcons))) {697p_candidate.thumbnail = get_editor_theme_icon(actual_type);698file_type_icons.insert(actual_type, p_candidate.thumbnail);699} else {700p_candidate.thumbnail = *file_type_icons.getptr(SNAME("__default_icon"));701}702703candidates.push_back(p_candidate);704candidates_uids.insert(p_candidate.uid);705}706707void QuickOpenResultContainer::update_results() {708candidates.clear();709candidates_uids.clear();710711if (query.is_empty()) {712_use_default_candidates();713} else {714_score_and_sort_candidates();715}716717_update_result_items(MIN(candidates.size(), max_total_results), 0);718}719720void QuickOpenResultContainer::_use_default_candidates() {721HashSet<ResourceUID::ID> existing_uids;722723Vector<ResourceUID::ID> *history = _get_history();724if (history) {725for (const ResourceUID::ID &uid : *history) {726bool success;727QuickOpenResultCandidate candidate = QuickOpenResultCandidate::from_uid(uid, success);728if (!success) {729continue;730}731_add_candidate(candidate);732}733}734735for (const ResourceUID::ID &uid : uids) {736if (candidates.size() >= max_total_results) {737break;738}739if (candidates_uids.has(uid)) {740continue;741}742743bool success;744QuickOpenResultCandidate candidate = QuickOpenResultCandidate::from_uid(uid, success);745if (!success) {746continue;747}748749_add_candidate(candidate);750}751}752753void QuickOpenResultContainer::_update_fuzzy_search_results() {754FuzzySearch fuzzy_search;755fuzzy_search.start_offset = 6; // Don't match against "res://" at the start of each filepath.756fuzzy_search.set_query(query);757fuzzy_search.max_results = max_total_results;758bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching");759int max_misses = EDITOR_GET("filesystem/quick_open_dialog/max_fuzzy_misses");760fuzzy_search.allow_subsequences = fuzzy_matching;761fuzzy_search.max_misses = fuzzy_matching ? max_misses : 0;762763PackedStringArray paths;764paths.reserve_exact(uids.size());765766for (const ResourceUID::ID &uid : uids) {767paths.push_back(ResourceUID::get_singleton()->get_id_path(uid));768}769770fuzzy_search.search_all(paths, search_results);771}772773void QuickOpenResultContainer::_score_and_sort_candidates() {774_update_fuzzy_search_results();775776for (const FuzzySearchResult &result : search_results) {777bool success;778QuickOpenResultCandidate candidate = QuickOpenResultCandidate::from_result(result, success);779if (!success) {780continue;781}782783_add_candidate(candidate);784}785}786787void QuickOpenResultContainer::_update_result_items(int p_new_visible_results_count, int p_new_selection_index) {788// Only need to update items that were not hidden in previous update.789int num_items_needing_updates = MAX(num_visible_results, p_new_visible_results_count);790num_visible_results = p_new_visible_results_count;791792for (int i = 0; i < num_items_needing_updates; i++) {793QuickOpenResultItem *item = result_items[i];794795if (i < num_visible_results) {796item->set_content(candidates[i]);797} else {798item->reset();799}800};801802const bool any_results = num_visible_results > 0;803_select_item(any_results ? p_new_selection_index : -1);804805scroll_container->set_visible(any_results);806no_results_container->set_visible(!any_results);807808if (!any_results) {809if (uids.is_empty()) {810no_results_label->set_text(TTR("No files found for this type"));811} else {812no_results_label->set_text(TTR("No results found"));813}814}815}816817void QuickOpenResultContainer::handle_search_box_input(const Ref<InputEvent> &p_ie) {818if (num_visible_results < 0) {819return;820}821822Ref<InputEventKey> key_event = p_ie;823if (key_event.is_valid() && key_event->is_pressed()) {824bool move_selection = false;825826switch (key_event->get_keycode()) {827case Key::UP:828case Key::DOWN:829case Key::PAGEUP:830case Key::PAGEDOWN: {831move_selection = true;832} break;833case Key::LEFT:834case Key::RIGHT: {835if (content_display_mode == QuickOpenDisplayMode::GRID) {836// Maybe strip off the shift modifier to allow non-selecting navigation by character?837if (key_event->get_modifiers_mask().is_empty()) {838move_selection = true;839}840}841} break;842default:843break; // Let the event through so it will reach the search box.844}845846if (move_selection) {847_move_selection_index(key_event->get_keycode());848queue_redraw();849accept_event();850}851}852}853854void QuickOpenResultContainer::_move_selection_index(Key p_key) {855// Don't move selection if there are no results.856if (num_visible_results <= 0) {857return;858}859const int max_index = num_visible_results - 1;860861int idx = selection_index;862if (content_display_mode == QuickOpenDisplayMode::LIST) {863if (p_key == Key::UP) {864idx = (idx == 0) ? max_index : (idx - 1);865} else if (p_key == Key::DOWN) {866idx = (idx == max_index) ? 0 : (idx + 1);867} else if (p_key == Key::PAGEUP) {868idx = (idx == 0) ? idx : MAX(idx - 10, 0);869} else if (p_key == Key::PAGEDOWN) {870idx = (idx == max_index) ? idx : MIN(idx + 10, max_index);871}872} else {873int column_count = grid->get_line_max_child_count();874875if (p_key == Key::LEFT) {876idx = (idx == 0) ? max_index : (idx - 1);877} else if (p_key == Key::RIGHT) {878idx = (idx == max_index) ? 0 : (idx + 1);879} else if (p_key == Key::UP) {880idx = (idx == 0) ? max_index : MAX(idx - column_count, 0);881} else if (p_key == Key::DOWN) {882idx = (idx == max_index) ? 0 : MIN(idx + column_count, max_index);883} else if (p_key == Key::PAGEUP) {884idx = (idx == 0) ? idx : MAX(idx - (3 * column_count), 0);885} else if (p_key == Key::PAGEDOWN) {886idx = (idx == max_index) ? idx : MIN(idx + (3 * column_count), max_index);887}888}889890_select_item(idx);891}892893void QuickOpenResultContainer::_select_item(int p_index) {894if (!has_nothing_selected()) {895result_items[selection_index]->highlight_item(false);896}897898selection_index = p_index;899900if (has_nothing_selected()) {901file_details_path->set_text("");902return;903}904905result_items[selection_index]->highlight_item(true);906bool in_history = history_set.has(candidates[selection_index].uid);907file_details_path->set_text(get_selected_path() + (in_history ? TTR(" (recently opened)") : ""));908909emit_signal(SNAME("selection_changed"));910911const QuickOpenResultItem *item = result_items[selection_index];912913// Copied from Tree.914const int selected_position = item->get_position().y;915const int selected_size = item->get_size().y;916const int scroll_window_size = scroll_container->get_size().y;917const int scroll_position = scroll_container->get_v_scroll();918919if (selected_position <= scroll_position) {920scroll_container->set_v_scroll(selected_position);921} else if (selected_position + selected_size > scroll_position + scroll_window_size) {922scroll_container->set_v_scroll(selected_position + selected_size - scroll_window_size);923}924}925926void QuickOpenResultContainer::_item_input(const Ref<InputEvent> &p_ev, int p_index) {927Ref<InputEventMouseButton> mb = p_ev;928929if (mb.is_valid() && mb->is_pressed()) {930if (mb->get_button_index() == MouseButton::LEFT) {931_select_item(p_index);932emit_signal(SNAME("result_clicked"), mb->is_double_click());933} else if (mb->get_button_index() == MouseButton::RIGHT) {934_select_item(p_index);935file_context_menu->set_position(result_items[p_index]->get_screen_position() + mb->get_position());936file_context_menu->reset_size();937file_context_menu->popup();938}939}940}941942void QuickOpenResultContainer::_toggle_instant_preview(bool p_pressed) {943EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/instant_preview", p_pressed);944}945946void QuickOpenResultContainer::_toggle_fuzzy_search(bool p_pressed) {947EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/enable_fuzzy_matching", p_pressed);948update_results();949}950951String QuickOpenResultContainer::_get_cache_file_path() const {952return EditorPaths::get_singleton()->get_project_settings_dir().path_join("quick_open_dialog_cache.cfg");953}954955void QuickOpenResultContainer::_toggle_include_addons(bool p_pressed) {956EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/include_addons", p_pressed);957cleanup();958_create_initial_results();959}960961void QuickOpenResultContainer::_toggle_display_mode() {962QuickOpenDisplayMode new_display_mode = (content_display_mode == QuickOpenDisplayMode::LIST) ? QuickOpenDisplayMode::GRID : QuickOpenDisplayMode::LIST;963_set_display_mode(new_display_mode);964}965966CanvasItem *QuickOpenResultContainer::_get_result_root() {967if (content_display_mode == QuickOpenDisplayMode::LIST) {968return list;969} else {970return grid;971}972}973974void QuickOpenResultContainer::_layout_result_item(QuickOpenResultItem *item) {975item->set_display_mode(content_display_mode);976Node *parent = item->get_parent();977if (parent) {978parent->remove_child(item);979}980_get_result_root()->add_child(item);981}982983void QuickOpenResultContainer::_set_display_mode(QuickOpenDisplayMode p_display_mode) {984CanvasItem *prev_root = _get_result_root();985986if (prev_root->is_visible() && content_display_mode == p_display_mode) {987return;988}989990content_display_mode = p_display_mode;991CanvasItem *next_root = _get_result_root();992993EditorSettings::get_singleton()->set_project_metadata("quick_open_dialog", "last_mode", (int)content_display_mode);994995prev_root->hide();996next_root->show();997998for (QuickOpenResultItem *item : result_items) {999_layout_result_item(item);1000}10011002_update_result_items(num_visible_results, selection_index);10031004if (content_display_mode == QuickOpenDisplayMode::LIST) {1005display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));1006display_mode_toggle->set_tooltip_text(TTR("Grid view"));1007} else {1008display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));1009display_mode_toggle->set_tooltip_text(TTR("List view"));1010}1011}10121013bool QuickOpenResultContainer::has_nothing_selected() const {1014return selection_index < 0;1015}10161017ResourceUID::ID QuickOpenResultContainer::get_selected() const {1018ERR_FAIL_COND_V_MSG(has_nothing_selected(), ResourceUID::INVALID_ID, "Tried to get selected file, but nothing was selected.");1019return candidates[selection_index].uid;1020}10211022String QuickOpenResultContainer::get_selected_path() const {1023ERR_FAIL_COND_V_MSG(has_nothing_selected(), "", "Tried to get selected file path, but nothing was selected.");1024String path = ResourceUID::get_singleton()->get_id_path(candidates[selection_index].uid);1025ERR_FAIL_COND_V_MSG(path.is_empty(), "", "Failed to get selected file path.");1026return path;1027}10281029QuickOpenDisplayMode QuickOpenResultContainer::get_adaptive_display_mode(const Vector<StringName> &p_base_types) {1030static const Vector<StringName> grid_preferred_types = {1031StringName("Font", true),1032StringName("Texture2D", true),1033StringName("Material", true),1034StringName("Mesh", true),1035};10361037for (const StringName &type : grid_preferred_types) {1038for (const StringName &base_type : p_base_types) {1039if (base_type == type || ClassDB::is_parent_class(base_type, type)) {1040return QuickOpenDisplayMode::GRID;1041}1042}1043}10441045return QuickOpenDisplayMode::LIST;1046}10471048String _get_uid_string(const String &p_filepath) {1049ResourceUID::ID id = EditorFileSystem::get_singleton()->get_file_uid(p_filepath);1050return id == ResourceUID::INVALID_ID ? p_filepath : ResourceUID::get_singleton()->id_to_text(id);1051}10521053bool QuickOpenResultContainer::is_instant_preview_enabled() const {1054return instant_preview_toggle && instant_preview_toggle->is_visible() && instant_preview_toggle->is_pressed();1055}10561057void QuickOpenResultContainer::set_instant_preview_toggle_visible(bool p_visible) {1058instant_preview_toggle->set_visible(p_visible);1059}10601061void QuickOpenResultContainer::save_selected_item() {1062if (base_types.size() > 1) {1063// Getting the type of the file and checking which base type it belongs to should be possible.1064// However, for now these are not supported, and we don't record this.1065return;1066}10671068const StringName &base_type = base_types[0];1069ResourceUID::ID selected = get_selected();1070Vector<ResourceUID::ID> *type_history = selected_history.getptr(base_type);10711072if (!type_history) {1073selected_history.insert(base_type, Vector<ResourceUID::ID>());1074type_history = selected_history.getptr(base_type);1075} else {1076for (int i = 0; i < type_history->size(); i++) {1077if (selected == type_history->get(i)) {1078type_history->remove_at(i);1079break;1080}1081}1082}10831084history_set.insert(selected);1085type_history->insert(0, selected);1086if (type_history->size() > MAX_HISTORY_SIZE) {1087type_history->resize(MAX_HISTORY_SIZE);1088}10891090PackedStringArray history_uids;1091history_uids.resize(type_history->size());1092{1093String *uids_write = history_uids.ptrw();10941095int i = 0;1096for (const ResourceUID::ID &uid : *type_history) {1097uids_write[i] = ResourceUID::get_singleton()->id_to_text(uid);1098i++;1099}1100}1101history_file->set_value("selected_history", base_type, history_uids);1102history_file->save(_get_cache_file_path());1103}11041105void QuickOpenResultContainer::cleanup() {1106num_visible_results = 0;1107candidates.clear();1108history_set.clear();1109_select_item(-1);11101111for (QuickOpenResultItem *item : result_items) {1112item->reset();1113}1114}11151116void QuickOpenResultContainer::_notification(int p_what) {1117switch (p_what) {1118case NOTIFICATION_THEME_CHANGED: {1119Color text_color = get_theme_color("font_readonly_color", EditorStringName(Editor));1120file_details_path->add_theme_color_override(SceneStringName(font_color), text_color);1121no_results_label->add_theme_color_override(SceneStringName(font_color), text_color);11221123file_context_menu->set_item_icon(FILE_SHOW_IN_FILESYSTEM, get_editor_theme_icon(SNAME("ShowInFileSystem")));1124file_context_menu->set_item_icon(FILE_SHOW_IN_FILE_MANAGER, get_editor_theme_icon(SNAME("Filesystem")));11251126panel_container->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));11271128if (content_display_mode == QuickOpenDisplayMode::LIST) {1129display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));1130} else {1131display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));1132}1133} break;1134}1135}11361137void QuickOpenResultContainer::_bind_methods() {1138ADD_SIGNAL(MethodInfo("selection_changed"));1139ADD_SIGNAL(MethodInfo("result_clicked", PropertyInfo(Variant::BOOL, "double_click")));1140}11411142//------------------------- Result Item11431144QuickOpenResultItem::QuickOpenResultItem() {1145set_focus_mode(FocusMode::FOCUS_NONE);1146_set_enabled(false);11471148list_item = memnew(QuickOpenResultListItem);1149list_item->hide();1150add_child(list_item);11511152grid_item = memnew(QuickOpenResultGridItem);1153grid_item->hide();1154add_child(grid_item);1155}11561157void QuickOpenResultItem::set_display_mode(QuickOpenDisplayMode p_display_mode) {1158if (p_display_mode == QuickOpenDisplayMode::LIST) {1159grid_item->hide();1160grid_item->reset();1161list_item->show();1162} else {1163list_item->hide();1164list_item->reset();1165grid_item->show();1166}11671168queue_redraw();1169}11701171void QuickOpenResultItem::set_content(const QuickOpenResultCandidate &p_candidate) {1172_set_enabled(true);11731174if (list_item->is_visible()) {1175list_item->set_content(p_candidate, enable_highlights);1176} else {1177grid_item->set_content(p_candidate, enable_highlights);1178}11791180queue_redraw();1181}11821183void QuickOpenResultItem::reset() {1184_set_enabled(false);1185is_hovering = false;1186is_selected = false;1187list_item->reset();1188grid_item->reset();1189}11901191void QuickOpenResultItem::highlight_item(bool p_enabled) {1192is_selected = p_enabled;11931194if (list_item->is_visible()) {1195if (p_enabled) {1196list_item->highlight_item(highlighted_font_color);1197} else {1198list_item->remove_highlight();1199}1200} else {1201if (p_enabled) {1202grid_item->highlight_item(highlighted_font_color);1203} else {1204grid_item->remove_highlight();1205}1206}12071208queue_redraw();1209}12101211void QuickOpenResultItem::_set_enabled(bool p_enabled) {1212set_visible(p_enabled);1213set_process(p_enabled);1214set_process_input(p_enabled);1215}12161217void QuickOpenResultItem::_notification(int p_what) {1218switch (p_what) {1219case NOTIFICATION_MOUSE_ENTER:1220case NOTIFICATION_MOUSE_EXIT: {1221is_hovering = is_visible() && p_what == NOTIFICATION_MOUSE_ENTER;1222queue_redraw();1223} break;1224case NOTIFICATION_THEME_CHANGED: {1225selected_stylebox = get_theme_stylebox("selected", "Tree");1226hovering_stylebox = get_theme_stylebox(SNAME("hovered"), "Tree");1227highlighted_font_color = get_theme_color("font_focus_color", EditorStringName(Editor));1228} break;1229case NOTIFICATION_DRAW: {1230if (is_selected) {1231draw_style_box(selected_stylebox, Rect2(Point2(), get_size()));1232} else if (is_hovering) {1233draw_style_box(hovering_stylebox, Rect2(Point2(), get_size()));1234}1235} break;1236}1237}12381239//----------------- List item12401241static Vector2i _get_path_interval(const Vector2i &p_interval, int p_dir_index) {1242if (p_interval.x >= p_dir_index || p_interval.y < 1) {1243return { -1, -1 };1244}1245return { p_interval.x, MIN(p_interval.x + p_interval.y, p_dir_index) - p_interval.x };1246}12471248static Vector2i _get_name_interval(const Vector2i &p_interval, int p_dir_index) {1249if (p_interval.x + p_interval.y <= p_dir_index || p_interval.y < 1) {1250return { -1, -1 };1251}1252int first_name_idx = p_dir_index + 1;1253int start = MAX(p_interval.x, first_name_idx);1254return { start - first_name_idx, p_interval.y - start + p_interval.x };1255}12561257QuickOpenResultListItem::QuickOpenResultListItem() {1258set_h_size_flags(Control::SIZE_EXPAND_FILL);1259add_theme_constant_override("margin_left", 6 * EDSCALE);1260add_theme_constant_override("margin_right", 6 * EDSCALE);12611262hbc = memnew(HBoxContainer);1263hbc->add_theme_constant_override(SNAME("separation"), 4 * EDSCALE);1264add_child(hbc);12651266const int max_size = 36 * EDSCALE;12671268thumbnail = memnew(TextureRect);1269thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);1270thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);1271thumbnail->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);1272thumbnail->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);1273thumbnail->set_custom_minimum_size(Size2i(max_size, max_size));1274hbc->add_child(thumbnail);12751276text_container = memnew(VBoxContainer);1277text_container->add_theme_constant_override(SNAME("separation"), -7 * EDSCALE);1278text_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);1279text_container->set_v_size_flags(Control::SIZE_FILL);1280hbc->add_child(text_container);12811282name = memnew(HighlightedLabel);1283name->set_h_size_flags(Control::SIZE_EXPAND_FILL);1284name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);1285name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT);1286text_container->add_child(name);12871288path = memnew(HighlightedLabel);1289path->set_h_size_flags(Control::SIZE_EXPAND_FILL);1290path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);1291path->add_theme_font_size_override(SceneStringName(font_size), 12 * EDSCALE);1292text_container->add_child(path);1293}12941295void QuickOpenResultListItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) {1296thumbnail->set_texture(p_candidate.thumbnail);12971298String file_path = ResourceUID::get_singleton()->get_id_path(p_candidate.uid);1299name->set_text(file_path.get_file());1300path->set_text(file_path.get_base_dir());1301name->reset_highlights();1302path->reset_highlights();13031304if (p_highlight && p_candidate.result != nullptr) {1305for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) {1306for (const Vector2i &interval : match.substrings) {1307path->add_highlight(_get_path_interval(interval, p_candidate.result->dir_index));1308name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index));1309}1310}1311}1312}13131314void QuickOpenResultListItem::reset() {1315thumbnail->set_texture(nullptr);1316name->set_text("");1317path->set_text("");1318name->reset_highlights();1319path->reset_highlights();1320}13211322void QuickOpenResultListItem::highlight_item(const Color &p_color) {1323name->add_theme_color_override(SceneStringName(font_color), p_color);1324}13251326void QuickOpenResultListItem::remove_highlight() {1327name->remove_theme_color_override(SceneStringName(font_color));1328}13291330void QuickOpenResultListItem::_notification(int p_what) {1331switch (p_what) {1332case NOTIFICATION_THEME_CHANGED: {1333path->add_theme_color_override(SceneStringName(font_color), get_theme_color("font_disabled_color", EditorStringName(Editor)));1334} break;1335}1336}13371338//--------------- Grid Item13391340QuickOpenResultGridItem::QuickOpenResultGridItem() {1341set_custom_minimum_size(Size2i(120 * EDSCALE, 0));1342add_theme_constant_override("margin_top", 6 * EDSCALE);1343add_theme_constant_override("margin_left", 2 * EDSCALE);1344add_theme_constant_override("margin_right", 2 * EDSCALE);13451346vbc = memnew(VBoxContainer);1347vbc->set_h_size_flags(Control::SIZE_FILL);1348vbc->set_v_size_flags(Control::SIZE_EXPAND_FILL);1349vbc->add_theme_constant_override(SNAME("separation"), 0);1350add_child(vbc);13511352const int max_size = 64 * EDSCALE;13531354thumbnail = memnew(TextureRect);1355thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);1356thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);1357thumbnail->set_custom_minimum_size(Size2i(max_size, max_size));1358vbc->add_child(thumbnail);13591360name = memnew(HighlightedLabel);1361name->set_h_size_flags(Control::SIZE_EXPAND_FILL);1362name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);1363name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);1364name->add_theme_font_size_override(SceneStringName(font_size), 13 * EDSCALE);1365vbc->add_child(name);1366}13671368void QuickOpenResultGridItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) {1369thumbnail->set_texture(p_candidate.thumbnail);13701371String file_path = ResourceUID::get_singleton()->get_id_path(p_candidate.uid);1372name->set_text(file_path.get_file());1373name->set_tooltip_text(file_path);1374name->reset_highlights();13751376if (p_highlight && p_candidate.result != nullptr) {1377for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) {1378for (const Vector2i &interval : match.substrings) {1379name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index));1380}1381}1382}13831384bool uses_icon = p_candidate.thumbnail->get_width() < (32 * EDSCALE);13851386if (uses_icon || p_candidate.thumbnail->get_height() <= thumbnail->get_custom_minimum_size().y) {1387thumbnail->set_expand_mode(TextureRect::EXPAND_KEEP_SIZE);1388thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);1389} else {1390thumbnail->set_expand_mode(TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL);1391thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_SCALE);1392}1393}13941395void QuickOpenResultGridItem::reset() {1396thumbnail->set_texture(nullptr);1397name->set_text("");1398name->reset_highlights();1399}14001401void QuickOpenResultGridItem::highlight_item(const Color &p_color) {1402name->add_theme_color_override(SceneStringName(font_color), p_color);1403}14041405void QuickOpenResultGridItem::remove_highlight() {1406name->remove_theme_color_override(SceneStringName(font_color));1407}140814091410