Skip to content

Commit 7ed0865

Browse files
[ProjectList] Fix Wrap Long Project Title
Co-authored-by: unit-tick <official.prince.john@gmail.com>
1 parent 7220d45 commit 7ed0865

2 files changed

Lines changed: 29 additions & 70 deletions

File tree

editor/project_manager/project_list.cpp

Lines changed: 27 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -424,71 +424,37 @@ void ProjectListItemControl::set_is_grayed(bool p_grayed) {
424424
}
425425
}
426426

427-
void ProjectListItemControl::set_project_title_index(int p_title_index) {
428-
project_title_index = p_title_index;
429-
}
430-
431427
void ProjectListItemControl::set_project_title_autowrap() {
432428
title_fullsize_cache = project_title->get_size().x;
433-
window_size_cache = get_window()->get_size().x;
429+
ProjectList *pl = get_list();
430+
pl->window_size_cache = get_window()->get_size().x;
434431

435432
int tag_size = 0;
436-
int tag_maxsize = 0;
437433
for (Node *child : tag_container->iterate_children()) {
438434
ProjectTag *tag = Object::cast_to<ProjectTag>(child);
439435
tag_size += tag->get_size().x;
440-
441-
if (tag_maxsize == 0) {
442-
tag_maxsize = tag->get_custom_maximum_size().x;
443-
}
444436
}
445-
tag_size_cache = MIN(tag_size, tag_maxsize);
446-
int &title_size_cache = get_list()->title_size_cache[project_title_index];
437+
tag_size_cache = tag_size;
447438

448-
int size_check = 800 * EDSCALE;
449-
if (title_size_cache == 0) {
450-
title_size_cache = size_check;
451-
}
452-
453-
if (title_fullsize_cache > size_check - tag_size_cache) {
439+
if (title_fullsize_cache > pl->title_size_cache - tag_size) {
454440
resize_project_title();
455441
}
456442
}
457443

458444
void ProjectListItemControl::resize_project_title() {
459-
if (get_window() == nullptr) {
460-
return;
461-
}
462-
463-
int window_size = get_window()->get_size().x;
464-
int difference = window_size - window_size_cache;
465-
window_size_cache = window_size;
466-
467-
int &title_size_cache = get_list()->title_size_cache[project_title_index];
468-
title_size_cache += difference;
469-
470-
if (title_size_cache > title_fullsize_cache + tag_size_cache) {
445+
const int title_available_space = get_list()->title_size_cache - tag_size_cache;
446+
if (title_fullsize_cache <= title_available_space) {
471447
project_title->set_custom_maximum_size(Vector2(-1, -1));
472448
project_title->set_custom_minimum_size(Vector2(0, 0));
473449
project_title->set_autowrap_mode(TextServer::AUTOWRAP_OFF);
474-
475450
return;
476451
}
477-
ProjectTag tag = ProjectTag("dummy");
478-
int tag_maxsize = tag.get_custom_maximum_size().x;
479-
int title_maxsize = title_size_cache - tag_size_cache;
480-
int title_minsize = title_size_cache - tag_maxsize;
481452

482-
int abs_minsize = (200 * EDSCALE);
453+
const int abs_minsize = (200 * EDSCALE);
483454
if (title_fullsize_cache > abs_minsize) {
484-
if (title_minsize < abs_minsize) {
485-
title_minsize = abs_minsize + tag_maxsize - tag_size_cache;
486-
}
487-
if (title_maxsize < title_minsize) {
488-
project_title->set_custom_maximum_size(Vector2(title_minsize, -1));
489-
} else {
490-
project_title->set_custom_maximum_size(Vector2(title_maxsize, -1));
491-
}
455+
const int title_maxsize = title_available_space;
456+
const int title_minsize = MAX(title_maxsize, abs_minsize);
457+
project_title->set_custom_maximum_size(Vector2(MAX(title_minsize, title_maxsize), -1));
492458
project_title->set_custom_minimum_size(Vector2(title_minsize, 0));
493459
project_title->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
494460
}
@@ -661,6 +627,13 @@ bool ProjectList::project_feature_looks_like_version(const String &p_feature) {
661627

662628
void ProjectList::_notification(int p_what) {
663629
switch (p_what) {
630+
case NOTIFICATION_ENTER_TREE: {
631+
/*Approx. size of project_title + tag_size.
632+
Could also get size and subtract icon, favorite, etc. size,
633+
but this is simpler and faster.*/
634+
title_size_cache = 800 * EDSCALE;
635+
} break;
636+
664637
case NOTIFICATION_TRANSLATION_CHANGED: {
665638
if (is_ready()) {
666639
for (const Item &item : _projects) {
@@ -936,14 +909,12 @@ void ProjectList::update_project_list() {
936909
// If you have 150 projects, it may read through 150 files on your disk at once + load 150 icons.
937910
// FIXME: Does it really have to be a full, hard reload? Runtime updates should be made much cheaper.
938911

939-
int temp_title_index = -1;
940912
if (ProjectManager::get_singleton()->is_initialized()) {
941913
// Clear whole list
942914
for (int i = 0; i < _projects.size(); ++i) {
943915
Item &project = _projects.write[i];
944916
CRASH_COND(project.control == nullptr);
945917

946-
temp_title_index = project.project_title_index;
947918
memdelete(project.control); // Why not queue_free()?
948919
}
949920

@@ -956,9 +927,6 @@ void ProjectList::update_project_list() {
956927

957928
// Create controls
958929
for (int i = 0; i < _projects.size(); ++i) {
959-
Item &item = _projects.write[i];
960-
item.project_title_index = temp_title_index;
961-
962930
_create_project_item_control(i);
963931
}
964932

@@ -1146,14 +1114,10 @@ int ProjectList::refresh_project(const String &dir_path) {
11461114

11471115
bool was_selected = _selected_project_paths.has(dir_path);
11481116

1149-
int temp_title_index = -1;
1150-
11511117
// Remove item in any case
11521118
for (int i = 0; i < _projects.size(); ++i) {
11531119
const Item &existing_item = _projects[i];
11541120
if (existing_item.path == dir_path) {
1155-
temp_title_index = existing_item.project_title_index;
1156-
11571121
_remove_project(i, false);
11581122
break;
11591123
}
@@ -1165,7 +1129,6 @@ int ProjectList::refresh_project(const String &dir_path) {
11651129

11661130
Item item = load_project_data(dir_path, is_favorite);
11671131

1168-
item.project_title_index = temp_title_index;
11691132
_projects.push_back(item);
11701133
_create_project_item_control(_projects.size() - 1);
11711134

@@ -1233,15 +1196,6 @@ void ProjectList::_create_project_item_control(int p_index) {
12331196
#endif
12341197
hb->connect("request_menu", callable_mp(this, &ProjectList::_open_menu).bind(hb));
12351198

1236-
if (item.project_title_index == -1) {
1237-
project_title_index_count++;
1238-
title_size_cache[project_title_index_count] = 0;
1239-
item.project_title_index = project_title_index_count;
1240-
hb->set_project_title_index(project_title_index_count);
1241-
} else {
1242-
hb->set_project_title_index(item.project_title_index);
1243-
}
1244-
12451199
project_list_vbox->add_child(hb);
12461200
}
12471201

@@ -1597,6 +1551,16 @@ void ProjectList::erase_selected_projects(bool p_delete_project_contents) {
15971551
// Resize project titles.
15981552

15991553
void ProjectList::resize_project_titles() {
1554+
Window *win = get_window();
1555+
if (win == nullptr) {
1556+
return;
1557+
}
1558+
1559+
const int window_size = win->get_size().x;
1560+
const int difference = window_size - window_size_cache;
1561+
window_size_cache = window_size;
1562+
title_size_cache += difference;
1563+
16001564
for (Item &item : _projects) {
16011565
item.control->resize_project_title();
16021566
}

editor/project_manager/project_list.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ class ProjectListItemControl : public HBoxContainer {
6262

6363
Color favorite_focus_color;
6464

65-
int project_title_index = -1;
66-
6765
bool project_is_missing = false;
6866
bool icon_needs_reload = true;
6967
bool is_selected = false;
@@ -98,7 +96,6 @@ class ProjectListItemControl : public HBoxContainer {
9896

9997
int title_fullsize_cache = 0;
10098
int tag_size_cache = 0;
101-
int window_size_cache = 0;
10299

103100
protected:
104101
void _notification(int p_what);
@@ -120,7 +117,6 @@ class ProjectListItemControl : public HBoxContainer {
120117
void set_is_favorite(bool p_favorite);
121118
void set_is_missing(bool p_missing);
122119
void set_is_grayed(bool p_grayed);
123-
void set_project_title_index(int p_title_index);
124120
void set_project_title_autowrap();
125121

126122
void resize_project_title();
@@ -172,7 +168,6 @@ class ProjectList : public ScrollContainer {
172168
bool missing = false;
173169
bool recovery_mode = false;
174170
int version = 0;
175-
int project_title_index = -1;
176171

177172
ProjectListItemControl *control = nullptr;
178173

@@ -221,8 +216,8 @@ class ProjectList : public ScrollContainer {
221216
String get_last_edited_string() const;
222217
};
223218

224-
HashMap<int, int> title_size_cache;
225-
int project_title_index_count = -1;
219+
int window_size_cache = 0;
220+
int title_size_cache = 0;
226221

227222
private:
228223
String _config_path;

0 commit comments

Comments
 (0)