Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select default tab not working #7835

Closed
rrizik opened this issue Jul 29, 2024 · 4 comments
Closed

Select default tab not working #7835

rrizik opened this issue Jul 29, 2024 · 4 comments
Labels
settings .ini persistance tabs tab bars, tabs

Comments

@rrizik
Copy link

rrizik commented Jul 29, 2024

Version/Branch of Dear ImGui:

v1.90.9

Back-ends:

imgui_impl_dx11.cpp

Compiler, OS:

Windows 10

Full config/build information:

No response

Details:

I'm writing a program where I can serialize/deserialize data.
One piece of data that I'm saving/loading is which tab was last selected, so that when I load the program, it selects the tab I was last on.

I'm using the flag ImGuiTabItemFlags_SetSelected which almost works but is doing something weird.

More Context:
I'm writing a budgeting program where I have tabs from Jan - Dec to select from. By selecting a Tab, it simply sets the appropriate index for me to use for other parts of the code. I have an array[12] of flags for each Tab so it has its own flag to use so that I can set different tabs with flags of either ImGuiTabItemFlags_SetSelected or ImGuiTabItemFlags_None.

If I default my tab to lets say May, that works. And then if I click on April (one tab left), that works as well in setting the previous tabs flag (May) to None and the new tab (April) to Selected. But if I try and go to any tab to the right, it switches for a frame and then switches back. (So I can only move tabs left essentially, due to order of operations)

When I step through the program, it looks like even though I'm passing in ImGuiTabItemFlags_None into the tab that I'm switching away from (so in this case switching away from April and moving to May), it still evaluates to true, overwriting my selection.

I'm not clear on why or how this tab is evaluating to True even though I'm passing it ImGuiTabItemFlags_None.

I can provide any and all information.
I was having trouble getting a video less than 10mb's on file size which is a the limit. But if it becomes imperative that I provide more, I can try again.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

// Even though pm->tf[3] is 0, it still evaluates this tab as True even though I didn't click on it
            if(ImGui::BeginTabItem("April", 0, pm->tf[3])){
                pm->tf[pm->month_idx] = 0;
                pm->month_idx = Month_Apr;
                pm->tf[pm->month_idx] = ImGuiTabItemFlags_SetSelected;
                ImGui::EndTabItem();
            }
// This is the tab I'm clicking on, attempting to move from April to May
            if(ImGui::BeginTabItem("May", 0, pm->tf[4])){
                pm->tf[pm->month_idx] = 0;
                pm->month_idx = Month_May;
                pm->tf[pm->month_idx] = ImGuiTabItemFlags_SetSelected;
                ImGui::EndTabItem();
            }
@ocornut ocornut added tabs tab bars, tabs settings .ini persistance labels Jul 29, 2024
@ocornut
Copy link
Owner

ocornut commented Jul 29, 2024

When I step through the program, it looks like even though I'm passing in ImGuiTabItemFlags_None into the tab that I'm switching away from (so in this case switching away from April and moving to May), it still evaluates to true, overwriting my selection.

ImGuiTabItemFlags_SetSelected cannot behave on the same frame, see #6681

If i may suggest, your code also seems a little error-prone. You could probably simplify it by storing a single int selected_tab_idx; field.

(We should eventually support auto-saving of selected tab in standalone tab-bar.)

@ocornut
Copy link
Owner

ocornut commented Jul 29, 2024

Apart from the solution of using TabBarQueueFocus() from imgui_internal.h right after BeginTabBar(), your other solution is to only set the ImGuiTabItemFlags_SetSelected flag once on load, there's no necessity to set it every frame, in fact that is what is causing your issue.

@rrizik
Copy link
Author

rrizik commented Jul 29, 2024

Ahhh ok, your second suggestion solved it for me perfectly. Thanks!

"If i may suggest, your code also seems a little error-pron" - yeah I wasn't happy with the code, I was just struggling to anything working haha. All of that code went away and I'm just storing an index.

Thanks for the help

@ocornut ocornut closed this as completed Jul 30, 2024
@ocornut
Copy link
Owner

ocornut commented Jul 30, 2024

Good to hear. Closing now. I will post here again if we end up adding .ini support for standalone tab-bars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
settings .ini persistance tabs tab bars, tabs
Projects
None yet
Development

No branches or pull requests

2 participants