From 5b282ac8bbc92aeda86fe4f7ba34bba4e55f7881 Mon Sep 17 00:00:00 2001 From: tidely <43219534+tidely@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:38:13 +0000 Subject: [PATCH] gpui: Add `simulate_window_scale_factor_change` to `HeadlessAppContext` (#67113) ## Summary I was writing a skill for agents to iterate on their GPUI UI changes by screenshotting frames in a headless app context, and I ran into this missing function, which meant all headless renders used the same scale factor. There are likely many other functions that could be useful if forwarded, but this is the first one I ran into. ## Testing Ran a heavier test locally, but not worth pushing here ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - N/A - [GPUI] Added `simulate_window_scale_factor_change` to `HeadlessAppContext` --- crates/gpui/src/app/headless_app_context.rs | 11 ++++++++- crates/gpui/src/app/test_context.rs | 25 ++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/crates/gpui/src/app/headless_app_context.rs b/crates/gpui/src/app/headless_app_context.rs index c7f7c913451..3eb2ebeef9c 100644 --- a/crates/gpui/src/app/headless_app_context.rs +++ b/crates/gpui/src/app/headless_app_context.rs @@ -13,7 +13,7 @@ use crate::{ Context, Entity, EntityId, ForegroundExecutor, Global, Pixels, PlatformHeadlessRenderer, PlatformTextSystem, Render, Reservation, Size, Task, TestDispatcher, TestPlatform, TextSystem, Window, WindowBounds, WindowHandle, WindowOptions, - app::{GpuiBorrow, GpuiMode}, + app::{GpuiBorrow, GpuiMode, test_context::test_window}, }; use anyhow::Result; use image::RgbaImage; @@ -130,6 +130,15 @@ impl HeadlessAppContext { self.dispatcher.run_until_parked(); } + /// Simulates the window moving to a display with a different scale factor. + pub fn simulate_window_scale_factor_change( + &mut self, + window: AnyWindowHandle, + scale_factor: f32, + ) { + test_window(&self.app, window).simulate_scale_factor_change(scale_factor); + } + /// Advances the simulated clock. pub fn advance_clock(&self, duration: Duration) { self.dispatcher.advance_clock(duration); diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rs index 4e6bf835798..df3cce6f760 100644 --- a/crates/gpui/src/app/test_context.rs +++ b/crates/gpui/src/app/test_context.rs @@ -16,6 +16,19 @@ use std::{ cell::RefCell, future::Future, ops::Deref, path::PathBuf, rc::Rc, sync::Arc, time::Duration, }; +pub(crate) fn test_window(app: &AppCell, window: AnyWindowHandle) -> TestWindow { + app.borrow_mut() + .windows + .get_mut(window.id) + .expect("window not found") + .as_deref_mut() + .expect("window is being updated") + .platform_window + .as_test() + .expect("window is not a TestWindow") + .clone() +} + /// A TestAppContext is provided to tests created with `#[gpui::test]`, it provides /// an implementation of `Context` with additional methods that are useful in tests. #[derive(Clone)] @@ -670,17 +683,7 @@ impl TestAppContext { /// Returns the `TestWindow` backing the given handle. pub(crate) fn test_window(&self, window: AnyWindowHandle) -> TestWindow { - self.app - .borrow_mut() - .windows - .get_mut(window.id) - .unwrap() - .as_deref_mut() - .unwrap() - .platform_window - .as_test() - .unwrap() - .clone() + test_window(&self.app, window) } /// Returns a stream of notifications whenever the Entity is updated.