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

Tabs: Add a way to set the selected tab from the application. #2473

Closed
wants to merge 1 commit into from

Conversation

kudaba
Copy link
Contributor

@kudaba kudaba commented Apr 5, 2019

Add a way for the application to change the selected tab. I was adding functionality to restore window state . Unfortunately while this method works it does have the drawback of not working if the tab doesn't exist yet so I had to use as follows:

foreach (tab) {
    if (ImGui::BeginTabItem(tab)) {
        if (!firstFrame) draw_tab();
        ImGui::EndTabItem();
    }
}

if (firstFrame) ImGui::SetTabItemSelected(selectedTab);

@ocornut
Copy link
Owner

ocornut commented Apr 5, 2019

. Unfortunately while this method works it does have the drawback of not working if the tab doesn't exist yet
"// Due to how tabs are managed, if you call this before the tab exists then the action will get lost. This means you need to add all the tabs first, "

I don't understand this statement, could you clarify?
If there is a bug we could fix the bug and make function works rather than encourage a work-around.

Note that there was a bug fixed in 1.69, a per changelog:
"TabBar: Fixed ImGuiTabItemFlags_SetSelected being ignored if the tab is not visible (with scrolling policy enabled) or if is currently appearing."

Have you checked the ImGuiTabItemFlags_SetSelected flag? (Also see #2410, in that case the user actually only needed ImGuiTabBarFlags_AutoSelectNewTabs).

@kudaba
Copy link
Contributor Author

kudaba commented Apr 5, 2019

Hrm, actually I realize now that I can accomplish the same thing with the flags by simply doing the following:

foreach (tab) {
    var flags = firstFrame && selectedTab == tab ? ImGuiTabItemFlags_SetSelected : 0;
    if (ImGui::BeginTabItem(tab, null, flags)) {
        if (!firstFrame) {
            selecteTab = tab;
            draw_tab();
        }
        ImGui::EndTabItem();
    }
}

I was trying to synchronize my local view of the selected tab with what imgui is doing. The real trick here is to know when it's safe to record the selected tab since there's a frame delay between setting selected and rendering selected.

@kudaba kudaba closed this Apr 5, 2019
@ocornut
Copy link
Owner

ocornut commented Apr 7, 2019

Just to clarify: what you are trying to achieve is persist the last focused tab and/or the tab order?

This is in the TODO list as:
- tabs: persistent order/focus in BeginTabBar() api (#261, #351)

@kudaba
Copy link
Contributor Author

kudaba commented Apr 8, 2019

Exactly, I'm slowly expanding on my ability save/load application state. Right now it's restoring last state on startup, but eventually it will be a full layouting sytem to build and share application layouts, so I'll need ways to set various window/control states outside of imguis default save behavior.

@ocornut
Copy link
Owner

ocornut commented Apr 8, 2019

Well, for tab bar we ought to add the saving features (*) and down the line we could eventually pursue adding more persistance features all accross the board (initially expressed in ~#437).

(*) Note that tab bars created by the docking system already have their order saved by the docking system, which go a slightly different route.

@kudaba kudaba deleted the set_selected_tab branch May 6, 2019 16:12
@ocornut ocornut added the tabs tab bars, tabs label Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tabs tab bars, tabs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants