Skip to content

Commit 1a7b594

Browse files
committed
Backends: GLFW/SDL2/SDL3: Update monitors and work areas information every frame, as the later may change regardless of monitor changes. (ocornut#8415)
1 parent ea59440 commit 1a7b594

File tree

4 files changed

+9
-36
lines changed

4 files changed

+9
-36
lines changed

backends/imgui_impl_glfw.cpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
// CHANGELOG
3131
// (minor and older changes stripped away, please see git history for details)
3232
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
33+
// 2025-02-21: [Docking] Update monitors and work areas information every frame, as the later may change regardless of monitor changes. (#8415)
3334
// 2024-11-05: [Docking] Added Linux workaround for spurious mouse up events emitted while dragging and creating new viewport. (#3158, #7733, #7922)
3435
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
3536
// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
@@ -180,7 +181,6 @@ struct ImGui_ImplGlfw_Data
180181
GLFWwindow* KeyOwnerWindows[GLFW_KEY_LAST];
181182
bool InstalledCallbacks;
182183
bool CallbacksChainForAllWindows;
183-
bool WantUpdateMonitors;
184184
#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
185185
const char* CanvasSelector;
186186
#endif
@@ -519,8 +519,7 @@ void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c)
519519

520520
void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int)
521521
{
522-
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
523-
bd->WantUpdateMonitors = true;
522+
// This function is technically part of the API even if we stopped using the callback, so leaving it around.
524523
}
525524

526525
#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
@@ -627,7 +626,6 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw
627626

628627
bd->Window = window;
629628
bd->Time = 0.0;
630-
bd->WantUpdateMonitors = true;
631629

632630
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
633631
platform_io.Platform_SetClipboardTextFn = [](ImGuiContext*, const char* text) { glfwSetClipboardString(nullptr, text); };
@@ -910,9 +908,7 @@ static void ImGui_ImplGlfw_UpdateGamepads()
910908

911909
static void ImGui_ImplGlfw_UpdateMonitors()
912910
{
913-
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
914911
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
915-
bd->WantUpdateMonitors = false;
916912

917913
int monitors_count = 0;
918914
GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count);
@@ -966,8 +962,7 @@ void ImGui_ImplGlfw_NewFrame()
966962
io.DisplaySize = ImVec2((float)w, (float)h);
967963
if (w > 0 && h > 0)
968964
io.DisplayFramebufferScale = ImVec2((float)display_w / (float)w, (float)display_h / (float)h);
969-
if (bd->WantUpdateMonitors)
970-
ImGui_ImplGlfw_UpdateMonitors();
965+
ImGui_ImplGlfw_UpdateMonitors();
971966

972967
// Setup time step
973968
// (Accept glfwGetTime() not returning a monotonically increasing value. Seems to happens on disconnecting peripherals and probably on VMs and Emscripten, see #6491, #6189, #6114, #3644)

backends/imgui_impl_sdl2.cpp

+2-14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
// CHANGELOG
2727
// (minor and older changes stripped away, please see git history for details)
2828
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
29+
// 2025-02-21: [Docking] Update monitors and work areas information every frame, as the later may change regardless of monitor changes. (#8415)
2930
// 2025-02-10: Using SDL_OpenURL() in platform_io.Platform_OpenInShellFn handler.
3031
// 2025-01-20: Made ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode_Manual) accept an empty array.
3132
// 2024-10-24: Emscripten: from SDL 2.30.9, SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f.
@@ -144,7 +145,6 @@ struct ImGui_ImplSDL2_Data
144145
Uint64 Time;
145146
char* ClipboardTextData;
146147
bool UseVulkan;
147-
bool WantUpdateMonitors;
148148

