Scale ImGui windows when DPI changes.

Code is directly lifted from ImGui, with certain known caveats.
This commit is contained in:
Bartosz Taudul 2024-03-15 20:19:32 +01:00
parent f5f82a2bd5
commit 55f9341aec
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3

View File

@ -3,6 +3,7 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <inttypes.h> #include <inttypes.h>
#define IMGUI_DEFINE_MATH_OPERATORS 1
#include <imgui.h> #include <imgui.h>
#include <mutex> #include <mutex>
#include <stdint.h> #include <stdint.h>
@ -97,6 +98,7 @@ static double animTime = 0;
static float dpiScale = 1.f; static float dpiScale = 1.f;
static bool dpiScaleOverriddenFromEnv = false; static bool dpiScaleOverriddenFromEnv = false;
static float userScale = 1.f; static float userScale = 1.f;
static float prevScale = 1.f;
static Filters* filt; static Filters* filt;
static RunQueue mainThreadTasks; static RunQueue mainThreadTasks;
static uint32_t updateVersion = 0; static uint32_t updateVersion = 0;
@ -134,10 +136,21 @@ static void RunOnMainThread( const std::function<void()>& cb, bool forceDelay =
mainThreadTasks.Queue( cb, forceDelay ); mainThreadTasks.Queue( cb, forceDelay );
} }
static void ScaleWindow(ImGuiWindow* window, float scale)
{
ImVec2 origin = window->Viewport->Pos;
window->Pos = ImFloor((window->Pos - origin) * scale + origin);
window->Size = ImTrunc(window->Size * scale);
window->SizeFull = ImTrunc(window->SizeFull * scale);
window->ContentSize = ImTrunc(window->ContentSize * scale);
}
static void SetupDPIScale() static void SetupDPIScale()
{ {
auto scale = dpiScale * userScale; auto scale = dpiScale * userScale;
if( prevScale == scale ) return;
LoadFonts( scale ); LoadFonts( scale );
if( view ) view->UpdateFont( s_fixedWidth, s_smallFont, s_bigFont ); if( view ) view->UpdateFont( s_fixedWidth, s_smallFont, s_bigFont );
@ -166,6 +179,11 @@ static void SetupDPIScale()
stbir_resize_uint8( iconPx, iconX, iconY, 0, scaleIcon, ty, ty, 0, 4 ); stbir_resize_uint8( iconPx, iconX, iconY, 0, scaleIcon, ty, ty, 0, 4 );
tracy::UpdateTextureRGBA( iconTex, scaleIcon, ty, ty ); tracy::UpdateTextureRGBA( iconTex, scaleIcon, ty, ty );
delete[] scaleIcon; delete[] scaleIcon;
const auto ratio = scale / prevScale;
prevScale = scale;
auto ctx = ImGui::GetCurrentContext();
for( auto& w : ctx->Windows ) ScaleWindow( w, ratio );
} }
static void SetupScaleCallback( float scale ) static void SetupScaleCallback( float scale )