From 5d818306ef1a22f94ecaefee786f2e47ae8aaf4f Mon Sep 17 00:00:00 2001 From: Haroon Qureshi Date: Mon, 11 May 2026 07:43:29 -0700 Subject: [PATCH] Save/restore window size in UX settings. PiperOrigin-RevId: 913695007 Change-Id: I3366b11a5de53292d4ffe0e4c46f866381624788 --- src/experimental/platform/hal/window.cc | 12 ++++++++++++ src/experimental/platform/hal/window.h | 3 +++ src/experimental/studio/app.cc | 17 ++++++++++++++++- src/experimental/studio/app.h | 2 ++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/experimental/platform/hal/window.cc b/src/experimental/platform/hal/window.cc index 054fcb88..4d481b15 100644 --- a/src/experimental/platform/hal/window.cc +++ b/src/experimental/platform/hal/window.cc @@ -177,6 +177,18 @@ void Window::SetTitle(std::string_view title) { SDL_SetWindowTitle(sdl_window_, title.data()); } +void Window::Resize(int width, int height) { + SDL_SetWindowSize(sdl_window_, width, height); + SDL_SetWindowPosition(sdl_window_, SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED); + + SDL_GetWindowSize(sdl_window_, &width_, &height_); + int drawable_width = width_; + int drawable_height = height_; + SDL_GL_GetDrawableSize(sdl_window_, &drawable_width, &drawable_height); + scale_ = (float)drawable_width / (float)width_; +} + void Window::DisableWindowResizing() { SDL_SetWindowResizable(sdl_window_, SDL_FALSE); } diff --git a/src/experimental/platform/hal/window.h b/src/experimental/platform/hal/window.h index 4e9f504a..2cc61452 100644 --- a/src/experimental/platform/hal/window.h +++ b/src/experimental/platform/hal/window.h @@ -79,6 +79,9 @@ class Window { // Returns the graphics configuration of the window. GraphicsMode GetGraphicsMode() const; + // Resizes the window to the given width and height. + void Resize(int width, int height); + // Enables window resizing. void EnableWindowResizing(); diff --git a/src/experimental/studio/app.cc b/src/experimental/studio/app.cc index 44442808..ece64f22 100644 --- a/src/experimental/studio/app.cc +++ b/src/experimental/studio/app.cc @@ -104,6 +104,11 @@ void App::SwitchGraphicsMode(int width, int height, window_config); renderer_ = std::make_unique( window_->GetNativeWindowHandle(), gfx_mode_); + + LoadSettings(); + if (ui_.window_width > 0 && ui_.window_height > 0) { + window_->Resize(ui_.window_width, ui_.window_height); + } } void App::Recompile() { @@ -791,6 +796,10 @@ void App::LoadSettings() { void App::SaveSettings() { if (!ini_path_.empty()) { std::string settings = ImGui::SaveIniSettingsToMemory(); + if (window_) { + ui_.window_width = window_->GetWidth(); + ui_.window_height = window_->GetHeight(); + } platform::AppendIniSection(settings, "[Studio][UX]", ui_.ToDict()); platform::KeyValues plugin_names; @@ -1839,12 +1848,18 @@ App::UiState::Dict App::UiState::ToDict() const { return { {"theme", std::to_string(static_cast(theme))}, {"font_scale", std::to_string(font_scale)}, + {"window_width", std::to_string(window_width)}, + {"window_height", std::to_string(window_height)}, }; } void App::UiState::FromDict(const Dict& dict) { + using platform::ReadIniValue; + *this = UiState(); theme = ReadIniValue(dict, "theme", theme); - font_scale = platform::ReadIniValue(dict, "font_scale", font_scale); + window_width = ReadIniValue(dict, "window_width", window_width); + window_height = ReadIniValue(dict, "window_height", window_height); + font_scale = ReadIniValue(dict, "font_scale", font_scale); } } // namespace mujoco::studio diff --git a/src/experimental/studio/app.h b/src/experimental/studio/app.h index 4f2b734c..b38d551b 100644 --- a/src/experimental/studio/app.h +++ b/src/experimental/studio/app.h @@ -106,6 +106,8 @@ class App { int key_idx = 0; platform::GuiTheme theme = platform::GuiTheme::kLight; float font_scale = 1.0f; + int window_width = 0; + int window_height = 0; using Dict = std::unordered_map; Dict ToDict() const;