mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Merge pull request #9463 from dotCooCoo/fix/ui-swatches-reorder-document
Keep Swatches row reordering in its source document
This commit is contained in:
4 files changed
+87
-9
No files matched your search
@@ -41,6 +41,8 @@ pub enum PanelDrag {
|
||||
/// panel order.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct SwatchRows {
|
||||
/// The document whose rows the drag started on.
|
||||
pub document: u64,
|
||||
pub grabbed: String,
|
||||
pub names: Vec<String>,
|
||||
/// The names are colour groups.
|
||||
|
||||
@@ -409,8 +409,8 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let dragged_doc = Arc::clone(&thumbs.doc);
|
||||
let document = st.uid;
|
||||
let shown_app: &VectorcraftApp = app;
|
||||
let out = SwatchesPanel::new(&data, Bind::bound(&mut model.state), Srgba::rgb(0.0, 0.0, 0.0))
|
||||
.drag_payload(move |drag: &ListDrag<SwatchKey>| Some(dnd::drag_of(&dragged_doc, &drag.grabbed, dragged_rows(drag))))
|
||||
let browser = SwatchesPanel::new(&data, Bind::bound(&mut model.state), Srgba::rgb(0.0, 0.0, 0.0))
|
||||
.drag_payload(move |drag: &ListDrag<SwatchKey>| Some(dnd::drag_of(&dragged_doc, document, &drag.grabbed, dragged_rows(drag))))
|
||||
.thumb(thumbs, thumb)
|
||||
.row_icons(|key| match key {
|
||||
SwatchKey::Swatch(name) => row_icons(shown_app, name),
|
||||
@@ -422,8 +422,9 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
.select(SelectMode::Multi)
|
||||
.search(model.show_find)
|
||||
.reveal(focus.map(SwatchKey::Swatch))
|
||||
.footer(None)
|
||||
.show(ui);
|
||||
.footer(None);
|
||||
// A list drag is keyed by its browser ID. Each document has its own browser.
|
||||
let out = ui.push_id(("swatch-rows", document), |ui| browser.show(ui)).inner;
|
||||
app.ui.swatches_panel = model;
|
||||
let shown = shown_keys(&data);
|
||||
if let Some((at, _, alt)) = out.used
|
||||
@@ -766,7 +767,8 @@ fn footer(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let opts = actions::editable(app, &sel);
|
||||
let group = actions::selected_group(app, &sel).filter(|_| opts.is_none());
|
||||
let icon = |id: &'static str| IconSpec::svg(IconId::new(id));
|
||||
let rows_drag = |d: &PanelDrag| matches!(d, PanelDrag::Paint { rows: Some(_), .. });
|
||||
let document = app.session.active().map(|state| state.uid);
|
||||
let rows_drag = move |d: &PanelDrag| matches!(d, PanelDrag::Paint { rows: Some(rows), .. } if Some(rows.document) == document);
|
||||
let edit = if group.is_some() {
|
||||
FooterAction::new(icon("palette"), tl!("Edit or Apply Color Group"))
|
||||
} else {
|
||||
|
||||
@@ -4,9 +4,10 @@ use craft::color_ui::ui::SwatchKey;
|
||||
use craft::keymap::Mods;
|
||||
use craft::ui::contract::{DropAt, ReorderMove};
|
||||
|
||||
/// Swatches take paint payloads and artwork from the current document's canvas.
|
||||
/// Swatches take paints, and take swatch rows or canvas art from the current document.
|
||||
pub(super) fn accepts(drag: &PanelDrag, document: u64) -> bool {
|
||||
match drag {
|
||||
PanelDrag::Paint { rows: Some(rows), .. } => rows.document == document,
|
||||
PanelDrag::Paint { .. } => true,
|
||||
PanelDrag::Art { document: source, .. } => *source == document,
|
||||
_ => false,
|
||||
@@ -53,7 +54,7 @@ impl Drop {
|
||||
|
||||
/// The drag a row of `key` starts, moving rows `names`. Colour groups have no paint of their own
|
||||
/// (the chip under the pointer draws Registration's mark for Registration).
|
||||
pub(super) fn drag_of(d: &Document, key: &SwatchKey, names: Vec<String>) -> PanelDrag {
|
||||
pub(super) fn drag_of(d: &Document, document: u64, key: &SwatchKey, names: Vec<String>) -> PanelDrag {
|
||||
let name = name_of(key);
|
||||
let (paint, params) = match key {
|
||||
SwatchKey::Swatch(_) if name == REGISTRATION => (Paint::registration(), swatch_params(d, name)),
|
||||
@@ -63,7 +64,7 @@ pub(super) fn drag_of(d: &Document, key: &SwatchKey, names: Vec<String>) -> Pane
|
||||
},
|
||||
_ => (Paint::None, Value::Null),
|
||||
};
|
||||
let rows = SwatchRows { grabbed: name.to_string(), names, groups: matches!(key, SwatchKey::Group(_)) };
|
||||
let rows = SwatchRows { document, grabbed: name.to_string(), names, groups: matches!(key, SwatchKey::Group(_)) };
|
||||
PanelDrag::Paint { paint, params, rows: Some(rows), library: None }
|
||||
}
|
||||
|
||||
@@ -124,6 +125,9 @@ pub(super) enum FooterTarget {
|
||||
/// the button does. Other drags are left to their own targets.
|
||||
pub(super) fn on_footer_drop(app: &mut VectorcraftApp, target: FooterTarget, drag: &PanelDrag) {
|
||||
let PanelDrag::Paint { rows: Some(r), .. } = drag else { return };
|
||||
if !app.session.active().is_some_and(|state| state.uid == r.document) {
|
||||
return;
|
||||
}
|
||||
let rows = footer_rows(app, r);
|
||||
match target {
|
||||
FooterTarget::Delete => {
|
||||
|
||||
@@ -96,7 +96,61 @@ fn dropped_below(app: &mut VectorcraftApp, drag: PanelDrag, mods: Mods) {
|
||||
|
||||
/// The drag the row `name` starts, carrying rows `names`.
|
||||
fn rows_drag(app: &VectorcraftApp, name: &str, names: &[&str]) -> PanelDrag {
|
||||
dnd::drag_of(&doc(app), &key_of(app, name), names.iter().map(|n| n.to_string()).collect())
|
||||
let document = app.session.active().unwrap().uid;
|
||||
dnd::drag_of(&doc(app), document, &key_of(app, name), names.iter().map(|n| n.to_string()).collect())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_swatch_row_drag_reorders_only_its_source_document() {
|
||||
use crate::tests::panel_harness::Panel;
|
||||
use craft::ui::contract::{ListDrag, Role};
|
||||
use craft::ui::runtime::drag;
|
||||
use egui::{Key, Modifiers};
|
||||
|
||||
let mut app = app();
|
||||
let source = app.session.active().unwrap().uid;
|
||||
let before_source = names_of(&app, None);
|
||||
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
|
||||
let destination = app.session.active().unwrap().uid;
|
||||
let before_destination = names_of(&app, None);
|
||||
app.run("window.previousDocument", json!({})).unwrap();
|
||||
assert_eq!(app.session.active().unwrap().uid, source);
|
||||
|
||||
let mut panel = Panel::new(app, show);
|
||||
let drag_red = |panel: &mut Panel| {
|
||||
let red = "Red, #ed1c24";
|
||||
let from = panel.rect(red).center();
|
||||
let to = panel.rect("Green, #39b54a").center();
|
||||
panel.drag_hold_role(red, Role::LIST_ITEM, to - from);
|
||||
let grabbed = drag::active::<ListDrag<SwatchKey>>(panel.ctx()).map(|d| d.grabbed.clone());
|
||||
assert_eq!(grabbed, Some(SwatchKey::Swatch("Red".into())));
|
||||
};
|
||||
drag_red(&mut panel);
|
||||
panel.key(Key::F6, Modifiers::COMMAND);
|
||||
assert_eq!(panel.app().session.active().unwrap().uid, destination);
|
||||
panel.release();
|
||||
assert_eq!(names_of(&panel.app(), None), before_destination);
|
||||
|
||||
panel.key(Key::F6, Modifiers::COMMAND);
|
||||
assert_eq!(panel.app().session.active().unwrap().uid, source);
|
||||
assert_eq!(names_of(&panel.app(), None), before_source);
|
||||
drag_red(&mut panel);
|
||||
panel.release();
|
||||
assert_ne!(names_of(&panel.app(), None), before_source);
|
||||
panel.app().run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(names_of(&panel.app(), None), before_source);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_foreign_swatch_row_payload_cannot_move_or_replace_local_swatches() {
|
||||
let mut app = app();
|
||||
let drag = rows_drag(&app, "Red", &["Red"]);
|
||||
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
|
||||
let before = doc(&app);
|
||||
assert!(!dnd::accepts(&drag, app.session.active().unwrap().uid));
|
||||
dropped_below(&mut app, drag.clone(), none());
|
||||
dropped(&mut app, drag, "Green", alt());
|
||||
assert_eq!(doc(&app).swatches, before.swatches);
|
||||
}
|
||||
|
||||
/// Art `ids` dragged off the canvas.
|
||||
@@ -551,6 +605,22 @@ fn drops_on_the_buttons_and_the_panel_run_their_commands() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn footer_drops_ignore_rows_from_another_document() {
|
||||
let mut app = app();
|
||||
let drag = rows_drag(&app, "Orange", &["Orange"]);
|
||||
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
|
||||
app.run("swatch.new", json!({"name": "Orange", "color": "#ff8000"})).unwrap();
|
||||
let before = doc(&app);
|
||||
let undo = undo_steps(&app);
|
||||
for target in [dnd::FooterTarget::New, dnd::FooterTarget::Delete] {
|
||||
dnd::on_footer_drop(&mut app, target, &drag);
|
||||
assert_eq!(doc(&app), before);
|
||||
assert_eq!(undo_steps(&app), undo);
|
||||
assert!(!dialog_open(&app, crate::dialogs::confirm::KIND));
|
||||
}
|
||||
}
|
||||
|
||||
/// A selected swatch dragged onto New Swatch or Delete Swatch carries the selected swatches that
|
||||
/// the find field hides, which the browser leaves out of the drag, as a click on Delete Swatch
|
||||
/// takes them too. The copies are selected in panel order. A grabbed swatch outside the selection
|
||||
|
||||
Reference in new issue
Block a user