diff --git a/wpigui/src/main/native/cpp/wpigui.cpp b/wpigui/src/main/native/cpp/wpigui.cpp index 5da6371d80..0dc9c8f4b9 100644 --- a/wpigui/src/main/native/cpp/wpigui.cpp +++ b/wpigui/src/main/native/cpp/wpigui.cpp @@ -115,6 +115,47 @@ void gui::DestroyContext() { gContext = nullptr; } +static void UpdateFontScale() { + // Scale based on OS window content scaling + float windowScale = 1.0; +#ifndef __APPLE__ + glfwGetWindowContentScale(gContext->window, &windowScale, nullptr); +#endif + // map to closest font size: 0 = 0.5x, 1 = 0.75x, 2 = 1.0x, 3 = 1.25x, + // 4 = 1.5x, 5 = 1.75x, 6 = 2x + int fontScale = + gContext->userScale + static_cast((windowScale - 1.0) * 4); + if (fontScale < 0) { + fontScale = 0; + } + if (gContext->fontScale != fontScale) { + gContext->reloadFonts = true; + gContext->fontScale = fontScale; + } +} + +// the range is based on 13px being the "nominal" 100% size and going from +// ~0.5x (7px) to ~2.0x (25px) +static void ReloadFonts() { + auto& io = ImGui::GetIO(); + io.Fonts->Clear(); + gContext->fonts.clear(); + float size = 7.0f + gContext->fontScale * 3.0f; + bool first = true; + for (auto&& makeFont : gContext->makeFonts) { + if (makeFont.second) { + ImFontConfig cfg; + std::snprintf(cfg.Name, sizeof(cfg.Name), "%s", makeFont.first); + ImFont* font = makeFont.second(io, size, &cfg); + if (first) { + ImGui::GetIO().FontDefault = font; + first = false; + } + gContext->fonts.emplace_back(font); + } + } +} + bool gui::Initialize(const char* title, int width, int height, ImGuiConfigFlags configFlags) { gContext->title = title; @@ -268,20 +309,9 @@ bool gui::Initialize(const char* title, int width, int height, SetStyle(static_cast