project_list.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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. #include "project_list.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/os/time.h"
  34. #include "core/version.h"
  35. #include "editor/editor_paths.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/project_manager.h"
  39. #include "editor/project_manager/project_tag.h"
  40. #include "editor/themes/editor_scale.h"
  41. #include "scene/gui/button.h"
  42. #include "scene/gui/dialogs.h"
  43. #include "scene/gui/label.h"
  44. #include "scene/gui/line_edit.h"
  45. #include "scene/gui/progress_bar.h"
  46. #include "scene/gui/texture_button.h"
  47. #include "scene/gui/texture_rect.h"
  48. #include "scene/resources/image_texture.h"
  49. const char *ProjectList::SIGNAL_LIST_CHANGED = "list_changed";
  50. const char *ProjectList::SIGNAL_SELECTION_CHANGED = "selection_changed";
  51. const char *ProjectList::SIGNAL_PROJECT_ASK_OPEN = "project_ask_open";
  52. void ProjectListItemControl::_notification(int p_what) {
  53. switch (p_what) {
  54. case NOTIFICATION_THEME_CHANGED: {
  55. if (icon_needs_reload) {
  56. // The project icon may not be loaded by the time the control is displayed,
  57. // so use a loading placeholder.
  58. project_icon->set_texture(get_editor_theme_icon(SNAME("ProjectIconLoading")));
  59. }
  60. project_title->begin_bulk_theme_override();
  61. project_title->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("title"), EditorStringName(EditorFonts)));
  62. project_title->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size(SNAME("title_size"), EditorStringName(EditorFonts)));
  63. project_title->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("Tree")));
  64. project_title->end_bulk_theme_override();
  65. project_path->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("Tree")));
  66. project_unsupported_features->set_texture(get_editor_theme_icon(SNAME("NodeWarning")));
  67. favorite_button->set_texture_normal(get_editor_theme_icon(SNAME("Favorites")));
  68. if (project_is_missing) {
  69. explore_button->set_button_icon(get_editor_theme_icon(SNAME("FileBroken")));
  70. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  71. } else {
  72. explore_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));
  73. #endif
  74. }
  75. } break;
  76. case NOTIFICATION_MOUSE_ENTER: {
  77. is_hovering = true;
  78. queue_redraw();
  79. queue_accessibility_update();
  80. } break;
  81. case NOTIFICATION_MOUSE_EXIT: {
  82. is_hovering = false;
  83. queue_redraw();
  84. queue_accessibility_update();
  85. } break;
  86. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  87. RID ae = get_accessibility_element();
  88. ERR_FAIL_COND(ae.is_null());
  89. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LIST_BOX_OPTION);
  90. DisplayServer::get_singleton()->accessibility_update_set_name(ae, TTR("Project") + " " + project_title->get_text());
  91. DisplayServer::get_singleton()->accessibility_update_set_value(ae, project_title->get_text());
  92. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &ProjectListItemControl::_accessibility_action_open));
  93. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &ProjectListItemControl::_accessibility_action_scroll_into_view));
  94. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &ProjectListItemControl::_accessibility_action_focus));
  95. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &ProjectListItemControl::_accessibility_action_blur));
  96. ProjectList *pl = get_list();
  97. if (pl) {
  98. DisplayServer::get_singleton()->accessibility_update_set_list_item_index(ae, pl->get_index(this));
  99. }
  100. DisplayServer::get_singleton()->accessibility_update_set_list_item_level(ae, 0);
  101. DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(ae, is_selected);
  102. } break;
  103. case NOTIFICATION_FOCUS_ENTER: {
  104. ProjectList *pl = get_list();
  105. if (pl) {
  106. int idx = pl->get_index(this);
  107. if (idx >= 0) {
  108. pl->ensure_project_visible(idx);
  109. pl->select_project(idx);
  110. pl->emit_signal(SNAME(ProjectList::SIGNAL_SELECTION_CHANGED));
  111. }
  112. }
  113. } break;
  114. case NOTIFICATION_DRAW: {
  115. if (is_selected) {
  116. draw_style_box(get_theme_stylebox(SNAME("selected"), SNAME("Tree")), Rect2(Point2(), get_size()));
  117. }
  118. if (is_hovering) {
  119. draw_style_box(get_theme_stylebox(SNAME("hovered"), SNAME("Tree")), Rect2(Point2(), get_size()));
  120. }
  121. draw_line(Point2(0, get_size().y + 1), Point2(get_size().x, get_size().y + 1), get_theme_color(SNAME("guide_color"), SNAME("Tree")));
  122. } break;
  123. }
  124. }
  125. ProjectList *ProjectListItemControl::get_list() const {
  126. if (!is_inside_tree()) {
  127. return nullptr;
  128. }
  129. ProjectList *pl = Object::cast_to<ProjectList>(get_parent()->get_parent());
  130. return pl;
  131. }
  132. void ProjectListItemControl::_accessibility_action_scroll_into_view(const Variant &p_data) {
  133. ProjectList *pl = get_list();
  134. if (pl) {
  135. int idx = pl->get_index(this);
  136. if (idx >= 0) {
  137. pl->ensure_project_visible(idx);
  138. }
  139. }
  140. }
  141. void ProjectListItemControl::_accessibility_action_open(const Variant &p_data) {
  142. ProjectList *pl = get_list();
  143. if (pl && !pl->project_opening_initiated) {
  144. pl->emit_signal(SNAME(ProjectList::SIGNAL_PROJECT_ASK_OPEN));
  145. }
  146. }
  147. void ProjectListItemControl::_accessibility_action_focus(const Variant &p_data) {
  148. ProjectList *pl = get_list();
  149. if (pl) {
  150. int idx = pl->get_index(this);
  151. if (idx >= 0) {
  152. pl->ensure_project_visible(idx);
  153. pl->select_project(idx);
  154. }
  155. }
  156. }
  157. void ProjectListItemControl::_accessibility_action_blur(const Variant &p_data) {
  158. ProjectList *pl = get_list();
  159. if (pl) {
  160. int idx = pl->get_index(this);
  161. if (idx >= 0) {
  162. pl->ensure_project_visible(idx);
  163. pl->deselect_project(idx);
  164. }
  165. }
  166. }
  167. void ProjectListItemControl::_favorite_button_pressed() {
  168. emit_signal(SNAME("favorite_pressed"));
  169. }
  170. void ProjectListItemControl::_explore_button_pressed() {
  171. emit_signal(SNAME("explore_pressed"));
  172. }
  173. void ProjectListItemControl::set_project_title(const String &p_title) {
  174. project_title->set_text(p_title);
  175. project_title->set_accessibility_name(TTRC("Project Name"));
  176. queue_accessibility_update();
  177. }
  178. void ProjectListItemControl::set_project_path(const String &p_path) {
  179. project_path->set_text(p_path);
  180. project_path->set_accessibility_name(TTRC("Project Path"));
  181. queue_accessibility_update();
  182. }
  183. void ProjectListItemControl::set_tags(const PackedStringArray &p_tags, ProjectList *p_parent_list) {
  184. for (const String &tag : p_tags) {
  185. ProjectTag *tag_control = memnew(ProjectTag(tag));
  186. tag_container->add_child(tag_control);
  187. tag_control->connect_button_to(callable_mp(p_parent_list, &ProjectList::add_search_tag).bind(tag));
  188. }
  189. }
  190. void ProjectListItemControl::set_project_icon(const Ref<Texture2D> &p_icon) {
  191. icon_needs_reload = false;
  192. // The default project icon is 128×128 to look crisp on hiDPI displays,
  193. // but we want the actual displayed size to be 64×64 on loDPI displays.
  194. project_icon->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  195. project_icon->set_custom_minimum_size(Size2(64, 64) * EDSCALE);
  196. project_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  197. project_icon->set_texture(p_icon);
  198. }
  199. void ProjectListItemControl::set_last_edited_info(const String &p_info) {
  200. last_edited_info->set_text(p_info);
  201. }
  202. void ProjectListItemControl::set_project_version(const String &p_info) {
  203. project_version->set_text(p_info);
  204. }
  205. void ProjectListItemControl::set_unsupported_features(PackedStringArray p_features) {
  206. if (p_features.size() > 0) {
  207. String tooltip_text = "";
  208. for (int i = 0; i < p_features.size(); i++) {
  209. if (ProjectList::project_feature_looks_like_version(p_features[i])) {
  210. PackedStringArray project_version_split = p_features[i].split(".");
  211. int project_version_major = 0, project_version_minor = 0;
  212. if (project_version_split.size() >= 2) {
  213. project_version_major = project_version_split[0].to_int();
  214. project_version_minor = project_version_split[1].to_int();
  215. }
  216. if (GODOT_VERSION_MAJOR != project_version_major || GODOT_VERSION_MINOR <= project_version_minor) {
  217. // Don't show a warning if the project was last edited in a previous minor version.
  218. tooltip_text += TTR("This project was last edited in a different Godot version: ") + p_features[i] + "\n";
  219. }
  220. p_features.remove_at(i);
  221. i--;
  222. }
  223. }
  224. if (p_features.size() > 0) {
  225. String unsupported_features_str = String(", ").join(p_features);
  226. tooltip_text += TTR("This project uses features unsupported by the current build:") + "\n" + unsupported_features_str;
  227. }
  228. if (tooltip_text.is_empty()) {
  229. return;
  230. }
  231. project_version->set_tooltip_text(tooltip_text);
  232. project_unsupported_features->set_focus_mode(FOCUS_ACCESSIBILITY);
  233. project_unsupported_features->set_accessibility_name(tooltip_text);
  234. project_unsupported_features->set_tooltip_text(tooltip_text);
  235. project_unsupported_features->show();
  236. } else {
  237. project_unsupported_features->hide();
  238. }
  239. }
  240. bool ProjectListItemControl::should_load_project_icon() const {
  241. return icon_needs_reload;
  242. }
  243. void ProjectListItemControl::set_selected(bool p_selected) {
  244. is_selected = p_selected;
  245. queue_redraw();
  246. queue_accessibility_update();
  247. }
  248. void ProjectListItemControl::set_is_favorite(bool p_favorite) {
  249. favorite_button->set_modulate(p_favorite ? Color(1, 1, 1, 1) : Color(1, 1, 1, 0.2));
  250. }
  251. void ProjectListItemControl::set_is_missing(bool p_missing) {
  252. project_is_missing = p_missing;
  253. if (project_is_missing) {
  254. project_icon->set_modulate(Color(1, 1, 1, 0.5));
  255. explore_button->set_button_icon(get_editor_theme_icon(SNAME("FileBroken")));
  256. explore_button->set_tooltip_text(TTRC("Error: Project is missing on the filesystem."));
  257. } else {
  258. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  259. explore_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));
  260. explore_button->set_tooltip_text(TTRC("Show in File Manager"));
  261. #else
  262. // Opening the system file manager is not supported on the Android and web editors.
  263. explore_button->hide();
  264. #endif
  265. }
  266. }
  267. void ProjectListItemControl::set_is_grayed(bool p_grayed) {
  268. if (p_grayed) {
  269. main_vbox->set_modulate(Color(1, 1, 1, 0.5));
  270. // Don't make the icon less prominent if the parent is already grayed out.
  271. explore_button->set_modulate(Color(1, 1, 1, 1.0));
  272. } else {
  273. main_vbox->set_modulate(Color(1, 1, 1, 1.0));
  274. explore_button->set_modulate(Color(1, 1, 1, 0.5));
  275. }
  276. }
  277. void ProjectListItemControl::_bind_methods() {
  278. ADD_SIGNAL(MethodInfo("favorite_pressed"));
  279. ADD_SIGNAL(MethodInfo("explore_pressed"));
  280. }
  281. ProjectListItemControl::ProjectListItemControl() {
  282. set_focus_mode(FocusMode::FOCUS_ALL);
  283. VBoxContainer *favorite_box = memnew(VBoxContainer);
  284. favorite_box->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  285. add_child(favorite_box);
  286. favorite_button = memnew(TextureButton);
  287. favorite_button->set_name("FavoriteButton");
  288. favorite_button->set_tooltip_text(TTR("Add to favorites"));
  289. favorite_button->set_accessibility_name(TTRC("Add to favorites"));
  290. // This makes the project's "hover" style display correctly when hovering the favorite icon.
  291. favorite_button->set_mouse_filter(MOUSE_FILTER_PASS);
  292. favorite_box->add_child(favorite_button);
  293. favorite_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_favorite_button_pressed));
  294. project_icon = memnew(TextureRect);
  295. project_icon->set_name("ProjectIcon");
  296. project_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
  297. add_child(project_icon);
  298. main_vbox = memnew(VBoxContainer);
  299. main_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  300. add_child(main_vbox);
  301. Control *ec = memnew(Control);
  302. ec->set_custom_minimum_size(Size2(0, 1));
  303. ec->set_mouse_filter(MOUSE_FILTER_PASS);
  304. main_vbox->add_child(ec);
  305. // Top half, title, tags and unsupported features labels.
  306. {
  307. HBoxContainer *title_hb = memnew(HBoxContainer);
  308. main_vbox->add_child(title_hb);
  309. project_title = memnew(Label);
  310. project_title->set_focus_mode(FOCUS_ACCESSIBILITY);
  311. project_title->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  312. project_title->set_name("ProjectName");
  313. project_title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  314. project_title->set_clip_text(true);
  315. title_hb->add_child(project_title);
  316. tag_container = memnew(HBoxContainer);
  317. title_hb->add_child(tag_container);
  318. Control *spacer = memnew(Control);
  319. spacer->set_custom_minimum_size(Size2(10, 10));
  320. title_hb->add_child(spacer);
  321. }
  322. // Bottom half, containing the path and view folder button.
  323. {
  324. HBoxContainer *path_hb = memnew(HBoxContainer);
  325. path_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  326. main_vbox->add_child(path_hb);
  327. explore_button = memnew(Button);
  328. explore_button->set_name("ExploreButton");
  329. explore_button->set_tooltip_text(TTR("Open in file manager"));
  330. explore_button->set_accessibility_name(TTRC("Open in file manager"));
  331. explore_button->set_flat(true);
  332. path_hb->add_child(explore_button);
  333. explore_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectListItemControl::_explore_button_pressed));
  334. project_path = memnew(Label);
  335. project_path->set_name("ProjectPath");
  336. project_path->set_focus_mode(FOCUS_ACCESSIBILITY);
  337. project_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  338. project_path->set_clip_text(true);
  339. project_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  340. project_path->set_modulate(Color(1, 1, 1, 0.5));
  341. path_hb->add_child(project_path);
  342. project_unsupported_features = memnew(TextureRect);
  343. project_unsupported_features->set_name("ProjectUnsupportedFeatures");
  344. project_unsupported_features->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  345. path_hb->add_child(project_unsupported_features);
  346. project_unsupported_features->hide();
  347. project_version = memnew(Label);
  348. project_version->set_focus_mode(FOCUS_ACCESSIBILITY);
  349. project_version->set_name("ProjectVersion");
  350. project_version->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  351. path_hb->add_child(project_version);
  352. last_edited_info = memnew(Label);
  353. last_edited_info->set_focus_mode(FOCUS_ACCESSIBILITY);
  354. last_edited_info->set_name("LastEditedInfo");
  355. last_edited_info->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  356. last_edited_info->set_tooltip_text(TTRC("Last edited timestamp"));
  357. last_edited_info->set_modulate(Color(1, 1, 1, 0.5));
  358. path_hb->add_child(last_edited_info);
  359. Control *spacer = memnew(Control);
  360. spacer->set_custom_minimum_size(Size2(10, 10));
  361. path_hb->add_child(spacer);
  362. }
  363. }
  364. struct ProjectListComparator {
  365. ProjectList::FilterOption order_option = ProjectList::FilterOption::EDIT_DATE;
  366. // operator<
  367. _FORCE_INLINE_ bool operator()(const ProjectList::Item &a, const ProjectList::Item &b) const {
  368. if (a.favorite && !b.favorite) {
  369. return true;
  370. }
  371. if (b.favorite && !a.favorite) {
  372. return false;
  373. }
  374. switch (order_option) {
  375. case ProjectList::PATH:
  376. return a.path < b.path;
  377. case ProjectList::EDIT_DATE:
  378. return a.last_edited > b.last_edited;
  379. case ProjectList::TAGS:
  380. return a.tag_sort_string < b.tag_sort_string;
  381. default:
  382. return a.project_name < b.project_name;
  383. }
  384. }
  385. };
  386. // Helpers.
  387. bool ProjectList::project_feature_looks_like_version(const String &p_feature) {
  388. return p_feature.contains_char('.') && p_feature.substr(0, 3).is_numeric();
  389. }
  390. // Notifications.
  391. void ProjectList::_notification(int p_what) {
  392. switch (p_what) {
  393. case NOTIFICATION_TRANSLATION_CHANGED: {
  394. if (is_ready()) {
  395. // FIXME: Technically this only needs to update some dynamic texts, not the whole list.
  396. update_project_list();
  397. }
  398. } break;
  399. case NOTIFICATION_PROCESS: {
  400. // Load icons as a coroutine to speed up launch when you have hundreds of projects.
  401. if (_icon_load_index < _projects.size()) {
  402. Item &item = _projects.write[_icon_load_index];
  403. if (item.control->should_load_project_icon()) {
  404. _load_project_icon(_icon_load_index);
  405. }
  406. _icon_load_index++;
  407. // Scan directories in thread to avoid blocking the window.
  408. } else if (scan_data && scan_data->scan_in_progress.is_set()) {
  409. // Wait for the thread.
  410. } else {
  411. set_process(false);
  412. if (scan_data) {
  413. _scan_finished();
  414. }
  415. }
  416. } break;
  417. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  418. RID ae = get_accessibility_element();
  419. ERR_FAIL_COND(ae.is_null());
  420. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LIST_BOX);
  421. DisplayServer::get_singleton()->accessibility_update_set_list_item_count(ae, _projects.size());
  422. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_MULTISELECTABLE, false);
  423. }
  424. }
  425. }
  426. // Projects scan.
  427. void ProjectList::_scan_thread(void *p_scan_data) {
  428. ScanData *scan_data = static_cast<ScanData *>(p_scan_data);
  429. for (const String &base_path : scan_data->paths_to_scan) {
  430. print_verbose(vformat("Scanning for projects in \"%s\".", base_path));
  431. _scan_folder_recursive(base_path, &scan_data->found_projects, scan_data->scan_in_progress);
  432. if (!scan_data->scan_in_progress.is_set()) {
  433. print_verbose("Scan aborted.");
  434. break;
  435. }
  436. }
  437. print_verbose(vformat("Found %d project(s).", scan_data->found_projects.size()));
  438. scan_data->scan_in_progress.clear();
  439. }
  440. void ProjectList::_scan_finished() {
  441. if (scan_data->scan_in_progress.is_set()) {
  442. // Abort scanning.
  443. scan_data->scan_in_progress.clear();
  444. }
  445. scan_data->thread->wait_to_finish();
  446. memdelete(scan_data->thread);
  447. if (scan_progress) {
  448. scan_progress->hide();
  449. }
  450. for (const String &E : scan_data->found_projects) {
  451. add_project(E, false);
  452. }
  453. memdelete(scan_data);
  454. scan_data = nullptr;
  455. save_config();
  456. if (ProjectManager::get_singleton()->is_initialized()) {
  457. update_project_list();
  458. }
  459. }
  460. // Initialization & loading.
  461. void ProjectList::_migrate_config() {
  462. // Proposal #1637 moved the project list from editor settings to a separate config file
  463. // If the new config file doesn't exist, populate it from EditorSettings
  464. if (FileAccess::exists(_config_path)) {
  465. return;
  466. }
  467. List<PropertyInfo> properties;
  468. EditorSettings::get_singleton()->get_property_list(&properties);
  469. for (const PropertyInfo &E : properties) {
  470. // This is actually something like "projects/C:::Documents::Godot::Projects::MyGame"
  471. String property_key = E.name;
  472. if (!property_key.begins_with("projects/")) {
  473. continue;
  474. }
  475. String path = EDITOR_GET(property_key);
  476. print_line("Migrating legacy project '" + path + "'.");
  477. String favoriteKey = "favorite_projects/" + property_key.get_slicec('/', 1);
  478. bool favorite = EditorSettings::get_singleton()->has_setting(favoriteKey);
  479. add_project(path, favorite);
  480. if (favorite) {
  481. EditorSettings::get_singleton()->erase(favoriteKey);
  482. }
  483. EditorSettings::get_singleton()->erase(property_key);
  484. }
  485. save_config();
  486. }
  487. void ProjectList::save_config() {
  488. _config.save(_config_path);
  489. }
  490. // Load project data from p_property_key and return it in a ProjectList::Item.
  491. // p_favorite is passed directly into the Item.
  492. ProjectList::Item ProjectList::load_project_data(const String &p_path, bool p_favorite) {
  493. String conf = p_path.path_join("project.godot");
  494. bool grayed = false;
  495. bool missing = false;
  496. bool recovery_mode = false;
  497. Ref<ConfigFile> cf = memnew(ConfigFile);
  498. Error cf_err = cf->load(conf);
  499. int config_version = 0;
  500. String cf_project_name;
  501. String project_name = TTR("Unnamed Project");
  502. if (cf_err == OK) {
  503. cf_project_name = cf->get_value("application", "config/name", "");
  504. if (!cf_project_name.is_empty()) {
  505. project_name = cf_project_name.xml_unescape();
  506. }
  507. config_version = (int)cf->get_value("", "config_version", 0);
  508. }
  509. if (config_version > ProjectSettings::CONFIG_VERSION) {
  510. // Comes from an incompatible (more recent) Godot version, gray it out.
  511. grayed = true;
  512. }
  513. const String description = cf->get_value("application", "config/description", "");
  514. const PackedStringArray tags = cf->get_value("application", "config/tags", PackedStringArray());
  515. const String main_scene = cf->get_value("application", "run/main_scene", "");
  516. String icon = cf->get_value("application", "config/icon", "");
  517. if (icon.begins_with("uid://")) {
  518. Error err;
  519. Ref<FileAccess> file = FileAccess::open(p_path.path_join(".godot/uid_cache.bin"), FileAccess::READ, &err);
  520. if (err == OK) {
  521. icon = ResourceUID::get_path_from_cache(file, icon);
  522. if (icon.is_empty()) {
  523. WARN_PRINT(vformat("Could not load icon from UID for project at path \"%s\". Make sure UID cache exists.", p_path));
  524. }
  525. } else {
  526. // Cache does not exist yet, so ignore and fallback to default icon.
  527. icon = "";
  528. }
  529. }
  530. PackedStringArray project_features = cf->get_value("application", "config/features", PackedStringArray());
  531. PackedStringArray unsupported_features = ProjectSettings::get_unsupported_features(project_features);
  532. String project_version = "?";
  533. for (int i = 0; i < project_features.size(); i++) {
  534. if (ProjectList::project_feature_looks_like_version(project_features[i])) {
  535. project_version = project_features[i];
  536. break;
  537. }
  538. }
  539. if (config_version < ProjectSettings::CONFIG_VERSION) {
  540. // Previous versions may not have unsupported features.
  541. if (config_version == 4) {
  542. unsupported_features.push_back("3.x");
  543. project_version = "3.x";
  544. } else {
  545. unsupported_features.push_back(TTR("Unknown version"));
  546. }
  547. }
  548. uint64_t last_edited = 0;
  549. if (cf_err == OK) {
  550. // The modification date marks the date the project was last edited.
  551. // This is because the `project.godot` file will always be modified
  552. // when editing a project (but not when running it).
  553. last_edited = FileAccess::get_modified_time(conf);
  554. String fscache = p_path.path_join(".fscache");
  555. if (FileAccess::exists(fscache)) {
  556. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  557. if (cache_modified > last_edited) {
  558. last_edited = cache_modified;
  559. }
  560. }
  561. } else {
  562. grayed = true;
  563. missing = true;
  564. }
  565. for (const String &tag : tags) {
  566. ProjectManager::get_singleton()->add_new_tag(tag);
  567. }
  568. // We can't use OS::get_user_dir() because it attempts to load paths from the current loaded project through ProjectSettings,
  569. // while here we're parsing project files externally. Therefore, we have to replicate its behavior.
  570. String user_dir;
  571. if (!cf_project_name.is_empty()) {
  572. String appname = OS::get_singleton()->get_safe_dir_name(cf_project_name);
  573. bool use_custom_dir = cf->get_value("application", "config/use_custom_user_dir", false);
  574. if (use_custom_dir) {
  575. String custom_dir = OS::get_singleton()->get_safe_dir_name(cf->get_value("application", "config/custom_user_dir_name", ""), true);
  576. if (custom_dir.is_empty()) {
  577. custom_dir = appname;
  578. }
  579. user_dir = custom_dir;
  580. } else {
  581. user_dir = OS::get_singleton()->get_godot_dir_name().path_join("app_userdata").path_join(appname);
  582. }
  583. } else {
  584. user_dir = OS::get_singleton()->get_godot_dir_name().path_join("app_userdata").path_join("[unnamed project]");
  585. }
  586. String recovery_mode_lock_file = OS::get_singleton()->get_user_data_dir(user_dir).path_join(".recovery_mode_lock");
  587. recovery_mode = FileAccess::exists(recovery_mode_lock_file);
  588. 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);
  589. }
  590. void ProjectList::_update_icons_async() {
  591. _icon_load_index = 0;
  592. set_process(true);
  593. }
  594. void ProjectList::_load_project_icon(int p_index) {
  595. Item &item = _projects.write[p_index];
  596. Ref<Texture2D> default_icon = get_editor_theme_icon(SNAME("DefaultProjectIcon"));
  597. Ref<Texture2D> icon;
  598. if (!item.icon.is_empty()) {
  599. Ref<Image> img;
  600. img.instantiate();
  601. Error err = img->load(item.icon.replace_first("res://", item.path + "/"));
  602. if (err == OK) {
  603. img->resize(default_icon->get_width(), default_icon->get_height(), Image::INTERPOLATE_LANCZOS);
  604. icon = ImageTexture::create_from_image(img);
  605. }
  606. }
  607. if (icon.is_null()) {
  608. icon = default_icon;
  609. }
  610. item.control->set_project_icon(icon);
  611. }
  612. // Project list updates.
  613. void ProjectList::update_project_list() {
  614. // This is a full, hard reload of the list. Don't call this unless really required, it's expensive.
  615. // If you have 150 projects, it may read through 150 files on your disk at once + load 150 icons.
  616. // FIXME: Does it really have to be a full, hard reload? Runtime updates should be made much cheaper.
  617. if (ProjectManager::get_singleton()->is_initialized()) {
  618. // Clear whole list
  619. for (int i = 0; i < _projects.size(); ++i) {
  620. Item &project = _projects.write[i];
  621. CRASH_COND(project.control == nullptr);
  622. memdelete(project.control); // Why not queue_free()?
  623. }
  624. _projects.clear();
  625. _last_clicked = "";
  626. _selected_project_paths.clear();
  627. load_project_list();
  628. }
  629. // Create controls
  630. for (int i = 0; i < _projects.size(); ++i) {
  631. _create_project_item_control(i);
  632. }
  633. sort_projects();
  634. _update_icons_async();
  635. update_dock_menu();
  636. set_v_scroll(0);
  637. emit_signal(SNAME(SIGNAL_LIST_CHANGED));
  638. queue_accessibility_update();
  639. }
  640. void ProjectList::sort_projects() {
  641. SortArray<Item, ProjectListComparator> sorter;
  642. sorter.compare.order_option = _order_option;
  643. sorter.sort(_projects.ptrw(), _projects.size());
  644. String search_term;
  645. PackedStringArray tags;
  646. if (!_search_term.is_empty()) {
  647. PackedStringArray search_parts = _search_term.split(" ");
  648. if (search_parts.size() > 1 || search_parts[0].begins_with("tag:")) {
  649. PackedStringArray remaining;
  650. for (const String &part : search_parts) {
  651. if (part.begins_with("tag:")) {
  652. tags.push_back(part.get_slicec(':', 1));
  653. } else {
  654. remaining.append(part);
  655. }
  656. }
  657. search_term = String(" ").join(remaining); // Search term without tags.
  658. } else {
  659. search_term = _search_term;
  660. }
  661. }
  662. for (int i = 0; i < _projects.size(); ++i) {
  663. Item &item = _projects.write[i];
  664. bool item_visible = true;
  665. if (!_search_term.is_empty()) {
  666. String search_path;
  667. if (search_term.contains_char('/')) {
  668. // Search path will match the whole path
  669. search_path = item.path;
  670. } else {
  671. // Search path will only match the last path component to make searching more strict
  672. search_path = item.path.get_file();
  673. }
  674. bool missing_tags = false;
  675. for (const String &tag : tags) {
  676. if (!item.tags.has(tag)) {
  677. missing_tags = true;
  678. break;
  679. }
  680. }
  681. // When searching, display projects whose name or path contain the search term and whose tags match the searched tags.
  682. item_visible = !missing_tags && (search_term.is_empty() || item.project_name.containsn(search_term) || search_path.containsn(search_term));
  683. }
  684. item.control->set_visible(item_visible);
  685. }
  686. for (int i = 0; i < _projects.size(); ++i) {
  687. Item &item = _projects.write[i];
  688. item.control->get_parent()->move_child(item.control, i);
  689. }
  690. // Rewind the coroutine because order of projects changed
  691. _update_icons_async();
  692. update_dock_menu();
  693. queue_accessibility_update();
  694. }
  695. int ProjectList::get_project_count() const {
  696. return _projects.size();
  697. }
  698. void ProjectList::find_projects(const String &p_path) {
  699. PackedStringArray paths = { p_path };
  700. find_projects_multiple(paths);
  701. }
  702. void ProjectList::find_projects_multiple(const PackedStringArray &p_paths) {
  703. if (!scan_progress && is_inside_tree()) {
  704. scan_progress = memnew(AcceptDialog);
  705. scan_progress->set_title(TTRC("Scanning"));
  706. scan_progress->set_ok_button_text(TTRC("Cancel"));
  707. VBoxContainer *vb = memnew(VBoxContainer);
  708. scan_progress->add_child(vb);
  709. Label *label = memnew(Label);
  710. label->set_text(TTRC("Scanning for projects..."));
  711. vb->add_child(label);
  712. ProgressBar *progress = memnew(ProgressBar);
  713. progress->set_indeterminate(true);
  714. vb->add_child(progress);
  715. add_child(scan_progress);
  716. scan_progress->connect(SceneStringName(confirmed), callable_mp(this, &ProjectList::_scan_finished));
  717. scan_progress->connect("canceled", callable_mp(this, &ProjectList::_scan_finished));
  718. }
  719. scan_data = memnew(ScanData);
  720. scan_data->paths_to_scan = p_paths;
  721. scan_data->scan_in_progress.set();
  722. scan_data->thread = memnew(Thread);
  723. scan_data->thread->start(_scan_thread, scan_data);
  724. if (scan_progress) {
  725. scan_progress->reset_size();
  726. scan_progress->popup_centered();
  727. }
  728. set_process(true);
  729. }
  730. void ProjectList::load_project_list() {
  731. _config.load(_config_path);
  732. Vector<String> sections = _config.get_sections();
  733. for (const String &path : sections) {
  734. bool favorite = _config.get_value(path, "favorite", false);
  735. _projects.push_back(load_project_data(path, favorite));
  736. }
  737. }
  738. void ProjectList::_scan_folder_recursive(const String &p_path, List<String> *r_projects, const SafeFlag &p_scan_active) {
  739. if (!p_scan_active.is_set()) {
  740. return;
  741. }
  742. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  743. Error error = da->change_dir(p_path);
  744. ERR_FAIL_COND_MSG(error != OK, vformat("Failed to open the path \"%s\" for scanning (code %d).", p_path, error));
  745. da->list_dir_begin();
  746. String n = da->get_next();
  747. while (!n.is_empty()) {
  748. if (!p_scan_active.is_set()) {
  749. return;
  750. }
  751. if (da->current_is_dir() && n[0] != '.') {
  752. _scan_folder_recursive(da->get_current_dir().path_join(n), r_projects, p_scan_active);
  753. } else if (n == "project.godot") {
  754. r_projects->push_back(da->get_current_dir());
  755. }
  756. n = da->get_next();
  757. }
  758. da->list_dir_end();
  759. }
  760. // Project list items.
  761. void ProjectList::add_project(const String &dir_path, bool favorite) {
  762. if (!_config.has_section(dir_path)) {
  763. _config.set_value(dir_path, "favorite", favorite);
  764. }
  765. queue_accessibility_update();
  766. }
  767. void ProjectList::set_project_version(const String &p_project_path, int p_version) {
  768. for (ProjectList::Item &E : _projects) {
  769. if (E.path == p_project_path) {
  770. E.version = p_version;
  771. break;
  772. }
  773. }
  774. }
  775. int ProjectList::refresh_project(const String &dir_path) {
  776. // Reloads information about a specific project.
  777. // If it wasn't loaded and should be in the list, it is added (i.e new project).
  778. // If it isn't in the list anymore, it is removed.
  779. // If it is in the list but doesn't exist anymore, it is marked as missing.
  780. bool should_be_in_list = _config.has_section(dir_path);
  781. bool is_favourite = _config.get_value(dir_path, "favorite", false);
  782. bool was_selected = _selected_project_paths.has(dir_path);
  783. // Remove item in any case
  784. for (int i = 0; i < _projects.size(); ++i) {
  785. const Item &existing_item = _projects[i];
  786. if (existing_item.path == dir_path) {
  787. _remove_project(i, false);
  788. break;
  789. }
  790. }
  791. int index = -1;
  792. if (should_be_in_list) {
  793. // Recreate it with updated info
  794. Item item = load_project_data(dir_path, is_favourite);
  795. _projects.push_back(item);
  796. _create_project_item_control(_projects.size() - 1);
  797. sort_projects();
  798. for (int i = 0; i < _projects.size(); ++i) {
  799. if (_projects[i].path == dir_path) {
  800. if (was_selected) {
  801. select_project(i);
  802. ensure_project_visible(i);
  803. }
  804. _load_project_icon(i);
  805. index = i;
  806. break;
  807. }
  808. }
  809. }
  810. return index;
  811. }
  812. int ProjectList::get_index(const ProjectListItemControl *p_control) const {
  813. for (int i = 0; i < _projects.size(); ++i) {
  814. if (_projects[i].control == p_control) {
  815. return i;
  816. }
  817. }
  818. return -1;
  819. }
  820. void ProjectList::ensure_project_visible(int p_index) {
  821. const Item &item = _projects[p_index];
  822. ensure_control_visible(item.control);
  823. }
  824. void ProjectList::_create_project_item_control(int p_index) {
  825. // Will be added last in the list, so make sure indexes match
  826. ERR_FAIL_COND(p_index != project_list_vbox->get_child_count());
  827. Item &item = _projects.write[p_index];
  828. ERR_FAIL_COND(item.control != nullptr); // Already created
  829. ProjectListItemControl *hb = memnew(ProjectListItemControl);
  830. hb->add_theme_constant_override("separation", 10 * EDSCALE);
  831. hb->set_project_title(!item.missing ? item.project_name : TTR("Missing Project"));
  832. hb->set_project_path(item.path);
  833. hb->set_tooltip_text(item.description);
  834. hb->set_tags(item.tags, this);
  835. hb->set_unsupported_features(item.unsupported_features.duplicate());
  836. hb->set_project_version(item.project_version);
  837. hb->set_last_edited_info(!item.missing ? Time::get_singleton()->get_datetime_string_from_unix_time(item.last_edited, true) : TTR("Missing Date"));
  838. hb->set_is_favorite(item.favorite);
  839. hb->set_is_missing(item.missing);
  840. hb->set_is_grayed(item.grayed);
  841. hb->connect(SceneStringName(gui_input), callable_mp(this, &ProjectList::_list_item_input).bind(hb));
  842. hb->connect("favorite_pressed", callable_mp(this, &ProjectList::_on_favorite_pressed).bind(hb));
  843. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  844. hb->connect("explore_pressed", callable_mp(this, &ProjectList::_on_explore_pressed).bind(item.path));
  845. #endif
  846. project_list_vbox->add_child(hb);
  847. item.control = hb;
  848. }
  849. void ProjectList::_toggle_project(int p_index) {
  850. // This methods adds to the selection or removes from the
  851. // selection.
  852. Item &item = _projects.write[p_index];
  853. if (_selected_project_paths.has(item.path)) {
  854. _deselect_project_nocheck(p_index);
  855. } else {
  856. _select_project_nocheck(p_index);
  857. }
  858. }
  859. void ProjectList::_remove_project(int p_index, bool p_update_config) {
  860. const Item item = _projects[p_index]; // Take a copy
  861. _selected_project_paths.erase(item.path);
  862. if (_last_clicked == item.path) {
  863. _last_clicked = "";
  864. }
  865. memdelete(item.control);
  866. _projects.remove_at(p_index);
  867. if (p_update_config) {
  868. _config.erase_section(item.path);
  869. // Not actually saving the file, in case you are doing more changes to settings
  870. }
  871. queue_accessibility_update();
  872. update_dock_menu();
  873. }
  874. void ProjectList::_list_item_input(const Ref<InputEvent> &p_ev, Node *p_hb) {
  875. Ref<InputEventMouseButton> mb = p_ev;
  876. int clicked_index = p_hb->get_index();
  877. const Item &clicked_project = _projects[clicked_index];
  878. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  879. if (mb->is_shift_pressed() && _selected_project_paths.size() > 0 && !_last_clicked.is_empty() && clicked_project.path != _last_clicked) {
  880. int anchor_index = -1;
  881. for (int i = 0; i < _projects.size(); ++i) {
  882. const Item &p = _projects[i];
  883. if (p.path == _last_clicked) {
  884. anchor_index = p.control->get_index();
  885. break;
  886. }
  887. }
  888. CRASH_COND(anchor_index == -1);
  889. _select_project_range(anchor_index, clicked_index);
  890. } else if (mb->is_command_or_control_pressed()) {
  891. _toggle_project(clicked_index);
  892. } else {
  893. _last_clicked = clicked_project.path;
  894. select_project(clicked_index);
  895. }
  896. emit_signal(SNAME(SIGNAL_SELECTION_CHANGED));
  897. // Do not allow opening a project more than once using a single project manager instance.
  898. // Opening the same project in several editor instances at once can lead to various issues.
  899. if (!mb->is_command_or_control_pressed() && mb->is_double_click() && !project_opening_initiated) {
  900. emit_signal(SNAME(SIGNAL_PROJECT_ASK_OPEN));
  901. }
  902. }
  903. }
  904. void ProjectList::_on_favorite_pressed(Node *p_hb) {
  905. ProjectListItemControl *control = Object::cast_to<ProjectListItemControl>(p_hb);
  906. int index = control->get_index();
  907. Item item = _projects.write[index]; // Take copy
  908. item.favorite = !item.favorite;
  909. _config.set_value(item.path, "favorite", item.favorite);
  910. save_config();
  911. _projects.write[index] = item;
  912. control->set_is_favorite(item.favorite);
  913. sort_projects();
  914. if (item.favorite) {
  915. for (int i = 0; i < _projects.size(); ++i) {
  916. if (_projects[i].path == item.path) {
  917. ensure_project_visible(i);
  918. break;
  919. }
  920. }
  921. }
  922. update_dock_menu();
  923. }
  924. void ProjectList::_on_explore_pressed(const String &p_path) {
  925. OS::get_singleton()->shell_show_in_file_manager(p_path, true);
  926. }
  927. // Project list selection.
  928. void ProjectList::_clear_project_selection() {
  929. Vector<Item> previous_selected_items = get_selected_projects();
  930. _selected_project_paths.clear();
  931. for (int i = 0; i < previous_selected_items.size(); ++i) {
  932. previous_selected_items[i].control->set_selected(false);
  933. }
  934. queue_accessibility_update();
  935. }
  936. void ProjectList::_select_project_nocheck(int p_index) {
  937. Item &item = _projects.write[p_index];
  938. _selected_project_paths.insert(item.path);
  939. item.control->set_selected(true);
  940. queue_accessibility_update();
  941. }
  942. void ProjectList::_deselect_project_nocheck(int p_index) {
  943. Item &item = _projects.write[p_index];
  944. _selected_project_paths.erase(item.path);
  945. item.control->set_selected(false);
  946. queue_accessibility_update();
  947. }
  948. inline void _sort_project_range(int &a, int &b) {
  949. if (a > b) {
  950. int temp = a;
  951. a = b;
  952. b = temp;
  953. }
  954. }
  955. void ProjectList::_select_project_range(int p_begin, int p_end) {
  956. _clear_project_selection();
  957. _sort_project_range(p_begin, p_end);
  958. for (int i = p_begin; i <= p_end; ++i) {
  959. _select_project_nocheck(i);
  960. }
  961. }
  962. void ProjectList::select_project(int p_index) {
  963. // This method keeps only one project selected.
  964. _clear_project_selection();
  965. _select_project_nocheck(p_index);
  966. }
  967. void ProjectList::deselect_project(int p_index) {
  968. _deselect_project_nocheck(p_index);
  969. }
  970. void ProjectList::select_first_visible_project() {
  971. _clear_project_selection();
  972. for (int i = 0; i < _projects.size(); i++) {
  973. if (_projects[i].control->is_visible()) {
  974. _select_project_nocheck(i);
  975. break;
  976. }
  977. }
  978. }
  979. Vector<ProjectList::Item> ProjectList::get_selected_projects() const {
  980. Vector<Item> items;
  981. if (_selected_project_paths.is_empty()) {
  982. return items;
  983. }
  984. items.resize(_selected_project_paths.size());
  985. int j = 0;
  986. for (int i = 0; i < _projects.size(); ++i) {
  987. const Item &item = _projects[i];
  988. if (_selected_project_paths.has(item.path)) {
  989. items.write[j++] = item;
  990. }
  991. }
  992. ERR_FAIL_COND_V(j != items.size(), items);
  993. return items;
  994. }
  995. const HashSet<String> &ProjectList::get_selected_project_keys() const {
  996. // Faster if that's all you need
  997. return _selected_project_paths;
  998. }
  999. int ProjectList::get_single_selected_index() const {
  1000. if (_selected_project_paths.is_empty()) {
  1001. // Default selection
  1002. return 0;
  1003. }
  1004. String key;
  1005. if (_selected_project_paths.size() == 1) {
  1006. // Only one selected
  1007. key = *_selected_project_paths.begin();
  1008. } else {
  1009. // Multiple selected, consider the last clicked one as "main"
  1010. key = _last_clicked;
  1011. }
  1012. for (int i = 0; i < _projects.size(); ++i) {
  1013. if (_projects[i].path == key) {
  1014. return i;
  1015. }
  1016. }
  1017. return 0;
  1018. }
  1019. void ProjectList::erase_selected_projects(bool p_delete_project_contents) {
  1020. if (_selected_project_paths.is_empty()) {
  1021. return;
  1022. }
  1023. for (int i = 0; i < _projects.size(); ++i) {
  1024. Item &item = _projects.write[i];
  1025. if (_selected_project_paths.has(item.path) && item.control->is_visible()) {
  1026. _config.erase_section(item.path);
  1027. // Comment out for now until we have a better warning system to
  1028. // ensure users delete their project only.
  1029. //if (p_delete_project_contents) {
  1030. // OS::get_singleton()->move_to_trash(item.path);
  1031. //}
  1032. memdelete(item.control);
  1033. _projects.remove_at(i);
  1034. --i;
  1035. }
  1036. }
  1037. save_config();
  1038. _selected_project_paths.clear();
  1039. _last_clicked = "";
  1040. update_dock_menu();
  1041. }
  1042. // Missing projects.
  1043. bool ProjectList::is_any_project_missing() const {
  1044. for (int i = 0; i < _projects.size(); ++i) {
  1045. if (_projects[i].missing) {
  1046. return true;
  1047. }
  1048. }
  1049. return false;
  1050. }
  1051. void ProjectList::erase_missing_projects() {
  1052. if (_projects.is_empty()) {
  1053. return;
  1054. }
  1055. int deleted_count = 0;
  1056. int remaining_count = 0;
  1057. for (int i = 0; i < _projects.size(); ++i) {
  1058. const Item &item = _projects[i];
  1059. if (item.missing) {
  1060. _remove_project(i, true);
  1061. --i;
  1062. ++deleted_count;
  1063. } else {
  1064. ++remaining_count;
  1065. }
  1066. }
  1067. print_line("Removed " + itos(deleted_count) + " projects from the list, remaining " + itos(remaining_count) + " projects");
  1068. save_config();
  1069. }
  1070. // Project list sorting and filtering.
  1071. void ProjectList::set_search_term(String p_search_term) {
  1072. _search_term = p_search_term;
  1073. }
  1074. void ProjectList::add_search_tag(const String &p_tag) {
  1075. const String tag_string = "tag:" + p_tag;
  1076. int exists = _search_term.find(tag_string);
  1077. if (exists > -1) {
  1078. _search_term = _search_term.erase(exists, tag_string.length() + 1);
  1079. } else if (_search_term.is_empty() || _search_term.ends_with(" ")) {
  1080. _search_term += tag_string;
  1081. } else {
  1082. _search_term += " " + tag_string;
  1083. }
  1084. ProjectManager::get_singleton()->get_search_box()->set_text(_search_term);
  1085. sort_projects();
  1086. }
  1087. void ProjectList::set_order_option(int p_option) {
  1088. FilterOption selected = (FilterOption)p_option;
  1089. EditorSettings::get_singleton()->set("project_manager/sorting_order", p_option);
  1090. EditorSettings::get_singleton()->save();
  1091. _order_option = selected;
  1092. sort_projects();
  1093. }
  1094. // Global menu integration.
  1095. void ProjectList::update_dock_menu() {
  1096. if (!NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) {
  1097. return;
  1098. }
  1099. RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID);
  1100. NativeMenu::get_singleton()->clear(dock_rid);
  1101. int favs_added = 0;
  1102. int total_added = 0;
  1103. for (int i = 0; i < _projects.size(); ++i) {
  1104. if (!_projects[i].grayed && !_projects[i].missing) {
  1105. if (_projects[i].favorite) {
  1106. favs_added++;
  1107. } else {
  1108. if (favs_added != 0) {
  1109. NativeMenu::get_singleton()->add_separator(dock_rid);
  1110. }
  1111. favs_added = 0;
  1112. }
  1113. NativeMenu::get_singleton()->add_item(dock_rid, _projects[i].project_name + " ( " + _projects[i].path + " )", callable_mp(this, &ProjectList::_global_menu_open_project), Callable(), i);
  1114. total_added++;
  1115. }
  1116. }
  1117. if (total_added != 0) {
  1118. NativeMenu::get_singleton()->add_separator(dock_rid);
  1119. }
  1120. NativeMenu::get_singleton()->add_item(dock_rid, TTR("New Window"), callable_mp(this, &ProjectList::_global_menu_new_window));
  1121. }
  1122. void ProjectList::_global_menu_new_window(const Variant &p_tag) {
  1123. List<String> args;
  1124. args.push_back("-p");
  1125. OS::get_singleton()->create_instance(args);
  1126. }
  1127. void ProjectList::_global_menu_open_project(const Variant &p_tag) {
  1128. int idx = (int)p_tag;
  1129. if (idx >= 0 && idx < _projects.size()) {
  1130. String conf = _projects[idx].path.path_join("project.godot");
  1131. List<String> args;
  1132. args.push_back(conf);
  1133. OS::get_singleton()->create_instance(args);
  1134. }
  1135. }
  1136. // Object methods.
  1137. void ProjectList::_bind_methods() {
  1138. ADD_SIGNAL(MethodInfo(SIGNAL_LIST_CHANGED));
  1139. ADD_SIGNAL(MethodInfo(SIGNAL_SELECTION_CHANGED));
  1140. ADD_SIGNAL(MethodInfo(SIGNAL_PROJECT_ASK_OPEN));
  1141. }
  1142. ProjectList::ProjectList() {
  1143. project_list_vbox = memnew(VBoxContainer);
  1144. project_list_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1145. add_child(project_list_vbox);
  1146. _config_path = EditorPaths::get_singleton()->get_data_dir().path_join("projects.cfg");
  1147. _migrate_config();
  1148. }