viewport: fixing stutter when dragging a window out of the main viewport on Linux #9594

Open
opened 2026-10-24 17:05:43 +00:00 by hinxx · 0 comments
hinxx commented 2026-10-24 17:05:43 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

1.93.1 WIP, branch docking

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp (GLFW 3.3.10)

Compiler, OS:

g++ 13.3, Linux Mint 22.3 (kernel 6.8), X11, Xfce 4.18 / xfwm4 with compositing, Mesa Intel HD 630, single 1920x1080 60 Hz monitor

Full config/build information:

Dear ImGui 1.93.1 WIP (19301)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201103
define: __linux__
define: __GNUC__=13
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
IM_ASSERT: runs expression: OK. expand size: OK
--------------------------------
io.BackendPlatformName: imgui_impl_glfw (3310)
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000483
 NavEnableKeyboard
 NavEnableGamepad
 DockingEnable
 ViewportsEnable
io.ConfigDpiScaleFonts
io.ConfigDpiScaleViewports
io.ConfigViewportsNoDecoration
io.ConfigViewportsNoDefaultParent
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C1E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasTextures
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: stb_truetype
io.DisplaySize: 1280.00,800.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 3.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

When I drag ImGui window out of the main viewport it jiggles and stutters.
Drag "Hello, world!" window out of the main viewport and back, then drag it around the desktop for
10–20 seconds. It will lag the cursor by a varying amount. Combine with left click releases when inside the viewport.

Seems to be related to #2117.

Disclaimer: Code work done by Claude and reviewed by me. I intentionally did not open PR due to LLM / AI contribution policy. Nevertheless I wanted to share this with others and upstream authors can take the idea and implement the fix on their own if they like.

Code with changes: https://github.com/hinxx/imgui/tree/viewport-drag-latency

Two issues were observed:

  1. One slow frame when draggin ImGui window out of main viewport

Today UpdatePlatformWindows() creates the platform window at the moment the window no longer fits the
host viewport; in the middle of the draging it out. At 60Hz vsync I get a frame that takes about ~100 ms and it is noticable as a glitch in movement.

  1. window position is updated once per frame, so it wobbles when dragged

After trying out several things like turning the xfce4 compositor off, which did help, but not completely
the issue iss that ImGui samples the mouse once per frame (for me 16 ms with 60hz refresh rate). This makes ImGui position and WM position out of sync and results in wobbles or jitter in window posisiton.
Brute force solution is not using vsync (refresh at > 300 frames/s) but then CPU cycles are wasted.

Three changes that fix the above:

  1. have a pool of hidden platform windows instead of destroying them. this is created at init and reduces first slow frame time when draggin window out by factotr of ~2.
  2. io.ConfigViewportsOwnViewportOnMove (new, default off): set own viewport when the drag starts and not when the window starts leaving the viewport.
  3. "Late mouse pos latching" in app main loop does extra mouse poll just before the next vblank at high rate (~500 time a asec).

With 2+3 the stutter is gone for me as far as I was able to test.

Questions

Is having a window pool acceptable?
Is a core flag like ConfigViewportsOwnViewportOnMove OK?
Where would late mouse pos latching belong (maybe it is apps bussiness and not a concern for imgui)?

Screenshots/Video:

Before fix the wobble is noticeable as is the glitch when window leaves the main viewport: IMGUI_GLFW_WINDOW_POOL=0 ./example_glfw_opengl3:

https://github.com/user-attachments/assets/c71dad07-f7d4-4e09-822e-f25988b26b6f

Fixed: IMGUI_OWNVP=1 IMGUI_LATELATCH=500 ./example_glfw_opengl3

https://github.com/user-attachments/assets/e780645b-f4b7-487a-ac01-b028ab3d83f1

It might a bit hard to tell what the improvement is on the video, but one telling sign is the mouse pointer being glued to the window titlebar on move for the fixed vs. the wobbly where it visibly looks "detached".

Minimal, Complete and Verifiable Example code:

The code fixes in my branch can be selectively enabled or disabled to observe the improvement.

Observed behavior before fix (stock code) with lots of debug for analysis with scripts/llm:

IMGUI_DIAG=1 IMGUI_GLFW_WINDOW_POOL=0 ./example_glfw_opengl3

Fixes applied, no more stutter on move out and not wobble outside the main viewport:

IMGUI_OWNVP=1 IMGUI_LATELATCH=500 ./example_glfw_opengl3

See the code for some other env variables that you play arond with, but these 2 IMGUI_OWNVP=1 IMGUI_LATELATCH=500 do th etrick for me..
See misc/debug/viewport_drag/ for scripts that were used for analysis (need xdotool) and some notes on automated tests done by Claude. I tested the behavior manually to confirm that the wobble is gone.

