@@ -71,36 +71,31 @@ void ProjectMGUI::UpdateFontSize()
7171{
7272 ImGuiIO& io = ImGui::GetIO ();
7373
74- float dpi{96 .0f }; // Use default value of 96 DPI if SDL_GetDisplayDPI doesn't return a value!
7574 auto displayIndex = SDL_GetWindowDisplayIndex (_renderingWindow);
7675 if (displayIndex < 0 )
7776 {
7877 poco_debug_f1 (_logger, " Could not get display index for application window: %s" , std::string (SDL_GetError ()));
7978 return ;
8079 }
8180
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 ();
8782
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 )
9085 {
9186 return ;
9287 }
9388
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 );
9590
96- _dpi = dpi ;
91+ _textScalingFactor = newScalingFactor ;
9792
9893 ImFontConfig config;
9994 config.MergeMode = true ;
10095
10196 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 ));
10499 io.Fonts ->Build ();
105100 ImGui_ImplOpenGL3_CreateFontsTexture ();
106101
@@ -213,10 +208,25 @@ void ProjectMGUI::ShowHelpWindow()
213208 _helpWindow.Show ();
214209}
215210
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+
216226void ProjectMGUI::DisplayToastNotificationHandler (const Poco::AutoPtr<DisplayToastNotification>& notification)
217227{
218228 if (Poco::Util::Application::instance ().config ().getBool (" projectM.displayToasts" , true ))
219229 {
220230 _toast = std::make_unique<ToastMessage>(notification->ToastText (), 3 .0f );
221231 }
222- }
232+ }
0 commit comments