mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 05:58:06 +00:00
Merge pull request #8249 from dotCooCoo/feat/swatches-drag-to-delete
Swatches: swatches dragged onto the Delete Swatch button are deleted
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
use super::dnd::{group_of, selection};
|
||||
use super::libraries::SwatchLibraries;
|
||||
use super::{Kind, LibraryKind, Ui, Value, VectorcraftApp, View, active_paint, json, library_panel, menu_item, pstate, set_pstate, widgets};
|
||||
use super::{
|
||||
Kind, LibraryKind, PanelDrag, Response, Ui, Value, VectorcraftApp, View, active_paint, json, library_panel, menu_item, pstate, set_pstate,
|
||||
widgets,
|
||||
};
|
||||
|
||||
/// The selected swatches and groups that can be deleted (all but None and Registration).
|
||||
pub(super) fn deletable(sel: &[String]) -> Vec<String> {
|
||||
@@ -84,14 +87,52 @@ pub(super) fn bottom(app: &mut VectorcraftApp, ui: &mut Ui, sel: &[String]) {
|
||||
if widgets::icon_button(ui, "dc-new-item", tl!("New Swatch"), false, 24.0).clicked() {
|
||||
new_swatch(app, sel, ui.input(|i| i.modifiers));
|
||||
}
|
||||
// Alt-click deletes without asking.
|
||||
// Alt-click deletes without asking. Swatches or color groups dragged from the list can be
|
||||
// dropped on it, also with nothing selected: the button is enabled while they are held.
|
||||
let del = deletable(sel);
|
||||
if widgets::icon_button_enabled(ui, "trash-2", tl!("Delete Swatch"), false, !del.is_empty(), 24.0).clicked() {
|
||||
let dragged = dragged_deletable(app, ui.ctx());
|
||||
let trash = widgets::icon_button_enabled(ui, "trash-2", tl!("Delete Swatch"), false, !del.is_empty() || !dragged.is_empty(), 24.0);
|
||||
if trash.clicked() && !del.is_empty() {
|
||||
delete(app, del, ui.input(|i| i.modifiers.alt));
|
||||
}
|
||||
drop_on_delete(app, ui, &trash, dragged);
|
||||
});
|
||||
}
|
||||
|
||||
/// The panel state key of the document (`uid`) the swatches being dragged from the list belong to.
|
||||
pub(super) const DRAG_DOC: &str = "swatch-drag-doc";
|
||||
|
||||
/// The swatches and color groups of the active document being dragged from the list that Delete
|
||||
/// Swatch can delete: neither None nor Registration, and still in the document. Empty for any other
|
||||
/// drag, and for a drag started in another document.
|
||||
fn dragged_deletable(app: &VectorcraftApp, ctx: &egui::Context) -> Vec<String> {
|
||||
let Some(st) = app.session.active() else { return vec![] };
|
||||
if pstate::<Option<u64>>(ctx, DRAG_DOC) != Some(st.uid) {
|
||||
return vec![];
|
||||
}
|
||||
match egui::DragAndDrop::payload::<PanelDrag>(ctx).as_deref() {
|
||||
Some(PanelDrag::Paint { rows: Some(r), .. }) => deletable(&r.names).into_iter().filter(|n| st.doc.swatch_name_taken(n)).collect(),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Swatches or color groups (`names`) dragged onto Delete Swatch (`button`): the button is
|
||||
/// outlined while they are held over it, and the release of the primary button asks to delete them
|
||||
/// as a plain click does ([`delete`]); Alt does not skip the question. The release of another
|
||||
/// button ends the drag, and other drags are left to their own targets.
|
||||
fn drop_on_delete(app: &mut VectorcraftApp, ui: &Ui, button: &Response, names: Vec<String>) {
|
||||
if names.is_empty() || button.dnd_hover_payload::<PanelDrag>().is_none() {
|
||||
return;
|
||||
}
|
||||
widgets::drop_target_outline(ui, button.rect);
|
||||
if !ui.input(|i| i.pointer.button_released(egui::PointerButton::Primary)) {
|
||||
return;
|
||||
}
|
||||
if button.dnd_release_payload::<PanelDrag>().is_some() {
|
||||
delete(app, names, false);
|
||||
}
|
||||
}
|
||||
|
||||
/// The colour group the selection points at: the last selected group, or the group of the last
|
||||
/// selected swatch.
|
||||
fn target_group(app: &VectorcraftApp, sel: &[String]) -> Option<String> {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::actions::{bottom, edit_group};
|
||||
use super::actions::{DRAG_DOC, bottom, edit_group};
|
||||
use super::dnd::{TileEvents, apply_drop, click_selection, highlighted, selection, tile_input, zone_input};
|
||||
use super::tiles::{
|
||||
draw_folder, draw_registration, folder_tile, global_mark, list_icons, list_row, list_row_name, pattern_thumb, tile_grid, tile_id,
|
||||
@@ -116,7 +116,9 @@ pub(super) fn body(app: &mut VectorcraftApp, ui: &mut Ui, salt: &'static str) {
|
||||
}
|
||||
}
|
||||
});
|
||||
let zone = ui.interact(out.inner_rect, ui.scope_id().with("swatch-drop"), Sense::hover());
|
||||
// The drop zone ends where the list does, so it does not reach over the buttons below a list
|
||||
// shrunk to its rows.
|
||||
let zone = ui.interact(out.inner_rect.intersect(ui.min_rect()), ui.scope_id().with("swatch-drop"), Sense::hover());
|
||||
zone_input(ui, &zone, &mut ev);
|
||||
let r = ui.min_rect();
|
||||
ui.ctx().accesskit_node_builder(ui.unique_id(), |node| {
|
||||
@@ -130,6 +132,9 @@ pub(super) fn body(app: &mut VectorcraftApp, ui: &mut Ui, salt: &'static str) {
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
if ev.started {
|
||||
set_pstate(ui.ctx(), DRAG_DOC, app.session.active().map(|st| st.uid));
|
||||
}
|
||||
if let Some(drop) = ev.drop {
|
||||
apply_drop(app, drop);
|
||||
}
|
||||
|
||||
@@ -114,6 +114,8 @@ pub(super) struct TileEvents {
|
||||
pub(super) drop: Option<Drop>,
|
||||
/// A drag is held over a tile or row.
|
||||
over: bool,
|
||||
/// A drag of swatches or color groups started on a tile or row.
|
||||
pub(super) started: bool,
|
||||
}
|
||||
|
||||
/// Clicks, double-clicks, tooltips and drag and drop of the tile or row `resp` of entry `e`
|
||||
@@ -133,6 +135,7 @@ pub(super) fn tile_input(ui: &Ui, resp: Response, e: &Entry, items: &[Entry], se
|
||||
.map(|x| x.name().to_string())
|
||||
.collect();
|
||||
egui::DragAndDrop::set_payload(ui.ctx(), drag_of(e, names));
|
||||
ev.started = true;
|
||||
}
|
||||
tile_drop(ui, &resp, e, list, ev);
|
||||
// The default swatches' names show in the UI's language; the document keeps them as they are.
|
||||
|
||||
@@ -596,6 +596,177 @@ fn only_a_primary_drag_moves_swatches_and_groups() {
|
||||
assert_eq!(app.session.doc().unwrap().history.undo.len(), undo + 1);
|
||||
}
|
||||
|
||||
/// One frame of the panel at `time` with `modifiers` held and the accessibility tree on; returns
|
||||
/// the frame's output.
|
||||
fn frame_out(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec<Event>, modifiers: Modifiers, time: f64) -> egui::FullOutput {
|
||||
ctx.enable_accesskit();
|
||||
let screen_rect = Some(Rect::from_min_size(Pos2::ZERO, vec2(280.0, 700.0)));
|
||||
let events = std::iter::once(Event::ModifiersChanged(modifiers)).chain(events).collect();
|
||||
let input = egui::RawInput { events, time: Some(time), screen_rect, ..Default::default() };
|
||||
let mut out = ctx.run_ui(input, |ui| show(app, ui));
|
||||
out.textures_delta.clear();
|
||||
out
|
||||
}
|
||||
|
||||
/// The Delete Swatch button in `out`: its rectangle and whether it is enabled.
|
||||
fn trash_button(out: &egui::FullOutput) -> (Rect, bool) {
|
||||
let nodes = &out.platform_output.accesskit_update.as_ref().expect("an accessibility tree").nodes;
|
||||
let node = nodes.iter().map(|(_, n)| n).find(|n| n.label().is_some_and(|l| l.trim() == "Delete Swatch")).expect("a Delete Swatch button");
|
||||
let b = node.bounds().expect("the button's bounds");
|
||||
(Rect::from_min_max(pos2(b.x0 as f32, b.y0 as f32), pos2(b.x1 as f32, b.y1 as f32)), !node.is_disabled())
|
||||
}
|
||||
|
||||
/// Whether `out` draws the drop outline around `rect`.
|
||||
fn outlined(out: &egui::FullOutput, rect: Rect) -> bool {
|
||||
out.shapes.iter().any(|c| match &c.shape {
|
||||
Shape::Rect(r) => r.rect.center().distance(rect.center()) < 0.5 && (r.stroke.width - 1.5).abs() < 0.01,
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Drag the tile of `name` with `modifiers` held onto Delete Swatch and release the primary button
|
||||
/// there, a frame per step from `time`. Returns whether the button was outlined and enabled while
|
||||
/// the drag was held over it.
|
||||
fn drag_to_trash(app: &mut VectorcraftApp, ctx: &egui::Context, name: &str, modifiers: Modifiers, time: f64) -> (bool, bool) {
|
||||
frame_out(app, ctx, vec![], modifiers, time);
|
||||
let (trash, _) = trash_button(&frame_out(app, ctx, vec![], modifiers, time + 0.05));
|
||||
let from = tile_center(ctx, name);
|
||||
let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers };
|
||||
frame_out(app, ctx, vec![Event::PointerMoved(from), button(from, true)], modifiers, time + 0.1);
|
||||
frame_out(app, ctx, vec![Event::PointerMoved(from + vec2(6.0, 4.0))], modifiers, time + 0.2);
|
||||
frame_out(app, ctx, vec![Event::PointerMoved(trash.center())], modifiers, time + 0.3);
|
||||
let held = frame_out(app, ctx, vec![Event::PointerMoved(trash.center())], modifiers, time + 0.4);
|
||||
let seen = (outlined(&held, trash), trash_button(&held).1);
|
||||
frame_out(app, ctx, vec![Event::PointerMoved(trash.center()), button(trash.center(), false)], modifiers, time + 0.5);
|
||||
seen
|
||||
}
|
||||
|
||||
fn undo_steps(app: &VectorcraftApp) -> usize {
|
||||
app.session.doc().unwrap().history.undo.len()
|
||||
}
|
||||
|
||||
fn dialog_message(app: &VectorcraftApp) -> Option<String> {
|
||||
app.ui.dialog.as_ref().map(|d| d.str("message"))
|
||||
}
|
||||
|
||||
/// Swatches and color groups dragged from the list onto Delete Swatch are deleted as a plain click
|
||||
/// on the button deletes the selection: the drop asks first, Cancel keeps them, and OK deletes them
|
||||
/// in one undo step. The button is outlined and enabled while they are held over it, also with
|
||||
/// nothing selected. A selected swatch carries the other selected swatches with it, but not the
|
||||
/// selected groups, None or Registration, and Alt does not skip the question.
|
||||
#[test]
|
||||
fn swatches_dropped_on_delete_swatch_are_deleted_after_asking() {
|
||||
let mut app = app();
|
||||
let ctx = context();
|
||||
let doc = |app: &VectorcraftApp| app.session.doc().unwrap().doc.clone();
|
||||
let has_group = |app: &VectorcraftApp, g: &str| doc(app).swatch_groups.iter().any(|x| x.name == g);
|
||||
assert!(!trash_button(&frame_out(&mut app, &ctx, vec![], Modifiers::NONE, 0.0)).1, "disabled with nothing selected");
|
||||
let undo = undo_steps(&app);
|
||||
// An unselected swatch, nothing selected.
|
||||
assert_eq!(drag_to_trash(&mut app, &ctx, "Orange", Modifiers::NONE, 1.0), (true, true), "outlined and enabled while held");
|
||||
assert_eq!(dialog_message(&app).as_deref(), Some("Delete “Orange”?"));
|
||||
crate::dialogs::cancel(&mut app);
|
||||
assert!(doc(&app).swatch("Orange").is_some(), "Cancel keeps it");
|
||||
assert_eq!(undo_steps(&app), undo, "Cancel adds no undo step");
|
||||
drag_to_trash(&mut app, &ctx, "Orange", Modifiers::NONE, 2.0);
|
||||
crate::dialogs::confirm(&mut app).unwrap();
|
||||
assert!(app.ui.dialog.is_none() && doc(&app).swatch("Orange").is_none(), "OK deletes it");
|
||||
assert_eq!(undo_steps(&app), undo + 1, "one undo step");
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
assert!(doc(&app).swatch("Orange").is_some(), "Undo brings it back");
|
||||
// A selected swatch carries the selected swatches; Alt still asks.
|
||||
let sel = ["[None]", "Red", "Grays", "Lime"].map(String::from).to_vec();
|
||||
set_pstate(&ctx, "swatch-selected", sel);
|
||||
let undo = undo_steps(&app);
|
||||
assert_eq!(drag_to_trash(&mut app, &ctx, "Lime", Modifiers::ALT, 3.0), (true, true));
|
||||
assert_eq!(dialog_message(&app).as_deref(), Some("Delete these 2 swatches and groups?"));
|
||||
crate::dialogs::confirm(&mut app).unwrap();
|
||||
let d = doc(&app);
|
||||
assert!(d.swatch("Red").is_none() && d.swatch("Lime").is_none(), "the dragged selection is deleted");
|
||||
assert!(has_group(&app, "Grays") && d.swatch("[None]").is_some() && d.swatch("Orange").is_some(), "the rest stays");
|
||||
assert_eq!(undo_steps(&app), undo + 1, "one undo step for both");
|
||||
// A color group is deleted with its swatches.
|
||||
drag_to_trash(&mut app, &ctx, "Grays", Modifiers::NONE, 4.0);
|
||||
assert_eq!(dialog_message(&app).as_deref(), Some("Delete “Grays”?"));
|
||||
crate::dialogs::confirm(&mut app).unwrap();
|
||||
assert!(!has_group(&app, "Grays") && doc(&app).swatch("K=50").is_none());
|
||||
assert_eq!(undo_steps(&app), undo + 2);
|
||||
// None and Registration are never deleted, and the button does not take them.
|
||||
set_pstate(&ctx, "swatch-selected", Vec::<String>::new());
|
||||
let kept = doc(&app);
|
||||
for (i, name) in ["[None]", REGISTRATION].into_iter().enumerate() {
|
||||
assert_eq!(drag_to_trash(&mut app, &ctx, name, Modifiers::NONE, 5.0 + i as f64), (false, false), "{name}");
|
||||
assert_eq!(dialog_message(&app), None, "{name}");
|
||||
assert_eq!(doc(&app), kept, "{name}");
|
||||
}
|
||||
assert_eq!(undo_steps(&app), undo + 2);
|
||||
}
|
||||
|
||||
/// Delete Swatch takes only swatches dragged from the active document's list: a paint dragged from
|
||||
/// elsewhere, art, and a swatch dragged from another document whose name the active document also
|
||||
/// has delete nothing, ask nothing and are not outlined. A secondary button released while a swatch
|
||||
/// is held over the button ends the drag without asking.
|
||||
#[test]
|
||||
fn delete_swatch_ignores_other_drags() {
|
||||
let mut app = app();
|
||||
let id = app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50})).unwrap()["id"].as_u64().unwrap();
|
||||
let ctx = context();
|
||||
let (trash, _) = trash_button(&frame_out(&mut app, &ctx, vec![], Modifiers::NONE, 0.0));
|
||||
let (before, undo) = (app.session.doc().unwrap().doc.clone(), undo_steps(&app));
|
||||
let unchanged = |app: &VectorcraftApp, case: &str| {
|
||||
assert_eq!(dialog_message(app), None, "{case}: asks nothing");
|
||||
assert_eq!(app.session.doc().unwrap().doc, before, "{case}: deletes nothing");
|
||||
assert_eq!(undo_steps(app), undo, "{case}: no undo step");
|
||||
};
|
||||
// The payloads are set directly, as if dragged from this document's list.
|
||||
set_pstate(&ctx, super::actions::DRAG_DOC, app.session.active().map(|st| st.uid));
|
||||
let release = |at| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE };
|
||||
let cases = [
|
||||
("a paint", PanelDrag::paint(Paint::solid(Color::from_hex("#ed1c24").unwrap()))),
|
||||
("art", PanelDrag::Art(vec![vectorcraft_doc::NodeId(id)])),
|
||||
];
|
||||
for (i, (case, drag)) in cases.into_iter().enumerate() {
|
||||
egui::DragAndDrop::set_payload(&ctx, drag);
|
||||
let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 1.0 + i as f64);
|
||||
assert!(!outlined(&held, trash), "{case}: not outlined");
|
||||
frame_out(&mut app, &ctx, vec![release(trash.center())], Modifiers::NONE, 1.5 + i as f64);
|
||||
unchanged(&app, case);
|
||||
egui::DragAndDrop::clear_payload(&ctx);
|
||||
}
|
||||
// A secondary release over the button, then the primary release.
|
||||
frame_out(&mut app, &ctx, vec![], Modifiers::NONE, 3.0);
|
||||
let from = tile_center(&ctx, "Orange");
|
||||
let press = |at, button, pressed| Event::PointerButton { pos: at, button, pressed, modifiers: Modifiers::NONE };
|
||||
frame_out(&mut app, &ctx, vec![Event::PointerMoved(from), press(from, PointerButton::Primary, true)], Modifiers::NONE, 3.1);
|
||||
frame_out(&mut app, &ctx, vec![Event::PointerMoved(from + vec2(6.0, 4.0))], Modifiers::NONE, 3.2);
|
||||
frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 3.3);
|
||||
let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 3.4);
|
||||
assert!(outlined(&held, trash), "Orange is held over the button");
|
||||
frame_out(&mut app, &ctx, vec![press(trash.center(), PointerButton::Secondary, true)], Modifiers::NONE, 3.5);
|
||||
frame_out(&mut app, &ctx, vec![press(trash.center(), PointerButton::Secondary, false)], Modifiers::NONE, 3.6);
|
||||
unchanged(&app, "a secondary release");
|
||||
frame_out(&mut app, &ctx, vec![release(trash.center())], Modifiers::NONE, 3.7);
|
||||
unchanged(&app, "the primary release after it");
|
||||
// A swatch dragged from this document while another document with the same swatches becomes
|
||||
// active.
|
||||
frame_out(&mut app, &ctx, vec![], Modifiers::NONE, 4.0);
|
||||
let from = tile_center(&ctx, "Orange");
|
||||
frame_out(&mut app, &ctx, vec![Event::PointerMoved(from), press(from, PointerButton::Primary, true)], Modifiers::NONE, 4.1);
|
||||
frame_out(&mut app, &ctx, vec![Event::PointerMoved(from + vec2(6.0, 4.0))], Modifiers::NONE, 4.2);
|
||||
assert!(egui::DragAndDrop::payload::<PanelDrag>(&ctx).is_some(), "Orange is dragged");
|
||||
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
|
||||
let undo_new = undo_steps(&app);
|
||||
frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 4.3);
|
||||
let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 4.4);
|
||||
let (trash_new, enabled) = trash_button(&held);
|
||||
assert!(!outlined(&held, trash_new) && !enabled, "not outlined or enabled in the other document");
|
||||
frame_out(&mut app, &ctx, vec![release(trash_new.center())], Modifiers::NONE, 4.5);
|
||||
assert_eq!(dialog_message(&app), None, "the other document asks nothing");
|
||||
assert!(app.session.doc().unwrap().doc.swatch("Orange").is_some() && undo_steps(&app) == undo_new, "the other document keeps Orange");
|
||||
assert!(app.session.set_active(0));
|
||||
unchanged(&app, "the first document");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn art_dropped_on_the_panel_becomes_a_pattern_swatch() {
|
||||
let mut app = app();
|
||||
|
||||
Reference in New Issue
Block a user