Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: DPI scaling improvements #725

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ngscopeclient/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ void MainWindow::UpdateFonts()
//Check for any changes to font preferences and rebuild the atlas if so
//Skip rebuilding atlas if nothing changed
auto& prefs = GetSession().GetPreferences();
if(m_fontmgr.UpdateFonts(prefs.AllPreferences(), GetContentScale()))
if(m_fontmgr.UpdateFonts(prefs.AllPreferences(), GetUIScale() * GetFontScale()))
{
//Download imgui fonts
ImGui_ImplVulkan_CreateFontsTexture();
Expand Down
49 changes: 33 additions & 16 deletions src/ngscopeclient/VulkanWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,11 @@ VulkanWindow::VulkanWindow(const string& title, shared_ptr<QueueHandle> queue)
ImGui_ImplVulkan_Init(&info);
}

// Apply DPI scaling now that glfw initialized
float scale = GetContentScale();
float ui_scale = GetUIScale(), font_scale = GetFontScale();
LogTrace("UI scale: %.2f\n", ui_scale);
LogTrace("Text density: %.2f dpi = 96 dpi × %.2f (UI scale) × %.2f (font scale)\n", 96.0 * ui_scale * font_scale, ui_scale, font_scale);

LogTrace("Applying ImGui style scale factor: %.2f\n", scale);

//WORKAROUND: handle HiDPI correctly on macOS.
#ifdef __APPLE__
io.FontGlobalScale = 1.0f / scale;
#else
ImGui::GetStyle().ScaleAllSizes(scale);
#endif
ImGui::GetStyle().ScaleAllSizes(ui_scale);

//Hook a couple of backend functions with mutexing
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
Expand Down Expand Up @@ -430,14 +424,37 @@ bool VulkanWindow::UpdateFramebuffer()
return true;
}

float VulkanWindow::GetContentScale()
float VulkanWindow::GetUIScale()
{
float xscale;
float yscale;
glfwGetWindowContentScale(m_window, &xscale, &yscale);
if (const char *scale_override = getenv("NGSCOPECLIENT_UI_SCALE"))
return atof(scale_override);
#if defined(WIN32)
// FIXME
#elif defined(__APPLE__)
// FIXME
#else
// KDE also sets these variables.
if (const char *gdk_scale = getenv("GDK_SCALE"))
return atoi(gdk_scale);
#endif

// Hope this works well should a screen have unequal X- and Y- DPIs...
return (xscale + yscale) / 2;
return 1.0;
}

float VulkanWindow::GetFontScale()
{
if (const char *scale_override = getenv("NGSCOPECLIENT_FONT_SCALE"))
return atof(scale_override);
#if defined(WIN32)
// FIXME
#elif defined(__APPLE__)
// FIXME
#else
// KDE also sets these variables.
if (const char *gdk_dpi_scale = getenv("GDK_DPI_SCALE"))
return atof(gdk_dpi_scale);
#endif
return 1.0;
}

void VulkanWindow::Render()
Expand Down
8 changes: 5 additions & 3 deletions src/ngscopeclient/VulkanWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ class VulkanWindow
GLFWwindow* GetWindow()
{ return m_window; }

// Return a DPI 'scale' value where 1 ~= 96DPI
// Akin to uses of `get_pango_context()->get_resolution() / 96` in glscopeclient
float GetContentScale();
///@brief Scale factor for UI elements (icons, scrollbars, etc); typically an integer number.
float GetUIScale();

///@brief Scale factor for fonts, applied on top of UI scale factor; often a fractional number.
float GetFontScale();

virtual void Render();

Expand Down
Loading