Path: blob/master/editor/gui/editor_quick_open_dialog.cpp
9896 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/file_system/editor_file_system.h"38#include "editor/file_system/editor_paths.h"39#include "editor/inspector/editor_resource_preview.h"40#include "editor/settings/editor_settings.h"41#include "editor/themes/editor_scale.h"42#include "scene/gui/center_container.h"43#include "scene/gui/check_button.h"44#include "scene/gui/flow_container.h"45#include "scene/gui/line_edit.h"46#include "scene/gui/margin_container.h"47#include "scene/gui/panel_container.h"48#include "scene/gui/separator.h"49#include "scene/gui/texture_rect.h"50#include "scene/gui/tree.h"5152void HighlightedLabel::draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing) {53for (int i = get_lines_skipped(); i < p_line_limit; i++) {54RID line = get_line_rid(i);55Vector<Vector2> ranges = TS->shaped_text_get_selection(line, p_substr.x, p_substr.x + p_substr.y);56Rect2 line_rect = get_line_rect(i);57for (const Vector2 &range : ranges) {58Rect2 rect = Rect2(Point2(range.x, 0) + line_rect.position, Size2(range.y - range.x, line_rect.size.y));59rect.position = p_offset + line_rect.position;60rect.position.x += range.x;61rect.size = Size2(range.y - range.x, line_rect.size.y);62rect.size.x = MIN(rect.size.x, line_rect.size.x - range.x);63if (rect.size.x > 0) {64draw_rect(rect, Color(1, 1, 1, 0.07), true);65draw_rect(rect, Color(0.5, 0.7, 1.0, 0.4), false, 1);66}67}68p_offset.y += line_spacing + TS->shaped_text_get_ascent(line) + TS->shaped_text_get_descent(line);69}70}7172void HighlightedLabel::add_highlight(const Vector2i &p_interval) {73if (p_interval.y > 0) {74highlights.append(p_interval);75queue_redraw();76}77}7879void HighlightedLabel::reset_highlights() {80highlights.clear();81queue_redraw();82}8384void HighlightedLabel::_notification(int p_notification) {85if (p_notification == NOTIFICATION_DRAW) {86if (highlights.is_empty()) {87return;88}8990Vector2 offset;91int line_limit;92int line_spacing;93get_layout_data(offset, line_limit, line_spacing);9495for (const Vector2i &substr : highlights) {96draw_substr_rects(substr, offset, line_limit, line_spacing);97}98}99}100101EditorQuickOpenDialog::EditorQuickOpenDialog() {102VBoxContainer *vbc = memnew(VBoxContainer);103vbc->add_theme_constant_override("separation", 0);104add_child(vbc);105106{107// Search bar108MarginContainer *mc = memnew(MarginContainer);109mc->add_theme_constant_override("margin_top", 6);110mc->add_theme_constant_override("margin_bottom", 6);111mc->add_theme_constant_override("margin_left", 1);112mc->add_theme_constant_override("margin_right", 1);113vbc->add_child(mc);114115search_box = memnew(LineEdit);116search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);117search_box->set_placeholder(TTR("Search files..."));118search_box->set_accessibility_name(TTRC("Search"));119search_box->set_clear_button_enabled(true);120mc->add_child(search_box);121}122123{124container = memnew(QuickOpenResultContainer);125container->connect("result_clicked", callable_mp(this, &EditorQuickOpenDialog::ok_pressed));126vbc->add_child(container);127}128129search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorQuickOpenDialog::_search_box_text_changed));130search_box->connect(SceneStringName(gui_input), callable_mp(container, &QuickOpenResultContainer::handle_search_box_input));131register_text_enter(search_box);132get_ok_button()->hide();133}134135String EditorQuickOpenDialog::get_dialog_title(const Vector<StringName> &p_base_types) {136if (p_base_types.size() > 1) {137return TTR("Select Resource");138}139140if (p_base_types[0] == SNAME("PackedScene")) {141return TTR("Select Scene");142}143144return TTR("Select") + " " + p_base_types[0];145}146147void EditorQuickOpenDialog::popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback) {148ERR_FAIL_COND(p_base_types.is_empty());149ERR_FAIL_COND(!p_item_selected_callback.is_valid());150151item_selected_callback = p_item_selected_callback;152153container->init(p_base_types);154get_ok_button()->set_disabled(container->has_nothing_selected());155156set_title(get_dialog_title(p_base_types));157popup_centered_clamped(Size2(780, 650) * EDSCALE, 0.8f);158search_box->grab_focus();159}160161void EditorQuickOpenDialog::ok_pressed() {162item_selected_callback.call(container->get_selected());163164container->save_selected_item();165container->cleanup();166search_box->clear();167hide();168}169170void EditorQuickOpenDialog::cancel_pressed() {171container->cleanup();172search_box->clear();173}174175void EditorQuickOpenDialog::_search_box_text_changed(const String &p_query) {176container->set_query_and_update(p_query);177get_ok_button()->set_disabled(container->has_nothing_selected());178}179180//------------------------- Result Container181182void style_button(Button *p_button) {183p_button->set_flat(true);184p_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);185p_button->set_default_cursor_shape(Control::CURSOR_POINTING_HAND);186}187188QuickOpenResultContainer::QuickOpenResultContainer() {189set_h_size_flags(Control::SIZE_EXPAND_FILL);190set_v_size_flags(Control::SIZE_EXPAND_FILL);191add_theme_constant_override("separation", 0);192history_file.instantiate();193194{195// Results section196panel_container = memnew(PanelContainer);197panel_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);198add_child(panel_container);199200{201// No search results202no_results_container = memnew(CenterContainer);203no_results_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);204no_results_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);205panel_container->add_child(no_results_container);206207no_results_label = memnew(Label);208no_results_label->set_focus_mode(FOCUS_ACCESSIBILITY);209no_results_label->add_theme_font_size_override(SceneStringName(font_size), 24 * EDSCALE);210no_results_container->add_child(no_results_label);211no_results_container->hide();212}213214{215// Search results216scroll_container = memnew(ScrollContainer);217scroll_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);218scroll_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);219scroll_container->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);220scroll_container->hide();221panel_container->add_child(scroll_container);222223list = memnew(VBoxContainer);224list->set_h_size_flags(Control::SIZE_EXPAND_FILL);225list->add_theme_constant_override(SNAME("separation"), 0);226list->hide();227scroll_container->add_child(list);228229grid = memnew(HFlowContainer);230grid->set_h_size_flags(Control::SIZE_EXPAND_FILL);231grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);232grid->add_theme_constant_override(SNAME("v_separation"), 0);233grid->add_theme_constant_override(SNAME("h_separation"), 0);234grid->hide();235scroll_container->add_child(grid);236237file_context_menu = memnew(PopupMenu);238file_context_menu->add_item(TTR("Show in FileSystem"), FILE_SHOW_IN_FILESYSTEM);239file_context_menu->add_item(TTR("Show in File Manager"), FILE_SHOW_IN_FILE_MANAGER);240file_context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &QuickOpenResultContainer::_menu_option));241file_context_menu->hide();242scroll_container->add_child(file_context_menu);243}244}245246{247// Selected filepath248file_details_path = memnew(Label);249file_details_path->set_focus_mode(FOCUS_ACCESSIBILITY);250file_details_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);251file_details_path->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);252file_details_path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);253add_child(file_details_path);254}255256{257// Bottom bar258HBoxContainer *bottom_bar = memnew(HBoxContainer);259bottom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);260bottom_bar->set_alignment(ALIGNMENT_END);261bottom_bar->add_theme_constant_override("separation", 3);262add_child(bottom_bar);263264fuzzy_search_toggle = memnew(CheckButton);265style_button(fuzzy_search_toggle);266fuzzy_search_toggle->set_text(TTR("Fuzzy Search"));267fuzzy_search_toggle->set_tooltip_text(TTRC("Include approximate matches."));268fuzzy_search_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_fuzzy_search));269bottom_bar->add_child(fuzzy_search_toggle);270271include_addons_toggle = memnew(CheckButton);272style_button(include_addons_toggle);273include_addons_toggle->set_text(TTR("Addons"));274include_addons_toggle->set_tooltip_text(TTR("Include files from addons"));275include_addons_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_include_addons));276bottom_bar->add_child(include_addons_toggle);277278VSeparator *vsep = memnew(VSeparator);279vsep->set_v_size_flags(Control::SIZE_SHRINK_CENTER);280vsep->set_custom_minimum_size(Size2i(0, 14 * EDSCALE));281bottom_bar->add_child(vsep);282283display_mode_toggle = memnew(Button);284display_mode_toggle->set_accessibility_name(TTRC("Display Mode"));285style_button(display_mode_toggle);286display_mode_toggle->connect(SceneStringName(pressed), callable_mp(this, &QuickOpenResultContainer::_toggle_display_mode));287bottom_bar->add_child(display_mode_toggle);288}289}290291void QuickOpenResultContainer::_menu_option(int p_option) {292switch (p_option) {293case FILE_SHOW_IN_FILESYSTEM: {294FileSystemDock::get_singleton()->navigate_to_path(get_selected());295} break;296case FILE_SHOW_IN_FILE_MANAGER: {297String dir = ProjectSettings::get_singleton()->globalize_path(get_selected());298OS::get_singleton()->shell_show_in_file_manager(dir, true);299} break;300}301}302303void QuickOpenResultContainer::_ensure_result_vector_capacity() {304int target_size = EDITOR_GET("filesystem/quick_open_dialog/max_results");305int initial_size = result_items.size();306for (int i = target_size; i < initial_size; i++) {307result_items[i]->queue_free();308}309result_items.resize(target_size);310for (int i = initial_size; i < target_size; i++) {311QuickOpenResultItem *item = memnew(QuickOpenResultItem);312item->connect(SceneStringName(gui_input), callable_mp(this, &QuickOpenResultContainer::_item_input).bind(i));313result_items.write[i] = item;314if (!never_opened) {315_layout_result_item(item);316}317}318}319320void QuickOpenResultContainer::init(const Vector<StringName> &p_base_types) {321_ensure_result_vector_capacity();322base_types = p_base_types;323324const int display_mode_behavior = EDITOR_GET("filesystem/quick_open_dialog/default_display_mode");325const bool adaptive_display_mode = (display_mode_behavior == 0);326const bool first_open = never_opened;327328if (adaptive_display_mode) {329_set_display_mode(get_adaptive_display_mode(p_base_types));330} else if (never_opened) {331int last = EditorSettings::get_singleton()->get_project_metadata("quick_open_dialog", "last_mode", (int)QuickOpenDisplayMode::LIST);332_set_display_mode((QuickOpenDisplayMode)last);333}334335const bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching");336const bool include_addons = EDITOR_GET("filesystem/quick_open_dialog/include_addons");337fuzzy_search_toggle->set_pressed_no_signal(fuzzy_matching);338include_addons_toggle->set_pressed_no_signal(include_addons);339never_opened = false;340341const bool enable_highlights = EDITOR_GET("filesystem/quick_open_dialog/show_search_highlight");342for (QuickOpenResultItem *E : result_items) {343E->enable_highlights = enable_highlights;344}345346if (first_open && history_file->load(_get_cache_file_path()) == OK) {347// Load history when opening for the first time.348file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object")));349350bool history_modified = false;351Vector<String> history_keys = history_file->get_section_keys("selected_history");352for (const String &type : history_keys) {353const StringName type_name = type;354const PackedStringArray paths = history_file->get_value("selected_history", type);355356PackedStringArray cleaned_paths;357cleaned_paths.resize(paths.size());358359Vector<QuickOpenResultCandidate> loaded_candidates;360loaded_candidates.resize(paths.size());361{362QuickOpenResultCandidate *candidates_write = loaded_candidates.ptrw();363String *cleanup_write = cleaned_paths.ptrw();364int i = 0;365for (String path : paths) {366if (path.begins_with("uid://")) {367ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(path);368if (!ResourceUID::get_singleton()->has_id(id)) {369continue;370}371path = ResourceUID::get_singleton()->get_id_path(id);372}373374if (!ResourceLoader::exists(path)) {375continue;376}377378filetypes.insert(path, type_name);379QuickOpenResultCandidate candidate;380_setup_candidate(candidate, path);381candidates_write[i] = candidate;382cleanup_write[i] = path;383i++;384}385loaded_candidates.resize(i);386cleaned_paths.resize(i);387selected_history.insert(type, loaded_candidates);388389if (i < paths.size()) {390// Some paths removed, need to update history.391if (i == 0) {392history_file->erase_section_key("selected_history", type);393} else {394history_file->set_value("selected_history", type, cleaned_paths);395}396history_modified = true;397}398}399}400if (history_modified) {401history_file->save(_get_cache_file_path());402}403}404405_create_initial_results();406}407408void QuickOpenResultContainer::_sort_filepaths(int p_max_results) {409struct FilepathComparator {410bool operator()(const String &p_lhs, const String &p_rhs) const {411// Sort on (length, alphanumeric) to prioritize shorter filepaths412return p_lhs.length() == p_rhs.length() ? p_lhs < p_rhs : p_lhs.length() < p_rhs.length();413}414};415416SortArray<String, FilepathComparator> sorter;417if (filepaths.size() > p_max_results) {418sorter.partial_sort(0, filepaths.size(), p_max_results, filepaths.ptrw());419} else {420sorter.sort(filepaths.ptrw(), filepaths.size());421}422}423424void QuickOpenResultContainer::_create_initial_results() {425file_type_icons.clear();426file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object")));427filepaths.clear();428filetypes.clear();429history_set.clear();430Vector<QuickOpenResultCandidate> *history = _get_history();431if (history) {432for (const QuickOpenResultCandidate &candidate : *history) {433history_set.insert(candidate.file_path);434}435}436_find_filepaths_in_folder(EditorFileSystem::get_singleton()->get_filesystem(), include_addons_toggle->is_pressed());437_sort_filepaths(result_items.size());438max_total_results = MIN(filepaths.size(), result_items.size());439update_results();440}441442void QuickOpenResultContainer::_find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons) {443for (int i = 0; i < p_directory->get_subdir_count(); i++) {444if (p_include_addons || p_directory->get_name() != "addons") {445_find_filepaths_in_folder(p_directory->get_subdir(i), p_include_addons);446}447}448449for (int i = 0; i < p_directory->get_file_count(); i++) {450String file_path = p_directory->get_file_path(i);451452const StringName engine_type = p_directory->get_file_type(i);453const StringName script_type = p_directory->get_file_resource_script_class(i);454455const bool is_engine_type = script_type == StringName();456const StringName &actual_type = is_engine_type ? engine_type : script_type;457458for (const StringName &parent_type : base_types) {459bool 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));460461if (is_valid) {462filepaths.append(file_path);463filetypes.insert(file_path, actual_type);464break; // Stop testing base types as soon as we get a match.465}466}467}468}469470void QuickOpenResultContainer::set_query_and_update(const String &p_query) {471query = p_query;472update_results();473}474475Vector<QuickOpenResultCandidate> *QuickOpenResultContainer::_get_history() {476if (base_types.size() == 1) {477return selected_history.getptr(base_types[0]);478}479return nullptr;480}481482void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &p_candidate, const String &p_filepath) {483p_candidate.file_path = ResourceUID::ensure_path(p_filepath);484p_candidate.result = nullptr;485StringName actual_type;486{487StringName *actual_type_ptr = filetypes.getptr(p_filepath);488if (actual_type_ptr) {489actual_type = *actual_type_ptr;490} else {491ERR_PRINT(vformat("EditorQuickOpenDialog: No type for path %s.", p_candidate.file_path));492}493}494EditorResourcePreview::PreviewItem item = EditorResourcePreview::get_singleton()->get_resource_preview_if_available(p_candidate.file_path);495if (item.preview.is_valid()) {496p_candidate.thumbnail = item.preview;497} else if (file_type_icons.has(actual_type)) {498p_candidate.thumbnail = *file_type_icons.getptr(actual_type);499} else if (has_theme_icon(actual_type, EditorStringName(EditorIcons))) {500p_candidate.thumbnail = get_editor_theme_icon(actual_type);501file_type_icons.insert(actual_type, p_candidate.thumbnail);502} else {503p_candidate.thumbnail = *file_type_icons.getptr(SNAME("__default_icon"));504}505}506507void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result) {508_setup_candidate(p_candidate, p_result.target);509p_candidate.result = &p_result;510}511512void QuickOpenResultContainer::update_results() {513candidates.clear();514if (query.is_empty()) {515_use_default_candidates();516} else {517_score_and_sort_candidates();518}519_update_result_items(MIN(candidates.size(), max_total_results), 0);520}521522void QuickOpenResultContainer::_use_default_candidates() {523Vector<QuickOpenResultCandidate> *history = _get_history();524if (history) {525candidates.append_array(*history);526}527candidates.resize(MIN(max_total_results, filepaths.size()));528int count = candidates.size();529int i = 0;530for (const String &filepath : filepaths) {531if (i >= count) {532break;533}534_setup_candidate(candidates.write[i++], filepath);535}536}537538void QuickOpenResultContainer::_update_fuzzy_search_results() {539FuzzySearch fuzzy_search;540fuzzy_search.start_offset = 6; // Don't match against "res://" at the start of each filepath.541fuzzy_search.set_query(query);542fuzzy_search.max_results = max_total_results;543bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching");544int max_misses = EDITOR_GET("filesystem/quick_open_dialog/max_fuzzy_misses");545fuzzy_search.allow_subsequences = fuzzy_matching;546fuzzy_search.max_misses = fuzzy_matching ? max_misses : 0;547fuzzy_search.search_all(filepaths, search_results);548}549550void QuickOpenResultContainer::_score_and_sort_candidates() {551_update_fuzzy_search_results();552candidates.resize(search_results.size());553QuickOpenResultCandidate *candidates_write = candidates.ptrw();554for (const FuzzySearchResult &result : search_results) {555_setup_candidate(*candidates_write++, result);556}557}558559void QuickOpenResultContainer::_update_result_items(int p_new_visible_results_count, int p_new_selection_index) {560// Only need to update items that were not hidden in previous update.561int num_items_needing_updates = MAX(num_visible_results, p_new_visible_results_count);562num_visible_results = p_new_visible_results_count;563564for (int i = 0; i < num_items_needing_updates; i++) {565QuickOpenResultItem *item = result_items[i];566567if (i < num_visible_results) {568item->set_content(candidates[i]);569} else {570item->reset();571}572};573574const bool any_results = num_visible_results > 0;575_select_item(any_results ? p_new_selection_index : -1);576577scroll_container->set_visible(any_results);578no_results_container->set_visible(!any_results);579580if (!any_results) {581if (filepaths.is_empty()) {582no_results_label->set_text(TTR("No files found for this type"));583} else {584no_results_label->set_text(TTR("No results found"));585}586}587}588589void QuickOpenResultContainer::handle_search_box_input(const Ref<InputEvent> &p_ie) {590if (num_visible_results < 0) {591return;592}593594Ref<InputEventKey> key_event = p_ie;595if (key_event.is_valid() && key_event->is_pressed()) {596bool move_selection = false;597598switch (key_event->get_keycode()) {599case Key::UP:600case Key::DOWN:601case Key::PAGEUP:602case Key::PAGEDOWN: {603move_selection = true;604} break;605case Key::LEFT:606case Key::RIGHT: {607if (content_display_mode == QuickOpenDisplayMode::GRID) {608// Maybe strip off the shift modifier to allow non-selecting navigation by character?609if (key_event->get_modifiers_mask().is_empty()) {610move_selection = true;611}612}613} break;614default:615break; // Let the event through so it will reach the search box.616}617618if (move_selection) {619_move_selection_index(key_event->get_keycode());620queue_redraw();621accept_event();622}623}624}625626void QuickOpenResultContainer::_move_selection_index(Key p_key) {627// Don't move selection if there are no results.628if (num_visible_results <= 0) {629return;630}631const int max_index = num_visible_results - 1;632633int idx = selection_index;634if (content_display_mode == QuickOpenDisplayMode::LIST) {635if (p_key == Key::UP) {636idx = (idx == 0) ? max_index : (idx - 1);637} else if (p_key == Key::DOWN) {638idx = (idx == max_index) ? 0 : (idx + 1);639} else if (p_key == Key::PAGEUP) {640idx = (idx == 0) ? idx : MAX(idx - 10, 0);641} else if (p_key == Key::PAGEDOWN) {642idx = (idx == max_index) ? idx : MIN(idx + 10, max_index);643}644} else {645int column_count = grid->get_line_max_child_count();646647if (p_key == Key::LEFT) {648idx = (idx == 0) ? max_index : (idx - 1);649} else if (p_key == Key::RIGHT) {650idx = (idx == max_index) ? 0 : (idx + 1);651} else if (p_key == Key::UP) {652idx = (idx == 0) ? max_index : MAX(idx - column_count, 0);653} else if (p_key == Key::DOWN) {654idx = (idx == max_index) ? 0 : MIN(idx + column_count, max_index);655} else if (p_key == Key::PAGEUP) {656idx = (idx == 0) ? idx : MAX(idx - (3 * column_count), 0);657} else if (p_key == Key::PAGEDOWN) {658idx = (idx == max_index) ? idx : MIN(idx + (3 * column_count), max_index);659}660}661662_select_item(idx);663}664665void QuickOpenResultContainer::_select_item(int p_index) {666if (!has_nothing_selected()) {667result_items[selection_index]->highlight_item(false);668}669670selection_index = p_index;671672if (has_nothing_selected()) {673file_details_path->set_text("");674return;675}676677result_items[selection_index]->highlight_item(true);678bool in_history = history_set.has(candidates[selection_index].file_path);679file_details_path->set_text(get_selected() + (in_history ? TTR(" (recently opened)") : ""));680681const QuickOpenResultItem *item = result_items[selection_index];682683// Copied from Tree.684const int selected_position = item->get_position().y;685const int selected_size = item->get_size().y;686const int scroll_window_size = scroll_container->get_size().y;687const int scroll_position = scroll_container->get_v_scroll();688689if (selected_position <= scroll_position) {690scroll_container->set_v_scroll(selected_position);691} else if (selected_position + selected_size > scroll_position + scroll_window_size) {692scroll_container->set_v_scroll(selected_position + selected_size - scroll_window_size);693}694}695696void QuickOpenResultContainer::_item_input(const Ref<InputEvent> &p_ev, int p_index) {697Ref<InputEventMouseButton> mb = p_ev;698699if (mb.is_valid() && mb->is_pressed()) {700if (mb->get_button_index() == MouseButton::LEFT) {701_select_item(p_index);702emit_signal(SNAME("result_clicked"));703} else if (mb->get_button_index() == MouseButton::RIGHT) {704_select_item(p_index);705file_context_menu->set_position(result_items[p_index]->get_screen_position() + mb->get_position());706file_context_menu->reset_size();707file_context_menu->popup();708}709}710}711712void QuickOpenResultContainer::_toggle_fuzzy_search(bool p_pressed) {713EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/enable_fuzzy_matching", p_pressed);714update_results();715}716717String QuickOpenResultContainer::_get_cache_file_path() const {718return EditorPaths::get_singleton()->get_project_settings_dir().path_join("quick_open_dialog_cache.cfg");719}720721void QuickOpenResultContainer::_toggle_include_addons(bool p_pressed) {722EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/include_addons", p_pressed);723cleanup();724_create_initial_results();725}726727void QuickOpenResultContainer::_toggle_display_mode() {728QuickOpenDisplayMode new_display_mode = (content_display_mode == QuickOpenDisplayMode::LIST) ? QuickOpenDisplayMode::GRID : QuickOpenDisplayMode::LIST;729_set_display_mode(new_display_mode);730}731732CanvasItem *QuickOpenResultContainer::_get_result_root() {733if (content_display_mode == QuickOpenDisplayMode::LIST) {734return list;735} else {736return grid;737}738}739740void QuickOpenResultContainer::_layout_result_item(QuickOpenResultItem *item) {741item->set_display_mode(content_display_mode);742Node *parent = item->get_parent();743if (parent) {744parent->remove_child(item);745}746_get_result_root()->add_child(item);747}748749void QuickOpenResultContainer::_set_display_mode(QuickOpenDisplayMode p_display_mode) {750CanvasItem *prev_root = _get_result_root();751752if (prev_root->is_visible() && content_display_mode == p_display_mode) {753return;754}755756content_display_mode = p_display_mode;757CanvasItem *next_root = _get_result_root();758759EditorSettings::get_singleton()->set_project_metadata("quick_open_dialog", "last_mode", (int)content_display_mode);760761prev_root->hide();762next_root->show();763764for (QuickOpenResultItem *item : result_items) {765_layout_result_item(item);766}767768_update_result_items(num_visible_results, selection_index);769770if (content_display_mode == QuickOpenDisplayMode::LIST) {771display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));772display_mode_toggle->set_tooltip_text(TTR("Grid view"));773} else {774display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));775display_mode_toggle->set_tooltip_text(TTR("List view"));776}777}778779bool QuickOpenResultContainer::has_nothing_selected() const {780return selection_index < 0;781}782783String QuickOpenResultContainer::get_selected() const {784ERR_FAIL_COND_V_MSG(has_nothing_selected(), String(), "Tried to get selected file, but nothing was selected.");785return candidates[selection_index].file_path;786}787788QuickOpenDisplayMode QuickOpenResultContainer::get_adaptive_display_mode(const Vector<StringName> &p_base_types) {789static const Vector<StringName> grid_preferred_types = {790StringName("Font", true),791StringName("Texture2D", true),792StringName("Material", true),793StringName("Mesh", true),794};795796for (const StringName &type : grid_preferred_types) {797for (const StringName &base_type : p_base_types) {798if (base_type == type || ClassDB::is_parent_class(base_type, type)) {799return QuickOpenDisplayMode::GRID;800}801}802}803804return QuickOpenDisplayMode::LIST;805}806807String _get_uid_string(const String &p_filepath) {808ResourceUID::ID id = EditorFileSystem::get_singleton()->get_file_uid(p_filepath);809return id == ResourceUID::INVALID_ID ? p_filepath : ResourceUID::get_singleton()->id_to_text(id);810}811812void QuickOpenResultContainer::save_selected_item() {813if (base_types.size() > 1) {814// Getting the type of the file and checking which base type it belongs to should be possible.815// However, for now these are not supported, and we don't record this.816return;817}818819const StringName &base_type = base_types[0];820QuickOpenResultCandidate &selected = candidates.write[selection_index];821Vector<QuickOpenResultCandidate> *type_history = selected_history.getptr(base_type);822823if (!type_history) {824selected_history.insert(base_type, Vector<QuickOpenResultCandidate>());825type_history = selected_history.getptr(base_type);826} else {827for (int i = 0; i < type_history->size(); i++) {828if (selected.file_path == type_history->get(i).file_path) {829type_history->remove_at(i);830break;831}832}833}834835selected.result = nullptr;836history_set.insert(selected.file_path);837type_history->insert(0, selected);838if (type_history->size() > MAX_HISTORY_SIZE) {839type_history->resize(MAX_HISTORY_SIZE);840}841842PackedStringArray paths;843paths.resize(type_history->size());844{845String *paths_write = paths.ptrw();846847int i = 0;848for (const QuickOpenResultCandidate &candidate : *type_history) {849paths_write[i] = _get_uid_string(candidate.file_path);850i++;851}852}853history_file->set_value("selected_history", base_type, paths);854history_file->save(_get_cache_file_path());855}856857void QuickOpenResultContainer::cleanup() {858num_visible_results = 0;859candidates.clear();860history_set.clear();861_select_item(-1);862863for (QuickOpenResultItem *item : result_items) {864item->reset();865}866}867868void QuickOpenResultContainer::_notification(int p_what) {869switch (p_what) {870case NOTIFICATION_THEME_CHANGED: {871Color text_color = get_theme_color("font_readonly_color", EditorStringName(Editor));872file_details_path->add_theme_color_override(SceneStringName(font_color), text_color);873no_results_label->add_theme_color_override(SceneStringName(font_color), text_color);874875panel_container->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));876877if (content_display_mode == QuickOpenDisplayMode::LIST) {878display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));879} else {880display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));881}882} break;883}884}885886void QuickOpenResultContainer::_bind_methods() {887ADD_SIGNAL(MethodInfo("result_clicked"));888}889890//------------------------- Result Item891892QuickOpenResultItem::QuickOpenResultItem() {893set_focus_mode(FocusMode::FOCUS_ALL);894_set_enabled(false);895set_default_cursor_shape(CURSOR_POINTING_HAND);896897list_item = memnew(QuickOpenResultListItem);898list_item->hide();899add_child(list_item);900901grid_item = memnew(QuickOpenResultGridItem);902grid_item->hide();903add_child(grid_item);904}905906void QuickOpenResultItem::set_display_mode(QuickOpenDisplayMode p_display_mode) {907if (p_display_mode == QuickOpenDisplayMode::LIST) {908grid_item->hide();909grid_item->reset();910list_item->show();911} else {912list_item->hide();913list_item->reset();914grid_item->show();915}916917queue_redraw();918}919920void QuickOpenResultItem::set_content(const QuickOpenResultCandidate &p_candidate) {921_set_enabled(true);922923if (list_item->is_visible()) {924list_item->set_content(p_candidate, enable_highlights);925} else {926grid_item->set_content(p_candidate, enable_highlights);927}928929queue_redraw();930}931932void QuickOpenResultItem::reset() {933_set_enabled(false);934is_hovering = false;935is_selected = false;936list_item->reset();937grid_item->reset();938}939940void QuickOpenResultItem::highlight_item(bool p_enabled) {941is_selected = p_enabled;942943if (list_item->is_visible()) {944if (p_enabled) {945list_item->highlight_item(highlighted_font_color);946} else {947list_item->remove_highlight();948}949} else {950if (p_enabled) {951grid_item->highlight_item(highlighted_font_color);952} else {953grid_item->remove_highlight();954}955}956957queue_redraw();958}959960void QuickOpenResultItem::_set_enabled(bool p_enabled) {961set_visible(p_enabled);962set_process(p_enabled);963set_process_input(p_enabled);964}965966void QuickOpenResultItem::_notification(int p_what) {967switch (p_what) {968case NOTIFICATION_MOUSE_ENTER:969case NOTIFICATION_MOUSE_EXIT: {970is_hovering = is_visible() && p_what == NOTIFICATION_MOUSE_ENTER;971queue_redraw();972} break;973case NOTIFICATION_THEME_CHANGED: {974selected_stylebox = get_theme_stylebox("selected", "Tree");975hovering_stylebox = get_theme_stylebox(SNAME("hovered"), "Tree");976highlighted_font_color = get_theme_color("font_focus_color", EditorStringName(Editor));977} break;978case NOTIFICATION_DRAW: {979if (is_selected) {980draw_style_box(selected_stylebox, Rect2(Point2(), get_size()));981} else if (is_hovering) {982draw_style_box(hovering_stylebox, Rect2(Point2(), get_size()));983}984} break;985}986}987988//----------------- List item989990static Vector2i _get_path_interval(const Vector2i &p_interval, int p_dir_index) {991if (p_interval.x >= p_dir_index || p_interval.y < 1) {992return { -1, -1 };993}994return { p_interval.x, MIN(p_interval.x + p_interval.y, p_dir_index) - p_interval.x };995}996997static Vector2i _get_name_interval(const Vector2i &p_interval, int p_dir_index) {998if (p_interval.x + p_interval.y <= p_dir_index || p_interval.y < 1) {999return { -1, -1 };1000}1001int first_name_idx = p_dir_index + 1;1002int start = MAX(p_interval.x, first_name_idx);1003return { start - first_name_idx, p_interval.y - start + p_interval.x };1004}10051006QuickOpenResultListItem::QuickOpenResultListItem() {1007set_h_size_flags(Control::SIZE_EXPAND_FILL);1008add_theme_constant_override("margin_left", 6 * EDSCALE);1009add_theme_constant_override("margin_right", 6 * EDSCALE);10101011hbc = memnew(HBoxContainer);1012hbc->add_theme_constant_override(SNAME("separation"), 4 * EDSCALE);1013add_child(hbc);10141015const int max_size = 36 * EDSCALE;10161017thumbnail = memnew(TextureRect);1018thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);1019thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);1020thumbnail->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);1021thumbnail->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);1022thumbnail->set_custom_minimum_size(Size2i(max_size, max_size));1023hbc->add_child(thumbnail);10241025text_container = memnew(VBoxContainer);1026text_container->add_theme_constant_override(SNAME("separation"), -7 * EDSCALE);1027text_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);1028text_container->set_v_size_flags(Control::SIZE_FILL);1029hbc->add_child(text_container);10301031name = memnew(HighlightedLabel);1032name->set_h_size_flags(Control::SIZE_EXPAND_FILL);1033name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);1034name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT);1035text_container->add_child(name);10361037path = memnew(HighlightedLabel);1038path->set_h_size_flags(Control::SIZE_EXPAND_FILL);1039path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);1040path->add_theme_font_size_override(SceneStringName(font_size), 12 * EDSCALE);1041text_container->add_child(path);1042}10431044void QuickOpenResultListItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) {1045thumbnail->set_texture(p_candidate.thumbnail);1046name->set_text(p_candidate.file_path.get_file());1047path->set_text(p_candidate.file_path.get_base_dir());1048name->reset_highlights();1049path->reset_highlights();10501051if (p_highlight && p_candidate.result != nullptr) {1052for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) {1053for (const Vector2i &interval : match.substrings) {1054path->add_highlight(_get_path_interval(interval, p_candidate.result->dir_index));1055name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index));1056}1057}1058}1059}10601061void QuickOpenResultListItem::reset() {1062thumbnail->set_texture(nullptr);1063name->set_text("");1064path->set_text("");1065name->reset_highlights();1066path->reset_highlights();1067}10681069void QuickOpenResultListItem::highlight_item(const Color &p_color) {1070name->add_theme_color_override(SceneStringName(font_color), p_color);1071}10721073void QuickOpenResultListItem::remove_highlight() {1074name->remove_theme_color_override(SceneStringName(font_color));1075}10761077void QuickOpenResultListItem::_notification(int p_what) {1078switch (p_what) {1079case NOTIFICATION_THEME_CHANGED: {1080path->add_theme_color_override(SceneStringName(font_color), get_theme_color("font_disabled_color", EditorStringName(Editor)));1081} break;1082}1083}10841085//--------------- Grid Item10861087QuickOpenResultGridItem::QuickOpenResultGridItem() {1088set_custom_minimum_size(Size2i(120 * EDSCALE, 0));1089add_theme_constant_override("margin_top", 6 * EDSCALE);1090add_theme_constant_override("margin_left", 2 * EDSCALE);1091add_theme_constant_override("margin_right", 2 * EDSCALE);10921093vbc = memnew(VBoxContainer);1094vbc->set_h_size_flags(Control::SIZE_FILL);1095vbc->set_v_size_flags(Control::SIZE_EXPAND_FILL);1096vbc->add_theme_constant_override(SNAME("separation"), 0);1097add_child(vbc);10981099const int max_size = 64 * EDSCALE;11001101thumbnail = memnew(TextureRect);1102thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);1103thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);1104thumbnail->set_custom_minimum_size(Size2i(max_size, max_size));1105vbc->add_child(thumbnail);11061107name = memnew(HighlightedLabel);1108name->set_h_size_flags(Control::SIZE_EXPAND_FILL);1109name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);1110name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);1111name->add_theme_font_size_override(SceneStringName(font_size), 13 * EDSCALE);1112vbc->add_child(name);1113}11141115void QuickOpenResultGridItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) {1116thumbnail->set_texture(p_candidate.thumbnail);1117name->set_text(p_candidate.file_path.get_file());1118name->set_tooltip_text(p_candidate.file_path);1119name->reset_highlights();11201121if (p_highlight && p_candidate.result != nullptr) {1122for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) {1123for (const Vector2i &interval : match.substrings) {1124name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index));1125}1126}1127}11281129bool uses_icon = p_candidate.thumbnail->get_width() < (32 * EDSCALE);11301131if (uses_icon || p_candidate.thumbnail->get_height() <= thumbnail->get_custom_minimum_size().y) {1132thumbnail->set_expand_mode(TextureRect::EXPAND_KEEP_SIZE);1133thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);1134} else {1135thumbnail->set_expand_mode(TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL);1136thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_SCALE);1137}1138}11391140void QuickOpenResultGridItem::reset() {1141thumbnail->set_texture(nullptr);1142name->set_text("");1143name->reset_highlights();1144}11451146void QuickOpenResultGridItem::highlight_item(const Color &p_color) {1147name->add_theme_color_override(SceneStringName(font_color), p_color);1148}11491150void QuickOpenResultGridItem::remove_highlight() {1151name->remove_theme_color_override(SceneStringName(font_color));1152}115311541155