From 9bf09194cc2def3922a44f55be97603630e42e1b Mon Sep 17 00:00:00 2001 From: Michael Alexsander Date: Tue, 26 Sep 2023 14:12:08 -0300 Subject: [PATCH] Make hovered tabs be drawn with the unselected's width at minimum (cherry picked from commit decae848a8fb6399a596418168a1cd0bd4305f9c) --- doc/classes/TabBar.xml | 1 + doc/classes/TabContainer.xml | 1 + scene/gui/tab_bar.cpp | 15 ++++++++++----- scene/gui/tab_bar.h | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/doc/classes/TabBar.xml b/doc/classes/TabBar.xml index cdbe9d933a6..a57a2f292f5 100644 --- a/doc/classes/TabBar.xml +++ b/doc/classes/TabBar.xml @@ -398,6 +398,7 @@ The style of the currently hovered tab. Does not apply to the selected tab. + [b]Note:[/b] This style will be drawn with the same width as [theme_item tab_unselected] at minimum. The style of the currently selected tab. diff --git a/doc/classes/TabContainer.xml b/doc/classes/TabContainer.xml index ebf9bb25841..3f324ffb46f 100644 --- a/doc/classes/TabContainer.xml +++ b/doc/classes/TabContainer.xml @@ -293,6 +293,7 @@ The style of the currently hovered tab. + [b]Note:[/b] This style will be drawn with the same width as [theme_item tab_unselected] at minimum. The style of the currently selected tab. diff --git a/scene/gui/tab_bar.cpp b/scene/gui/tab_bar.cpp index 959a51eff91..0224416d79f 100644 --- a/scene/gui/tab_bar.cpp +++ b/scene/gui/tab_bar.cpp @@ -899,7 +899,7 @@ void TabBar::_update_hover() { } } -void TabBar::_update_cache() { +void TabBar::_update_cache(bool p_update_hover) { if (tabs.is_empty()) { buttons_visible = false; return; @@ -962,7 +962,9 @@ void TabBar::_update_cache() { buttons_visible = offset > 0 || missing_right; if (tab_alignment == ALIGNMENT_LEFT) { - _update_hover(); + if (p_update_hover) { + _update_hover(); + } return; } @@ -980,7 +982,9 @@ void TabBar::_update_cache() { } } - _update_hover(); + if (p_update_hover) { + _update_hover(); + } } void TabBar::_on_mouse_exited() { @@ -990,7 +994,7 @@ void TabBar::_on_mouse_exited() { highlight_arrow = -1; dragging_valid_tab = false; - _update_cache(); + _update_cache(false); queue_redraw(); } @@ -1324,7 +1328,8 @@ int TabBar::get_tab_width(int p_idx) const { style = theme_cache.tab_disabled_style; } else if (current == p_idx) { style = theme_cache.tab_selected_style; - } else if (hover == p_idx) { + // Use the unselected style's width if the hovered one is shorter, to avoid an infinite loop when switching tabs with the mouse. + } else if (hover == p_idx && theme_cache.tab_hovered_style->get_minimum_size().width >= theme_cache.tab_unselected_style->get_minimum_size().width) { style = theme_cache.tab_hovered_style; } else { style = theme_cache.tab_unselected_style; diff --git a/scene/gui/tab_bar.h b/scene/gui/tab_bar.h index 438b6c60b5c..33eefcd90b4 100644 --- a/scene/gui/tab_bar.h +++ b/scene/gui/tab_bar.h @@ -143,7 +143,7 @@ private: void _ensure_no_over_offset(); void _update_hover(); - void _update_cache(); + void _update_cache(bool p_update_hover = true); void _on_mouse_exited();