Skip to content

Commit

Permalink
Viewport: Handle case where host window gets moved and resized simult…
Browse files Browse the repository at this point in the history
…aneous (toggling maximized state). There's no perfect solution there, than using io.ConfigViewportsNoAutoMerge = false. (#1542)
  • Loading branch information
ocornut committed Aug 1, 2019
1 parent b8d8355 commit 967073b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
52 changes: 35 additions & 17 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3472,7 +3472,7 @@ void ImGui::UpdateMouseMovingWindowNewFrame()
{
// 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.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)
if (g.ConfigFlagsCurrFrame & 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 @@ -3821,10 +3821,10 @@ void ImGui::NewFrame()
NewFrameSanityChecks();

// Perform simple check: error if Docking or Viewport are enabled _exactly_ on frame 1 (instead of frame 0 or later), which is a common error leading to loss of .ini data.
const ImGuiColumnsFlags prev_config_flags = g.ConfigFlagsForFrame;
if (g.FrameCount == 1 && (g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable) && (prev_config_flags & ImGuiConfigFlags_DockingEnable) == 0)
g.ConfigFlagsLastFrame = g.ConfigFlagsCurrFrame;
if (g.FrameCount == 1 && (g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable) && (g.ConfigFlagsLastFrame & ImGuiConfigFlags_DockingEnable) == 0)
IM_ASSERT(0 && "Please DockingEnable before the first call to NewFrame()! Otherwise you will lose your .ini settings!");
if (g.FrameCount == 1 && (g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) && (prev_config_flags & ImGuiConfigFlags_ViewportsEnable) == 0)
if (g.FrameCount == 1 && (g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) && (g.ConfigFlagsLastFrame & ImGuiConfigFlags_ViewportsEnable) == 0)
IM_ASSERT(0 && "Please ViewportEnable before the first call to NewFrame()! Otherwise you will lose your .ini settings!");

// Perform simple checks: multi-viewport and platform windows support
Expand Down Expand Up @@ -3863,6 +3863,7 @@ void ImGui::NewFrame()
IM_ASSERT(mon.DpiScale != 0.0f);
}
}
g.ConfigFlagsCurrFrame = g.IO.ConfigFlags;

// Load settings on first frame (if not explicitly loaded manually before)
if (!g.SettingsLoaded)
Expand Down Expand Up @@ -3892,7 +3893,6 @@ void ImGui::NewFrame()
g.FrameCount += 1;
g.TooltipOverrideCount = 0;
g.WindowsActiveCount = 0;
g.ConfigFlagsForFrame = g.IO.ConfigFlags;

UpdateViewportsNewFrame();

Expand Down Expand Up @@ -10317,7 +10317,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 protrude and create their own.
ImGuiContext& g = *GImGui;
if (g.IO.ConfigViewportsNoAutoMerge || (window->WindowClass.ViewportFlagsOverrideSet & ImGuiViewportFlags_NoAutoMerge))
if (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)
if (g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable)
if (!window->DockIsActive)
if ((window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) == 0)
return true;
Expand Down Expand Up @@ -10362,6 +10362,26 @@ static bool ImGui::UpdateTryMergeWindowIntoHostViewports(ImGuiWindow* window)
return UpdateTryMergeWindowIntoHostViewport(window, g.Viewports[0]);
}

// Translate imgui windows when a Host Viewport has been moved
// (This additionally keeps windows at the same place when ImGuiConfigFlags_ViewportsEnable is toggled!)
void ImGui::TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos)
{
ImGuiContext& g = *GImGui;
IM_ASSERT(viewport->Window == NULL && (viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows));

// 1) We test if ImGuiConfigFlags_ViewportsEnable was just toggled, which allows us to conveniently
// translate imgui windows from OS-window-local to absolute coordinates or vice-versa.
// 2) If it's not going to fit into the new size, keep it at same absolute position.
// One problem with this is that most Win32 applications doesn't update their render while dragging,
// and so the window will appear to teleport when releasing the mouse.
const bool translate_all_windows = (g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable) != (g.ConfigFlagsLastFrame & ImGuiConfigFlags_ViewportsEnable);
ImRect test_still_fit_rect(old_pos, old_pos + viewport->Size);
ImVec2 delta_pos = new_pos - old_pos;
for (int window_n = 0; window_n < g.Windows.Size; window_n++) // FIXME-OPT
if (translate_all_windows || (g.Windows[window_n]->Viewport == viewport && test_still_fit_rect.Contains(g.Windows[window_n]->Rect())))
TranslateWindow(g.Windows[window_n], delta_pos);
}

