mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-16 19:11:13 +00:00
How to center a window? (was: How do I set a window position that relies on knowing window attributes without weird initial jittering?) #5618
Labels
No labels
accidentally closed
android
backends
bug
building
clipper
coloredit
color spaces
cplusplus
data types
demo
disabled
doc
docking
dpi
drag drop
drawing/drawlist
dropped
dx12
enhancement
focus
font/text
gallery
help wanted
idle/power saving
ime
in progress
inputs
inputtext
internals
label/id and id stack
language bindings
layout
linux/bsd
menus
multi-contexts
multi-threading
multi-viewports
nav
news/info
opengl
optimization
osx/ios
overlap
plot/graph
popups
question
scrolling
selection
settings
sliders/drags
style
tables/columns
tabs
test wanted
tools
tooltips
tree
tricks & tips
useful widgets
vulkan
web
window pos/depth
window sizing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ocornut/imgui#5618
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
My Issue/Question:
With SetWindowPos, centering horizontally is easy:
ImGui::SetWindowPos(ImVec2(io.DisplaySize.x / 2 - ImGui::GetWindowWidth() / 2, 0));But that introduces initial frames where the window is a different shape or position and it snaps into place.
SetNextWindow position solves this, but then you can't center the window or really move it into any position that relies on knowing its width or height, because you don't know those attributes yet. How do I solve this?
Screenshots/Video
https://streamable.com/4oride
The initial popup doesn't have the artifacting issue, because I hardcoded SetNextWindowPos to look as centered as possible, which you can see below.
Example:
In this situation you can use the pivot argment of SetNextWindowPos(). Set it to (0.5f, 0.5f) and so you can pass the central point. When the position is resolved on the appearing frame it’ll have access to size and can center the window without a hitch.
Closing this as answered.
See Demo e.g.
I guess this is not explicitly documented as a demo scenario.
What's the importance of the ImGuiCond_Appearing condition? I've looked at the ImGuiCond_ enum definition and it doesn't help. When I use this example to centre a window, it does behave as I expect unless the condition is set to None or Always (I realize they're the same).
I suspect I don't understand what the comments mean when they refer to "the variable".
Normally it should be sufficient to position during the frames the window is Appearing, as on the second frame the content size should be known. If Appearing doesn't work for you because the window keeps resizing for more than one frame then using no condition is fine.
I think I understand that. Just to be clear, mine wasn't a resizing problem but a position problem. I'm trying to understand the various coordinate systems (the screen, the window, the child window, and the viewport) so that I can reliably place child windows programmatically, independent of the main window and screen location. In my case, the child window position did not change when I tried to centre it. Only changing the condition to Always permitted the move.
My current investigative goal is to prevent child windows from appearing outside the main window, which can occur on app start, since Windows is determining the initial position and size of the main window.
I cannot find a description of SetNextWindow's 3rd parameter, pivot. I note that it is simply passed to NextWindowData.PosPivotVal. Looking through that struct's definition doesn't provide any clarity and I can't find any other reference to PosPivotVal that might give me a clue.
Not understanding the coordinate systems, makes trial and error testing a waste of time. I am trying to use the demo's tools to display the window and viewport coordinates.