Merge pull request #9931 from dotCooCoo/fix/appearance-drag-revert

Appearance: an appearance drag held across File Revert is not dropped on the restored art
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-28 08:01:53 -08:00
commit ef2ba3fd8f
8 files changed
+260 -35

No files matched your search

+8 -4
View File
@@ -120,6 +120,9 @@ pub(crate) struct CanvasDrops<'a> {
kind: BodyKind,
/// The uid of the document the canvas shows.
document: u64,
/// The revert count of the document the canvas shows: an appearance dragged from contents File › Revert replaced is
/// refused.
reverts: u64,
/// `(uid, revision)` of every open document: carried art whose source is not listed unchanged is refused.
sources: Vec<(u64, u64)>,
/// Whether the art being carried is taken though its source's revision moved: its move was given up and the source
@@ -172,7 +175,7 @@ impl Component for CanvasDrops<'_> {
// The art of this canvas's own document is not accepted as a panel payload: its move resumes.
let panel = Target::new(rect, view, covered, |p: &PanelDrag| match p {
PanelDrag::Art { .. } => None,
PanelDrag::Appearance { document, .. } if *document != self.document => None,
PanelDrag::Appearance { .. } if p.appearance_for(self.document, self.reverts).is_none() => None,
_ => Some(Payload::Panel(p.clone())),
});
let style = Target::new(rect, view, covered, |d: &StyleDrag| (d.document == self.document).then(|| Payload::Style(d.clone())));
@@ -218,7 +221,7 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kind: BodyKi
BodyKind::Live => app.canvas_view(),
BodyKind::Pane => pane_view(app, win, rect),
};
let (Some(view), Some(document)) = (view, app.session.documents().get(win.0).map(|d| d.uid)) else { return };
let (Some(view), Some((document, reverts))) = (view, app.session.documents().get(win.0).map(|d| (d.uid, d.reverts))) else { return };
let sources = app.session.documents().iter().map(|d| (d.uid, d.revision)).chain(app.canvas.panes.released_from).collect();
// A tab rest leaves the art to the document it showed while that is the current one: any canvas or pane takes it
// then, and nothing once that document was closed or left by other means.
@@ -231,7 +234,7 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kind: BodyKi
let ctx = ui.ctx().clone();
let covered = |at: Pos2| crate::panel_docking::os_window_at(app, &ctx, at);
let files = (CanvasFiles::new(app, rect, &view), &files_out);
let drops = CanvasDrops { common: Common::new(), view, kind, document, sources, given_up, covered: &covered, out: &out, files };
let drops = CanvasDrops { common: Common::new(), view, kind, document, reverts, sources, given_up, covered: &covered, out: &out, files };
drops.id_salt(("canvas-drops", win)).show_at(ui, rect);
if let Some(files) = files_out.take() {
crate::place::canvas_drop(app, ui.ctx(), files);
@@ -394,7 +397,8 @@ pub(crate) fn apply_drop(app: &mut VectorcraftApp, drop: &Dropped) -> Result<(),
}
run(app, cmd, params)
}
Payload::Panel(PanelDrag::Appearance { document, source }) if app.session.active().is_some_and(|st| st.uid == *document) => {
Payload::Panel(drag @ PanelDrag::Appearance { .. }) => {
let Some(source) = app.session.active().and_then(|st| drag.appearance_for(st.uid, st.reverts)) else { return Ok(()) };
let Some(hit) = hit() else { return Ok(()) };
let target = top_object(app, &hit)?;
if target == source.0 {
+89 -4
View File
@@ -152,8 +152,8 @@ fn a_drop_runs_the_effect_of_its_payload() {
drop_at(&mut app, Payload::Panel(PanelDrag::paint(Paint::Gradient(Box::new(gradient)))), on_b, Move).unwrap();
assert!(matches!(fill(&app), Paint::Gradient(g) if g.geom.is_none()), "a gradient fits the object it lands on");
app.run("transparency.set", json!({"opacity": 40})).unwrap();
let document = app.session.active().unwrap().uid;
let appearance = |source| Payload::Panel(PanelDrag::Appearance { document, source });
let (document, reverts) = (app.session.active().unwrap().uid, app.session.active().unwrap().reverts);
let appearance = |source| Payload::Panel(PanelDrag::Appearance { document, reverts, source });
drop_at(&mut app, appearance(a), on_b, Move).unwrap();
let doc = &app.session.active().unwrap().doc;
let (na, nb) = (doc.node(a).unwrap(), doc.node(b).unwrap());
@@ -619,7 +619,7 @@ fn a_foreign_appearance_drop_never_copies_a_matching_local_object() {
NodeId(app.run("shape.rectangle", json!({"x": x, "y": 20, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap())
};
let source = add(&mut app, 20);
let document = app.session.active().unwrap().uid;
let (document, reverts) = (app.session.active().unwrap().uid, app.session.active().unwrap().reverts);
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
let other = add(&mut app, 20);
assert_eq!(source, other, "both documents have different objects with the same local ID");
@@ -631,7 +631,92 @@ fn a_foreign_appearance_drop_never_copies_a_matching_local_object() {
let fill = |app: &VectorcraftApp| app.session.active().unwrap().doc.node(target).unwrap().appearance.fill_paint();
let before = fill(&app);
let history = undo_len(&app);
drop_at(&mut app, Payload::Panel(PanelDrag::Appearance { document, source }), Point::new(250.0, 70.0), DropOperation::Move).unwrap();
let foreign = PanelDrag::Appearance { document, reverts, source };
drop_at(&mut app, Payload::Panel(foreign), Point::new(250.0, 70.0), DropOperation::Move).unwrap();
assert_eq!(fill(&app), before);
assert_eq!(undo_len(&app), history, "a foreign appearance drop makes no edit");
}
/// An appearance held across File › Revert, released on the canvas: the restored object with the same ID keeps its
/// look, both where the canvas takes the drop and where the drop is applied. A drag started after the revert copies.
#[test]
fn an_appearance_drop_after_file_revert_leaves_the_restored_object_unchanged() {
use crate::tests::panel_harness::Panel;
use craft::ui::contract::ShowCollection as _;
use craft::ui::contract::{ListDrag, Role};
use craft::ui::list::{List, ListRow};
use craft::ui::runtime::drag;
fn source_and_canvas(app: &mut VectorcraftApp, ui: &mut egui::Ui) {
app.sync_views();
let st = app.session.active().unwrap();
let (document, reverts) = (st.uid, st.reverts);
let source = st.doc.layers[0].children().unwrap()[0].id;
egui::Panel::top("appearance-source").exact_size(60.0).show(ui, |ui| {
let keys = craft::behavior::keyed(&[0_usize], |key: &usize| *key);
List::<usize>::new("Appearance source")
.drag_payload(move |_: &ListDrag<usize>| Some(PanelDrag::Appearance { document, reverts, source }))
.show_items(ui, &keys, &mut |_, _| ListRow::new("Dragged appearance"));
});
canvas::show(app, ui);
}
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string())));
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string())));
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
let add = |app: &mut VectorcraftApp, x, color| {
let id = NodeId(app.run("shape.rectangle", json!({"x": x, "y": 100, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap());
app.run("paint.setFill", json!({"color": color, "ids": [id.0]})).unwrap();
id
};
let source = add(&mut app, 20, "#ff0000");
let target = add(&mut app, 260, "#0000ff");
let folder = vectorcraft_testkit::TempDir::new("canvas-appearance-drop-revert");
let path = folder.join("drawing.vectorcraft");
app.run("file.saveAs", json!({"path": path.to_string_lossy()})).unwrap();
app.run("shape.ellipse", json!({"x": 20, "y": 20, "width": 20, "height": 20})).unwrap();
app.run("select.none", json!({})).unwrap();
let fill = |app: &VectorcraftApp| app.session.active().unwrap().doc.node(target).unwrap().appearance.fill_paint();
let (red, blue) = (app.session.active().unwrap().doc.node(source).unwrap().appearance.fill_paint(), fill(&app));
assert_ne!(red, blue);
let mut panel = Panel::new(app, source_and_canvas);
let hold = |panel: &mut Panel| {
let from = panel.rect("Dragged appearance").center();
let to = panel.app().canvas_view().unwrap().to_screen(Point::new(310.0, 150.0));
panel.drag_hold_role("Dragged appearance", Role::LIST_ITEM, to - from);
let carried = drag::active::<PanelDrag>(panel.ctx());
assert!(matches!(carried.as_deref(), Some(PanelDrag::Appearance { source: s, .. }) if *s == source), "the source drags its appearance");
};
hold(&mut panel);
assert!(drag::mark(panel.ctx()).is_some(), "the canvas takes an appearance from the current contents");
let opening = {
let app = panel.app();
let st = app.session.active().unwrap();
(st.uid, st.reverts)
};
panel.app().run("file.revert", json!({"confirmed": true})).unwrap();
panel.frame();
let history = {
let app = panel.app();
let st = app.session.active().unwrap();
assert_eq!((st.uid, st.reverts), (opening.0, opening.1 + 1));
assert!(st.doc.node(source).is_some() && st.doc.node(target).is_some(), "the restored objects keep their IDs");
st.history.undo.len()
};
assert!(drag::active::<PanelDrag>(panel.ctx()).is_some(), "the revert leaves the drag held");
assert!(drag::mark(panel.ctx()).is_none(), "the canvas refuses an appearance carried from before the revert");
panel.release();
assert_eq!(fill(&panel.app()), blue, "the restored object with the same ID keeps its look");
assert_eq!(undo_len(&panel.app()), history, "the stale drop makes no edit");
let stale = PanelDrag::Appearance { document: opening.0, reverts: opening.1, source };
let at = Point::new(310.0, 150.0);
drop_at(&mut panel.app(), Payload::Panel(stale), at, DropOperation::Move).unwrap();
assert_eq!(fill(&panel.app()), blue, "a stale appearance that reaches the drop is not applied");
hold(&mut panel);
panel.release();
assert_eq!(fill(&panel.app()), red, "a drag started after the revert copies the appearance");
}
+11 -2
View File
@@ -20,8 +20,9 @@ pub enum PanelDrag {
Paint { paint: Paint, params: serde_json::Value, rows: Option<SwatchRows>, library: Option<(String, String)> },
/// The Appearance panel's thumbnail: art it is dropped on takes object `0`'s appearance
/// (`appearance.copyFrom`); the Graphic Styles panel makes a style of it, or with Alt held
/// replaces the style it is dropped on with it (`graphicStyle.redefine`).
Appearance { document: u64, source: vectorcraft_doc::NodeId },
/// replaces the style it is dropped on with it (`graphicStyle.redefine`). `document` is the uid of the
/// document it came from and `reverts` identifies the contents it came from.
Appearance { document: u64, reverts: u64, source: vectorcraft_doc::NodeId },
/// The selected art, dragged off the canvas with the Selection tool (`document`: the uid of the
/// document it came from): the Graphic Styles panel makes a style of the first object
/// (`graphicStyle.new`), or with Alt held replaces the style it is dropped on with that
@@ -62,6 +63,14 @@ impl PanelDrag {
}
}
/// The object whose appearance is dragged while the destination still shows the contents the drag came from.
pub(crate) fn appearance_for(&self, uid: u64, reload_count: u64) -> Option<vectorcraft_doc::NodeId> {
match self {
Self::Appearance { document, reverts, source } if (*document, *reverts) == (uid, reload_count) => Some(*source),
_ => None,
}
}
/// `paint` dragged from a Fill/Stroke proxy, a Recent Colors chip, a Color Guide color or the
/// Gradient panel's thumbnail. A gradient carries no placement: it fits the art it lands on
/// (freeform points are placed afresh there, coloured like the dragged ones).
+3 -1
View File
@@ -806,7 +806,9 @@ fn object_row<'r>(cx: &'r RowCx<'_>) -> ListRow<'r> {
};
let row = row.thumb(ThumbSize::Small, move |painter, rect| paint_preview::paint_rect(painter, rect, &fill));
match cx.node.filter(|_| cx.mix.is_none()).zip(cx.app.session.active()) {
Some((n, st)) => row.part_drag(RowDragSpot::Thumb(ThumbTarget::First), PanelDrag::Appearance { document: st.uid, source: n.id }),
Some((n, st)) => {
row.part_drag(RowDragSpot::Thumb(ThumbTarget::First), PanelDrag::Appearance { document: st.uid, reverts: st.reverts, source: n.id })
}
None => row,
}
}
+74 -8
View File
@@ -608,12 +608,12 @@ fn reorder(app: &mut VectorcraftApp, edit: EditEvent<ReorderMove<StyleKey>>) {
}
}
/// The object whose look a dropped payload carries: the first of the art dragged from this document, or the
/// Appearance panel's object from this document.
/// The object whose look a dropped payload carries: the first of the art dragged from this document's current
/// contents, or the Appearance panel's object from them.
fn dropped_object(drag: &PanelDrag, uid: u64, reverts: u64) -> Option<NodeId> {
match drag {
PanelDrag::Art { .. } => drag.artwork_for(uid, reverts)?.first().copied(),
PanelDrag::Appearance { document, source } if *document == uid => Some(*source),
PanelDrag::Appearance { .. } => drag.appearance_for(uid, reverts),
_ => None,
}
}
@@ -1429,7 +1429,7 @@ mod tests {
let n = names(&app);
let st = app.session.active().unwrap();
let (document, reverts) = (st.uid, st.reverts);
for payload in [PanelDrag::Art { document, reverts, ids: vec![oval] }, PanelDrag::Appearance { document, source: oval }] {
for payload in [PanelDrag::Art { document, reverts, ids: vec![oval] }, PanelDrag::Appearance { document, reverts, source: oval }] {
drop_on(&mut app, Some("Red"), dropped_object(&payload, document, reverts), None, true);
let d = doc(&app);
assert_eq!(d.graphic_style("Red").unwrap().appearance.fill_paint(), green);
@@ -1505,11 +1505,11 @@ mod tests {
fn with_appearance(app: &mut VectorcraftApp, ui: &mut Ui) {
let st = app.session.active().unwrap();
let source = st.selection.objects.first().copied();
let document = st.uid;
let (document, reverts) = (st.uid, st.reverts);
ui.allocate_ui(egui::vec2(ui.available_width(), 80.0), |ui| {
let keys = craft::behavior::keyed(&[0_usize], |key: &usize| *key);
List::<usize>::new("Appearance source")
.drag_payload(move |_: &ListDrag<usize>| source.map(|source| PanelDrag::Appearance { document, source }))
.drag_payload(move |_: &ListDrag<usize>| source.map(|source| PanelDrag::Appearance { document, reverts, source }))
.show_items(ui, &keys, &mut |_, _| ListRow::new("Dragged appearance"));
});
show(app, ui);
@@ -1518,7 +1518,7 @@ mod tests {
let mut app = app();
app.run("graphicStyle.new", json!({"name": "Drop Target"})).unwrap();
let first = app.session.active().unwrap();
let (source_document, source_object) = (first.uid, first.selection.objects[0]);
let (source_document, source_reverts, source_object) = (first.uid, first.reverts, first.selection.objects[0]);
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 50, "height": 50})).unwrap();
app.run("graphicStyle.new", json!({"name": "Drop Target"})).unwrap();
@@ -1536,7 +1536,8 @@ mod tests {
p.key(egui::Key::F6, egui::Modifiers::COMMAND);
assert_eq!(p.app().session.active().unwrap().uid, target_document, "Next Document switches tabs during the drag");
let carried = drag::active::<PanelDrag>(p.ctx());
assert_eq!(carried.as_deref(), Some(&PanelDrag::Appearance { document: source_document, source: source_object }));
let foreign = PanelDrag::Appearance { document: source_document, reverts: source_reverts, source: source_object };
assert_eq!(carried.as_deref(), Some(&foreign));
assert!(drag::mark(p.ctx()).is_none(), "the target withdraws its drop indicator for the foreign appearance");
p.release();
assert_eq!(names(&p.app()), before, "the foreign drop creates no style from the current document's same-ID object");
@@ -1545,6 +1546,71 @@ mod tests {
assert_eq!(names(&p.app()).len(), before.len() + 1, "a same-document appearance still makes a style");
}
/// The Appearance panel's thumbnail held across File › Revert makes no style of the restored object with the same
/// ID; a thumbnail drag started after the revert still makes one.
#[test]
fn graphic_styles_rejects_an_appearance_carried_across_file_revert() {
use crate::tests::panel_harness::Panel as PanelHarness;
use craft::ui::runtime::drag;
fn appearance_and_styles(app: &mut VectorcraftApp, ui: &mut Ui) {
egui::Panel::top("appearance").exact_size(240.0).show(ui, |ui| crate::panels::appearance::show(app, ui));
show(app, ui);
}
let mut app = app();
let rect = app.session.active().unwrap().selection.objects[0];
app.run("paint.setFill", json!({"color": "#ff0000", "ids": [rect.0]})).unwrap();
app.run("graphicStyle.new", json!({"name": "Drop Target"})).unwrap();
app.run("select.set", json!({"ids": [rect.0]})).unwrap();
let folder = vectorcraft_testkit::TempDir::new("graphic-styles-appearance-revert");
let path = folder.join("drawing.vectorcraft");
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|err| err.to_string())));
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|err| err.to_string())));
app.run("file.saveAs", json!({"path": path.to_string_lossy()})).unwrap();
app.run("shape.ellipse", json!({"x": 60, "y": 60, "width": 20, "height": 20})).unwrap();
app.run("select.set", json!({"ids": [rect.0]})).unwrap();
let before = names(&app);
let mut p = PanelHarness::new(app, appearance_and_styles);
let hold = |p: &mut PanelHarness| {
// The thumbnail is the first part of the object row, at its start.
let row = p.rect("Rectangle: Drop Target");
let (from, to) = (row.left_center() + egui::vec2(14.0, 0.0), p.rect("Drop Target").center());
p.drag_hold_between(from, to);
let carried = drag::active::<PanelDrag>(p.ctx());
assert!(
matches!(carried.as_deref(), Some(PanelDrag::Appearance { source, .. }) if *source == rect),
"the thumbnail drags the appearance"
);
};
hold(&mut p);
assert!(drag::mark(p.ctx()).is_some(), "Graphic Styles takes an appearance from the current contents");
let opening = {
let app = p.app();
let st = app.session.active().unwrap();
(st.uid, st.reverts)
};
p.app().run("file.revert", json!({"confirmed": true})).unwrap();
p.frame();
{
let app = p.app();
let st = app.session.active().unwrap();
assert_eq!((st.uid, st.reverts), (opening.0, opening.1 + 1));
assert!(st.doc.node(rect).is_some(), "the restored object keeps the same ID");
}
assert!(drag::active::<PanelDrag>(p.ctx()).is_some(), "the revert leaves the drag held");
assert!(drag::mark(p.ctx()).is_none(), "Graphic Styles refuses an appearance carried from before the revert");
p.release();
assert_eq!(names(&p.app()), before, "the stale drop makes no style of the restored object");
p.app().run("select.set", json!({"ids": [rect.0]})).unwrap();
p.frame();
hold(&mut p);
p.release();
assert_eq!(names(&p.app()).len(), before.len() + 1, "a thumbnail drag started after the revert still makes a style");
}
/// A style dropped on another with Alt takes that style's look; on itself it does nothing.
#[test]
fn a_style_dropped_on_another_with_alt_replaces_that_style() {
+12 -13
View File
@@ -94,7 +94,8 @@ impl Outcome for CellOutput {
pub(super) struct TargetCircle {
#[common]
common: Common,
document: u64,
/// The document's uid and revert count: File › Revert keeps the uid but replaces the drawing.
document: (u64, u64),
id: NodeId,
targeted: bool,
styled: bool,
@@ -104,19 +105,19 @@ pub(super) struct TargetCircle {
}
impl TargetCircle {
fn new(document: u64, id: NodeId, targeted: bool, styled: bool) -> Self {
fn new(document: (u64, u64), id: NodeId, targeted: bool, styled: bool) -> Self {
Self { common: Common::new(), document, id, targeted, styled, held: false, pick: None }
}
/// A targeted circle with an appearance, for the suite.
fn sample() -> Self {
Self::new(1, NodeId(1), true, true).label("Target")
Self::new((1, 0), NodeId(1), true, true).label("Target")
}
}
/// The circle as a drop target: it takes another row's appearance.
/// The circle as a drop target: it takes another row's appearance from the same contents.
struct TakesAppearance {
document: u64,
document: (u64, u64),
own: NodeId,
rect: Rect,
}
@@ -125,12 +126,9 @@ impl DropTarget<PanelDrag> for TakesAppearance {
type Drop = (NodeId, bool);
fn accept(&self, payload: &PanelDrag, _: Pos2, op: DropOperation) -> Option<(NodeId, bool)> {
match payload {
PanelDrag::Appearance { document, source } if *document == self.document && *source != self.own => {
Some((*source, op == DropOperation::Copy))
}
_ => None,
}
let (uid, reverts) = self.document;
let source = payload.appearance_for(uid, reverts).filter(|source| *source != self.own)?;
Some((source, op == DropOperation::Copy))
}
fn mark(&self, _: &(NodeId, bool)) -> DropMark {
@@ -171,7 +169,8 @@ impl Component for TargetCircle {
self.pick = Some(CellPick::Target(activation.mods));
}
if primary_drag_start(cx) {
cx.start_drag(PanelDrag::Appearance { document: self.document, source: self.id }, DragPreview::None);
let (document, reverts) = self.document;
cx.start_drag(PanelDrag::Appearance { document, reverts, source: self.id }, DragPreview::None);
}
let rect = cx.rect;
let target = TakesAppearance { document: self.document, own: self.id, rect };
@@ -379,7 +378,7 @@ pub(super) fn build(tree: &DocLayers<'_>, row: &RowId, info: &LayerInfo) -> Cell
let is_sel = selected(id);
let (full, holds) = square_marks(tree, n);
let art = if full || holds { st.selection.objects.clone() } else { vec![] };
let circle = TargetCircle::new(st.uid, id, shows_targeted(tree, n, is_sel), has_styled_target(n))
let circle = TargetCircle::new((st.uid, st.reverts), id, shows_targeted(tree, n, is_sel), has_styled_target(n))
.label(crate::i18n::fmt(tl!("Target {row}"), &[("row", info.name.as_str())]));
let square = SelectionSquare::new(st.uid, Some(id), full, holds, color, art)
.label(crate::i18n::fmt(tl!("Select art of {row}"), &[("row", info.name.as_str())]));
+1 -3
View File
@@ -463,9 +463,7 @@ pub(crate) fn footer(tree: &DocLayers, ui: &mut Ui, out: &mut Vec<(String, Value
if let Some(drop) = &output.dropped {
let run = if let Some(d) = drop.payload::<LayersDrag<RowId>>().filter(|d| own(d)) {
footer_drop(tree, drop.index, FooterPayload::Rows(&d.layers()))
} else if let Some(PanelDrag::Appearance { document, source }) = drop.payload::<PanelDrag>()
&& *document == st.uid
{
} else if let Some(source) = drop.payload::<PanelDrag>().and_then(|d| d.appearance_for(st.uid, st.reverts)) {
footer_drop(tree, drop.index, FooterPayload::Appearance(source.0))
} else {
None
+62
View File
@@ -874,3 +874,65 @@ fn full_layers_are_the_layers_with_all_their_art_selected() {
assert_eq!(rows::full_layers(&doc, &sel), want, "selection {bits:b}");
}
}
/// A target circle's appearance held across File › Revert and dropped on another row's circle
/// leaves the restored objects with the same IDs as they were; a drag started after the revert
/// still moves the appearance.
#[test]
fn a_target_circle_drop_after_file_revert_leaves_the_restored_appearance_in_place() {
use crate::panel_drag::PanelDrag;
use crate::tests::panel_harness::Panel;
use craft::ui::contract::Role;
use craft::ui::runtime::drag;
use egui::Modifiers;
let (mut app, _, [a, b]) = two_rects();
name_row(&mut app, a, "A");
name_row(&mut app, b, "B");
app.run("paint.setFill", json!({"color": "#ff0000", "ids": [a.0]})).unwrap();
app.run("paint.setFill", json!({"color": "#0000ff", "ids": [b.0]})).unwrap();
app.run("select.none", json!({})).unwrap();
let folder = vectorcraft_testkit::TempDir::new("layers-appearance-drag-revert");
let path = folder.join("drawing.vectorcraft");
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|err| err.to_string())));
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|err| err.to_string())));
app.run("file.saveAs", json!({"path": path.to_string_lossy()})).unwrap();
let fill = |app: &VectorcraftApp, id: NodeId| app.session.active().unwrap().doc.node(id).unwrap().appearance.fill_paint();
let (red, blue) = (fill(&app, a), fill(&app, b));
assert_ne!(red, blue);
// An edit that leaves the rows where they are, so the revert has changes to discard.
app.run("paint.setFill", json!({"color": "#00ff00", "ids": [b.0]})).unwrap();
app.run("select.none", json!({})).unwrap();
let mut panel = Panel::new(app, show);
let from = panel.rect_role("Target A", Role::BUTTON).center();
let to = panel.rect_role("Target B", Role::BUTTON).center();
panel.drag_hold_role("Target A", Role::BUTTON, to - from);
assert!(drag::mark(panel.ctx()).is_some(), "the other row's circle takes the appearance before the revert");
let opening = {
let app = panel.app();
let st = app.session.active().unwrap();
(st.uid, st.reverts)
};
panel.app().run("file.revert", json!({"confirmed": true})).unwrap();
panel.frame();
let history = {
let app = panel.app();
let st = app.session.active().unwrap();
assert_eq!((st.uid, st.reverts), (opening.0, opening.1 + 1));
st.history.undo.len()
};
let carried = drag::active::<PanelDrag>(panel.ctx());
assert!(matches!(carried.as_deref(), Some(PanelDrag::Appearance { source, .. }) if *source == a), "the revert leaves the drag held");
assert!(drag::mark(panel.ctx()).is_none(), "the circle refuses an appearance carried from before the revert");
panel.release();
{
let app = panel.app();
assert_eq!((fill(&app, a), fill(&app, b)), (red.clone(), blue.clone()), "the restored objects with the same IDs keep their looks");
assert_eq!(app.session.active().unwrap().history.undo.len(), history, "the stale drop makes no edit");
}
panel.drag("Target A", "Target B", Modifiers::NONE);
assert_eq!(fill(&panel.app(), b), red, "a target circle drag started after the revert still moves the appearance");
}