mirror of
https://github.com/Drezil/imgui.git
synced 2025-07-04 12:08:47 +02:00
Tab Bar: Added TabItemButton(), ImGuiTabItemFlags_Leading, ImGuiTabItemFlags_Trailing + demo. (#3291)
(squashed various commits by 2 authors)
This commit is contained in:
committed by
ocornut
parent
29836412e1
commit
4a57a982be
@ -2354,6 +2354,91 @@ static void ShowDemoWindowLayout()
|
||||
ImGui::Separator();
|
||||
ImGui::TreePop();
|
||||
}
|
||||
|
||||
if (ImGui::TreeNode("TabItem Button"))
|
||||
{
|
||||
static int next_id = 0;
|
||||
static ImVector<int> tabs_list;
|
||||
if (next_id == 0) // Initialize with a default tab
|
||||
for (int i = 0; i < 9; i++)
|
||||
tabs_list.push_back(next_id++);
|
||||
|
||||
static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown;
|
||||
ImGui::CheckboxFlags("ImGuiTabBarFlags_Reorderable", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_Reorderable);
|
||||
ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton);
|
||||
if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown))
|
||||
tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown);
|
||||
if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll))
|
||||
tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll);
|
||||
|
||||
static bool show_leading_button = true;
|
||||
static bool show_trailing_button = true;
|
||||
ImGui::Checkbox("Show Leading TabItemButton()", &show_leading_button);
|
||||
ImGui::Checkbox("Show Trailing TabItemButton()", &show_trailing_button);
|
||||
static bool enable_position = false;
|
||||
ImGui::Checkbox("Enable Leading/Trailing TabItem()", &enable_position);
|
||||
|
||||
if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags))
|
||||
{
|
||||
if (show_leading_button)
|
||||
{
|
||||
if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_NoTooltip))
|
||||
tabs_list.push_back(next_id++);
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("Add a new TabItem() in the tab bar");
|
||||
|
||||
// Popup Context also works with TabItemButton
|
||||
if (ImGui::BeginPopupContextItem())
|
||||
{
|
||||
ImGui::Text("I'm a popup!");
|
||||
if (ImGui::Button("Close"))
|
||||
ImGui::CloseCurrentPopup();
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
}
|
||||
|
||||
bool close_current_tab = false;
|
||||
if (show_trailing_button)
|
||||
{
|
||||
if (ImGui::TabItemButton("X", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip))
|
||||
close_current_tab = true;
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("Close the currently selected TabItem()");
|
||||
}
|
||||
|
||||
for (int n = 0; n < tabs_list.Size; )
|
||||
{
|
||||
int mod = tabs_list[n] % 3;
|
||||
ImGuiTabItemFlags flags = mod == 0 ? 0 : mod == 1 ? ImGuiTabItemFlags_Leading : ImGuiTabItemFlags_Trailing;
|
||||
if (!enable_position)
|
||||
flags = 0;
|
||||
|
||||
char name[16];
|
||||
snprintf(name, IM_ARRAYSIZE(name), "%04d", tabs_list[n]);
|
||||
bool open = true;
|
||||
if (ImGui::BeginTabItem(name, &open, flags))
|
||||
{
|
||||
ImGui::Text("This is the %s tab!", name);
|
||||
ImGui::EndTabItem();
|
||||
|
||||
if (close_current_tab)
|
||||
{
|
||||
open = false;
|
||||
ImGui::SetTabItemClosed(name);
|
||||
}
|
||||
}
|
||||
|
||||
if (!open)
|
||||
tabs_list.erase(tabs_list.Data + n);
|
||||
else
|
||||
++n;
|
||||
}
|
||||
|
||||
ImGui::EndTabBar();
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::TreePop();
|
||||
}
|
||||
ImGui::TreePop();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user