mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 11:45:55 +00:00
Merge pull request #9430 from dotCooCoo/fix/three-d-graphics-drag-document
Guard 3D and Materials graphics drops by document
This commit is contained in:
@@ -55,6 +55,8 @@ pub(crate) struct Data {
|
||||
pub bevels_file: bool,
|
||||
/// The active document, for the symbol pictures.
|
||||
pub doc: Option<Arc<Document>>,
|
||||
/// The document that owns the object IDs accepted by the Graphics drop target.
|
||||
pub document_uid: Option<u64>,
|
||||
/// The active document's version.
|
||||
pub revision: u64,
|
||||
/// Whether the active document has a selection.
|
||||
@@ -77,6 +79,7 @@ pub(crate) fn data(app: &VectorcraftApp, effect: Option<&str>, params: &Map<Stri
|
||||
bevels: effect.map(|e| revolve::bevel_options(app, params, e).0).unwrap_or_default(),
|
||||
bevels_file: app.session.bevels.path().is_some(),
|
||||
doc: st.map(|s| s.doc.clone()),
|
||||
document_uid: st.map(|s| s.uid),
|
||||
revision: st.map_or(0, |s| s.revision),
|
||||
selected: st.is_some_and(|s| !s.selection.objects.is_empty()),
|
||||
surfaces: None,
|
||||
|
||||
@@ -174,11 +174,12 @@ fn body(ui: &mut Ui, fields: &mut DialogFields, cx: &mut CustomContext, data: &D
|
||||
fn library(ui: &mut Ui, data: &Data, apply: bool) -> Option<Action> {
|
||||
Text::new(tl!("Graphics")).show(ui);
|
||||
let mut action = None;
|
||||
let (Some(doc), true) = (data.doc.clone(), true) else { return action };
|
||||
let (Some(doc), Some(uid)) = (data.doc.clone(), data.document_uid) else { return action };
|
||||
let names: Vec<String> = doc.symbols.iter().map(|s| s.name.clone()).collect();
|
||||
let keys = crate::panels::symbols::document_symbols(&doc, data.revision);
|
||||
let source = keyed(&names[..], |name: &String| name.clone()).with_text(|name| Cow::Borrowed(name.as_str()));
|
||||
let mut dropped = DropAction::<PanelDrag>::new(|drag| matches!(drag, PanelDrag::Art { .. })).show(ui, |ui, _| {
|
||||
let drop_art = DropAction::<PanelDrag>::new(move |drag| matches!(drag, PanelDrag::Art { document, .. } if *document == uid));
|
||||
let mut dropped = drop_art.show(ui, |ui, _| {
|
||||
TileGrid::new(tl!("Graphics")).activate(ActivateOn::Click).show_items(ui, &source, &mut |name, _| {
|
||||
let tip = if apply { name.clone() } else { format!("{name}: {}", tl!("Select a 3D object to map the graphic onto it.")) };
|
||||
let (doc, keys, name) = (doc.clone(), keys.clone(), name.clone());
|
||||
|
||||
@@ -686,6 +686,54 @@ fn art_can_be_added_as_a_graphic_before_any_3d_effect() {
|
||||
assert!(painted_text(&mut app, show).contains("Add as Single Graphic"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn three_d_graphics_accepts_artwork_only_from_its_document() {
|
||||
use crate::panel_drag::PanelDrag;
|
||||
use crate::tests::panel_harness::Panel;
|
||||
use craft::ui::contract::{ListDrag, ShowCollection as _};
|
||||
use craft::ui::list::{List, ListRow};
|
||||
use craft::ui::runtime::drag;
|
||||
|
||||
fn with_artwork(app: &mut VectorcraftApp, ui: &mut egui::Ui) {
|
||||
let st = app.session.active().unwrap();
|
||||
let payload = PanelDrag::Art { document: st.uid, ids: st.selection.objects.clone() };
|
||||
ui.allocate_ui(egui::vec2(ui.available_width(), 80.0), |ui| {
|
||||
let keys = craft::behavior::keyed(&[0_usize], |key: &usize| *key);
|
||||
List::<usize>::new("Artwork")
|
||||
.drag_payload(move |_: &ListDrag<usize>| Some(payload.clone()))
|
||||
.show_items(ui, &keys, &mut |_, _| ListRow::new("Dragged artwork"));
|
||||
});
|
||||
show(app, ui);
|
||||
}
|
||||
|
||||
let mut app = app();
|
||||
let source = app.session.active().unwrap().uid;
|
||||
let first = app.session.active().unwrap().selection.objects[0];
|
||||
let add_existing = |app: &mut VectorcraftApp| app.run("threeD.addGraphic", json!({"name": "Existing Graphic"})).unwrap();
|
||||
add_existing(&mut app);
|
||||
app.run("file.new", json!({"width": 400, "height": 400})).unwrap();
|
||||
let second = app.run("shape.rectangle", json!({"x": 30, "y": 30, "width": 50, "height": 50})).unwrap()["id"].as_u64().unwrap();
|
||||
add_existing(&mut app);
|
||||
assert_eq!(first.0, second, "the two documents have different artwork with the same local ID");
|
||||
assert!(app.session.set_active(0));
|
||||
|
||||
let mut p = Panel::new(app, with_artwork);
|
||||
let from = p.rect("Dragged artwork").center();
|
||||
let to = p.rect("Existing Graphic").center();
|
||||
p.drag_hold_role("Dragged artwork", craft::ui::contract::Role::LIST_ITEM, to - from);
|
||||
assert!(drag::mark(p.ctx()).is_some(), "Graphics accepts the artwork before switching documents");
|
||||
assert!(p.app().session.set_active(1));
|
||||
p.frame();
|
||||
let carried = drag::active::<PanelDrag>(p.ctx());
|
||||
assert!(matches!(carried.as_deref(), Some(PanelDrag::Art { document, .. }) if *document == source));
|
||||
assert!(drag::mark(p.ctx()).is_none(), "Graphics withdraws the drop indicator when the artwork is foreign");
|
||||
p.release();
|
||||
assert_eq!(p.app().session.active().unwrap().doc.symbols.len(), 1, "the foreign drop adds no symbol from the active document's same-ID artwork");
|
||||
|
||||
p.drag("Dragged artwork", "Existing Graphic", egui::Modifiers::NONE);
|
||||
assert_eq!(p.app().session.active().unwrap().doc.symbols.len(), 2, "a same-document drop still makes a graphic");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_graphics_handle_drags_it_over_its_face_in_one_undo_step() {
|
||||
let (mut app, _) = materials_app();
|
||||
|
||||
Reference in New Issue
Block a user