149149
// Mouse handling
150150
Uint32 MouseWindowID;
@@ -436,15 +436,6 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
436436
io.SetKeyEventNativeData(key, event->key.keysym.sym, event->key.keysym.scancode, event->key.keysym.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
437437
return true;
438438
}
439-
#if SDL_HAS_DISPLAY_EVENT
440-
case SDL_DISPLAYEVENT:
441-
{
442-
// 2.0.26 has SDL_DISPLAYEVENT_CONNECTED/SDL_DISPLAYEVENT_DISCONNECTED/SDL_DISPLAYEVENT_ORIENTATION,
443-
// so change of DPI/Scaling are not reflected in this event. (SDL3 has it)
444-
bd->WantUpdateMonitors = true;
445-
return true;
446-
}
447-
#endif
448439
case SDL_WINDOWEVENT:
449440
{
450441
ImGuiViewport* viewport = ImGui_ImplSDL2_GetViewportForWindowID(event->window.windowID);
@@ -862,10 +853,8 @@ static void ImGui_ImplSDL2_UpdateGamepads()
862853
// FIXME: Note that doesn't update with DPI/Scaling change only as SDL2 doesn't have an event for it (SDL3 has).
863854
static void ImGui_ImplSDL2_UpdateMonitors()
864855
{
865-
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
866856
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
867857
platform_io.Monitors.resize(0);
868-
bd->WantUpdateMonitors = false;
869858
int display_count = SDL_GetNumVideoDisplays();
870859
for (int n = 0; n < display_count; n++)
871860
{
@@ -921,8 +910,7 @@ void ImGui_ImplSDL2_NewFrame()
921910
io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
922911

923912
// Update monitors
924-
if (bd->WantUpdateMonitors)
925-
ImGui_ImplSDL2_UpdateMonitors();
913+
ImGui_ImplSDL2_UpdateMonitors();
926914

927915
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
928916
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)

backends/imgui_impl_sdl3.cpp

+2-14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// CHANGELOG
2525
// (minor and older changes stripped away, please see git history for details)
2626
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
27+
// 2025-02-21: [Docking] Update monitors and work areas information every frame, as the later may change regardless of monitor changes. (#8415)
2728
// 2025-02-10: Using SDL_OpenURL() in platform_io.Platform_OpenInShellFn handler.
2829
// 2025-01-20: Made ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode_Manual) accept an empty array.
2930
// 2024-10-24: Emscripten: SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f on Emscripten.
@@ -99,7 +100,6 @@ struct ImGui_ImplSDL3_Data
99100
Uint64 Time;
100101
char* ClipboardTextData;
101102
bool UseVulkan;
102-
bool WantUpdateMonitors;
103103

104104
// IME handling
105105
SDL_Window* ImeWindow;
@@ -400,15 +400,6 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
400400
io.SetKeyEventNativeData(key, event->key.key, event->key.scancode, event->key.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
401401
return true;
402402
}
403-
case SDL_EVENT_DISPLAY_ORIENTATION:
404-
case SDL_EVENT_DISPLAY_ADDED:
405-
case SDL_EVENT_DISPLAY_REMOVED:
406-
case SDL_EVENT_DISPLAY_MOVED:
407-
case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
408-
{
409-
bd->WantUpdateMonitors = true;
410-
return true;
411-
}
412403
case SDL_EVENT_WINDOW_MOUSE_ENTER:
413404
{
414405
if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
@@ -824,10 +815,8 @@ static void ImGui_ImplSDL3_UpdateGamepads()
824815

825816
static void ImGui_ImplSDL3_UpdateMonitors()
826817
{
827-
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
828818
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
829819
platform_io.Monitors.resize(0);
830-
bd->WantUpdateMonitors = false;
831820

832821
int display_count;
833822
SDL_DisplayID* displays = SDL_GetDisplays(&display_count);
@@ -872,8 +861,7 @@ void ImGui_ImplSDL3_NewFrame()
872861
io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
873862

874863
// Update monitors
875-
if (bd->WantUpdateMonitors)
876-
ImGui_ImplSDL3_UpdateMonitors();
864+
ImGui_ImplSDL3_UpdateMonitors();
877865

878866
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
879867
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)

docs/CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Docking+Viewports Branch:
9999

100100
- Viewports: fixed an assert when a window load settings with a position outside
101101
monitor bounds, when there are multiple monitors. (#8393, #8385) [@gaborodriguez]
102+
- Backends: GLFW, SDL2/SDL3: Update monitors and work areas information every frame,
103+
as the later may change regardless of monitor/display changes. (#8415)
102104
- Backends: Win32: WM_SETTINGCHANGE's SPI_SETWORKAREA message also triggers a refresh
103105
of monitor list, as they contain work area information. (#8415) [@PathogenDavid, @lailoken]
104106

0 commit comments

Comments
 (0)