Debian Codegraph @

editor/project_manager/project_list.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* project_list.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 "project_list.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/io/dir_access.h"
35 #include "core/os/time.h"
36 #include "core/version.h"
37 #include "editor/editor_string_names.h"
38 #include "editor/file_system/editor_paths.h"
39 #include "editor/project_manager/project_manager.h"
40 #include "editor/project_manager/project_tag.h"
41 #include "editor/settings/editor_settings.h"
42 #include "editor/themes/editor_scale.h"
43 #include "scene/gui/button.h"
44 #include "scene/gui/dialogs.h"
45 #include "scene/gui/label.h"
46 #include "scene/gui/line_edit.h"
47 #include "scene/gui/popup_menu.h"
48 #include "scene/gui/progress_bar.h"
49 #include "scene/gui/texture_button.h"
50 #include "scene/gui/texture_rect.h"
51 #include "scene/resources/image_texture.h"
52
53 void ProjectListItemControl::_notification(int p_what) {
54 switch (p_what) {
55 case NOTIFICATION_THEME_CHANGED: {
56 if (icon_needs_reload) {
57 // The project icon may not be loaded by the time the control is displayed,
58 // so use a loading placeholder.
59 project_icon->set_texture(get_editor_theme_icon(SNAME("ProjectIconLoading")));
60 }
61
62 project_title->begin_bulk_theme_override();
63 project_title->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("title"), EditorStringName(EditorFonts)));
64 project_title->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size(SNAME("title_size"), EditorStringName(EditorFonts)));
65 project_title->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("ProjectList")));
66 project_title->end_bulk_theme_override();
67
68 project_path->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("ProjectList")));
69 project_unsupported_features->set_texture(get_editor_theme_icon(SNAME("NodeWarning")));
70
71 favorite_focus_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
72 _update_favorite_button_focus_color();
73 if (is_favorite) {
74 favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Favorites")));
75 } else {
76 favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Unfavorite")));
77 }
78
79 if (project_is_missing) {
80 explore_button->set_button_icon(get_editor_theme_icon(SNAME("FileBroken")));
81 #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
82 } else {
83 explore_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));
84 #endif
85 }
86
87 if (touch_menu_button) {
88 touch_menu_button->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
89 }
90 } break;
91
92 case NOTIFICATION_MOUSE_ENTER: {
93 is_hovering = true;
94 queue_redraw();
95 queue_accessibility_update();
96 } break;
97
98 case NOTIFICATION_MOUSE_EXIT: {
99 is_hovering = false;
100 queue_redraw();
101 queue_accessibility_update();
102 } break;
103
104 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
105 RID ae = get_accessibility_element();
106 ERR_FAIL_COND(ae.is_null());
107
108 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LIST_BOX_OPTION);
109 DisplayServer::get_singleton()->accessibility_update_set_name(ae, TTR("Project") + " " + project_title->get_text());
110 DisplayServer::get_singleton()->accessibility_update_set_value(ae, project_title->get_text());
111
112 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &ProjectListItemControl::_accessibility_action_open));
113 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &ProjectListItemControl::_accessibility_action_scroll_into_view));
114 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &ProjectListItemControl::_accessibility_action_focus));
115 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &ProjectListItemControl::_accessibility_action_blur));
116
117 ProjectList *pl = get_list();
118 if (pl) {
119 DisplayServer::get_singleton()->accessibility_update_set_list_item_index(ae, pl->get_index(this));
120 }
121 DisplayServer::get_singleton()->accessibility_update_set_list_item_level(ae, 0);
122 DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(ae, is_selected);
123 } break;
124
125 case NOTIFICATION_FOCUS_ENTER: {
126 ProjectList *pl = get_list();
127 if (pl) {
128 int idx = pl->get_index(this);
129 if (idx >= 0) {
130 // has_focus(true) is false on mouse-initiated focus, true on keyboard navigation.
131 pl->select_project(idx, !has_focus(true));
132 pl->ensure_project_visible(idx);
133
134 pl->emit_signal(SNAME(ProjectList::SIGNAL_SELECTION_CHANGED));
135 }
136 }
137 } break;
138
139 case NOTIFICATION_DRAW: {
140 if (is_selected && is_hovering) {
141 draw_style_box(get_theme_stylebox(SNAME("hover_pressed"), SNAME("ProjectList")), Rect2(Point2(), get_size()));
142 } else if (is_selected) {
143 draw_style_box(get_theme_stylebox(SNAME("selected"), SNAME("ProjectList")), Rect2(Point2(), get_size()));
144 } else if (is_hovering) {
145 draw_style_box(get_theme_stylebox(SNAME("hovered"), SNAME("ProjectList")), Rect2(Point2(), get_size()));
146 }
147 // Due to how this control works, we can't rely on the built-in way of checking for focus visibility.
148 if (has_focus() && !is_focus_hidden) {
149 draw_style_box(get_theme_stylebox(SNAME("focus"), SNAME("ProjectList")), Rect2(Point2(), get_size()));
150 }
151
152 draw_line(Point2(0, get_size().y + 1), Point2(get_size().x, get_size().y + 1), get_theme_color(SNAME("guide_color"), SNAME("ProjectList")));
153 } break;
154 }
155 }
156
157 ProjectList *ProjectListItemControl::get_list() const {
158 if (!is_inside_tree()) {
159 return nullptr;
160 }
161 ProjectList *pl = Object::cast_to<ProjectList>(get_parent()->get_parent());
162 return pl;
163 }
164
165 void ProjectListItemControl::_accessibility_action_scroll_into_view(const Variant &p_data) {
166 ProjectList *pl = get_list();
167 if (pl) {
168 int idx = pl->get_index(this);
169 if (idx >= 0) {
170 pl->ensure_project_visible(idx);
171 }
172 }
173 }
174
175 void ProjectListItemControl::_accessibility_action_open(const Variant &p_data) {
176 ProjectList *pl = get_list();
177 if (pl && !pl->project_opening_initiated) {
178 pl->emit_signal(SNAME(ProjectList::SIGNAL_PROJECT_ASK_OPEN));
179 }
180 }
181
182 void ProjectListItemControl::_accessibility_action_focus(const Variant &p_data) {
183 ProjectList *pl = get_list();
184 if (pl) {
185 int idx = pl->get_index(this);
186 if (idx >= 0) {
187 pl->ensure_project_visible(idx);
188 pl->select_project(idx);
189 }
190 }
191 }
192
193 void ProjectListItemControl::_accessibility_action_blur(const Variant &p_data) {
194 ProjectList *pl = get_list();
195 if (pl) {
196 int idx = pl->get_index(this);
197 if (idx >= 0) {
198 pl->ensure_project_visible(idx);
199 pl->deselect_project(idx);
200 }
201 }
202 }
203 void ProjectListItemControl::_update_favorite_button_focus_color() {
204 if (favorite_button->has_focus()) {
205 favorite_button->set_self_modulate(favorite_focus_color);
206 } else {
207 favorite_button->set_self_modulate(Color(1.0, 1.0, 1.0, 1.0));
208 }
209 }
210
211 void ProjectListItemControl::_favorite_button_pressed() {
212 emit_signal(SNAME("favorite_pressed"));
213 }
214
215 void ProjectListItemControl::_explore_button_pressed() {
216 emit_signal(SNAME("explore_pressed"));
217 }
218
219 void ProjectListItemControl::_request_menu() {
220 emit_signal(SNAME("request_menu"), Vector2(touch_menu_button->get_position()));
221 }
222
223 void ProjectListItemControl::set_project_title(const String &p_title) {
224 project_title->set_text(p_title);
225 project_title->set_accessibility_name(TTRC("Project Name"));
226 queue_accessibility_update();
227 }
228
229 void ProjectListItemControl::set_project_path(const String &p_path) {
230 project_path->set_text(p_path);
231 project_path->set_accessibility_name(TTRC("Project Path"));
232 queue_accessibility_update();
233 }
234
235 void ProjectListItemControl::set_tags(const PackedStringArray &p_tags, ProjectList *p_parent_list) {
236 for (const String &tag : p_tags) {
237 ProjectTag *tag_control = memnew(ProjectTag(tag));
238 tag_container->add_child(tag_control);
239 tag_control->connect_button_to(callable_mp(p_parent_list, &ProjectList::add_search_tag).bind(tag));
240 }
241 }
242
243 void ProjectListItemControl::set_project_icon(const Ref<Texture2D> &p_icon) {
244 icon_needs_reload = false;
245
246 // The default project icon is 128×128 to look crisp on hiDPI displays,
247 // but we want the actual displayed size to be 64×64 on loDPI displays.
248 project_icon->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
249 project_icon->set_custom_minimum_size(Size2(64, 64) * EDSCALE);
250 project_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
251
252 project_icon->set_texture(p_icon);
253 }
254
255 void ProjectListItemControl::set_last_edited_info(const String &p_info) {
256 last_edited_info->set_text(p_info);
257 }
258
259 void ProjectListItemControl::set_project_version(const String &p_info) {
260 project_version->set_text(p_info);
261 }
262
263 void ProjectListItemControl::set_unsupported_features(PackedStringArray p_features) {
264 if (p_features.size() > 0) {
265 String tooltip_text = "";
266 for (int i = 0; i < p_features.size(); i++) {
267 if (ProjectList::project_feature_looks_like_version(p_features[i])) {
268 PackedStringArray project_version_split = p_features[i].split(".");
269 int project_version_major = 0, project_version_minor = 0;
270 if (project_version_split.size() >= 2) {
271 project_version_major = project_version_split[0].to_int();
272 project_version_minor = project_version_split[1].to_int();
273 }
274 if (GODOT_VERSION_MAJOR != project_version_major || GODOT_VERSION_MINOR <= project_version_minor) {
275 // Don't show a warning if the project was last edited in a previous minor version.
276 tooltip_text += TTR("This project was last edited in a different Godot version: ") + p_features[i] + "\n";
277 }
278 p_features.remove_at(i);
279 i--;
280 }
281 }
282 if (p_features.size() > 0) {
283 String unsupported_features_str = String(", ").join(p_features);
284 tooltip_text += TTR("This project uses features unsupported by the current build:") + "\n" + unsupported_features_str;
285 }
286 if (tooltip_text.is_empty()) {
287 return;
288 }
289 project_version->set_tooltip_text(tooltip_text);
290 project_unsupported_features->set_focus_mode(FOCUS_ACCESSIBILITY);
291 project_unsupported_features->set_tooltip_text(tooltip_text);
292 project_unsupported_features->show();
293 } else {
294 project_unsupported_features->hide();
295 }
296 }
297
298 bool ProjectListItemControl::should_load_project_icon() const {
299 return icon_needs_reload;
300 }
301
302 void ProjectListItemControl::set_selected(bool p_selected, bool p_hide_focus) {
303 is_selected = p_selected;
304 is_focus_hidden = is_selected && p_hide_focus;
305 queue_redraw();
306 queue_accessibility_update();
307 }
308
309 void ProjectListItemControl::set_is_favorite(bool p_favorite) {
310 is_favorite = p_favorite;
311 if (p_favorite) {
312 favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Favorites")));
313 favorite_button->set_accessibility_name(TTRC("Remove from Favorites"));
314 } else {
315 favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Unfavorite")));
316 favorite_button->set_accessibility_name(TTRC("Add to Favorites"));
317 }
318 }
319
320 void ProjectListItemControl::set_is_missing(bool p_missing) {
321 project_is_missing = p_missing;
322
323 if (project_is_missing) {
324 project_icon->set_modulate(Color(1, 1, 1, 0.5));
325
326 explore_button->set_button_icon(get_editor_theme_icon(SNAME("FileBroken")));
327 explore_button->set_tooltip_text(TTRC("Error: Project is missing on the filesystem."));
328 } else {
329 #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
330 explore_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));
331 explore_button->set_tooltip_text(TTRC("Show in File Manager"));
332 #else
333 // Opening the system file manager is not supported on the Android and web editors.
334 explore_button->hide();
335 #endif
336 }
337 }
338
339 void ProjectListItemControl::set_is_grayed(bool p_grayed) {
340 if (p_grayed) {
341 main_vbox->set_modulate(Color(1, 1, 1, 0.5));
342 // Don't make the icon less prominent if the parent is already grayed out.
343 explore_button->set_modulate(Color(1, 1, 1, 1.0));
344 } else {
345 main_vbox->set_modulate(Color(1, 1, 1, 1.0));
346 explore_button->set_modulate(Color(1, 1, 1, 0.5));
347 }
348 }
349
350 void ProjectListItemControl::_bind_methods() {
351 ADD_SIGNAL(MethodInfo("favorite_pressed"));
352 ADD_SIGNAL(MethodInfo("explore_pressed"));
353 ADD_SIGNAL(MethodInfo("request_menu"));
354 }
355
356 ProjectListItemControl::ProjectListItemControl() {
357 set_focus_mode(FocusMode::FOCUS_ALL);
358 set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
359
360 // Left spacer.
361 add_child(memnew(Control));
362
363 VBoxContainer *favorite_box = memnew(VBoxContainer);
364 favorite_box->set_alignment(BoxContainer::ALIGNMENT_CENTER);
365 add_child(favorite_box);
366
367 favorite_button = memnew(TextureButton);
368 favorite_button->set_name("FavoriteButton");
369 favorite_button->set_tooltip_text(TTRC("Toggle Favorite"));
370 favorite_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_ALWAYS);
371 // This makes the project's "hover" style display correctly when hovering the favorite icon.
372 favorite_button->set_mouse_filter(MOUSE_FILTER_PASS);
373 favorite_box->add_child(favorite_button);
374 favorite_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_favorite_button_pressed));
375 favorite_button->connect(SceneStringName(focus_entered), callable_mp(this, &ProjectListItemControl::_update_favorite_button_focus_color));
376 favorite_button->connect(SceneStringName(focus_exited), callable_mp(this, &ProjectListItemControl::_update_favorite_button_focus_color));
377
378 project_icon = memnew(TextureRect);
379 project_icon->set_name("ProjectIcon");
380 project_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
381 add_child(project_icon);
382
383 main_vbox = memnew(VBoxContainer);
384 main_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
385 add_child(main_vbox);
386
387 Control *ec = memnew(Control);
388 ec->set_custom_minimum_size(Size2(0, 1));
389 ec->set_mouse_filter(MOUSE_FILTER_PASS);
390 main_vbox->add_child(ec);
391
392 // Top half, title, tags and unsupported features labels.
393 {
394 HBoxContainer *title_hb = memnew(HBoxContainer);
395 main_vbox->add_child(title_hb);
396
397 project_title = memnew(Label);
398 project_title->set_focus_mode(FOCUS_ACCESSIBILITY);
399 project_title->set_name("ProjectName");
400 project_title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
401 project_title->set_clip_text(true);
402 title_hb->add_child(project_title);
403
404 tag_container = memnew(HBoxContainer);
405 title_hb->add_child(tag_container);
406 }
407
408 // Bottom half, containing the path and view folder button.
409 {
410 HBoxContainer *path_hb = memnew(HBoxContainer);
411 path_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
412 main_vbox->add_child(path_hb);
413
414 explore_button = memnew(Button);
415 explore_button->set_name("ExploreButton");
416 explore_button->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_ALWAYS);
417 explore_button->set_mouse_filter(MOUSE_FILTER_PASS);
418 explore_button->set_tooltip_text(TTRC("Open in file manager"));
419 explore_button->set_flat(true);
420 path_hb->add_child(explore_button);
421 explore_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_explore_button_pressed));
422
423 project_path = memnew(Label);
424 project_path->set_name("ProjectPath");
425 project_path->set_focus_mode(FOCUS_ACCESSIBILITY);
426 project_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
427 project_path->set_clip_text(true);
428 project_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
429 project_path->set_modulate(Color(1, 1, 1, 0.5));
430 path_hb->add_child(project_path);
431
432 project_unsupported_features = memnew(TextureRect);
433 project_unsupported_features->set_name("ProjectUnsupportedFeatures");
434 project_unsupported_features->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
435 path_hb->add_child(project_unsupported_features);
436 project_unsupported_features->hide();
437
438 project_version = memnew(Label);
439 project_version->set_focus_mode(FOCUS_ACCESSIBILITY);
440 project_version->set_name("ProjectVersion");
441 project_version->set_mouse_filter(Control::MOUSE_FILTER_PASS);
442 path_hb->add_child(project_version);
443
444 last_edited_info = memnew(Label);
445 last_edited_info->set_focus_mode(FOCUS_ACCESSIBILITY);
446 last_edited_info->set_name("LastEditedInfo");
447 last_edited_info->set_mouse_filter(Control::MOUSE_FILTER_PASS);
448 last_edited_info->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_ALWAYS);
449 last_edited_info->set_tooltip_text(TTRC("Last edited timestamp"));
450 last_edited_info->set_modulate(Color(1, 1, 1, 0.5));
451 path_hb->add_child(last_edited_info);
452 }
453
454 if (DisplayServer::get_singleton()->is_touchscreen_available()) {
455 touch_menu_button = memnew(Button);
456 touch_menu_button->set_theme_type_variation(SceneStringName(FlatButton));
457 touch_menu_button->set_v_size_flags(SIZE_SHRINK_CENTER);
458 add_child(touch_menu_button);
459 touch_menu_button->set_mouse_filter(MOUSE_FILTER_PASS);
460 touch_menu_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_request_menu));
461 }
462
463 // Right spacer.
464 add_child(memnew(Control));
465 }
466
467 struct ProjectListComparator {
468 ProjectList::FilterOption order_option = ProjectList::FilterOption::EDIT_DATE;
469
470 // operator<
471 _FORCE_INLINE_ bool operator()(const ProjectList::Item &a, const ProjectList::Item &b) const {
472 if (a.favorite && !b.favorite) {
473 return true;
474 }
475 if (b.favorite && !a.favorite) {
476 return false;
477 }
478 switch (order_option) {
479 case ProjectList::PATH:
480 return a.path < b.path;
481 case ProjectList::EDIT_DATE:
482 return a.last_edited > b.last_edited;
483 case ProjectList::TAGS:
484 return a.tag_sort_string < b.tag_sort_string;
485 default:
486 return a.project_name < b.project_name;
487 }
488 }
489 };
490
491 // Helpers.
492
493 bool ProjectList::project_feature_looks_like_version(const String &p_feature) {
494 return p_feature.contains_char('.') && p_feature.substr(0, 3).is_numeric();
495 }
496
497 // Notifications.
498
499 void ProjectList::_notification(int p_what) {
500 switch (p_what) {
501 case NOTIFICATION_TRANSLATION_CHANGED: {
502 if (is_ready()) {
503 // FIXME: Technically this only needs to update some dynamic texts, not the whole list.
504 update_project_list();
505 }
506 } break;
507
508 case NOTIFICATION_THEME_CHANGED: {
509 if (project_context_menu) {
510 _update_menu_icons();
511 }
512 } break;
513
514 case NOTIFICATION_PROCESS: {
515 // Load icons as a coroutine to speed up launch when you have hundreds of projects.
516 if (_icon_load_index < _projects.size()) {
517 Item &item = _projects.write[_icon_load_index];
518 if (item.control->should_load_project_icon()) {
519 _load_project_icon(_icon_load_index);
520 }
521 _icon_load_index++;
522
523 // Scan directories in thread to avoid blocking the window.
524 } else if (scan_data && scan_data->scan_in_progress.is_set()) {
525 // Wait for the thread.
526 } else {
527 set_process(false);
528 if (scan_data) {
529 _scan_finished();
530 }
531 }
532 } break;
533
534 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
535 RID ae = get_accessibility_element();
536 ERR_FAIL_COND(ae.is_null());
537
538 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LIST_BOX);
539 DisplayServer::get_singleton()->accessibility_update_set_list_item_count(ae, _projects.size());
540 DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_MULTISELECTABLE, false);
541 }
542 }
543 }
544
545 // Projects scan.
546
547 void ProjectList::_scan_thread(void *p_scan_data) {
548 ScanData *scan_data = static_cast<ScanData *>(p_scan_data);
549
550 for (const String &base_path : scan_data->paths_to_scan) {
551 print_verbose(vformat("Scanning for projects in \"%s\".", base_path));
552 _scan_folder_recursive(base_path, &scan_data->found_projects, scan_data->scan_in_progress);
553
554 if (!scan_data->scan_in_progress.is_set()) {
555 print_verbose("Scan aborted.");
556 break;
557 }
558 }
559 print_verbose(vformat("Found %d project(s).", scan_data->found_projects.size()));
560 scan_data->scan_in_progress.clear();
561 }
562
563 void ProjectList::_scan_finished() {
564 if (scan_data->scan_in_progress.is_set()) {
565 // Abort scanning.
566 scan_data->scan_in_progress.clear();
567 }
568
569 scan_data->thread->wait_to_finish();
570 memdelete(scan_data->thread);
571 if (scan_progress) {
572 scan_progress->hide();
573 }
574
575 for (const String &E : scan_data->found_projects) {
576 add_project(E, false);
577 }
578 memdelete(scan_data);
579 scan_data = nullptr;
580
581 save_config();
582
583 if (ProjectManager::get_singleton()->is_initialized()) {
584 update_project_list();
585 }
586 }
587
588 // Initialization & loading.
589
590 void ProjectList::_migrate_config() {
591 // Proposal #1637 moved the project list from editor settings to a separate config file
592 // If the new config file doesn't exist, populate it from EditorSettings
593 if (FileAccess::exists(_config_path)) {
594 return;
595 }
596
597 List<PropertyInfo> properties;
598 EditorSettings::get_singleton()->get_property_list(&properties);
599
600 for (const PropertyInfo &E : properties) {
601 // This is actually something like "projects/C:::Documents::Godot::Projects::MyGame"
602 String property_key = E.name;
603 if (!property_key.begins_with("projects/")) {
604 continue;
605 }
606
607 String path = EDITOR_GET(property_key);
608 print_line("Migrating legacy project '" + path + "'.");
609
610 String favoriteKey = "favorite_projects/" + property_key.get_slicec('/', 1);
611 bool favorite = EditorSettings::get_singleton()->has_setting(favoriteKey);
612 add_project(path, favorite);
613 if (favorite) {
614 EditorSettings::get_singleton()->erase(favoriteKey);
615 }
616 EditorSettings::get_singleton()->erase(property_key);
617 }
618
619 save_config();
620 }
621
622 void ProjectList::save_config() {
623 _config.save(_config_path);
624 }
625
626 // Load project data from p_property_key and return it in a ProjectList::Item.
627 // p_favorite is passed directly into the Item.
628 ProjectList::Item ProjectList::load_project_data(const String &p_path, bool p_favorite) {
629 String conf = p_path.path_join("project.godot");
630 bool grayed = false;
631 bool missing = false;
632 bool recovery_mode = false;
633
634 Ref<ConfigFile> cf = memnew(ConfigFile);
635 Error cf_err = cf->load(conf);
636
637 int config_version = 0;
638 String cf_project_name;
639 String project_name = TTR("Unnamed Project");
640 if (cf_err == OK) {
641 cf_project_name = cf->get_value("application", "config/name", "");
642 if (!cf_project_name.is_empty()) {
643 project_name = cf_project_name.xml_unescape();
644 }
645 config_version = (int)cf->get_value("", "config_version", 0);
646 }
647
648 if (config_version > ProjectSettings::CONFIG_VERSION) {
649 // Comes from an incompatible (more recent) Godot version, gray it out.
650 grayed = true;
651 }
652
653 const String description = cf->get_value("application", "config/description", "");
654 const PackedStringArray tags = cf->get_value("application", "config/tags", PackedStringArray());
655 const String main_scene = cf->get_value("application", "run/main_scene", "");
656
657 String icon = cf->get_value("application", "config/icon", "");
658 if (icon.begins_with("uid://")) {
659 Error err;
660 Ref<FileAccess> file = FileAccess::open(p_path.path_join(".godot/uid_cache.bin"), FileAccess::READ, &err);
661 if (err == OK) {
662 icon = ResourceUID::get_path_from_cache(file, icon);
663 if (icon.is_empty()) {
664 WARN_PRINT(vformat("Could not load icon from UID for project at path \"%s\". Make sure UID cache exists.", p_path));
665 }
666 } else {
667 // Cache does not exist yet, so ignore and fallback to default icon.
668 icon = "";
669 }
670 }
671
672 PackedStringArray project_features = cf->get_value("application", "config/features", PackedStringArray());
673 PackedStringArray unsupported_features = ProjectSettings::get_unsupported_features(project_features);
674
675 String project_version = "?";
676 for (int i = 0; i < project_features.size(); i++) {
677 if (ProjectList::project_feature_looks_like_version(project_features[i])) {
678 project_version = project_features[i];
679 break;
680 }
681 }
682
683 if (config_version < ProjectSettings::CONFIG_VERSION) {
684 // Previous versions may not have unsupported features.
685 if (config_version == 4) {
686 unsupported_features.push_back("3.x");
687 project_version = "3.x";
688 } else {
689 unsupported_features.push_back(TTR("Unknown version"));
690 }
691 }
692
693 uint64_t last_edited = 0;
694 if (cf_err == OK) {
695 // The modification date marks the date the project was last edited.
696 // This is because the `project.godot` file will always be modified
697 // when editing a project (but not when running it).
698 last_edited = FileAccess::get_modified_time(conf);
699
700 String fscache = p_path.path_join(".fscache");
701 if (FileAccess::exists(fscache)) {
702 uint64_t cache_modified = FileAccess::get_modified_time(fscache);
703 if (cache_modified > last_edited) {
704 last_edited = cache_modified;
705 }
706 }
707 } else {
708 grayed = true;
709 missing = true;
710 }
711
712 for (const String &tag : tags) {
713 ProjectManager::get_singleton()->add_new_tag(tag);
714 }
715
716 // We can't use OS::get_user_dir() because it attempts to load paths from the current loaded project through ProjectSettings,
717 // while here we're parsing project files externally. Therefore, we have to replicate its behavior.
718 String user_dir;
719 if (!cf_project_name.is_empty()) {
720 String appname = OS::get_singleton()->get_safe_dir_name(cf_project_name);
721 bool use_custom_dir = cf->get_value("application", "config/use_custom_user_dir", false);
722 if (use_custom_dir) {
723 String custom_dir = OS::get_singleton()->get_safe_dir_name(cf->get_value("application", "config/custom_user_dir_name", ""), true);
724 if (custom_dir.is_empty()) {
725 custom_dir = appname;
726 }
727 user_dir = custom_dir;
728 } else {
729 user_dir = OS::get_singleton()->get_godot_dir_name().path_join("app_userdata").path_join(appname);
730 }
731 } else {
732 user_dir = OS::get_singleton()->get_godot_dir_name().path_join("app_userdata").path_join("[unnamed project]");
733 }
734
735 String recovery_mode_lock_file = OS::get_singleton()->get_user_data_dir(user_dir).path_join(".recovery_mode_lock");
736 recovery_mode = FileAccess::exists(recovery_mode_lock_file);
737
738 return Item(project_name, description, project_version, tags, p_path, icon, main_scene, unsupported_features, last_edited, p_favorite, grayed, missing, recovery_mode, config_version);
739 }
740
741 void ProjectList::_update_icons_async() {
742 _icon_load_index = 0;
743 set_process(true);
744 }
745
746 void ProjectList::_load_project_icon(int p_index) {
747 Item &item = _projects.write[p_index];
748
749 Ref<Texture2D> default_icon = get_editor_theme_icon(SNAME("DefaultProjectIcon"));
750 Ref<Texture2D> icon;
751 if (!item.icon.is_empty()) {
752 Ref<Image> img;
753 img.instantiate();
754 Error err = img->load(item.icon.replace_first("res://", item.path + "/"));
755 if (err == OK) {
756 img->resize(default_icon->get_width(), default_icon->get_height(), Image::INTERPOLATE_LANCZOS);
757 icon = ImageTexture::create_from_image(img);
758 }
759 }
760 if (icon.is_null()) {
761 icon = default_icon;
762 }
763
764 item.control->set_project_icon(icon);
765 }
766
767 // Project list updates.
768
769 void ProjectList::update_project_list() {
770 // This is a full, hard reload of the list. Don't call this unless really required, it's expensive.
771 // If you have 150 projects, it may read through 150 files on your disk at once + load 150 icons.
772 // FIXME: Does it really have to be a full, hard reload? Runtime updates should be made much cheaper.
773
774 if (ProjectManager::get_singleton()->is_initialized()) {
775 // Clear whole list
776 for (int i = 0; i < _projects.size(); ++i) {
777 Item &project = _projects.write[i];
778 CRASH_COND(project.control == nullptr);
779 memdelete(project.control); // Why not queue_free()?
780 }
781
782 _projects.clear();
783 _last_clicked = "";
784 _selected_project_paths.clear();
785
786 load_project_list();
787 }
788
789 // Create controls
790 for (int i = 0; i < _projects.size(); ++i) {
791 _create_project_item_control(i);
792 }
793
794 sort_projects();
795 _update_icons_async();
796 update_dock_menu();
797
798 set_v_scroll(0);
799 emit_signal(SNAME(SIGNAL_LIST_CHANGED));
800 queue_accessibility_update();
801 }
802
803 void ProjectList::sort_projects() {
804 SortArray<Item, ProjectListComparator> sorter;
805 sorter.compare.order_option = _order_option;
806 sorter.sort(_projects.ptrw(), _projects.size());
807
808 String search_term;
809 PackedStringArray tags;
810
811 if (!_search_term.is_empty()) {
812 PackedStringArray search_parts = _search_term.split(" ");
813 if (search_parts.size() > 1 || search_parts[0].begins_with("tag:")) {
814 PackedStringArray remaining;
815 for (const String &part : search_parts) {
816 if (part.begins_with("tag:")) {
817 tags.push_back(part.get_slicec(':', 1));
818 } else {
819 remaining.append(part);
820 }
821 }
822 search_term = String(" ").join(remaining); // Search term without tags.
823 } else {
824 search_term = _search_term;
825 }
826 }
827
828 for (int i = 0; i < _projects.size(); ++i) {
829 Item &item = _projects.write[i];
830
831 bool item_visible = true;
832 if (!_search_term.is_empty()) {
833 String search_path;
834 if (search_term.contains_char('/')) {
835 // Search path will match the whole path
836 search_path = item.path;
837 } else {
838 // Search path will only match the last path component to make searching more strict
839 search_path = item.path.get_file();
840 }
841
842 bool missing_tags = false;
843 for (const String &tag : tags) {
844 if (!item.tags.has(tag)) {
845 missing_tags = true;
846 break;
847 }
848 }
849
850 // When searching, display projects whose name or path contain the search term and whose tags match the searched tags.
851 item_visible = !missing_tags && (search_term.is_empty() || item.project_name.containsn(search_term) || search_path.containsn(search_term));
852 }
853
854 item.control->set_visible(item_visible);
855 }
856
857 for (int i = 0; i < _projects.size(); ++i) {
858 Item &item = _projects.write[i];
859 item.control->get_parent()->move_child(item.control, i);
860 }
861
862 // Rewind the coroutine because order of projects changed
863 _update_icons_async();
864 update_dock_menu();
865 queue_accessibility_update();
866 }
867
868 int ProjectList::get_project_count() const {
869 return _projects.size();
870 }
871
872 void ProjectList::find_projects(const String &p_path) {
873 PackedStringArray paths = { p_path };
874 find_projects_multiple(paths);
875 }
876
877 void ProjectList::find_projects_multiple(const PackedStringArray &p_paths) {
878 if (!scan_progress && is_inside_tree()) {
879 scan_progress = memnew(AcceptDialog);
880 scan_progress->set_title(TTRC("Scanning"));
881 scan_progress->set_ok_button_text(TTRC("Cancel"));
882
883 VBoxContainer *vb = memnew(VBoxContainer);
884 scan_progress->add_child(vb);
885
886 Label *label = memnew(Label);
887 label->set_text(TTRC("Scanning for projects..."));
888 vb->add_child(label);
889
890 ProgressBar *progress = memnew(ProgressBar);
891 progress->set_indeterminate(true);
892 vb->add_child(progress);
893
894 add_child(scan_progress);
895 scan_progress->connect(SceneStringName(confirmed), callable_mp(this, &ProjectList::_scan_finished));
896 scan_progress->connect("canceled", callable_mp(this, &ProjectList::_scan_finished));
897 }
898
899 scan_data = memnew(ScanData);
900 scan_data->paths_to_scan = p_paths;
901 scan_data->scan_in_progress.set();
902
903 scan_data->thread = memnew(Thread);
904 scan_data->thread->start(_scan_thread, scan_data);
905
906 if (scan_progress) {
907 scan_progress->reset_size();
908 scan_progress->popup_centered();
909 }
910 set_process(true);
911 }
912
913 void ProjectList::load_project_list() {
914 _config.load(_config_path);
915 Vector<String> sections = _config.get_sections();
916
917 for (const String &path : sections) {
918 bool favorite = _config.get_value(path, "favorite", false);
919 _projects.push_back(load_project_data(path, favorite));
920 }
921 }
922
923 void ProjectList::_scan_folder_recursive(const String &p_path, List<String> *r_projects, const SafeFlag &p_scan_active) {
924 if (!p_scan_active.is_set()) {
925 return;
926 }
927
928 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
929 Error error = da->change_dir(p_path);
930 ERR_FAIL_COND_MSG(error != OK, vformat("Failed to open the path \"%s\" for scanning (code %d).", p_path, error));
931
932 da->list_dir_begin();
933 String n = da->get_next();
934 while (!n.is_empty()) {
935 if (!p_scan_active.is_set()) {
936 return;
937 }
938
939 if (da->current_is_dir() && n[0] != '.') {
940 _scan_folder_recursive(da->get_current_dir().path_join(n), r_projects, p_scan_active);
941 } else if (n == "project.godot") {
942 r_projects->push_back(da->get_current_dir());
943 }
944 n = da->get_next();
945 }
946 da->list_dir_end();
947 }
948
949 // Project list items.
950
951 void ProjectList::add_project(const String &dir_path, bool favorite) {
952 if (!_config.has_section(dir_path)) {
953 _config.set_value(dir_path, "favorite", favorite);
954 }
955 queue_accessibility_update();
956 }
957
958 void ProjectList::set_project_version(const String &p_project_path, int p_version) {
959 for (ProjectList::Item &E : _projects) {
960 if (E.path == p_project_path) {
961 E.version = p_version;
962 break;
963 }
964 }
965 }
966
967 int ProjectList::refresh_project(const String &dir_path) {
968 // Reloads information about a specific project.
969 // If it wasn't loaded and should be in the list, it is added (i.e new project).
970 // If it isn't in the list anymore, it is removed.
971 // If it is in the list but doesn't exist anymore, it is marked as missing.
972
973 bool should_be_in_list = _config.has_section(dir_path);
974 bool is_favorite = _config.get_value(dir_path, "favorite", false);
975
976 bool was_selected = _selected_project_paths.has(dir_path);
977
978 // Remove item in any case
979 for (int i = 0; i < _projects.size(); ++i) {
980 const Item &existing_item = _projects[i];
981 if (existing_item.path == dir_path) {
982 _remove_project(i, false);
983 break;
984 }
985 }
986
987 int index = -1;
988 if (should_be_in_list) {
989 // Recreate it with updated info
990
991 Item item = load_project_data(dir_path, is_favorite);
992
993 _projects.push_back(item);
994 _create_project_item_control(_projects.size() - 1);
995
996 sort_projects();
997
998 for (int i = 0; i < _projects.size(); ++i) {
999 if (_projects[i].path == dir_path) {
1000 if (was_selected) {
1001 ensure_project_visible(i);
1002 }
1003 _load_project_icon(i);
1004
1005 index = i;
1006 break;
1007 }
1008 }
1009 }
1010
1011 return index;
1012 }
1013
1014 int ProjectList::get_index(const ProjectListItemControl *p_control) const {
1015 for (int i = 0; i < _projects.size(); ++i) {
1016 if (_projects[i].control == p_control) {
1017 return i;
1018 }
1019 }
1020 return -1;
1021 }
1022
1023 void ProjectList::ensure_project_visible(int p_index) {
1024 const Item &item = _projects[p_index];
1025 // Since follow focus is enabled.
1026 item.control->grab_focus(true);
1027 }
1028
1029 void ProjectList::_create_project_item_control(int p_index) {
1030 // Will be added last in the list, so make sure indexes match
1031 ERR_FAIL_COND(p_index != project_list_vbox->get_child_count());
1032
1033 Item &item = _projects.write[p_index];
1034 ERR_FAIL_COND(item.control != nullptr); // Already created
1035
1036 ProjectListItemControl *hb = memnew(ProjectListItemControl);
1037 hb->add_theme_constant_override("separation", 10 * EDSCALE);
1038
1039 hb->set_project_title(!item.missing ? item.project_name : TTR("Missing Project"));
1040 hb->set_project_path(item.path);
1041 hb->set_tooltip_text(item.description);
1042 hb->set_tags(item.tags, this);
1043 hb->set_unsupported_features(item.unsupported_features.duplicate());
1044 hb->set_project_version(item.project_version);
1045 hb->set_last_edited_info(item.get_last_edited_string());
1046
1047 hb->set_is_favorite(item.favorite);
1048 hb->set_is_missing(item.missing);
1049 hb->set_is_grayed(item.grayed);
1050
1051 hb->connect(SceneStringName(gui_input), callable_mp(this, &ProjectList::_list_item_input).bind(hb));
1052 hb->connect("favorite_pressed", callable_mp(this, &ProjectList::_on_favorite_pressed).bind(hb));
1053
1054 #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
1055 hb->connect("explore_pressed", callable_mp(this, &ProjectList::_on_explore_pressed).bind(item.path));
1056 #endif
1057 hb->connect("request_menu", callable_mp(this, &ProjectList::_open_menu).bind(hb));
1058
1059 project_list_vbox->add_child(hb);
1060 item.control = hb;
1061 }
1062
1063 void ProjectList::_toggle_project(int p_index) {
1064 // This methods adds to the selection or removes from the
1065 // selection.
1066 Item &item = _projects.write[p_index];
1067
1068 if (_selected_project_paths.has(item.path)) {
1069 _deselect_project_nocheck(p_index);
1070 } else {
1071 _select_project_nocheck(p_index);
1072 }
1073 }
1074
1075 void ProjectList::_remove_project(int p_index, bool p_update_config) {
1076 const Item item = _projects[p_index]; // Take a copy
1077
1078 _selected_project_paths.erase(item.path);
1079
1080 if (_last_clicked == item.path) {
1081 _last_clicked = "";
1082 }
1083
1084 memdelete(item.control);
1085 _projects.remove_at(p_index);
1086
1087 if (p_update_config) {
1088 _config.erase_section(item.path);
1089 // Not actually saving the file, in case you are doing more changes to settings
1090 }
1091
1092 queue_accessibility_update();
1093 update_dock_menu();
1094 }
1095
1096 void ProjectList::_list_item_input(const Ref<InputEvent> &p_ev, Control *p_hb) {
1097 Ref<InputEventMouseButton> mb = p_ev;
1098 int clicked_index = p_hb->get_index();
1099 const Item &clicked_project = _projects[clicked_index];
1100
1101 if (mb.is_valid() && mb->is_pressed()) {
1102 if (mb->get_button_index() == MouseButton::LEFT) {
1103 if (mb->is_shift_pressed() && _selected_project_paths.size() > 0 && !_last_clicked.is_empty() && clicked_project.path != _last_clicked) {
1104 int anchor_index = -1;
1105 for (int i = 0; i < _projects.size(); ++i) {
1106 const Item &p = _projects[i];
1107 if (p.path == _last_clicked) {
1108 anchor_index = p.control->get_index();
1109 break;
1110 }
1111 }
1112 CRASH_COND(anchor_index == -1);
1113 _select_project_range(anchor_index, clicked_index);
1114
1115 } else if (mb->is_command_or_control_pressed()) {
1116 _toggle_project(clicked_index);
1117
1118 } else {
1119 _last_clicked = clicked_project.path;
1120 select_project(clicked_index, true);
1121 }
1122
1123 emit_signal(SNAME(SIGNAL_SELECTION_CHANGED));
1124
1125 // Do not allow opening a project more than once using a single project manager instance.
1126 // Opening the same project in several editor instances at once can lead to various issues.
1127 if (!mb->is_command_or_control_pressed() && mb->is_double_click() && !project_opening_initiated) {
1128 emit_signal(SNAME(SIGNAL_PROJECT_ASK_OPEN));
1129 }
1130 } else if (mb->get_button_index() == MouseButton::RIGHT) {
1131 _open_menu(mb->get_position(), p_hb);
1132 }
1133 }
1134
1135 Ref<InputEventKey> kev = p_ev;
1136
1137 if (kev.is_valid() && kev->is_pressed()) {
1138 switch (kev->get_keycode()) {
1139 case Key::E: {
1140 _on_explore_pressed(clicked_project.path);
1141 accept_event();
1142 } break;
1143 case Key::F: {
1144 if (kev->is_command_or_control_pressed()) {
1145 return; // Focus the search box by the ProjectManager.
1146 }
1147 _on_favorite_pressed(p_hb);
1148 accept_event();
1149 } break;
1150 default: {
1151 } break;
1152 }
1153 }
1154 }
1155
1156 void ProjectList::_on_favorite_pressed(Node *p_hb) {
1157 ProjectListItemControl *control = Object::cast_to<ProjectListItemControl>(p_hb);
1158
1159 int index = control->get_index();
1160 Item item = _projects.write[index]; // Take copy
1161
1162 item.favorite = !item.favorite;
1163
1164 _config.set_value(item.path, "favorite", item.favorite);
1165 save_config();
1166
1167 _projects.write[index] = item;
1168
1169 control->set_is_favorite(item.favorite);
1170
1171 sort_projects();
1172
1173 // As controls are sorted, the calls are delayed in case follow focus does not take effect.
1174 if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
1175 callable_mp((ScrollContainer *)this, &ScrollContainer::ensure_control_visible).call_deferred(control);
1176 } else {
1177 // Do not follow the control when toggling.
1178 callable_mp(this, &ProjectList::ensure_project_visible).call_deferred(index);
1179 }
1180
1181 update_dock_menu();
1182 }
1183
1184 void ProjectList::_on_explore_pressed(const String &p_path) {
1185 OS::get_singleton()->shell_show_in_file_manager(p_path, true);
1186 }
1187
1188 void ProjectList::_open_menu(const Vector2 &p_at, Control *p_hb) {
1189 int clicked_index = p_hb->get_index();
1190 const Item &clicked_project = _projects[clicked_index];
1191
1192 if (!project_context_menu) {
1193 project_context_menu = memnew(PopupMenu);
1194 project_context_menu->add_item(TTRC("Open in Editor"), MENU_EDIT);
1195 project_context_menu->add_item(TTRC("Open in Editor (Verbose Mode)"), MENU_EDIT_VERBOSE);
1196 project_context_menu->add_item(TTRC("Open in Editor (Recovery Mode)"), MENU_EDIT_RECOVERY);
1197 project_context_menu->add_item(TTRC("Run Project"), MENU_RUN);
1198 project_context_menu->add_separator();
1199 #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
1200 project_context_menu->add_item(TTRC("Show in File Manager"), MENU_SHOW_IN_FILE_MANAGER);
1201 #endif
1202 project_context_menu->add_item(TTRC("Copy Path"), MENU_COPY_PATH);
1203 project_context_menu->add_separator();
1204 project_context_menu->add_item(TTRC("Rename"), MENU_RENAME);
1205 project_context_menu->add_item(TTRC("Manage Tags"), MENU_MANAGE_TAGS);
1206 project_context_menu->add_item(TTRC("Duplicate"), MENU_DUPLICATE);
1207 project_context_menu->add_item(TTRC("Remove from Project List"), MENU_REMOVE);
1208 add_child(project_context_menu);
1209 project_context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ProjectList::_menu_option));
1210 _update_menu_icons();
1211 }
1212 select_project(clicked_index, true);
1213
1214 for (int id : Vector<int>{
1215 MENU_EDIT,
1216 MENU_EDIT_VERBOSE,
1217 MENU_EDIT_RECOVERY,
1218 MENU_RUN,
1219 #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
1220 MENU_SHOW_IN_FILE_MANAGER,
1221 #endif
1222 MENU_RENAME,
1223 MENU_MANAGE_TAGS,
1224 MENU_DUPLICATE }) {
1225 project_context_menu->set_item_disabled(project_context_menu->get_item_index(id), clicked_project.missing);
1226 }
1227
1228 project_context_menu->set_position(p_hb->get_screen_position() + p_at);
1229 project_context_menu->reset_size();
1230 project_context_menu->popup();
1231 }
1232
1233 void ProjectList::_menu_option(int p_option) {
1234 emit_signal(SIGNAL_MENU_OPTION_SELECTED, p_option);
1235 }
1236
1237 void ProjectList::_update_menu_icons() {
1238 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_EDIT), get_editor_theme_icon("Edit"));
1239 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_EDIT_VERBOSE), get_editor_theme_icon("Notification"));
1240 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_EDIT_RECOVERY), get_editor_theme_icon("NodeWarning"));
1241 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_RUN), get_editor_theme_icon("Play"));
1242 #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
1243 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_SHOW_IN_FILE_MANAGER), get_editor_theme_icon("Load"));
1244 #endif
1245 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_COPY_PATH), get_editor_theme_icon("ActionCopy"));
1246 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_RENAME), get_editor_theme_icon("Rename"));
1247 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_MANAGE_TAGS), get_editor_theme_icon("Script"));
1248 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_DUPLICATE), get_editor_theme_icon("Duplicate"));
1249 project_context_menu->set_item_icon(project_context_menu->get_item_index(MENU_REMOVE), get_editor_theme_icon("Remove"));
1250 }
1251
1252 // Project list selection.
1253
1254 void ProjectList::_clear_project_selection() {
1255 Vector<Item> previous_selected_items = get_selected_projects();
1256 _selected_project_paths.clear();
1257
1258 for (int i = 0; i < previous_selected_items.size(); ++i) {
1259 previous_selected_items[i].control->set_selected(false);
1260 }
1261 queue_accessibility_update();
1262 }
1263
1264 void ProjectList::_select_project_nocheck(int p_index, bool p_hide_focus) {
1265 Item &item = _projects.write[p_index];
1266 _selected_project_paths.insert(item.path);
1267 item.control->set_selected(true, p_hide_focus);
1268 queue_accessibility_update();
1269 }
1270
1271 void ProjectList::_deselect_project_nocheck(int p_index) {
1272 Item &item = _projects.write[p_index];
1273 _selected_project_paths.erase(item.path);
1274 item.control->set_selected(false);
1275 queue_accessibility_update();
1276 }
1277
1278 inline void _sort_project_range(int &a, int &b) {
1279 if (a > b) {
1280 int temp = a;
1281 a = b;
1282 b = temp;
1283 }
1284 }
1285
1286 void ProjectList::_select_project_range(int p_begin, int p_end) {
1287 _clear_project_selection();
1288
1289 _sort_project_range(p_begin, p_end);
1290 for (int i = p_begin; i <= p_end; ++i) {
1291 _select_project_nocheck(i);
1292 }
1293 }
1294
1295 void ProjectList::select_project(int p_index, bool p_hide_focus) {
1296 // This method keeps only one project selected.
1297 _clear_project_selection();
1298 _select_project_nocheck(p_index, p_hide_focus);
1299 }
1300
1301 void ProjectList::deselect_project(int p_index) {
1302 _deselect_project_nocheck(p_index);
1303 }
1304
1305 void ProjectList::select_first_visible_project() {
1306 _clear_project_selection();
1307
1308 for (int i = 0; i < _projects.size(); i++) {
1309 if (_projects[i].control->is_visible()) {
1310 _select_project_nocheck(i);
1311 break;
1312 }
1313 }
1314 }
1315
1316 void ProjectList::deselect_all_visible_projects() {
1317 for (int i = 0; i < _projects.size(); i++) {
1318 if (_projects[i].control->is_visible()) {
1319 _deselect_project_nocheck(i);
1320 }
1321 }
1322 }
1323
1324 void ProjectList::select_all_visible_projects() {
1325 for (int i = 0; i < _projects.size(); i++) {
1326 if (_projects[i].control->is_visible()) {
1327 _select_project_nocheck(i);
1328 }
1329 }
1330 }
1331
1332 Vector<ProjectList::Item> ProjectList::get_selected_projects() const {
1333 Vector<Item> items;
1334 if (_selected_project_paths.is_empty()) {
1335 return items;
1336 }
1337 items.resize(_selected_project_paths.size());
1338 int j = 0;
1339 for (int i = 0; i < _projects.size(); ++i) {
1340 const Item &item = _projects[i];
1341 if (_selected_project_paths.has(item.path)) {
1342 items.write[j++] = item;
1343 }
1344 }
1345 ERR_FAIL_COND_V(j != items.size(), items);
1346 return items;
1347 }
1348
1349 const HashSet<String> &ProjectList::get_selected_project_keys() const {
1350 // Faster if that's all you need
1351 return _selected_project_paths;
1352 }
1353
1354 int ProjectList::get_single_selected_index() const {
1355 if (_selected_project_paths.is_empty()) {
1356 // Default selection
1357 return 0;
1358 }
1359 String key;
1360 if (_selected_project_paths.size() == 1) {
1361 // Only one selected
1362 key = *_selected_project_paths.begin();
1363 } else {
1364 // Multiple selected, consider the last clicked one as "main"
1365 key = _last_clicked;
1366 }
1367 for (int i = 0; i < _projects.size(); ++i) {
1368 if (_projects[i].path == key) {
1369 return i;
1370 }
1371 }
1372 return 0;
1373 }
1374
1375 void ProjectList::erase_selected_projects(bool p_delete_project_contents) {
1376 if (_selected_project_paths.is_empty()) {
1377 return;
1378 }
1379
1380 for (int i = 0; i < _projects.size(); ++i) {
1381 Item &item = _projects.write[i];
1382 if (_selected_project_paths.has(item.path) && item.control->is_visible()) {
1383 _config.erase_section(item.path);
1384
1385 // Comment out for now until we have a better warning system to
1386 // ensure users delete their project only.
1387 //if (p_delete_project_contents) {
1388 // OS::get_singleton()->move_to_trash(item.path);
1389 //}
1390
1391 memdelete(item.control);
1392 _projects.remove_at(i);
1393 --i;
1394 }
1395 }
1396
1397 save_config();
1398 _selected_project_paths.clear();
1399 _last_clicked = "";
1400
1401 update_dock_menu();
1402 }
1403
1404 // Missing projects.
1405
1406 bool ProjectList::is_any_project_missing() const {
1407 for (int i = 0; i < _projects.size(); ++i) {
1408 if (_projects[i].missing) {
1409 return true;
1410 }
1411 }
1412 return false;
1413 }
1414
1415 void ProjectList::erase_missing_projects() {
1416 if (_projects.is_empty()) {
1417 return;
1418 }
1419
1420 int deleted_count = 0;
1421 int remaining_count = 0;
1422
1423 for (int i = 0; i < _projects.size(); ++i) {
1424 const Item &item = _projects[i];
1425
1426 if (item.missing) {
1427 _remove_project(i, true);
1428 --i;
1429 ++deleted_count;
1430
1431 } else {
1432 ++remaining_count;
1433 }
1434 }
1435
1436 print_line("Removed " + itos(deleted_count) + " projects from the list, remaining " + itos(remaining_count) + " projects");
1437 save_config();
1438 }
1439
1440 // Project list sorting and filtering.
1441
1442 void ProjectList::set_search_term(String p_search_term) {
1443 _search_term = p_search_term;
1444 }
1445
1446 void ProjectList::add_search_tag(const String &p_tag) {
1447 const String tag_string = "tag:" + p_tag;
1448
1449 int exists = _search_term.find(tag_string);
1450 if (exists > -1) {
1451 _search_term = _search_term.erase(exists, tag_string.length() + 1);
1452 } else if (_search_term.is_empty() || _search_term.ends_with(" ")) {
1453 _search_term += tag_string;
1454 } else {
1455 _search_term += " " + tag_string;
1456 }
1457 ProjectManager::get_singleton()->get_search_box()->set_text(_search_term);
1458
1459 sort_projects();
1460 }
1461
1462 void ProjectList::set_order_option(int p_option, bool p_save) {
1463 FilterOption selected = (FilterOption)p_option;
1464 if (p_save) {
1465 EditorSettings::get_singleton()->set("project_manager/sorting_order", p_option);
1466 EditorSettings::get_singleton()->save();
1467 }
1468 _order_option = selected;
1469
1470 sort_projects();
1471 }
1472
1473 // Global menu integration.
1474
1475 void ProjectList::update_dock_menu() {
1476 if (!NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) {
1477 return;
1478 }
1479 RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID);
1480 NativeMenu::get_singleton()->clear(dock_rid);
1481
1482 int favs_added = 0;
1483 int total_added = 0;
1484 for (int i = 0; i < _projects.size(); ++i) {
1485 if (!_projects[i].grayed && !_projects[i].missing) {
1486 if (_projects[i].favorite) {
1487 favs_added++;
1488 } else {
1489 if (favs_added != 0) {
1490 NativeMenu::get_singleton()->add_separator(dock_rid);
1491 }
1492 favs_added = 0;
1493 }
1494 NativeMenu::get_singleton()->add_item(dock_rid, _projects[i].project_name + " ( " + _projects[i].path + " )", callable_mp(this, &ProjectList::_global_menu_open_project), Callable(), i);
1495 total_added++;
1496 }
1497 }
1498 if (total_added != 0) {
1499 NativeMenu::get_singleton()->add_separator(dock_rid);
1500 }
1501 NativeMenu::get_singleton()->add_item(dock_rid, TTR("New Window"), callable_mp(this, &ProjectList::_global_menu_new_window));
1502 }
1503
1504 void ProjectList::_global_menu_new_window(const Variant &p_tag) {
1505 List<String> args;
1506 args.push_back("-p");
1507 OS::get_singleton()->create_instance(args);
1508 }
1509
1510 void ProjectList::_global_menu_open_project(const Variant &p_tag) {
1511 int idx = (int)p_tag;
1512
1513 if (idx >= 0 && idx < _projects.size()) {
1514 String conf = _projects[idx].path.path_join("project.godot");
1515 List<String> args;
1516 args.push_back(conf);
1517 OS::get_singleton()->create_instance(args);
1518 }
1519 }
1520
1521 // Object methods.
1522
1523 void ProjectList::_bind_methods() {
1524 ADD_SIGNAL(MethodInfo(SIGNAL_LIST_CHANGED));
1525 ADD_SIGNAL(MethodInfo(SIGNAL_SELECTION_CHANGED));
1526 ADD_SIGNAL(MethodInfo(SIGNAL_PROJECT_ASK_OPEN));
1527 ADD_SIGNAL(MethodInfo(SIGNAL_MENU_OPTION_SELECTED));
1528 }
1529
1530 ProjectList::ProjectList() {
1531 set_follow_focus(true);
1532
1533 project_list_vbox = memnew(VBoxContainer);
1534 project_list_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1535 add_child(project_list_vbox);
1536
1537 _config_path = EditorPaths::get_singleton()->get_data_dir().path_join("projects.cfg");
1538 _migrate_config();
1539 }