Skip to content

Commit

Permalink
Merge pull request #578 from 602p/ngscope_dpi
Browse files Browse the repository at this point in the history
Support window content scale in ngscopeclient
  • Loading branch information
azonenberg committed Mar 25, 2023
2 parents 17d69b2 + 83b38f0 commit 1563bb4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/ngscopeclient/FontManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ FontManager::~FontManager()
@return True if changes were made to the font atlas
*/
bool FontManager::UpdateFonts(PreferenceCategory& root)
bool FontManager::UpdateFonts(PreferenceCategory& root, float contentScale)
{
//Make a list of fonts we want to have
set<FontDescription> fonts;
Expand Down Expand Up @@ -98,7 +98,7 @@ bool FontManager::UpdateFonts(PreferenceCategory& root)

//Load the fonts
for(auto f : fonts)
m_fonts[f] = atlas->AddFontFromFileTTF(f.first.c_str(), f.second, nullptr, ranges.Data);
m_fonts[f] = atlas->AddFontFromFileTTF(f.first.c_str(), f.second * contentScale, nullptr, ranges.Data);

//Done loading fonts, build the texture
atlas->Flags = ImFontAtlasFlags_NoMouseCursors;
Expand Down
2 changes: 1 addition & 1 deletion src/ngscopeclient/FontManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FontManager
FontManager();
~FontManager();

bool UpdateFonts(PreferenceCategory& root);
bool UpdateFonts(PreferenceCategory& root, float contentScale);

/**
@brief Gets the font, if any, for the provided description
Expand Down
2 changes: 1 addition & 1 deletion src/ngscopeclient/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ void MainWindow::UpdateFonts()
//Check for any changes to font preferences and rebuild the atlas if so
//Early out if nothing changed
auto& prefs = GetSession().GetPreferences();
if(!m_fontmgr.UpdateFonts(prefs.AllPreferences()))
if(!m_fontmgr.UpdateFonts(prefs.AllPreferences(), GetContentScale()))
return;

//Set the default font
Expand Down
20 changes: 15 additions & 5 deletions src/ngscopeclient/VulkanWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ VulkanWindow::VulkanWindow(const string& title, shared_ptr<QueueHandle> queue)
ImGui_ImplVulkan_Init(&info, **m_renderPass);
}

// Apply DPI scaling now that glfw initialized
float scale = GetContentScale();
LogTrace("Applying ImGui style scale factor: %.2f\n", scale);
ImGui::GetStyle().ScaleAllSizes(scale);

//Hook a couple of backend functions with mutexing
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplVulkan_CreateWindow = platform_io.Renderer_CreateWindow;
Expand Down Expand Up @@ -304,11 +309,6 @@ bool VulkanWindow::UpdateFramebuffer()
return false;
}

float xscale;
float yscale;
glfwGetWindowContentScale(m_window, &xscale, &yscale);
LogTrace("Scale: %.2f, %.2f\n", xscale, yscale);

const VkFormat requestSurfaceImageFormat[] =
{
VK_FORMAT_B8G8R8A8_UNORM,
Expand Down Expand Up @@ -403,6 +403,16 @@ bool VulkanWindow::UpdateFramebuffer()
return true;
}

float VulkanWindow::GetContentScale()
{
float xscale;
float yscale;
glfwGetWindowContentScale(m_window, &xscale, &yscale);

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

void VulkanWindow::Render()
{
//If we're re-rendering after the window size changed, fix up the framebuffer before we worry about anything else
Expand Down
4 changes: 4 additions & 0 deletions src/ngscopeclient/VulkanWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ 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();

virtual void Render();

std::shared_ptr<QueueHandle> GetRenderQueue()
Expand Down

0 comments on commit 1563bb4

Please sign in to comment.