Skip to content

Commit

Permalink
Replace DPI scaling with a window/rendering size factor
Browse files Browse the repository at this point in the history
Should make fonts on macOS smaller, and fonts on Linux highDPI displays larger.
  • Loading branch information
kblaschke committed Apr 12, 2024
1 parent 517c0c7 commit cb6c2ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
36 changes: 23 additions & 13 deletions src/gui/ProjectMGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,31 @@ void ProjectMGUI::UpdateFontSize()
{
ImGuiIO& io = ImGui::GetIO();

float dpi{96.0f}; // Use default value of 96 DPI if SDL_GetDisplayDPI doesn't return a value!
auto displayIndex = SDL_GetWindowDisplayIndex(_renderingWindow);
if (displayIndex < 0)
{
poco_debug_f1(_logger, "Could not get display index for application window: %s", std::string(SDL_GetError()));
return;
}

auto result = SDL_GetDisplayDPI(displayIndex, &dpi, nullptr, nullptr);
if (result != 0)
{
poco_debug_f2(_logger, "Could not get DPI info for display %?d: %s", displayIndex, std::string(SDL_GetError()));
}
auto newScalingFactor = GetScalingFactor();

// Only interested in changes of > 1 DPI, really.
if (static_cast<uint32_t>(dpi) == static_cast<uint32_t>(_dpi))
// Only interested in changes of .05 or more
if (std::abs(_textScalingFactor - newScalingFactor) < 0.05)
{
return;
}

poco_debug_f3(_logger, "DPI change for display %?d: %hf -> %hf", displayIndex, _dpi, dpi);
poco_debug_f3(_logger, "Scaling factor change for display %?d: %hf -> %hf", displayIndex, _textScalingFactor, newScalingFactor);

_dpi = dpi;
_textScalingFactor = newScalingFactor;

ImFontConfig config;
config.MergeMode = true;

io.Fonts->Clear();
_uiFont = io.Fonts->AddFontFromMemoryCompressedTTF(&AnonymousPro_compressed_data, AnonymousPro_compressed_size, floor(12.0f * (_dpi / 96.0f)));
_toastFont = io.Fonts->AddFontFromMemoryCompressedTTF(&LiberationSans_compressed_data, LiberationSans_compressed_size, floor(20.0f * (_dpi / 96.0f)));
_uiFont = io.Fonts->AddFontFromMemoryCompressedTTF(&AnonymousPro_compressed_data, AnonymousPro_compressed_size, floor(24.0f * _textScalingFactor));
_toastFont = io.Fonts->AddFontFromMemoryCompressedTTF(&LiberationSans_compressed_data, LiberationSans_compressed_size, floor(40.0f * _textScalingFactor));
io.Fonts->Build();
ImGui_ImplOpenGL3_CreateFontsTexture();

Expand Down Expand Up @@ -213,10 +208,25 @@ void ProjectMGUI::ShowHelpWindow()
_helpWindow.Show();
}

float ProjectMGUI::GetScalingFactor()
{
int windowWidth;
int windowHeight;
int renderWidth;
int renderHeight;

SDL_GetWindowSize(_renderingWindow, &windowWidth, &windowHeight);
SDL_GL_GetDrawableSize(_renderingWindow, &renderWidth, &renderHeight);

// If the OS has a scaled UI, this will return the inverse factor. E.g. if the display is scaled to 200%,
// the renderWidth (in actual pixels) will be twice as much as the "virtual" unscaled window width.
return ((static_cast<float>(windowWidth) / static_cast<float>(renderWidth)) + (static_cast<float>(windowHeight) / static_cast<float>(renderHeight))) * 0.5f;
}

void ProjectMGUI::DisplayToastNotificationHandler(const Poco::AutoPtr<DisplayToastNotification>& notification)
{
if (Poco::Util::Application::instance().config().getBool("projectM.displayToasts", true))
{
_toast = std::make_unique<ToastMessage>(notification->ToastText(), 3.0f);
}
}
}
4 changes: 3 additions & 1 deletion src/gui/ProjectMGUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class ProjectMGUI : public Poco::Util::Subsystem
void ShowHelpWindow();

private:
float GetScalingFactor();

void DisplayToastNotificationHandler(const Poco::AutoPtr<DisplayToastNotification>& notification);

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

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

float _dpi{0.0f}; //!< Last DPI value.
float _textScalingFactor{0.0f}; //!< The text scaling factor.

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

0 comments on commit cb6c2ff

Please sign in to comment.