Skip to content

Commit

Permalink
Tab Bar: Allow calling SetTabItemClosed() after a tab has been submit…
Browse files Browse the repository at this point in the history
…ted (will process next frame).

+ larger combo height on TabBarTabListPopupButton()
  • Loading branch information
ocornut committed Aug 17, 2020
1 parent 0e5b1ea commit 46d7520
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Other Changes:
limits when close-enough by (WindowPadding - ItemPadding), which was a tweak with too many
side-effects. The behavior is still present in SetScrollHere functions as they are more explicitly
aiming at making widgets visible. May later be moved to a flag.
- Tab Bar: Allow calling SetTabItemClosed() after a tab has been submitted (will process next frame).
- InvisibleButton: Made public a small selection of ImGuiButtonFlags (previously in imgui_internal.h)
and allowed to pass them to InvisibleButton(): ImGuiButtonFlags_MouseButtonLeft/Right/Middle.
This is a small but rather important change because lots of multi-button behaviors could previously
Expand Down
1 change: 1 addition & 0 deletions docs/TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
- dock: merge docking branch (#2109)
- dock: dock out from a collapsing header? would work nicely but need emitting window to keep submitting the code.

- tabs: while dragging/reordering a tab, close button decoration shouldn't appear on other tabs
- tabs: make EndTabBar fail if users doesn't respect BeginTabBar return value, for consistency/future-proofing.
- tabs: persistent order/focus in BeginTabBar() api (#261, #351)
- tabs: TabItem could honor SetNextItemWidth()?
Expand Down
9 changes: 5 additions & 4 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1696,19 +1696,20 @@ enum ImGuiTabItemFlagsPrivate_
ImGuiTabItemFlags_NoCloseButton = 1 << 20 // Track whether p_open was set or not (we'll need this info on the next frame to recompute ContentWidth during layout)
};

// Storage for one active tab item (sizeof() 26~32 bytes)
// Storage for one active tab item (sizeof() 28~32 bytes)
struct ImGuiTabItem
{
ImGuiID ID;
ImGuiTabItemFlags Flags;
int LastFrameVisible;
int LastFrameSelected; // This allows us to infer an ordered list of the last activated tabs with little maintenance
int NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames
float Offset; // Position relative to beginning of tab
float Width; // Width currently displayed
float ContentWidth; // Width of actual contents, stored during BeginTabItem() call
ImS16 NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames
bool WantClose; // Marked as closed by SetTabItemClosed()

ImGuiTabItem() { ID = 0; Flags = ImGuiTabItemFlags_None; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = ContentWidth = 0.0f; }
ImGuiTabItem() { ID = 0; Flags = ImGuiTabItemFlags_None; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = ContentWidth = 0.0f; WantClose = false; }
};

// Storage for a tab bar (sizeof() 92~96 bytes)
Expand Down Expand Up @@ -1743,7 +1744,7 @@ struct ImGuiTabBar
int GetTabOrder(const ImGuiTabItem* tab) const { return Tabs.index_from_ptr(tab); }
const char* GetTabName(const ImGuiTabItem* tab) const
{
IM_ASSERT(tab->NameOffset != -1 && tab->NameOffset < TabsNames.Buf.Size);
IM_ASSERT(tab->NameOffset != -1 && (int)tab->NameOffset < TabsNames.Buf.Size);
return TabsNames.Buf.Data + tab->NameOffset;
}
};
Expand Down
21 changes: 11 additions & 10 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6709,15 +6709,17 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)
ImGuiContext& g = *GImGui;
tab_bar->WantLayout = false;

// Garbage collect
// Garbage collect by compacting list
int tab_dst_n = 0;
for (int tab_src_n = 0; tab_src_n < tab_bar->Tabs.Size; tab_src_n++)
{
ImGuiTabItem* tab = &tab_bar->Tabs[tab_src_n];
if (tab->LastFrameVisible < tab_bar->PrevFrameVisible)
if (tab->LastFrameVisible < tab_bar->PrevFrameVisible || tab->WantClose)
{
if (tab->ID == tab_bar->SelectedTabId)
tab_bar->SelectedTabId = 0;
// Remove tab
if (tab_bar->VisibleTabId == tab->ID) { tab_bar->VisibleTabId = 0; }
if (tab_bar->SelectedTabId == tab->ID) { tab_bar->SelectedTabId = 0; }
if (tab_bar->NextSelectedTabId == tab->ID) { tab_bar->NextSelectedTabId = 0; }
continue;
}
if (tab_dst_n != tab_src_n)
Expand Down Expand Up @@ -7039,7 +7041,7 @@ static ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar)
arrow_col.w *= 0.5f;
PushStyleColor(ImGuiCol_Text, arrow_col);
PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
bool open = BeginCombo("##v", NULL, ImGuiComboFlags_NoPreview);
bool open = BeginCombo("##v", NULL, ImGuiComboFlags_NoPreview | ImGuiComboFlags_HeightLargest);
PopStyleColor(2);

ImGuiTabItem* tab_to_select = NULL;
Expand Down Expand Up @@ -7167,7 +7169,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open,
tab->Flags = flags;

// Append name with zero-terminator
tab->NameOffset = tab_bar->TabsNames.size();
tab->NameOffset = (ImS16)tab_bar->TabsNames.size();
tab_bar->TabsNames.append(label, label + strlen(label) + 1);

// If we are not reorderable, always reset offset based on submission order.
Expand Down Expand Up @@ -7316,19 +7318,18 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open,
}

// [Public] This is call is 100% optional but it allows to remove some one-frame glitches when a tab has been unexpectedly removed.
// To use it to need to call the function SetTabItemClosed() after BeginTabBar() and before any call to BeginTabItem().
// To use it to need to call the function SetTabItemClosed() between BeginTabBar() and EndTabBar().
// Tabs closed by the close button will automatically be flagged to avoid this issue.
// FIXME: We should aim to support calling SetTabItemClosed() after the tab submission (for next frame)
void ImGui::SetTabItemClosed(const char* label)
{
ImGuiContext& g = *GImGui;
bool is_within_manual_tab_bar = g.CurrentTabBar && !(g.CurrentTabBar->Flags & ImGuiTabBarFlags_DockNode);
if (is_within_manual_tab_bar)
{
ImGuiTabBar* tab_bar = g.CurrentTabBar;
IM_ASSERT(tab_bar->WantLayout); // Needs to be called AFTER BeginTabBar() and BEFORE the first call to BeginTabItem()
ImGuiID tab_id = TabBarCalcTabID(tab_bar, label);
TabBarRemoveTab(tab_bar, tab_id);
if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_id))
tab->WantClose = true; // Will be processed by next call to TabBarLayout()
}
}

Expand Down

0 comments on commit 46d7520

Please sign in to comment.