Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad5ebd7

Browse files
committedOct 27, 2023
Merge pull request #83893 from kitbdev/save-current-tab
Save current tab in `TabBar` and `TabContainer`
2 parents d3fb6c1 + 8985a46 commit ad5ebd7

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed
 

‎scene/gui/tab_bar.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ void TabBar::_shape(int p_tab) {
334334

335335
void TabBar::_notification(int p_what) {
336336
switch (p_what) {
337+
case NOTIFICATION_ENTER_TREE: {
338+
if (scroll_to_selected) {
339+
ensure_tab_visible(current);
340+
}
341+
} break;
342+
337343
case NOTIFICATION_INTERNAL_PROCESS: {
338344
Input *input = Input::get_singleton();
339345

@@ -1745,7 +1751,10 @@ void TabBar::_bind_methods() {
17451751
ADD_SIGNAL(MethodInfo("tab_hovered", PropertyInfo(Variant::INT, "tab")));
17461752
ADD_SIGNAL(MethodInfo("active_tab_rearranged", PropertyInfo(Variant::INT, "idx_to")));
17471753

1748-
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
1754+
// "current_tab" property must come after "tab_count", otherwise the property isn't loaded correctly.
1755+
ADD_ARRAY_COUNT("Tabs", "tab_count", "set_tab_count", "get_tab_count", "tab_");
1756+
1757+
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1"), "set_current_tab", "get_current_tab");
17491758
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
17501759
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
17511760
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_close_display_policy", PROPERTY_HINT_ENUM, "Show Never,Show Active Only,Show Always"), "set_tab_close_display_policy", "get_tab_close_display_policy");
@@ -1756,8 +1765,6 @@ void TabBar::_bind_methods() {
17561765
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_to_selected"), "set_scroll_to_selected", "get_scroll_to_selected");
17571766
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_with_rmb"), "set_select_with_rmb", "get_select_with_rmb");
17581767

1759-
ADD_ARRAY_COUNT("Tabs", "tab_count", "set_tab_count", "get_tab_count", "tab_");
1760-
17611768
BIND_ENUM_CONSTANT(ALIGNMENT_LEFT);
17621769
BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
17631770
BIND_ENUM_CONSTANT(ALIGNMENT_RIGHT);

‎scene/gui/tab_container.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ void TabContainer::_notification(int p_what) {
143143
}
144144
} break;
145145

146+
case NOTIFICATION_POST_ENTER_TREE: {
147+
if (setup_current_tab >= 0) {
148+
set_current_tab(setup_current_tab);
149+
setup_current_tab = -1;
150+
}
151+
} break;
152+
146153
case NOTIFICATION_READY:
147154
case NOTIFICATION_RESIZED: {
148155
_update_margins();
@@ -528,6 +535,10 @@ int TabContainer::get_tab_count() const {
528535
}
529536

530537
void TabContainer::set_current_tab(int p_current) {
538+
if (!is_inside_tree()) {
539+
setup_current_tab = p_current;
540+
return;
541+
}
531542
tab_bar->set_current_tab(p_current);
532543
}
533544

@@ -912,7 +923,7 @@ void TabContainer::_bind_methods() {
912923
ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
913924

914925
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
915-
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
926+
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1"), "set_current_tab", "get_current_tab");
916927
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
917928
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
918929
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "all_tabs_in_front"), "set_all_tabs_in_front", "is_all_tabs_in_front");

‎scene/gui/tab_container.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TabContainer : public Container {
4747
bool theme_changing = false;
4848
Vector<Control *> children_removing;
4949
bool drag_to_rearrange_enabled = false;
50+
int setup_current_tab = -1;
5051

5152
struct ThemeCache {
5253
int side_margin = 0;

0 commit comments

Comments
 (0)
Please sign in to comment.