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