Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with text clearing in RichTextLabel #89100

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/classes/RichTextLabel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
<method name="clear">
<return type="void" />
<description>
Clears the tag stack.
[b]Note:[/b] This method will not modify [member text], but setting [member text] to an empty string also clears the stack.
Clears the tag stack, causing the label to display nothing.
[b]Note:[/b] This method does not affect [member text], and its contents will show again if the label is redrawn. However, setting [member text] to an empty [String] also clears the stack.
</description>
</method>
<method name="deselect">
Expand Down Expand Up @@ -594,6 +594,7 @@
</member>
<member name="bbcode_enabled" type="bool" setter="set_use_bbcode" getter="is_using_bbcode" default="false">
If [code]true[/code], the label uses BBCode formatting.
[b]Note:[/b] This only affects the contents of [member text], not the tag stack.
</member>
<member name="clip_contents" type="bool" setter="set_clip_contents" getter="is_clipping_contents" overrides="Control" default="true" />
<member name="context_menu_enabled" type="bool" setter="set_context_menu_enabled" getter="is_context_menu_enabled" default="false">
Expand Down
20 changes: 12 additions & 8 deletions scene/gui/rich_text_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,11 @@ void RichTextLabel::_notification(int p_what) {

case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
case NOTIFICATION_TRANSLATION_CHANGED: {
_apply_translation();
// If `text` is empty, it could mean that the tag stack is being used instead. Leave it be.
if (!text.is_empty()) {
_apply_translation();
}

queue_redraw();
} break;

Expand Down Expand Up @@ -5667,19 +5671,16 @@ int RichTextLabel::get_selection_to() const {
}

void RichTextLabel::set_text(const String &p_bbcode) {
if (text == p_bbcode) {
// Allow clearing the tag stack.
if (!p_bbcode.is_empty() && text == p_bbcode) {
return;
}

text = p_bbcode;
_apply_translation();
}

void RichTextLabel::_apply_translation() {
// If `text` is empty, it could mean that the tag stack is being used instead. Leave it be.
if (text.is_empty()) {
return;
}

String xl_text = atr(text);
if (use_bbcode) {
parse_bbcode(xl_text);
Expand All @@ -5700,7 +5701,10 @@ void RichTextLabel::set_use_bbcode(bool p_enable) {
use_bbcode = p_enable;
notify_property_list_changed();

_apply_translation();
// If `text` is empty, it could mean that the tag stack is being used instead. Leave it be.
if (!text.is_empty()) {
_apply_translation();
}
}

bool RichTextLabel::is_using_bbcode() const {
Expand Down
Loading