Skip to content

Commit

Permalink
Viewport: Better support for toggling ImGuiConfigFlags_ViewportsEnabl…
Browse files Browse the repository at this point in the history
…e. (#2196)
  • Loading branch information
ocornut committed Nov 27, 2018
1 parent 1a0d257 commit c08b4b4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
33 changes: 17 additions & 16 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3000,7 +3000,7 @@ void ImGui::UpdateMouseMovingWindow()
{
// Try to merge the window back into the main viewport.
// This works because MouseViewport should be != MovingWindow->Viewport on release (as per code in UpdateViewports)
if (g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
if (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)
UpdateTryMergeWindowIntoHostViewport(moving_window, g.MouseViewport);

// Restore the mouse viewport so that we don't hover the viewport _under_ the moved window during the frame we released the mouse button.
Expand Down Expand Up @@ -3303,6 +3303,7 @@ void ImGui::NewFrame()
g.FrameCount += 1;
g.TooltipOverrideCount = 0;
g.WindowsActiveCount = 0;
g.ConfigFlagsForFrame = g.IO.ConfigFlags;

UpdateViewports();

Expand Down Expand Up @@ -3778,6 +3779,7 @@ void ImGui::EndFrame()
for (int i = 0; i < g.Viewports.Size; i++)
{
ImGuiViewportP* viewport = g.Viewports[i];
viewport->LastPos = viewport->Pos;
if (viewport->LastFrameActive < g.FrameCount || viewport->Size.x <= 0.0f || viewport->Size.y <= 0.0f)
continue;
if (viewport->Window && !IsWindowActiveAndVisible(viewport->Window))
Expand Down Expand Up @@ -7175,7 +7177,7 @@ static bool ImGui::GetWindowAlwaysWantOwnViewport(ImGuiWindow* window)
{
// Tooltips and menus are not automatically forced into their own viewport when the NoMerge flag is set, however the multiplication of viewports makes them more likely to protude and create their own.
ImGuiContext& g = *GImGui;
if ((g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsNoMerge) && (g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
if ((g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsNoMerge) && (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
//if (window->DockStatus == ImGuiDockStatus_Floating)
if ((window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_Tooltip)) == 0)
return true;
Expand Down Expand Up @@ -7257,7 +7259,7 @@ static void ImGui::UpdateViewports()
ImGuiViewportP* main_viewport = g.Viewports[0];
IM_ASSERT(main_viewport->ID == IMGUI_VIEWPORT_DEFAULT_ID);
ImVec2 main_viewport_platform_pos = ImVec2(0.0f, 0.0f);
if ((g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
if ((g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
main_viewport_platform_pos = g.PlatformIO.Platform_GetWindowPos(main_viewport);
AddUpdateViewport(NULL, IMGUI_VIEWPORT_DEFAULT_ID, main_viewport_platform_pos, g.IO.DisplaySize, ImGuiViewportFlags_CanHostOtherWindows);

Expand Down Expand Up @@ -7290,7 +7292,7 @@ static void ImGui::UpdateViewports()
continue;
}

if ((g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
if ((g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
{
if (g.PlatformIO.Platform_GetWindowMinimized && (n == 0 || viewport->CreatedPlatformWindow))
viewport->PlatformIsMinimized = g.PlatformIO.Platform_GetWindowMinimized(viewport);
Expand All @@ -7305,17 +7307,18 @@ static void ImGui::UpdateViewports()
viewport->Size = viewport->LastPlatformSize = g.PlatformIO.Platform_GetWindowSize(viewport);
}

// Translate imgui windows when a Host Viewport has been moved
ImVec2 delta = viewport->Pos - viewport->LastPos;
if ((viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows) && (delta.x != 0.0f || delta.y != 0.0f))
for (int window_n = 0; window_n < g.Windows.Size; window_n++)
if (g.Windows[window_n]->Viewport == viewport)
TranslateWindow(g.Windows[window_n], delta);

// Update monitor (we'll use this info to clamp windows and save windows lost in a removed monitor)
viewport->PlatformMonitor = FindPlatformMonitorForRect(viewport->GetRect());
}

// Translate imgui windows when a Host Viewport has been moved
// (This additionally keeps windows at the same place when ImGuiConfigFlags_ViewportsEnable is toggled!)
ImVec2 viewport_delta = viewport->Pos - viewport->LastPos;
if ((viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows) && (viewport_delta.x != 0.0f || viewport_delta.y != 0.0f))
for (int window_n = 0; window_n < g.Windows.Size; window_n++)
if (g.Windows[window_n]->Viewport == viewport || (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable) == 0)
TranslateWindow(g.Windows[window_n], viewport_delta);

// Update DPI scale
float new_dpi_scale;
if (g.PlatformIO.Platform_GetWindowDpiScale)
Expand All @@ -7339,7 +7342,7 @@ static void ImGui::UpdateViewports()
viewport->DpiScale = new_dpi_scale;
}

if (!(g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
{
g.MouseViewport = main_viewport;
return;
Expand Down Expand Up @@ -7455,7 +7458,7 @@ static void ImGui::UpdateSelectWindowViewport(ImGuiWindow* window)

// Restore main viewport if multi-viewport is not supported by the back-end
ImGuiViewportP* main_viewport = g.Viewports[0];
if (!(g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
{
SetWindowViewport(window, main_viewport);
return;
Expand Down Expand Up @@ -7559,16 +7562,14 @@ void ImGui::UpdatePlatformWindows()
IM_ASSERT(g.FrameCountEnded == g.FrameCount && "Forgot to call Render() or EndFrame() before UpdatePlatformWindows()?");
IM_ASSERT(g.FrameCountPlatformEnded < g.FrameCount);
g.FrameCountPlatformEnded = g.FrameCount;
g.Viewports[0]->LastPos = g.Viewports[0]->Pos;
if (!(g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
return;

// Create/resize/destroy platform windows to match each active viewport.
// Skip the main viewport (index 0), which is always fully handled by the application!
for (int i = 1; i < g.Viewports.Size; i++)
{
ImGuiViewportP* viewport = g.Viewports[i];
viewport->LastPos = viewport->Pos;

// Destroy platform window if the viewport hasn't been submitted or if it is hosting a hidden window (the implicit Debug window will be registered its viewport then be disabled)
bool destroy_platform_window = false;
Expand Down
1 change: 0 additions & 1 deletion imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ void ImGui::ShowDemoWindow(bool* p_open)
ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange);
ImGui::SameLine(); ShowHelpMarker("Instruct back-end to not alter mouse cursor shape and visibility.");
ImGui::CheckboxFlags("io.ConfigFlags: ViewportsEnable", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_ViewportsEnable);
ImGui::SameLine(); ShowHelpMarker("Toggling this at runtime is normally unsupported (it will offset your windows).");
ImGui::CheckboxFlags("io.ConfigFlags: ViewportsNoTaskBarIcon", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_ViewportsNoTaskBarIcon);
ImGui::SameLine(); ShowHelpMarker("Toggling this at runtime is normally unsupported (most platform back-ends won't refresh the task bar icon state right away).");
ImGui::CheckboxFlags("io.ConfigFlags: ViewportsDecoration", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_ViewportsDecoration);
Expand Down
2 changes: 2 additions & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ struct ImGuiContext
ImGuiIO IO;
ImGuiPlatformIO PlatformIO;
ImGuiStyle Style;
ImGuiConfigFlags ConfigFlagsForFrame; // = g.IO.ConfigFlags at the time of NewFrame()
ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back()
float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window.
float FontBaseSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Base text height.
Expand Down Expand Up @@ -876,6 +877,7 @@ struct ImGuiContext
{
Initialized = false;
FrameScopeActive = false;
ConfigFlagsForFrame = ImGuiConfigFlags_None;
Font = NULL;
FontSize = FontBaseSize = 0.0f;
FontAtlasOwnedByContext = shared_font_atlas ? false : true;
Expand Down

0 comments on commit c08b4b4

Please sign in to comment.