Skip to content

Commit

Permalink
Viewports: fixed an issue where a window manually constrained to the …
Browse files Browse the repository at this point in the history
…main viewport while crossing over main viewport bounds isn't translated properly. (#7985)

Amend 967073b
  • Loading branch information
ocornut committed Sep 16, 2024
1 parent 8326dab commit 8040c02
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Other changes:

Docking+Viewports Branch:

- Viewports: fixed an issue where a window manually constrained to the main viewport while crossing
over main viewport bounds isn't translated properly. (#7985)
- Backends: SDL3: added support for viewport->ParentViewportId field to support parenting
windows at OS level. (#7973) [@RT2code]

Expand Down
9 changes: 5 additions & 4 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15224,7 +15224,7 @@ static bool ImGui::UpdateTryMergeWindowIntoHostViewports(ImGuiWindow* window)

// Translate Dear 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)
void ImGui::TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos, const ImVec2& old_size, const ImVec2& new_size)
{
ImGuiContext& g = *GImGui;
IM_ASSERT(viewport->Window == NULL && (viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows));
Expand All @@ -15238,7 +15238,7 @@ void ImGui::TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& o
ImRect test_still_fit_rect(old_pos, old_pos + viewport->Size);
ImVec2 delta_pos = new_pos - old_pos;
for (ImGuiWindow* window : g.Windows) // FIXME-OPT
if (translate_all_windows || (window->Viewport == viewport && test_still_fit_rect.Contains(window->Rect())))
if (translate_all_windows || (window->Viewport == viewport && (old_size == new_size || test_still_fit_rect.Contains(window->Rect()))))
TranslateWindow(window, delta_pos);
}

Expand Down Expand Up @@ -15415,7 +15415,7 @@ static void ImGui::UpdateViewportsNewFrame()
// (This additionally keeps windows at the same place when ImGuiConfigFlags_ViewportsEnable is toggled!)
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);
TranslateWindowsInViewport(viewport, viewport->LastPos, viewport->Pos, viewport->LastSize, viewport->Size);

// Update DPI scale
float new_dpi_scale;
Expand Down Expand Up @@ -15525,6 +15525,7 @@ static void ImGui::UpdateViewportsEndFrame()
{
ImGuiViewportP* viewport = g.Viewports[i];
viewport->LastPos = viewport->Pos;
viewport->LastSize = viewport->Size;
if (viewport->LastFrameActive < g.FrameCount || viewport->Size.x <= 0.0f || viewport->Size.y <= 0.0f)
if (i > 0) // Always include main viewport in the list
continue;
Expand Down Expand Up @@ -15571,7 +15572,7 @@ ImGuiViewportP* ImGui::AddUpdateViewport(ImGuiWindow* window, ImGuiID id, const
viewport->ID = id;
viewport->Idx = g.Viewports.Size;
viewport->Pos = viewport->LastPos = pos;
viewport->Size = size;
viewport->Size = viewport->LastSize = size;
viewport->Flags = flags;
UpdateViewportPlatformMonitor(viewport);
g.Viewports.push_back(viewport);
Expand Down
3 changes: 2 additions & 1 deletion imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,7 @@ struct ImGuiViewportP : public ImGuiViewport
int LastFocusedStampCount; // Last stamp number from when a window hosted by this viewport was focused (by comparing this value between two viewport we have an implicit viewport z-order we use as fallback)
ImGuiID LastNameHash;
ImVec2 LastPos;
ImVec2 LastSize;
float Alpha; // Window opacity (when dragging dockable windows/viewports we make them transparent)
float LastAlpha;
bool LastFocusedHadNavWindow;// Instead of maintaining a LastFocusedWindow (which may harder to correctly maintain), we merely store weither NavWindow != NULL last time the viewport was focused.
Expand Down Expand Up @@ -3411,7 +3412,7 @@ namespace ImGui
IMGUI_API void CallContextHooks(ImGuiContext* context, ImGuiContextHookType type);

// Viewports
IMGUI_API void TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos);
IMGUI_API void TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos, const ImVec2& old_size, const ImVec2& new_size);
IMGUI_API void ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale);
IMGUI_API void DestroyPlatformWindow(ImGuiViewportP* viewport);
IMGUI_API void SetWindowViewport(ImGuiWindow* window, ImGuiViewportP* viewport);
Expand Down

0 comments on commit 8040c02

Please sign in to comment.