Merge pull request #9462 from dotCooCoo/fix/appearance-drag-document-current

Keep appearance drags tied to their source document
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-28 00:22:40 -08:00
commit 64c966d662
7 files changed
+107 -22

No files matched your search

+6 -2
View File
@@ -170,7 +170,11 @@ impl Component for CanvasDrops<'_> {
}
if self.kind == BodyKind::Live {
// 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| (!matches!(p, PanelDrag::Art { .. })).then(|| Payload::Panel(p.clone())));
let panel = Target::new(rect, view, covered, |p: &PanelDrag| match p {
PanelDrag::Art { .. } => None,
PanelDrag::Appearance { document, .. } if *document != self.document => None,
_ => Some(Payload::Panel(p.clone())),
});
let style = Target::new(rect, view, covered, |d: &StyleDrag| (d.document == self.document).then(|| Payload::Style(d.clone())));
let symbol = Target::new(rect, view, covered, |d: &ListDrag<SymbolKey>| Some(Payload::Symbol(d.grabbed.clone())));
let brush = Target::new(rect, view, covered, |d: &ListDrag<BrushKey>| {
@@ -390,7 +394,7 @@ pub(crate) fn apply_drop(app: &mut VectorcraftApp, drop: &Dropped) -> Result<(),
}
run(app, cmd, params)
}
Payload::Panel(PanelDrag::Appearance(source)) => {
Payload::Panel(PanelDrag::Appearance { document, source }) if app.session.active().is_some_and(|st| st.uid == *document) => {
let Some(hit) = hit() else { return Ok(()) };
let target = top_object(app, &hit)?;
if target == source.0 {
+28 -2
View File
@@ -152,12 +152,14 @@ 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();
drop_at(&mut app, Payload::Panel(PanelDrag::Appearance(a)), on_b, Move).unwrap();
let document = app.session.active().unwrap().uid;
let appearance = |source| Payload::Panel(PanelDrag::Appearance { document, 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());
assert_eq!((&nb.appearance, nb.opacity), (&na.appearance, na.opacity), "the appearance is copied");
let n = undo_len(&app);
drop_at(&mut app, Payload::Panel(PanelDrag::Appearance(b)), on_b, Move).unwrap();
drop_at(&mut app, appearance(b), on_b, Move).unwrap();
assert_eq!(undo_len(&app), n, "dropped on its own source: nothing changes");
let path = NodeId(app.run("path.create", json!({"anchors": [{"x": 50, "y": 200}, {"x": 350, "y": 200}]})).unwrap()["id"].as_u64().unwrap());
@@ -609,3 +611,27 @@ fn an_anchor_dragged_off_the_canvas_is_not_carried() {
frame(&mut app, &ctx, vec![]);
assert!(craft::ui::runtime::drag::active::<PanelDrag>(&ctx).is_none());
}
#[test]
fn a_foreign_appearance_drop_never_copies_a_matching_local_object() {
let (mut app, ctx) = app_with_canvas();
let add = |app: &mut VectorcraftApp, x| {
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;
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");
app.run("paint.setFill", json!({"color": "#ed1c24"})).unwrap();
let target = add(&mut app, 200);
app.run("paint.setFill", json!({"color": "#0000ff"})).unwrap();
frame(&mut app, &ctx, vec![]);
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();
assert_eq!(fill(&app), before);
assert_eq!(undo_len(&app), history, "a foreign appearance drop makes no edit");
}
+1 -1
View File
@@ -21,7 +21,7 @@ pub enum PanelDrag {
/// 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(vectorcraft_doc::NodeId),
Appearance { document: 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
+2 -2
View File
@@ -805,8 +805,8 @@ fn object_row<'r>(cx: &'r RowCx<'_>) -> ListRow<'r> {
None => cx.app.session.paint.fill.clone(),
};
let row = row.thumb(ThumbSize::Small, move |painter, rect| paint_preview::paint_rect(painter, rect, &fill));
match cx.node.filter(|_| cx.mix.is_none()) {
Some(n) => row.part_drag(RowDragSpot::Thumb(ThumbTarget::First), PanelDrag::Appearance(n.id)),
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 }),
None => row,
}
}
+53 -3
View File
@@ -609,11 +609,11 @@ 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.
/// Appearance panel's object from this document.
fn dropped_object(drag: &PanelDrag, uid: u64, reverts: u64) -> Option<NodeId> {
match drag {
PanelDrag::Art { .. } => drag.artwork_for(uid, reverts)?.first().copied(),
PanelDrag::Appearance(id) => Some(*id),
PanelDrag::Appearance { document, source } if *document == uid => Some(*source),
_ => 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(oval)] {
for payload in [PanelDrag::Art { document, reverts, ids: vec![oval] }, PanelDrag::Appearance { document, 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);
@@ -1495,6 +1495,56 @@ mod tests {
assert_eq!(names(&p.app()).len(), before.len() + 1, "a same-document drop still makes a style");
}
#[test]
fn graphic_styles_rejects_a_foreign_appearance_after_next_document() {
use crate::tests::panel_harness::Panel as PanelHarness;
use craft::ui::contract::ShowCollection as _;
use craft::ui::list::{List, ListRow};
use craft::ui::runtime::drag;
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;
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 }))
.show_items(ui, &keys, &mut |_, _| ListRow::new("Dragged appearance"));
});
show(app, ui);
}
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]);
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();
let second = app.session.active().unwrap();
assert_eq!(source_object, second.selection.objects[0], "both documents have distinct objects with the same local ID");
let target_document = second.uid;
let before = names(&app);
assert!(app.session.set_active(0));
let mut p = PanelHarness::new(app, with_appearance);
let from = p.rect("Dragged appearance").center();
let to = p.rect("Drop Target").center();
p.drag_hold_role("Dragged appearance", craft::ui::contract::Role::LIST_ITEM, to - from);
assert!(drag::mark(p.ctx()).is_some());
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 }));
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");
p.drag("Dragged appearance", "Drop Target", egui::Modifiers::NONE);
assert_eq!(names(&p.app()).len(), before.len() + 1, "a same-document appearance 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() {
+13 -10
View File
@@ -94,6 +94,7 @@ impl Outcome for CellOutput {
pub(super) struct TargetCircle {
#[common]
common: Common,
document: u64,
id: NodeId,
targeted: bool,
styled: bool,
@@ -103,18 +104,19 @@ pub(super) struct TargetCircle {
}
impl TargetCircle {
fn new(id: NodeId, targeted: bool, styled: bool) -> Self {
Self { common: Common::new(), id, targeted, styled, held: false, pick: None }
fn new(document: 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(NodeId(1), true, true).label("Target")
Self::new(1, NodeId(1), true, true).label("Target")
}
}
/// The circle as a drop target: it takes another row's appearance.
struct TakesAppearance {
document: u64,
own: NodeId,
rect: Rect,
}
@@ -124,7 +126,9 @@ impl DropTarget<PanelDrag> for TakesAppearance {
fn accept(&self, payload: &PanelDrag, _: Pos2, op: DropOperation) -> Option<(NodeId, bool)> {
match payload {
PanelDrag::Appearance(source) if *source != self.own => Some((*source, op == DropOperation::Copy)),
PanelDrag::Appearance { document, source } if *document == self.document && *source != self.own => {
Some((*source, op == DropOperation::Copy))
}
_ => None,
}
}
@@ -167,13 +171,12 @@ impl Component for TargetCircle {
self.pick = Some(CellPick::Target(activation.mods));
}
if primary_drag_start(cx) {
cx.start_drag(PanelDrag::Appearance(self.id), DragPreview::None);
cx.start_drag(PanelDrag::Appearance { document: self.document, source: self.id }, DragPreview::None);
}
let rect = cx.rect;
let own = self.id;
self.held =
cx.dragging::<PanelDrag>().is_some_and(|(drag, at)| rect.contains(at) && matches!(*drag, PanelDrag::Appearance(source) if source != own));
if let Some((source, copy)) = cx.drop_target::<PanelDrag, _>(&TakesAppearance { own, rect }) {
let target = TakesAppearance { document: self.document, own: self.id, rect };
self.held = cx.dragging::<PanelDrag>().is_some_and(|(drag, at)| rect.contains(at) && target.accept(&drag, at, DropOperation::Move).is_some());
if let Some((source, copy)) = cx.drop_target::<PanelDrag, _>(&target) {
self.pick = Some(CellPick::TakeAppearance { source, copy });
}
}
@@ -376,7 +379,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(id, shows_targeted(tree, n, is_sel), has_styled_target(n))
let circle = TargetCircle::new(st.uid, 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())]));
+4 -2
View File
@@ -463,8 +463,10 @@ 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(id)) = drop.payload::<PanelDrag>() {
footer_drop(tree, drop.index, FooterPayload::Appearance(id.0))
} else if let Some(PanelDrag::Appearance { document, source }) = drop.payload::<PanelDrag>()
&& *document == st.uid
{
footer_drop(tree, drop.index, FooterPayload::Appearance(source.0))
} else {
None
};