@@ -71,36 +71,31 @@ void ProjectMGUI::UpdateFontSize()
71
71
{
72
72
ImGuiIO& io = ImGui::GetIO ();
73
73
74
- float dpi{96 .0f }; // Use default value of 96 DPI if SDL_GetDisplayDPI doesn't return a value!
75
74
auto displayIndex = SDL_GetWindowDisplayIndex (_renderingWindow);
76
75
if (displayIndex < 0 )
77
76
{
78
77
poco_debug_f1 (_logger, " Could not get display index for application window: %s" , std::string (SDL_GetError ()));
79
78
return ;
80
79
}
81
80
82
- auto result = SDL_GetDisplayDPI (displayIndex, &dpi, nullptr , nullptr );
83
- if (result != 0 )
84
- {
85
- poco_debug_f2 (_logger, " Could not get DPI info for display %?d: %s" , displayIndex, std::string (SDL_GetError ()));
86
- }
81
+ auto newScalingFactor = GetScalingFactor ();
87
82
88
- // Only interested in changes of > 1 DPI, really.
89
- if (static_cast < uint32_t >(dpi) == static_cast < uint32_t >(_dpi) )
83
+ // Only interested in changes of .05 or more
84
+ if (std::abs (_textScalingFactor - newScalingFactor) < 0.05 )
90
85
{
91
86
return ;
92
87
}
93
88
94
- poco_debug_f3 (_logger, " DPI change for display %?d: %hf -> %hf" , displayIndex, _dpi, dpi );
89
+ poco_debug_f3 (_logger, " Scaling factor change for display %?d: %hf -> %hf" , displayIndex, _textScalingFactor, newScalingFactor );
95
90
96
- _dpi = dpi ;
91
+ _textScalingFactor = newScalingFactor ;
97
92
98
93
ImFontConfig config;
99
94
config.MergeMode = true ;
100
95
101
96
io.Fonts ->Clear ();
102
- _uiFont = io.Fonts ->AddFontFromMemoryCompressedTTF (&AnonymousPro_compressed_data, AnonymousPro_compressed_size, floor (12 .0f * (_dpi / 96 . 0f ) ));
103
- _toastFont = io.Fonts ->AddFontFromMemoryCompressedTTF (&LiberationSans_compressed_data, LiberationSans_compressed_size, floor (20 .0f * (_dpi / 96 . 0f ) ));
97
+ _uiFont = io.Fonts ->AddFontFromMemoryCompressedTTF (&AnonymousPro_compressed_data, AnonymousPro_compressed_size, floor (24 .0f * _textScalingFactor ));
98
+ _toastFont = io.Fonts ->AddFontFromMemoryCompressedTTF (&LiberationSans_compressed_data, LiberationSans_compressed_size, floor (40 .0f * _textScalingFactor ));
104
99
io.Fonts ->Build ();
105
100
ImGui_ImplOpenGL3_CreateFontsTexture ();
106
101
@@ -213,10 +208,25 @@ void ProjectMGUI::ShowHelpWindow()
213
208
_helpWindow.Show ();
214
209
}
215
210
211
+ float ProjectMGUI::GetScalingFactor ()
212
+ {
213
+ int windowWidth;
214
+ int windowHeight;
215
+ int renderWidth;
216
+ int renderHeight;
217
+
218
+ SDL_GetWindowSize (_renderingWindow, &windowWidth, &windowHeight);
219
+ SDL_GL_GetDrawableSize (_renderingWindow, &renderWidth, &renderHeight);
220
+
221
+ // If the OS has a scaled UI, this will return the inverse factor. E.g. if the display is scaled to 200%,
222
+ // the renderWidth (in actual pixels) will be twice as much as the "virtual" unscaled window width.
223
+ return ((static_cast <float >(windowWidth) / static_cast <float >(renderWidth)) + (static_cast <float >(windowHeight) / static_cast <float >(renderHeight))) * 0 .5f ;
224
+ }
225
+
216
226
void ProjectMGUI::DisplayToastNotificationHandler (const Poco::AutoPtr<DisplayToastNotification>& notification)
217
227
{
218
228
if (Poco::Util::Application::instance ().config ().getBool (" projectM.displayToasts" , true ))
219
229
{
220
230
_toast = std::make_unique<ToastMessage>(notification->ToastText (), 3 .0f );
221
231
}
222
- }
232
+ }
0 commit comments