Skip to content

Commit c4b9101

Browse files
committed
TabBar: Tweak shrinking policy so that while resizing tabs that don't need shrinking keep their initial width more precisely.
Has been the case before but adding support for SetNextItemWidth() ocornut#5262 made this more noticeable.
1 parent 4b97296 commit c4b9101

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

docs/CHANGELOG.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Other Changes:
106106
- Inputs: Fixed IsMouseClicked() repeat mode rate being half of keyboard repeat rate.
107107
- ColorEdit: Fixed text baseline alignment after a SameLine() after a ColorEdit() with visible label.
108108
- TabBar: TabItem() now reacts to SetNextItemWidth() and SetNextItemOpen(true). (#5262)
109+
- TabBar: Tweak shrinking policy so that while resizing tabs that don't need shrinking keep their
110+
initial width more precisely (without the occasional +1 worth of width).
109111
- Menus: Adjusted BeginMenu() closing logic so hovering void or non-MenuItem() in parent window
110112
always lead to menu closure. Fixes using items that are not MenuItem() or BeginItem() at the root
111113
level of a popup with a child menu opened.

imgui_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,7 @@ struct ImGuiShrinkWidthItem
11501150
{
11511151
int Index;
11521152
float Width;
1153+
float InitialWidth;
11531154
};
11541155

11551156
struct ImGuiPtrOrIndex

imgui_widgets.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_exc
15401540
width_excess -= width_to_remove_per_item * count_same_width;
15411541
}
15421542

1543-
// Round width and redistribute remainder left-to-right (could make it an option of the function?)
1543+
// Round width and redistribute remainder
15441544
// Ensure that e.g. the right-most tab of a shrunk tab-bar always reaches exactly at the same distance from the right-most edge of the tab bar separator.
15451545
width_excess = 0.0f;
15461546
for (int n = 0; n < count; n++)
@@ -1549,10 +1549,13 @@ void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_exc
15491549
width_excess += items[n].Width - width_rounded;
15501550
items[n].Width = width_rounded;
15511551
}
1552-
if (width_excess > 0.0f)
1552+
while (width_excess > 0.0f)
15531553
for (int n = 0; n < count; n++)
1554-
if (items[n].Index < (int)(width_excess + 0.01f))
1554+
if (items[n].Width + 1.0f <= items[n].InitialWidth)
1555+
{
15551556
items[n].Width += 1.0f;
1557+
width_excess -= 1.0f;
1558+
}
15561559
}
15571560

15581561
//-------------------------------------------------------------------------
@@ -7557,9 +7560,9 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)
75577560

75587561
// Store data so we can build an array sorted by width if we need to shrink tabs down
75597562
IM_MSVC_WARNING_SUPPRESS(6385);
7560-
int shrink_buffer_index = shrink_buffer_indexes[section_n]++;
7561-
g.ShrinkWidthBuffer[shrink_buffer_index].Index = tab_n;
7562-
g.ShrinkWidthBuffer[shrink_buffer_index].Width = tab->ContentWidth;
7563+
ImGuiShrinkWidthItem* shrink_width_item = &g.ShrinkWidthBuffer[shrink_buffer_indexes[section_n]++];
7564+
shrink_width_item->Index = tab_n;
7565+
shrink_width_item->Width = shrink_width_item->InitialWidth = tab->ContentWidth;
75637566

75647567
IM_ASSERT(tab->ContentWidth > 0.0f);
75657568
tab->Width = tab->ContentWidth;

0 commit comments

Comments
 (0)