Fix issues with text clearing in RichTextLabel

This commit is contained in:
Michael Alexsander 2024-03-02 20:38:04 -03:00
parent dad6c774b0
commit d6917d2206
No known key found for this signature in database
GPG Key ID: A9C91EE110F4EABA
2 changed files with 15 additions and 10 deletions

View File

@ -55,8 +55,8 @@
<method name="clear"> <method name="clear">
<return type="void" /> <return type="void" />
<description> <description>
Clears the tag stack. Clears the tag stack, causing the label to display nothing.
[b]Note:[/b] This method will not modify [member text], but setting [member text] to an empty string also clears the stack. [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> </description>
</method> </method>
<method name="deselect"> <method name="deselect">
@ -594,6 +594,7 @@
</member> </member>
<member name="bbcode_enabled" type="bool" setter="set_use_bbcode" getter="is_using_bbcode" default="false"> <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. 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>
<member name="clip_contents" type="bool" setter="set_clip_contents" getter="is_clipping_contents" overrides="Control" default="true" /> <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"> <member name="context_menu_enabled" type="bool" setter="set_context_menu_enabled" getter="is_context_menu_enabled" default="false">

View File

@ -1901,7 +1901,11 @@ void RichTextLabel::_notification(int p_what) {
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
case NOTIFICATION_TRANSLATION_CHANGED: { case NOTIFICATION_TRANSLATION_CHANGED: {
// If `text` is empty, it could mean that the tag stack is being used instead. Leave it be.
if (!text.is_empty()) {
_apply_translation(); _apply_translation();
}
queue_redraw(); queue_redraw();
} break; } break;
@ -5667,19 +5671,16 @@ int RichTextLabel::get_selection_to() const {
} }
void RichTextLabel::set_text(const String &p_bbcode) { 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; return;
} }
text = p_bbcode; text = p_bbcode;
_apply_translation(); _apply_translation();
} }
void RichTextLabel::_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); String xl_text = atr(text);
if (use_bbcode) { if (use_bbcode) {
parse_bbcode(xl_text); parse_bbcode(xl_text);
@ -5700,7 +5701,10 @@ void RichTextLabel::set_use_bbcode(bool p_enable) {
use_bbcode = p_enable; use_bbcode = p_enable;
notify_property_list_changed(); notify_property_list_changed();
// If `text` is empty, it could mean that the tag stack is being used instead. Leave it be.
if (!text.is_empty()) {
_apply_translation(); _apply_translation();
}
} }
bool RichTextLabel::is_using_bbcode() const { bool RichTextLabel::is_using_bbcode() const {