Merge pull request #71330 from Geometror/richtextlabel-fit-content

[RichTextLabel] Match minimum size calculation of Label (proper content fitting)
This commit is contained in:
Rémi Verschelde 2023-01-25 09:16:38 +01:00
commit fd66a86d72
No known key found for this signature in database
GPG Key ID: C3336907360768E1
6 changed files with 31 additions and 43 deletions

View File

@ -8,7 +8,7 @@
[b]Note:[/b] Assignments to [member text] clear the tag stack and reconstruct it from the property's contents. Any edits made to [member text] will erase previous edits made from other manual sources such as [method append_text] and the [code]push_*[/code] / [method pop] methods.
[b]Note:[/b] RichTextLabel doesn't support entangled BBCode tags. For example, instead of using [code][b]bold[i]bold italic[/b]italic[/i][/code], use [code][b]bold[i]bold italic[/i][/b][i]italic[/i][/code].
[b]Note:[/b] [code]push_*/pop[/code] functions won't affect BBCode.
[b]Note:[/b] Unlike [Label], RichTextLabel doesn't have a [i]property[/i] to horizontally align text to the center. Instead, enable [member bbcode_enabled] and surround the text in a [code][center][/code] tag as follows: [code][center]Example[/center][/code]. There is currently no built-in way to vertically align text either, but this can be emulated by relying on anchors/containers and the [member fit_content_height] property.
[b]Note:[/b] Unlike [Label], RichTextLabel doesn't have a [i]property[/i] to horizontally align text to the center. Instead, enable [member bbcode_enabled] and surround the text in a [code][center][/code] tag as follows: [code][center]Example[/center][/code]. There is currently no built-in way to vertically align text either, but this can be emulated by relying on anchors/containers and the [member fit_content] property.
</description>
<tutorials>
<link title="BBCode in RichTextLabel">$DOCS_URL/tutorials/ui/bbcode_in_richtextlabel.html</link>
@ -474,9 +474,8 @@
<member name="deselect_on_focus_loss_enabled" type="bool" setter="set_deselect_on_focus_loss_enabled" getter="is_deselect_on_focus_loss_enabled" default="true">
If [code]true[/code], the selected text will be deselected when focus is lost.
</member>
<member name="fit_content_height" type="bool" setter="set_fit_content_height" getter="is_fit_content_height_enabled" default="false">
If [code]true[/code], the label's height will be automatically updated to fit its content.
[b]Note:[/b] This property is used as a workaround to fix issues with [RichTextLabel] in [Container]s, but it's unreliable in some cases and will be removed in future versions.
<member name="fit_content" type="bool" setter="set_fit_content" getter="is_fit_content_enabled" default="false">
If [code]true[/code], the label's minimum size will be automatically updated to fit its content, matching the behavior of [Label].
</member>
<member name="hint_underlined" type="bool" setter="set_hint_underline" getter="is_hint_underlined" default="true">
If [code]true[/code], the label underlines hint tags such as [code][hint=description]{text}[/hint][/code].

View File

