From 4adb78cd7655462768d2226e7d8f5003bf669d34 Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Mon, 5 Feb 2024 18:52:17 +0100 Subject: [PATCH] Add DpiAwareParams --- src/hello_imgui/doc_api.md | 8 + src/hello_imgui/doc_params.md | 169 +++++++++++---- src/hello_imgui/doc_params.src.md | 28 ++- src/hello_imgui/dpi_aware.h | 74 +++++++ src/hello_imgui/impl/dpi_aware.cpp | 13 +- .../backend_impls/abstract_runner.cpp | 199 ++++++++++++++---- .../internal/backend_impls/abstract_runner.h | 11 +- src/hello_imgui/runner_params.h | 3 + 8 files changed, 393 insertions(+), 112 deletions(-) diff --git a/src/hello_imgui/doc_api.md b/src/hello_imgui/doc_api.md index 3682b22e..680e404e 100644 --- a/src/hello_imgui/doc_api.md +++ b/src/hello_imgui/doc_api.md @@ -278,6 +278,14 @@ float FrameRate(float durationForMean = 0.5f); // of ImGuiTestEngine that was initialized by HelloImGui // (iif ImGui Test Engine is active). ImGuiTestEngine* GetImGuiTestEngine(); + +// `GetBackendDescription()`: returns a string with the backend info +// Could be for example: +// "Glfw - OpenGL3" +// "Glfw - Metal" +// "Sdl - Vulkan" +std::string GetBackendDescription(); + ``` ---- diff --git a/src/hello_imgui/doc_params.md b/src/hello_imgui/doc_params.md index e723a96b..932a55fc 100644 --- a/src/hello_imgui/doc_params.md +++ b/src/hello_imgui/doc_params.md @@ -56,13 +56,17 @@ struct SimpleRunnerParams float fpsIdle = 9.f; // `enableIdling`: _bool, default=true_. - // Set this to false to disable idling at startup + // Disable idling at startup by setting this to false + // When running, use: + // HelloImGui::GetRunnerParams()->fpsIdling.enableIdling = false; bool enableIdling = true; RunnerParams ToRunnerParams() const; }; ``` +--- + ## Full params ```cpp @@ -114,9 +118,10 @@ struct RunnerParams // Select the wanted platform backend type between `Sdl`, `Glfw`. // if `FirstAvailable`, Glfw will be preferred over Sdl when both are available. // Only useful when multiple backend are compiled and available. - PlatformBackendType backendType = PlatformBackendType::FirstAvailable; + // (for compatibility with older versions, you can use BackendType instead of PlatformBackendType) + PlatformBackendType platformBackendType = PlatformBackendType::FirstAvailable; - // `rendererBackendType`: _enum RendererBackendType, default=RendererBackendType::FirstAvailable_ + // `renderingBackendType`: _enum RenderingBackendType, default=RenderingBackendType::FirstAvailable_ // Select the wanted rendering backend type between `OpenGL3`, `Metal`, `Vulkan`, `DirectX11`, `DirectX12`. // if `FirstAvailable`, it will be selected in the order of preference mentioned above. // Only useful when multiple rendering backend are compiled and available. @@ -166,6 +171,8 @@ struct RunnerParams // (set fpsIdling.enableIdling to false to disable Idling) FpsIdling fpsIdling; + // --------------- DPI Handling ----------- + DpiAwareParams dpiAwareParams; // --------------- Misc ------------------- @@ -185,6 +192,11 @@ struct RunnerParams // Set the application refresh rate // (only used on emscripten: 0 stands for "let the app or the browser decide") int emscripten_fps = 0; + + // --------------- Legacy -------------------` +#ifndef HELLOIMGUI_DISABLE_OBSOLETE_BACKEND + PlatformBackendType& backendType = platformBackendType; // a synonym, for backward compatibility +#endif }; ``` @@ -194,7 +206,7 @@ struct RunnerParams ```cpp // You can select the platform backend type (SDL, GLFW) and the rendering backend type -// via RunnerParams.backendType and RunnerParams.rendererBackendType. +// via RunnerParams.backendType and RunnerParams.renderingBackendType. // Platform backend type (SDL, GLFW) // They are listed in the order of preference when FirstAvailable is selected. @@ -205,7 +217,9 @@ enum class PlatformBackendType Sdl, }; +#ifndef HELLOIMGUI_DISABLE_OBSOLETE_BACKEND using BackendType = PlatformBackendType; // for backward compatibility +#endif // Rendering backend type (OpenGL3, Metal, Vulkan, DirectX11, DirectX12) // They are listed in the order of preference when FirstAvailable is selected. @@ -222,44 +236,6 @@ enum class RendererBackendType ``` -# Fps Idling - -See [runner_params.h](https://github.com/pthom/hello_imgui/blob/master/src/hello_imgui/runner_params.h). - -```cpp - -// FpsIdling is a struct that contains Fps Idling parameters -struct FpsIdling -{ - // `fpsIdle`: _float, default=9_. - // ImGui applications can consume a lot of CPU, since they update the screen - // very frequently. In order to reduce the CPU usage, the FPS is reduced when - // no user interaction is detected. - // This is ok most of the time but if you are displaying animated widgets - // (for example a live video), you may want to ask for a faster refresh: - // either increase fpsIdle, or set it to 0 for maximum refresh speed - // (you can change this value during the execution depending on your application - // refresh needs) - float fpsIdle = 9.f; - - // `enableIdling`: _bool, default=true_. - // Disable idling by setting this to false. - // (this can be changed dynamically during execution) - bool enableIdling = true; - - // `isIdling`: bool (dynamically updated during execution) - // This bool will be updated during the application execution, - // and will be set to true when it is idling. - bool isIdling = false; - - // `rememberEnableIdling`: _bool, default=true_. - // If true, the last value of enableIdling is restored from the settings at startup. - bool rememberEnableIdling = false; -}; -``` - ----- - # Runner callbacks See [runner_callbacks.h](https://github.com/pthom/hello_imgui/blob/master/src/hello_imgui/runner_callbacks.h). @@ -812,6 +788,115 @@ enum class DefaultImGuiWindowType }; ``` +# Fps Idling + +See [runner_params.h](https://github.com/pthom/hello_imgui/blob/master/src/hello_imgui/runner_params.h). + +```cpp + +// FpsIdling is a struct that contains Fps Idling parameters +struct FpsIdling +{ + // `fpsIdle`: _float, default=9_. + // ImGui applications can consume a lot of CPU, since they update the screen + // very frequently. In order to reduce the CPU usage, the FPS is reduced when + // no user interaction is detected. + // This is ok most of the time but if you are displaying animated widgets + // (for example a live video), you may want to ask for a faster refresh: + // either increase fpsIdle, or set it to 0 for maximum refresh speed + // (you can change this value during the execution depending on your application + // refresh needs) + float fpsIdle = 9.f; + + // `enableIdling`: _bool, default=true_. + // Disable idling by setting this to false. + // (this can be changed dynamically during execution) + bool enableIdling = true; + + // `isIdling`: bool (dynamically updated during execution) + // This bool will be updated during the application execution, + // and will be set to true when it is idling. + bool isIdling = false; + + // `rememberEnableIdling`: _bool, default=true_. + // If true, the last value of enableIdling is restored from the settings at startup. + bool rememberEnableIdling = false; +}; +``` + +# Dpi Aware Params + +See [dpi_aware.h](ttps://github.com/pthom/hello_imgui/blob/master/src/hello_imgui/dpi_aware.h) + +```cpp + +// +// Hello ImGui will try its best to automatically handle DPI scaling for you. +// It does this by setting two values: +// +// - `dpiWindowSizeFactor`: +// factor by which window size should be multiplied +// +// - `fontRenderingScale`: +// factor by which fonts glyphs should be scaled at rendering time +// (typically 1 on windows, and 0.5 on macOS retina screens) +// +// By default, Hello ImGui will compute them automatically, +// when dpiWindowSizeFactor and fontRenderingScale are set to 0. +// +// How to set those values manually: +// --------------------------------- +// If it fails (i.e. your window and/or fonts are too big or too small), +// you may set them manually: +// (1) Either by setting them programmatically in your application +// (set their values in `runnerParams.dpiAwareParams`) +// (2) Either by setting them in the app ini file +// (3) Either by setting them in a `hello_imgui.ini` file in the app folder, or any of its parent folders. +// (this is useful when you want to set them for a specific app or set of apps, without modifying the app code) +// Note: if several methods are used, the order of priority is (1) > (2) > (3) +// +// Example content of a ini file: +// ------------------------------ +// [DpiAwareParams] +// dpiWindowSizeFactor=2 +// fontRenderingScale=0.5 +// +struct DpiAwareParams +{ + // `dpiWindowSizeFactor` + // factor by which window size should be multiplied to get a similar + // visible size on different OSes. + // In a standard environment (i.e. outside of Hello ImGui), an application with a size of 960x480 pixels, + // may have a physical size (in mm or inches) that varies depending on the screen DPI, and the OS. + // + // Inside Hello ImGui, the window size is always treated as targeting a 96 PPI screen, so that its size will + // look similar whatever the OS and the screen DPI. + // In our example, our 960x480 pixels window will try to correspond to a 10x5 inches window + // + // Hello ImGui does its best to compute it on all OSes. + // However, if it fails you may set its value manually. + // If it is set to 0, Hello ImGui will compute it automatically, + // and the resulting value will be stored in `dpiWindowSizeFactor`. + float dpiWindowSizeFactor = 0.0f; + + // `fontRenderingScale` + // factor (that is either 1 or < 1.) by which fonts glyphs should be + // scaled at rendering time. + // On macOS retina screens, it will be 0.5, since macOS APIs hide + // the real resolution of the screen. + float fontRenderingScale = 0.0f; + + // `dpiFontLoadingFactor` + // factor by which font size should be multiplied at loading time to get a similar + // visible size on different OSes. + // The size will be equivalent to a size given for a 96 PPI screen + float DpiFontLoadingFactor() { return dpiWindowSizeFactor / fontRenderingScale;}; +}; + +// ---------------------------------------------------------------------------- + +``` + ---- # Docking diff --git a/src/hello_imgui/doc_params.src.md b/src/hello_imgui/doc_params.src.md index 3997d4b9..310c9f33 100644 --- a/src/hello_imgui/doc_params.src.md +++ b/src/hello_imgui/doc_params.src.md @@ -16,6 +16,8 @@ See [runner_params.h](https://github.com/pthom/hello_imgui/blob/master/src/hello @import "runner_params.h" {md_id=SimpleRunnerParams} ``` +--- + ## Full params ```cpp @@ -30,16 +32,6 @@ See [runner_params.h](https://github.com/pthom/hello_imgui/blob/master/src/hello ``` -# Fps Idling - -See [runner_params.h](https://github.com/pthom/hello_imgui/blob/master/src/hello_imgui/runner_params.h). - -```cpp -@import "runner_params.h" {md_id=FpsIdling} -``` - ----- - # Runner callbacks See [runner_callbacks.h](https://github.com/pthom/hello_imgui/blob/master/src/hello_imgui/runner_callbacks.h). @@ -118,6 +110,22 @@ See [imgui_window_params.h](https://github.com/pthom/hello_imgui/blob/master/src @import "imgui_window_params.h" {md_id=DefaultImGuiWindowType} ``` +# Fps Idling + +See [runner_params.h](https://github.com/pthom/hello_imgui/blob/master/src/hello_imgui/runner_params.h). + +```cpp +@import "runner_params.h" {md_id=FpsIdling} +``` + +# Dpi Aware Params + +See [dpi_aware.h](ttps://github.com/pthom/hello_imgui/blob/master/src/hello_imgui/dpi_aware.h) + +```cpp +@import "dpi_aware.h" {md_id=DpiAwareParams} +``` + ---- # Docking diff --git a/src/hello_imgui/dpi_aware.h b/src/hello_imgui/dpi_aware.h index 9ca382e4..82c6669a 100644 --- a/src/hello_imgui/dpi_aware.h +++ b/src/hello_imgui/dpi_aware.h @@ -3,6 +3,75 @@ namespace HelloImGui { +// @@md#DpiAwareParams + +// +// Hello ImGui will try its best to automatically handle DPI scaling for you. +// It does this by setting two values: +// +// - `dpiWindowSizeFactor`: +// factor by which window size should be multiplied +// +// - `fontRenderingScale`: +// factor by which fonts glyphs should be scaled at rendering time +// (typically 1 on windows, and 0.5 on macOS retina screens) +// +// By default, Hello ImGui will compute them automatically, +// when dpiWindowSizeFactor and fontRenderingScale are set to 0. +// +// How to set those values manually: +// --------------------------------- +// If it fails (i.e. your window and/or fonts are too big or too small), +// you may set them manually: +// (1) Either by setting them programmatically in your application +// (set their values in `runnerParams.dpiAwareParams`) +// (2) Either by setting them in the app ini file +// (3) Either by setting them in a `hello_imgui.ini` file in the current folder, or any of its parent folders. +// (this is useful when you want to set them for a specific app or set of apps, without modifying the app code) +// Note: if several methods are used, the order of priority is (1) > (2) > (3) +// +// Example content of a ini file: +// ------------------------------ +// [DpiAwareParams] +// dpiWindowSizeFactor=2 +// fontRenderingScale=0.5 +// +struct DpiAwareParams +{ + // `dpiWindowSizeFactor` + // factor by which window size should be multiplied to get a similar + // visible size on different OSes. + // In a standard environment (i.e. outside of Hello ImGui), an application with a size of 960x480 pixels, + // may have a physical size (in mm or inches) that varies depending on the screen DPI, and the OS. + // + // Inside Hello ImGui, the window size is always treated as targeting a 96 PPI screen, so that its size will + // look similar whatever the OS and the screen DPI. + // In our example, our 960x480 pixels window will try to correspond to a 10x5 inches window + // + // Hello ImGui does its best to compute it on all OSes. + // However, if it fails you may set its value manually. + // If it is set to 0, Hello ImGui will compute it automatically, + // and the resulting value will be stored in `dpiWindowSizeFactor`. + float dpiWindowSizeFactor = 0.0f; + + // `fontRenderingScale` + // factor (that is either 1 or < 1.) by which fonts glyphs should be + // scaled at rendering time. + // On macOS retina screens, it will be 0.5, since macOS APIs hide + // the real resolution of the screen. + float fontRenderingScale = 0.0f; + + // `dpiFontLoadingFactor` + // factor by which font size should be multiplied at loading time to get a similar + // visible size on different OSes. + // The size will be equivalent to a size given for a 96 PPI screen + float DpiFontLoadingFactor() { return dpiWindowSizeFactor / fontRenderingScale;}; +}; + +// ---------------------------------------------------------------------------- + +// @@md + /** @@md#DocEmToVec2 @@ -35,6 +104,11 @@ float EmSize(float nbLines); } // namespace HelloImGui +// ---------------------------------------------------------------------------- + +// +// Legacy API, you should use RunnerParams.dpAwareParams instead +// namespace HelloImGui { // Multiply font sizes by this factor when loading fonts manually with ImGui::GetIO().Fonts->AddFont... diff --git a/src/hello_imgui/impl/dpi_aware.cpp b/src/hello_imgui/impl/dpi_aware.cpp index 256767b7..fcc05f2c 100644 --- a/src/hello_imgui/impl/dpi_aware.cpp +++ b/src/hello_imgui/impl/dpi_aware.cpp @@ -37,24 +37,17 @@ namespace HelloImGui float DpiWindowSizeFactor() { - auto runner = GetAbstractRunner(); - IM_ASSERT(runner != nullptr); - float dpiFactor = runner->DpiWindowSizeFactor(); - return dpiFactor; + return GetRunnerParams()->dpiAwareParams.dpiWindowSizeFactor; } float DpiFontLoadingFactor() { - auto runner = GetAbstractRunner(); - IM_ASSERT(runner != nullptr); - return runner->DpiFontLoadingFactor(); + return GetRunnerParams()->dpiAwareParams.DpiFontLoadingFactor(); } float ImGuiDefaultFontGlobalScale() { - auto runner = GetAbstractRunner(); - IM_ASSERT(runner != nullptr); - return runner->ImGuiDefaultFontGlobalScale(); + return GetRunnerParams()->dpiAwareParams.fontRenderingScale; } } \ No newline at end of file diff --git a/src/hello_imgui/internal/backend_impls/abstract_runner.cpp b/src/hello_imgui/internal/backend_impls/abstract_runner.cpp index 3e2cb7cf..4ab09497 100644 --- a/src/hello_imgui/internal/backend_impls/abstract_runner.cpp +++ b/src/hello_imgui/internal/backend_impls/abstract_runner.cpp @@ -6,6 +6,7 @@ #include "hello_imgui/internal/hello_imgui_ini_settings.h" #include "hello_imgui/internal/menu_statusbar.h" #include "hello_imgui/internal/platform/ini_folder_locations.h" +#include "hello_imgui/internal/inicpp.h" #include "imgui.h" #include "hello_imgui/internal/imgui_global_context.h" // must be included before imgui_internal.h @@ -24,6 +25,7 @@ #include #include #include +#include #if __APPLE__ #include @@ -175,57 +177,176 @@ bool AbstractRunner::ShallSizeWindowRelativeTo96Ppi() return shallSizeRelativeTo96Ppi; } -float AbstractRunner::ImGuiDefaultFontGlobalScale() + +void ReadDpiAwareParams(const std::string& appIniSettingLocation, DpiAwareParams* dpiAwareParams) { - float fontSizeIncreaseFactor = 1.f; + // cf dpi_aware.h + // + // Hello ImGui will try its best to automatically handle DPI scaling for you. + // It does this by setting two values: + // + // - `dpiWindowSizeFactor`: + // factor by which window size should be multiplied + // + // - `fontRenderingScale`: + // factor by which fonts glyphs should be scaled at rendering time + // (typically 1 on windows, and 0.5 on macOS retina screens) + // + // By default, Hello ImGui will compute them automatically, + // when dpiWindowSizeFactor and fontRenderingScale are set to 0. + // + // How to set those values manually: + // --------------------------------- + // If it fails (i.e. your window and/or fonts are too big or too small), + // you may set them manually: + // (1) Either by setting them programmatically in your application + // (set their values in `runnerParams.dpiAwareParams`) + // (2) Either by setting them in the app ini file + // (3) Either by setting them in a `hello_imgui.ini` file in the current folder, or any of its parent folders. + // (this is useful when you want to set them for a specific app or set of apps, without modifying the app code) + // Note: if several methods are used, the order of priority is (1) > (2) > (3) + // + // Example content of a ini file: + // ------------------------------ + // [DpiAwareParams] + // dpiWindowSizeFactor=2 + // fontRenderingScale=0.5 + // -#ifdef __EMSCRIPTEN__ - // Query the brower to ask for devicePixelRatio - double windowDevicePixelRatio = EM_ASM_DOUBLE( { - scale = window.devicePixelRatio; - return scale; - } - ); - printf("window.devicePixelRatio=%lf\n", windowDevicePixelRatio); + auto folderAndAllParents = [](const std::string& folder) -> std::vector + { + std::vector result; + std::string currentFolder = folder; + while (true) + { + result.push_back(currentFolder); + std::filesystem::path p(currentFolder); + if (p.has_parent_path() && (p.parent_path() != p)) + currentFolder = p.parent_path().string(); + else + break; + } + return result; + }; - fontSizeIncreaseFactor = windowDevicePixelRatio; -#endif -#ifdef HELLOIMGUI_MACOS - // Crisp fonts on macOS: - // cf https://github.com/ocornut/imgui/issues/5301 - // Issue with macOS is that it pretends screen has 2x fewer pixels than it actually does. - // This simplifies application development in most cases, but in our case we happen to render fonts at 1x scale - // while screen renders at 2x scale. - fontSizeIncreaseFactor = (float) NSScreen.mainScreen.backingScaleFactor; -#endif + auto readIniFloatValue = [] + (const std::string& iniFilePath, const std::string& sectionName, const std::string& valuNeame) -> std::optional + { + if (! std::filesystem::exists(iniFilePath)) + return std::nullopt; + if (! std::filesystem::is_regular_file(iniFilePath)) + return std::nullopt; - float defaultFontGlobalScale = 1.0f / fontSizeIncreaseFactor; - return defaultFontGlobalScale; -} + try + { + std::optional result; + ini::IniFile ini; + ini.load(iniFilePath); + if (ini.find(sectionName) != ini.end()) + { + auto& section = ini.at(sectionName); + if (section.find(valuNeame) != section.end()) + result = ini[sectionName][valuNeame].as(); + } + return result; + } + catch(...) + { + return std::nullopt; + } + }; -float AbstractRunner::DpiWindowSizeFactor() -{ -#ifdef HELLOIMGUI_HAS_DIRECTX11 - return 1.f; // The current implementation of Dx11 backend does not support changing the window size -#endif - float r = mBackendWindowHelper->GetWindowSizeDpiScaleFactor(mWindow); - return r; + auto allIniFilesToSearch = [&]() -> std::vector + { + std::vector allIniFileToSearch; + allIniFileToSearch.push_back(appIniSettingLocation); + + std::string currentFolder = std::filesystem::current_path().string(); + for (const auto& folder: folderAndAllParents(currentFolder)) + allIniFileToSearch.push_back(folder + "/hello_imgui.ini"); + + return allIniFileToSearch; + }; + + std::vector allIniFiles = allIniFilesToSearch(); + + if (dpiAwareParams->dpiWindowSizeFactor == 0.f) + { + for (const auto& iniFile: allIniFiles) + { + auto dpiWindowSizeFactor = readIniFloatValue(iniFile, "DpiAwareParams", "dpiWindowSizeFactor"); + if (dpiWindowSizeFactor.has_value()) + { + dpiAwareParams->dpiWindowSizeFactor = dpiWindowSizeFactor.value(); + break; + } + } + } + + if (dpiAwareParams->fontRenderingScale == 0.f) + { + for (const auto& iniFile: allIniFiles) + { + auto fontRenderingScale = readIniFloatValue(iniFile, "DpiAwareParams", "fontRenderingScale"); + if (fontRenderingScale.has_value()) + { + dpiAwareParams->fontRenderingScale = fontRenderingScale.value(); + break; + } + } + } } -// If we want a font to visually render like a font size of 14, -// we need to multiply its size by this factor -float AbstractRunner::DpiFontLoadingFactor() +void AbstractRunner::SetupDpiAwareParams() { - float k = DpiWindowSizeFactor() * 1.f / ImGui::GetIO().FontGlobalScale; - return k; + ReadDpiAwareParams(IniSettingsLocation(params), ¶ms.dpiAwareParams); + if (params.dpiAwareParams.dpiWindowSizeFactor == 0.f) + { + #ifdef HELLOIMGUI_HAS_DIRECTX11 + // The current implementation of Dx11 backend does not support changing the window size + params.dpiAwareParams.dpiWindowSizeFactor = 1.f; + #endif + params.dpiAwareParams.dpiWindowSizeFactor = mBackendWindowHelper->GetWindowSizeDpiScaleFactor(mWindow); + } + + if (params.dpiAwareParams.fontRenderingScale == 0.f) + { + float fontSizeIncreaseFactor = 1.f; + + #ifdef __EMSCRIPTEN__ + // Query the brower to ask for devicePixelRatio + double windowDevicePixelRatio = EM_ASM_DOUBLE( { + scale = window.devicePixelRatio; + return scale; + } + ); + printf("window.devicePixelRatio=%lf\n", windowDevicePixelRatio); + + fontSizeIncreaseFactor = windowDevicePixelRatio; + #endif + #ifdef HELLOIMGUI_MACOS + // Crisp fonts on macOS: + // cf https://github.com/ocornut/imgui/issues/5301 + // Issue with macOS is that it pretends screen has 2x fewer pixels than it actually does. + // This simplifies application development in most cases, but in our case we happen to render fonts at 1x scale + // while screen renders at 2x scale. + fontSizeIncreaseFactor = (float) NSScreen.mainScreen.backingScaleFactor; + #endif + + params.dpiAwareParams.fontRenderingScale = 1.0f / fontSizeIncreaseFactor; + } + + ImGui::GetIO().FontGlobalScale = params.dpiAwareParams.fontRenderingScale; + ImGui::GetIO().DisplayFramebufferScale = mBackendWindowHelper->GetWindowScaleFactor(mWindow); } + void AbstractRunner::MakeWindowSizeRelativeTo96Ppi_IfRequired() { if (ShallSizeWindowRelativeTo96Ppi()) { - float scaleFactor = DpiWindowSizeFactor(); + float scaleFactor = params.dpiAwareParams.dpiWindowSizeFactor; if (scaleFactor != 1.f) { auto bounds = mBackendWindowHelper->GetWindowBounds(mWindow); @@ -263,7 +384,7 @@ void AbstractRunner::HandleDpiOnSecondFrame() // High DPI handling on windows & linux { - float dpiScale = DpiWindowSizeFactor(); + float dpiScale = params.dpiAwareParams.dpiWindowSizeFactor; if ( dpiScale > 1.f) { ImGuiStyle& style = ImGui::GetStyle(); @@ -516,7 +637,7 @@ void AbstractRunner::Setup() Impl_SetWindowIcon(); - ImGui::GetIO().DisplayFramebufferScale = mBackendWindowHelper->GetWindowScaleFactor(mWindow); + SetupDpiAwareParams(); // This should be done before Impl_LinkPlatformAndRenderBackends() // because, in the case of glfw ImGui_ImplGlfw_InstallCallbacks @@ -541,8 +662,6 @@ void AbstractRunner::Setup() // load fonts & set ImGui::GetIO().FontGlobalScale // ImGui::GetIO().Fonts->Clear(); - // (On macOS with HighDPI) FontGlobalScale may be set to 0.5 - ImGui::GetIO().FontGlobalScale = this->ImGuiDefaultFontGlobalScale(); // LoadAdditionalFonts will load fonts and resize them by 1./FontGlobalScale // (if and only if it uses HelloImGui::LoadFontTTF instead of ImGui's font loading functions) params.callbacks.LoadAdditionalFonts(); diff --git a/src/hello_imgui/internal/backend_impls/abstract_runner.h b/src/hello_imgui/internal/backend_impls/abstract_runner.h index ffd918d8..aea7895e 100644 --- a/src/hello_imgui/internal/backend_impls/abstract_runner.h +++ b/src/hello_imgui/internal/backend_impls/abstract_runner.h @@ -37,16 +37,6 @@ class AbstractRunner // For jupyter notebook, which displays a screenshot post execution ImageBuffer ScreenshotRgb() { return mRenderingBackendCallbacks->Impl_ScreenshotRgb_3D(); } - // - // Dpi related methods - // - // DpiWindowSizeFactor() is the factor by which window size should be multiplied to get a similar visible size on different OSes. - // It returns ApplicationScreenPixelPerInch / 96 under windows and linux. Under macOS, it will return 1. - float DpiWindowSizeFactor(); - // If we want a font to visually render like a font size of 14 we need to multiply its size by this factor - float DpiFontLoadingFactor(); - // returns the default value that should be stored inside `ImGui::GetIO().FontGlobalScale` - static float ImGuiDefaultFontGlobalScale(); void LayoutSettings_SwitchLayout(const std::string& layoutName); @@ -92,6 +82,7 @@ class AbstractRunner void SetImGuiPrefs(); void InitRenderBackendCallbacks(); + void SetupDpiAwareParams(); void PrepareWindowGeometry(); void HandleDpiOnSecondFrame(); void ReloadFontIfFailed() const; diff --git a/src/hello_imgui/runner_params.h b/src/hello_imgui/runner_params.h index 7dfb469e..99209548 100644 --- a/src/hello_imgui/runner_params.h +++ b/src/hello_imgui/runner_params.h @@ -5,6 +5,7 @@ #include "hello_imgui/docking_params.h" #include "hello_imgui/backend_pointers.h" #include "hello_imgui/renderer_backend_options.h" +#include "hello_imgui/dpi_aware.h" #include // #define HELLOIMGUI_DISABLE_OBSOLETE_BACKEND @@ -238,6 +239,8 @@ struct RunnerParams // (set fpsIdling.enableIdling to false to disable Idling) FpsIdling fpsIdling; + // --------------- DPI Handling ----------- + DpiAwareParams dpiAwareParams; // --------------- Misc -------------------