Skip to content

Commit

Permalink
Viewports: default to first monitor is viewport is outside bounds. (o…
Browse files Browse the repository at this point in the history
…cornut#8393, ocornut#8385)

Before the assert was introduced in d66f4e5 the viewport would be eventually clamped with ClampWindowPos using g.FallbackMonitor, but code would run temporarly with DpiScale=0.
  • Loading branch information
Gabriel Rodriguez authored and ocornut committed Feb 13, 2025
1 parent 71d39a4 commit 95c4111
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ Other changes:
- Backends: WebGPU: Fix for DAWN API rename WGPUProgrammableStageDescriptor -> WGPUComputeState.
[@PhantomCloak] (#8369)

Docking+Viewports Branch:

- Viewports: fixed an assert when a window load settings with a position outside
monitor bounds, when there are multiple monitors. (#8393, #8385) [@gaborodriguez]


-----------------------------------------------------------------------
VERSION 1.91.8 (Released 2025-01-31)
Expand Down
5 changes: 3 additions & 2 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16536,13 +16536,14 @@ static int ImGui::FindPlatformMonitorForRect(const ImRect& rect)
ImGuiContext& g = *GImGui;

const int monitor_count = g.PlatformIO.Monitors.Size;
IM_ASSERT(monitor_count > 0);
if (monitor_count <= 1)
return monitor_count - 1;
return 0;

// Use a minimum threshold of 1.0f so a zero-sized rect won't false positive, and will still find the correct monitor given its position.
// This is necessary for tooltips which always resize down to zero at first.
const float surface_threshold = ImMax(rect.GetWidth() * rect.GetHeight() * 0.5f, 1.0f);
int best_monitor_n = -1;
int best_monitor_n = 0; // Default to the first monitor as fallback
float best_monitor_surface = 0.001f;

for (int monitor_n = 0; monitor_n < g.PlatformIO.Monitors.Size && best_monitor_surface < surface_threshold; monitor_n++)
Expand Down

0 comments on commit 95c4111

Please sign in to comment.