From 5a482af812cbab55c01cbc440cbf627903aaa3e1 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 14 Nov 2026 03:38:02 -0500 Subject: [PATCH] M3: the Local variables window fits a narrow screen and resizes from a fixed corner (#2354) The minimum (600 x 240) is capped at the screen less its margin, so on a screen narrower than that the window stays inside it. The window is pinned at its top-left corner and opens centred, so dragging one edge leaves the opposite one in place (the centre pivot moved both). Tests on_a_narrow_screen_the_variables_window_fits_inside_it (red before: the window ran from x -26 to 576 on a 550-wide screen) and the top-edge check in the_variables_window_grows_when_dragged_taller (red before: 210 -> 190); variables 44 passed, clippy clean (CodeRabbit on #2363). Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs/variables_panel.rs | 23 +++++++++++-------- .../src/variables_window_resize_tests.rs | 17 ++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/dialogs/variables_panel.rs b/crates/ui-egui/src/dialogs/variables_panel.rs index 4e3b1e541..d018e7f2f 100644 --- a/crates/ui-egui/src/dialogs/variables_panel.rs +++ b/crates/ui-egui/src/dialogs/variables_panel.rs @@ -243,9 +243,11 @@ impl<'a> View<'a> { /// search field and a library collection's "Extend collection" button side by side. const MIN_SIZE: egui::Vec2 = vec2(600.0, 240.0); -/// The largest the window can be dragged to: the screen less a margin. -fn max_size(screen: Rect) -> egui::Vec2 { - (screen.size() - vec2(16.0, 16.0)).max(MIN_SIZE) +/// The smallest and largest the window can be: at most the screen less a margin, and at least +/// [`MIN_SIZE`] unless the screen is smaller than that (then the screen less the margin). +fn limits(screen: Rect) -> (egui::Vec2, egui::Vec2) { + let max = (screen.size() - vec2(16.0, 16.0)).max(vec2(1.0, 1.0)); + (MIN_SIZE.min(max), max) } /// Show the editor while it's open (call once per frame). @@ -270,7 +272,8 @@ pub fn show(app: &mut FigApp, ctx: &Context) { st.collection = coll.map(|c| c.id.clone()); let t = Tokens::get(ctx); let screen = ctx.content_rect(); - let size = vec2(880.0_f32.min(screen.width() - 80.0).max(MIN_SIZE.x), 480.0_f32.min(screen.height() - 120.0).max(MIN_SIZE.y)); + let (min, max) = limits(screen); + let size = vec2(880.0_f32.min(screen.width() - 80.0), 480.0_f32.min(screen.height() - 120.0)).clamp(min, max); let mut open = true; egui::Window::new(tr("Local variables")) .id(Id::unique("variables_window")) @@ -278,14 +281,16 @@ pub fn show(app: &mut FigApp, ctx: &Context) { .collapsible(false) .resizable(true) .default_size(size) - .min_size(MIN_SIZE) - .max_size(max_size(screen)) - .pivot(Align2::CENTER_CENTER) - .default_pos(screen.center()) + .min_size(min) + .max_size(max) + // Pinned at its top-left corner, so dragging one edge leaves the opposite one in place; + // it opens centred. + .pivot(Align2::LEFT_TOP) + .default_pos((screen.center() - size / 2.0).max(screen.min)) .frame(egui::Frame::window(&ctx.global_style()).fill(t.panel).inner_margin(egui::Margin::same(0))) .show(ctx, |ui| { // The window's current size (its default until the user drags an edge or corner). - let size = ui.available_size().max(MIN_SIZE); + let size = ui.available_size().max(min); ui.set_min_size(size); ui.spacing_mut().item_spacing = vec2(0.0, 0.0); header(app, ui, &mut open); diff --git a/crates/ui-egui/src/variables_window_resize_tests.rs b/crates/ui-egui/src/variables_window_resize_tests.rs index b08d2ace1..e2dedae75 100644 --- a/crates/ui-egui/src/variables_window_resize_tests.rs +++ b/crates/ui-egui/src/variables_window_resize_tests.rs @@ -55,10 +55,27 @@ fn the_variables_window_grows_when_dragged_taller() { drag_corner(&mut u, vec2(0.0, 200.0)); let after = rect(&u); assert!((after.height() - before.height() - 200.0).abs() < 2.0, "height {} -> {}", before.height(), after.height()); + assert!((after.min.y - before.min.y).abs() < 2.0, "the top edge stays where it was: {} -> {}", before.min.y, after.min.y); let rows_after = rows(&mut u); assert!(rows_after > rows_before, "the table shows more rows: {rows_before} -> {rows_after}"); } +#[test] +fn on_a_narrow_screen_the_variables_window_fits_inside_it() { + // Narrower than the minimum (600): the window takes the screen less its margin instead. + let mut s = Session::new(); + s.execute("variable.createCollection", &json!({"name": "Theme"})).unwrap(); + let mut u = Ui::new(s); + u.screen = vec2(550.0, 700.0); + u.app.ui.variables = Some(Default::default()); + for _ in 0..10 { + u.frame(vec![]); + } + let r = rect(&u); + let screen = Rect::from_min_size(Pos2::ZERO, u.screen); + assert!(screen.contains_rect(r), "the window {r:?} is inside the screen {screen:?}"); +} + #[test] fn the_variables_window_keeps_a_minimum_size() { let mut u = window();