From b4d2f8cdcebe203f870a47675df992f193c61cec Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 12:41:31 +0000 Subject: [PATCH] craft-layers: a mask thumbnail dropped on another layer's row moves the mask there, Alt copies (LayerDrop::Mask) The rows take the mask through the list's drop kind for the panel's drag payload. LayerTree::accepts_mask_drop decides which layers take it; by default a layer takes a mask of a kind it does not have. --- crates/layers/src/ui/panel/body.rs | 11 ++- crates/layers/src/ui/panel/body/map.rs | 7 +- crates/layers/src/ui/panel/drag.rs | 34 +++++++-- crates/layers/src/ui/panel/events.rs | 28 ++++++- crates/layers/src/ui/panel/mod.rs | 6 +- crates/layers/src/ui/tree.rs | 12 ++- crates/layers/tests/it/main.rs | 1 + crates/layers/tests/it/panel_drag_menu.rs | 91 +++++++++++++++++++++++ 8 files changed, 175 insertions(+), 15 deletions(-) create mode 100644 crates/layers/tests/it/panel_drag_menu.rs diff --git a/crates/layers/src/ui/panel/body.rs b/crates/layers/src/ui/panel/body.rs index 88376d1c8..056dab190 100644 --- a/crates/layers/src/ui/panel/body.rs +++ b/crates/layers/src/ui/panel/body.rs @@ -11,7 +11,8 @@ use rows::{RevealPlan, list_selection}; use craft_behavior::{ColumnWidth, IconId}; use craft_ui::contract::{ - Bind, CollectionEvent, ColumnKind, ColumnSpec, ListDrag, PendingDrop, Props as _, ReorderRule, ShowCollection as _, ToggleColumn, + Bind, CollectionEvent, ColumnKind, ColumnSpec, DropAt, DropKind, DropPlace, ListDrag, PendingDrop, Props as _, ReorderRule, ShowCollection as _, + ToggleColumn, }; use craft_ui::list::List; use craft_ui::runtime::env::Label; @@ -19,7 +20,7 @@ use egui::Ui; use super::Session; use super::context::PanelContext; -use super::drag::{LayersDrag, accepts_drop, layer_drop}; +use super::drag::{LayersDrag, accepts_drop, accepts_mask_drop, layer_drop}; use super::events::{LayerDrop, LayerMove, LayersEvent}; use super::row::{EYE, LOCK, NAME, build_row}; use crate::ui::tree::{LayerRowKey, LayerTree}; @@ -67,6 +68,7 @@ pub(super) fn show_body( columns.push(ColumnSpec::new(NAME, Label::Empty, ColumnWidth::fill(1.0))); let parent_of = |id: &T::Id| tree.parent(id); let ctx = ui.ctx().clone(); + let list_id = cx.list; let mut list = List::new(cx.words.list.clone()) .id(cx.list) @@ -81,7 +83,10 @@ pub(super) fn show_body( .accepts(|drag: &ListDrag>, _over, insert| { accepts_drop(tree, drag, &order, insert, craft_ui::runtime::pointer::frame(&ctx).mods) }) - .drag_payload(|drag: &ListDrag>| Some(LayersDrag::of_rows(drag))); + .drag_payload(|drag: &ListDrag>| Some(LayersDrag::of_rows(drag))) + .takes_drop(DropKind::new(DropPlace::Onto, move |drag: &LayersDrag, at: &DropAt>| { + accepts_mask_drop(tree, list_id, drag, at) + })); if let Some(key) = reveal_key { list = list.reveal(key); } diff --git a/crates/layers/src/ui/panel/body/map.rs b/crates/layers/src/ui/panel/body/map.rs index ae61d13fc..bada4da1f 100644 --- a/crates/layers/src/ui/panel/body/map.rs +++ b/crates/layers/src/ui/panel/body/map.rs @@ -10,7 +10,7 @@ use std::sync::Arc; use super::super::Session; use super::super::context::PanelContext; -use super::super::drag::drop_event; +use super::super::drag::{LayersDrag, drop_event, mask_drop_event}; use super::super::events::{ActivateOn, LayerColumn, LayerSelect, LayerToggle, LayersEvent, MenuTarget}; use super::super::menu::OpenMenu; use super::super::row::{EYE, FX_PART, LOCK_PART, NAME, OPTIONS_PART}; @@ -117,6 +117,11 @@ pub(super) fn map_events( } } CollectionEvent::Reorder(edit) => events.extend(drop_event(edit, order, parent_of, filtered)), + CollectionEvent::Dropped { at, op, payload, .. } => { + if let Some(drag) = payload.get::>() { + events.extend(mask_drop_event(cx.list, drag, &at, op, Arc::from(cx.words.list.as_str()))); + } + } CollectionEvent::Toggle(edit) => toggle_events(edit, &mut session.sweeps, &mut toggle_label, events), CollectionEvent::Rename(edit) => { if let EditEvent::Commit { after, .. } | EditEvent::Set { after, .. } = edit diff --git a/crates/layers/src/ui/panel/drag.rs b/crates/layers/src/ui/panel/drag.rs index f6d15de23..2065c5158 100644 --- a/crates/layers/src/ui/panel/drag.rs +++ b/crates/layers/src/ui/panel/drag.rs @@ -1,14 +1,14 @@ //! Drag and drop: what a reorder of the list means for layers, effects and Smart Filters, the //! payload that footer buttons accept, and the stacking commands. -use craft_behavior::{EditEvent, Mixed}; +use craft_behavior::{DropOperation, EditEvent, Mixed}; use craft_keymap::Mods; -use craft_ui::contract::{ListDrag, ReorderMove}; +use craft_ui::contract::{DropAt, ListDrag, ReorderMove}; use std::sync::Arc; use super::LayersPanelCommands; -use super::events::{LayerDrop, LayerMove, LayerPosition, LayersEvent, RowTransfer, SubRowMove}; -use crate::ui::tree::{LayerKind, LayerRowKey, LayerTree, SubRowGroup}; +use super::events::{LayerDrop, LayerMove, LayerPosition, LayersEvent, MaskMove, RowTransfer, SubRowMove}; +use crate::ui::tree::{LayerKind, LayerPart, LayerRowKey, LayerTree, SubRowGroup}; /// What a drag from the Layers panel carries for the drop targets outside its rows: the footer buttons, the rows /// that take a mask, and the app's own targets (a document tab that copies the dragged layers into its document). @@ -151,10 +151,34 @@ pub(super) fn accepts_drop( transfer.rows.iter().all(|row| !matches!(row, LayerRowKey::Filters(..))) || tree.info(&transfer.to).kind == LayerKind::SmartObject } Some(LayerDrop::SubRows(_)) => true, - None => false, + Some(LayerDrop::Mask(_)) | None => false, } } +/// The mask a drag of `list` carries and the layer row it is over, when that row is another layer's. +fn mask_onto<'d, K: PartialEq>(list: egui::Id, drag: &'d LayersDrag, at: &'d DropAt>) -> Option<(&'d K, LayerPart, &'d K)> { + let (DropAt::Onto(LayerRowKey::Layer(to)), [LayerRowKey::Mask(from, part)]) = (at, drag.keys.as_slice()) else { return None }; + (drag.list == list && to != from).then_some((from, *part, to)) +} + +/// Whether the mask thumbnail a drag of the rows' list `list` carries may land on the row under the pointer. +pub(super) fn accepts_mask_drop(tree: &T, list: egui::Id, drag: &LayersDrag, at: &DropAt>) -> bool { + mask_onto(list, drag, at).is_some_and(|(from, part, to)| tree.accepts_mask_drop(from, part, to)) +} + +/// The drop a mask thumbnail makes on the row it was released over, one `Set` named `label`. +pub(super) fn mask_drop_event( + list: egui::Id, + drag: &LayersDrag, + at: &DropAt>, + op: DropOperation, + label: Arc, +) -> Option> { + let (from, part, to) = mask_onto(list, drag, at)?; + let drop = LayerDrop::Mask(MaskMove::new(from.clone(), part, to.clone(), op == DropOperation::Copy)); + Some(LayersEvent::Drop(EditEvent::set(Mixed::Mixed, drop, label, None))) +} + /// The reorder event of the list as the panel reports it: one `Set` per drop. pub(super) fn drop_event( edit: EditEvent>>, diff --git a/crates/layers/src/ui/panel/events.rs b/crates/layers/src/ui/panel/events.rs index 727645f5e..9c4e5c6a0 100644 --- a/crates/layers/src/ui/panel/events.rs +++ b/crates/layers/src/ui/panel/events.rs @@ -1,5 +1,5 @@ //! `LayersEvent` and the types it carries (`LayerSelect`, `LayerToggle`, `LayerDrop`, `LayerMove`, -//! `SubRowMove`, `RowTransfer`). +//! `SubRowMove`, `RowTransfer`, `MaskMove`). //! //! The panel reports what the user did in the order it happened; the app applies each event with //! its own commands and undo, in the pass it is reported. @@ -112,7 +112,8 @@ pub enum LayersEvent { Properties(LayerPropertyEdit), /// The filter bar's edit, for a filter binding that does not write through. Filter(EditEvent), - /// A drop of rows (a drag, a stacking key in the list, or a stacking command): one `Set`, one undo step. + /// A drop of rows or a mask thumbnail (a drag, a stacking key in the list, or a stacking command): one `Set`, one + /// undo step. Drop(EditEvent>), /// A press on a footer button. Footer(LayersFooterAction), @@ -233,6 +234,8 @@ pub enum LayerDrop { SubRows(SubRowMove), /// Effects or Smart Filters moved to another layer. Transfer(RowTransfer), + /// A mask thumbnail dropped on another layer's row: the mask moves there. + Mask(MaskMove), } /// Layers moved or copied next to a target layer. @@ -287,6 +290,27 @@ pub struct RowTransfer { pub copy: bool, } +/// A mask moved or copied onto another layer. +#[derive(Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub struct MaskMove { + /// The layer the mask comes from. + pub layer: Id, + /// The mask: [`LayerPart::LayerMask`] or [`LayerPart::VectorMask`]. + pub part: LayerPart, + /// The layer it goes to. + pub to: Id, + /// Alt: copy instead of move. + pub copy: bool, +} + +impl MaskMove { + /// The mask `part` of `layer` lands on `to`. + pub fn new(layer: Id, part: LayerPart, to: Id, copy: bool) -> Self { + Self { layer, part, to, copy } + } +} + /// Where the host's row menu was opened. #[derive(Clone, Debug, PartialEq, Eq)] #[non_exhaustive] diff --git a/crates/layers/src/ui/panel/mod.rs b/crates/layers/src/ui/panel/mod.rs index ab867b89f..94fa6ebcf 100644 --- a/crates/layers/src/ui/panel/mod.rs +++ b/crates/layers/src/ui/panel/mod.rs @@ -16,7 +16,8 @@ mod stories; pub use drag::LayersDrag; pub use events::{ - ActivateOn, LayerColumn, LayerDrop, LayerMove, LayerPosition, LayerSelect, LayerToggle, LayersEvent, MenuTarget, RowTransfer, SubRowMove, + ActivateOn, LayerColumn, LayerDrop, LayerMove, LayerPosition, LayerSelect, LayerToggle, LayersEvent, MaskMove, MenuTarget, RowTransfer, + SubRowMove, }; pub use footer::{LayersFooterAction, LayersFooterButton}; pub use menu::{LayersMenuAction, LayersPanelCommands, LayersPanelKeys, panel_rows}; @@ -151,7 +152,8 @@ impl Default for Session { /// - **Pointer**: clicks select (Cmd/Ctrl toggles, Shift extends, the empty space clears), the eye /// and lock columns sweep, Alt-click on an eye shows only that layer, the eye's right-click opens /// its colour menu, a modified thumbnail click is a [`LayersEvent::ThumbClick`], double clicks -/// rename or activate, rows drag to reorder (Alt copies) and onto the footer. +/// rename or activate, rows drag to reorder (Alt copies) and onto the footer, and a mask thumbnail drags onto +/// another layer's row to move the mask there (Alt copies). /// - **Right to left**: the list and the footer mirror. #[must_use = "show the panel with `show`"] #[derive(craft_ui::contract::Pattern)] diff --git a/crates/layers/src/ui/tree.rs b/crates/layers/src/ui/tree.rs index 830ffd7fb..937c37ff0 100644 --- a/crates/layers/src/ui/tree.rs +++ b/crates/layers/src/ui/tree.rs @@ -86,6 +86,14 @@ pub trait LayerTree { true } + /// Whether the mask `part` of layer `from`, dragged by its thumbnail, may drop onto the row of layer `to`, another + /// layer. A row that refuses shows no marker and takes nothing. By default a layer takes a mask of a kind it does + /// not have (Photoshop asks before it replaces one); PhotoCraft takes only layer masks. + fn accepts_mask_drop(&self, from: &Self::Id, part: LayerPart, to: &Self::Id) -> bool { + let _ = from; + !self.info(to).masks.iter().any(|mask| mask.part == part) + } + /// The layer under the pointer on the canvas, drawn with the list's hover look. fn hovered(&self) -> Option { None @@ -359,8 +367,8 @@ pub enum LayerRowKey { Effects(Id, Option), /// A Smart Filters row: `None` is the header row, `Some(i)` the `i`-th filter, top first. Filters(Id, Option), - /// A mask thumbnail being dragged: a drag payload only, never a row. Only the footer's - /// Delete takes it. + /// A mask thumbnail being dragged: a drag payload only, never a row. The footer's Delete + /// and the rows of other layers take it. Mask(Id, LayerPart), } diff --git a/crates/layers/tests/it/main.rs b/crates/layers/tests/it/main.rs index c0f828fed..9942f93bd 100644 --- a/crates/layers/tests/it/main.rs +++ b/crates/layers/tests/it/main.rs @@ -1,5 +1,6 @@ //! The integration binary of craft-layers: every test is a registered trial. +mod panel_drag_menu; mod panel_filter; mod panel_input; mod panel_order; diff --git a/crates/layers/tests/it/panel_drag_menu.rs b/crates/layers/tests/it/panel_drag_menu.rs new file mode 100644 index 000000000..4a3bcc385 --- /dev/null +++ b/crates/layers/tests/it/panel_drag_menu.rs @@ -0,0 +1,91 @@ +//! `LayersPanel` drags and menus: a mask thumbnail dropped on another layer, and what a drag carries for the app's +//! own drop targets. + +use craft_behavior::EditEvent; +use craft_layers::ui::mock::FakeLayers; +use craft_layers::ui::panel::{LayerDrop, LayersDrag, LayersEvent, LayersPanel, MaskMove}; +use craft_layers::ui::tree::LayerPart; +use craft_ui::contract::Entry; +use craft_ui::testing::{Fixture, Target, trial}; +use egui::{Modifiers, Pos2, Vec2}; +use std::cell::RefCell; +use std::rc::Rc; + +type Seen = Rc>>>; + +/// The room of the panel: wide enough for a row's parts, tall enough for the sample tree and the footer. +const PANEL: Vec2 = Vec2::new(420.0, 560.0); + +/// The panel over the sample tree, with a row menu of one entry, and the events it reports. +fn rig() -> (Fixture, Seen) { + let seen: Seen = Rc::default(); + let sink = Rc::clone(&seen); + let tree = FakeLayers::sample(); + let mut fixture = Fixture::new(move |ui| { + let out = LayersPanel::new(&tree).row_menu(|_| vec![Entry::new(String::from("rename"), "Rename Layer…")]).show(ui); + sink.borrow_mut().extend(out.events); + }); + fixture.room(PANEL); + fixture.settle(); + (fixture, seen) +} + +fn node(fixture: &Fixture, label: &str) -> Target { + fixture.by_label(label).unwrap_or_else(|| panic!("no node named {label:?}")) +} + +/// A point on the layer mask thumbnail of `layer`, which follows its link chain. +fn mask_thumb(fixture: &Fixture, layer: &str) -> Pos2 { + let link = node(fixture, &format!("Unlink layer mask of {layer}")); + Pos2::new(link.rect.right() + link.rect.width() * 0.75, link.rect.center().y) +} + +/// Drags from `from` and releases on the middle of the row named `onto`. +fn drag_onto(fixture: &mut Fixture, from: Pos2, onto: &str) { + let to = node(fixture, onto).center(); + fixture.drag_hold_at(from, to - from).release_at(to).idle(2); +} + +/// The mask drops among the events reported since the last call. +fn mask_drops(seen: &Seen) -> Vec> { + seen.borrow_mut() + .drain(..) + .filter_map(|event| match event { + LayersEvent::Drop(EditEvent::Set { after: LayerDrop::Mask(mv), .. }) => Some(mv), + _ => None, + }) + .collect() +} + +/// A layer mask's thumbnail dropped on another layer's row moves the mask there and Alt copies it; a layer that has a +/// layer mask already takes none. +#[trial] +fn a_mask_thumbnail_dropped_on_another_layer_moves_the_mask_there() { + let (mut fixture, seen) = rig(); + let from = mask_thumb(&fixture, "Masked"); + drag_onto(&mut fixture, from, "Title"); + assert_eq!(mask_drops(&seen), [MaskMove::new(3, LayerPart::LayerMask, 4, false)], "onto Title"); + fixture.hold(Modifiers::ALT); + drag_onto(&mut fixture, from, "Title"); + fixture.hold(Modifiers::NONE); + assert_eq!(mask_drops(&seen), [MaskMove::new(3, LayerPart::LayerMask, 4, true)], "Alt copies"); + drag_onto(&mut fixture, from, "Levels 1"); + assert_eq!(mask_drops(&seen), [], "Levels 1 has a layer mask already"); +} + +/// While layer rows are dragged, the drag session carries their layers for the app's targets outside the panel (a +/// document tab); a mask's drag carries no layers. +#[trial] +fn a_row_drag_carries_its_layers_for_targets_outside_the_panel() { + let (mut fixture, _) = rig(); + let held = |fixture: &Fixture| craft_ui::runtime::drag::active::>(fixture.ctx()).expect("a drag of the panel is held"); + let title = node(&fixture, "Title").center(); + let away = Vec2::new(0.0, 40.0); + fixture.drag_hold_at(title, away); + assert_eq!(held(&fixture).layers(), [&4], "the dragged row's layer"); + fixture.release_at(title + away).idle(2); + let mask = mask_thumb(&fixture, "Masked"); + fixture.drag_hold_at(mask, away); + assert!(held(&fixture).layers().is_empty(), "a mask is no layer"); + fixture.release_at(mask + away).idle(2); +}