mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-29 09:46:34 +01:00
Layers: follow vertical auto-layout order (#2470)
This commit is contained in:
1 parent
b828e6c75a
commit
c24392ea55
4 files changed
+139
-27
No files matched your search
@@ -356,7 +356,14 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Move layer into",
|
||||
"{ids?, parent, index? (final position among parent's children, bottom first; default top)}",
|
||||
true,
|
||||
reparent
|
||||
|s, p| reparent(s, p, "node.reparent", false)
|
||||
),
|
||||
spec!(
|
||||
"layers.reparent",
|
||||
"Move layer into",
|
||||
"{ids (in the destination's stored child order), parent, index? (final position among parent's children, bottom first; default top)}",
|
||||
true,
|
||||
|s, p| reparent(s, p, "layers.reparent", true)
|
||||
),
|
||||
spec!("node.reorder", "Reorder layer", "{id, index}", true, |s, p| {
|
||||
let i = req_id("node.reorder", p, "id")?;
|
||||
|
||||
@@ -268,12 +268,13 @@ pub(super) fn stroke_brush(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
ok()
|
||||
}
|
||||
|
||||
pub(super) fn reparent(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let ids = req_targets("node.reparent", s, p)?;
|
||||
let parent = req_id("node.reparent", p, "parent")?;
|
||||
let index = opt_index("node.reparent", p, "index")?;
|
||||
pub(super) fn reparent(s: &mut Session, p: &Value, cmd: &str, keep_order: bool) -> Result<Value> {
|
||||
let ids = req_targets(cmd, s, p)?;
|
||||
let parent = req_id(cmd, p, "parent")?;
|
||||
let index = opt_index(cmd, p, "index")?;
|
||||
let doc = s.doc_mut()?;
|
||||
let ids = z_sorted(doc, &roots(doc, &ids));
|
||||
let roots = roots(doc, &ids);
|
||||
let ids = if keep_order { roots } else { z_sorted(doc, &roots) };
|
||||
// `index` is where the first moved layer ends up among the parent's children (bottom first),
|
||||
// counted after the move, the way `node.reorder` counts it. The layers land together, keeping
|
||||
// their order, under the sibling that stays put and ends up just after them (none: on top).
|
||||
|
||||
@@ -130,6 +130,97 @@ fn setup() -> (Ui, NodeId, NodeId, NodeId) {
|
||||
(u, frame, child, rect)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vertical_auto_layout_layers_follow_the_canvas_before_and_after_a_drop() {
|
||||
let mut s = Session::new();
|
||||
let frame = id(s.execute("node.create", &json!({"type": "frame", "width": 180, "height": 160, "props": {"layout": "vertical"}})).unwrap());
|
||||
let rect = |s: &mut Session, name: &str| {
|
||||
id(s.execute("node.create", &json!({"type": "rectangle", "parent": frame, "width": 80, "height": 30, "name": name})).unwrap())
|
||||
};
|
||||
let first = rect(&mut s, "First");
|
||||
let second = rect(&mut s, "Second");
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
let mut u = Ui::new(s);
|
||||
u.app.ui.expanded.insert(frame);
|
||||
let canvas_top = |u: &Ui, id: NodeId| u.app.session.doc().unwrap().world_bounds(id).unwrap().y0;
|
||||
assert!(canvas_top(&u, first) < canvas_top(&u, second));
|
||||
let (top, bottom) = (u.row_at(first), u.row_at(second));
|
||||
assert!(top.y < bottom.y, "the Layers panel follows the vertical flow");
|
||||
|
||||
let above_first = top - vec2(0.0, 4.0);
|
||||
u.drag_to(bottom, above_first);
|
||||
u.button(above_first, false);
|
||||
assert!(canvas_top(&u, second) < canvas_top(&u, first), "dropping above moves it up on the canvas");
|
||||
assert!(u.row_at(second).y < u.row_at(first).y, "the Layers order follows the drop");
|
||||
|
||||
u.app.run("select.set", json!({"ids": [second]}));
|
||||
u.ctx.enable_accesskit();
|
||||
u.frame(vec![]);
|
||||
let press = |u: &mut Ui, key| {
|
||||
u.frame(
|
||||
[true, false]
|
||||
.into_iter()
|
||||
.map(|pressed| Event::Key { key, physical_key: None, pressed, repeat: false, modifiers: Default::default() })
|
||||
.collect(),
|
||||
);
|
||||
};
|
||||
for _ in 0..4 {
|
||||
press(&mut u, egui::Key::F6);
|
||||
if u.app.ui.nav.region == crate::a11y::Region::Layers {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert_eq!(u.app.ui.nav.region, crate::a11y::Region::Layers);
|
||||
press(&mut u, egui::Key::ArrowDown);
|
||||
u.frame(vec![]);
|
||||
assert_eq!(u.app.session.selection(), vec![first]);
|
||||
assert_eq!(crate::a11y::focused_name(&u.ctx), "First");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grouped_drop_between_vertical_and_stacked_rows_keeps_the_visible_order() {
|
||||
for source_vertical in [true, false] {
|
||||
let mut s = Session::new();
|
||||
let container = |s: &mut Session, x, vertical| {
|
||||
let props = if vertical { json!({"layout": "vertical"}) } else { json!({}) };
|
||||
id(s.execute("node.create", &json!({"type": "frame", "x": x, "width": 180, "height": 160, "props": props})).unwrap())
|
||||
};
|
||||
let source = container(&mut s, 0, source_vertical);
|
||||
let target = container(&mut s, 240, !source_vertical);
|
||||
let rect = |s: &mut Session, parent, name| {
|
||||
id(s.execute("node.create", &json!({"type": "rectangle", "parent": parent, "width": 80, "height": 30, "name": name})).unwrap())
|
||||
};
|
||||
let (a, b) = if source_vertical {
|
||||
(rect(&mut s, source, "A"), rect(&mut s, source, "B"))
|
||||
} else {
|
||||
let b = rect(&mut s, source, "B");
|
||||
(rect(&mut s, source, "A"), b)
|
||||
};
|
||||
let c = rect(&mut s, target, "C");
|
||||
s.execute("select.set", &json!({"ids": [b, a]})).unwrap();
|
||||
let mut u = Ui::new(s);
|
||||
u.app.ui.expanded.extend([source, target]);
|
||||
let top = u.row_at(a);
|
||||
assert!(top.y < u.row_at(b).y);
|
||||
let above_c = u.row_at(c) - vec2(0.0, 4.0);
|
||||
u.drag_to(top, above_c);
|
||||
u.button(above_c, false);
|
||||
|
||||
let (a_row, b_row, c_row) = (u.row_at(a), u.row_at(b), u.row_at(c));
|
||||
assert!(a_row.y < b_row.y && b_row.y < c_row.y, "A, B, C in the destination (source vertical: {source_vertical})");
|
||||
let doc = u.app.session.doc().unwrap();
|
||||
assert!(doc.node(source).unwrap().children.is_empty());
|
||||
let want = if source_vertical { vec![c, b, a] } else { vec![a, b, c] };
|
||||
assert_eq!(doc.node(target).unwrap().children, want);
|
||||
|
||||
u.app.run("edit.undo", json!({}));
|
||||
let doc = u.app.session.doc().unwrap();
|
||||
assert_eq!(doc.node(a).unwrap().parent, Some(source));
|
||||
assert_eq!(doc.node(b).unwrap().parent, Some(source));
|
||||
assert_eq!(doc.node(target).unwrap().children, vec![c]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn holding_over_a_collapsed_frame_opens_it_and_dropping_moves_the_layer_in() {
|
||||
let (mut u, frame, child, rect) = setup();
|
||||
|
||||
@@ -616,12 +616,22 @@ fn hides_children(n: &figcraft_doc::Node, dimmed: bool) -> bool {
|
||||
dimmed || !n.visible || n.hides_contents()
|
||||
}
|
||||
|
||||
fn vertical_flow(n: &figcraft_doc::Node) -> bool {
|
||||
n.layout.as_ref().is_some_and(|layout| layout.mode == figcraft_doc::LayoutMode::Vertical)
|
||||
}
|
||||
|
||||
/// Follow a vertical flow from top to bottom; elsewhere start at the top of the stacking order.
|
||||
fn panel_children(n: &figcraft_doc::Node) -> impl Iterator<Item = &NodeId> {
|
||||
let mut children = n.children.iter();
|
||||
let forward = vertical_flow(n);
|
||||
std::iter::from_fn(move || if forward { children.next() } else { children.next_back() })
|
||||
}
|
||||
|
||||
fn collect(doc: &Document, id: NodeId, depth: usize, dimmed: bool, locked: bool, expanded: &BTreeSet<NodeId>, out: &mut Vec<Row>) {
|
||||
let Some(n) = doc.node(id) else { return };
|
||||
let dim_kids = !n.is_page() && hides_children(n, dimmed);
|
||||
let lock_kids = locked || n.locked;
|
||||
// Layers panel lists top of the stack first.
|
||||
for c in n.children.iter().rev() {
|
||||
for c in panel_children(n) {
|
||||
let Some(cn) = doc.node(*c) else { continue };
|
||||
let has_kids = !cn.children.is_empty() || matches!(cn.kind, NodeKind::BooleanOp { .. });
|
||||
out.push(Row { id: *c, depth, has_kids, dimmed: dim_kids, locked_above: lock_kids });
|
||||
@@ -631,10 +641,10 @@ fn collect(doc: &Document, id: NodeId, depth: usize, dimmed: bool, locked: bool,
|
||||
}
|
||||
}
|
||||
|
||||
/// Layers whose name contains `q` (lowercase), top of the stack first, as a flat list.
|
||||
/// Layers whose name contains `q` (lowercase), in panel order as a flat list.
|
||||
fn matching(doc: &Document, id: NodeId, q: &str, out: &mut Vec<Row>) {
|
||||
let Some(n) = doc.node(id) else { return };
|
||||
for c in n.children.iter().rev() {
|
||||
for c in panel_children(n) {
|
||||
let Some(cn) = doc.node(*c) else { continue };
|
||||
let name = if cn.name.is_empty() { cn.kind.label() } else { cn.name.as_str() };
|
||||
if name.to_lowercase().contains(q) {
|
||||
@@ -974,26 +984,29 @@ fn select_clicked(app: &mut FigApp, rows: &[Row], sel: &[NodeId], id: NodeId, m:
|
||||
|
||||
/// Drops `moving` on `target` (`zone`: 0 into it, -1 above, 1 below), unless Dev Mode refuses
|
||||
/// (a drag started in the design can end after a switch to it).
|
||||
fn drop_layers(app: &mut FigApp, doc: &Document, moving: &[NodeId], target: NodeId, zone: i8) {
|
||||
fn drop_layers(app: &mut FigApp, doc: &Document, rows: &[Row], moving: &[NodeId], target: NodeId, zone: i8) {
|
||||
if crate::menus::dev_mode_refuses(app, "node.reparent") {
|
||||
return;
|
||||
}
|
||||
let ids = moving.to_vec();
|
||||
match zone {
|
||||
0 => {
|
||||
app.run("node.reparent", json!({"ids": ids, "parent": target}));
|
||||
}
|
||||
z => {
|
||||
// Above in the list = higher z = after the target among siblings.
|
||||
if let Some(parent) = doc.node(target).and_then(|n| n.parent) {
|
||||
let idx = doc.index_in_parent(target).unwrap_or(0);
|
||||
let gap = if z < 0 { idx + 1 } else { idx };
|
||||
// `index` is the final position: the dragged layers below the gap leave it.
|
||||
let below = doc.node(parent).map_or(0, |p| p.children.iter().take(gap).filter(|c| ids.contains(c)).count());
|
||||
app.run("node.reparent", json!({"ids": ids, "parent": parent, "index": gap - below}));
|
||||
}
|
||||
}
|
||||
let parent = if zone == 0 { Some(target) } else { doc.node(target).and_then(|n| n.parent) };
|
||||
let Some(parent) = parent.and_then(|id| doc.node(id)) else { return };
|
||||
let forward = vertical_flow(parent);
|
||||
let mut pending: BTreeSet<_> = moving.iter().copied().collect();
|
||||
let index = (zone != 0).then(|| {
|
||||
let idx = doc.index_in_parent(target).unwrap_or(0);
|
||||
// Above means before the target in a vertical flow, after it in a stack.
|
||||
let gap = if (zone < 0) != forward { idx + 1 } else { idx };
|
||||
// `index` is the final position: the dragged layers below the gap leave it.
|
||||
let below = parent.children.iter().take(gap).filter(|c| pending.contains(c)).count();
|
||||
gap - below
|
||||
});
|
||||
// Keep the displayed order of the moved rows. A selection can include a layer outside
|
||||
// the current list (for example, under a collapsed row); include it after the listed rows.
|
||||
let mut ids: Vec<_> = rows.iter().map(|row| row.id).chain(moving.iter().copied()).filter(|id| pending.remove(id)).collect();
|
||||
if !forward {
|
||||
ids.reverse();
|
||||
}
|
||||
app.run("layers.reparent", json!({"ids": ids, "parent": parent.id, "index": index}));
|
||||
}
|
||||
|
||||
/// The eye and padlock of a row: painted when hovered (`over`, false in Dev Mode) or set, and, outside
|
||||
@@ -1402,7 +1415,7 @@ fn layers(app: &mut FigApp, ui: &mut Ui) {
|
||||
d.remove::<(NodeId, f64)>(Id::unique("layer_drag_hold"));
|
||||
});
|
||||
if let (Some(_), Some((target, zone))) = (dragging, drop) {
|
||||
drop_layers(app, &doc, &moving, target, zone);
|
||||
drop_layers(app, &doc, &rows, &moving, target, zone);
|
||||
}
|
||||
}
|
||||
if let Ok(ds) = app.session.ds_mut() {
|
||||
|
||||
Reference in new issue
Block a user