mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-18 08:07:26 +00:00
Merge pull request #2363 from TrevorBrown-dev/feat/variables-window-resize-2354
M3: the Local variables window resizes from its edges and corners (#2354)
This commit is contained in:
4 files changed
+154
-7
No files matched your search
@@ -239,6 +239,17 @@ impl<'a> View<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The smallest the window can be dragged to: wide enough that the table's header row holds the
|
||||
/// search field and a library collection's "Extend collection" button side by side.
|
||||
const MIN_SIZE: egui::Vec2 = vec2(600.0, 240.0);
|
||||
|
||||
/// 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).
|
||||
pub fn show(app: &mut FigApp, ctx: &Context) {
|
||||
let Some(mut st) = app.ui.variables.take() else { return };
|
||||
@@ -261,22 +272,30 @@ 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(480.0), 480.0_f32.min(screen.height() - 120.0).max(240.0));
|
||||
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"))
|
||||
.title_bar(false)
|
||||
.collapsible(false)
|
||||
.resizable(false)
|
||||
.fixed_size(size)
|
||||
.pivot(Align2::CENTER_CENTER)
|
||||
.default_pos(screen.center())
|
||||
.resizable(true)
|
||||
.default_size(size)
|
||||
.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);
|
||||
ui.set_min_size(size);
|
||||
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
|
||||
header(app, ui, &mut open);
|
||||
let h = (size.y - 44.0).max(160.0);
|
||||
// The body below the 44-point header: what is left of the window (the table scrolls).
|
||||
let h = (size.y - 44.0).max(0.0);
|
||||
let top_down = egui::Layout::top_down(egui::Align::Min);
|
||||
ui.horizontal_top(|ui| {
|
||||
ui.allocate_ui_with_layout(vec2(SIDEBAR_W, h), top_down, |ui| sidebar(app, ui, &views, &doc.variables, &mut st));
|
||||
|
||||
@@ -296,6 +296,8 @@ mod variables_rename_tests;
|
||||
#[cfg(test)]
|
||||
mod variables_select_tests;
|
||||
#[cfg(test)]
|
||||
mod variables_window_resize_tests;
|
||||
#[cfg(test)]
|
||||
mod variants_discovery_tests;
|
||||
#[cfg(test)]
|
||||
mod vector_keys_tests;
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
//! The Local variables window resizes from its edges and corners, height as well as width, stops at
|
||||
//! a minimum size, and keeps its size when it is closed and opened again (#2354).
|
||||
|
||||
use egui::{Event, Id, Pos2, Rect, vec2};
|
||||
use figcraft_engine::Session;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::layers_drag_tests::Ui;
|
||||
use crate::live_tests::{flat, texts};
|
||||
|
||||
const COUNT: usize = 60;
|
||||
|
||||
/// The window open on a collection of many number variables.
|
||||
fn window() -> Ui {
|
||||
let mut s = Session::new();
|
||||
s.execute("variable.createCollection", &json!({"name": "Theme"})).unwrap();
|
||||
for k in 0..COUNT {
|
||||
s.execute("variable.create", &json!({"collection": "Theme", "name": format!("v{k:02}"), "type": "number", "value": 1})).unwrap();
|
||||
}
|
||||
let mut u = Ui::new(s);
|
||||
u.app.ui.variables = Some(Default::default());
|
||||
for _ in 0..10 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
u
|
||||
}
|
||||
|
||||
fn rect(u: &Ui) -> Rect {
|
||||
u.ctx.memory(|m| m.area_rect(Id::unique("variables_window"))).expect("the variables window has an area")
|
||||
}
|
||||
|
||||
/// How many variable rows are drawn inside the window (the table clips the rest).
|
||||
fn rows(u: &mut Ui) -> usize {
|
||||
let window = rect(u);
|
||||
let shapes = flat(&u.frame_output(vec![]).shapes);
|
||||
texts(&shapes).iter().filter(|(t, p, _)| t.len() == 3 && t.starts_with('v') && window.contains(*p)).count()
|
||||
}
|
||||
|
||||
/// Drag the bottom-right corner by `by` and let go.
|
||||
fn drag_corner(u: &mut Ui, by: egui::Vec2) {
|
||||
let from = rect(u).right_bottom();
|
||||
u.drag_to(from, from + by);
|
||||
let to: Pos2 = from + by;
|
||||
u.frame(vec![Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() }]);
|
||||
for _ in 0..5 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_variables_window_grows_when_dragged_taller() {
|
||||
let mut u = window();
|
||||
let before = rect(&u);
|
||||
let rows_before = rows(&mut u);
|
||||
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 on_a_short_screen_the_variables_window_fits_inside_it() {
|
||||
// Lower than the minimum (240): the body takes what is left below the header and scrolls.
|
||||
let mut s = Session::new();
|
||||
s.execute("variable.createCollection", &json!({"name": "Theme"})).unwrap();
|
||||
for k in 0..COUNT {
|
||||
s.execute("variable.create", &json!({"collection": "Theme", "name": format!("v{k:02}"), "type": "number", "value": 1})).unwrap();
|
||||
}
|
||||
let mut u = Ui::new(s);
|
||||
u.screen = vec2(800.0, 180.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();
|
||||
drag_corner(&mut u, vec2(-2000.0, -2000.0));
|
||||
let r = rect(&u);
|
||||
assert!((r.width() - 600.0).abs() < 3.0, "width {}", r.width());
|
||||
assert!((r.height() - 240.0).abs() < 3.0, "height {}", r.height());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_resized_variables_window_keeps_its_size_when_reopened() {
|
||||
let mut u = window();
|
||||
drag_corner(&mut u, vec2(0.0, 150.0));
|
||||
let resized = rect(&u);
|
||||
let close = resized.right_top() + vec2(-22.0, 22.0);
|
||||
u.button(close, true);
|
||||
u.button(close, false);
|
||||
for _ in 0..5 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
assert!(u.app.ui.variables.is_none(), "the Close button closed the window");
|
||||
u.app.ui.variables = Some(Default::default());
|
||||
for _ in 0..10 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
let again = rect(&u);
|
||||
assert!((again.width() - resized.width()).abs() < 1.0 && (again.height() - resized.height()).abs() < 1.0, "{resized:?} -> {again:?}");
|
||||
}
|
||||
@@ -65,7 +65,7 @@ updates with overrides kept: `library_tests.rs`, `components_scale_tests.rs`.
|
||||
| Editing a style updates its layers; detach and delete keep values; editing a styled property unlinks that style | done | an edited fill/stroke/effect/grid list or text chars unlink; applying a style replaces variables bound to what it sets; text styles on text layers only |
|
||||
| Organising styles: folders by `/` names, order, duplicate, rename folder, Local styles list | done | `style.move` (into a folder renames), `style.duplicate` (right below), `style.renameFolder`; the page's Local styles list by type and folder (drag to reorder, right-click) |
|
||||
| Variables inside styles (paint, effect, grid and text styles; each user resolves them in its own modes) | done | `style.bind`, `style_vars_tests`; the editor binds paint colours, shadow colours, blur radii, grid sizes and a text style's font, font style (a face name from a string variable, or a numeric weight from a number variable, which picks the nearest face and a variable font's `wght` axis), size, line height and letter spacing, the rest through the command; a bound line height is applied in pixels, as the reference tool does (measured 2026-11-10) |
|
||||
| Variables: colour, number, string, boolean; collections, modes, aliases, scopes, groups | done | behaviour suite: `variables_behaviour_tests.rs`; aliases same type, no loops (also on create); colour calculations per mode (`{alpha}`: a colour variable with its alpha replaced, `{mix}`: a per-channel blend of two colour variables or literals; they compose, can't loop, and bound layers, modes and undo follow them; REST, CSS, Penpot and `.fig` exports write the colour they show in each of their collection's modes, other collections in their default modes; read-only in the panel, which can swap one for an alias or detach it; `variables_colour_tests.rs`), beyond the incumbent, whose variables only alias; scopes filter pickers only; several variables can be selected in the editor (Shift and Cmd/Ctrl click) and deleted, moved to a group or rescoped at once; a group can be duplicated from its row's menu (Duplicate group, one undo step) |
|
||||
| Variables: colour, number, string, boolean; collections, modes, aliases, scopes, groups | done | behaviour suite: `variables_behaviour_tests.rs`; aliases same type, no loops (also on create); colour calculations per mode (`{alpha}`: a colour variable with its alpha replaced, `{mix}`: a per-channel blend of two colour variables or literals; they compose, can't loop, and bound layers, modes and undo follow them; REST, CSS, Penpot and `.fig` exports write the colour they show in each of their collection's modes, other collections in their default modes; read-only in the panel, which can swap one for an alias or detach it; `variables_colour_tests.rs`), beyond the incumbent, whose variables only alias; scopes filter pickers only; several variables can be selected in the editor (Shift and Cmd/Ctrl click) and deleted, moved to a group or rescoped at once; a group can be duplicated from its row's menu (Duplicate group, one undo step); the window resizes from its edges and corners (minimum 600 x 240) and keeps its size and place while the app runs, #2354 |
|
||||
| Modes: add, duplicate, rename, delete, set as default / reorder | done | layers on Auto follow a new default; explicit modes stay |
|
||||
| Deleting a bound variable or its collection | done | layers keep their last value and are unbound; aliases to it take its values and colour calculations reading it keep the colour they show |
|
||||
| Code syntax per platform (Web, iOS, Android) | done | generated CSS uses the Web name, SwiftUI the iOS one and Compose the Android one (a variable without one keeps its value there); in the variables JSON; ArkUI and Flutter have no platform of their own and write values |
|
||||
@@ -80,6 +80,7 @@ updates with overrides kept: `library_tests.rs`, `components_scale_tests.rs`.
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-11-14 | patch | M3, #2354: the Local variables window resizes from its edges and corners, width and height (minimum 600 x 240), and keeps its size and place while the app runs. |
|
||||
| 2026-11-13 | patch | Rows that recent merges had duplicated or garbled are merged back into one row each (no behaviour change): Create component, instances, detach, reset, go to main, restore main, push changes. |
|
||||
| 2026-11-14 | patch | M3, #2353: renaming a group in the Local variables window to a path with `/` nests it, as in Figma: `Orange` renamed to `Palette/Orange` moves its variables to `Palette/Orange/100`… and the sidebar shows Orange indented under a new Palette group, where the slash used to become a hyphen (`Palette-Orange`). The typed path replaces the group's last part (a nested group stays in its parent), each part is trimmed and empty parts are dropped; a rename onto a variable name that stays is refused with nothing changed, and it is one undo step. Tests: `variables_select_tests::renaming_a_group_with_a_slash_nests_it_in_a_new_parent_group_in_one_undo_step` and the group rename tests beside it, `variables_panel::tests`. Grade unchanged. |
|
||||
| 2026-11-07 | minor | M3, #2084: a group's context menu in the Local variables window has Duplicate group (after Rename group, before Delete group). It adds a group named after the original plus " 2" (the next free number when taken: " 3"), whose variables are copies of the originals with the same leaf names, values in every mode, description, scopes, hidden-from-publishing and code syntax, each sent as `variable.duplicate` plus a rename in one `command.batch` labelled Duplicate group, so one undo step. Measured in the incumbent with one mode; an alias to a variable of the same group keeps pointing at the original. Ungroup is not built. Tests: `variables_select_tests` (headless UI). Grade unchanged |
|
||||
|
||||
Reference in new issue
Block a user