Skip to content

Commit cb6c2ff

Browse files
committed
Replace DPI scaling with a window/rendering size factor
Should make fonts on macOS smaller, and fonts on Linux highDPI displays larger.
1 parent 517c0c7 commit cb6c2ff

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/gui/ProjectMGUI.cpp

+23-13
Original file line numberDiff line numberDiff line change
@@ -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+
216226
void 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+
}

src/gui/ProjectMGUI.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ class ProjectMGUI : public Poco::Util::Subsystem
105105
void ShowHelpWindow();
106106

107107
private:
108+
float GetScalingFactor();
109+
108110
void DisplayToastNotificationHandler(const Poco::AutoPtr<DisplayToastNotification>& notification);
109111

110112
ProjectMWrapper* _projectMWrapper{nullptr};
@@ -120,7 +122,7 @@ class ProjectMGUI : public Poco::Util::Subsystem
120122

121123
uint64_t _lastFrameTicks{0}; //!< Tick count of the last frame (see SDL_GetTicks64)
122124

123-
float _dpi{0.0f}; //!< Last DPI value.
125+
float _textScalingFactor{0.0f}; //!< The text scaling factor.
124126

125127
MainMenu _mainMenu{*this};
126128
SettingsWindow _settingsWindow{*this}; //!< The settings window.

0 commit comments

Comments
 (0)