Skip to content

Commit 3f721d6

Browse files
committed
Merge pull request #112019 from YeldhamDev/trim_it_just_a_little
Add text trimming in `LinkButton`
2 parents 9ea8b10 + 687aa56 commit 3f721d6

File tree

6 files changed

+43
-30
lines changed

6 files changed

+43
-30
lines changed

doc/classes/LinkButton.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
<member name="text_direction" type="int" setter="set_text_direction" getter="get_text_direction" enum="Control.TextDirection" default="0">
2828
Base text writing direction.
2929
</member>
30+
<member name="text_overrun_behavior" type="int" setter="set_text_overrun_behavior" getter="get_text_overrun_behavior" enum="TextServer.OverrunBehavior" default="0">
31+
Sets the clipping behavior when the text exceeds the node's bounding rectangle.
32+
</member>
3033
<member name="underline" type="int" setter="set_underline_mode" getter="get_underline_mode" enum="LinkButton.UnderlineMode" default="0">
3134
The underline mode to use for the text.
3235
</member>

editor/asset_library/asset_library_editor_plugin.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,6 @@ void EditorAssetLibraryItem::configure(const String &p_title, int p_asset_id, co
6767
price->set_text(p_cost);
6868
}
6969

70-
// TODO: Refactor this method to use the TextServer.
71-
void EditorAssetLibraryItem::clamp_width(int p_max_width) {
72-
int text_pixel_width = title->get_button_font()->get_string_size(title_text).x * EDSCALE;
73-
74-
if (text_pixel_width > p_max_width) {
75-
// Truncate title text to within the current column width.
76-
int max_length = p_max_width / (text_pixel_width / title_text.length());
77-
String truncated_text = title_text.left(max_length - 3) + "...";
78-
title->set_text(truncated_text);
79-
} else {
80-
title->set_text(title_text);
81-
}
82-
}
83-
8470
void EditorAssetLibraryItem::set_image(int p_type, int p_index, const Ref<Texture2D> &p_image) {
8571
ERR_FAIL_COND(p_type != EditorAssetLibrary::IMAGE_QUEUE_ICON);
8672
ERR_FAIL_COND(p_index != 0);
@@ -148,11 +134,13 @@ EditorAssetLibraryItem::EditorAssetLibraryItem(bool p_clickable) {
148134
title = memnew(LinkButton);
149135
title->set_accessibility_name(TTRC("Title"));
150136
title->set_auto_translate_mode(AutoTranslateMode::AUTO_TRANSLATE_MODE_DISABLED);
137+
title->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
151138
title->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
152139
vb->add_child(title);
153140

154141
category = memnew(LinkButton);
155142
category->set_accessibility_name(TTRC("Category"));
143+
category->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
156144
category->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
157145
vb->add_child(category);
158146

@@ -162,6 +150,7 @@ EditorAssetLibraryItem::EditorAssetLibraryItem(bool p_clickable) {
162150

163151
author = memnew(LinkButton);
164152
author->set_tooltip_text(TTRC("Author"));
153+
author->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
165154
author->set_accessibility_name(TTRC("Author"));
166155
author_price_hbox->add_child(author);
167156

@@ -1398,7 +1387,6 @@ void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const
13981387
EditorAssetLibraryItem *item = memnew(EditorAssetLibraryItem(true));
13991388
asset_items->add_child(item);
14001389
item->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"]);
1401-
item->clamp_width(asset_items_column_width);
14021390
item->connect("asset_selected", callable_mp(this, &EditorAssetLibrary::_select_asset));
14031391
item->connect("author_selected", callable_mp(this, &EditorAssetLibrary::_select_author));
14041392
item->connect("category_selected", callable_mp(this, &EditorAssetLibrary::_select_category));
@@ -1534,16 +1522,6 @@ void EditorAssetLibrary::_update_asset_items_columns() {
15341522
if (new_columns != asset_items->get_columns()) {
15351523
asset_items->set_columns(new_columns);
15361524
}
1537-
1538-
asset_items_column_width = (get_size().x / new_columns) - (120 * EDSCALE);
1539-
1540-
for (int i = 0; i < asset_items->get_child_count(); i++) {
1541-
EditorAssetLibraryItem *item = Object::cast_to<EditorAssetLibraryItem>(asset_items->get_child(i));
1542-
if (!item || !item->is_visible()) {
1543-
continue;
1544-
}
1545-
item->clamp_width(asset_items_column_width);
1546-
}
15471525
}
15481526

15491527
void EditorAssetLibrary::_set_library_message(const String &p_message) {

editor/asset_library/asset_library_editor_plugin.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ class EditorAssetLibraryItem : public PanelContainer {
7676
public:
7777
void configure(const String &p_title, int p_asset_id, const String &p_category, int p_category_id, const String &p_author, int p_author_id, const String &p_cost);
7878

79-
void clamp_width(int p_max_width);
80-
8179
EditorAssetLibraryItem(bool p_clickable = false);
8280
};
8381

@@ -312,8 +310,6 @@ class EditorAssetLibrary : public PanelContainer {
312310

313311
void _install_external_asset(String p_zip_path, String p_title);
314312

315-
int asset_items_column_width = 0;
316-
317313
void _update_asset_items_columns();
318314

319315
friend class EditorAssetLibraryItemDescription;

scene/gui/link_button.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void LinkButton::_shape() {
3737
int font_size = theme_cache.font_size;
3838

3939
text_buf->clear();
40+
text_buf->set_width(-1);
4041
if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
4142
text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
4243
} else {
@@ -45,6 +46,7 @@ void LinkButton::_shape() {
4546
TS->shaped_text_set_bidi_override(text_buf->get_rid(), structured_text_parser(st_parser, st_args, xl_text));
4647
const String &lang = language.is_empty() ? _get_locale() : language;
4748
text_buf->add_string(xl_text, font, font_size, lang);
49+
text_buf->set_text_overrun_behavior(overrun_behavior);
4850

4951
queue_accessibility_update();
5052
}
@@ -64,6 +66,19 @@ String LinkButton::get_text() const {
6466
return text;
6567
}
6668

69+
void LinkButton::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
70+
if (overrun_behavior != p_behavior) {
71+
overrun_behavior = p_behavior;
72+
_shape();
73+
update_minimum_size();
74+
queue_redraw();
75+
}
76+
}
77+
78+
TextServer::OverrunBehavior LinkButton::get_text_overrun_behavior() const {
79+
return overrun_behavior;
80+
}
81+
6782
void LinkButton::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
6883
if (st_parser != p_parser) {
6984
st_parser = p_parser;
@@ -148,7 +163,12 @@ void LinkButton::pressed() {
148163
}
149164

150165
Size2 LinkButton::get_minimum_size() const {
151-
return text_buf->get_size();
166+
Size2 minsize = text_buf->get_size();
167+
if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
168+
minsize.width = 0;
169+
}
170+
171+
return minsize;
152172
}
153173

154174
void LinkButton::_notification(int p_what) {
@@ -178,6 +198,7 @@ void LinkButton::_notification(int p_what) {
178198
queue_redraw();
179199
} break;
180200

201+
case NOTIFICATION_RESIZED:
181202
case NOTIFICATION_THEME_CHANGED: {
182203
_shape();
183204
update_minimum_size();
@@ -228,6 +249,9 @@ void LinkButton::_notification(int p_what) {
228249
style->draw(ci, Rect2(Point2(), size));
229250
}
230251

252+
if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
253+
text_buf->set_width(MAX(1.0f, size.width));
254+
}
231255
int width = text_buf->get_line_width();
232256

233257
Color font_outline_color = theme_cache.font_outline_color;
@@ -262,6 +286,8 @@ void LinkButton::_notification(int p_what) {
262286
void LinkButton::_bind_methods() {
263287
ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text);
264288
ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text);
289+
ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &LinkButton::set_text_overrun_behavior);
290+
ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &LinkButton::get_text_overrun_behavior);
265291
ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LinkButton::set_text_direction);
266292
ClassDB::bind_method(D_METHOD("get_text_direction"), &LinkButton::get_text_direction);
267293
ClassDB::bind_method(D_METHOD("set_language", "language"), &LinkButton::set_language);
@@ -283,6 +309,9 @@ void LinkButton::_bind_methods() {
283309
ADD_PROPERTY(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode");
284310
ADD_PROPERTY(PropertyInfo(Variant::STRING, "uri"), "set_uri", "get_uri");
285311

312+
ADD_GROUP("Text Behavior", "");
313+
ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis (6+ Characters),Word Ellipsis (6+ Characters),Ellipsis (Always),Word Ellipsis (Always)"), "set_text_overrun_behavior", "get_text_overrun_behavior");
314+
286315
ADD_GROUP("BiDi", "");
287316
ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
288317
ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");

scene/gui/link_button.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class LinkButton : public BaseButton {
5454
TextDirection text_direction = TEXT_DIRECTION_AUTO;
5555
TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
5656
Array st_args;
57+
TextServer::OverrunBehavior overrun_behavior = TextServer::OVERRUN_NO_TRIMMING;
5758

5859
struct ThemeCache {
5960
Ref<StyleBox> focus;
@@ -88,6 +89,9 @@ class LinkButton : public BaseButton {
8889
void set_uri(const String &p_uri);
8990
String get_uri() const;
9091

92+
void set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior);
93+
TextServer::OverrunBehavior get_text_overrun_behavior() const;
94+
9195
void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
9296
TextServer::StructuredTextParser get_structured_text_bidi_override() const;
9397

scene/resources/text_line.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ String TextLine::get_ellipsis_char() const {
321321
}
322322

323323
void TextLine::set_width(float p_width) {
324+
if (width == p_width) {
325+
return;
326+
}
324327
width = p_width;
325328
if (alignment == HORIZONTAL_ALIGNMENT_FILL || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
326329
dirty = true;

0 commit comments

Comments
 (0)