1
0
mirror of https://github.com/Drezil/imgui.git synced 2024-12-25 09:06:35 +00:00

Viewports, Internals: added GetViewportPlatformMonitor() will a safety net to keep code portable + simplified handling of disconnected monitor in Begin().

This commit is contained in:
ocornut 2021-03-10 17:01:00 +01:00
parent d5a4d53000
commit 4b9bc49020
2 changed files with 35 additions and 19 deletions

View File

@ -6449,21 +6449,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
} }
else if (window->ViewportOwned && g.PlatformIO.Monitors.Size > 0) else if (window->ViewportOwned && g.PlatformIO.Monitors.Size > 0)
{ {
if (window->Viewport->PlatformMonitor == -1) // Lost windows (e.g. a monitor disconnected) will naturally moved to the fallback/dummy monitor aka the main viewport.
{ const ImGuiPlatformMonitor* monitor = GetViewportPlatformMonitor(window->Viewport);
// Fallback for "lost" window (e.g. a monitor disconnected): we move the window back over the main viewport visibility_rect.Min = monitor->WorkPos + visibility_padding;
const ImGuiViewport* main_viewport = GetMainViewport(); visibility_rect.Max = monitor->WorkPos + monitor->WorkSize - visibility_padding;
SetWindowPos(window, main_viewport->Pos + style.DisplayWindowPadding, ImGuiCond_Always);
}
else
{
ImGuiPlatformMonitor& monitor = g.PlatformIO.Monitors[window->Viewport->PlatformMonitor];
visibility_rect.Min = monitor.WorkPos + visibility_padding;
visibility_rect.Max = monitor.WorkPos + monitor.WorkSize - visibility_padding;
ClampWindowRect(window, visibility_rect); ClampWindowRect(window, visibility_rect);
} }
} }
}
window->Pos = ImFloor(window->Pos); window->Pos = ImFloor(window->Pos);
// Lock window rounding for the frame (so that altering them doesn't cause inconsistencies) // Lock window rounding for the frame (so that altering them doesn't cause inconsistencies)
@ -11534,6 +11526,17 @@ static void ImGui::UpdateViewportsNewFrame()
viewport->DpiScale = new_dpi_scale; viewport->DpiScale = new_dpi_scale;
} }
// Update fallback monitor
if (g.PlatformIO.Monitors.Size == 0)
{
ImGuiPlatformMonitor* monitor = &g.FallbackMonitor;
monitor->MainPos = main_viewport->Pos;
monitor->MainSize = main_viewport->Size;
monitor->WorkPos = main_viewport->WorkPos;
monitor->WorkSize = main_viewport->WorkSize;
monitor->DpiScale = main_viewport->DpiScale;
}
if (!viewports_enabled) if (!viewports_enabled)
{ {
g.MouseViewport = main_viewport; g.MouseViewport = main_viewport;
@ -12010,6 +12013,17 @@ static void ImGui::UpdateViewportPlatformMonitor(ImGuiViewportP* viewport)
viewport->PlatformMonitor = (short)FindPlatformMonitorForRect(viewport->GetMainRect()); viewport->PlatformMonitor = (short)FindPlatformMonitorForRect(viewport->GetMainRect());
} }
// Return value is always != NULL, but don't hold on it across frames.
const ImGuiPlatformMonitor* ImGui::GetViewportPlatformMonitor(ImGuiViewport* viewport_p)
{
ImGuiContext& g = *GImGui;
ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)viewport_p;
int monitor_idx = viewport->PlatformMonitor;
if (monitor_idx >= 0 || monitor_idx < g.PlatformIO.Monitors.Size)
return &g.PlatformIO.Monitors[monitor_idx];
return &g.FallbackMonitor;
}
void ImGui::DestroyPlatformWindow(ImGuiViewportP* viewport) void ImGui::DestroyPlatformWindow(ImGuiViewportP* viewport)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;

View File

@ -1542,6 +1542,7 @@ struct ImGuiContext
ImGuiViewportP* MouseViewport; ImGuiViewportP* MouseViewport;
ImGuiViewportP* MouseLastHoveredViewport; // Last known viewport that was hovered by mouse (even if we are not hovering any viewport any more) + honoring the _NoInputs flag. ImGuiViewportP* MouseLastHoveredViewport; // Last known viewport that was hovered by mouse (even if we are not hovering any viewport any more) + honoring the _NoInputs flag.
ImGuiID PlatformLastFocusedViewportId; ImGuiID PlatformLastFocusedViewportId;
ImGuiPlatformMonitor FallbackMonitor; // Virtual monitor used as fallback if backend doesn't provide monitor information.
int ViewportFrontMostStampCount; // Every time the front-most window changes, we stamp its viewport with an incrementing counter int ViewportFrontMostStampCount; // Every time the front-most window changes, we stamp its viewport with an incrementing counter
// Gamepad/keyboard Navigation // Gamepad/keyboard Navigation
@ -2455,6 +2456,7 @@ namespace ImGui
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);
IMGUI_API void ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale); IMGUI_API void ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale);
IMGUI_API void DestroyPlatformWindow(ImGuiViewportP* viewport); IMGUI_API void DestroyPlatformWindow(ImGuiViewportP* viewport);
const ImGuiPlatformMonitor* GetViewportPlatformMonitor(ImGuiViewport* viewport);
// Settings // Settings
IMGUI_API void MarkIniSettingsDirty(); IMGUI_API void MarkIniSettingsDirty();