@ -567,7 +567,7 @@ ConnectDialog::~ConnectDialog() {
// Originally copied and adapted from EditorProperty, try to keep style in sync.
Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
EditorHelpBit *help_bit = memnew(EditorHelpBit);
help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
help_bit->get_rich_text()->set_custom_minimum_size(Size2(360 * EDSCALE, 1));
// p_text is expected to be something like this:
// "gui_input::(event: InputEvent)::<Signal description>"

View File

@ -2370,7 +2370,7 @@ EditorHelpBit::EditorHelpBit() {
rich_text = memnew(RichTextLabel);
add_child(rich_text);
rich_text->connect("meta_clicked", callable_mp(this, &EditorHelpBit::_meta_clicked));
rich_text->set_fit_content_height(true);
rich_text->set_fit_content(true);
set_custom_minimum_size(Size2(0, 50 * EDSCALE));
}

View File

@ -886,7 +886,7 @@ void EditorProperty::_update_pin_flags() {
static Control *make_help_bit(const String &p_text, bool p_property) {
EditorHelpBit *help_bit = memnew(EditorHelpBit);
help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
help_bit->get_rich_text()->set_custom_minimum_size(Size2(360 * EDSCALE, 1));
PackedStringArray slices = p_text.split("::", false);
if (slices.is_empty()) {

View File

@ -2778,7 +2778,7 @@ bool RichTextLabel::_validate_line_caches() {
main->first_resized_line.store(main->lines.size());
if (fit_content_height) {
if (fit_content) {
update_minimum_size();
}
return true;
@ -2862,7 +2862,7 @@ void RichTextLabel::_process_line_caches() {
main->first_resized_line.store(main->lines.size());
main->first_invalid_font_line.store(main->lines.size());
if (fit_content_height) {
if (fit_content) {
update_minimum_size();
}
emit_signal(SNAME("finished"));
@ -2963,7 +2963,7 @@ void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline)
_invalidate_current_line(current_frame);
if (fixed_width != -1) {
if (fit_content) {
update_minimum_size();
}
queue_redraw();
@ -3551,7 +3551,7 @@ void RichTextLabel::clear() {
scroll_following = true;
}
if (fixed_width != -1) {
if (fit_content) {
update_minimum_size();
}
}
@ -3572,15 +3572,17 @@ int RichTextLabel::get_tab_size() const {
return tab_size;
}
void RichTextLabel::set_fit_content_height(bool p_enabled) {
if (p_enabled != fit_content_height) {
fit_content_height = p_enabled;
update_minimum_size();
void RichTextLabel::set_fit_content(bool p_enabled) {
if (p_enabled == fit_content) {
return;
}
fit_content = p_enabled;
update_minimum_size();
}
bool RichTextLabel::is_fit_content_height_enabled() const {
return fit_content_height;
bool RichTextLabel::is_fit_content_enabled() const {
return fit_content;
}
void RichTextLabel::set_meta_underline(bool p_underline) {
@ -5381,8 +5383,8 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_tab_size", "spaces"), &RichTextLabel::set_tab_size);
ClassDB::bind_method(D_METHOD("get_tab_size"), &RichTextLabel::get_tab_size);
ClassDB::bind_method(D_METHOD("set_fit_content_height", "enabled"), &RichTextLabel::set_fit_content_height);
ClassDB::bind_method(D_METHOD("is_fit_content_height_enabled"), &RichTextLabel::is_fit_content_height_enabled);
ClassDB::bind_method(D_METHOD("set_fit_content", "enabled"), &RichTextLabel::set_fit_content);
ClassDB::bind_method(D_METHOD("is_fit_content_enabled"), &RichTextLabel::is_fit_content_enabled);
ClassDB::bind_method(D_METHOD("set_selection_enabled", "enabled"), &RichTextLabel::set_selection_enabled);
ClassDB::bind_method(D_METHOD("is_selection_enabled"), &RichTextLabel::is_selection_enabled);
@ -5457,7 +5459,7 @@ void RichTextLabel::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content_height"), "set_fit_content_height", "is_fit_content_height_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content"), "set_fit_content", "is_fit_content_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
@ -5644,27 +5646,17 @@ int RichTextLabel::get_total_glyph_count() const {
return tg;
}
void RichTextLabel::set_fixed_size_to_width(int p_width) {
if (fixed_width == p_width) {
return;
}
fixed_width = p_width;
update_minimum_size();
}
Size2 RichTextLabel::get_minimum_size() const {
Size2 size = theme_cache.normal_style->get_minimum_size();
Size2 sb_min_size = theme_cache.normal_style->get_minimum_size();
Size2 min_size;
if (fixed_width != -1) {
size.x += fixed_width;
if (fit_content) {
min_size.x = get_content_width();
min_size.y = get_content_height();
}
if (fit_content_height) {
size.y += get_content_height();
}
return size;
return sb_min_size +
((autowrap_mode != TextServer::AUTOWRAP_OFF) ? Size2(1, min_size.height) : min_size);
}
// Context menu.

View File

@ -524,9 +524,7 @@ private:
bool use_bbcode = false;
String text;
int fixed_width = -1;
bool fit_content_height = false;
bool fit_content = false;
struct ThemeCache {
Ref<StyleBox> normal_style;
@ -640,8 +638,8 @@ public:
void set_shortcut_keys_enabled(bool p_enabled);
bool is_shortcut_keys_enabled() const;
void set_fit_content_height(bool p_enabled);
bool is_fit_content_height_enabled() const;
void set_fit_content(bool p_enabled);
bool is_fit_content_enabled() const;
bool search(const String &p_string, bool p_from_selection = false, bool p_search_previous = false);
@ -731,7 +729,6 @@ public:
void install_effect(const Variant effect);
void set_fixed_size_to_width(int p_width);
virtual Size2 get_minimum_size() const override;
RichTextLabel(const String &p_text = String());