Skip to content

Commit d3b46a4

Browse files
authored
rework high-dpi auto-setting to fix it on macOS (#347)
* rework high-dpi handling for macOS * always start with default style in config
1 parent d3e6ca6 commit d3b46a4

File tree

4 files changed

+54
-28
lines changed

4 files changed

+54
-28
lines changed

examples/demo-app/demo_app.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ int main(int argc, char** argv) {
879879
// polyscope::view::windowWidth = 600;
880880
// polyscope::view::windowHeight = 800;
881881
// polyscope::options::maxFPS = -1;
882-
polyscope::options::verbosity = 100;
882+
polyscope::options::verbosity = 101;
883883
polyscope::options::enableRenderErrorChecks = true;
884884
polyscope::options::allowHeadlessBackends = true;
885885

src/imgui_config.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ const unsigned int* getLatoRegularCompressedData();
1616

1717
void configureImGuiStyle() {
1818

19-
// Style
2019
ImGuiStyle* style = &ImGui::GetStyle();
21-
style->WindowRounding = 1 * options::uiScale;
22-
style->FrameRounding = 1 * options::uiScale;
23-
style->FramePadding.y = 4 * options::uiScale;
24-
style->ScrollbarRounding = 1 * options::uiScale;
25-
style->ScrollbarSize = 20 * options::uiScale;
20+
*style = ImGuiStyle(); // apply the default style as a starting point
21+
22+
// Style
23+
style->WindowRounding = 1;
24+
style->FrameRounding = 1;
25+
style->FramePadding.y = 4;
26+
style->ScrollbarRounding = 1;
27+
style->ScrollbarSize = 20;
28+
style->ScaleAllSizes(options::uiScale);
2629

2730

2831
// Colors
@@ -77,21 +80,32 @@ std::tuple<ImFontAtlas*, ImFont*, ImFont*> prepareImGuiFonts() {
7780

7881
ImGuiIO& io = ImGui::GetIO();
7982

83+
ImVec2 windowSize{static_cast<float>(view::windowWidth), static_cast<float>(view::windowHeight)};
84+
ImVec2 bufferSize{static_cast<float>(view::bufferWidth), static_cast<float>(view::bufferHeight)};
85+
ImVec2 imguiCoordScale = {bufferSize.x / windowSize.x, bufferSize.y / windowSize.y};
86+
8087
// outputs
8188
ImFontAtlas* fontAtlas;
8289
ImFont* regularFont;
8390
ImFont* monoFont;
8491

92+
float fontSize = 16.0 * options::uiScale;
93+
fontSize = std::max(1.0f, std::roundf(fontSize));
94+
8595
{ // add regular font
8696
ImFontConfig config;
97+
config.RasterizerDensity = std::max(imguiCoordScale.x, imguiCoordScale.y);
8798
regularFont = io.Fonts->AddFontFromMemoryCompressedTTF(render::getLatoRegularCompressedData(),
88-
render::getLatoRegularCompressedSize(), options::uiScale*18.0f, &config);
99+
render::getLatoRegularCompressedSize(),
100+
options::uiScale * 18.0f, &config);
89101
}
90102

91103
{ // add mono font
92104
ImFontConfig config;
105+
config.RasterizerDensity = std::max(imguiCoordScale.x, imguiCoordScale.y);
93106
monoFont = io.Fonts->AddFontFromMemoryCompressedTTF(render::getCousineRegularCompressedData(),
94-
render::getCousineRegularCompressedSize(), options::uiScale*16.0f, &config);
107+
render::getCousineRegularCompressedSize(),
108+
options::uiScale * 16.0f, &config);
95109
}
96110

97111
// io.Fonts->AddFontFromFileTTF("test-font-name.ttf", 16);

src/render/opengl/gl_engine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ void GLCompiledProgram::compileGLProgram(const std::vector<ShaderStageSpecificat
10781078
if (options::verbosity > 2) {
10791079
printShaderInfoLog(h);
10801080
}
1081-
if (options::verbosity > 100) {
1081+
if (options::verbosity > 200) {
10821082
std::cout << "Program text:" << std::endl;
10831083
std::cout << s.src.c_str() << std::endl;
10841084
}

src/render/opengl/gl_engine_glfw.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

123123
void 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

134147
void GLEngineGLFW::initializeImGui() {
@@ -146,7 +159,7 @@ void GLEngineGLFW::initializeImGui() {
146159

147160
void 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-
176188
void GLEngineGLFW::shutdown() {
177189
checkError();
178190
shutdownImGui();

0 commit comments

Comments
 (0)