From 8b956216b75cdc7b746d7593193b44caaa5a3708 Mon Sep 17 00:00:00 2001 From: omar Date: Mon, 15 Oct 2018 17:19:52 +0200 Subject: [PATCH] Demo: Testing return value of BeginTabBar() for consistency. --- docs/TODO.txt | 3 +- imgui_demo.cpp | 111 ++++++++++++++++++++++++++----------------------- 2 files changed, 60 insertions(+), 54 deletions(-) diff --git a/docs/TODO.txt b/docs/TODO.txt index a872ef29e..daa347897 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -150,7 +150,8 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i - dock: C- nav: CTRL+TAB highlighting tabs shows the mismatch between focus-stack and tab-order (not visible in VS because it doesn't highlight the tabs) - dock: C- after a dock/undock, the Scrollbar Status update in Begin() should use an updated e.g. size_y_for_scrollbars to avoid a 1 frame scrollbar flicker. - - tabs: re-ordering, close buttons, context menu, persistent order (#261, #351) + - 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) - ext: stl-ish friendly extension (imgui_stl.h) that has wrapper for std::string, std::vector etc. diff --git a/imgui_demo.cpp b/imgui_demo.cpp index e3cf62117..fc448bbf1 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -1721,23 +1721,25 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::TreeNode("Basic")) { ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None; - ImGui::BeginTabBar("MyTabBar", tab_bar_flags); - if (ImGui::BeginTabItem("Avocado")) + if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) { - ImGui::Text("This is the Avocado tab!\nblah blah blah blah blah"); - ImGui::EndTabItem(); + if (ImGui::BeginTabItem("Avocado")) + { + ImGui::Text("This is the Avocado tab!\nblah blah blah blah blah"); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Broccoli")) + { + ImGui::Text("This is the Broccoli tab!\nblah blah blah blah blah"); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Cucumber")) + { + ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah"); + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); } - if (ImGui::BeginTabItem("Broccoli")) - { - ImGui::Text("This is the Broccoli tab!\nblah blah blah blah blah"); - ImGui::EndTabItem(); - } - if (ImGui::BeginTabItem("Cucumber")) - { - ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah"); - ImGui::EndTabItem(); - } - ImGui::EndTabBar(); ImGui::Separator(); ImGui::TreePop(); } @@ -1766,16 +1768,18 @@ void ImGui::ShowDemoWindow(bool* p_open) } // Passing a bool* to BeginTabItem() is similar to passing one to Begin(): the underlying bool will be set to false when the tab is closed. - ImGui::BeginTabBar("MyTabBar", tab_bar_flags); - for (int n = 0; n < IM_ARRAYSIZE(opened); n++) - if (opened[n] && ImGui::BeginTabItem(names[n], &opened[n])) - { - ImGui::Text("This is the %s tab!", names[n]); - if (n & 1) - ImGui::Text("I am an odd tab."); - ImGui::EndTabItem(); - } - ImGui::EndTabBar(); + if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) + { + for (int n = 0; n < IM_ARRAYSIZE(opened); n++) + if (opened[n] && ImGui::BeginTabItem(names[n], &opened[n])) + { + ImGui::Text("This is the %s tab!", names[n]); + if (n & 1) + ImGui::Text("I am an odd tab."); + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); + } ImGui::Separator(); ImGui::TreePop(); } @@ -3952,41 +3956,42 @@ void ShowExampleAppDocuments(bool* p_open) if (opt_target == Target_Tab) { ImGuiTabBarFlags tab_bar_flags = (opt_fitting_flags) | (opt_reorderable ? ImGuiTabBarFlags_Reorderable : 0); - ImGui::BeginTabBar("##tabs", tab_bar_flags); - - if (opt_reorderable) - NotifyOfDocumentsClosedElsewhere(app); - - // [DEBUG] Stress tests - //if ((ImGui::GetFrameCount() % 30) == 0) docs[1].Open ^= 1; // [DEBUG] Automatically show/hide a tab. Test various interactions e.g. dragging with this on. - //if (ImGui::GetIO().KeyCtrl) ImGui::SetTabItemSelected(docs[1].Name); // [DEBUG] Test SetTabItemSelected(), probably not very useful as-is anyway.. - - // Submit Tabs - for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + if (ImGui::BeginTabBar("##tabs", tab_bar_flags)) { - MyDocument* doc = &app.Documents[doc_n]; - if (!doc->Open) - continue; + if (opt_reorderable) + NotifyOfDocumentsClosedElsewhere(app); - ImGuiTabItemFlags tab_flags = (doc->Dirty ? ImGuiTabItemFlags_UnsavedDocument : 0); - bool visible = ImGui::BeginTabItem(doc->Name, &doc->Open, tab_flags); + // [DEBUG] Stress tests + //if ((ImGui::GetFrameCount() % 30) == 0) docs[1].Open ^= 1; // [DEBUG] Automatically show/hide a tab. Test various interactions e.g. dragging with this on. + //if (ImGui::GetIO().KeyCtrl) ImGui::SetTabItemSelected(docs[1].Name); // [DEBUG] Test SetTabItemSelected(), probably not very useful as-is anyway.. - // Cancel attempt to close when unsaved add to save queue so we can display a popup. - if (!doc->Open && doc->Dirty) + // Submit Tabs + for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) { - doc->Open = true; - doc->DoQueueClose(); + MyDocument* doc = &app.Documents[doc_n]; + if (!doc->Open) + continue; + + ImGuiTabItemFlags tab_flags = (doc->Dirty ? ImGuiTabItemFlags_UnsavedDocument : 0); + bool visible = ImGui::BeginTabItem(doc->Name, &doc->Open, tab_flags); + + // Cancel attempt to close when unsaved add to save queue so we can display a popup. + if (!doc->Open && doc->Dirty) + { + doc->Open = true; + doc->DoQueueClose(); + } + + MyDocument::DisplayContextMenu(doc); + if (visible) + { + MyDocument::DisplayContents(doc); + ImGui::EndTabItem(); + } } - MyDocument::DisplayContextMenu(doc); - if (visible) - { - MyDocument::DisplayContents(doc); - ImGui::EndTabItem(); - } + ImGui::EndTabBar(); } - - ImGui::EndTabBar(); } else if (opt_target == Target_Window) {