From 5acbf0b6872b6b17250218fb357a096fc868c8e1 Mon Sep 17 00:00:00 2001 From: Katie McKean Date: Fri, 16 Feb 2024 14:39:34 -0500 Subject: [PATCH] Fixing the issue where the wrong ImGui window can end up in the front. --- src/cata_imgui.cpp | 10 +++++++--- src/ui_manager.cpp | 6 ++---- src/ui_manager.h | 1 + 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/cata_imgui.cpp b/src/cata_imgui.cpp index 024a50c0de411..fbc6f766d70c1 100644 --- a/src/cata_imgui.cpp +++ b/src/cata_imgui.cpp @@ -339,7 +339,8 @@ cataimgui::window::window( int window_flags ) p_impl = nullptr; this->window_flags = window_flags | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | - ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNavFocus; + ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNavFocus | + ImGuiWindowFlags_NoBringToFrontOnFocus; } cataimgui::window::window( const std::string &id_, int window_flags ) : window( window_flags ) @@ -414,7 +415,7 @@ void cataimgui::window::draw() // we want to make sure is_resized is able to be handled for at least a full frame handled_resize = true; } - if( cached_bounds.x == -1 || cached_bounds.y == -1 ) { + if( cached_bounds.x == -1 || cached_bounds.y == -1 ) { ImVec2 center = ImGui::GetMainViewport()->GetCenter(); if( cached_bounds.x != -1.f ) { center.x = cached_bounds.x; @@ -429,8 +430,11 @@ void cataimgui::window::draw() if( cached_bounds.h > 0 || cached_bounds.w > 0 ) { ImGui::SetNextWindowSize( { cached_bounds.w, cached_bounds.h } ); } - if( ImGui::Begin( id.c_str(), &is_open, window_flags ) ) { + if( ImGui::Begin( id.c_str(), &is_open, window_flags) ) { draw_controls(); + if( p_impl->window_adaptor->is_on_top ) { + ImGui::BringWindowToDisplayFront( ImGui::GetCurrentWindow() ); + } } ImGui::End(); if( handled_resize ) { diff --git a/src/ui_manager.cpp b/src/ui_manager.cpp index 4d3ebce44a0d6..46427689341c6 100644 --- a/src/ui_manager.cpp +++ b/src/ui_manager.cpp @@ -417,13 +417,11 @@ void ui_adaptor::redraw_invalidated( ) ui_stack_orig = &*ui_stack_copy; } std::optional cursor_pos; + auto top_ui = std::prev( ui_stack_orig->end() ); for( auto it = first_enabled; !restart_redrawing && it != ui_stack_orig->end(); ++it ) { ui_adaptor &ui = *it; + ui.is_on_top = it == top_ui; if( ui.invalidated || ui.is_imgui ) { - if( ui.redraw_cb ) { - ui.redraw_cb( ui ); - } - } else if( ui.invalidated ) { if( ui.redraw_cb ) { ui.default_cursor(); ui.redraw_cb( ui ); diff --git a/src/ui_manager.h b/src/ui_manager.h index 2c26fac57c240..48a1d2110c868 100644 --- a/src/ui_manager.h +++ b/src/ui_manager.h @@ -75,6 +75,7 @@ class ui_adaptor { public: bool is_imgui; + bool is_on_top; using redraw_callback_t = std::function; using screen_resize_callback_t = std::function;