Merge pull request #9445 from dotCooCoo/fix/layers-footer-row-drag-document

Keep Layers footer drops in their source document
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 05:17:04 +07:00
commit 24347a0424
2 files changed
+62 -17

No files matched your search

+16 -17
View File
@@ -1,8 +1,8 @@
use craft::behavior::IconId;
use craft::keymap::Mods;
use craft::layers::ui::panel::MenuTarget; // PLAN: assumed path of craft-layers' panel types
use craft::layers::ui::panel::{LayersDrag, MenuTarget, document_hash};
use craft::layers::ui::tree::LayerRowKey;
use craft::ui::contract::{Entry, ListDrag, Show as _};
use craft::ui::contract::{Entry, Show as _};
use craft::ui::display::{Elide, Text};
use craft::ui::menu::{MenuRow, PanelRow};
use craft::ui::primitives::IconSpec;
@@ -347,19 +347,18 @@ fn spine_menu(tree: &DocLayers, blend: NodeId) -> Vec<MenuRow<String>> {
/// What was dropped on a footer action.
pub(super) enum FooterPayload<'a> {
/// Rows of the panel.
Rows(&'a [LayerRowKey<RowId>]),
Rows(&'a [&'a RowId]),
/// The target circle of an appearance (its node id).
Appearance(u64),
}
/// The node ids and the ruler guide indexes among `keys`.
fn row_ids(keys: &[LayerRowKey<RowId>]) -> (Vec<u64>, Vec<usize>) {
fn row_ids(keys: &[&RowId]) -> (Vec<u64>, Vec<usize>) {
let (mut nodes, mut guides) = (vec![], vec![]);
for key in keys {
match key {
LayerRowKey::Layer(RowId::Node(id) | RowId::Spine(id)) => nodes.push(id.0),
LayerRowKey::Layer(RowId::Guide(i)) => guides.push(*i),
_ => {}
RowId::Node(id) | RowId::Spine(id) => nodes.push(id.0),
RowId::Guide(i) => guides.push(*i),
}
}
(nodes, guides)
@@ -433,19 +432,19 @@ pub(crate) fn footer(tree: &DocLayers, ui: &mut Ui, out: &mut Vec<(String, Value
let locate_ok = !layers_only || super::rows::locate_layer_listed(tree.doc(), tree.root());
let root = tree.root();
let icon = |name: &'static str| IconSpec::svg(IconId::new(name));
let layer_rows = |keys: &[LayerRowKey<RowId>]| keys.iter().all(|key| matches!(key, LayerRowKey::Layer(_)));
let document = document_hash(st.uid);
let own = move |drag: &LayersDrag<RowId>| drag.document() == Some(document);
let duplicate = move |drag: &LayersDrag<RowId>| {
let (nodes, guides) = row_ids(&drag.layers());
own(drag) && !nodes.is_empty() && guides.is_empty() && !root.is_some_and(|r| nodes.contains(&r.0))
};
let actions = [
// PLAN: the framework's `accepts_drops` takes one payload type, so Delete cannot also take
// `PanelDrag` (the appearance drop `footer_drop` maps); it needs a second payload there.
FooterAction::new(icon("trash-2"), tl!("Delete Selection").to_string())
.takes_drops_disabled(true)
.accepts_drops(move |d: &ListDrag<LayerRowKey<RowId>>| layer_rows(&d.keys)),
FooterAction::new(icon("file-plus"), tl!("Create New Layer").to_string()).enabled(new_layer_ok).accepts_drops(
move |d: &ListDrag<LayerRowKey<RowId>>| {
let (nodes, guides) = row_ids(&d.keys);
layer_rows(&d.keys) && guides.is_empty() && !root.is_some_and(|r| nodes.contains(&r.0))
},
),
.accepts_drops(move |d: &LayersDrag<RowId>| own(d) && !d.layers().is_empty()),
FooterAction::new(icon("file-plus"), tl!("Create New Layer").to_string()).enabled(new_layer_ok).accepts_drops(duplicate),
FooterAction::new(icon("plus"), tl!("Create New Sublayer").to_string()).enabled(new_sublayer_ok),
FooterAction::new(icon("frame"), tl!("Make/Release Clipping Mask").to_string()),
FooterAction::new(icon("search"), if layers_only { tl!("Locate Layer").to_string() } else { tl!("Locate Object").to_string() })
@@ -462,8 +461,8 @@ pub(crate) fn footer(tree: &DocLayers, ui: &mut Ui, out: &mut Vec<(String, Value
}
out.extend(output.picked.and_then(|index| footer_pick(tree, index, output.mods)));
if let Some(drop) = &output.dropped {
let run = if let Some(d) = drop.payload::<ListDrag<LayerRowKey<RowId>>>() {
footer_drop(tree, drop.index, FooterPayload::Rows(&d.keys))
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 {
+46
View File
@@ -507,6 +507,52 @@ fn context_menu_others_actions_sublayer_state_enables_all_commands() {
assert!(lock_enabled, "locked sublayer enables Unlock All Layers");
}
#[test]
fn layer_row_footer_drops_after_next_document_leave_the_new_document_alone() {
use crate::tests::panel_harness::Panel;
use craft::layers::ui::panel::{LayersDrag, document_hash};
use craft::ui::contract::Role;
use craft::ui::runtime::drag;
use egui::{Key, Modifiers};
for (button, deletes) in [("Delete Selection", true), ("Create New Layer", false)] {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
let mut setup = |x| {
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
let source = app.session.active().unwrap().doc.layers[0].id;
name_row(&mut app, source, "Dragged Layer");
app.run("shape.rectangle", json!({"x": x, "y": 20, "width": 40, "height": 30})).unwrap();
app.run("layer.new", json!({"name": "Other Layer"})).unwrap();
(app.session.active().unwrap().uid, source)
};
let (first_doc, first_layer) = setup(10);
let (second_doc, second_layer) = setup(60);
assert_eq!(first_layer, second_layer, "the documents have different layers with the same local ID");
assert!(app.session.set_active(0));
let mut panel = Panel::new(app, show);
let from = panel.rect_role("Dragged Layer", Role::TREE_ITEM).center();
let to = panel.rect(button).center();
panel.drag_hold_role("Dragged Layer", Role::TREE_ITEM, to - from);
assert!(drag::mark(panel.ctx()).is_some(), "{button} accepts a drag from its own list");
panel.key(Key::F6, Modifiers::COMMAND);
assert_eq!(panel.app().session.active().unwrap().uid, second_doc);
let carried = drag::active::<LayersDrag<RowId>>(panel.ctx()).expect("the shortcut leaves the drag held");
assert_eq!(carried.document(), Some(document_hash(first_doc)));
assert!(drag::mark(panel.ctx()).is_none(), "{button} rejects a drag belonging to another document");
panel.release();
assert_eq!(panel.app().session.active().unwrap().doc.layers.len(), 2);
assert!(crate::dialogs::confirm::open_question(&panel.app()).is_none());
panel.drag("Dragged Layer", button, Modifiers::NONE);
if deletes {
assert!(crate::dialogs::confirm::open_question(&panel.app()).is_some(), "a same-document drop still asks to delete its layer");
} else {
assert_eq!(panel.app().session.active().unwrap().doc.layers.len(), 3, "a same-document drop still duplicates its layer");
}
}
}
/// The footer's layer count is in the accessibility tree, and it follows the document's layers.
#[test]
fn the_footer_counts_the_layers() {