Reject artwork drops from a different document in Libraries

This commit is contained in:
dotCooCoo
2026-11-26 08:22:40 -08:00
parent 2a1b573a78
commit 682cac41bd
2 changed files with 70 additions and 26 deletions
+42 -26
View File
@@ -162,21 +162,6 @@ enum Grab {
New(ItemKind, Value),
}
/// The item of library `library` that `drag` holds, or what it would add, if anything.
fn grabbed(drag: &PanelDrag, library: &str) -> Option<Grab> {
match drag {
PanelDrag::Paint { library: Some((l, item)), .. } if l == library => Some(Grab::Item(ItemKind::Color, item.clone())),
PanelDrag::LibraryGraphic { library: l, item } if l == library => Some(Grab::Item(ItemKind::Graphic, item.clone())),
PanelDrag::LibraryItem { library: l, kind, item } if l == library => Some(Grab::Item(*kind, item.clone())),
PanelDrag::Art { ids, .. } => {
let ids: Vec<u64> = ids.iter().map(|id| id.0).collect();
Some(Grab::New(ItemKind::Graphic, json!({ "kind": "graphic", "ids": ids })))
}
PanelDrag::Paint { .. } => swatch_add(drag, library).map(|(kind, add)| Grab::New(kind, add)),
_ => None,
}
}
/// The kind and `library.add` params of what `drag` holds, to add to library `library`: a color
/// (not one of that library's own; a spot colour with its swatch's name), or a gradient or pattern
/// swatch, by its name.
@@ -205,6 +190,7 @@ fn swatch_add(drag: &PanelDrag, library: &str) -> Option<(ItemKind, Value)> {
/// which keep their test, can read it: what a drop into a group, or out of every group, does.
struct Membership {
library: String,
active_document: Option<u64>,
/// The group of each grouped item, by its index in the library's groups.
index: HashMap<(ItemKind, String), usize>,
/// The groups' names, in the library's order.
@@ -212,9 +198,25 @@ struct Membership {
}
impl Membership {
fn of(library: &str, lib: &Library) -> Self {
fn of(library: &str, lib: &Library, active_document: Option<u64>) -> Self {
let index = lib.group_index().into_iter().map(|((kind, item), group)| ((kind, item.to_string()), group)).collect();
Membership { library: library.to_string(), index, names: lib.groups.iter().map(|g| g.name.clone()).collect() }
Membership { library: library.to_string(), active_document, index, names: lib.groups.iter().map(|g| g.name.clone()).collect() }
}
/// The item of this library that `drag` holds, or what it would add. `library.add` reads object IDs in the current document.
fn grabbed(&self, drag: &PanelDrag) -> Option<Grab> {
let library = &self.library;
match drag {
PanelDrag::Paint { library: Some((l, item)), .. } if l == library => Some(Grab::Item(ItemKind::Color, item.clone())),
PanelDrag::LibraryGraphic { library: l, item } if l == library => Some(Grab::Item(ItemKind::Graphic, item.clone())),
PanelDrag::LibraryItem { library: l, kind, item } if l == library => Some(Grab::Item(*kind, item.clone())),
PanelDrag::Art { document, ids } if Some(*document) == self.active_document => {
let ids: Vec<u64> = ids.iter().map(|id| id.0).collect();
Some(Grab::New(ItemKind::Graphic, json!({ "kind": "graphic", "ids": ids })))
}
PanelDrag::Paint { .. } => swatch_add(drag, library).map(|(kind, add)| Grab::New(kind, add)),
_ => None,
}
}
fn group_of(&self, kind: ItemKind, key: &str) -> Option<usize> {
@@ -232,7 +234,7 @@ impl Membership {
/// another group moves in, art or a swatch is added there. `None` when nothing would change,
/// as for an item dropped on its own group, so that the target does not take it.
fn landing(&self, drag: &PanelDrag, group: Option<usize>) -> Option<Act> {
let (cmd, mut params) = match grabbed(drag, &self.library)? {
let (cmd, mut params) = match self.grabbed(drag)? {
Grab::Item(kind, item) if self.group_of(kind, &item) != group => ("library.moveItem", json!({ "kind": kind_param(kind), "item": item })),
Grab::New(_, add) => ("library.add", add),
Grab::Item(..) => return None,
@@ -303,8 +305,8 @@ struct View<'a> {
}
impl<'a> View<'a> {
fn new(library: &'a str, lib: &'a Library, query: &str, list: bool, fill_active: bool, has_doc: bool, can_apply: bool) -> Self {
let membership = Membership::of(library, lib);
fn new(library: &'a str, lib: &'a Library, query: &str, list: bool, fill_active: bool, active_document: Option<u64>, can_apply: bool) -> Self {
let membership = Membership::of(library, lib, active_document);
let query = query.trim().to_lowercase();
let searching = !query.is_empty();
let items: Vec<Shown<'a>> = lib
@@ -319,7 +321,20 @@ impl<'a> View<'a> {
}
}
let empty = lib.items().next().is_none() && lib.groups.is_empty();
View { library, lib, membership: Arc::new(membership), items, counts, query, searching, empty, list, fill_active, has_doc, can_apply }
View {
library,
lib,
membership: Arc::new(membership),
items,
counts,
query,
searching,
empty,
list,
fill_active,
has_doc: active_document.is_some(),
can_apply,
}
}
/// The list: the ungrouped items, then the groups as sections. A drop anywhere on it away from
@@ -387,7 +402,7 @@ impl<'a> View<'a> {
/// Whether the drag in progress holds an item of one of this library's groups.
fn dragging_grouped(&self, ui: &Ui) -> bool {
craft::ui::runtime::drag::active::<PanelDrag>(ui.ctx()).is_some_and(
|drag| matches!(grabbed(&drag, self.library), Some(Grab::Item(kind, item)) if self.membership.group_of(kind, &item).is_some()),
|drag| matches!(self.membership.grabbed(&drag), Some(Grab::Item(kind, item)) if self.membership.group_of(kind, &item).is_some()),
)
}
@@ -476,7 +491,7 @@ impl<'a> View<'a> {
let source = keyed(shown, |item: &&Shown<'a>| item.key.to_string());
let takes = DropKind::new::<PanelDrag>(
DropPlace::Between,
move |drag: &PanelDrag, _: &DropAt<String>| matches!(grabbed(drag, self.library), Some(Grab::Item(k, _) | Grab::New(k, _)) if k == kind),
move |drag: &PanelDrag, _: &DropAt<String>| matches!(self.membership.grabbed(drag), Some(Grab::Item(k, _) | Grab::New(k, _)) if k == kind),
);
// A graphic is placed by a double click; every other item is used by a click.
let activate = if kind == ItemKind::Graphic { ActivateOn::DoubleClick } else { ActivateOn::Click };
@@ -565,7 +580,7 @@ impl<'a> View<'a> {
/// `kind` moves there, art or a swatch of that kind is added there. Nothing for an item dropped
/// beside itself.
fn dropped(&self, drag: &PanelDrag, kind: ItemKind, at: Option<(&'static str, &str)>, group: Option<usize>) -> Option<Act> {
match grabbed(drag, self.library)? {
match self.membership.grabbed(drag)? {
Grab::Item(k, item) if k == kind => (at.is_none_or(|(_, key)| key != item)).then(|| self.moved(kind, &item, at, group)),
Grab::New(k, mut params) if k == kind => {
if let Some((side, key)) = at {
@@ -761,7 +776,8 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
return;
};
let acts = RefCell::new(Vec::new());
let has_doc = app.session.active().is_some();
let active_document = app.session.active().map(|st| st.uid);
let has_doc = active_document.is_some();
{
// PLAN: assumed `UiState::{libraries_list_view: bool, library_search: String, library_groups_closed: HashSet<(String, String)>}` (ap.AS17, hub-w1).
let (session, state) = (&app.session, &mut app.ui);
@@ -769,7 +785,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
// Whether a character style's Apply Character Style is enabled (looked up only when the library has one).
let can_apply =
has_doc && !lib.char_styles.is_empty() && vectorcraft_engine::cmd::textstyles::target_style_attrs(session, &json!({}), false).is_some();
let view = View::new(&lib_id, lib, &state.library_search, state.libraries_list_view, session.fill_active, has_doc, can_apply);
let view = View::new(&lib_id, lib, &state.library_search, state.libraries_list_view, session.fill_active, active_document, can_apply);
Panel::new(tl!("Libraries")).footer(|ui, _| footer(ui, has_doc, &acts)).show(ui, |ui, _| {
let search = (!view.empty).then_some(&mut state.library_search);
header(ui, &session.libraries, &lib_id, &mut state.libraries_list_view, search, &acts);
@@ -248,6 +248,34 @@ fn an_empty_library_takes_art_anywhere() {
assert_eq!(graphics(&mut panel.app()).len(), 1);
}
#[test]
fn a_library_drop_does_not_collect_unrelated_artwork_after_a_document_switch() {
let mut app = app();
app.run("library.create", json!({"name": "Brand"})).unwrap();
let first = art_drag(&mut app, 10.0);
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
let second = art_drag(&mut app, 50.0);
assert!(matches!((&first, &second), (PanelDrag::Art { ids: a, .. }, PanelDrag::Art { ids: b, .. }) if a == b));
assert!(app.session.set_active(0));
let mut panel = Harness::new(app, with_source);
let from = panel.rect(SOURCE).center();
let to = panel.rect("This library is empty").center();
CARRIED.with(|carried| *carried.borrow_mut() = Some(first.clone()));
panel.drag_hold_role(SOURCE, craft::ui::contract::Role::LIST_ITEM, to - from);
assert!(craft::ui::runtime::drag::mark(panel.ctx()).is_some());
assert!(panel.app().session.set_active(1));
panel.frame();
let drag = craft::ui::runtime::drag::active::<PanelDrag>(panel.ctx());
assert_eq!(drag.as_deref(), Some(&first));
assert!(craft::ui::runtime::drag::mark(panel.ctx()).is_none());
panel.release();
assert!(graphics(&mut panel.app()).is_empty(), "the library never collects the target document's same-ID artwork");
drop_at(&mut panel, second, to);
assert_eq!(graphics(&mut panel.app()).len(), 1, "artwork from the current document can still be added");
}
fn gradients_and_patterns_come_from_swatches_and_paint_the_selection() {
let mut app = app();
app.run("library.create", json!({"name": "Brand"})).unwrap();