From b37ddfc606a5cff69f7ba8dd4bd2ee3c09b6b5c2 Mon Sep 17 00:00:00 2001 From: Mansur Isaev <737dab2f169a@mail.ru> Date: Thu, 7 Mar 2024 15:18:27 +0400 Subject: [PATCH] Add tab tooltip text --- doc/classes/TabBar.xml | 16 ++++++++++++++++ doc/classes/TabContainer.xml | 16 ++++++++++++++++ scene/gui/tab_bar.cpp | 29 ++++++++++++++++++++++++++++- scene/gui/tab_bar.h | 7 +++++++ scene/gui/tab_container.cpp | 12 ++++++++++++ scene/gui/tab_container.h | 3 +++ 6 files changed, 82 insertions(+), 1 deletion(-) diff --git a/doc/classes/TabBar.xml b/doc/classes/TabBar.xml index a2beef81eb7..d080294c1a4 100644 --- a/doc/classes/TabBar.xml +++ b/doc/classes/TabBar.xml @@ -111,6 +111,13 @@ Returns the title of the tab at index [param tab_idx]. + + + + + Returns the tooltip text of the tab at index [param tab_idx]. + + @@ -224,6 +231,15 @@ Sets a [param title] for the tab at index [param tab_idx]. + + + + + + Sets a [param tooltip] for tab at index [param tab_idx]. + [b]Note:[/b] By default, if the [param tooltip] is empty and the tab text is truncated (not all characters fit into the tab), the title will be displayed as a tooltip. To hide the tooltip, assign [code]" "[/code] as the [param tooltip] text. + + diff --git a/doc/classes/TabContainer.xml b/doc/classes/TabContainer.xml index b0e3f677919..f4d69c3076b 100644 --- a/doc/classes/TabContainer.xml +++ b/doc/classes/TabContainer.xml @@ -92,6 +92,13 @@ Returns the title of the tab at index [param tab_idx]. Tab titles default to the name of the indexed child node, but this can be overridden with [method set_tab_title]. + + + + + Returns the tooltip text of the tab at index [param tab_idx]. + + @@ -173,6 +180,15 @@ Sets a custom title for the tab at index [param tab_idx] (tab titles default to the name of the indexed child node). Set it back to the child's name to make the tab default to it again. + + + + + + Sets a custom tooltip text for tab at index [param tab_idx]. + [b]Note:[/b] By default, if the [param tooltip] is empty and the tab text is truncated (not all characters fit into the tab), the title will be displayed as a tooltip. To hide the tooltip, assign [code]" "[/code] as the [param tooltip] text. + + diff --git a/scene/gui/tab_bar.cpp b/scene/gui/tab_bar.cpp index 2d687eb2017..0e130d60af7 100644 --- a/scene/gui/tab_bar.cpp +++ b/scene/gui/tab_bar.cpp @@ -323,6 +323,19 @@ void TabBar::gui_input(const Ref &p_event) { } } +String TabBar::get_tooltip(const Point2 &p_pos) const { + int tab_idx = get_tab_idx_at_point(p_pos); + if (tab_idx < 0) { + return Control::get_tooltip(p_pos); + } + + if (tabs[tab_idx].tooltip.is_empty() && tabs[tab_idx].truncated) { + return tabs[tab_idx].text; + } + + return tabs[tab_idx].tooltip; +} + void TabBar::_shape(int p_tab) { tabs.write[p_tab].text_buf->clear(); tabs.write[p_tab].text_buf->set_width(-1); @@ -757,6 +770,16 @@ String TabBar::get_tab_title(int p_tab) const { return tabs[p_tab].text; } +void TabBar::set_tab_tooltip(int p_tab, const String &p_tooltip) { + ERR_FAIL_INDEX(p_tab, tabs.size()); + tabs.write[p_tab].tooltip = p_tooltip; +} + +String TabBar::get_tab_tooltip(int p_tab) const { + ERR_FAIL_INDEX_V(p_tab, tabs.size(), ""); + return tabs[p_tab].tooltip; +} + void TabBar::set_tab_text_direction(int p_tab, Control::TextDirection p_text_direction) { ERR_FAIL_INDEX(p_tab, tabs.size()); ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3); @@ -998,7 +1021,8 @@ void TabBar::_update_cache(bool p_update_hover) { tabs.write[i].size_text = Math::ceil(tabs[i].text_buf->get_size().x); tabs.write[i].size_cache = get_tab_width(i); - if (max_width > 0 && tabs[i].size_cache > max_width) { + tabs.write[i].truncated = max_width > 0 && tabs[i].size_cache > max_width; + if (tabs[i].truncated) { int size_textless = tabs[i].size_cache - tabs[i].size_text; int mw = MAX(size_textless, max_width); @@ -1730,6 +1754,8 @@ void TabBar::_bind_methods() { ClassDB::bind_method(D_METHOD("select_next_available"), &TabBar::select_next_available); ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabBar::set_tab_title); ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabBar::get_tab_title); + ClassDB::bind_method(D_METHOD("set_tab_tooltip", "tab_idx", "tooltip"), &TabBar::set_tab_tooltip); + ClassDB::bind_method(D_METHOD("get_tab_tooltip", "tab_idx"), &TabBar::get_tab_tooltip); ClassDB::bind_method(D_METHOD("set_tab_text_direction", "tab_idx", "direction"), &TabBar::set_tab_text_direction); ClassDB::bind_method(D_METHOD("get_tab_text_direction", "tab_idx"), &TabBar::get_tab_text_direction); ClassDB::bind_method(D_METHOD("set_tab_language", "tab_idx", "language"), &TabBar::set_tab_language); @@ -1843,6 +1869,7 @@ void TabBar::_bind_methods() { base_property_helper.set_prefix("tab_"); base_property_helper.register_property(PropertyInfo(Variant::STRING, "title"), defaults.text, &TabBar::set_tab_title, &TabBar::get_tab_title); + base_property_helper.register_property(PropertyInfo(Variant::STRING, "tooltip"), defaults.tooltip, &TabBar::set_tab_tooltip, &TabBar::get_tab_tooltip); base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &TabBar::set_tab_icon, &TabBar::get_tab_icon); base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &TabBar::set_tab_disabled, &TabBar::is_tab_disabled); } diff --git a/scene/gui/tab_bar.h b/scene/gui/tab_bar.h index 6c09e960f13..52f1da5ec87 100644 --- a/scene/gui/tab_bar.h +++ b/scene/gui/tab_bar.h @@ -56,6 +56,7 @@ public: private: struct Tab { String text; + String tooltip; String language; Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED; @@ -66,6 +67,8 @@ private: bool disabled = false; bool hidden = false; + bool truncated = false; + Variant metadata; int ofs_cache = 0; int size_cache = 0; @@ -168,6 +171,7 @@ private: protected: virtual void gui_input(const Ref &p_event) override; + virtual String get_tooltip(const Point2 &p_pos) const override; bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); } bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); } @@ -192,6 +196,9 @@ public: void set_tab_title(int p_tab, const String &p_title); String get_tab_title(int p_tab) const; + void set_tab_tooltip(int p_tab, const String &p_tooltip); + String get_tab_tooltip(int p_tab) const; + void set_tab_text_direction(int p_tab, TextDirection p_text_direction); TextDirection get_tab_text_direction(int p_tab) const; diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp index 16358ac21f9..4750d2f63e0 100644 --- a/scene/gui/tab_container.cpp +++ b/scene/gui/tab_container.cpp @@ -400,6 +400,7 @@ void TabContainer::move_tab_from_tab_container(TabContainer *p_from, int p_from_ // Get the tab properties before they get erased by the child removal. String tab_title = p_from->get_tab_title(p_from_index); + String tab_tooltip = p_from->get_tab_tooltip(p_from_index); Ref tab_icon = p_from->get_tab_icon(p_from_index); Ref tab_button_icon = p_from->get_tab_button_icon(p_from_index); bool tab_disabled = p_from->is_tab_disabled(p_from_index); @@ -417,6 +418,7 @@ void TabContainer::move_tab_from_tab_container(TabContainer *p_from, int p_from_ move_child(moving_tabc, get_tab_control(p_to_index)->get_index(false)); set_tab_title(p_to_index, tab_title); + set_tab_tooltip(p_to_index, tab_tooltip); set_tab_icon(p_to_index, tab_icon); set_tab_button_icon(p_to_index, tab_button_icon); set_tab_disabled(p_to_index, tab_disabled); @@ -770,6 +772,14 @@ String TabContainer::get_tab_title(int p_tab) const { return tab_bar->get_tab_title(p_tab); } +void TabContainer::set_tab_tooltip(int p_tab, const String &p_tooltip) { + tab_bar->set_tab_tooltip(p_tab, p_tooltip); +} + +String TabContainer::get_tab_tooltip(int p_tab) const { + return tab_bar->get_tab_tooltip(p_tab); +} + void TabContainer::set_tab_icon(int p_tab, const Ref &p_icon) { if (tab_bar->get_tab_icon(p_tab) == p_icon) { return; @@ -978,6 +988,8 @@ void TabContainer::_bind_methods() { ClassDB::bind_method(D_METHOD("is_all_tabs_in_front"), &TabContainer::is_all_tabs_in_front); ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title); ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title); + ClassDB::bind_method(D_METHOD("set_tab_tooltip", "tab_idx", "tooltip"), &TabContainer::set_tab_tooltip); + ClassDB::bind_method(D_METHOD("get_tab_tooltip", "tab_idx"), &TabContainer::get_tab_tooltip); ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon); ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon); ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled); diff --git a/scene/gui/tab_container.h b/scene/gui/tab_container.h index 8645a6d14e9..c11d9824e7f 100644 --- a/scene/gui/tab_container.h +++ b/scene/gui/tab_container.h @@ -155,6 +155,9 @@ public: void set_tab_title(int p_tab, const String &p_title); String get_tab_title(int p_tab) const; + void set_tab_tooltip(int p_tab, const String &p_tooltip); + String get_tab_tooltip(int p_tab) const; + void set_tab_icon(int p_tab, const Ref &p_icon); Ref get_tab_icon(int p_tab) const;