// Scale all windows (position, size). Use when e.g. changing DPI. (This is a lossy operation!)
void ImGui::ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale)
{
Expand Down Expand Up @@ -10403,7 +10423,7 @@ static void ImGui::UpdateViewportsNewFrame()
IM_ASSERT(g.PlatformIO.Viewports.Size <= g.Viewports.Size);

// Update Minimized status (we need it first in order to decide if we'll apply Pos/Size of the main viewport)
if ((g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
if ((g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable))
{
for (int n = 0; n < g.Viewports.Size; n++)
{
Expand All @@ -10426,7 +10446,7 @@ static void ImGui::UpdateViewportsNewFrame()
IM_ASSERT(main_viewport->Window == NULL);
ImVec2 main_viewport_platform_pos = ImVec2(0.0f, 0.0f);
ImVec2 main_viewport_platform_size = g.IO.DisplaySize;
if (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)
if (g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable)
main_viewport_platform_pos = (main_viewport->Flags & ImGuiViewportFlags_Minimized) ? main_viewport->Pos : g.PlatformIO.Platform_GetWindowPos(main_viewport);
AddUpdateViewport(NULL, IMGUI_VIEWPORT_DEFAULT_ID, main_viewport_platform_pos, main_viewport_platform_size, ImGuiViewportFlags_CanHostOtherWindows);

Expand Down Expand Up @@ -10461,7 +10481,7 @@ static void ImGui::UpdateViewportsNewFrame()
}

const bool platform_funcs_available = viewport->PlatformWindowCreated;
if ((g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
if ((g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable))
{
// Update Position and Size (from Platform Window to ImGui) if requested.
// We do it early in the frame instead of waiting for UpdatePlatformWindows() to avoid a frame of lag when moving/resizing using OS facilities.
Expand All @@ -10482,11 +10502,9 @@ static void ImGui::UpdateViewportsNewFrame()

// 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);
const ImVec2 viewport_delta_pos = viewport->Pos - viewport->LastPos;
if ((viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows) && (viewport_delta_pos.x != 0.0f || viewport_delta_pos.y != 0.0f))
TranslateWindowsInViewport(viewport, viewport->LastPos, viewport->Pos);

// Update DPI scale
float new_dpi_scale;
Expand All @@ -10513,7 +10531,7 @@ static void ImGui::UpdateViewportsNewFrame()
viewport->DpiScale = new_dpi_scale;
}

if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
if (!(g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable))
{
g.MouseViewport = main_viewport;
return;
Expand Down Expand Up @@ -10657,7 +10675,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.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
if (!(g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable))
{
SetWindowViewport(window, main_viewport);
return;
Expand Down Expand Up @@ -10783,7 +10801,7 @@ 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;
if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))
if (!(g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable))
return;

// Create/resize/destroy platform windows to match each active viewport.
Expand Down
6 changes: 4 additions & 2 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,8 @@ struct ImGuiContext
ImGuiIO IO;
ImGuiPlatformIO PlatformIO;
ImGuiStyle Style;
ImGuiConfigFlags ConfigFlagsForFrame; // = g.IO.ConfigFlags at the time of NewFrame()
ImGuiConfigFlags ConfigFlagsCurrFrame; // = g.IO.ConfigFlags at the time of NewFrame()
ImGuiConfigFlags ConfigFlagsLastFrame;
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 @@ -1209,7 +1210,7 @@ struct ImGuiContext
{
Initialized = false;
FrameScopeActive = FrameScopePushedFallbackWindow = false;
ConfigFlagsForFrame = ImGuiConfigFlags_None;
ConfigFlagsCurrFrame = ImGuiConfigFlags_None;
Font = NULL;
FontSize = FontBaseSize = 0.0f;
FontAtlasOwnedByContext = shared_font_atlas ? false : true;
Expand Down Expand Up @@ -1683,6 +1684,7 @@ namespace ImGui
IMGUI_API void UpdateMouseMovingWindowEndFrame();

// Viewports
IMGUI_API void TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos);
IMGUI_API void ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale);
IMGUI_API void DestroyPlatformWindow(ImGuiViewportP* viewport);
IMGUI_API void ShowViewportThumbnails();
Expand Down

0 comments on commit 967073b

Please sign in to comment.