Debian Codegraph @

editor/docks/filesystem_dock.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* filesystem_dock.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 "filesystem_dock.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/io/dir_access.h"
35 #include "core/io/file_access.h"
36 #include "core/io/resource_loader.h"
37 #include "core/os/keyboard.h"
38 #include "core/os/os.h"
39 #include "core/templates/list.h"
40 #include "editor/docks/editor_dock_manager.h"
41 #include "editor/docks/import_dock.h"
42 #include "editor/docks/scene_tree_dock.h"
43 #include "editor/editor_node.h"
44 #include "editor/editor_string_names.h"
45 #include "editor/editor_undo_redo_manager.h"
46 #include "editor/file_system/dependency_editor.h"
47 #include "editor/gui/create_dialog.h"
48 #include "editor/gui/directory_create_dialog.h"
49 #include "editor/gui/editor_dir_dialog.h"
50 #include "editor/import/3d/scene_import_settings.h"
51 #include "editor/inspector/editor_context_menu_plugin.h"
52 #include "editor/inspector/editor_resource_preview.h"
53 #include "editor/inspector/editor_resource_tooltip_plugins.h"
54 #include "editor/plugins/editor_resource_conversion_plugin.h"
55 #include "editor/scene/editor_scene_tabs.h"
56 #include "editor/scene/scene_create_dialog.h"
57 #include "editor/settings/editor_command_palette.h"
58 #include "editor/settings/editor_feature_profile.h"
59 #include "editor/settings/editor_settings.h"
60 #include "editor/shader/shader_create_dialog.h"
61 #include "editor/themes/editor_scale.h"
62 #include "editor/themes/editor_theme_manager.h"
63 #include "scene/gui/box_container.h"
64 #include "scene/gui/item_list.h"
65 #include "scene/gui/label.h"
66 #include "scene/gui/line_edit.h"
67 #include "scene/gui/progress_bar.h"
68 #include "scene/resources/packed_scene.h"
69 #include "servers/display/display_server.h"
70
71 Control *FileSystemTree::make_custom_tooltip(const String &p_text) const {
72 TreeItem *item = get_item_at_position(get_local_mouse_position());
73 if (!item) {
74 return nullptr;
75 }
76 return FileSystemDock::get_singleton()->create_tooltip_for_path(item->get_metadata(0));
77 }
78
79 Control *FileSystemList::make_custom_tooltip(const String &p_text) const {
80 int idx = get_item_at_position(get_local_mouse_position(), true);
81 if (idx == -1) {
82 return nullptr;
83 }
84 return FileSystemDock::get_singleton()->create_tooltip_for_path(get_item_metadata(idx));
85 }
86
87 void FileSystemList::_line_editor_submit(const String &p_text) {
88 if (popup_edit_committed) {
89 return; // Already processed by _text_editor_popup_modal_close
90 }
91
92 if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
93 return; // ESC pressed, app focus lost, or forced close from code.
94 }
95
96 popup_edit_committed = true; // End edit popup processing.
97 popup_editor->hide();
98
99 emit_signal(SNAME("item_edited"));
100 queue_redraw();
101 }
102
103 bool FileSystemList::edit_selected() {
104 ERR_FAIL_COND_V_MSG(!is_anything_selected(), false, "No item selected.");
105 int s = get_current();
106 ERR_FAIL_COND_V_MSG(s < 0, false, "No current item selected.");
107 ensure_current_is_visible();
108
109 Rect2 rect;
110 Rect2 popup_rect;
111 Vector2 ofs;
112
113 Vector2 icon_size = get_fixed_icon_size() * get_icon_scale();
114
115 // Handles the different icon modes (TOP/LEFT).
116 switch (get_icon_mode()) {
117 case ItemList::ICON_MODE_LEFT:
118 rect = get_item_rect(s, true);
119 if (get_v_scroll_bar()->is_visible()) {
120 rect.position.y -= get_v_scroll_bar()->get_value();
121 }
122 if (get_h_scroll_bar()->is_visible()) {
123 rect.position.x -= get_h_scroll_bar()->get_value();
124 }
125 ofs = Vector2(0, Math::floor((MAX(line_editor->get_minimum_size().height, rect.size.height) - rect.size.height) / 2));
126 popup_rect.position = rect.position - ofs;
127 popup_rect.size = rect.size;
128
129 // Adjust for icon position and size.
130 popup_rect.size.x -= MAX(theme_cache.h_separation, 0) / 2 + icon_size.x;
131 popup_rect.position.x += MAX(theme_cache.h_separation, 0) / 2 + icon_size.x;
132 break;
133 case ItemList::ICON_MODE_TOP:
134 rect = get_item_rect(s, false);
135 if (get_v_scroll_bar()->is_visible()) {
136 rect.position.y -= get_v_scroll_bar()->get_value();
137 }
138 if (get_h_scroll_bar()->is_visible()) {
139 rect.position.x -= get_h_scroll_bar()->get_value();
140 }
141 popup_rect.position = rect.position;
142 popup_rect.size = rect.size;
143
144 // Adjust for icon position and size.
145 popup_rect.size.y -= MAX(theme_cache.v_separation, 0) / 2 + theme_cache.icon_margin + icon_size.y;
146 popup_rect.position.y += MAX(theme_cache.v_separation, 0) / 2 + theme_cache.icon_margin + icon_size.y;
147 break;
148 }
149 if (is_layout_rtl()) {
150 popup_rect.position.x = get_size().width - popup_rect.position.x - popup_rect.size.x;
151 }
152 popup_rect.position += get_screen_position();
153
154 popup_editor->set_position(popup_rect.position);
155 popup_editor->set_size(popup_rect.size);
156
157 String name = get_item_text(s);
158 line_editor->set_text(name);
159 line_editor->select(0, name.rfind_char('.'));
160
161 popup_edit_committed = false; // Start edit popup processing.
162 popup_editor->popup();
163 popup_editor->child_controls_changed();
164 line_editor->grab_focus();
165 return true;
166 }
167
168 String FileSystemList::get_edit_text() {
169 return line_editor->get_text();
170 }
171
172 void FileSystemList::_text_editor_popup_modal_close() {
173 if (popup_edit_committed) {
174 return; // Already processed by _text_editor_popup_modal_close
175 }
176
177 if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
178 return; // ESC pressed, app focus lost, or forced close from code.
179 }
180
181 _line_editor_submit(line_editor->get_text());
182 }
183
184 void FileSystemList::_bind_methods() {
185 ADD_SIGNAL(MethodInfo("item_edited"));
186 }
187
188 FileSystemList::FileSystemList() {
189 set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
190
191 popup_editor = memnew(Popup);
192 add_child(popup_editor);
193
194 popup_editor_vb = memnew(VBoxContainer);
195 popup_editor_vb->add_theme_constant_override("separation", 0);
196 popup_editor_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
197 popup_editor->add_child(popup_editor_vb);
198
199 line_editor = memnew(LineEdit);
200 line_editor->set_v_size_flags(SIZE_EXPAND_FILL);
201 popup_editor_vb->add_child(line_editor);
202 line_editor->connect(SceneStringName(text_submitted), callable_mp(this, &FileSystemList::_line_editor_submit));
203 popup_editor->connect("popup_hide", callable_mp(this, &FileSystemList::_text_editor_popup_modal_close));
204 }
205
206 Ref<Texture2D> FileSystemDock::_get_tree_item_icon(bool p_is_valid, const String &p_file_type, const String &p_icon_path) {
207 if (!p_icon_path.is_empty()) {
208 Ref<Texture2D> icon = ResourceLoader::load(p_icon_path);
209 if (icon.is_valid()) {
210 return icon;
211 }
212 }
213
214 if (!p_is_valid) {
215 return get_editor_theme_icon(SNAME("ImportFail"));
216 } else if (has_theme_icon(p_file_type, EditorStringName(EditorIcons))) {
217 return get_editor_theme_icon(p_file_type);
218 } else {
219 return get_editor_theme_icon(SNAME("File"));
220 }
221 }
222
223 void FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory *p_dir, Vector<String> &uncollapsed_paths, bool p_select_in_favorites, bool p_unfold_path) {
224 // Create a tree item for the subdirectory.
225 TreeItem *subdirectory_item = tree->create_item(p_parent);
226 String dname = p_dir->get_name();
227 String lpath = p_dir->get_path();
228
229 if (dname.is_empty()) {
230 dname = "res://";
231 resources_item = subdirectory_item;
232 }
233
234 // Set custom folder color (if applicable).
235 bool has_custom_color = assigned_folder_colors.has(lpath);
236 Color custom_color = has_custom_color ? folder_colors[assigned_folder_colors[lpath]] : Color();
237 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
238
239 if (has_custom_color) {
240 subdirectory_item->set_icon_modulate(0, editor_is_dark_icon_and_font ? custom_color : custom_color * ITEM_COLOR_SCALE);
241 subdirectory_item->set_custom_bg_color(0, Color(custom_color, editor_is_dark_icon_and_font ? ITEM_ALPHA_MIN : ITEM_ALPHA_MAX));
242 } else {
243 TreeItem *parent = subdirectory_item->get_parent();
244 if (parent) {
245 Color parent_bg_color = parent->get_custom_bg_color(0);
246 if (parent_bg_color != Color()) {
247 bool parent_has_custom_color = assigned_folder_colors.has(parent->get_metadata(0));
248 subdirectory_item->set_custom_bg_color(0, parent_has_custom_color ? parent_bg_color.darkened(ITEM_BG_DARK_SCALE) : parent_bg_color);
249 subdirectory_item->set_icon_modulate(0, parent->get_icon_modulate(0));
250 } else {
251 subdirectory_item->set_icon_modulate(0, get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog")));
252 }
253 }
254 }
255
256 subdirectory_item->set_text(0, dname);
257 subdirectory_item->set_structured_text_bidi_override(0, TextServer::STRUCTURED_TEXT_FILE);
258 subdirectory_item->set_icon(0, get_editor_theme_icon(SNAME("Folder")));
259 if (da->is_link(lpath)) {
260 subdirectory_item->set_icon_overlay(0, get_editor_theme_icon(SNAME("LinkOverlay")));
261 subdirectory_item->set_tooltip_text(0, vformat(TTR("Link to: %s"), da->read_link(lpath)));
262 }
263 subdirectory_item->set_selectable(0, true);
264 subdirectory_item->set_metadata(0, lpath);
265 folder_map[lpath] = subdirectory_item;
266
267 if (!p_select_in_favorites && (current_path == lpath || ((display_mode != DISPLAY_MODE_TREE_ONLY) && current_path.get_base_dir() == lpath))) {
268 subdirectory_item->select(0);
269 // Keep select an item when re-created a tree
270 // To prevent crashing when nothing is selected.
271 subdirectory_item->set_as_cursor(0);
272 }
273
274 if (p_unfold_path && current_path.begins_with(lpath) && current_path != lpath) {
275 subdirectory_item->set_collapsed(false);
276 } else {
277 subdirectory_item->set_collapsed(!uncollapsed_paths.has(lpath));
278 }
279
280 // Create items for all subdirectories.
281 bool reversed = file_sort == FileSortOption::FILE_SORT_NAME_REVERSE;
282 for (int i = reversed ? p_dir->get_subdir_count() - 1 : 0;
283 reversed ? i >= 0 : i < p_dir->get_subdir_count();
284 reversed ? i-- : i++) {
285 _create_tree(subdirectory_item, p_dir->get_subdir(i), uncollapsed_paths, p_select_in_favorites, p_unfold_path);
286 }
287
288 // Create all items for the files in the subdirectory.
289 if (display_mode == DISPLAY_MODE_TREE_ONLY) {
290 // Build the list of the files to display.
291 List<FileInfo> file_list;
292 for (int i = 0; i < p_dir->get_file_count(); i++) {
293 String file_type = p_dir->get_file_type(i);
294 if (_is_file_type_disabled_by_feature_profile(file_type)) {
295 // If type is disabled, file won't be displayed.
296 continue;
297 }
298
299 FileInfo file_info;
300 file_info.name = p_dir->get_file(i);
301 file_info.type = p_dir->get_file_type(i);
302 file_info.icon_path = p_dir->get_file_icon_path(i);
303 file_info.import_broken = !p_dir->get_file_import_is_valid(i);
304 file_info.modified_time = p_dir->get_file_modified_time(i);
305
306 file_list.push_back(file_info);
307 }
308
309 // Sort the file list if needed.
310 sort_file_info_list(file_list, file_sort);
311
312 // Build the tree.
313 const int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
314
315 for (const FileInfo &file_info : file_list) {
316 TreeItem *file_item = tree->create_item(subdirectory_item);
317 const String file_metadata = lpath.path_join(file_info.name);
318 file_item->set_text(0, file_info.name);
319 file_item->set_structured_text_bidi_override(0, TextServer::STRUCTURED_TEXT_FILE);
320 file_item->set_icon(0, _get_tree_item_icon(!file_info.import_broken, file_info.type, file_info.icon_path));
321 if (da->is_link(file_metadata)) {
322 file_item->set_icon_overlay(0, get_editor_theme_icon(SNAME("LinkOverlay")));
323 // TRANSLATORS: This is a tooltip for a file that is a symbolic link to another file.
324 file_item->set_tooltip_text(0, vformat(TTR("Link to: %s"), da->read_link(file_metadata)));
325 }
326 file_item->set_icon_max_width(0, icon_size);
327 Color parent_bg_color = subdirectory_item->get_custom_bg_color(0);
328 if (has_custom_color) {
329 file_item->set_custom_bg_color(0, parent_bg_color.darkened(ITEM_BG_DARK_SCALE));
330 } else if (parent_bg_color != Color()) {
331 file_item->set_custom_bg_color(0, parent_bg_color);
332 }
333 file_item->set_metadata(0, file_metadata);
334 if (!p_select_in_favorites && current_path == file_metadata) {
335 file_item->select(0);
336 file_item->set_as_cursor(0);
337 }
338 if (main_scene_path == file_metadata) {
339 file_item->set_custom_color(0, get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
340 }
341 EditorResourcePreview::get_singleton()->queue_resource_preview(file_metadata, callable_mp(this, &FileSystemDock::_tree_thumbnail_done).bind(tree_update_id, file_item->get_instance_id()));
342 }
343 } else {
344 if (lpath.get_base_dir() == current_path.get_base_dir()) {
345 subdirectory_item->select(0);
346 subdirectory_item->set_as_cursor(0);
347 }
348 }
349 }
350
351 Vector<String> FileSystemDock::get_uncollapsed_paths() const {
352 Vector<String> uncollapsed_paths;
353 TreeItem *root = tree->get_root();
354 if (root) {
355 if (!favorites_item->is_collapsed()) {
356 uncollapsed_paths.push_back(favorites_item->get_metadata(0));
357 }
358
359 // BFS to find all uncollapsed paths of the resource directory.
360 TreeItem *res_subtree = root->get_first_child()->get_next();
361 if (res_subtree) {
362 List<TreeItem *> queue;
363 queue.push_back(res_subtree);
364
365 while (!queue.is_empty()) {
366 TreeItem *ti = queue.back()->get();
367 queue.pop_back();
368 if (!ti->is_collapsed() && ti->get_child_count() > 0) {
369 Variant path = ti->get_metadata(0);
370 if (path) {
371 uncollapsed_paths.push_back(path);
372 }
373 }
374 for (int i = 0; i < ti->get_child_count(); i++) {
375 queue.push_back(ti->get_child(i));
376 }
377 }
378 }
379 }
380 return uncollapsed_paths;
381 }
382
383 void FileSystemDock::_update_tree(const Vector<String> &p_uncollapsed_paths, bool p_uncollapse_root, bool p_scroll_to_selected) {
384 // Recreate the tree.
385 tree->clear();
386 tree_update_id++;
387 updating_tree = true;
388 TreeItem *root = tree->create_item();
389 folder_map.clear();
390
391 // Handles the favorites.
392 favorites_item = tree->create_item(root);
393 favorites_item->set_icon(0, get_editor_theme_icon(SNAME("Favorites")));
394 favorites_item->set_text(0, TTRC("Favorites:"));
395 favorites_item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_ALWAYS);
396 favorites_item->set_metadata(0, "Favorites");
397 favorites_item->set_collapsed(!p_uncollapsed_paths.has("Favorites"));
398
399 Vector<String> favorite_paths = EditorSettings::get_singleton()->get_favorites();
400
401 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
402 bool fav_changed = false;
403 for (int i = favorite_paths.size() - 1; i >= 0; i--) {
404 if (da->dir_exists(favorite_paths[i]) || da->file_exists(favorite_paths[i])) {
405 continue;
406 }
407 favorite_paths.remove_at(i);
408 fav_changed = true;
409 }
410 if (fav_changed) {
411 EditorSettings::get_singleton()->set_favorites(favorite_paths);
412 // Setting favorites causes the tree to update, so continuing is redundant.
413 return;
414 }
415
416 Ref<Texture2D> folder_icon = get_editor_theme_icon(SNAME("Folder"));
417 const Color default_folder_color = get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog"));
418
419 const int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
420 for (const String &favorite : favorite_paths) {
421 if (!favorite.begins_with("res://")) {
422 continue;
423 }
424
425 String text;
426 Ref<Texture2D> icon;
427 Color color;
428 if (favorite == "res://") {
429 text = "/";
430 icon = folder_icon;
431 color = default_folder_color;
432 } else if (favorite.ends_with("/")) {
433 text = favorite.substr(0, favorite.length() - 1).get_file();
434 icon = folder_icon;
435 color = FileSystemDock::get_dir_icon_color(favorite, default_folder_color);
436 } else {
437 text = favorite.get_file();
438 int index;
439 EditorFileSystemDirectory *dir = EditorFileSystem::get_singleton()->find_file(favorite, &index);
440 if (dir) {
441 icon = _get_tree_item_icon(dir->get_file_import_is_valid(index), dir->get_file_type(index), dir->get_file_icon_path(index));
442 } else {
443 icon = get_editor_theme_icon(SNAME("File"));
444 }
445 color = Color(1, 1, 1);
446 }
447
448 TreeItem *ti = tree->create_item(favorites_item);
449 ti->set_text(0, text);
450 ti->set_icon(0, icon);
451 ti->set_icon_modulate(0, color);
452 ti->set_icon_max_width(0, icon_size);
453 ti->set_tooltip_text(0, favorite);
454 ti->set_selectable(0, true);
455 ti->set_metadata(0, favorite);
456
457 if (!favorite.ends_with("/")) {
458 EditorResourcePreview::get_singleton()->queue_resource_preview(favorite, callable_mp(this, &FileSystemDock::_tree_thumbnail_done).bind(tree_update_id, ti->get_instance_id()));
459 }
460 }
461
462 Vector<String> uncollapsed_paths = p_uncollapsed_paths;
463 if (p_uncollapse_root) {
464 uncollapsed_paths.push_back("res://");
465 }
466
467 // Create the remaining of the tree.
468 _create_tree(root, EditorFileSystem::get_singleton()->get_filesystem(), uncollapsed_paths, false);
469 if (!searched_tokens.is_empty()) {
470 _update_filtered_items();
471 }
472
473 if (p_scroll_to_selected) {
474 tree->ensure_cursor_is_visible();
475 }
476
477 updating_tree = false;
478 }
479
480 void FileSystemDock::set_display_mode(DisplayMode p_display_mode) {
481 display_mode = p_display_mode;
482 _update_display_mode(false);
483 }
484
485 void FileSystemDock::_update_display_mode(bool p_force) {
486 // Compute the new display mode.
487 if (p_force || old_display_mode != display_mode) {
488 switch (display_mode) {
489 case DISPLAY_MODE_TREE_ONLY: {
490 button_toggle_display_mode->set_button_icon(get_editor_theme_icon(SNAME("Panels1")));
491 tree->show();
492 tree->set_v_size_flags(SIZE_EXPAND_FILL);
493 tree->set_theme_type_variation("");
494 if (horizontal) {
495 toolbar2_hbc->hide();
496
497 tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTH);
498 tree_mc->set_theme_type_variation("NoBorderBottomPanel");
499 } else {
500 toolbar2_hbc->show();
501
502 tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_TOP);
503 tree_mc->set_theme_type_variation("NoBorderHorizontalBottom");
504 }
505 button_file_list_display_mode->hide();
506
507 _update_tree(get_uncollapsed_paths());
508 file_list_vb->hide();
509 } break;
510
511 case DISPLAY_MODE_HSPLIT:
512 case DISPLAY_MODE_VSPLIT: {
513 const bool is_vertical = display_mode == DISPLAY_MODE_VSPLIT;
514 split_box->set_vertical(is_vertical);
515
516 const int actual_offset = is_vertical ? split_box_offset_v : split_box_offset_h;
517 split_box->set_split_offset(actual_offset);
518 const StringName icon = is_vertical ? SNAME("Panels2") : SNAME("Panels2Alt");
519 button_toggle_display_mode->set_button_icon(get_editor_theme_icon(icon));
520
521 tree->show();
522 tree->set_v_size_flags(SIZE_EXPAND_FILL);
523 if (is_vertical) {
524 tree->set_theme_type_variation("");
525 tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTH);
526 tree_mc->set_theme_type_variation(horizontal ? "NoBorderBottomPanel" : "NoBorderHorizontal");
527
528 files->set_theme_type_variation(horizontal ? "ItemListSecondary" : "");
529 files->set_scroll_hint_mode(horizontal ? ItemList::SCROLL_HINT_MODE_DISABLED : ItemList::SCROLL_HINT_MODE_TOP);
530 files_mc->set_theme_type_variation(horizontal ? "" : "NoBorderHorizontalBottom");
531 } else {
532 tree->set_theme_type_variation("TreeSecondary");
533 tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_DISABLED);
534 tree_mc->set_theme_type_variation("");
535
536 files->set_theme_type_variation("ItemListSecondary");
537 files->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_DISABLED);
538 files_mc->set_theme_type_variation("");
539 }
540 tree->ensure_cursor_is_visible();
541
542 toolbar2_hbc->hide();
543 button_file_list_display_mode->show();
544 _update_tree(get_uncollapsed_paths());
545
546 file_list_vb->show();
547 _update_file_list(true);
548 } break;
549 }
550
551 old_display_mode = display_mode;
552 }
553 }
554
555 void FileSystemDock::_notification(int p_what) {
556 switch (p_what) {
557 case NOTIFICATION_READY: {
558 EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &FileSystemDock::_feature_profile_changed));
559 EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &FileSystemDock::_fs_changed));
560 EditorResourcePreview::get_singleton()->connect("preview_invalidated", callable_mp(this, &FileSystemDock::_preview_invalidated));
561
562 button_file_list_display_mode->connect(SceneStringName(pressed), callable_mp(this, &FileSystemDock::_toggle_file_display));
563 files->connect("item_activated", callable_mp(this, &FileSystemDock::_file_list_activate_file));
564 button_hist_next->connect(SceneStringName(pressed), callable_mp(this, &FileSystemDock::_fw_history));
565 button_hist_prev->connect(SceneStringName(pressed), callable_mp(this, &FileSystemDock::_bw_history));
566 file_list_popup->connect(SceneStringName(id_pressed), callable_mp(this, &FileSystemDock::_file_list_rmb_option));
567 tree_popup->connect(SceneStringName(id_pressed), callable_mp(this, &FileSystemDock::_tree_rmb_option));
568 current_path_line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &FileSystemDock::_navigate_to_path).bind(false, true));
569
570 always_show_folders = bool(EDITOR_GET("docks/filesystem/always_show_folders"));
571 thumbnail_size_setting = EDITOR_GET("docks/filesystem/thumbnail_size");
572
573 set_file_list_display_mode(FileSystemDock::FILE_LIST_DISPLAY_LIST);
574
575 _update_display_mode();
576
577 if (EditorFileSystem::get_singleton()->is_scanning()) {
578 _set_scanning_mode();
579 } else {
580 _update_tree(Vector<String>(), true);
581 }
582 } break;
583
584 case NOTIFICATION_PROCESS: {
585 if (EditorFileSystem::get_singleton()->is_scanning()) {
586 scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress() * 100.0f);
587 }
588 } break;
589
590 case NOTIFICATION_DRAG_BEGIN: {
591 Dictionary dd = get_viewport()->gui_get_drag_data();
592 if (tree->is_visible_in_tree() && dd.has("type")) {
593 if (dd.has("favorite")) {
594 if ((String(dd["favorite"]) == "all")) {
595 tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
596 }
597 } else if ((String(dd["type"]) == "files") || (String(dd["type"]) == "files_and_dirs")) {
598 tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM | Tree::DROP_MODE_INBETWEEN);
599 } else if ((String(dd["type"]) == "nodes") || (String(dd["type"]) == "resource")) {
600 holding_branch = true;
601 TreeItem *item = tree->get_next_selected(tree->get_root());
602 while (item) {
603 tree_items_selected_on_drag_begin.push_back(item);
604 item = tree->get_next_selected(item);
605 }
606 list_items_selected_on_drag_begin = files->get_selected_items();
607 }
608 }
609 } break;
610
611 case NOTIFICATION_DRAG_END: {
612 tree->set_drop_mode_flags(0);
613
614 if (holding_branch) {
615 holding_branch = false;
616 _reselect_items_selected_on_drag_begin(true);
617 }
618 } break;
619
620 case NOTIFICATION_TRANSLATION_CHANGED:
621 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
622 case NOTIFICATION_THEME_CHANGED: {
623 _update_display_mode(true);
624
625 StringName mode_icon = "Panels1";
626 if (display_mode == DISPLAY_MODE_VSPLIT) {
627 mode_icon = "Panels2";
628 } else if (display_mode == DISPLAY_MODE_HSPLIT) {
629 mode_icon = "Panels2Alt";
630 }
631 button_toggle_display_mode->set_button_icon(get_editor_theme_icon(mode_icon));
632
633 if (file_list_display_mode == FILE_LIST_DISPLAY_LIST) {
634 button_file_list_display_mode->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));
635 } else {
636 button_file_list_display_mode->set_button_icon(get_editor_theme_icon(SNAME("FileList")));
637 }
638
639 tree_search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
640 tree_button_sort->set_button_icon(get_editor_theme_icon(SNAME("Sort")));
641
642 file_list_search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
643 file_list_button_sort->set_button_icon(get_editor_theme_icon(SNAME("Sort")));
644
645 if (is_layout_rtl()) {
646 button_hist_next->set_button_icon(get_editor_theme_icon(SNAME("Back")));
647 button_hist_prev->set_button_icon(get_editor_theme_icon(SNAME("Forward")));
648 } else {
649 button_hist_next->set_button_icon(get_editor_theme_icon(SNAME("Forward")));
650 button_hist_prev->set_button_icon(get_editor_theme_icon(SNAME("Back")));
651 }
652
653 overwrite_dialog_scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), "Tree"));
654 } break;
655
656 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
657 // Update editor dark theme & always show folders states from editor settings, redraw if needed.
658 bool do_redraw = false;
659
660 bool new_editor_is_dark_icon_and_font = EditorThemeManager::is_dark_icon_and_font();
661 if (new_editor_is_dark_icon_and_font != editor_is_dark_icon_and_font) {
662 editor_is_dark_icon_and_font = new_editor_is_dark_icon_and_font;
663 do_redraw = true;
664 }
665
666 bool new_always_show_folders = bool(EDITOR_GET("docks/filesystem/always_show_folders"));
667 if (new_always_show_folders != always_show_folders) {
668 always_show_folders = new_always_show_folders;
669 do_redraw = true;
670 }
671
672 int new_thumbnail_size_setting = EDITOR_GET("docks/filesystem/thumbnail_size");
673 if (new_thumbnail_size_setting != thumbnail_size_setting) {
674 thumbnail_size_setting = new_thumbnail_size_setting;
675 do_redraw = true;
676 }
677
678 if (do_redraw) {
679 update_all();
680 }
681
682 if (EditorThemeManager::is_generated_theme_outdated()) {
683 // Change full tree mode.
684 _update_display_mode();
685 }
686 } break;
687 }
688 }
689
690 void FileSystemDock::_tree_multi_selected(Object *p_item, int p_column, bool p_selected) {
691 // Update the import dock.
692 import_dock_needs_update = true;
693 callable_mp(this, &FileSystemDock::_update_import_dock).call_deferred();
694
695 // Return if we don't select something new.
696 if (!p_selected) {
697 return;
698 }
699
700 // Tree item selected.
701 TreeItem *selected = tree->get_selected();
702 if (!selected) {
703 return;
704 }
705
706 if (selected->get_parent() == favorites_item && !String(selected->get_metadata(0)).ends_with("/")) {
707 // Go to the favorites if we click in the favorites and the path has changed.
708 current_path = "Favorites";
709 } else {
710 current_path = selected->get_metadata(0);
711 // Note: the "Favorites" item also leads to this path.
712 }
713
714 // Display the current path.
715 _set_current_path_line_edit_text(current_path);
716 _push_to_history();
717
718 // Update the file list.
719 if (!updating_tree && display_mode != DISPLAY_MODE_TREE_ONLY) {
720 _update_file_list(false);
721 }
722 }
723
724 Vector<String> FileSystemDock::get_selected_paths() const {
725 if (display_mode == DISPLAY_MODE_TREE_ONLY) {
726 return _tree_get_selected(false);
727 } else {
728 Vector<String> selected = _file_list_get_selected();
729 if (selected.is_empty()) {
730 selected.push_back(get_current_directory());
731 }
732 return selected;
733 }
734 }
735
736 String FileSystemDock::get_current_path() const {
737 return current_path;
738 }
739
740 String FileSystemDock::get_current_directory() const {
741 if (current_path.ends_with("/")) {
742 return current_path;
743 } else {
744 return current_path.get_base_dir();
745 }
746 }
747
748 void FileSystemDock::_set_current_path_line_edit_text(const String &p_path) {
749 if (p_path == "Favorites") {
750 current_path_line_edit->set_text(TTR("Favorites"));
751 } else {
752 current_path_line_edit->set_text(current_path);
753 }
754 }
755
756 void FileSystemDock::_navigate_to_path(const String &p_path, bool p_select_in_favorites, bool p_grab_focus) {
757 String target_path = p_path;
758 bool is_directory = false;
759
760 if (p_path.is_empty()) {
761 target_path = "res://";
762 is_directory = true;
763 } else if (p_path != "Favorites") {
764 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
765 if (da->dir_exists(p_path)) {
766 is_directory = true;
767 if (!p_path.ends_with("/")) {
768 target_path += "/";
769 }
770 } else if (!da->file_exists(p_path)) {
771 ERR_FAIL_MSG(vformat("Cannot navigate to '%s' as it has not been found in the file system!", p_path));
772 }
773 }
774
775 current_path = target_path;
776 _set_current_path_line_edit_text(current_path);
777 _push_to_history();
778
779 String base_dir_path = target_path.get_base_dir();
780 if (base_dir_path != "res://") {
781 base_dir_path += "/";
782 }
783
784 TreeItem **directory_ptr = folder_map.getptr(base_dir_path);
785 if (!directory_ptr) {
786 return;
787 }
788
789 // Unfold all folders along the path.
790 TreeItem *ti = *directory_ptr;
791 while (ti) {
792 ti->set_collapsed(false);
793 ti = ti->get_parent();
794 }
795
796 // Select the file or directory in the tree.
797 tree->deselect_all();
798 if (display_mode == DISPLAY_MODE_TREE_ONLY) {
799 // Either search for 'folder/' or '/file.ext'.
800 const String file_name = is_directory ? target_path.trim_suffix("/").get_file() + "/" : "/" + target_path.get_file();
801 TreeItem *item = is_directory ? *directory_ptr : (*directory_ptr)->get_first_child();
802 while (item) {
803 if (item->get_metadata(0).operator String().ends_with(file_name)) {
804 item->select(0);
805 break;
806 }
807 item = item->get_next();
808 }
809 if (p_grab_focus) {
810 tree->grab_focus(true);
811 }
812 } else {
813 (*directory_ptr)->select(0);
814 _update_file_list(false);
815 if (p_grab_focus) {
816 files->grab_focus(true);
817 }
818 }
819 tree->ensure_cursor_is_visible();
820 }
821
822 bool FileSystemDock::_update_filtered_items(TreeItem *p_tree_item) {
823 TreeItem *item = p_tree_item;
824 if (!item) {
825 item = tree->get_root();
826 }
827 ERR_FAIL_NULL_V(item, false);
828
829 bool keep_visible = false;
830 for (TreeItem *child = item->get_first_child(); child; child = child->get_next()) {
831 keep_visible = _update_filtered_items(child) || keep_visible;
832 }
833
834 if (searched_tokens.is_empty()) {
835 item->set_visible(true);
836 // Always uncollapse root (the hidden item above res:// and favorites).
837 item->set_collapsed(item != tree->get_root() && !uncollapsed_paths_before_search.has(item->get_metadata(0)));
838 return true;
839 }
840
841 if (keep_visible) {
842 item->set_collapsed(false);
843 } else {
844 // res:// and favorites are always visible.
845 keep_visible = item == resources_item || item == favorites_item;
846 keep_visible = keep_visible || _matches_all_search_tokens(item->get_text(0));
847 }
848 item->set_visible(keep_visible);
849 return keep_visible;
850 }
851
852 void FileSystemDock::navigate_to_path(const String &p_path) {
853 file_list_search_box->clear();
854 // Try to set the FileSystem dock visible.
855 EditorDockManager::get_singleton()->focus_dock(this);
856 _navigate_to_path(p_path, false, is_visible_in_tree());
857
858 import_dock_needs_update = true;
859 _update_import_dock();
860 }
861
862 void FileSystemDock::_file_list_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, int p_index, const String &p_filename) {
863 if (p_preview.is_valid()) {
864 if (p_index < files->get_item_count() && files->get_item_text(p_index) == p_filename && files->get_item_metadata(p_index) == p_path) {
865 if (file_list_display_mode == FILE_LIST_DISPLAY_LIST) {
866 if (p_small_preview.is_valid()) {
867 files->set_item_icon(p_index, p_small_preview);
868 }
869 } else {
870 files->set_item_icon(p_index, p_preview);
871 }
872 }
873 }
874 }
875
876 void FileSystemDock::_tree_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, int p_update_id, ObjectID p_item) {
877 TreeItem *item = ObjectDB::get_instance<TreeItem>(p_item);
878 if (item && tree_update_id == p_update_id && p_small_preview.is_valid()) {
879 item->set_icon(0, p_small_preview);
880 }
881 }
882
883 void FileSystemDock::_toggle_file_display() {
884 _set_file_display(file_list_display_mode != FILE_LIST_DISPLAY_LIST);
885 emit_signal(SNAME("display_mode_changed"));
886 }
887
888 void FileSystemDock::_set_file_display(bool p_active) {
889 if (p_active) {
890 file_list_display_mode = FILE_LIST_DISPLAY_LIST;
891 button_file_list_display_mode->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));
892 button_file_list_display_mode->set_tooltip_text(TTRC("View items as a grid of thumbnails."));
893 } else {
894 file_list_display_mode = FILE_LIST_DISPLAY_THUMBNAILS;
895 button_file_list_display_mode->set_button_icon(get_editor_theme_icon(SNAME("FileList")));
896 button_file_list_display_mode->set_tooltip_text(TTRC("View items as a list."));
897 }
898
899 _update_file_list(true);
900 }
901
902 bool FileSystemDock::_is_file_type_disabled_by_feature_profile(const StringName &p_class) {
903 Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
904 if (profile.is_null() || !ClassDB::class_exists(p_class)) {
905 return false;
906 }
907
908 StringName class_name = p_class;
909
910 while (class_name != StringName()) {
911 if (profile->is_class_disabled(class_name)) {
912 return true;
913 }
914 class_name = ClassDB::get_parent_class(class_name);
915 }
916
917 return false;
918 }
919
920 void FileSystemDock::_search(EditorFileSystemDirectory *p_path, List<FileInfo> *matches, int p_max_items) {
921 if (matches->size() > p_max_items) {
922 return;
923 }
924
925 for (int i = 0; i < p_path->get_subdir_count(); i++) {
926 _search(p_path->get_subdir(i), matches, p_max_items);
927 }
928
929 for (int i = 0; i < p_path->get_file_count(); i++) {
930 String file = p_path->get_file(i);
931
932 if (_matches_all_search_tokens(file)) {
933 FileInfo file_info;
934 file_info.name = file;
935 file_info.type = p_path->get_file_type(i);
936 file_info.path = p_path->get_file_path(i);
937 file_info.import_broken = !p_path->get_file_import_is_valid(i);
938 file_info.modified_time = p_path->get_file_modified_time(i);
939
940 if (_is_file_type_disabled_by_feature_profile(file_info.type)) {
941 // This type is disabled, will not appear here.
942 continue;
943 }
944
945 matches->push_back(file_info);
946 if (matches->size() > p_max_items) {
947 return;
948 }
949 }
950 }
951 }
952
953 void FileSystemDock::_update_file_list(bool p_keep_selection) {
954 // Register the previously current and selected items.
955 HashSet<String> previous_selection;
956 HashSet<int> valid_selection;
957 if (p_keep_selection) {
958 for (int i = 0; i < files->get_item_count(); i++) {
959 if (files->is_selected(i)) {
960 previous_selection.insert(files->get_item_text(i));
961 }
962 }
963 }
964
965 files->clear();
966
967 _set_current_path_line_edit_text(current_path);
968
969 String directory = current_path;
970 String file = "";
971
972 int thumbnail_size = thumbnail_size_setting * EDSCALE;
973 Ref<Texture2D> folder_thumbnail;
974 Ref<Texture2D> file_thumbnail;
975 Ref<Texture2D> file_thumbnail_broken;
976
977 bool use_thumbnails = (file_list_display_mode == FILE_LIST_DISPLAY_THUMBNAILS);
978
979 if (use_thumbnails) {
980 // Thumbnails mode.
981 files->set_max_columns(0);
982 files->set_icon_mode(ItemList::ICON_MODE_TOP);
983 files->set_fixed_column_width(thumbnail_size * 3 / 2);
984 files->set_max_text_lines(2);
985 files->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
986
987 const int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
988 files->set_fixed_tag_icon_size(Size2(icon_size, icon_size));
989
990 if (thumbnail_size < 64) {
991 folder_thumbnail = get_editor_theme_icon(SNAME("FolderMediumThumb"));
992 file_thumbnail = get_editor_theme_icon(SNAME("FileMediumThumb"));
993 file_thumbnail_broken = get_editor_theme_icon(SNAME("FileDeadMediumThumb"));
994 } else {
995 folder_thumbnail = get_editor_theme_icon(SNAME("FolderBigThumb"));
996 file_thumbnail = get_editor_theme_icon(SNAME("FileBigThumb"));
997 file_thumbnail_broken = get_editor_theme_icon(SNAME("FileDeadBigThumb"));
998 }
999 } else {
1000 // No thumbnails.
1001 files->set_icon_mode(ItemList::ICON_MODE_LEFT);
1002 files->set_max_columns(1);
1003 files->set_max_text_lines(1);
1004 files->set_fixed_column_width(0);
1005 const int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
1006 files->set_fixed_icon_size(Size2(icon_size, icon_size));
1007 }
1008
1009 Ref<Texture2D> folder_icon = (use_thumbnails) ? folder_thumbnail : get_theme_icon(SNAME("folder"), SNAME("FileDialog"));
1010 const Color default_folder_color = get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog"));
1011
1012 // Build the FileInfo list.
1013 List<FileInfo> file_list;
1014 if (current_path == "Favorites") {
1015 // Display the favorites.
1016 Vector<String> favorites_list = EditorSettings::get_singleton()->get_favorites();
1017 for (const String &favorite : favorites_list) {
1018 if (!favorite.begins_with("res://")) {
1019 continue;
1020 }
1021 String text;
1022 Ref<Texture2D> icon;
1023 if (favorite == "res://") {
1024 text = "/";
1025 icon = folder_icon;
1026 if (searched_tokens.is_empty() || _matches_all_search_tokens(text)) {
1027 files->add_item(text, icon, true);
1028 files->set_item_metadata(-1, favorite);
1029 }
1030 } else if (favorite.ends_with("/")) {
1031 text = favorite.substr(0, favorite.length() - 1).get_file();
1032 icon = folder_icon;
1033 if (searched_tokens.is_empty() || _matches_all_search_tokens(text)) {
1034 files->add_item(text, icon, true);
1035 files->set_item_metadata(-1, favorite);
1036 }
1037 } else {
1038 int index;
1039 EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->find_file(favorite, &index);
1040
1041 FileInfo file_info;
1042 file_info.name = favorite.get_file();
1043 file_info.path = favorite;
1044 if (efd) {
1045 file_info.type = efd->get_file_type(index);
1046 file_info.icon_path = efd->get_file_icon_path(index);
1047 file_info.import_broken = !efd->get_file_import_is_valid(index);
1048 file_info.modified_time = efd->get_file_modified_time(index);
1049 } else {
1050 file_info.type = "";
1051 file_info.import_broken = true;
1052 file_info.modified_time = 0;
1053 }
1054
1055 if (searched_tokens.is_empty() || _matches_all_search_tokens(file_info.name)) {
1056 file_list.push_back(file_info);
1057 }
1058 }
1059 }
1060 } else {
1061 if (!directory.begins_with("res://")) {
1062 directory = "res://" + directory;
1063 }
1064 // Get infos on the directory + file.
1065 if (directory.ends_with("/") && directory != "res://") {
1066 directory = directory.substr(0, directory.length() - 1);
1067 }
1068 EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->get_filesystem_path(directory);
1069 if (!efd) {
1070 directory = current_path.get_base_dir();
1071 file = current_path.get_file();
1072 efd = EditorFileSystem::get_singleton()->get_filesystem_path(directory);
1073 }
1074 if (!efd) {
1075 return;
1076 }
1077
1078 if (!searched_tokens.is_empty()) {
1079 // Display the search results.
1080 // Limit the number of results displayed to avoid an infinite loop.
1081 _search(EditorFileSystem::get_singleton()->get_filesystem(), &file_list, 10000);
1082 } else {
1083 if (display_mode == DISPLAY_MODE_TREE_ONLY || always_show_folders) {
1084 // Check for a folder color to inherit (if one is assigned).
1085 Color inherited_folder_color = default_folder_color;
1086 String color_scan_dir = directory;
1087 while (color_scan_dir != "res://" && inherited_folder_color == default_folder_color) {
1088 if (!color_scan_dir.ends_with("/")) {
1089 color_scan_dir += "/";
1090 }
1091
1092 if (assigned_folder_colors.has(color_scan_dir)) {
1093 inherited_folder_color = folder_colors[assigned_folder_colors[color_scan_dir]];
1094 }
1095
1096 color_scan_dir = color_scan_dir.rstrip("/").get_base_dir();
1097 }
1098
1099 // Display folders in the list.
1100 if (directory != "res://") {
1101 files->add_item("..", folder_icon, true);
1102
1103 String bd = directory.get_base_dir();
1104 if (bd != "res://" && !bd.ends_with("/")) {
1105 bd += "/";
1106 }
1107
1108 files->set_item_metadata(-1, bd);
1109 files->set_item_selectable(-1, false);
1110 if (!editor_is_dark_icon_and_font && inherited_folder_color != default_folder_color) {
1111 files->set_item_icon_modulate(-1, inherited_folder_color * ITEM_COLOR_SCALE);
1112 } else {
1113 files->set_item_icon_modulate(-1, inherited_folder_color);
1114 }
1115 }
1116
1117 bool reversed = file_sort == FileSortOption::FILE_SORT_NAME_REVERSE;
1118 for (int i = reversed ? efd->get_subdir_count() - 1 : 0;
1119 reversed ? i >= 0 : i < efd->get_subdir_count();
1120 reversed ? i-- : i++) {
1121 String dname = efd->get_subdir(i)->get_name();
1122 String dpath = directory.path_join(dname) + "/";
1123 bool has_custom_color = assigned_folder_colors.has(dpath);
1124
1125 files->add_item(dname, folder_icon, true);
1126 files->set_item_metadata(-1, dpath);
1127 Color this_folder_color = has_custom_color ? folder_colors[assigned_folder_colors[dpath]] : inherited_folder_color;
1128 if (!editor_is_dark_icon_and_font && this_folder_color != default_folder_color) {
1129 this_folder_color *= ITEM_COLOR_SCALE;
1130 }
1131 files->set_item_icon_modulate(-1, this_folder_color);
1132
1133 if (previous_selection.has(dname)) {
1134 files->select(files->get_item_count() - 1, false);
1135 valid_selection.insert(files->get_item_count() - 1);
1136 }
1137 }
1138 }
1139
1140 // Display the folder content.
1141 for (int i = 0; i < efd->get_file_count(); i++) {
1142 FileInfo file_info;
1143 file_info.name = efd->get_file(i);
1144 file_info.path = directory.path_join(file_info.name);
1145 file_info.type = efd->get_file_type(i);
1146 file_info.icon_path = efd->get_file_icon_path(i);
1147 file_info.import_broken = !efd->get_file_import_is_valid(i);
1148 file_info.modified_time = efd->get_file_modified_time(i);
1149
1150 file_list.push_back(file_info);
1151 }
1152 }
1153 }
1154
1155 // Sort the file list if needed.
1156 sort_file_info_list(file_list, file_sort);
1157
1158 // Fills the ItemList control node from the FileInfos.
1159 for (FileInfo &E : file_list) {
1160 FileInfo *finfo = &(E);
1161 String fname = finfo->name;
1162 String fpath = finfo->path;
1163
1164 Ref<Texture2D> type_icon;
1165 Ref<Texture2D> big_icon;
1166
1167 String tooltip = fpath;
1168
1169 // Select the icons.
1170 type_icon = _get_tree_item_icon(!finfo->import_broken, finfo->type, finfo->icon_path);
1171 if (!finfo->import_broken) {
1172 big_icon = file_thumbnail;
1173 } else {
1174 big_icon = file_thumbnail_broken;
1175 tooltip += "\n" + TTR("Status: Import of file failed. Please fix file and reimport manually.");
1176 }
1177
1178 // Add the item to the ItemList.
1179 int item_index;
1180 if (use_thumbnails) {
1181 files->add_item(fname, big_icon, true);
1182 item_index = files->get_item_count() - 1;
1183 files->set_item_metadata(item_index, fpath);
1184 files->set_item_tag_icon(item_index, type_icon);
1185
1186 } else {
1187 files->add_item(fname, type_icon, true);
1188 item_index = files->get_item_count() - 1;
1189 files->set_item_metadata(item_index, fpath);
1190 }
1191
1192 if (fpath == main_scene_path) {
1193 files->set_item_custom_fg_color(item_index, get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
1194 }
1195
1196 // Generate the preview.
1197 if (!finfo->import_broken) {
1198 EditorResourcePreview::get_singleton()->queue_resource_preview(fpath, callable_mp(this, &FileSystemDock::_file_list_thumbnail_done).bind(item_index, fname));
1199 }
1200
1201 // Select the items.
1202 if (previous_selection.has(fname)) {
1203 files->select(item_index, false);
1204 valid_selection.insert(item_index);
1205 }
1206
1207 if (!p_keep_selection && !file.is_empty() && fname == file) {
1208 files->select(item_index, true);
1209 files->ensure_current_is_visible();
1210 }
1211
1212 // Tooltip.
1213 if (finfo->sources.size()) {
1214 for (int j = 0; j < finfo->sources.size(); j++) {
1215 tooltip += "\nSource: " + finfo->sources[j];
1216 }
1217 }
1218 files->set_item_tooltip(item_index, tooltip);
1219 }
1220
1221 // If we only have any selected items retained, we need to update the current idx.
1222 if (!valid_selection.is_empty()) {
1223 files->set_current(*valid_selection.begin());
1224 }
1225 }
1226
1227 HashSet<String> FileSystemDock::_get_valid_conversions_for_file_paths(const Vector<String> &p_paths) {
1228 HashSet<String> all_valid_conversion_to_targets;
1229 for (const String &fpath : p_paths) {
1230 if (fpath.is_empty() || fpath == "res://" || !FileAccess::exists(fpath) || FileAccess::exists(fpath + ".import")) {
1231 return HashSet<String>();
1232 }
1233
1234 Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin_for_type_name(EditorFileSystem::get_singleton()->get_file_type(fpath));
1235
1236 if (conversions.is_empty()) {
1237 // This resource can't convert to anything, so return an empty list.
1238 return HashSet<String>();
1239 }
1240
1241 // Get a list of all potential conversion-to targets.
1242 HashSet<String> current_valid_conversion_to_targets;
1243 for (const Ref<EditorResourceConversionPlugin> &E : conversions) {
1244 const String what = E->converts_to();
1245 current_valid_conversion_to_targets.insert(what);
1246 }
1247
1248 if (all_valid_conversion_to_targets.is_empty()) {
1249 // If we have no existing valid conversions, this is the first one, so copy them directly.
1250 all_valid_conversion_to_targets = current_valid_conversion_to_targets;
1251 } else {
1252 // Check existing conversion targets and remove any which are not in the current list.
1253 for (const String &S : all_valid_conversion_to_targets) {
1254 if (!current_valid_conversion_to_targets.has(S)) {
1255 all_valid_conversion_to_targets.erase(S);
1256 }
1257 }
1258 // We have no more remaining valid conversions, so break the loop.
1259 if (all_valid_conversion_to_targets.is_empty()) {
1260 break;
1261 }
1262 }
1263 }
1264
1265 return all_valid_conversion_to_targets;
1266 }
1267
1268 void FileSystemDock::_select_file(const String &p_path, bool p_select_in_favorites, bool p_navigate) {
1269 String fpath = p_path;
1270 if (fpath.ends_with("/")) {
1271 // Ignore a directory.
1272 } else if (fpath != "Favorites") {
1273 if (FileAccess::exists(fpath + ".import")) {
1274 Ref<ConfigFile> config;
1275 config.instantiate();
1276 Error err = config->load(fpath + ".import");
1277 if (err == OK) {
1278 if (config->has_section_key("remap", "importer")) {
1279 String importer = config->get_value("remap", "importer");
1280 if (importer == "keep" || importer == "skip") {
1281 EditorNode::get_singleton()->show_warning(TTRC("Importing has been disabled for this file, so it can't be opened for editing."));
1282 return;
1283 }
1284 }
1285 }
1286 }
1287
1288 String resource_type = ResourceLoader::get_resource_type(fpath);
1289 if (resource_type == "PackedScene" || resource_type == "AnimationLibrary") {
1290 bool is_imported = false;
1291 {
1292 List<String> importer_exts;
1293 ResourceImporterScene::get_scene_importer_extensions(&importer_exts);
1294 String extension = fpath.get_extension();
1295 for (const String &E : importer_exts) {
1296 if (extension.nocasecmp_to(E) == 0) {
1297 is_imported = true;
1298 break;
1299 }
1300 }
1301 }
1302
1303 if (is_imported) {
1304 SceneImportSettingsDialog::get_singleton()->open_settings(p_path, resource_type);
1305 } else {
1306 EditorNode::get_singleton()->load_scene_or_resource(fpath);
1307 }
1308 } else if (ResourceLoader::is_imported(fpath)) {
1309 // If the importer has advanced settings, show them.
1310 int order;
1311 bool can_threads;
1312 String name;
1313 Error err = ResourceFormatImporter::get_singleton()->get_import_order_threads_and_importer(fpath, order, can_threads, name);
1314 bool used_advanced_settings = false;
1315 if (err == OK) {
1316 Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(name);
1317 if (importer.is_valid() && importer->has_advanced_options()) {
1318 importer->show_advanced_options(fpath);
1319 used_advanced_settings = true;
1320 }
1321 }
1322
1323 if (!used_advanced_settings) {
1324 EditorNode::get_singleton()->load_resource(fpath);
1325 }
1326 } else {
1327 EditorNode::get_singleton()->load_resource(fpath);
1328 }
1329 }
1330 if (p_navigate) {
1331 _navigate_to_path(fpath, p_select_in_favorites);
1332 }
1333 }
1334
1335 void FileSystemDock::_tree_activate_file() {
1336 TreeItem *selected = tree->get_selected();
1337 if (selected) {
1338 String file_path = selected->get_metadata(0);
1339 TreeItem *parent = selected->get_parent();
1340 bool is_favorite = parent != nullptr && parent->get_metadata(0) == "Favorites";
1341 bool is_folder = file_path.ends_with("/");
1342
1343 if ((!is_favorite && is_folder) || file_path == "Favorites") {
1344 bool collapsed = selected->is_collapsed();
1345 selected->set_collapsed(!collapsed);
1346 } else {
1347 _select_file(file_path, is_favorite && !is_folder, is_favorite && is_folder);
1348 }
1349 }
1350 }
1351
1352 void FileSystemDock::_file_list_activate_file(int p_idx) {
1353 _select_file(files->get_item_metadata(p_idx));
1354 }
1355
1356 void FileSystemDock::_preview_invalidated(const String &p_path) {
1357 if (file_list_display_mode == FILE_LIST_DISPLAY_THUMBNAILS && p_path.get_base_dir() == current_path && searched_tokens.is_empty() && file_list_vb->is_visible_in_tree()) {
1358 for (int i = 0; i < files->get_item_count(); i++) {
1359 if (files->get_item_metadata(i) == p_path) {
1360 // Re-request preview.
1361 EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, callable_mp(this, &FileSystemDock::_file_list_thumbnail_done).bind(i, files->get_item_text(i)));
1362 break;
1363 }
1364 }
1365 }
1366 }
1367
1368 void FileSystemDock::_fs_changed() {
1369 button_hist_prev->set_disabled(history_pos == 0);
1370 button_hist_next->set_disabled(history_pos == history.size() - 1);
1371 scanning_vb->hide();
1372 split_box->show();
1373
1374 update_all();
1375
1376 if (!select_after_scan.is_empty()) {
1377 _navigate_to_path(select_after_scan);
1378 select_after_scan.clear();
1379 import_dock_needs_update = true;
1380 _update_import_dock();
1381 }
1382
1383 set_process(false);
1384 if (had_focus) {
1385 had_focus->grab_focus();
1386 had_focus = nullptr;
1387 }
1388 }
1389
1390 void FileSystemDock::_set_scanning_mode() {
1391 button_hist_prev->set_disabled(true);
1392 button_hist_next->set_disabled(true);
1393 split_box->hide();
1394 scanning_vb->show();
1395 set_process(true);
1396 if (EditorFileSystem::get_singleton()->is_scanning()) {
1397 scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress() * 100);
1398 } else {
1399 scanning_progress->set_value(0);
1400 }
1401 }
1402
1403 void FileSystemDock::_fw_history() {
1404 if (history_pos < history.size() - 1) {
1405 history_pos++;
1406 }
1407
1408 _update_history();
1409 }
1410
1411 void FileSystemDock::_bw_history() {
1412 if (history_pos > 0) {
1413 history_pos--;
1414 }
1415
1416 _update_history();
1417 }
1418
1419 void FileSystemDock::_update_history() {
1420 current_path = history[history_pos];
1421 _set_current_path_line_edit_text(current_path);
1422
1423 if (tree->is_visible()) {
1424 _update_tree(get_uncollapsed_paths());
1425 tree->grab_focus(true);
1426 }
1427
1428 if (file_list_vb->is_visible()) {
1429 _update_file_list(false);
1430 }
1431
1432 button_hist_prev->set_disabled(history_pos == 0);
1433 button_hist_next->set_disabled(history_pos == history.size() - 1);
1434 }
1435
1436 void FileSystemDock::_push_to_history() {
1437 if (history[history_pos] != current_path) {
1438 history.resize(history_pos + 1);
1439 history.push_back(current_path);
1440 history_pos++;
1441
1442 if (history.size() > history_max_size) {
1443 history.remove_at(0);
1444 history_pos = history_max_size - 1;
1445 }
1446 }
1447
1448 button_hist_prev->set_disabled(history_pos == 0);
1449 button_hist_next->set_disabled(history_pos == history.size() - 1);
1450 }
1451
1452 void FileSystemDock::_get_all_items_in_dir(EditorFileSystemDirectory *p_efsd, Vector<String> &r_files, Vector<String> &r_folders) const {
1453 if (p_efsd == nullptr) {
1454 return;
1455 }
1456
1457 for (int i = 0; i < p_efsd->get_subdir_count(); i++) {
1458 r_folders.push_back(p_efsd->get_subdir(i)->get_path());
1459 _get_all_items_in_dir(p_efsd->get_subdir(i), r_files, r_folders);
1460 }
1461 for (int i = 0; i < p_efsd->get_file_count(); i++) {
1462 r_files.push_back(p_efsd->get_file_path(i));
1463 }
1464 }
1465
1466 void FileSystemDock::_find_file_owners(EditorFileSystemDirectory *p_efsd, const HashSet<String> &p_renames, HashSet<String> &r_file_owners) const {
1467 for (int i = 0; i < p_efsd->get_subdir_count(); i++) {
1468 _find_file_owners(p_efsd->get_subdir(i), p_renames, r_file_owners);
1469 }
1470 for (int i = 0; i < p_efsd->get_file_count(); i++) {
1471 Vector<String> deps = p_efsd->get_file_deps(i);
1472 for (int j = 0; j < deps.size(); j++) {
1473 if (p_renames.has(deps[j])) {
1474 r_file_owners.insert(p_efsd->get_file_path(i));
1475 break;
1476 }
1477 }
1478 }
1479 }
1480
1481 void FileSystemDock::_try_move_item(const FileOrFolder &p_item, const String &p_new_path,
1482 HashMap<String, String> &p_file_renames, HashMap<String, String> &p_folder_renames) {
1483 // Ensure folder paths end with "/".
1484 String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
1485 String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
1486
1487 if (new_path == old_path) {
1488 return;
1489 } else if (old_path == "res://") {
1490 EditorNode::get_singleton()->add_io_error(TTR("Cannot move/rename resources root."));
1491 return;
1492 } else if (!p_item.is_file && new_path.begins_with(old_path)) {
1493 // This check doesn't erroneously catch renaming to a longer name as folder paths always end with "/".
1494 EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.") + "\n" + old_path + "\n");
1495 return;
1496 }
1497
1498 // Build a list of files which will have new paths as a result of this operation.
1499 Vector<String> file_changed_paths;
1500 Vector<String> folder_changed_paths;
1501 if (p_item.is_file) {
1502 file_changed_paths.push_back(old_path);
1503 } else {
1504 folder_changed_paths.push_back(old_path);
1505 _get_all_items_in_dir(EditorFileSystem::get_singleton()->get_filesystem_path(old_path), file_changed_paths, folder_changed_paths);
1506 }
1507
1508 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
1509 print_verbose("Moving " + old_path + " -> " + new_path);
1510 Error err = da->rename(old_path, new_path);
1511 if (err == OK) {
1512 // Move/Rename any corresponding import settings too.
1513 if (p_item.is_file && FileAccess::exists(old_path + ".import")) {
1514 err = da->rename(old_path + ".import", new_path + ".import");
1515 if (err != OK) {
1516 EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + ".import\n");
1517 }
1518 }
1519
1520 if (p_item.is_file && FileAccess::exists(old_path + ".uid")) {
1521 err = da->rename(old_path + ".uid", new_path + ".uid");
1522 if (err != OK) {
1523 EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + ".uid\n");
1524 }
1525 }
1526
1527 // Update scene if it is open.
1528 for (int i = 0; i < file_changed_paths.size(); ++i) {
1529 String new_item_path = p_item.is_file ? new_path : file_changed_paths[i].replace_first(old_path, new_path);
1530 if (ResourceLoader::get_resource_type(new_item_path) == "PackedScene" && EditorNode::get_singleton()->is_scene_open(file_changed_paths[i])) {
1531 EditorData *ed = &EditorNode::get_editor_data();
1532 for (int j = 0; j < ed->get_edited_scene_count(); j++) {
1533 if (ed->get_scene_path(j) == file_changed_paths[i]) {
1534 ed->get_edited_scene_root(j)->set_scene_file_path(new_item_path);
1535 EditorNode::get_singleton()->save_editor_layout_delayed();
1536 break;
1537 }
1538 }
1539 }
1540 }
1541
1542 // Only treat as a changed dependency if it was successfully moved.
1543 for (int i = 0; i < file_changed_paths.size(); ++i) {
1544 p_file_renames[file_changed_paths[i]] = file_changed_paths[i].replace_first(old_path, new_path);
1545 print_verbose(" Remap: " + file_changed_paths[i] + " -> " + p_file_renames[file_changed_paths[i]]);
1546 emit_signal(SNAME("files_moved"), file_changed_paths[i], p_file_renames[file_changed_paths[i]]);
1547 }
1548 for (int i = 0; i < folder_changed_paths.size(); ++i) {
1549 p_folder_renames[folder_changed_paths[i]] = folder_changed_paths[i].replace_first(old_path, new_path);
1550 emit_signal(SNAME("folder_moved"), folder_changed_paths[i], p_folder_renames[folder_changed_paths[i]].substr(0, p_folder_renames[folder_changed_paths[i]].length() - 1));
1551 }
1552 } else {
1553 EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + "\n");
1554 }
1555 }
1556
1557 void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const String &p_new_path) const {
1558 // Ensure folder paths end with "/".
1559 String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
1560 String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
1561
1562 if (new_path == old_path) {
1563 return;
1564 } else if (old_path == "res://") {
1565 EditorNode::get_singleton()->add_io_error(TTR("Cannot move/rename resources root."));
1566 return;
1567 } else if (!p_item.is_file && new_path.begins_with(old_path)) {
1568 // This check doesn't erroneously catch renaming to a longer name as folder paths always end with "/".
1569 EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.") + "\n" + old_path + "\n");
1570 return;
1571 }
1572
1573 if (p_item.is_file) {
1574 print_verbose("Duplicating " + old_path + " -> " + new_path);
1575
1576 // Create the directory structure.
1577 EditorFileSystem::get_singleton()->make_dir_recursive(p_new_path.get_base_dir());
1578
1579 Error err = EditorFileSystem::get_singleton()->copy_file(old_path, new_path);
1580 if (err != OK) {
1581 EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + U" → " + new_path + ": " + TTR(error_names[err]) + "\n");
1582 }
1583 } else {
1584 Error err = EditorFileSystem::get_singleton()->copy_directory(old_path, new_path);
1585 if (err != OK) {
1586 EditorNode::get_singleton()->add_io_error(TTR("Error duplicating directory:") + "\n" + old_path + U" → " + new_path + ": " + TTR(error_names[err]) + "\n");
1587 }
1588 }
1589 }
1590
1591 void FileSystemDock::_update_resource_paths_after_move(const HashMap<String, String> &p_renames, const HashMap<String, ResourceUID::ID> &p_uids) const {
1592 for (const KeyValue<String, String> &pair : p_renames) {
1593 // Update UID path.
1594 const String &old_path = pair.key;
1595 const String &new_path = pair.value;
1596
1597 const HashMap<String, ResourceUID::ID>::ConstIterator I = p_uids.find(old_path);
1598 if (I) {
1599 ResourceUID::get_singleton()->set_id(I->value, new_path);
1600 }
1601 EditorFileSystem::get_singleton()->register_global_class_script(old_path, new_path);
1602 }
1603
1604 // Rename all resources loaded, be it subresources or actual resources.
1605 List<Ref<Resource>> cached;
1606 ResourceCache::get_cached_resources(&cached);
1607
1608 for (Ref<Resource> &r : cached) {
1609 String base_path = r->get_path();
1610 String extra_path;
1611 int sep_pos = r->get_path().find("::");
1612 if (sep_pos >= 0) {
1613 extra_path = base_path.substr(sep_pos);
1614 base_path = base_path.substr(0, sep_pos);
1615 }
1616
1617 if (p_renames.has(base_path)) {
1618 base_path = p_renames[base_path];
1619 r->set_path(base_path + extra_path);
1620 }
1621 }
1622
1623 EditorNode::get_editor_data().script_class_save_global_classes();
1624 EditorFileSystem::get_singleton()->emit_signal(SNAME("script_classes_updated"));
1625 }
1626
1627 void FileSystemDock::_update_dependencies_after_move(const HashMap<String, String> &p_renames, const HashSet<String> &p_file_owners) const {
1628 // The following code assumes that the following holds:
1629 // 1) EditorFileSystem contains the old paths/folder structure from before the rename/move.
1630 // 2) ResourceLoader can use the new paths without needing to call rescan.
1631
1632 // The currently edited scene should be reloaded first, so get it's path (GH-82652).
1633 const String &edited_scene_path = EditorNode::get_editor_data().get_scene_path(EditorNode::get_editor_data().get_edited_scene());
1634 List<String> scenes_to_reload;
1635 for (const String &E : p_file_owners) {
1636 // Because we haven't called a rescan yet the found remap might still be an old path itself.
1637 const HashMap<String, String>::ConstIterator I = p_renames.find(E);
1638 const String file = I ? I->value : E;
1639 print_verbose("Remapping dependencies for: " + file);
1640 const Error err = ResourceLoader::rename_dependencies(file, p_renames);
1641 if (err == OK) {
1642 if (ResourceLoader::get_resource_type(file) == "PackedScene") {
1643 if (file == edited_scene_path) {
1644 scenes_to_reload.push_front(file);
1645 } else {
1646 scenes_to_reload.push_back(file);
1647 }
1648 }
1649 } else {
1650 EditorNode::get_singleton()->add_io_error(TTR("Unable to update dependencies for:") + "\n" + E + "\n");
1651 }
1652 }
1653
1654 for (const String &E : scenes_to_reload) {
1655 EditorNode::get_singleton()->reload_scene(E);
1656 }
1657 }
1658
1659 void FileSystemDock::_update_project_settings_after_move(const HashMap<String, String> &p_renames, const HashMap<String, String> &p_folders_renames) {
1660 // Find all project settings of type FILE and replace them if needed.
1661 const HashMap<StringName, PropertyInfo> prop_info = ProjectSettings::get_singleton()->get_custom_property_info();
1662 for (const KeyValue<StringName, PropertyInfo> &E : prop_info) {
1663 if (E.value.hint == PROPERTY_HINT_FILE || E.value.hint == PROPERTY_HINT_FILE_PATH) {
1664 String old_path = GLOBAL_GET(E.key);
1665 if (p_renames.has(old_path)) {
1666 ProjectSettings::get_singleton()->set_setting(E.key, p_renames[old_path]);
1667 }
1668 };
1669 }
1670
1671 // Also search for the file in autoload, as they are stored differently from normal files.
1672 List<PropertyInfo> property_list;
1673 ProjectSettings::get_singleton()->get_property_list(&property_list);
1674 for (const PropertyInfo &E : property_list) {
1675 if (E.name.begins_with("autoload/")) {
1676 // If the autoload resource paths has a leading "*", it indicates that it is a Singleton,
1677 // so we have to handle both cases when updating.
1678 String autoload = GLOBAL_GET(E.name);
1679 String autoload_singleton = autoload.substr(1);
1680 if (p_renames.has(autoload)) {
1681 ProjectSettings::get_singleton()->set_setting(E.name, p_renames[autoload]);
1682 } else if (autoload.begins_with("*") && p_renames.has(autoload_singleton)) {
1683 ProjectSettings::get_singleton()->set_setting(E.name, "*" + p_renames[autoload_singleton]);
1684 }
1685 }
1686 }
1687
1688 // Update folder colors.
1689 for (const KeyValue<String, String> &rename : p_folders_renames) {
1690 if (assigned_folder_colors.has(rename.key)) {
1691 assigned_folder_colors[rename.value] = assigned_folder_colors[rename.key];
1692 assigned_folder_colors.erase(rename.key);
1693 }
1694 }
1695 ProjectSettings::get_singleton()->save();
1696 }
1697
1698 String FileSystemDock::_get_unique_name(const FileOrFolder &p_entry, const String &p_at_path) {
1699 String new_path;
1700 String new_path_base;
1701
1702 if (p_entry.is_file) {
1703 new_path = p_at_path.path_join(p_entry.path.get_file());
1704 new_path_base = new_path.get_basename() + " (%d)." + new_path.get_extension();
1705 } else {
1706 PackedStringArray path_split = p_entry.path.split("/");
1707 new_path = p_at_path.path_join(path_split[path_split.size() - 2]);
1708 new_path_base = new_path + " (%d)";
1709 }
1710
1711 int exist_counter = 1;
1712 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
1713 while (da->file_exists(new_path) || da->dir_exists(new_path)) {
1714 exist_counter++;
1715 new_path = vformat(new_path_base, exist_counter);
1716 }
1717
1718 return new_path;
1719 }
1720
1721 void FileSystemDock::_update_favorites_after_move(const HashMap<String, String> &p_files_renames, const HashMap<String, String> &p_folders_renames) const {
1722 Vector<String> favorite_files = EditorSettings::get_singleton()->get_favorites();
1723 Vector<String> new_favorite_files;
1724 for (const String &old_path : favorite_files) {
1725 if (p_folders_renames.has(old_path)) {
1726 new_favorite_files.push_back(p_folders_renames[old_path]);
1727 } else if (p_files_renames.has(old_path)) {
1728 new_favorite_files.push_back(p_files_renames[old_path]);
1729 } else {
1730 new_favorite_files.push_back(old_path);
1731 }
1732 }
1733 EditorSettings::get_singleton()->set_favorites(new_favorite_files);
1734
1735 HashMap<String, PackedStringArray> favorite_properties = EditorSettings::get_singleton()->get_favorite_properties();
1736 for (const KeyValue<String, String> &KV : p_files_renames) {
1737 if (favorite_properties.has(KV.key)) {
1738 favorite_properties.replace_key(KV.key, KV.value);
1739 }
1740 }
1741 EditorSettings::get_singleton()->set_favorite_properties(favorite_properties);
1742 }
1743
1744 void FileSystemDock::_make_scene_confirm() {
1745 const String scene_path = make_scene_dialog->get_scene_path();
1746
1747 int idx = EditorNode::get_singleton()->new_scene();
1748 EditorNode::get_editor_data().set_scene_path(idx, scene_path);
1749 EditorNode::get_singleton()->set_edited_scene(make_scene_dialog->create_scene_root());
1750 EditorNode::get_singleton()->save_scene_if_open(scene_path);
1751 }
1752
1753 void FileSystemDock::_resource_removed(const Ref<Resource> &p_resource) {
1754 const Ref<Script> &scr = p_resource;
1755 if (scr.is_valid()) {
1756 ScriptServer::remove_global_class_by_path(scr->get_path());
1757 EditorNode::get_editor_data().script_class_save_global_classes();
1758 EditorFileSystem::get_singleton()->emit_signal(SNAME("script_classes_updated"));
1759 }
1760 emit_signal(SNAME("resource_removed"), p_resource);
1761 }
1762
1763 void FileSystemDock::_file_removed(const String &p_file) {
1764 emit_signal(SNAME("file_removed"), p_file);
1765
1766 // Find the closest parent directory available, in case multiple items were deleted along the same path.
1767 current_path = p_file.get_base_dir();
1768 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
1769 while (!da->dir_exists(current_path)) {
1770 current_path = current_path.get_base_dir();
1771 }
1772
1773 current_path_line_edit->set_text(current_path);
1774 }
1775
1776 void FileSystemDock::_folder_removed(const String &p_folder) {
1777 emit_signal(SNAME("folder_removed"), p_folder);
1778
1779 // Find the closest parent directory available, in case multiple items were deleted along the same path.
1780 current_path = p_folder.get_base_dir();
1781 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
1782 while (!da->dir_exists(current_path)) {
1783 current_path = current_path.get_base_dir();
1784 }
1785
1786 // Remove assigned folder color for all subfolders.
1787 bool folder_colors_updated = false;
1788 for (const Variant &E : assigned_folder_colors.get_key_list()) {
1789 const String &path = E;
1790 // These folder paths are guaranteed to end with a "/".
1791 if (path.begins_with(p_folder)) {
1792 assigned_folder_colors.erase(path);
1793 folder_colors_updated = true;
1794 }
1795 }
1796 if (folder_colors_updated) {
1797 _update_folder_colors_setting();
1798 }
1799
1800 current_path_line_edit->set_text(current_path);
1801 EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->get_filesystem_path(current_path);
1802 if (efd) {
1803 efd->force_update();
1804 }
1805 }
1806
1807 void FileSystemDock::_rename_operation_confirm() {
1808 String new_name;
1809 TreeItem *ti = tree->get_edited();
1810 int col_index = tree->get_edited_column();
1811
1812 if (ti) {
1813 new_name = ti->get_text(col_index).strip_edges();
1814 } else {
1815 new_name = files->get_edit_text().strip_edges();
1816 }
1817 String old_name = to_rename.is_file ? to_rename.path.get_file() : to_rename.path.left(-1).get_file();
1818
1819 bool rename_error = false;
1820 if (new_name.length() == 0) {
1821 EditorNode::get_singleton()->show_warning(TTRC("No name provided."));
1822 rename_error = true;
1823 } else if (new_name.contains_char('/') || new_name.contains_char('\\') || new_name.contains_char(':')) {
1824 EditorNode::get_singleton()->show_warning(TTRC("Name contains invalid characters."));
1825 rename_error = true;
1826 } else if (new_name[0] == '.') {
1827 EditorNode::get_singleton()->show_warning(TTRC("This filename begins with a dot rendering the file invisible to the editor.\nIf you want to rename it anyway, use your operating system's file manager."));
1828 rename_error = true;
1829 } else if (to_rename.is_file && to_rename.path.get_extension() != new_name.get_extension()) {
1830 if (!EditorFileSystem::get_singleton()->get_valid_extensions().find(new_name.get_extension())) {
1831 EditorNode::get_singleton()->show_warning(TTRC("This file extension is not recognized by the editor.\nIf you want to rename it anyway, use your operating system's file manager.\nAfter renaming to an unknown extension, the file won't be shown in the editor anymore."));
1832 rename_error = true;
1833 }
1834 }
1835
1836 // Restore original name.
1837 if (rename_error) {
1838 if (ti) {
1839 ti->set_text(col_index, old_name);
1840 }
1841 return;
1842 }
1843
1844 String old_path = to_rename.path.ends_with("/") ? to_rename.path.left(-1) : to_rename.path;
1845 String new_path = old_path.get_base_dir().path_join(new_name);
1846 if (old_path == new_path) {
1847 return;
1848 }
1849
1850 if (EditorFileSystem::get_singleton()->is_group_file(old_path)) {
1851 EditorFileSystem::get_singleton()->move_group_file(old_path, new_path);
1852 }
1853
1854 // Present a more user friendly warning for name conflict.
1855 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
1856
1857 bool new_exist = (da->file_exists(new_path) || da->dir_exists(new_path));
1858 if (!da->is_case_sensitive(new_path.get_base_dir())) {
1859 new_exist = new_exist && (new_path.to_lower() != old_path.to_lower());
1860 }
1861 if (new_exist) {
1862 EditorNode::get_singleton()->show_warning(TTRC("A file or folder with this name already exists."));
1863 if (ti) {
1864 ti->set_text(col_index, old_name);
1865 }
1866 return;
1867 }
1868
1869 HashMap<String, ResourceUID::ID> uids;
1870 HashSet<String> file_owners; // The files that use these moved/renamed resource files.
1871 _before_move(uids, file_owners);
1872
1873 HashMap<String, String> file_renames;
1874 HashMap<String, String> folder_renames;
1875 _try_move_item(to_rename, new_path, file_renames, folder_renames);
1876
1877 int current_tab = EditorSceneTabs::get_singleton()->get_current_tab();
1878 _update_resource_paths_after_move(file_renames, uids);
1879 _update_dependencies_after_move(file_renames, file_owners);
1880 _update_project_settings_after_move(file_renames, folder_renames);
1881 _update_favorites_after_move(file_renames, folder_renames);
1882
1883 EditorSceneTabs::get_singleton()->set_current_tab(current_tab);
1884
1885 if (ti) {
1886 current_path = new_path;
1887 current_path_line_edit->set_text(current_path);
1888 }
1889
1890 print_verbose("FileSystem: calling rescan.");
1891 _rescan();
1892 }
1893
1894 void FileSystemDock::_duplicate_operation_confirm(const String &p_path) {
1895 const String base_dir = p_path.trim_suffix("/").get_base_dir();
1896 if (!DirAccess::dir_exists_absolute(base_dir)) {
1897 Error err = EditorFileSystem::get_singleton()->make_dir_recursive(base_dir);
1898 if (err != OK) {
1899 EditorNode::get_singleton()->show_warning(vformat(TTR("Could not create base directory: %s"), TTR(error_names[err])));
1900 return;
1901 }
1902 }
1903 _try_duplicate_item(to_duplicate, p_path);
1904 }
1905
1906 void FileSystemDock::_move_confirm() {
1907 _move_operation_confirm(confirm_move_to_dir, confirm_to_copy);
1908 }
1909
1910 void FileSystemDock::_overwrite_dialog_action(bool p_overwrite) {
1911 overwrite_dialog->hide();
1912 _move_operation_confirm(to_move_path, to_move_or_copy, p_overwrite ? OVERWRITE_REPLACE : OVERWRITE_RENAME);
1913 }
1914
1915 void FileSystemDock::_convert_dialog_action() {
1916 Vector<Ref<Resource>> selected_resources;
1917 for (const String &S : to_convert) {
1918 Ref<Resource> res = ResourceLoader::load(S);
1919 ERR_FAIL_COND(res.is_null());
1920 selected_resources.push_back(res);
1921 }
1922
1923 Vector<Ref<Resource>> converted_resources;
1924 HashSet<Ref<Resource>> resources_to_erase_history_for;
1925 for (Ref<Resource> res : selected_resources) {
1926 Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin_for_resource(res);
1927 for (const Ref<EditorResourceConversionPlugin> &conversion : conversions) {
1928 int conversion_id = 0;
1929 for (const String &target : cached_valid_conversion_targets) {
1930 if (conversion_id == selected_conversion_id && conversion->converts_to() == target) {
1931 Ref<Resource> converted_res = conversion->convert(res);
1932 ERR_FAIL_COND(converted_res.is_null());
1933 ERR_FAIL_COND(res.is_null());
1934 converted_resources.push_back(converted_res);
1935 resources_to_erase_history_for.insert(res);
1936 break;
1937 }
1938 conversion_id++;
1939 }
1940 }
1941 }
1942
1943 // Clear history for the objects being replaced.
1944 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1945 for (Ref<Resource> res : resources_to_erase_history_for) {
1946 undo_redo->clear_history(true, undo_redo->get_history_id_for_object(res.ptr()));
1947 }
1948
1949 // Updates all the resources existing as node properties.
1950 EditorNode::get_singleton()->replace_resources_in_scenes(selected_resources, converted_resources);
1951
1952 // Overwrite the old resources.
1953 for (int i = 0; i < converted_resources.size(); i++) {
1954 Ref<Resource> original_resource = selected_resources.get(i);
1955 Ref<Resource> new_resource = converted_resources.get(i);
1956
1957 // Notify plugins that the original resource is removed.
1958 emit_signal(SNAME("file_removed"), original_resource->get_path());
1959 // Overwrite the path.
1960 new_resource->set_path(original_resource->get_path(), true);
1961
1962 ResourceSaver::save(new_resource);
1963 }
1964 }
1965
1966 Vector<String> FileSystemDock::_check_existing() {
1967 Vector<String> conflicting_items;
1968 for (const FileOrFolder &item : to_move) {
1969 String old_path = item.path.trim_suffix("/");
1970 String new_path = to_move_path.path_join(old_path.get_file());
1971
1972 if ((item.is_file && FileAccess::exists(new_path)) || (!item.is_file && DirAccess::exists(new_path))) {
1973 conflicting_items.push_back(old_path);
1974 }
1975 }
1976 return conflicting_items;
1977 }
1978
1979 void FileSystemDock::_move_operation_confirm(const String &p_to_path, bool p_copy, Overwrite p_overwrite) {
1980 if (p_overwrite == OVERWRITE_UNDECIDED) {
1981 to_move_path = p_to_path;
1982 to_move_or_copy = p_copy;
1983
1984 Vector<String> conflicting_items = _check_existing();
1985 if (!conflicting_items.is_empty()) {
1986 // Ask to do something.
1987 overwrite_dialog_header->set_text(vformat(
1988 TTR("The following files or folders conflict with items in the target location '%s':"), to_move_path));
1989 overwrite_dialog_file_list->set_text(String("\n").join(conflicting_items));
1990 overwrite_dialog_footer->set_text(
1991 p_copy ? TTRC("Do you wish to overwrite them or rename the copied files?")
1992 : TTRC("Do you wish to overwrite them or rename the moved files?"));
1993 overwrite_dialog->popup_centered();
1994 return;
1995 }
1996 }
1997
1998 Vector<String> new_paths;
1999 new_paths.resize(to_move.size());
2000 for (int i = 0; i < to_move.size(); i++) {
2001 if (p_overwrite == OVERWRITE_RENAME) {
2002 new_paths.write[i] = _get_unique_name(to_move[i], p_to_path);
2003 } else {
2004 new_paths.write[i] = p_to_path.path_join(to_move[i].path.trim_suffix("/").get_file());
2005 }
2006 }
2007
2008 if (p_copy) {
2009 for (int i = 0; i < to_move.size(); i++) {
2010 if (to_move[i].path != new_paths[i]) {
2011 _try_duplicate_item(to_move[i], new_paths[i]);
2012 select_after_scan = new_paths[i];
2013 }
2014 }
2015 } else {
2016 // Check groups.
2017 for (int i = 0; i < to_move.size(); i++) {
2018 if (to_move[i].is_file && EditorFileSystem::get_singleton()->is_group_file(to_move[i].path)) {
2019 EditorFileSystem::get_singleton()->move_group_file(to_move[i].path, new_paths[i]);
2020 }
2021 }
2022
2023 HashMap<String, ResourceUID::ID> uids;
2024 HashSet<String> file_owners; // The files that use these moved/renamed resource files.
2025 _before_move(uids, file_owners);
2026
2027 bool is_moved = false;
2028 HashMap<String, String> file_renames;
2029 HashMap<String, String> folder_renames;
2030
2031 for (int i = 0; i < to_move.size(); i++) {
2032 if (to_move[i].path != new_paths[i]) {
2033 _try_move_item(to_move[i], new_paths[i], file_renames, folder_renames);
2034 is_moved = true;
2035 }
2036 }
2037
2038 if (is_moved) {
2039 Vector<String> files_to_update;
2040 for (KeyValue<String, String> &E : file_renames) {
2041 if (!files_to_update.has(E.key)) {
2042 files_to_update.push_back(E.key);
2043 }
2044 if (!files_to_update.has(E.value)) {
2045 files_to_update.push_back(E.value);
2046 }
2047 }
2048 print_verbose("FileSystem: updating file infos.");
2049 EditorFileSystem::get_singleton()->update_files(files_to_update);
2050
2051 int current_tab = EditorSceneTabs::get_singleton()->get_current_tab();
2052 _update_resource_paths_after_move(file_renames, uids);
2053 _update_dependencies_after_move(file_renames, file_owners);
2054 _update_project_settings_after_move(file_renames, folder_renames);
2055 _update_favorites_after_move(file_renames, folder_renames);
2056
2057 EditorSceneTabs::get_singleton()->set_current_tab(current_tab);
2058
2059 print_verbose("FileSystem: calling rescan.");
2060 _rescan();
2061
2062 current_path = p_to_path;
2063 current_path_line_edit->set_text(current_path);
2064 }
2065 }
2066 }
2067
2068 void FileSystemDock::_before_move(HashMap<String, ResourceUID::ID> &r_uids, HashSet<String> &r_file_owners) const {
2069 HashSet<String> renamed_files;
2070 for (int i = 0; i < to_move.size(); i++) {
2071 if (to_move[i].is_file) {
2072 renamed_files.insert(to_move[i].path);
2073 ResourceUID::ID uid = ResourceLoader::get_resource_uid(to_move[i].path);
2074 if (uid != ResourceUID::INVALID_ID) {
2075 r_uids[to_move[i].path] = uid;
2076 }
2077 } else {
2078 EditorFileSystemDirectory *current_folder = EditorFileSystem::get_singleton()->get_filesystem_path(to_move[i].path);
2079 ERR_CONTINUE(current_folder == nullptr);
2080 List<EditorFileSystemDirectory *> folders;
2081 folders.push_back(current_folder);
2082 while (folders.front()) {
2083 current_folder = folders.front()->get();
2084 for (int j = 0; j < current_folder->get_file_count(); j++) {
2085 const String file_path = current_folder->get_file_path(j);
2086 renamed_files.insert(file_path);
2087 ResourceUID::ID uid = ResourceLoader::get_resource_uid(file_path);
2088 if (uid != ResourceUID::INVALID_ID) {
2089 r_uids[file_path] = uid;
2090 }
2091 }
2092 for (int j = 0; j < current_folder->get_subdir_count(); j++) {
2093 folders.push_back(current_folder->get_subdir(j));
2094 }
2095 folders.pop_front();
2096 }
2097 }
2098 }
2099
2100 // Look for files that use these moved/renamed resource files.
2101 _find_file_owners(EditorFileSystem::get_singleton()->get_filesystem(), renamed_files, r_file_owners);
2102
2103 // Open scenes with dependencies on the ones about to be moved will be reloaded,
2104 // so save them first to prevent losing unsaved changes.
2105 EditorNode::get_singleton()->save_scene_list(r_file_owners);
2106 }
2107
2108 Vector<String> FileSystemDock::_tree_get_selected(bool remove_self_inclusion, bool p_include_unselected_cursor) const {
2109 // Build a list of selected items with the active one at the first position.
2110 Vector<String> selected_strings;
2111
2112 TreeItem *cursor_item = tree->get_selected();
2113 if (cursor_item && (p_include_unselected_cursor || cursor_item->is_selected(0)) && cursor_item != favorites_item) {
2114 selected_strings.push_back(cursor_item->get_metadata(0));
2115 }
2116
2117 TreeItem *selected = tree->get_root();
2118 selected = tree->get_next_selected(selected);
2119 while (selected) {
2120 if (selected != cursor_item && selected != favorites_item && selected->is_visible_in_tree()) {
2121 selected_strings.push_back(selected->get_metadata(0));
2122 }
2123 selected = tree->get_next_selected(selected);
2124 }
2125
2126 if (remove_self_inclusion) {
2127 selected_strings = _remove_self_included_paths(selected_strings);
2128 }
2129 return selected_strings;
2130 }
2131
2132 Vector<String> FileSystemDock::_file_list_get_selected() const {
2133 Vector<int> selected_ids = files->get_selected_items();
2134 Vector<String> selected;
2135 selected.resize(selected_ids.size());
2136
2137 String *selected_write = selected.ptrw();
2138 int i = 0;
2139 for (const int id : selected_ids) {
2140 selected_write[i] = files->get_item_metadata(id);
2141 i++;
2142 }
2143 return selected;
2144 }
2145
2146 Vector<String> FileSystemDock::_remove_self_included_paths(Vector<String> selected_strings) {
2147 // Remove paths or files that are included into another.
2148 if (selected_strings.size() > 1) {
2149 selected_strings.sort_custom<FileNoCaseComparator>();
2150 String last_path = "";
2151 for (int i = 0; i < selected_strings.size(); i++) {
2152 if (!last_path.is_empty() && selected_strings[i].begins_with(last_path)) {
2153 selected_strings.remove_at(i);
2154 i--;
2155 }
2156 if (selected_strings[i].ends_with("/")) {
2157 last_path = selected_strings[i];
2158 }
2159 }
2160 }
2161 return selected_strings;
2162 }
2163
2164 void FileSystemDock::_tree_rmb_option(int p_option) {
2165 if (p_option > FILE_MENU_MAX && p_option < CONVERT_BASE_ID) {
2166 // Extra options don't need paths.
2167 _file_option(p_option, {});
2168 return;
2169 }
2170
2171 Vector<String> selected_strings = _tree_get_selected(false);
2172
2173 // Execute the current option.
2174 switch (p_option) {
2175 case FILE_MENU_EXPAND_ALL:
2176 case FILE_MENU_COLLAPSE_ALL: {
2177 // Expand or collapse the folder
2178 if (selected_strings.size() == 1) {
2179 tree->get_selected()->set_collapsed_recursive(p_option == FILE_MENU_COLLAPSE_ALL);
2180 }
2181 } break;
2182 case FILE_MENU_RENAME: {
2183 selected_strings = _tree_get_selected(false, true);
2184 [[fallthrough]];
2185 }
2186 default: {
2187 _file_option(p_option, selected_strings);
2188 } break;
2189 }
2190 }
2191
2192 void FileSystemDock::_file_list_rmb_option(int p_option) {
2193 if (p_option > FILE_MENU_MAX && p_option < CONVERT_BASE_ID) {
2194 // Extra options don't need paths.
2195 _file_option(p_option, {});
2196 return;
2197 }
2198 _file_option(p_option, _file_list_get_selected());
2199 }
2200
2201 void FileSystemDock::_generic_rmb_option_selected(int p_option) {
2202 // Used for submenu commands where we don't know whether we're
2203 // calling from the file_list_rmb menu or the _tree_rmb option.
2204 if (files->has_focus()) {
2205 _file_list_rmb_option(p_option);
2206 } else {
2207 _tree_rmb_option(p_option);
2208 }
2209 }
2210
2211 void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected) {
2212 // The first one should be the active item.
2213
2214 switch (p_option) {
2215 case FILE_MENU_SHOW_IN_EXPLORER: {
2216 // Show the file/folder in the OS explorer.
2217 String fpath = current_path;
2218 if (current_path == "Favorites") {
2219 if (p_selected.is_empty()) {
2220 return;
2221 }
2222 fpath = p_selected[0];
2223 }
2224
2225 String dir = ProjectSettings::get_singleton()->globalize_path(fpath);
2226 OS::get_singleton()->shell_show_in_file_manager(dir, true);
2227 } break;
2228
2229 case FILE_MENU_OPEN_EXTERNAL: {
2230 String fpath = current_path;
2231 if (current_path == "Favorites") {
2232 if (p_selected.is_empty()) {
2233 return;
2234 }
2235 fpath = p_selected[0];
2236 }
2237
2238 const String file = ProjectSettings::get_singleton()->globalize_path(fpath);
2239 const String extension = file.get_extension();
2240
2241 const String resource_type = ResourceLoader::get_resource_type(fpath);
2242 String external_program;
2243
2244 if (ClassDB::is_parent_class(resource_type, "Script") || extension == "tres" || extension == "tscn") {
2245 external_program = EDITOR_GET("text_editor/external/exec_path");
2246 } else if (extension == "res" || extension == "scn") {
2247 // Binary resources have no meaningful editor outside Godot, so just fallback to something default.
2248 } else if (resource_type == "CompressedTexture2D" || resource_type == "Image") {
2249 if (extension == "svg" || extension == "svgz") {
2250 external_program = EDITOR_GET("filesystem/external_programs/vector_image_editor");
2251 } else {
2252 external_program = EDITOR_GET("filesystem/external_programs/raster_image_editor");
2253 }
2254 } else if (ClassDB::is_parent_class(resource_type, "AudioStream")) {
2255 external_program = EDITOR_GET("filesystem/external_programs/audio_editor");
2256 } else if (resource_type == "PackedScene") {
2257 external_program = EDITOR_GET("filesystem/external_programs/3d_model_editor");
2258 }
2259
2260 if (external_program.is_empty()) {
2261 OS::get_singleton()->shell_open(file);
2262 } else {
2263 List<String> paths;
2264 paths.push_back(file);
2265 OS::get_singleton()->open_with_program(external_program, paths);
2266 }
2267 } break;
2268
2269 case FILE_MENU_OPEN_IN_TERMINAL: {
2270 String fpath = current_path;
2271 if (current_path == "Favorites") {
2272 if (p_selected.is_empty()) {
2273 return;
2274 }
2275 fpath = p_selected[0];
2276 }
2277
2278 Vector<String> terminal_emulators;
2279 const String terminal_emulator_setting = EDITOR_GET("filesystem/external_programs/terminal_emulator");
2280 if (terminal_emulator_setting.is_empty()) {
2281 // Figure out a default terminal emulator to use.
2282 #if defined(WINDOWS_ENABLED)
2283 // Default to PowerShell as done by Windows 10 and later.
2284 terminal_emulators.push_back("powershell");
2285 #elif defined(MACOS_ENABLED)
2286 // NOTE: To avoid duplicating the Terminal icon on the Dock, we will use the `open` command
2287 // rather than directly launching the Terminal.app bundle.
2288 terminal_emulators.push_back("open");
2289 #elif defined(LINUXBSD_ENABLED)
2290 // Try terminal emulators that ship with common Linux distributions first.
2291 terminal_emulators.push_back("gnome-terminal");
2292 terminal_emulators.push_back("konsole");
2293 terminal_emulators.push_back("xfce4-terminal");
2294 terminal_emulators.push_back("lxterminal");
2295 terminal_emulators.push_back("kitty");
2296 terminal_emulators.push_back("alacritty");
2297 terminal_emulators.push_back("urxvt");
2298 terminal_emulators.push_back("xterm");
2299 #endif
2300 } else {
2301 // Use the user-specified terminal.
2302 terminal_emulators.push_back(terminal_emulator_setting);
2303 }
2304
2305 String flags = EDITOR_GET("filesystem/external_programs/terminal_emulator_flags");
2306 String arguments = flags;
2307 if (arguments.is_empty()) {
2308 // NOTE: This default value is ignored further below if the terminal executable is `powershell` or `cmd`,
2309 // due to these terminals requiring nonstandard syntax to start in a specified folder.
2310 arguments = "{directory}";
2311 }
2312
2313 #ifdef LINUXBSD_ENABLED
2314 String chosen_terminal_emulator;
2315 for (const String &terminal_emulator : terminal_emulators) {
2316 String pipe;
2317 List<String> test_args; // Required for `execute()`, as it doesn't accept `Vector<String>`.
2318 test_args.push_back("-cr");
2319 test_args.push_back("command -v " + terminal_emulator);
2320 const Error err = OS::get_singleton()->execute("bash", test_args, &pipe);
2321 // Check if a path to the terminal executable exists.
2322 if (err == OK && pipe.contains_char('/')) {
2323 chosen_terminal_emulator = terminal_emulator;
2324 break;
2325 } else if (err == ERR_CANT_FORK) {
2326 ERR_PRINT_ED(vformat(TTR("Couldn't run external program to check for terminal emulator presence: command -v %s"), terminal_emulator));
2327 }
2328 }
2329 #else
2330 // On Windows and macOS, the first (and only) terminal emulator in the list is always available.
2331 String chosen_terminal_emulator = terminal_emulators[0];
2332 #endif
2333
2334 List<String> terminal_emulator_args; // Required for `execute()`, as it doesn't accept `Vector<String>`.
2335 bool append_default_args = true;
2336
2337 #ifdef LINUXBSD_ENABLED
2338 // Prepend default arguments based on the terminal emulator name.
2339 // Use `String.ends_with()` so that installations in non-default paths
2340 // or `/usr/local/bin` are detected correctly.
2341 if (flags.is_empty()) {
2342 if (chosen_terminal_emulator.ends_with("konsole")) {
2343 terminal_emulator_args.push_back("--workdir");
2344 } else if (chosen_terminal_emulator.ends_with("gnome-terminal")) {
2345 terminal_emulator_args.push_back("--working-directory");
2346 } else if (chosen_terminal_emulator.ends_with("urxvt")) {
2347 terminal_emulator_args.push_back("-cd");
2348 } else if (chosen_terminal_emulator.ends_with("xfce4-terminal")) {
2349 terminal_emulator_args.push_back("--working-directory");
2350 } else if (chosen_terminal_emulator.ends_with("lxterminal")) {
2351 terminal_emulator_args.push_back("--working-directory={directory}");
2352 append_default_args = false;
2353 } else if (chosen_terminal_emulator.ends_with("kitty")) {
2354 terminal_emulator_args.push_back("--directory");
2355 } else if (chosen_terminal_emulator.ends_with("alacritty")) {
2356 terminal_emulator_args.push_back("--working-directory");
2357 } else if (chosen_terminal_emulator.ends_with("xterm")) {
2358 terminal_emulator_args.push_back("-e");
2359 terminal_emulator_args.push_back("cd '{directory}' && exec $SHELL");
2360 append_default_args = false;
2361 }
2362 }
2363 #endif
2364
2365 #ifdef MACOS_ENABLED
2366 if (terminal_emulator_setting.is_empty()) {
2367 terminal_emulator_args.push_back("-b");
2368 terminal_emulator_args.push_back("com.apple.terminal");
2369 }
2370 #endif
2371
2372 #ifdef WINDOWS_ENABLED
2373 // Prepend default arguments based on the terminal emulator name.
2374 // Use `String.get_basename().to_lower()` to handle Windows' case-insensitive paths
2375 // with optional file extensions for executables in `PATH`.
2376 if (chosen_terminal_emulator.get_basename().to_lower() == "powershell") {
2377 terminal_emulator_args.push_back("-noexit");
2378 terminal_emulator_args.push_back("-command");
2379 terminal_emulator_args.push_back("cd '{directory}'");
2380 append_default_args = false;
2381 } else if (chosen_terminal_emulator.get_basename().to_lower() == "cmd") {
2382 terminal_emulator_args.push_back("/K");
2383 terminal_emulator_args.push_back("cd /d {directory}");
2384 append_default_args = false;
2385 }
2386 #endif
2387
2388 Vector<String> arguments_array = arguments.split(" ");
2389 for (const String &argument : arguments_array) {
2390 if (!append_default_args && argument == "{directory}") {
2391 // Prevent appending a `{directory}` placeholder twice when using powershell or cmd.
2392 // This allows users to enter the path to cmd or PowerShell in the custom terminal emulator path,
2393 // and make it work without having to enter custom arguments.
2394 continue;
2395 }
2396 terminal_emulator_args.push_back(argument);
2397 }
2398
2399 const bool is_directory = fpath.ends_with("/");
2400 for (String &terminal_emulator_arg : terminal_emulator_args) {
2401 if (is_directory) {
2402 terminal_emulator_arg = terminal_emulator_arg.replace("{directory}", ProjectSettings::get_singleton()->globalize_path(fpath));
2403 } else {
2404 terminal_emulator_arg = terminal_emulator_arg.replace("{directory}", ProjectSettings::get_singleton()->globalize_path(fpath).get_base_dir());
2405 }
2406 }
2407
2408 if (OS::get_singleton()->is_stdout_verbose()) {
2409 // Print full command line to help with troubleshooting.
2410 String command_string = chosen_terminal_emulator;
2411 for (const String &arg : terminal_emulator_args) {
2412 command_string += " " + arg;
2413 }
2414 print_line("Opening terminal emulator:", command_string);
2415 }
2416
2417 const Error err = OS::get_singleton()->create_process(chosen_terminal_emulator, terminal_emulator_args, nullptr, true);
2418 if (err != OK) {
2419 String args_string;
2420 for (const String &terminal_emulator_arg : terminal_emulator_args) {
2421 args_string += terminal_emulator_arg;
2422 }
2423 ERR_PRINT_ED(vformat(TTR("Couldn't run external terminal program (error code %d): %s %s\nCheck `filesystem/external_programs/terminal_emulator` and `filesystem/external_programs/terminal_emulator_flags` in the Editor Settings."), err, chosen_terminal_emulator, args_string));
2424 }
2425 } break;
2426
2427 case FILE_MENU_OPEN: {
2428 // Open folders.
2429 TreeItem *selected = tree->get_root();
2430 selected = tree->get_next_selected(selected);
2431 while (selected) {
2432 if (p_selected.has(selected->get_metadata(0))) {
2433 selected->set_collapsed(false);
2434 }
2435 selected = tree->get_next_selected(selected);
2436 }
2437 // Open the file.
2438 for (int i = 0; i < p_selected.size(); i++) {
2439 _select_file(p_selected[i]);
2440 }
2441 } break;
2442
2443 case FILE_MENU_INHERIT: {
2444 // Create a new scene inherited from the selected one.
2445 if (p_selected.size() == 1) {
2446 emit_signal(SNAME("inherit"), p_selected[0]);
2447 }
2448 } break;
2449
2450 case FILE_MENU_MAIN_SCENE: {
2451 // Set as main scene with selected scene file.
2452 if (p_selected.size() == 1) {
2453 main_scene_path = ResourceUID::path_to_uid(p_selected[0]);
2454 ProjectSettings::get_singleton()->set("application/run/main_scene", main_scene_path);
2455 ProjectSettings::get_singleton()->save();
2456 _update_tree(get_uncollapsed_paths());
2457 _update_file_list(true);
2458 }
2459 } break;
2460
2461 case FILE_MENU_INSTANTIATE: {
2462 // Instantiate all selected scenes.
2463 Vector<String> paths;
2464 for (int i = 0; i < p_selected.size(); i++) {
2465 const String &fpath = p_selected[i];
2466 if (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene") {
2467 paths.push_back(fpath);
2468 }
2469 }
2470 if (!paths.is_empty()) {
2471 emit_signal(SNAME("instantiate"), paths);
2472 }
2473 } break;
2474
2475 case FILE_MENU_ADD_FAVORITE: {
2476 // Add the files from favorites.
2477 Vector<String> favorites_list = EditorSettings::get_singleton()->get_favorites();
2478 for (int i = 0; i < p_selected.size(); i++) {
2479 if (!favorites_list.has(p_selected[i])) {
2480 favorites_list.push_back(p_selected[i]);
2481 }
2482 }
2483 EditorSettings::get_singleton()->set_favorites(favorites_list);
2484 } break;
2485
2486 case FILE_MENU_REMOVE_FAVORITE: {
2487 // Remove the files from favorites.
2488 Vector<String> favorites_list = EditorSettings::get_singleton()->get_favorites();
2489 for (int i = 0; i < p_selected.size(); i++) {
2490 favorites_list.erase(p_selected[i]);
2491 }
2492 EditorSettings::get_singleton()->set_favorites(favorites_list);
2493 } break;
2494
2495 case FILE_MENU_SHOW_IN_FILESYSTEM: {
2496 if (!p_selected.is_empty()) {
2497 navigate_to_path(p_selected[0]);
2498 }
2499 } break;
2500
2501 case FILE_MENU_DEPENDENCIES: {
2502 // Checkout the file dependencies.
2503 if (!p_selected.is_empty()) {
2504 const String &fpath = p_selected[0];
2505 deps_editor->edit(fpath);
2506 }
2507 } break;
2508
2509 case FILE_MENU_OWNERS: {
2510 // Checkout the file owners.
2511 if (!p_selected.is_empty()) {
2512 const String &fpath = p_selected[0];
2513 owners_editor->show(fpath);
2514 }
2515 } break;
2516
2517 case FILE_MENU_MOVE: {
2518 // Move or copy the files to a given location.
2519 to_move.clear();
2520 Vector<String> collapsed_paths = _remove_self_included_paths(p_selected);
2521 for (int i = collapsed_paths.size() - 1; i >= 0; i--) {
2522 const String &fpath = collapsed_paths[i];
2523 if (fpath != "res://") {
2524 to_move.push_back(FileOrFolder(fpath, !fpath.ends_with("/")));
2525 }
2526 }
2527 if (to_move.size() > 0) {
2528 move_dialog->config(p_selected);
2529 move_dialog->popup_centered_ratio(0.4);
2530 }
2531 } break;
2532
2533 case FILE_MENU_RENAME: {
2534 if (!p_selected.is_empty()) {
2535 // Set to_rename variable for callback execution.
2536 to_rename.path = p_selected[0];
2537 to_rename.is_file = !to_rename.path.ends_with("/");
2538 if (to_rename.path == "res://") {
2539 break;
2540 }
2541
2542 // Rename has same logic as move for resource files.
2543 to_move.clear();
2544 to_move.push_back(to_rename);
2545
2546 if (tree->has_focus()) {
2547 tree->grab_focus(!tree->has_focus(true));
2548 // Edit node in Tree.
2549 tree->edit_selected(true);
2550
2551 if (to_rename.is_file) {
2552 String name = to_rename.path.get_file();
2553 tree->set_editor_selection(0, name.rfind_char('.'));
2554 } else {
2555 String name = to_rename.path.left(-1).get_file(); // Removes the "/" suffix for folders.
2556 tree->set_editor_selection(0, name.length());
2557 }
2558 } else if (files->has_focus()) {
2559 files->edit_selected();
2560 }
2561 }
2562 } break;
2563
2564 case FILE_MENU_REMOVE: {
2565 // Remove the selected files.
2566 Vector<String> remove_files;
2567 Vector<String> remove_folders;
2568 Vector<String> collapsed_paths = _remove_self_included_paths(p_selected);
2569
2570 for (int i = 0; i < collapsed_paths.size(); i++) {
2571 const String &fpath = collapsed_paths[i];
2572 if (fpath != "res://") {
2573 if (fpath.ends_with("/")) {
2574 remove_folders.push_back(fpath);
2575 } else {
2576 remove_files.push_back(fpath);
2577 }
2578 }
2579 }
2580
2581 if (remove_files.size() + remove_folders.size() > 0) {
2582 remove_dialog->show(remove_folders, remove_files);
2583 }
2584 } break;
2585
2586 case FILE_MENU_DUPLICATE: {
2587 if (p_selected.size() != 1) {
2588 return;
2589 }
2590
2591 to_duplicate.path = p_selected[0];
2592 to_duplicate.is_file = !to_duplicate.path.ends_with("/");
2593 if (to_duplicate.is_file) {
2594 String name = to_duplicate.path.get_file();
2595 make_dir_dialog->config(to_duplicate.path.get_base_dir(), callable_mp(this, &FileSystemDock::_duplicate_operation_confirm),
2596 DirectoryCreateDialog::MODE_FILE, TTR("Duplicating file:") + " " + name, name);
2597 } else {
2598 String name = to_duplicate.path.trim_suffix("/").get_file();
2599 make_dir_dialog->config(to_duplicate.path.trim_suffix("/").get_base_dir(), callable_mp(this, &FileSystemDock::_duplicate_operation_confirm),
2600 DirectoryCreateDialog::MODE_DIRECTORY, TTR("Duplicating folder:") + " " + name, name);
2601 }
2602 make_dir_dialog->popup_centered();
2603 } break;
2604
2605 case FILE_MENU_REIMPORT: {
2606 ImportDock::get_singleton()->reimport_resources(p_selected);
2607 } break;
2608
2609 case FILE_MENU_NEW_FOLDER: {
2610 String directory = current_path;
2611 if (!directory.ends_with("/")) {
2612 directory = directory.get_base_dir();
2613 }
2614 make_dir_dialog->config(directory, callable_mp(this, &FileSystemDock::create_directory).bind(directory),
2615 DirectoryCreateDialog::MODE_DIRECTORY, TTR("Create Folder"), "new folder");
2616 make_dir_dialog->popup_centered();
2617 } break;
2618
2619 case FILE_MENU_NEW_SCENE: {
2620 String directory = current_path;
2621 if (!directory.ends_with("/")) {
2622 directory = directory.get_base_dir();
2623 }
2624 make_scene_dialog->config(directory);
2625 make_scene_dialog->popup_centered();
2626 } break;
2627
2628 case FILE_MENU_NEW_SCRIPT: {
2629 String fpath = current_path;
2630 if (!fpath.ends_with("/")) {
2631 fpath = fpath.get_base_dir();
2632 }
2633 make_script_dialog->config("Node", fpath.path_join("new_script.gd"), false, false);
2634 make_script_dialog->popup_centered();
2635 } break;
2636
2637 case FILE_MENU_COPY_PATH: {
2638 if (!p_selected.is_empty()) {
2639 const String &fpath = p_selected[0];
2640 DisplayServer::get_singleton()->clipboard_set(fpath);
2641 }
2642 } break;
2643
2644 case FILE_MENU_COPY_ABSOLUTE_PATH: {
2645 if (!p_selected.is_empty()) {
2646 const String &fpath = p_selected[0];
2647 const String absolute_path = ProjectSettings::get_singleton()->globalize_path(fpath);
2648 DisplayServer::get_singleton()->clipboard_set(absolute_path);
2649 }
2650 } break;
2651
2652 case FILE_MENU_COPY_UID: {
2653 if (!p_selected.is_empty()) {
2654 ResourceUID::ID uid = ResourceLoader::get_resource_uid(p_selected[0]);
2655 if (uid != ResourceUID::INVALID_ID) {
2656 String uid_string = ResourceUID::get_singleton()->id_to_text(uid);
2657 DisplayServer::get_singleton()->clipboard_set(uid_string);
2658 }
2659 }
2660 } break;
2661
2662 case FILE_MENU_NEW_RESOURCE: {
2663 new_resource_dialog->popup_create(true);
2664 } break;
2665 case FILE_MENU_NEW_TEXTFILE: {
2666 String fpath = current_path;
2667 if (!fpath.ends_with("/")) {
2668 fpath = fpath.get_base_dir();
2669 }
2670 String dir = ProjectSettings::get_singleton()->globalize_path(fpath);
2671 ScriptEditor::get_singleton()->open_text_file_create_dialog(dir);
2672 } break;
2673 case FILE_MENU_RUN_SCRIPT: {
2674 if (p_selected.size() == 1) {
2675 const String &fpath = p_selected[0];
2676 Ref<Script> scr = ResourceLoader::load(fpath);
2677 ERR_FAIL_COND(scr.is_null());
2678 EditorNode::get_singleton()->run_editor_script(scr);
2679 }
2680 } break;
2681
2682 case EXTRA_FOCUS_PATH: {
2683 focus_on_filter();
2684 } break;
2685 case EXTRA_FOCUS_FILTER: {
2686 focus_on_filter();
2687 } break;
2688
2689 default: {
2690 if (p_option >= EditorContextMenuPlugin::BASE_ID) {
2691 if (!EditorContextMenuPluginManager::get_singleton()->activate_custom_option(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM, p_option, p_selected)) {
2692 // For create new file option, pass the path location of mouse click position instead, to plugin callback.
2693 String fpath = current_path;
2694 if (!fpath.ends_with("/")) {
2695 fpath = fpath.get_base_dir();
2696 }
2697 EditorContextMenuPluginManager::get_singleton()->activate_custom_option(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, p_option, { fpath });
2698 }
2699 } else if (p_option >= CONVERT_BASE_ID) {
2700 selected_conversion_id = p_option - CONVERT_BASE_ID;
2701 ERR_FAIL_INDEX(selected_conversion_id, (int)cached_valid_conversion_targets.size());
2702
2703 to_convert.clear();
2704 for (const String &S : p_selected) {
2705 to_convert.push_back(S);
2706 }
2707
2708 int conversion_id = 0;
2709 for (const String &E : cached_valid_conversion_targets) {
2710 if (conversion_id == selected_conversion_id) {
2711 conversion_dialog->set_text(vformat(TTR("Do you wish to convert these files to %s? (This operation cannot be undone!)"), E));
2712 conversion_dialog->popup_centered();
2713 break;
2714 }
2715 conversion_id++;
2716 }
2717 }
2718 break;
2719 }
2720 }
2721 }
2722
2723 int FileSystemDock::_get_menu_option_from_key(const Ref<InputEventKey> &p_key) {
2724 if (ED_IS_SHORTCUT("filesystem_dock/duplicate", p_key)) {
2725 return FILE_MENU_DUPLICATE;
2726 } else if (ED_IS_SHORTCUT("filesystem_dock/copy_path", p_key)) {
2727 return FILE_MENU_COPY_PATH;
2728 } else if (ED_IS_SHORTCUT("filesystem_dock/copy_absolute_path", p_key)) {
2729 return FILE_MENU_COPY_ABSOLUTE_PATH;
2730 } else if (ED_IS_SHORTCUT("filesystem_dock/copy_uid", p_key)) {
2731 return FILE_MENU_COPY_UID;
2732 } else if (ED_IS_SHORTCUT("filesystem_dock/delete", p_key)) {
2733 return FILE_MENU_REMOVE;
2734 } else if (ED_IS_SHORTCUT("filesystem_dock/new_folder", p_key)) {
2735 return FILE_MENU_NEW_FOLDER;
2736 } else if (ED_IS_SHORTCUT("filesystem_dock/new_scene", p_key)) {
2737 return FILE_MENU_NEW_SCENE;
2738 } else if (ED_IS_SHORTCUT("filesystem_dock/new_script", p_key)) {
2739 return FILE_MENU_NEW_SCRIPT;
2740 } else if (ED_IS_SHORTCUT("filesystem_dock/new_resource", p_key)) {
2741 return FILE_MENU_NEW_RESOURCE;
2742 } else if (ED_IS_SHORTCUT("filesystem_dock/new_textfile", p_key)) {
2743 return FILE_MENU_NEW_TEXTFILE;
2744 } else if (ED_IS_SHORTCUT("filesystem_dock/rename", p_key)) {
2745 return FILE_MENU_RENAME;
2746 } else if (ED_IS_SHORTCUT("filesystem_dock/show_in_explorer", p_key)) {
2747 return FILE_MENU_SHOW_IN_EXPLORER;
2748 } else if (ED_IS_SHORTCUT("filesystem_dock/open_in_external_program", p_key)) {
2749 return FILE_MENU_OPEN_EXTERNAL;
2750 } else if (ED_IS_SHORTCUT("filesystem_dock/open_in_terminal", p_key)) {
2751 return FILE_MENU_OPEN_IN_TERMINAL;
2752 } else if (ED_IS_SHORTCUT("filesystem_dock/focus_path", p_key)) {
2753 return EXTRA_FOCUS_PATH;
2754 } else if (ED_IS_SHORTCUT("editor/open_search", p_key)) {
2755 return EXTRA_FOCUS_FILTER;
2756 }
2757 return -1;
2758 }
2759
2760 void FileSystemDock::_resource_created() {
2761 String fpath = current_path;
2762 if (!fpath.ends_with("/")) {
2763 fpath = fpath.get_base_dir();
2764 }
2765
2766 const String type_name = new_resource_dialog->get_selected_type();
2767 if (type_name == "Shader") {
2768 make_shader_dialog->config(fpath.path_join("new_shader"), false, false, type_name);
2769 make_shader_dialog->popup_centered();
2770 return;
2771 } else if (type_name == "ShaderInclude") {
2772 make_shader_dialog->config(fpath.path_join("new_shader_include"), false, false, type_name);
2773 make_shader_dialog->popup_centered();
2774 return;
2775 } else if (type_name == "VisualShader") {
2776 make_shader_dialog->config(fpath.path_join("new_shader"), false, false, type_name);
2777 make_shader_dialog->popup_centered();
2778 return;
2779 } else if (ClassDB::is_parent_class(type_name, "Script")) {
2780 make_script_dialog->config("Node", fpath.path_join("new_script"), false, false);
2781 make_script_dialog->popup_centered();
2782 return;
2783 }
2784
2785 Variant c = new_resource_dialog->instantiate_selected();
2786
2787 ERR_FAIL_COND(!c);
2788 Resource *r = Object::cast_to<Resource>(c);
2789 ERR_FAIL_NULL(r);
2790
2791 PackedScene *scene = Object::cast_to<PackedScene>(r);
2792 if (scene) {
2793 Node *node = memnew(Node);
2794 node->set_name("Node");
2795 scene->pack(node);
2796 memdelete(node);
2797 }
2798
2799 EditorNode::get_singleton()->push_item(r);
2800 EditorNode::get_singleton()->save_resource_as(Ref<Resource>(r), fpath);
2801 }
2802
2803 void FileSystemDock::_script_or_shader_created(const Ref<Resource> &p_resource) {
2804 EditorNode::get_singleton()->push_item(p_resource.ptr());
2805 }
2806
2807 void FileSystemDock::_search_changed(const String &p_text, const Control *p_from) {
2808 if (searched_tokens.is_empty()) {
2809 // Register the uncollapsed paths before they change.
2810 uncollapsed_paths_before_search = get_uncollapsed_paths();
2811 }
2812
2813 const String searched_string = p_text.to_lower();
2814 if (searched_string.begins_with("uid://")) {
2815 ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(searched_string);
2816 if (id != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(id)) {
2817 navigate_to_path(ResourceUID::get_singleton()->get_id_path(id));
2818 return;
2819 }
2820 }
2821
2822 searched_tokens = searched_string.split(" ", false);
2823
2824 if (p_from == tree_search_box) {
2825 file_list_search_box->set_text(searched_string);
2826 } else { // File_list_search_box.
2827 tree_search_box->set_text(searched_string);
2828 }
2829
2830 _update_filtered_items();
2831 if (display_mode == DISPLAY_MODE_HSPLIT || display_mode == DISPLAY_MODE_VSPLIT) {
2832 _update_file_list(false);
2833 }
2834 if (searched_tokens.is_empty()) {
2835 _navigate_to_path(current_path);
2836 }
2837 }
2838
2839 bool FileSystemDock::_matches_all_search_tokens(const String &p_text) {
2840 if (searched_tokens.is_empty()) {
2841 return false;
2842 }
2843 const String s = p_text.to_lower();
2844 for (const String &t : searched_tokens) {
2845 if (!s.contains(t)) {
2846 return false;
2847 }
2848 }
2849 return true;
2850 }
2851
2852 void FileSystemDock::_rescan() {
2853 if (tree->has_focus()) {
2854 had_focus = tree;
2855 } else if (files->has_focus()) {
2856 had_focus = files;
2857 }
2858
2859 _set_scanning_mode();
2860 EditorFileSystem::get_singleton()->scan();
2861 }
2862
2863 void FileSystemDock::_change_split_mode() {
2864 DisplayMode next_mode = DISPLAY_MODE_TREE_ONLY;
2865 if (display_mode == DISPLAY_MODE_VSPLIT) {
2866 next_mode = DISPLAY_MODE_HSPLIT;
2867 } else if (display_mode == DISPLAY_MODE_TREE_ONLY) {
2868 next_mode = DISPLAY_MODE_VSPLIT;
2869 }
2870
2871 set_display_mode(next_mode);
2872 emit_signal(SNAME("display_mode_changed"));
2873 }
2874
2875 void FileSystemDock::_split_dragged(int p_offset) {
2876 if (split_box->is_vertical()) {
2877 split_box_offset_v = p_offset;
2878 } else {
2879 split_box_offset_h = p_offset;
2880 }
2881 }
2882
2883 void FileSystemDock::fix_dependencies(const String &p_for_file) {
2884 deps_editor->edit(p_for_file);
2885 }
2886
2887 void FileSystemDock::update_all() {
2888 if (tree->is_visible()) {
2889 _update_tree(get_uncollapsed_paths(), false, false);
2890 }
2891
2892 if (file_list_vb->is_visible()) {
2893 _update_file_list(true);
2894 }
2895 }
2896
2897 void FileSystemDock::focus_on_path() {
2898 current_path_line_edit->grab_focus();
2899 current_path_line_edit->select_all();
2900 }
2901
2902 void FileSystemDock::focus_on_filter() {
2903 LineEdit *current_search_box = nullptr;
2904 if (display_mode == DISPLAY_MODE_TREE_ONLY) {
2905 current_search_box = tree_search_box;
2906 } else {
2907 current_search_box = file_list_search_box;
2908 }
2909
2910 if (current_search_box) {
2911 current_search_box->grab_focus();
2912 current_search_box->select_all();
2913 }
2914 }
2915
2916 void FileSystemDock::create_directory(const String &p_path, const String &p_base_dir) {
2917 String trimmed_path = p_path;
2918 if (!p_base_dir.is_empty()) {
2919 // Trims off the joining '/' if the base didn't end with one. If the base did have it
2920 // and there's two slashes, the empty directory is safe to trim off anyways.
2921 trimmed_path = trimmed_path.trim_prefix(p_base_dir).trim_prefix("/");
2922 }
2923 Error err = EditorFileSystem::get_singleton()->make_dir_recursive(trimmed_path, p_base_dir);
2924 if (err != OK) {
2925 EditorNode::get_singleton()->show_warning(vformat(TTR("Could not create folder: %s"), TTR(error_names[err])));
2926 }
2927 }
2928
2929 ScriptCreateDialog *FileSystemDock::get_script_create_dialog() const {
2930 return make_script_dialog;
2931 }
2932
2933 void FileSystemDock::set_file_list_display_mode(FileListDisplayMode p_mode) {
2934 if (p_mode == file_list_display_mode) {
2935 return;
2936 }
2937
2938 _toggle_file_display();
2939 }
2940
2941 void FileSystemDock::add_resource_tooltip_plugin(const Ref<EditorResourceTooltipPlugin> &p_plugin) {
2942 tooltip_plugins.push_back(p_plugin);
2943 }
2944
2945 void FileSystemDock::remove_resource_tooltip_plugin(const Ref<EditorResourceTooltipPlugin> &p_plugin) {
2946 int index = tooltip_plugins.find(p_plugin);
2947 ERR_FAIL_COND_MSG(index == -1, "Can't remove plugin that wasn't registered.");
2948 tooltip_plugins.remove_at(index);
2949 }
2950
2951 String FileSystemDock::get_folder_path_at_mouse_position() const {
2952 TreeItem *item = tree->get_item_at_position(tree->get_local_mouse_position());
2953 if (!item) {
2954 return String();
2955 }
2956 String fpath = item->get_metadata(0);
2957 return fpath.get_base_dir();
2958 }
2959
2960 Control *FileSystemDock::create_tooltip_for_path(const String &p_path) const {
2961 if (p_path == "Favorites") {
2962 // No tooltip for the "Favorites" group.
2963 return nullptr;
2964 }
2965 if (DirAccess::exists(p_path)) {
2966 // No tooltip for directory.
2967 return nullptr;
2968 }
2969 ERR_FAIL_COND_V(!FileAccess::exists(p_path), nullptr);
2970
2971 const String type = ResourceLoader::get_resource_type(p_path);
2972 Control *tooltip = EditorResourceTooltipPlugin::make_default_tooltip(p_path);
2973
2974 for (const Ref<EditorResourceTooltipPlugin> &plugin : tooltip_plugins) {
2975 if (plugin->handles(type)) {
2976 tooltip = plugin->make_tooltip_for_path(p_path, EditorResourcePreview::get_singleton()->get_preview_metadata(p_path), tooltip);
2977 }
2978 }
2979 return tooltip;
2980 }
2981
2982 Variant FileSystemDock::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
2983 bool all_favorites = true;
2984 bool all_not_favorites = true;
2985
2986 Vector<String> paths;
2987
2988 if (p_from == tree) {
2989 // Check if the first selected is in favorite.
2990 TreeItem *selected = tree->get_next_selected(tree->get_root());
2991 while (selected) {
2992 if (selected == favorites_item) {
2993 // The "Favorites" item is not draggable.
2994 return Variant();
2995 }
2996
2997 bool is_favorite = selected->get_parent() != nullptr && tree->get_root()->get_first_child() == selected->get_parent();
2998 all_favorites &= is_favorite;
2999 all_not_favorites &= !is_favorite;
3000 selected = tree->get_next_selected(selected);
3001 }
3002 if (!all_not_favorites) {
3003 paths = _tree_get_selected(false);
3004 } else {
3005 paths = _tree_get_selected();
3006 }
3007 } else if (p_from == files) {
3008 for (int i = 0; i < files->get_item_count(); i++) {
3009 if (files->is_selected(i)) {
3010 paths.push_back(files->get_item_metadata(i));
3011 }
3012 }
3013 all_favorites = false;
3014 all_not_favorites = true;
3015 }
3016
3017 if (paths.is_empty()) {
3018 return Variant();
3019 }
3020
3021 Dictionary drag_data = EditorNode::get_singleton()->drag_files_and_dirs(paths, p_from);
3022 if (!all_not_favorites) {
3023 drag_data["favorite"] = all_favorites ? "all" : "mixed";
3024 }
3025 return drag_data;
3026 }
3027
3028 bool FileSystemDock::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
3029 Dictionary drag_data = p_data;
3030
3031 if (drag_data.has("favorite")) {
3032 if (String(drag_data["favorite"]) != "all") {
3033 return false;
3034 }
3035
3036 // Moving favorite around.
3037 TreeItem *ti = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_selected() : tree->get_item_at_position(p_point);
3038 if (!ti) {
3039 return false;
3040 }
3041
3042 int drop_section = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_drop_section_at_position(tree->get_item_rect(ti).position) : tree->get_drop_section_at_position(p_point);
3043 if (ti == favorites_item) {
3044 return (drop_section == 1); // The parent, first fav.
3045 }
3046 if (ti->get_parent() && favorites_item == ti->get_parent()) {
3047 return true; // A favorite
3048 }
3049 if (ti == resources_item) {
3050 return (drop_section == -1); // The tree, last fav.
3051 }
3052
3053 return false;
3054 }
3055
3056 if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
3057 // Move resources.
3058 String to_dir;
3059 bool favorite;
3060 _get_drag_target_folder(to_dir, favorite, p_point, p_from);
3061 return !favorite;
3062 }
3063
3064 if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
3065 // Move files or dir.
3066 String to_dir;
3067 bool favorite;
3068 _get_drag_target_folder(to_dir, favorite, p_point, p_from);
3069
3070 if (favorite) {
3071 return true;
3072 }
3073
3074 if (to_dir.is_empty()) {
3075 return false;
3076 }
3077
3078 // Attempting to move a folder into itself will fail later,
3079 // rather than bring up a message don't try to do it in the first place.
3080 to_dir = to_dir.ends_with("/") ? to_dir : (to_dir + "/");
3081 Vector<String> fnames = drag_data["files"];
3082 for (int i = 0; i < fnames.size(); ++i) {
3083 if (fnames[i].ends_with("/") && to_dir.begins_with(fnames[i])) {
3084 return false;
3085 }
3086 }
3087
3088 return true;
3089 }
3090
3091 if (drag_data.has("type") && String(drag_data["type"]) == "nodes") {
3092 // Save branch as scene.
3093 String to_dir;
3094 bool favorite;
3095 _get_drag_target_folder(to_dir, favorite, p_point, p_from);
3096 return !favorite && Array(drag_data["nodes"]).size() == 1;
3097 }
3098
3099 return false;
3100 }
3101
3102 void FileSystemDock::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
3103 if (!can_drop_data_fw(p_point, p_data, p_from)) {
3104 return;
3105 }
3106 Dictionary drag_data = p_data;
3107
3108 Vector<String> dirs = EditorSettings::get_singleton()->get_favorites();
3109
3110 if (drag_data.has("favorite")) {
3111 if (String(drag_data["favorite"]) != "all") {
3112 return;
3113 }
3114 // Moving favorite around.
3115 TreeItem *ti = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_selected() : tree->get_item_at_position(p_point);
3116 if (!ti) {
3117 return;
3118 }
3119 int drop_section = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_drop_section_at_position(tree->get_item_rect(ti).position) : tree->get_drop_section_at_position(p_point);
3120
3121 int drop_position;
3122 Vector<String> drag_files = drag_data["files"];
3123 if (ti == favorites_item) {
3124 // Drop on the favorite folder.
3125 drop_position = 0;
3126 } else if (ti == resources_item) {
3127 // Drop on the resource item.
3128 drop_position = dirs.size();
3129 } else {
3130 // Drop in the list.
3131 drop_position = dirs.find(ti->get_metadata(0));
3132 if (drop_section == 1) {
3133 drop_position++;
3134 }
3135 }
3136
3137 // Remove dragged favorites.
3138 Vector<int> to_remove;
3139 int offset = 0;
3140 for (int i = 0; i < drag_files.size(); i++) {
3141 int to_remove_pos = dirs.find(drag_files[i]);
3142 to_remove.push_back(to_remove_pos);
3143 if (to_remove_pos < drop_position) {
3144 offset++;
3145 }
3146 }
3147 drop_position -= offset;
3148 to_remove.sort();
3149 for (int i = 0; i < to_remove.size(); i++) {
3150 dirs.remove_at(to_remove[i] - i);
3151 }
3152
3153 // Re-add them at the right position.
3154 for (int i = 0; i < drag_files.size(); i++) {
3155 dirs.insert(drop_position, drag_files[i]);
3156 drop_position++;
3157 }
3158
3159 EditorSettings::get_singleton()->set_favorites(dirs);
3160 _update_tree(get_uncollapsed_paths());
3161
3162 if (display_mode != DISPLAY_MODE_TREE_ONLY && current_path == "Favorites") {
3163 _update_file_list(true);
3164 }
3165 return;
3166 }
3167
3168 if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
3169 // Moving resource.
3170 Ref<Resource> res = drag_data["resource"];
3171 String to_dir;
3172 bool favorite;
3173 tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
3174 _get_drag_target_folder(to_dir, favorite, p_point, p_from);
3175 if (to_dir.is_empty()) {
3176 to_dir = get_current_directory();
3177 }
3178
3179 if (res.is_valid() && !to_dir.is_empty()) {
3180 EditorNode::get_singleton()->push_item(res.ptr());
3181 EditorNode::get_singleton()->save_resource_as(res, to_dir);
3182 }
3183 }
3184
3185 if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
3186 // Move files or add to favorites.
3187 String to_dir;
3188 bool favorite;
3189 _get_drag_target_folder(to_dir, favorite, p_point, p_from);
3190 if (!to_dir.is_empty()) {
3191 Vector<String> fnames = drag_data["files"];
3192 to_move.clear();
3193 String target_dir = to_dir == "res://" ? to_dir : to_dir.trim_suffix("/");
3194
3195 for (int i = 0; i < fnames.size(); i++) {
3196 if (fnames[i].trim_suffix("/").get_base_dir() != target_dir) {
3197 to_move.push_back(FileOrFolder(fnames[i], !fnames[i].ends_with("/")));
3198 }
3199 }
3200 if (!to_move.is_empty()) {
3201 String move_confirm_text;
3202 confirm_move_to_dir = to_dir;
3203
3204 if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
3205 move_confirm_text = vformat(TTRN("Copy %d selected item to \"%s\"?", "Copy %d selected items to \"%s\"?", to_move.size()), to_move.size(), target_dir);
3206 confirm_to_copy = true;
3207 } else {
3208 move_confirm_text = vformat(TTRN("Move %d selected item to \"%s\"?", "Move %d selected items to \"%s\"?", to_move.size()), to_move.size(), target_dir);
3209 confirm_to_copy = false;
3210 }
3211
3212 move_confirm_dialog->set_text(move_confirm_text);
3213 move_confirm_dialog->popup_centered();
3214 }
3215 } else if (favorite) {
3216 // Add the files from favorites.
3217 Vector<String> fnames = drag_data["files"];
3218 Vector<String> favorites_list = EditorSettings::get_singleton()->get_favorites();
3219 for (int i = 0; i < fnames.size(); i++) {
3220 if (!favorites_list.has(fnames[i])) {
3221 favorites_list.push_back(fnames[i]);
3222 }
3223 }
3224 EditorSettings::get_singleton()->set_favorites(favorites_list);
3225 _update_tree(get_uncollapsed_paths());
3226 }
3227 }
3228
3229 if (drag_data.has("type") && String(drag_data["type"]) == "nodes") {
3230 String to_dir;
3231 bool favorite;
3232 tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
3233 _get_drag_target_folder(to_dir, favorite, p_point, p_from);
3234 if (to_dir.is_empty()) {
3235 to_dir = get_current_directory();
3236 }
3237 SceneTreeDock::get_singleton()->save_branch_to_file(to_dir);
3238 }
3239 }
3240
3241 void FileSystemDock::_get_drag_target_folder(String &target, bool &target_favorites, const Point2 &p_point, Control *p_from) const {
3242 target = String();
3243 target_favorites = false;
3244
3245 // In the file list.
3246 if (p_from == files) {
3247 int pos = (p_point == Vector2(Math::INF, Math::INF)) ? -1 : files->get_item_at_position(p_point, true);
3248 if (pos == -1) {
3249 target = get_current_directory();
3250 return;
3251 }
3252
3253 String ltarget = files->get_item_metadata(pos);
3254 target = ltarget.ends_with("/") ? ltarget : current_path.get_base_dir();
3255 return;
3256 }
3257
3258 // In the tree.
3259 if (p_from == tree) {
3260 TreeItem *ti = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_selected() : tree->get_item_at_position(p_point);
3261 int section = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_drop_section_at_position(tree->get_item_rect(ti).position) : tree->get_drop_section_at_position(p_point);
3262 if (ti) {
3263 // Check the favorites first.
3264 if (ti == tree->get_root()->get_first_child() && section >= 0) {
3265 target_favorites = true;
3266 return;
3267 } else if (ti->get_parent() == tree->get_root()->get_first_child()) {
3268 target_favorites = true;
3269 return;
3270 } else {
3271 String fpath = ti->get_metadata(0);
3272 if (section == 0) {
3273 if (fpath.ends_with("/")) {
3274 // We drop on a folder.
3275 target = fpath;
3276 return;
3277 } else {
3278 // We drop on the folder that the target file is in.
3279 target = fpath.get_base_dir();
3280 return;
3281 }
3282 } else {
3283 if (ti->get_parent() != tree->get_root()->get_first_child()) {
3284 // Not in the favorite section.
3285 if (fpath != "res://") {
3286 // We drop between two files
3287 if (fpath.ends_with("/")) {
3288 fpath = fpath.substr(0, fpath.length() - 1);
3289 }
3290 target = fpath.get_base_dir();
3291 return;
3292 }
3293 }
3294 }
3295 }
3296 }
3297 }
3298 }
3299
3300 void FileSystemDock::_update_folder_colors_setting() {
3301 if (!ProjectSettings::get_singleton()->has_setting("file_customization/folder_colors")) {
3302 ProjectSettings::get_singleton()->set_setting("file_customization/folder_colors", assigned_folder_colors);
3303 } else if (assigned_folder_colors.is_empty()) {
3304 ProjectSettings::get_singleton()->set_setting("file_customization/folder_colors", Variant());
3305 }
3306 ProjectSettings::get_singleton()->save();
3307 }
3308
3309 void FileSystemDock::_folder_color_index_pressed(int p_index, PopupMenu *p_menu) {
3310 Variant chosen_color_name = p_menu->get_item_metadata(p_index);
3311 Vector<String> selected;
3312
3313 // Get all selected folders based on whether the files panel or tree panel is currently focused.
3314 if (files->has_focus()) {
3315 Vector<int> files_selected_ids = files->get_selected_items();
3316 for (int i = 0; i < files_selected_ids.size(); i++) {
3317 selected.push_back(files->get_item_metadata(files_selected_ids[i]));
3318 }
3319 } else {
3320 TreeItem *tree_selected = tree->get_root();
3321 tree_selected = tree->get_next_selected(tree_selected);
3322 while (tree_selected) {
3323 selected.push_back(tree_selected->get_metadata(0));
3324 tree_selected = tree->get_next_selected(tree_selected);
3325 }
3326 }
3327
3328 // Update project settings with new folder colors.
3329 for (int i = 0; i < selected.size(); i++) {
3330 const String &fpath = selected[i];
3331
3332 if (chosen_color_name) {
3333 assigned_folder_colors[fpath] = chosen_color_name;
3334 } else {
3335 assigned_folder_colors.erase(fpath);
3336 }
3337 }
3338
3339 _update_folder_colors_setting();
3340 update_all();
3341
3342 emit_signal(SNAME("folder_color_changed"));
3343 }
3344
3345 void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, const Vector<String> &p_paths, bool p_display_path_dependent_options) {
3346 // Add options for files and folders.
3347 ERR_FAIL_COND_MSG(p_paths.is_empty(), "Path cannot be empty.");
3348
3349 Vector<String> filenames;
3350 Vector<String> foldernames;
3351
3352 Vector<String> favorites_list = EditorSettings::get_singleton()->get_favorites();
3353
3354 bool all_files = true;
3355 bool all_files_scenes = true;
3356 bool all_folders = true;
3357 bool all_favorites = true;
3358 bool all_not_favorites = true;
3359
3360 for (int i = 0; i < p_paths.size(); i++) {
3361 const String &fpath = p_paths[i];
3362 if (fpath.ends_with("/")) {
3363 foldernames.push_back(fpath);
3364 all_files = false;
3365 } else {
3366 filenames.push_back(fpath);
3367 all_folders = false;
3368 all_files_scenes &= (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene");
3369 }
3370
3371 // Check if in favorites.
3372 bool found = false;
3373 for (int j = 0; j < favorites_list.size(); j++) {
3374 if (favorites_list[j] == fpath) {
3375 found = true;
3376 break;
3377 }
3378 }
3379 if (found) {
3380 all_not_favorites = false;
3381 } else {
3382 all_favorites = false;
3383 }
3384 }
3385
3386 if (all_files) {
3387 if (all_files_scenes) {
3388 if (filenames.size() == 1) {
3389 p_popup->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTRC("Open Scene"), FILE_MENU_OPEN);
3390 p_popup->add_icon_item(get_editor_theme_icon(SNAME("CreateNewSceneFrom")), TTRC("New Inherited Scene"), FILE_MENU_INHERIT);
3391 if (main_scene_path != filenames[0]) {
3392 p_popup->add_icon_item(get_editor_theme_icon(SNAME("PlayScene")), TTRC("Set as Main Scene"), FILE_MENU_MAIN_SCENE);
3393 }
3394 } else {
3395 p_popup->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTRC("Open Scenes"), FILE_MENU_OPEN);
3396 }
3397 p_popup->add_icon_item(get_editor_theme_icon(SNAME("Instance")), TTRC("Instantiate"), FILE_MENU_INSTANTIATE);
3398 p_popup->add_separator();
3399 } else if (filenames.size() == 1) {
3400 p_popup->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTRC("Open"), FILE_MENU_OPEN);
3401
3402 String type = EditorFileSystem::get_singleton()->get_file_type(filenames[0]);
3403 if (ClassDB::is_parent_class(type, "Script")) {
3404 Ref<Script> scr = ResourceLoader::load(filenames[0]);
3405 if (scr.is_valid()) {
3406 if (ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
3407 p_popup->add_icon_item(get_editor_theme_icon(SNAME("MainPlay")), TTRC("Run"), FILE_MENU_RUN_SCRIPT);
3408 }
3409 }
3410 }
3411 p_popup->add_separator();
3412 }
3413
3414 if (filenames.size() == 1) {
3415 p_popup->add_item(TTRC("Edit Dependencies..."), FILE_MENU_DEPENDENCIES);
3416 p_popup->add_item(TTRC("View Owners..."), FILE_MENU_OWNERS);
3417 p_popup->add_separator();
3418 }
3419 }
3420
3421 if (p_paths.size() == 1 && p_display_path_dependent_options) {
3422 PopupMenu *new_menu = memnew(PopupMenu);
3423 new_menu->connect(SceneStringName(id_pressed), callable_mp(this, &FileSystemDock::_generic_rmb_option_selected));
3424
3425 p_popup->add_submenu_node_item(TTRC("Create New"), new_menu, FILE_MENU_NEW);
3426 p_popup->set_item_icon(p_popup->get_item_index(FILE_MENU_NEW), get_editor_theme_icon(SNAME("Add")));
3427
3428 new_menu->add_icon_item(get_editor_theme_icon(SNAME("Folder")), TTRC("Folder..."), FILE_MENU_NEW_FOLDER);
3429 new_menu->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_folder"));
3430 new_menu->add_icon_item(get_editor_theme_icon(SNAME("PackedScene")), TTRC("Scene..."), FILE_MENU_NEW_SCENE);
3431 new_menu->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_scene"));
3432 new_menu->add_icon_item(get_editor_theme_icon(SNAME("Script")), TTRC("Script..."), FILE_MENU_NEW_SCRIPT);
3433 new_menu->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_script"));
3434 new_menu->add_icon_item(get_editor_theme_icon(SNAME("Object")), TTRC("Resource..."), FILE_MENU_NEW_RESOURCE);
3435 new_menu->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_resource"));
3436 new_menu->add_icon_item(get_editor_theme_icon(SNAME("TextFile")), TTRC("TextFile..."), FILE_MENU_NEW_TEXTFILE);
3437 new_menu->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_textfile"));
3438
3439 const PackedStringArray folder_path = { p_paths[0].get_base_dir() };
3440 // Options for CONTEXT_SLOT_FILESYSTEM_CREATE are added with an offset, to avoid conflicts in case plugins add options for both FileSystem slots.
3441 EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(new_menu, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, folder_path, 500);
3442 p_popup->add_separator();
3443 }
3444
3445 // Check if the root path is selected, we must check p_paths[1] because the first string in
3446 // the list of paths obtained by _tree_get_selected(...) is not always the root path.
3447 bool root_path_not_selected = p_paths[0] != "res://" && (p_paths.size() <= 1 || p_paths[1] != "res://");
3448
3449 if (all_folders && foldernames.size() > 0) {
3450 p_popup->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTRC("Expand Folder"), FILE_MENU_OPEN);
3451
3452 if (foldernames.size() == 1) {
3453 p_popup->add_icon_item(get_editor_theme_icon(SNAME("GuiTreeArrowDown")), TTRC("Expand Hierarchy"), FILE_MENU_EXPAND_ALL);
3454 p_popup->add_icon_item(get_editor_theme_icon(SNAME("GuiTreeArrowRight")), TTRC("Collapse Hierarchy"), FILE_MENU_COLLAPSE_ALL);
3455 }
3456
3457 p_popup->add_separator();
3458
3459 // Only add the 'Set Folder Color...' option if the root path is not selected.
3460 if (root_path_not_selected) {
3461 PopupMenu *folder_colors_menu = memnew(PopupMenu);
3462 folder_colors_menu->connect(SceneStringName(id_pressed), callable_mp(this, &FileSystemDock::_folder_color_index_pressed).bind(folder_colors_menu));
3463
3464 p_popup->add_submenu_node_item(TTRC("Set Folder Color..."), folder_colors_menu);
3465 p_popup->set_item_icon(-1, get_editor_theme_icon(SNAME("Paint")));
3466
3467 folder_colors_menu->add_icon_item(get_editor_theme_icon(SNAME("Folder")), TTRC("Default (Reset)"));
3468 folder_colors_menu->set_item_icon_modulate(0, get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog")));
3469 folder_colors_menu->add_separator();
3470
3471 for (const KeyValue<String, Color> &E : folder_colors) {
3472 folder_colors_menu->add_icon_item(get_editor_theme_icon(SNAME("Folder")), E.key.capitalize());
3473
3474 folder_colors_menu->set_item_icon_modulate(-1, editor_is_dark_icon_and_font ? E.value : E.value * 2);
3475 folder_colors_menu->set_item_metadata(-1, E.key);
3476 }
3477 }
3478 }
3479
3480 // Add the options that are only available when a single item is selected.
3481 if (p_paths.size() == 1) {
3482 p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("ActionCopy")), ED_GET_SHORTCUT("filesystem_dock/copy_path"), FILE_MENU_COPY_PATH);
3483 p_popup->add_shortcut(ED_GET_SHORTCUT("filesystem_dock/copy_absolute_path"), FILE_MENU_COPY_ABSOLUTE_PATH);
3484 if (ResourceLoader::get_resource_uid(p_paths[0]) != ResourceUID::INVALID_ID) {
3485 p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Instance")), ED_GET_SHORTCUT("filesystem_dock/copy_uid"), FILE_MENU_COPY_UID);
3486 }
3487 if (root_path_not_selected) {
3488 p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Rename")), ED_GET_SHORTCUT("filesystem_dock/rename"), FILE_MENU_RENAME);
3489 p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Duplicate")), ED_GET_SHORTCUT("filesystem_dock/duplicate"), FILE_MENU_DUPLICATE);
3490 }
3491 }
3492
3493 // Add the options that are only available when the root path is not selected.
3494 if (root_path_not_selected) {
3495 p_popup->add_icon_item(get_editor_theme_icon(SNAME("MoveUp")), TTRC("Move/Duplicate To..."), FILE_MENU_MOVE);
3496 p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Remove")), ED_GET_SHORTCUT("filesystem_dock/delete"), FILE_MENU_REMOVE);
3497 }
3498
3499 // Only add a separator if we have actually placed any options in the menu since the last separator.
3500 if (p_paths.size() == 1 || root_path_not_selected) {
3501 p_popup->add_separator();
3502 }
3503
3504 // Add the options that are available when one or more items are selected.
3505 if (p_paths.size() >= 1) {
3506 if (!all_favorites) {
3507 p_popup->add_icon_item(get_editor_theme_icon(SNAME("Favorites")), TTRC("Add to Favorites"), FILE_MENU_ADD_FAVORITE);
3508 }
3509 if (!all_not_favorites) {
3510 p_popup->add_icon_item(get_editor_theme_icon(SNAME("NonFavorite")), TTRC("Remove from Favorites"), FILE_MENU_REMOVE_FAVORITE);
3511 }
3512
3513 if (root_path_not_selected) {
3514 cached_valid_conversion_targets = _get_valid_conversions_for_file_paths(p_paths);
3515
3516 int relative_id = 0;
3517 if (!cached_valid_conversion_targets.is_empty()) {
3518 p_popup->add_separator();
3519
3520 // If we have more than one type we can convert into, collapse it into a submenu.
3521 const int CONVERSION_SUBMENU_THRESHOLD = 1;
3522
3523 PopupMenu *container_menu = p_popup;
3524 String conversion_string_template = "Convert to %s";
3525
3526 if (cached_valid_conversion_targets.size() > CONVERSION_SUBMENU_THRESHOLD) {
3527 container_menu = memnew(PopupMenu);
3528 container_menu->connect(SceneStringName(id_pressed), callable_mp(this, &FileSystemDock::_generic_rmb_option_selected));
3529
3530 p_popup->add_submenu_node_item(TTRC("Convert to..."), container_menu, FILE_MENU_NEW);
3531 conversion_string_template = "%s";
3532 }
3533
3534 for (const String &E : cached_valid_conversion_targets) {
3535 Ref<Texture2D> icon;
3536 if (has_theme_icon(E, SNAME("EditorIcons"))) {
3537 icon = get_editor_theme_icon(E);
3538 } else {
3539 icon = get_editor_theme_icon(SNAME("Object"));
3540 }
3541
3542 container_menu->add_icon_item(icon, vformat(TTR(conversion_string_template), E), CONVERT_BASE_ID + relative_id);
3543 relative_id++;
3544 }
3545 }
3546 }
3547
3548 {
3549 List<String> resource_extensions;
3550 ResourceFormatImporter::get_singleton()->get_recognized_extensions_for_type("Resource", &resource_extensions);
3551 HashSet<String> extension_list;
3552 for (const String &extension : resource_extensions) {
3553 extension_list.insert(extension);
3554 }
3555
3556 bool resource_valid = true;
3557 String main_extension;
3558
3559 for (int i = 0; i != p_paths.size(); ++i) {
3560 String extension = p_paths[i].get_extension();
3561 if (extension_list.has(extension)) {
3562 if (main_extension.is_empty()) {
3563 main_extension = extension;
3564 } else if (extension != main_extension) {
3565 resource_valid = false;
3566 break;
3567 }
3568 } else {
3569 resource_valid = false;
3570 break;
3571 }
3572 }
3573
3574 if (resource_valid) {
3575 p_popup->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTRC("Reimport"), FILE_MENU_REIMPORT);
3576 }
3577 }
3578 }
3579
3580 if (p_paths.size() == 1) {
3581 const String &fpath = p_paths[0];
3582
3583 [[maybe_unused]] bool added_separator = false;
3584
3585 if (favorites_list.has(fpath)) {
3586 TreeItem *cursor_item = tree->get_selected();
3587 bool is_item_in_favorites = false;
3588 while (cursor_item != nullptr) {
3589 if (cursor_item == favorites_item) {
3590 is_item_in_favorites = true;
3591 break;
3592 }
3593
3594 cursor_item = cursor_item->get_parent();
3595 }
3596
3597 if (is_item_in_favorites) {
3598 p_popup->add_separator();
3599 added_separator = true;
3600 p_popup->add_icon_item(get_editor_theme_icon(SNAME("ShowInFileSystem")), TTRC("Show in FileSystem"), FILE_MENU_SHOW_IN_FILESYSTEM);
3601 }
3602 }
3603
3604 #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
3605 if (!added_separator) {
3606 p_popup->add_separator();
3607 added_separator = true;
3608 }
3609
3610 // Opening the system file manager is not supported on the Android and web editors.
3611 const bool is_directory = fpath.ends_with("/");
3612
3613 p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Terminal")), ED_GET_SHORTCUT("filesystem_dock/open_in_terminal"), FILE_MENU_OPEN_IN_TERMINAL);
3614 p_popup->set_item_text(p_popup->get_item_index(FILE_MENU_OPEN_IN_TERMINAL), is_directory ? TTRC("Open in Terminal") : TTRC("Open Folder in Terminal"));
3615
3616 if (!is_directory) {
3617 p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("ExternalLink")), ED_GET_SHORTCUT("filesystem_dock/open_in_external_program"), FILE_MENU_OPEN_EXTERNAL);
3618 }
3619
3620 p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Filesystem")), ED_GET_SHORTCUT("filesystem_dock/show_in_explorer"), FILE_MENU_SHOW_IN_EXPLORER);
3621 p_popup->set_item_text(p_popup->get_item_index(FILE_MENU_SHOW_IN_EXPLORER), is_directory ? TTRC("Open in File Manager") : TTRC("Show in File Manager"));
3622 #endif
3623
3624 current_path = fpath;
3625 }
3626 EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(p_popup, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM, p_paths);
3627 }
3628
3629 void FileSystemDock::_tree_rmb_select(const Vector2 &p_pos, MouseButton p_button) {
3630 if (p_button != MouseButton::RIGHT) {
3631 return;
3632 }
3633 tree->grab_focus(true);
3634
3635 // Right click is pressed in the tree.
3636 Vector<String> paths = _tree_get_selected(false);
3637
3638 tree_popup->clear();
3639
3640 // Popup.
3641 if (!paths.is_empty()) {
3642 tree_popup->reset_size();
3643 _file_and_folders_fill_popup(tree_popup, paths);
3644 tree_popup->set_position(tree->get_screen_position() + p_pos);
3645 tree_popup->reset_size();
3646 tree_popup->popup();
3647 }
3648 }
3649
3650 void FileSystemDock::_tree_empty_click(const Vector2 &p_pos, MouseButton p_button) {
3651 if (p_button != MouseButton::RIGHT) {
3652 return;
3653 }
3654 // Right click is pressed in the empty space of the tree.
3655 current_path = "res://";
3656 tree_popup->clear();
3657 tree_popup->reset_size();
3658 tree_popup->add_icon_item(get_editor_theme_icon(SNAME("Folder")), TTRC("New Folder..."), FILE_MENU_NEW_FOLDER);
3659 tree_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_folder"));
3660 tree_popup->add_icon_item(get_editor_theme_icon(SNAME("PackedScene")), TTRC("New Scene..."), FILE_MENU_NEW_SCENE);
3661 tree_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_scene"));
3662 tree_popup->add_icon_item(get_editor_theme_icon(SNAME("Script")), TTRC("New Script..."), FILE_MENU_NEW_SCRIPT);
3663 tree_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_script"));
3664 tree_popup->add_icon_item(get_editor_theme_icon(SNAME("Object")), TTRC("New Resource..."), FILE_MENU_NEW_RESOURCE);
3665 tree_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_resource"));
3666 tree_popup->add_icon_item(get_editor_theme_icon(SNAME("TextFile")), TTRC("New TextFile..."), FILE_MENU_NEW_TEXTFILE);
3667 tree_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_textfile"));
3668
3669 // To keep consistency with options added to "Create New..." menu (for plugin which has slot as CONTEXT_SLOT_FILESYSTEM_CREATE).
3670 EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(tree_popup, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, Vector<String>(), 500);
3671 #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
3672 // Opening the system file manager is not supported on the Android and web editors.
3673 tree_popup->add_separator();
3674 tree_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Terminal")), ED_GET_SHORTCUT("filesystem_dock/open_in_terminal"), FILE_MENU_OPEN_IN_TERMINAL);
3675 tree_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Filesystem")), ED_GET_SHORTCUT("filesystem_dock/show_in_explorer"), FILE_MENU_SHOW_IN_EXPLORER);
3676 #endif
3677
3678 tree_popup->set_position(tree->get_screen_position() + p_pos);
3679 tree_popup->reset_size();
3680 tree_popup->popup();
3681 }
3682
3683 void FileSystemDock::_tree_empty_selected() {
3684 tree->deselect_all();
3685 current_path = "";
3686 current_path_line_edit->set_text(current_path);
3687 if (file_list_vb->is_visible()) {
3688 _update_file_list(false);
3689 }
3690 _update_selection_changed();
3691 }
3692
3693 void FileSystemDock::_file_list_item_clicked(int p_item, const Vector2 &p_pos, MouseButton p_mouse_button_index) {
3694 if (p_mouse_button_index != MouseButton::RIGHT) {
3695 return;
3696 }
3697 files->grab_focus(true);
3698
3699 // Right click is pressed in the file list.
3700 Vector<String> paths;
3701 for (int i = 0; i < files->get_item_count(); i++) {
3702 if (!files->is_selected(i)) {
3703 continue;
3704 }
3705 if (files->get_item_text(p_item) == "..") {
3706 files->deselect(i);
3707 continue;
3708 }
3709 paths.push_back(files->get_item_metadata(i));
3710 }
3711
3712 // Popup.
3713 if (!paths.is_empty()) {
3714 file_list_popup->clear();
3715 _file_and_folders_fill_popup(file_list_popup, paths, searched_tokens.is_empty());
3716 file_list_popup->set_position(files->get_screen_position() + p_pos);
3717 file_list_popup->reset_size();
3718 file_list_popup->popup();
3719 }
3720 }
3721
3722 void FileSystemDock::_file_list_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index) {
3723 if (p_mouse_button_index != MouseButton::RIGHT) {
3724 return;
3725 }
3726
3727 // Right click on empty space for file list.
3728 if (!searched_tokens.is_empty()) {
3729 return;
3730 }
3731
3732 current_path = current_path_line_edit->get_text();
3733
3734 // Favorites isn't a directory so don't show menu.
3735 if (current_path == "Favorites") {
3736 return;
3737 }
3738
3739 file_list_popup->clear();
3740 file_list_popup->reset_size();
3741
3742 file_list_popup->add_icon_item(get_editor_theme_icon(SNAME("Folder")), TTRC("New Folder..."), FILE_MENU_NEW_FOLDER);
3743 file_list_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_folder"));
3744 file_list_popup->add_icon_item(get_editor_theme_icon(SNAME("PackedScene")), TTRC("New Scene..."), FILE_MENU_NEW_SCENE);
3745 file_list_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_scene"));
3746 file_list_popup->add_icon_item(get_editor_theme_icon(SNAME("Script")), TTRC("New Script..."), FILE_MENU_NEW_SCRIPT);
3747 file_list_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_script"));
3748 file_list_popup->add_icon_item(get_editor_theme_icon(SNAME("Object")), TTRC("New Resource..."), FILE_MENU_NEW_RESOURCE);
3749 file_list_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_resource"));
3750 file_list_popup->add_icon_item(get_editor_theme_icon(SNAME("TextFile")), TTRC("New TextFile..."), FILE_MENU_NEW_TEXTFILE);
3751 file_list_popup->set_item_shortcut(-1, ED_GET_SHORTCUT("filesystem_dock/new_textfile"));
3752
3753 // To keep consistency with options added to "Create New..." menu (for plugin which has slot as CONTEXT_SLOT_FILESYSTEM_CREATE).
3754 EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(file_list_popup, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, Vector<String>(), 500);
3755 file_list_popup->add_separator();
3756 file_list_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Terminal")), ED_GET_SHORTCUT("filesystem_dock/open_in_terminal"), FILE_MENU_OPEN_IN_TERMINAL);
3757 file_list_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Filesystem")), ED_GET_SHORTCUT("filesystem_dock/show_in_explorer"), FILE_MENU_SHOW_IN_EXPLORER);
3758
3759 file_list_popup->set_position(files->get_screen_position() + p_pos);
3760 file_list_popup->reset_size();
3761 file_list_popup->popup();
3762 }
3763
3764 void FileSystemDock::select_file(const String &p_file) {
3765 _navigate_to_path(p_file);
3766 }
3767
3768 void FileSystemDock::_file_multi_selected(int p_index, bool p_selected) {
3769 // Set the path to the current focused item.
3770 int current = files->get_current();
3771 if (current == p_index) {
3772 String fpath = files->get_item_metadata(current);
3773 if (!fpath.ends_with("/")) {
3774 current_path = fpath;
3775 }
3776 }
3777
3778 // Update the import dock.
3779 import_dock_needs_update = true;
3780 callable_mp(this, &FileSystemDock::_update_import_dock).call_deferred();
3781 }
3782
3783 void FileSystemDock::_update_selection_changed() {
3784 Vector<String> selection;
3785 selection.append_array(_tree_get_selected());
3786 selection.append_array(_file_list_get_selected());
3787 if (prev_selection != selection) {
3788 prev_selection = selection;
3789 emit_signal(SNAME("selection_changed"));
3790 }
3791 }
3792
3793 void FileSystemDock::_tree_mouse_exited() {
3794 if (holding_branch) {
3795 _reselect_items_selected_on_drag_begin();
3796 }
3797 }
3798
3799 void FileSystemDock::_reselect_items_selected_on_drag_begin(bool reset) {
3800 TreeItem *selected_item = tree->get_next_selected(tree->get_root());
3801 if (selected_item) {
3802 selected_item->deselect(0);
3803 }
3804 if (!tree_items_selected_on_drag_begin.is_empty()) {
3805 bool reselected = false;
3806 for (TreeItem *item : tree_items_selected_on_drag_begin) {
3807 if (item->get_tree()) {
3808 item->select(0);
3809 reselected = true;
3810 }
3811 }
3812
3813 if (reset) {
3814 tree_items_selected_on_drag_begin.clear();
3815 }
3816
3817 if (!reselected) {
3818 // If couldn't reselect the items selected on drag begin, select the "res://" item.
3819 tree->get_root()->get_child(1)->select(0);
3820 }
3821 }
3822
3823 files->deselect_all();
3824 if (!list_items_selected_on_drag_begin.is_empty()) {
3825 for (const int idx : list_items_selected_on_drag_begin) {
3826 files->select(idx, false);
3827 }
3828
3829 if (reset) {
3830 list_items_selected_on_drag_begin.clear();
3831 }
3832 }
3833 }
3834
3835 void FileSystemDock::_tree_gui_input(Ref<InputEvent> p_event) {
3836 Ref<InputEventMouseMotion> mm = p_event;
3837 if (mm.is_valid()) {
3838 TreeItem *item = tree->get_item_at_position(mm->get_position());
3839 if (item && holding_branch) {
3840 String fpath = item->get_metadata(0);
3841 while (!fpath.ends_with("/") && fpath != "res://" && item->get_parent()) { // Find the parent folder tree item.
3842 item = item->get_parent();
3843 fpath = item->get_metadata(0);
3844 }
3845
3846 TreeItem *deselect_item = tree->get_next_selected(tree->get_root());
3847 while (deselect_item) {
3848 deselect_item->deselect(0);
3849 deselect_item = tree->get_next_selected(deselect_item);
3850 }
3851 item->select(0);
3852
3853 if (display_mode != DisplayMode::DISPLAY_MODE_TREE_ONLY) {
3854 files->deselect_all();
3855 // Try to select the corresponding file list item.
3856 const int files_item_idx = files->find_metadata(fpath);
3857 if (files_item_idx != -1) {
3858 files->select(files_item_idx);
3859 }
3860 }
3861 }
3862 }
3863
3864 Ref<InputEventKey> key = p_event;
3865 if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
3866 int option_id = _get_menu_option_from_key(key);
3867 if (option_id > -1) {
3868 _tree_rmb_option(option_id);
3869 } else {
3870 bool create = false;
3871 Callable custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM, p_event);
3872 if (!custom_callback.is_valid()) {
3873 create = true;
3874 custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, p_event);
3875 }
3876
3877 if (custom_callback.is_valid()) {
3878 PackedStringArray selected = _tree_get_selected(false);
3879 if (create) {
3880 if (selected.is_empty()) {
3881 selected.append("res://");
3882 } else if (selected.size() == 1) {
3883 selected.write[0] = selected[0].get_base_dir();
3884 } else {
3885 return;
3886 }
3887 }
3888 EditorContextMenuPluginManager::get_singleton()->invoke_callback(custom_callback, selected);
3889 } else {
3890 return;
3891 }
3892 }
3893
3894 accept_event();
3895 }
3896 }
3897
3898 void FileSystemDock::_file_list_gui_input(Ref<InputEvent> p_event) {
3899 Ref<InputEventMouseMotion> mm = p_event;
3900 if (mm.is_valid() && holding_branch) {
3901 const int item_idx = files->get_item_at_position(mm->get_position(), true);
3902 files->deselect_all();
3903 String fpath;
3904 if (item_idx != -1) {
3905 fpath = files->get_item_metadata(item_idx);
3906 if (fpath.ends_with("/") || fpath == "res://") {
3907 files->select(item_idx);
3908 }
3909 } else {
3910 fpath = get_current_directory();
3911 }
3912
3913 TreeItem *deselect_item = tree->get_next_selected(tree->get_root());
3914 while (deselect_item) {
3915 deselect_item->deselect(0);
3916 deselect_item = tree->get_next_selected(deselect_item);
3917 }
3918
3919 // Try to select the corresponding tree item.
3920 TreeItem *tree_item = (item_idx != -1) ? tree->get_item_with_text(files->get_item_text(item_idx)) : nullptr;
3921
3922 if (tree_item) {
3923 tree_item->select(0);
3924 } else {
3925 // Find parent folder.
3926 fpath = fpath.substr(0, fpath.rfind_char('/') + 1);
3927 if (fpath.size() > String("res://").size()) {
3928 fpath = fpath.left(fpath.size() - 2); // Remove last '/'.
3929 const int slash_idx = fpath.rfind_char('/');
3930 fpath = fpath.substr(slash_idx + 1);
3931 }
3932
3933 tree_item = tree->get_item_with_text(fpath);
3934 if (tree_item) {
3935 tree_item->select(0);
3936 }
3937 }
3938 }
3939
3940 Ref<InputEventKey> key = p_event;
3941 if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
3942 int option_id = _get_menu_option_from_key(key);
3943 if (option_id > -1) {
3944 _file_list_rmb_option(option_id);
3945 } else {
3946 Callable custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM, p_event);
3947 if (!custom_callback.is_valid()) {
3948 custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, p_event);
3949 }
3950
3951 if (custom_callback.is_valid()) {
3952 EditorContextMenuPluginManager::get_singleton()->invoke_callback(custom_callback, _file_list_get_selected());
3953 } else {
3954 return;
3955 }
3956 }
3957
3958 accept_event();
3959 }
3960 }
3961
3962 bool FileSystemDock::_get_imported_files(const String &p_path, String &r_extension, Vector<String> &r_files) const {
3963 if (!p_path.ends_with("/")) {
3964 if (FileAccess::exists(p_path + ".import")) {
3965 if (r_extension.is_empty()) {
3966 r_extension = p_path.get_extension();
3967 } else if (r_extension != p_path.get_extension()) {
3968 r_files.clear();
3969 return false; // File type mismatch, stop search.
3970 }
3971
3972 r_files.push_back(p_path);
3973 }
3974 return true;
3975 }
3976
3977 Ref<DirAccess> da = DirAccess::open(p_path);
3978 ERR_FAIL_COND_V(da.is_null(), false);
3979
3980 da->list_dir_begin();
3981 String n = da->get_next();
3982 while (!n.is_empty()) {
3983 if (n != "." && n != ".." && !n.ends_with(".import")) {
3984 String npath = p_path + n + (da->current_is_dir() ? "/" : "");
3985 if (!_get_imported_files(npath, r_extension, r_files)) {
3986 return false;
3987 }
3988 }
3989 n = da->get_next();
3990 }
3991 da->list_dir_end();
3992 return true;
3993 }
3994
3995 void FileSystemDock::_update_import_dock() {
3996 if (!import_dock_needs_update) {
3997 return;
3998 }
3999
4000 _update_selection_changed();
4001
4002 // List selected.
4003 Vector<String> selected;
4004 if (display_mode == DISPLAY_MODE_TREE_ONLY) {
4005 // Use the tree
4006 selected = _tree_get_selected();
4007
4008 } else {
4009 // Use the file list.
4010 for (int i = 0; i < files->get_item_count(); i++) {
4011 if (!files->is_selected(i)) {
4012 continue;
4013 }
4014
4015 selected.push_back(files->get_item_metadata(i));
4016 }
4017 }
4018
4019 if (!selected.is_empty() && selected[0] == "res://") {
4020 // Scanning res:// is costly and unlikely to yield any useful results.
4021 return;
4022 }
4023
4024 // Expand directory selection.
4025 Vector<String> efiles;
4026 String extension;
4027 for (const String &fpath : selected) {
4028 _get_imported_files(fpath, extension, efiles);
4029 }
4030
4031 // Check import.
4032 Vector<String> imports;
4033 String import_type;
4034 for (int i = 0; i < efiles.size(); i++) {
4035 const String &fpath = efiles[i];
4036 Ref<ConfigFile> cf;
4037 cf.instantiate();
4038 Error err = cf->load(fpath + ".import");
4039 if (err != OK) {
4040 imports.clear();
4041 break;
4042 }
4043
4044 String type;
4045 if (cf->has_section_key("remap", "type")) {
4046 type = cf->get_value("remap", "type");
4047 }
4048 if (import_type.is_empty()) {
4049 import_type = type;
4050 } else if (import_type != type) {
4051 // All should be the same type.
4052 imports.clear();
4053 break;
4054 }
4055 imports.push_back(fpath);
4056 }
4057
4058 if (imports.is_empty()) {
4059 ImportDock::get_singleton()->clear();
4060 } else if (imports.size() == 1) {
4061 ImportDock::get_singleton()->set_edit_path(imports[0]);
4062 } else {
4063 ImportDock::get_singleton()->set_edit_multiple_paths(imports);
4064 }
4065
4066 import_dock_needs_update = false;
4067 }
4068
4069 void FileSystemDock::_feature_profile_changed() {
4070 _update_display_mode(true);
4071 }
4072
4073 void FileSystemDock::_project_settings_changed() {
4074 assigned_folder_colors = ProjectSettings::get_singleton()->get_setting("file_customization/folder_colors");
4075
4076 const String &current_main_scene_path = ResourceUID::ensure_path(GLOBAL_GET("application/run/main_scene"));
4077 if (main_scene_path != current_main_scene_path) {
4078 main_scene_path = current_main_scene_path;
4079 update_all();
4080 }
4081 }
4082
4083 void FileSystemDock::set_file_sort(FileSortOption p_file_sort) {
4084 for (int i = 0; i != (int)FileSortOption::FILE_SORT_MAX; i++) {
4085 tree_button_sort->get_popup()->set_item_checked(i, (i == (int)p_file_sort));
4086 file_list_button_sort->get_popup()->set_item_checked(i, (i == (int)p_file_sort));
4087 }
4088 file_sort = p_file_sort;
4089
4090 // Update everything needed.
4091 update_all();
4092 }
4093
4094 void FileSystemDock::_file_sort_popup(int p_id) {
4095 set_file_sort((FileSortOption)p_id);
4096 }
4097
4098 // TODO: Could use a unit test.
4099 Color FileSystemDock::get_dir_icon_color(const String &p_dir_path, const Color &p_default) {
4100 if (!singleton) { // This method can be called from the project manager.
4101 return p_default;
4102 }
4103 Color folder_icon_color = p_default;
4104
4105 // Check for a folder color to inherit (if one is assigned).
4106 String parent_dir = ProjectSettings::get_singleton()->localize_path(p_dir_path);
4107 while (!parent_dir.is_empty() && parent_dir != "res://") {
4108 if (!parent_dir.ends_with("/")) {
4109 parent_dir += "/";
4110 }
4111
4112 const String color_name = singleton->assigned_folder_colors.get(parent_dir, String());
4113 if (!color_name.is_empty()) {
4114 folder_icon_color = singleton->folder_colors[color_name];
4115 break;
4116 }
4117 parent_dir = parent_dir.trim_suffix("/").get_base_dir();
4118 }
4119 return folder_icon_color;
4120 }
4121
4122 const HashMap<String, Color> &FileSystemDock::get_folder_colors() const {
4123 return folder_colors;
4124 }
4125
4126 Dictionary FileSystemDock::get_assigned_folder_colors() const {
4127 return assigned_folder_colors;
4128 }
4129
4130 MenuButton *FileSystemDock::_create_file_menu_button() {
4131 MenuButton *button = memnew(MenuButton);
4132 button->set_flat(false);
4133 button->set_theme_type_variation("FlatMenuButton");
4134 button->set_tooltip_text(TTRC("Sort Files"));
4135 button->set_accessibility_name(TTRC("Sort Files"));
4136
4137 PopupMenu *p = button->get_popup();
4138 p->connect(SceneStringName(id_pressed), callable_mp(this, &FileSystemDock::_file_sort_popup));
4139 p->add_radio_check_item(TTRC("Sort by Name (Ascending)"), (int)FileSortOption::FILE_SORT_NAME);
4140 p->add_radio_check_item(TTRC("Sort by Name (Descending)"), (int)FileSortOption::FILE_SORT_NAME_REVERSE);
4141 p->add_radio_check_item(TTRC("Sort by Type (Ascending)"), (int)FileSortOption::FILE_SORT_TYPE);
4142 p->add_radio_check_item(TTRC("Sort by Type (Descending)"), (int)FileSortOption::FILE_SORT_TYPE_REVERSE);
4143 p->add_radio_check_item(TTRC("Sort by Last Modified"), (int)FileSortOption::FILE_SORT_MODIFIED_TIME);
4144 p->add_radio_check_item(TTRC("Sort by First Modified"), (int)FileSortOption::FILE_SORT_MODIFIED_TIME_REVERSE);
4145 p->set_item_checked((int)file_sort, true);
4146 return button;
4147 }
4148
4149 void FileSystemDock::update_layout(EditorDock::DockLayout p_layout) {
4150 bool new_horizontal = (p_layout == EditorDock::DOCK_LAYOUT_HORIZONTAL);
4151 if (horizontal == new_horizontal) {
4152 return;
4153 }
4154 horizontal = new_horizontal;
4155
4156 if (horizontal) {
4157 path_hb->reparent(toolbar_hbc);
4158 toolbar_hbc->move_child(path_hb, 2);
4159 set_meta("_dock_display_mode", get_display_mode());
4160 set_meta("_dock_file_display_mode", get_file_list_display_mode());
4161
4162 FileSystemDock::DisplayMode new_display_mode = FileSystemDock::DisplayMode(int(get_meta("_bottom_display_mode", int(FileSystemDock::DISPLAY_MODE_HSPLIT))));
4163 FileSystemDock::FileListDisplayMode new_file_display_mode = FileSystemDock::FileListDisplayMode(int(get_meta("_bottom_file_display_mode", int(FileSystemDock::FILE_LIST_DISPLAY_THUMBNAILS))));
4164
4165 set_display_mode(new_display_mode);
4166 set_file_list_display_mode(new_file_display_mode);
4167 set_custom_minimum_size(Size2(0, 200) * EDSCALE);
4168 } else {
4169 path_hb->reparent(file_list_vb);
4170 file_list_vb->move_child(path_hb, 0);
4171 set_meta("_bottom_display_mode", get_display_mode());
4172 set_meta("_bottom_file_display_mode", get_file_list_display_mode());
4173
4174 FileSystemDock::DisplayMode new_display_mode = FileSystemDock::DISPLAY_MODE_TREE_ONLY;
4175 FileSystemDock::FileListDisplayMode new_file_display_mode = FileSystemDock::FILE_LIST_DISPLAY_LIST;
4176
4177 new_display_mode = FileSystemDock::DisplayMode(int(get_meta("_dock_display_mode", new_display_mode)));
4178 new_file_display_mode = FileSystemDock::FileListDisplayMode(int(get_meta("_dock_file_display_mode", new_file_display_mode)));
4179
4180 set_display_mode(new_display_mode);
4181 set_file_list_display_mode(new_file_display_mode);
4182 set_custom_minimum_size(Size2(0, 0));
4183 }
4184 }
4185
4186 void FileSystemDock::save_layout_to_config(Ref<ConfigFile> &p_layout, const String &p_section) const {
4187 p_layout->set_value(p_section, "h_split_offset", get_h_split_offset());
4188 p_layout->set_value(p_section, "v_split_offset", get_v_split_offset());
4189 p_layout->set_value(p_section, "display_mode", get_display_mode());
4190 p_layout->set_value(p_section, "file_sort", (int)get_file_sort());
4191 p_layout->set_value(p_section, "file_list_display_mode", get_file_list_display_mode());
4192 PackedStringArray selected_files = get_selected_paths();
4193 p_layout->set_value(p_section, "selected_paths", selected_files);
4194 Vector<String> uncollapsed_paths = get_uncollapsed_paths();
4195 p_layout->set_value(p_section, "uncollapsed_paths", searched_tokens.is_empty() ? uncollapsed_paths : uncollapsed_paths_before_search);
4196 }
4197
4198 void FileSystemDock::load_layout_from_config(const Ref<ConfigFile> &p_layout, const String &p_section) {
4199 if (p_layout->has_section_key(p_section, "h_split_offset")) {
4200 int fs_h_split_ofs = p_layout->get_value(p_section, "h_split_offset");
4201 set_h_split_offset(fs_h_split_ofs);
4202 }
4203
4204 if (p_layout->has_section_key(p_section, "v_split_offset")) {
4205 int fs_v_split_ofs = p_layout->get_value(p_section, "v_split_offset");
4206 set_v_split_offset(fs_v_split_ofs);
4207 }
4208
4209 if (p_layout->has_section_key(p_section, "display_mode")) {
4210 DisplayMode dock_filesystem_display_mode = DisplayMode(int(p_layout->get_value(p_section, "display_mode")));
4211 set_display_mode(dock_filesystem_display_mode);
4212 }
4213
4214 if (p_layout->has_section_key(p_section, "file_sort")) {
4215 FileSortOption dock_filesystem_file_sort = FileSortOption(int(p_layout->get_value(p_section, "file_sort")));
4216 set_file_sort(dock_filesystem_file_sort);
4217 }
4218
4219 if (p_layout->has_section_key(p_section, "file_list_display_mode")) {
4220 FileListDisplayMode dock_filesystem_file_list_display_mode = FileListDisplayMode(int(p_layout->get_value(p_section, "file_list_display_mode")));
4221 set_file_list_display_mode(dock_filesystem_file_list_display_mode);
4222 }
4223
4224 if (p_layout->has_section_key(p_section, "selected_paths")) {
4225 PackedStringArray dock_filesystem_selected_paths = p_layout->get_value(p_section, "selected_paths");
4226 for (int i = 0; i < dock_filesystem_selected_paths.size(); i++) {
4227 select_file(dock_filesystem_selected_paths[i]);
4228 }
4229 }
4230
4231 // Restore collapsed state.
4232 PackedStringArray uncollapsed_tis;
4233 if (p_layout->has_section_key(p_section, "uncollapsed_paths")) {
4234 uncollapsed_tis = p_layout->get_value(p_section, "uncollapsed_paths");
4235 } else {
4236 uncollapsed_tis = { "res://" };
4237 }
4238
4239 if (!uncollapsed_tis.is_empty()) {
4240 for (int i = 0; i < uncollapsed_tis.size(); i++) {
4241 TreeItem *uncollapsed_ti = get_tree_control()->get_item_with_metadata(uncollapsed_tis[i], 0);
4242 if (uncollapsed_ti) {
4243 uncollapsed_ti->set_collapsed(false);
4244 }
4245 }
4246 get_tree_control()->queue_redraw();
4247 }
4248 }
4249
4250 void FileSystemDock::_bind_methods() {
4251 ClassDB::bind_method(D_METHOD("navigate_to_path", "path"), &FileSystemDock::navigate_to_path);
4252
4253 ClassDB::bind_method(D_METHOD("add_resource_tooltip_plugin", "plugin"), &FileSystemDock::add_resource_tooltip_plugin);
4254 ClassDB::bind_method(D_METHOD("remove_resource_tooltip_plugin", "plugin"), &FileSystemDock::remove_resource_tooltip_plugin);
4255
4256 ADD_SIGNAL(MethodInfo("inherit", PropertyInfo(Variant::STRING, "file")));
4257 ADD_SIGNAL(MethodInfo("instantiate", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
4258
4259 ADD_SIGNAL(MethodInfo("resource_removed", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
4260 ADD_SIGNAL(MethodInfo("file_removed", PropertyInfo(Variant::STRING, "file")));
4261 ADD_SIGNAL(MethodInfo("folder_removed", PropertyInfo(Variant::STRING, "folder")));
4262 ADD_SIGNAL(MethodInfo("files_moved", PropertyInfo(Variant::STRING, "old_file"), PropertyInfo(Variant::STRING, "new_file")));
4263 ADD_SIGNAL(MethodInfo("folder_moved", PropertyInfo(Variant::STRING, "old_folder"), PropertyInfo(Variant::STRING, "new_folder")));
4264 ADD_SIGNAL(MethodInfo("folder_color_changed"));
4265 ADD_SIGNAL(MethodInfo("selection_changed"));
4266
4267 ADD_SIGNAL(MethodInfo("display_mode_changed"));
4268 }
4269
4270 FileSystemDock::FileSystemDock() {
4271 singleton = this;
4272 set_name(TTRC("FileSystem"));
4273 set_icon_name("Folder");
4274 set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("docks/open_filesystem", TTRC("Open FileSystem Dock"), KeyModifierMask::ALT | Key::F));
4275 set_default_slot(EditorDock::DOCK_SLOT_LEFT_BR);
4276 set_available_layouts(DOCK_LAYOUT_ALL);
4277
4278 ProjectSettings::get_singleton()->add_hidden_prefix("file_customization/");
4279
4280 // `KeyModifierMask::CMD_OR_CTRL | Key::C` conflicts with other editor shortcuts.
4281 ED_SHORTCUT("filesystem_dock/copy_path", TTRC("Copy Path"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::C);
4282 ED_SHORTCUT("filesystem_dock/copy_absolute_path", TTRC("Copy Absolute Path"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::C);
4283 ED_SHORTCUT("filesystem_dock/copy_uid", TTRC("Copy UID"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | KeyModifierMask::SHIFT | Key::C);
4284 ED_SHORTCUT("filesystem_dock/duplicate", TTRC("Duplicate..."), KeyModifierMask::CMD_OR_CTRL | Key::D);
4285 ED_SHORTCUT("filesystem_dock/delete", TTRC("Delete"), Key::KEY_DELETE);
4286 ED_SHORTCUT("filesystem_dock/new_folder", TTRC("New Folder..."), Key::NONE);
4287 ED_SHORTCUT("filesystem_dock/new_scene", TTRC("New Scene..."), Key::NONE);
4288 ED_SHORTCUT("filesystem_dock/new_script", TTRC("New Script..."), Key::NONE);
4289 ED_SHORTCUT("filesystem_dock/new_resource", TTRC("New Resource..."), Key::NONE);
4290 ED_SHORTCUT("filesystem_dock/new_textfile", TTRC("New TextFile..."), Key::NONE);
4291 ED_SHORTCUT("filesystem_dock/rename", TTRC("Rename..."), Key::F2);
4292 ED_SHORTCUT_OVERRIDE("filesystem_dock/rename", "macos", Key::ENTER);
4293 #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
4294 // Opening the system file manager or opening in an external program is not supported on the Android and web editors.
4295 ED_SHORTCUT("filesystem_dock/show_in_explorer", TTRC("Open in File Manager"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::R);
4296 ED_SHORTCUT("filesystem_dock/open_in_external_program", TTRC("Open in External Program"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::E);
4297 ED_SHORTCUT("filesystem_dock/open_in_terminal", TTRC("Open in Terminal"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::T);
4298 #endif
4299
4300 ED_SHORTCUT("filesystem_dock/focus_path", TTRC("Focus Path"), KeyModifierMask::CMD_OR_CTRL | Key::L);
4301 // Allow both Cmd + L and Cmd + Shift + G to match Safari's and Finder's shortcuts respectively.
4302 ED_SHORTCUT_OVERRIDE_ARRAY("filesystem_dock/focus_path", "macos",
4303 { int32_t(KeyModifierMask::META | Key::L), int32_t(KeyModifierMask::META | KeyModifierMask::SHIFT | Key::G) });
4304
4305 // Properly translating color names would require a separate HashMap, so for simplicity they are provided as comments.
4306 folder_colors["red"] = Color(1.0, 0.271, 0.271); // TTR("Red")
4307 folder_colors["orange"] = Color(1.0, 0.561, 0.271); // TTR("Orange")
4308 folder_colors["yellow"] = Color(1.0, 0.890, 0.271); // TTR("Yellow")
4309 folder_colors["green"] = Color(0.502, 1.0, 0.271); // TTR("Green")
4310 folder_colors["teal"] = Color(0.271, 1.0, 0.635); // TTR("Teal")
4311 folder_colors["blue"] = Color(0.271, 0.843, 1.0); // TTR("Blue")
4312 folder_colors["purple"] = Color(0.502, 0.271, 1.0); // TTR("Purple")
4313 folder_colors["pink"] = Color(1.0, 0.271, 0.588); // TTR("Pink")
4314 folder_colors["gray"] = Color(0.616, 0.616, 0.616); // TTR("Gray")
4315
4316 assigned_folder_colors = ProjectSettings::get_singleton()->get_setting("file_customization/folder_colors");
4317
4318 editor_is_dark_icon_and_font = EditorThemeManager::is_dark_icon_and_font();
4319
4320 VBoxContainer *main_vb = memnew(VBoxContainer);
4321 add_child(main_vb);
4322
4323 VBoxContainer *top_vbc = memnew(VBoxContainer);
4324 main_vb->add_child(top_vbc);
4325
4326 toolbar_hbc = memnew(HBoxContainer);
4327 top_vbc->add_child(toolbar_hbc);
4328
4329 HBoxContainer *nav_hbc = memnew(HBoxContainer);
4330 nav_hbc->add_theme_constant_override("separation", 0);
4331 toolbar_hbc->add_child(nav_hbc);
4332
4333 button_hist_prev = memnew(Button);
4334 button_hist_prev->set_theme_type_variation(SceneStringName(FlatButton));
4335 button_hist_prev->set_disabled(true);
4336 button_hist_prev->set_focus_mode(FOCUS_ACCESSIBILITY);
4337 button_hist_prev->set_tooltip_text(TTRC("Go to previous selected folder/file."));
4338 nav_hbc->add_child(button_hist_prev);
4339
4340 button_hist_next = memnew(Button);
4341 button_hist_next->set_theme_type_variation(SceneStringName(FlatButton));
4342 button_hist_next->set_disabled(true);
4343 button_hist_next->set_focus_mode(FOCUS_ACCESSIBILITY);
4344 button_hist_next->set_tooltip_text(TTRC("Go to next selected folder/file."));
4345 nav_hbc->add_child(button_hist_next);
4346
4347 current_path_line_edit = memnew(LineEdit);
4348 current_path_line_edit->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
4349 current_path_line_edit->set_accessibility_name(TTRC("Path"));
4350 current_path_line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
4351 _set_current_path_line_edit_text(current_path);
4352 toolbar_hbc->add_child(current_path_line_edit);
4353
4354 button_toggle_display_mode = memnew(Button);
4355 button_toggle_display_mode->connect(SceneStringName(pressed), callable_mp(this, &FileSystemDock::_change_split_mode));
4356 button_toggle_display_mode->set_focus_mode(FOCUS_ACCESSIBILITY);
4357 button_toggle_display_mode->set_tooltip_text(TTRC("Change Split Mode"));
4358 button_toggle_display_mode->set_theme_type_variation("FlatMenuButton");
4359 toolbar_hbc->add_child(button_toggle_display_mode);
4360
4361 toolbar2_hbc = memnew(HBoxContainer);
4362 top_vbc->add_child(toolbar2_hbc);
4363
4364 tree_search_box = memnew(LineEdit);
4365 tree_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
4366 tree_search_box->set_placeholder(TTRC("Filter Files"));
4367 tree_search_box->set_clear_button_enabled(true);
4368 tree_search_box->connect(SceneStringName(text_changed), callable_mp(this, &FileSystemDock::_search_changed).bind(tree_search_box));
4369 toolbar2_hbc->add_child(tree_search_box);
4370
4371 tree_button_sort = _create_file_menu_button();
4372 toolbar2_hbc->add_child(tree_button_sort);
4373
4374 file_list_popup = memnew(PopupMenu);
4375
4376 add_child(file_list_popup);
4377
4378 tree_popup = memnew(PopupMenu);
4379
4380 add_child(tree_popup);
4381
4382 split_box = memnew(SplitContainer);
4383 split_box->set_v_size_flags(SIZE_EXPAND_FILL);
4384 split_box->connect("dragged", callable_mp(this, &FileSystemDock::_split_dragged));
4385 split_box_offset_h = 240 * EDSCALE;
4386 main_vb->add_child(split_box);
4387
4388 tree_mc = memnew(MarginContainer);
4389 split_box->add_child(tree_mc);
4390 tree_mc->set_theme_type_variation("NoBorderHorizontalBottom");
4391 tree_mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
4392
4393 tree = memnew(FileSystemTree);
4394 tree->set_accessibility_name(TTRC("Directories"));
4395 tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
4396 tree->set_hide_root(true);
4397 tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_TOP);
4398 SET_DRAG_FORWARDING_GCD(tree, FileSystemDock);
4399 tree->set_allow_rmb_select(true);
4400 tree->set_select_mode(Tree::SELECT_MULTI);
4401 tree->set_custom_minimum_size(Size2(40 * EDSCALE, 15 * EDSCALE));
4402 tree->set_column_clip_content(0, true);
4403 tree_mc->add_child(tree);
4404
4405 tree->connect("item_activated", callable_mp(this, &FileSystemDock::_tree_activate_file));
4406 tree->connect("multi_selected", callable_mp(this, &FileSystemDock::_tree_multi_selected));
4407 tree->connect("item_mouse_selected", callable_mp(this, &FileSystemDock::_tree_rmb_select));
4408 tree->connect("empty_clicked", callable_mp(this, &FileSystemDock::_tree_empty_click));
4409 tree->connect("nothing_selected", callable_mp(this, &FileSystemDock::_tree_empty_selected));
4410 tree->connect(SceneStringName(gui_input), callable_mp(this, &FileSystemDock::_tree_gui_input));
4411 tree->connect(SceneStringName(mouse_exited), callable_mp(this, &FileSystemDock::_tree_mouse_exited));
4412 tree->connect("item_edited", callable_mp(this, &FileSystemDock::_rename_operation_confirm));
4413
4414 file_list_vb = memnew(VBoxContainer);
4415 file_list_vb->set_v_size_flags(SIZE_EXPAND_FILL);
4416 split_box->add_child(file_list_vb);
4417
4418 path_hb = memnew(HBoxContainer);
4419 path_hb->set_h_size_flags(SIZE_EXPAND_FILL);
4420 file_list_vb->add_child(path_hb);
4421
4422 file_list_search_box = memnew(LineEdit);
4423 file_list_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
4424 file_list_search_box->set_placeholder(TTRC("Filter Files"));
4425 file_list_search_box->set_accessibility_name(TTRC("Filter Files"));
4426 file_list_search_box->set_clear_button_enabled(true);
4427 file_list_search_box->connect(SceneStringName(text_changed), callable_mp(this, &FileSystemDock::_search_changed).bind(file_list_search_box));
4428 path_hb->add_child(file_list_search_box);
4429
4430 file_list_button_sort = _create_file_menu_button();
4431 path_hb->add_child(file_list_button_sort);
4432
4433 button_file_list_display_mode = memnew(Button);
4434 button_file_list_display_mode->set_accessibility_name(TTRC("Display Mode"));
4435 button_file_list_display_mode->set_theme_type_variation("FlatMenuButton");
4436 path_hb->add_child(button_file_list_display_mode);
4437
4438 files_mc = memnew(MarginContainer);
4439 file_list_vb->add_child(files_mc);
4440 files_mc->set_theme_type_variation("NoBorderHorizontalBottom");
4441 files_mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
4442
4443 files = memnew(FileSystemList);
4444 files->set_accessibility_name(TTRC("Files"));
4445 files->set_select_mode(ItemList::SELECT_MULTI);
4446 files->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_TOP);
4447 SET_DRAG_FORWARDING_GCD(files, FileSystemDock);
4448 files->connect("item_clicked", callable_mp(this, &FileSystemDock::_file_list_item_clicked));
4449 files->connect(SceneStringName(gui_input), callable_mp(this, &FileSystemDock::_file_list_gui_input));
4450 files->connect("multi_selected", callable_mp(this, &FileSystemDock::_file_multi_selected));
4451 files->connect("empty_clicked", callable_mp(this, &FileSystemDock::_file_list_empty_clicked));
4452 files->connect("item_edited", callable_mp(this, &FileSystemDock::_rename_operation_confirm));
4453 files->set_custom_minimum_size(Size2(0, 15 * EDSCALE));
4454 files->set_allow_rmb_select(true);
4455 files_mc->add_child(files);
4456
4457 scanning_vb = memnew(VBoxContainer);
4458 scanning_vb->hide();
4459 main_vb->add_child(scanning_vb);
4460
4461 Label *slabel = memnew(Label);
4462 slabel->set_text(TTRC("Scanning Files,\nPlease Wait..."));
4463 slabel->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
4464 scanning_vb->add_child(slabel);
4465
4466 scanning_progress = memnew(ProgressBar);
4467 scanning_progress->set_accessibility_name(TTRC("Filesystem Scan"));
4468 scanning_vb->add_child(scanning_progress);
4469
4470 deps_editor = memnew(DependencyEditor);
4471 add_child(deps_editor);
4472
4473 owners_editor = memnew(DependencyEditorOwners());
4474 add_child(owners_editor);
4475
4476 remove_dialog = memnew(DependencyRemoveDialog);
4477 remove_dialog->connect("resource_removed", callable_mp(this, &FileSystemDock::_resource_removed));
4478 remove_dialog->connect("file_removed", callable_mp(this, &FileSystemDock::_file_removed));
4479 remove_dialog->connect("folder_removed", callable_mp(this, &FileSystemDock::_folder_removed));
4480 add_child(remove_dialog);
4481
4482 move_dialog = memnew(EditorDirDialog);
4483 add_child(move_dialog);
4484 move_dialog->connect("move_pressed", callable_mp(this, &FileSystemDock::_move_operation_confirm).bind(false, OVERWRITE_UNDECIDED));
4485 move_dialog->connect("copy_pressed", callable_mp(this, &FileSystemDock::_move_operation_confirm).bind(true, OVERWRITE_UNDECIDED));
4486
4487 overwrite_dialog = memnew(ConfirmationDialog);
4488 add_child(overwrite_dialog);
4489 overwrite_dialog->set_ok_button_text(TTRC("Overwrite"));
4490 overwrite_dialog->add_button(TTRC("Keep Both"), true)->connect(SceneStringName(pressed), callable_mp(this, &FileSystemDock::_overwrite_dialog_action).bind(false));
4491 overwrite_dialog->connect(SceneStringName(confirmed), callable_mp(this, &FileSystemDock::_overwrite_dialog_action).bind(true));
4492
4493 VBoxContainer *overwrite_dialog_vb = memnew(VBoxContainer);
4494 overwrite_dialog->add_child(overwrite_dialog_vb);
4495
4496 overwrite_dialog_header = memnew(Label);
4497 overwrite_dialog_vb->add_child(overwrite_dialog_header);
4498
4499 overwrite_dialog_scroll = memnew(ScrollContainer);
4500 overwrite_dialog_vb->add_child(overwrite_dialog_scroll);
4501 overwrite_dialog_scroll->set_custom_minimum_size(Vector2(400, 600) * EDSCALE);
4502
4503 overwrite_dialog_file_list = memnew(Label);
4504 overwrite_dialog_scroll->add_child(overwrite_dialog_file_list);
4505
4506 overwrite_dialog_footer = memnew(Label);
4507 overwrite_dialog_vb->add_child(overwrite_dialog_footer);
4508
4509 make_dir_dialog = memnew(DirectoryCreateDialog);
4510 add_child(make_dir_dialog);
4511
4512 make_scene_dialog = memnew(SceneCreateDialog);
4513 add_child(make_scene_dialog);
4514 make_scene_dialog->connect(SceneStringName(confirmed), callable_mp(this, &FileSystemDock::_make_scene_confirm));
4515
4516 make_script_dialog = memnew(ScriptCreateDialog);
4517 make_script_dialog->set_title(TTRC("Create Script"));
4518 add_child(make_script_dialog);
4519 make_script_dialog->connect("script_created", callable_mp(this, &FileSystemDock::_script_or_shader_created));
4520
4521 make_shader_dialog = memnew(ShaderCreateDialog);
4522 add_child(make_shader_dialog);
4523 make_shader_dialog->connect("shader_created", callable_mp(this, &FileSystemDock::_script_or_shader_created));
4524 make_shader_dialog->connect("shader_include_created", callable_mp(this, &FileSystemDock::_script_or_shader_created));
4525
4526 new_resource_dialog = memnew(CreateDialog);
4527 add_child(new_resource_dialog);
4528 new_resource_dialog->set_base_type("Resource");
4529 new_resource_dialog->connect("create", callable_mp(this, &FileSystemDock::_resource_created));
4530
4531 conversion_dialog = memnew(ConfirmationDialog);
4532 add_child(conversion_dialog);
4533 conversion_dialog->set_ok_button_text(TTRC("Convert"));
4534 conversion_dialog->connect(SceneStringName(confirmed), callable_mp(this, &FileSystemDock::_convert_dialog_action));
4535
4536 move_confirm_dialog = memnew(ConfirmationDialog);
4537 add_child(move_confirm_dialog);
4538 move_confirm_dialog->connect(SceneStringName(confirmed), callable_mp(this, &FileSystemDock::_move_confirm));
4539
4540 uncollapsed_paths_before_search = Vector<String>();
4541
4542 tree_update_id = 0;
4543
4544 history_pos = 0;
4545 history_max_size = 20;
4546 history.push_back("res://");
4547
4548 display_mode = DISPLAY_MODE_TREE_ONLY;
4549 old_display_mode = DISPLAY_MODE_TREE_ONLY;
4550 file_list_display_mode = FILE_LIST_DISPLAY_THUMBNAILS;
4551
4552 ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &FileSystemDock::_project_settings_changed));
4553 EditorSettings::get_singleton()->connect("_favorites_changed", callable_mp(this, &FileSystemDock::update_all));
4554 main_scene_path = ResourceUID::ensure_path(GLOBAL_GET("application/run/main_scene"));
4555
4556 add_resource_tooltip_plugin(memnew(EditorTextureTooltipPlugin));
4557 add_resource_tooltip_plugin(memnew(EditorAudioStreamTooltipPlugin));
4558 }
4559
4560 FileSystemDock::~FileSystemDock() {
4561 singleton = nullptr;
4562 }