@@ -51,7 +51,7 @@ void GLEngineGLFW::initialize() {
5151 glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
5252
5353 // This tells GLFW to scale window size/positioning/content based on the system-reported DPI scaling factor
54- // However, it can lead to some confusing behaviors, for instance, on linux with scaling=200%, if the user
54+ // However, it can lead to some confusing behaviors, for instance, on linux with scaling=200%, if the user
5555 // sets view::windowWidth = 1280, they might get back a window with windowWidth == bufferWidth == 2560,
5656 // which is quite confusing.
5757 // For this reason we _do not_ set this hint. If desired, the user can specify a windowWidth = 1280*uiScale,
@@ -80,14 +80,6 @@ void GLEngineGLFW::initialize() {
8080
8181 setWindowResizable (view::windowResizable);
8282
83- // Set the UI scale to account for system-requested DPI scaling
84- // Currently we do *not* watch for changes of this value e.g. if a window moves between
85- // monitors with different DPI behaviors. We could, but it would require some logic to
86- // avoid overwriting values that a user might have set.
87- if (options::uiScale < 0 ) { // only set from system if the value is -1, meaning not set yet
88- setUIScaleFromSystemDPI ();
89- }
90-
9183// === Initialize openGL
9284// Load openGL functions (using GLAD)
9385#ifndef __APPLE__
@@ -117,18 +109,39 @@ void GLEngineGLFW::initialize() {
117109 // glClearDepth(1.);
118110 }
119111
112+ // Set the UI scale to account for system-requested DPI scaling
113+ // Currently we do *not* watch for changes of this value e.g. if a window moves between
114+ // monitors with different DPI behaviors. We could, but it would require some logic to
115+ // avoid overwriting values that a user might have set.
116+ if (options::uiScale < 0 ) { // only set from system if the value is -1, meaning not set yet
117+ setUIScaleFromSystemDPI ();
118+ }
119+
120120 populateDefaultShadersAndRules ();
121121}
122122
123123void GLEngineGLFW::setUIScaleFromSystemDPI () {
124-
125- float xScale, dont_use_yScale;
126- glfwGetWindowContentScale (mainWindow, &xScale, &dont_use_yScale);
127124
128- // clamp to values within [1x,4x] scaling
129- xScale = std::fmin (std::fmax (xScale, 1 .0f ), 4 .0f );
125+ // logic adapted from a helpful imgui issue here: https://github.com/ocornut/imgui/issues/6967#issuecomment-2833882081
126+
127+ ImVec2 windowSize{static_cast <float >(view::windowWidth), static_cast <float >(view::windowHeight)};
128+ ImVec2 bufferSize{static_cast <float >(view::bufferWidth), static_cast <float >(view::bufferHeight)};
129+ ImVec2 imguiCoordScale = {bufferSize.x / windowSize.x , bufferSize.y / windowSize.y };
130130
131- options::uiScale = xScale;
131+ ImVec2 contentScale;
132+ glfwGetWindowContentScale (mainWindow, &contentScale.x , &contentScale.y );
133+
134+ float sx = contentScale.x / imguiCoordScale.x ;
135+ float sy = contentScale.y / imguiCoordScale.y ;
136+ options::uiScale = std::max (sx, sy);
137+ // clamp to values within [0.5x,4x] scaling
138+ options::uiScale = std::fmin (std::fmax (options::uiScale, 0 .5f ), 4 .0f );
139+
140+ info (100 , " window size: " + std::to_string (view::windowWidth) + " ," + std::to_string (view::windowHeight));
141+ info (100 , " buffer size: " + std::to_string (view::bufferWidth) + " ," + std::to_string (view::bufferHeight));
142+ info (100 , " imguiCoordScale: " + std::to_string (imguiCoordScale.x ) + " ," + std::to_string (imguiCoordScale.y ));
143+ info (100 , " contentScale: " + std::to_string (contentScale.x ) + " ," + std::to_string (contentScale.y ));
144+ info (100 , " computed uiScale: " + std::to_string (options::uiScale));
132145}
133146
134147void GLEngineGLFW::initializeImGui () {
@@ -146,7 +159,7 @@ void GLEngineGLFW::initializeImGui() {
146159
147160void GLEngineGLFW::configureImGui () {
148161
149- if (options::uiScale < 0 ){
162+ if (options::uiScale < 0 ) {
150163 exception (" uiScale is < 0. Perhaps it wasn't initialized?" );
151164 }
152165
@@ -161,7 +174,7 @@ void GLEngineGLFW::configureImGui() {
161174
162175 ImFontAtlas* _unused;
163176 std::tie (_unused, regularFont, monoFont) = options::prepareImGuiFontsCallback ();
164-
177+
165178 ImGui_ImplOpenGL3_CreateFontsTexture ();
166179 }
167180
@@ -172,7 +185,6 @@ void GLEngineGLFW::configureImGui() {
172185}
173186
174187
175-
176188void GLEngineGLFW::shutdown () {
177189 checkError ();
178190 shutdownImGui ();
0 commit comments