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

DPI awareness font scaling #85

Merged
merged 2 commits into from
Apr 9, 2022
Merged
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
3 changes: 3 additions & 0 deletions engine/source/editor/source/editor_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ namespace Pilot
// if (new_window_pos != m_engine_window_pos || new_window_size != m_engine_window_size)
{
#if defined(__MACH__)
// The dpi_scale is not reactive to DPI changes or monitor switching, it might be a bug from ImGui.
// Return value from ImGui::GetMainViewport()->DpiScal is always the same as first frame.
// glfwGetMonitorContentScale and glfwSetWindowContentScaleCallback are more adaptive.
float dpi_scale = main_viewport->DpiScale;
m_editor->onWindowChanged(new_window_pos.x * dpi_scale,
new_window_pos.y * dpi_scale,
Expand Down
17 changes: 16 additions & 1 deletion engine/source/runtime/function/render/source/surface_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

using namespace Pilot;

void window_content_scale_callback(GLFWwindow* window, float x_scale, float y_scale)
{
#if defined(__MACH__)
float font_scale = fmax(1.0f, fmax(x_scale, y_scale));
ImGui::GetIO().FontGlobalScale = 1.0f / font_scale;
#endif
}

int SurfaceUI::initialize(SurfaceRHI* rhi, PilotRenderer* prenderer, std::shared_ptr<SurfaceIO> pio)
{
m_io = pio;
Expand All @@ -22,9 +30,13 @@ int SurfaceUI::initialize(SurfaceRHI* rhi, PilotRenderer* prenderer, std::shared
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigDockingAlwaysTabBar = true;
io.ConfigWindowsMoveFromTitleBarOnly = true;

float x_scale, y_scale;
glfwGetWindowContentScale(pio->m_window, &x_scale, &y_scale);
float font_scale = fmax(1.0f, fmax(x_scale, y_scale));

io.Fonts->AddFontFromFileTTF(
ConfigManager::getInstance().getEditorFontPath().generic_string().data(), 16, nullptr, nullptr);
ConfigManager::getInstance().getEditorFontPath().generic_string().data(), font_scale * 16, nullptr, nullptr);
io.Fonts->Build();
style.WindowPadding = ImVec2(1.0, 0);
style.FramePadding = ImVec2(14.0, 2.0f);
Expand All @@ -50,6 +62,9 @@ int SurfaceUI::initialize(SurfaceRHI* rhi, PilotRenderer* prenderer, std::shared
init_info.ImageCount = rhi->m_vulkan_manager->m_max_frames_in_flight;
ImGui_ImplVulkan_Init(&init_info, rhi->m_vulkan_manager->getLightingPass());

window_content_scale_callback(pio->m_window, x_scale, y_scale);
glfwSetWindowContentScaleCallback(pio->m_window, window_content_scale_callback);

// fonts upload
fontsUpload(rhi);

Expand Down