Fix scaling issues on HDPI monitors.

PiperOrigin-RevId: 857024487
Change-Id: I7d3db9ff754374053bcd33ba93c7466253afb58c
This commit is contained in:
Haroon Qureshi
2026-01-16 01:16:50 -08:00
committed by Copybara-Service
parent c08e1d9e6e
commit 77d1ac10fe
3 changed files with 32 additions and 17 deletions
+7 -6
View File
@@ -30,12 +30,6 @@
namespace mujoco::platform {
static constexpr int kToolsBarHeight = 48;
static constexpr int kStatusBarHeight = 32;
static constexpr float kOptionsRelWidth = 0.22f;
static constexpr float kInspectorRelWidth = 0.22f;
static constexpr float kStatsRelHeight = 0.3f;
static ImVec2 GetFlexElementSize(int num_cols) {
const float width = (ImGui::GetContentRegionAvail().x / num_cols) -
ImGui::GetStyle().FramePadding.x * 2;
@@ -205,6 +199,13 @@ void SetupTheme(GuiTheme theme) {
ImVec4 ConfigureDockingLayout() {
ImGuiViewport* viewport = ImGui::GetMainViewport();
const float scale = ImGui::GetWindowDpiScale();
const float kOptionsRelWidth = 0.22f;
const float kInspectorRelWidth = 0.22f;
const float kStatsRelHeight = 0.3f;
const float kToolsBarHeight = 48.f * scale;
const float kStatusBarHeight = 32.f * scale;
const ImVec2 dockspace_pos{
viewport->WorkPos.x,
+14 -5
View File
@@ -40,16 +40,23 @@ extern void* GetNativeWindowOsx(void* window);
namespace mujoco::platform {
static void InitImGui(SDL_Window* window, bool load_fonts, bool build_fonts) {
static void InitImGui(SDL_Window* window, float content_scale, bool load_fonts,
bool build_fonts) {
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.IniFilename = nullptr;
io.ConfigDpiScaleFonts = true;
io.ConfigDpiScaleViewports = true;
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForOther(window);
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(content_scale);
style.FontScaleDpi = content_scale;
if (load_fonts) {
mjResource* font = nullptr;
int size = 0;
@@ -113,14 +120,16 @@ Window::Window(std::string_view title, int width, int height, Config config)
mju_error("Unsupported window config: %d", render_config);
}
sdl_window_ =
SDL_CreateWindow(title.data(), SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, width, height, window_flags);
const float content_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
sdl_window_ = SDL_CreateWindow(title.data(), SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, width * content_scale,
height * content_scale, window_flags);
if (!sdl_window_) {
mju_error("Error creating window: %s", SDL_GetError());
}
InitImGui(sdl_window_, config.load_fonts, (render_config != kClassicOpenGL));
InitImGui(sdl_window_, content_scale, config.load_fonts,
(render_config != kClassicOpenGL));
if (render_config == kFilamentWebGL || render_config == kClassicOpenGL) {
SDL_GLContext gl_context = SDL_GL_CreateContext(sdl_window_);