### Version/Branch of Dear ImGui: 1.93.1 WIP, branch `docking` ### Back-ends: imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp (GLFW 3.3.10) ### Compiler, OS: g++ 13.3, Linux Mint 22.3 (kernel 6.8), X11, Xfce 4.18 / xfwm4 with compositing, Mesa Intel HD 630, single 1920x1080 60 Hz monitor ### Full config/build information: ```cpp Dear ImGui 1.93.1 WIP (19301) -------------------------------- sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20 define: __cplusplus=201103 define: __linux__ define: __GNUC__=13 define: IMGUI_HAS_VIEWPORT define: IMGUI_HAS_DOCK IM_ASSERT: runs expression: OK. expand size: OK -------------------------------- io.BackendPlatformName: imgui_impl_glfw (3310) io.BackendRendererName: imgui_impl_opengl3 io.ConfigFlags: 0x00000483 NavEnableKeyboard NavEnableGamepad DockingEnable ViewportsEnable io.ConfigDpiScaleFonts io.ConfigDpiScaleViewports io.ConfigViewportsNoDecoration io.ConfigViewportsNoDefaultParent io.ConfigNavCaptureKeyboard io.ConfigInputTextCursorBlink io.ConfigWindowsResizeFromEdges io.ConfigMemoryCompactTimer = 60.0 io.BackendFlags: 0x00001C1E HasMouseCursors HasSetMousePos PlatformHasViewports HasMouseHoveredViewport RendererHasVtxOffset RendererHasTextures RendererHasViewports -------------------------------- io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128 io.Fonts->FontLoaderName: stb_truetype io.DisplaySize: 1280.00,800.00 io.DisplayFramebufferScale: 1.00,1.00 -------------------------------- style.WindowPadding: 8.00,8.00 style.WindowBorderSize: 1.00 style.FramePadding: 4.00,3.00 style.FrameRounding: 3.00 style.FrameBorderSize: 0.00 style.ItemSpacing: 8.00,4.00 style.ItemInnerSpacing: 4.00,4.00 ``` ### Details: When I drag ImGui window out of the main viewport it jiggles and stutters. Drag "Hello, world!" window out of the main viewport and back, then drag it around the desktop for 10–20 seconds. It will lag the cursor by a varying amount. Combine with left click releases when inside the viewport. Seems to be related to #2117. **Disclaimer**: Code work done by Claude and reviewed by me. I intentionally did not open PR due to LLM / AI contribution policy. Nevertheless I wanted to share this with others and upstream authors can take the idea and implement the fix on their own if they like. Code with changes: https://github.com/hinxx/imgui/tree/viewport-drag-latency Two issues were observed: 1. One slow frame when draggin ImGui window out of main viewport Today `UpdatePlatformWindows()` creates the platform window at the moment the window no longer fits the host viewport; in the middle of the draging it out. At 60Hz vsync I get a frame that takes about ~100 ms and it is noticable as a glitch in movement. 2. window position is updated once per frame, so it wobbles when dragged After trying out several things like turning the xfce4 compositor off, which did help, but not completely the issue iss that ImGui samples the mouse once per frame (for me 16 ms with 60hz refresh rate). This makes ImGui position and WM position out of sync and results in wobbles or jitter in window posisiton. Brute force solution is not using vsync (refresh at > 300 frames/s) but then CPU cycles are wasted. Three changes that fix the above: 1. have a pool of hidden platform windows instead of destroying them. this is created at init and reduces first slow frame time when draggin window out by factotr of ~2. 2. `io.ConfigViewportsOwnViewportOnMove` (new, default off): set own viewport when the drag starts and not when the window starts leaving the viewport. 3. "Late mouse pos latching" in app main loop does extra mouse poll just before the next vblank at high rate (~500 time a asec). With 2+3 the stutter is gone for me as far as I was able to test. Questions Is having a window pool acceptable? Is a core flag like `ConfigViewportsOwnViewportOnMove` OK? Where would late mouse pos latching belong (maybe it is apps bussiness and not a concern for imgui)? ### Screenshots/Video: Before fix the wobble is noticeable as is the glitch when window leaves the main viewport: `IMGUI_GLFW_WINDOW_POOL=0 ./example_glfw_opengl3`: https://github.com/user-attachments/assets/c71dad07-f7d4-4e09-822e-f25988b26b6f Fixed: `IMGUI_OWNVP=1 IMGUI_LATELATCH=500 ./example_glfw_opengl3` https://github.com/user-attachments/assets/e780645b-f4b7-487a-ac01-b028ab3d83f1 It might a bit hard to tell what the improvement is on the video, but one telling sign is the mouse pointer being glued to the window titlebar on move for the fixed vs. the wobbly where it visibly looks "detached". ### Minimal, Complete and Verifiable Example code: The code fixes in my branch can be selectively enabled or disabled to observe the improvement. Observed behavior before fix (stock code) with lots of debug for analysis with scripts/llm: ``` IMGUI_DIAG=1 IMGUI_GLFW_WINDOW_POOL=0 ./example_glfw_opengl3 ``` Fixes applied, no more stutter on move out and not wobble outside the main viewport: ``` IMGUI_OWNVP=1 IMGUI_LATELATCH=500 ./example_glfw_opengl3 ``` See the code for some other env variables that you play arond with, but these 2 `IMGUI_OWNVP=1 IMGUI_LATELATCH=500` do th etrick for me.. See `misc/debug/viewport_drag/` for scripts that were used for analysis (need `xdotool`) and some notes on automated tests done by Claude. I tested the behavior manually to confirm that the wobble is gone.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#9594
No description provided.