Backends: SDL2/SDL3: Update monitor list when receiving a display event. (#6348)

This commit is contained in:
ocornut
2023-04-21 15:40:14 +02:00
parent adf693c061
commit c11128891e
3 changed files with 40 additions and 8 deletions

View File

@@ -64,6 +64,7 @@ struct ImGui_ImplSDL3_Data
bool MouseCanUseGlobalState;
bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state.
bool UseVulkan;
bool WantUpdateMonitors;
ImGui_ImplSDL3_Data() { memset((void*)this, 0, sizeof(*this)); }
};
@@ -300,6 +301,15 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
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.
return true;
}
case SDL_EVENT_DISPLAY_ORIENTATION:
case SDL_EVENT_DISPLAY_CONNECTED:
case SDL_EVENT_DISPLAY_DISCONNECTED:
case SDL_EVENT_DISPLAY_MOVED:
case SDL_EVENT_DISPLAY_SCALE_CHANGED:
{
bd->WantUpdateMonitors = true;
return true;
}
case SDL_EVENT_WINDOW_MOUSE_ENTER:
{
bd->MouseWindowID = event->window.windowID;
@@ -375,6 +385,7 @@ static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void
#else
bd->MouseCanReportHoveredViewport = false;
#endif
bd->WantUpdateMonitors = true;
io.SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText;
io.GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText;
@@ -420,9 +431,6 @@ static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void
// SDL 3.x : see https://github.com/libsdl-org/SDL/issues/6659
SDL_SetHint("SDL_BORDERLESS_WINDOWED_STYLE", "0");
// Update monitors
ImGui_ImplSDL3_UpdateMonitors();
// We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports.
// We left the call to ImGui_ImplSDL3_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings.
if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) && (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports))
@@ -619,11 +627,12 @@ static void ImGui_ImplSDL3_UpdateGamepads()
#undef MAP_ANALOG
}
// FIXME-PLATFORM: SDL doesn't have an event to notify the application of display/monitor changes
static void ImGui_ImplSDL3_UpdateMonitors()
{
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
platform_io.Monitors.resize(0);
bd->WantUpdateMonitors = false;
int display_count;
SDL_DisplayID* displays = SDL_GetDisplays(&display_count);
@@ -665,6 +674,10 @@ void ImGui_ImplSDL3_NewFrame()
if (w > 0 && h > 0)
io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
// Update monitors
if (bd->WantUpdateMonitors)
ImGui_ImplSDL3_UpdateMonitors();
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
static Uint64 frequency = SDL_GetPerformanceFrequency();