From 9d2aecade0822a13caa860b2dda0d665621253d5 Mon Sep 17 00:00:00 2001
From: ShadyChibinko <60264149+ShadyChibinko@users.noreply.github.com>
Date: Tue, 11 Apr 2023 12:48:47 -0400
Subject: [PATCH] Add Tab Metadata
---
doc/classes/TabBar.xml | 15 +++++++++++++++
doc/classes/TabContainer.xml | 15 +++++++++++++++
scene/gui/tab_bar.cpp | 17 +++++++++++++++++
scene/gui/tab_bar.h | 4 ++++
scene/gui/tab_container.cpp | 14 ++++++++++++++
scene/gui/tab_container.h | 3 +++
6 files changed, 68 insertions(+)
diff --git a/doc/classes/TabBar.xml b/doc/classes/TabBar.xml
index 7211fc21378..013f853298f 100644
--- a/doc/classes/TabBar.xml
+++ b/doc/classes/TabBar.xml
@@ -77,6 +77,13 @@
Returns tab title language code.
+
+
+
+
+ Returns the metadata value set to the tab at index [param tab_idx] using [method set_tab_metadata]. If no metadata was previously set, returns [code]null[/code] by default.
+
+
@@ -181,6 +188,14 @@
Sets language code of tab title used for line-breaking and text shaping algorithms, if left empty current locale is used instead.
+
+
+
+
+
+ Sets the metadata value for the tab at index [param tab_idx], which can be retrieved later using [method get_tab_metadata].
+
+
diff --git a/doc/classes/TabContainer.xml b/doc/classes/TabContainer.xml
index b0dcb932dc4..dbc769cfe90 100644
--- a/doc/classes/TabContainer.xml
+++ b/doc/classes/TabContainer.xml
@@ -72,6 +72,13 @@
Returns the index of the tab tied to the given [param control]. The control must be a child of the [TabContainer].
+
+
+
+
+ Returns the metadata value set to the tab at index [param tab_idx] using [method set_tab_metadata]. If no metadata was previously set, returns [code]null[/code] by default.
+
+
@@ -132,6 +139,14 @@
Sets an icon for the tab at index [param tab_idx].
+
+
+
+
+
+ Sets the metadata value for the tab at index [param tab_idx], which can be retrieved later using [method get_tab_metadata].
+
+
diff --git a/scene/gui/tab_bar.cpp b/scene/gui/tab_bar.cpp
index 5eb5a519ff1..d664bea1681 100644
--- a/scene/gui/tab_bar.cpp
+++ b/scene/gui/tab_bar.cpp
@@ -791,6 +791,21 @@ bool TabBar::is_tab_hidden(int p_tab) const {
return tabs[p_tab].hidden;
}
+void TabBar::set_tab_metadata(int p_tab, const Variant &p_metadata) {
+ ERR_FAIL_INDEX(p_tab, tabs.size());
+
+ if (tabs[p_tab].metadata == p_metadata) {
+ return;
+ }
+
+ tabs.write[p_tab].metadata = p_metadata;
+}
+
+Variant TabBar::get_tab_metadata(int p_tab) const {
+ ERR_FAIL_INDEX_V(p_tab, tabs.size(), Variant());
+ return tabs[p_tab].metadata;
+}
+
void TabBar::set_tab_button_icon(int p_tab, const Ref &p_icon) {
ERR_FAIL_INDEX(p_tab, tabs.size());
@@ -1607,6 +1622,8 @@ void TabBar::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabBar::is_tab_disabled);
ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabBar::set_tab_hidden);
ClassDB::bind_method(D_METHOD("is_tab_hidden", "tab_idx"), &TabBar::is_tab_hidden);
+ ClassDB::bind_method(D_METHOD("set_tab_metadata", "tab_idx", "metadata"), &TabBar::set_tab_metadata);
+ ClassDB::bind_method(D_METHOD("get_tab_metadata", "tab_idx"), &TabBar::get_tab_metadata);
ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &TabBar::remove_tab);
ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &TabBar::add_tab, DEFVAL(""), DEFVAL(Ref()));
ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabBar::get_tab_idx_at_point);
diff --git a/scene/gui/tab_bar.h b/scene/gui/tab_bar.h
index a232061b693..fa6aece8270 100644
--- a/scene/gui/tab_bar.h
+++ b/scene/gui/tab_bar.h
@@ -66,6 +66,7 @@ private:
bool disabled = false;
bool hidden = false;
+ Variant metadata;
int ofs_cache = 0;
int size_cache = 0;
int size_text = 0;
@@ -184,6 +185,9 @@ public:
void set_tab_hidden(int p_tab, bool p_hidden);
bool is_tab_hidden(int p_tab) const;
+ void set_tab_metadata(int p_tab, const Variant &p_metadata);
+ Variant get_tab_metadata(int p_tab) const;
+
void set_tab_button_icon(int p_tab, const Ref &p_icon);
Ref get_tab_button_icon(int p_tab) const;
diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp
index f4f71834751..0db2b905de2 100644
--- a/scene/gui/tab_container.cpp
+++ b/scene/gui/tab_container.cpp
@@ -465,7 +465,9 @@ void TabContainer::_drop_data_fw(const Point2 &p_point, const Variant &p_data, C
if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
// Get the tab properties before they get erased by the child removal.
String tab_title = from_tabc->get_tab_title(tab_from_id);
+ Ref tab_icon = from_tabc->get_tab_icon(tab_from_id);
bool tab_disabled = from_tabc->is_tab_disabled(tab_from_id);
+ Variant tab_metadata = from_tabc->get_tab_metadata(tab_from_id);
// Drop the new tab to the left or right depending on where the target tab is being hovered.
if (hover_now != -1) {
@@ -482,7 +484,9 @@ void TabContainer::_drop_data_fw(const Point2 &p_point, const Variant &p_data, C
add_child(moving_tabc, true);
set_tab_title(get_tab_count() - 1, tab_title);
+ set_tab_icon(get_tab_count() - 1, tab_icon);
set_tab_disabled(get_tab_count() - 1, tab_disabled);
+ set_tab_metadata(get_tab_count() - 1, tab_metadata);
move_child(moving_tabc, get_tab_control(hover_now)->get_index(false));
if (!is_tab_disabled(hover_now)) {
@@ -794,6 +798,14 @@ bool TabContainer::is_tab_hidden(int p_tab) const {
return tab_bar->is_tab_hidden(p_tab);
}
+void TabContainer::set_tab_metadata(int p_tab, const Variant &p_metadata) {
+ tab_bar->set_tab_metadata(p_tab, p_metadata);
+}
+
+Variant TabContainer::get_tab_metadata(int p_tab) const {
+ return tab_bar->get_tab_metadata(p_tab);
+}
+
void TabContainer::set_tab_button_icon(int p_tab, const Ref &p_icon) {
tab_bar->set_tab_button_icon(p_tab, p_icon);
@@ -941,6 +953,8 @@ void TabContainer::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabContainer::is_tab_disabled);
ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabContainer::set_tab_hidden);
ClassDB::bind_method(D_METHOD("is_tab_hidden", "tab_idx"), &TabContainer::is_tab_hidden);
+ ClassDB::bind_method(D_METHOD("set_tab_metadata", "tab_idx", "metadata"), &TabContainer::set_tab_metadata);
+ ClassDB::bind_method(D_METHOD("get_tab_metadata", "tab_idx"), &TabContainer::get_tab_metadata);
ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabContainer::set_tab_button_icon);
ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabContainer::get_tab_button_icon);
ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabContainer::get_tab_idx_at_point);
diff --git a/scene/gui/tab_container.h b/scene/gui/tab_container.h
index f64f1fe2a33..e2a77bf477c 100644
--- a/scene/gui/tab_container.h
+++ b/scene/gui/tab_container.h
@@ -135,6 +135,9 @@ public:
void set_tab_hidden(int p_tab, bool p_hidden);
bool is_tab_hidden(int p_tab) const;
+ void set_tab_metadata(int p_tab, const Variant &p_metadata);
+ Variant get_tab_metadata(int p_tab) const;
+
void set_tab_button_icon(int p_tab, const Ref &p_icon);
Ref get_tab_button_icon(int p_tab) const;