M2: align canvas padding bands with inside strokes

This commit is contained in:
Carbocode committed 2026-11-09 12:19:04 +01:00
1 parent 3839fe4a0c
commit 71229fa8da
4 files changed
+88 -10

No files matched your search

+14 -8
View File
@@ -350,13 +350,17 @@ fn al_zones(app: &FigApp) -> Option<Zones> {
app.to_screen(p.x, p.y)
};
let band = |x0: f64, y0: f64, x1: f64, y1: f64| Rect::from_two_pos(to(x0, y0), to(x1, y1));
let [pt, pr, pb, pl] = al.padding;
let [pt, pr, pb, pl] = al.padding.map(|v| if v.is_finite() { v.max(0.0) } else { 0.0 });
let (it, ir, ib, il) = figcraft_layout::insets(n, al);
let (fw, fh) = (n.width, n.height);
// Padding starts at the inside edge of an included stroke, as the layout's content does.
// The bands exclude the stroke itself so their labels and drags still edit only padding.
let (left, top, right, bottom) = (il - pl, it - pt, fw - ir + pr, fh - ib + pb);
let bands = vec![
(0, band(0.0, 0.0, fw, pt.max(4.0)), pt),
(1, band(fw - pr.max(4.0), 0.0, fw, fh), pr),
(2, band(0.0, fh - pb.max(4.0), fw, fh), pb),
(3, band(0.0, 0.0, pl.max(4.0), fh), pl),
(0, band(left, top, right, top + pt.max(4.0)), pt),
(1, band(right - pr.max(4.0), top, right, bottom), pr),
(2, band(left, bottom - pb.max(4.0), right, bottom), pb),
(3, band(left, top, left + pl.max(4.0), bottom), pl),
];
// An auto gap shows (and is dragged from) the distance the layout gives it; the drag makes it a
// fixed gap again, as typing one does.
@@ -369,8 +373,8 @@ fn al_zones(app: &FigApp) -> Option<Zones> {
let mut tracks = Vec::new();
if let Some(g) = figcraft_layout::grid_geometry(doc, id) {
// Grid: bands between columns (column gap) and between rows (row gap), and the tracks.
let (top, bottom) = (g.rows.first().map(|r| r.0).unwrap_or(pt), g.rows.last().map(|r| r.1).unwrap_or(fh - pb));
let (left, right) = (g.columns.first().map(|c| c.0).unwrap_or(pl), g.columns.last().map(|c| c.1).unwrap_or(fw - pr));
let (top, bottom) = (g.rows.first().map(|r| r.0).unwrap_or(it), g.rows.last().map(|r| r.1).unwrap_or(fh - ib));
let (left, right) = (g.columns.first().map(|c| c.0).unwrap_or(il), g.columns.last().map(|c| c.1).unwrap_or(fw - ir));
for pair in g.columns.windows(2) {
if let [a, b] = pair {
gaps.push(main(band(a.1, top, b.0, bottom), false, al.gap));
@@ -396,7 +400,7 @@ fn al_zones(app: &FigApp) -> Option<Zones> {
let flip = |r: figcraft_geom::Rect| if vertical { figcraft_geom::Rect::new(r.y0, r.x0, r.y1, r.x1) } else { r };
let fband = |x0: f64, y0: f64, x1: f64, y1: f64| if vertical { band(y0, x0, y1, x1) } else { band(x0, y0, x1, y1) };
// Main-axis length and padding, cross-axis length and padding.
let (mw, mp0, mp1, cw, cp0, cp1) = if vertical { (fh, pt, pb, fw, pl, pr) } else { (fw, pl, pr, fh, pt, pb) };
let (mw, mp0, mp1, cw, cp0, cp1) = if vertical { (fh, it, ib, fw, il, ir) } else { (fw, il, ir, fh, it, ib) };
let boxes: Vec<figcraft_geom::Rect> = figcraft_layout::flow_children(doc, id)
.into_iter()
.filter_map(|c| doc.node(c))
@@ -799,4 +803,6 @@ fn apply(app: &mut FigApp, drag: &Drag, pos: Pos2, shift: bool) {
#[cfg(test)]
mod character_fill_tests;
#[cfg(test)]
mod padding_tests;
#[cfg(test)]
mod region_tests;
@@ -0,0 +1,69 @@
//! Padding bands follow the content box, including the frame's inside stroke.
use egui::Rect;
use figcraft_doc::NodeId;
use figcraft_engine::{Session, params::id_of};
use serde_json::json;
use super::al_zones;
use crate::layers_drag_tests::Ui;
fn scene() -> (Ui, NodeId) {
let mut s = Session::new();
let f = id_of(
&s.execute(
"frame.create",
&json!({"x": 100, "y": 100, "width": 300, "height": 200,
"props": {"layout": "horizontal", "gap": 10, "padding": [10, 20, 30, 40], "stroke": "#000000", "strokeWeight": 4, "strokeAlign": "inside"}
}),
)
.unwrap()["id"],
)
.unwrap();
s.execute("select.set", &json!({"ids": [f]})).unwrap();
(Ui::new(s), f)
}
#[test]
fn padding_bands_follow_uniform_and_individual_inside_strokes() {
let (mut u, f) = scene();
for (props, stroke) in [
(json!({"strokeWeight": 4}), [4.0; 4]),
(json!({"strokeWeight": 24}), [24.0; 4]),
(json!({"strokeSides": [8, 12, 16, 24]}), [8.0, 12.0, 16.0, 24.0]),
(json!({"strokesIncluded": false}), [0.0; 4]),
(json!({"strokesIncluded": true, "strokeAlign": "center"}), [0.0; 4]),
(json!({"strokeAlign": "outside"}), [0.0; 4]),
] {
u.app.run("node.set", json!({"ids": [f], "props": props}));
let [t, r, b, l] = stroke;
let expected = [
(l, t, 300.0 - r, t + 10.0),
(300.0 - r - 20.0, t, 300.0 - r, 200.0 - b),
(l, 200.0 - b - 30.0, 300.0 - r, 200.0 - b),
(l, t, l + 40.0, 200.0 - b),
];
let zones = al_zones(&u.app).unwrap();
for ((side, band, value), (x0, y0, x1, y1)) in zones.bands.iter().zip(expected) {
let expected = Rect::from_two_pos(u.app.to_screen(100.0 + x0, 100.0 + y0), u.app.to_screen(100.0 + x1, 100.0 + y1));
assert_eq!(*band, expected, "side {side}, stroke {stroke:?}");
assert_eq!(*value, [10.0, 20.0, 30.0, 40.0][*side]);
}
}
}
#[test]
fn gap_bands_span_the_content_box_inside_stroke_and_padding() {
let (mut u, f) = scene();
for _ in 0..2 {
u.app.run("shape.rectangle", json!({"parent": f, "width": 40, "height": 20}));
}
u.app.run("select.set", json!({"ids": [f]}));
for (mode, expected) in [("horizontal", (184.0, 114.0, 194.0, 266.0)), ("vertical", (144.0, 134.0, 376.0, 144.0))] {
u.app.run("node.set", json!({"ids": [f], "props": {"layout": mode}}));
let zones = al_zones(&u.app).unwrap();
assert_eq!(zones.gaps.len(), 1);
let (x0, y0, x1, y1) = expected;
assert_eq!(zones.gaps[0].rect, Rect::from_two_pos(u.app.to_screen(x0, y0), u.app.to_screen(x1, y1)));
}
}