Keep slice dialog actions on their opening selection

This commit is contained in:
dotCooCoo committed 2026-11-26 13:41:27 -08:00
1 parent c646504cec
commit 8bc385823f
4 files changed
+153 -8

No files matched your search

+5
View File
@@ -189,6 +189,11 @@ pub fn specs() -> Vec<CommandSpec> {
}
impl Session {
/// The selected slices, including selected objects that are object slices.
pub fn selected_slice_ids(&self) -> Vec<NodeId> {
self.active().map_or_else(Vec::new, |st| selected_slices(&st.doc, &st.selection))
}
/// View → Hide Slices.
pub fn slices_hidden(&self) -> bool {
self.menu.slices_hidden
+1
View File
@@ -240,6 +240,7 @@ impl VectorcraftApp {
three_d_dialog: None,
three_d: Default::default(),
export_dialogs: Default::default(),
slice_dialogs: Default::default(),
web_export: Default::default(),
flattener_preview: Default::default(),
flatten_dialog: Default::default(),
+145 -8
View File
@@ -12,8 +12,8 @@ use craft::draw::ui::slice::{
DivideBy, DivideSlices, DivideSlicesDialog, SliceBackground, SliceCellAlign, SliceCellVAlign, SliceKind, SliceOptions, SliceOptionsDialog,
};
use craft::theme::Srgba;
use craft::ui::dialog::DialogResult;
use serde_json::{Map, Value, json};
use vectorcraft_doc::NodeId;
use crate::VectorcraftApp;
// PLAN: assumed `crate::canvas::general_lengths(app: &VectorcraftApp) -> LengthUnits` (cv.B5b)
@@ -24,6 +24,31 @@ pub const OPTIONS: &str = "sliceOptions";
/// The dialog kind of Divide Slices.
pub const DIVIDE: &str = "divideSlices";
#[derive(Default)]
pub(crate) struct SliceDialogs {
options: Option<Target>,
divide: Option<Target>,
}
/// The document and effective slice selection used when opening either dialog.
#[derive(PartialEq)]
struct Target {
document: u64,
slices: Vec<NodeId>,
}
impl Target {
fn new(document: u64, mut slices: Vec<NodeId>) -> Self {
slices.sort_unstable();
Self { document, slices }
}
fn current(app: &VectorcraftApp) -> Option<Self> {
let document = app.session.active()?.uid;
Some(Self::new(document, app.session.selected_slice_ids()))
}
}
/// The command's key of each slice type, alignment and vertical alignment.
const KINDS: [(&str, SliceKind); 3] = [("image", SliceKind::Image), ("noImage", SliceKind::NoImage), ("htmlText", SliceKind::HtmlText)];
const ALIGNS: [(&str, SliceCellAlign); 4] =
@@ -95,6 +120,7 @@ fn divide_json(d: &DivideSlices) -> Value {
/// Open Slice Options on the selected slices' options (those of the first one).
pub fn open_options(app: &mut VectorcraftApp) -> Result<Value, String> {
let source = Target::current(app).ok_or("no document open")?;
let v = app.session.execute("object.slice.options", &json!({})).map_err(|e| e.to_string())?;
let map = v.as_object().cloned().unwrap_or_default();
let mut kinds = vec![SliceKind::Image, SliceKind::NoImage];
@@ -102,7 +128,12 @@ pub fn open_options(app: &mut VectorcraftApp) -> Result<Value, String> {
kinds.push(SliceKind::HtmlText);
}
let dialog = SliceOptionsDialog::new(options_of(&map)).kinds(&kinds);
app.dialogs().ok_or("no window")?.with(|host| host.open(dialog));
app.dialogs().ok_or("no window")?.with(|host| {
host.open(dialog);
// Replacing a still-open dialog leaves its Cancel behind.
let _ = host.take::<SliceOptionsDialog>();
});
app.slice_dialogs.options = Some(source);
Ok(Value::Null)
}
@@ -110,10 +141,16 @@ pub fn open_options(app: &mut VectorcraftApp) -> Result<Value, String> {
pub fn open_divide(app: &mut VectorcraftApp) -> Result<Value, String> {
let lengths = general_lengths(app);
let st = app.session.active().ok_or("no document open")?;
let first = st.selection.slices.iter().chain(&st.selection.objects).find_map(|id| st.doc.slice_bounds(*id));
let selected = app.session.selected_slice_ids();
let first = selected.iter().find_map(|id| st.doc.slice_bounds(*id));
let source = Target::new(st.uid, selected);
let (w, h) = first.map_or((50.0, 50.0), |r| (r.width() / 2.0, r.height() / 2.0));
let dialog = DivideSlicesDialog::new(DivideSlices::new(Some(DivideBy::Count(2)), Some(DivideBy::Count(2)))).lengths(lengths).start_sizes(h, w);
app.dialogs().ok_or("no window")?.with(|host| host.open(dialog));
app.dialogs().ok_or("no window")?.with(|host| {
host.open(dialog);
let _ = host.take::<DivideSlicesDialog>();
});
app.slice_dialogs.divide = Some(source);
Ok(Value::Null)
}
@@ -123,11 +160,19 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
let Some(host) = app.dialogs() else { return };
let (options, divide) = host.with(|h| (h.take::<SliceOptionsDialog>(), h.take::<DivideSlicesDialog>()));
let answers = [
options.and_then(|r| if let DialogResult::Ok(o) = r { Some(("object.slice.options", options_json(&o))) } else { None }),
divide.and_then(|r| if let DialogResult::Ok(d) = r { Some(("object.slice.divide", divide_json(&d))) } else { None }),
options.and_then(|r| {
let source = app.slice_dialogs.options.take();
r.ok().map(|o| (source, "object.slice.options", options_json(&o)))
}),
divide.and_then(|r| {
let source = app.slice_dialogs.divide.take();
r.ok().map(|d| (source, "object.slice.divide", divide_json(&d)))
}),
];
for (cmd, params) in answers.into_iter().flatten() {
if let Err(e) = app.run(cmd, params) {
for (source, cmd, params) in answers.into_iter().flatten() {
if !source.is_some_and(|source| Some(source) == Target::current(app)) {
app.status(tl!("The slice selection changed while the dialog was open."));
} else if let Err(e) = app.run(cmd, params) {
app.status(e);
}
}
@@ -137,6 +182,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
mod tests {
use super::*;
use craft::ui::dialog::{DialogHost, DialogId};
use vectorcraft_doc::NodeId;
use vectorcraft_engine::Session;
fn app() -> VectorcraftApp {
@@ -171,6 +217,97 @@ mod tests {
enabled
}
fn add_slice(app: &mut VectorcraftApp, x: f64) -> NodeId {
let id = app.run("shape.rectangle", json!({"x": x, "y": 0, "width": 60, "height": 40})).unwrap()["id"].as_u64().unwrap();
app.run("object.slice.make", json!({"ids": [id]})).unwrap();
NodeId(id)
}
fn select_slice(app: &mut VectorcraftApp, id: NodeId) {
app.run("object.slice.select", json!({"slices": [id.0]})).unwrap();
}
fn check_changed_selection(dialog: &str, divide: bool) {
let mut app = app();
let source = app.session.active().unwrap().doc.slice_ids()[0];
let destination = add_slice(&mut app, 80.0);
select_slice(&mut app, source);
app.run(dialog, json!({})).unwrap();
if !divide {
set(&mut app, "name", json!("Original"));
}
select_slice(&mut app, destination);
let st = app.session.active().unwrap();
let undo = st.history.undo.len();
let document = st.doc.clone();
assert!(ok(&mut app));
let st = app.session.active().unwrap();
assert_eq!(*st.doc, *document, "the changed selection is untouched");
assert_eq!(st.history.undo.len(), undo);
assert_eq!(app.ui.status, "The slice selection changed while the dialog was open.");
}
#[test]
fn stale_slice_dialog_options_leaves_the_new_selection_untouched() {
check_changed_selection("object.slice.options", false);
}
#[test]
fn stale_slice_dialog_divide_leaves_the_new_selection_untouched() {
check_changed_selection("object.slice.divide", true);
}
#[test]
fn stale_slice_dialog_refuses_a_different_document_with_matching_selected_ids() {
let mut app = app();
let from = app.session.active().unwrap().doc.slice_ids()[0];
app.run("object.slice.options", json!({})).unwrap();
set(&mut app, "name", json!("Source"));
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
let to = add_slice(&mut app, 0.0);
select_slice(&mut app, to);
assert_eq!(from, to, "selection IDs are local to each document");
let st = app.session.active().unwrap();
let undo = st.history.undo.len();
assert!(ok(&mut app));
let st = app.session.active().unwrap();
assert_eq!(st.doc.slice_options(to).unwrap().name, "");
assert_eq!(st.history.undo.len(), undo);
}
#[test]
fn replacing_slice_options_keeps_the_new_dialogs_target() {
let mut app = app();
app.run("object.slice.options", json!({})).unwrap();
let other = add_slice(&mut app, 80.0);
select_slice(&mut app, other);
app.run("object.slice.options", json!({})).unwrap();
let ctx = app.ui_ctx.clone().unwrap();
pump(&mut app, &ctx);
set(&mut app, "name", json!("Replacement"));
assert!(ok(&mut app));
assert_eq!(app.session.active().unwrap().doc.slice_options(other).unwrap().name, "Replacement");
}
#[test]
fn confirming_slice_options_still_works_when_the_same_slices_are_selected_in_a_different_order() {
let mut app = app();
let first = app.session.active().unwrap().doc.slice_ids()[0];
let second = add_slice(&mut app, 80.0);
app.run("object.slice.select", json!({"slices": [first.0, second.0]})).unwrap();
app.run("object.slice.options", json!({})).unwrap();
set(&mut app, "alt", json!("Two slices"));
app.run("object.slice.select", json!({"slices": [second.0, first.0]})).unwrap();
assert!(ok(&mut app));
let document = &app.session.active().unwrap().doc;
assert_eq!(document.slice_options(first).unwrap().alt, "Two slices");
assert_eq!(document.slice_options(second).unwrap().alt, "Two slices");
}
#[test]
fn slice_options_opens_on_the_selection_and_ok_sets_them() {
let mut app = app();
+2
View File
@@ -283,6 +283,8 @@ pub struct VectorcraftApp {
pub(crate) three_d: panels::three_d::Model,
/// The open export option dialogs and what runs after each.
pub(crate) export_dialogs: dialogs::export_host::ExportDialogs,
/// The document and selection used by each open slice dialog.
pub(crate) slice_dialogs: dialogs::slices::SliceDialogs,
/// Save for Web's renders and colour table.
pub(crate) web_export: dialogs::save_for_web::WebExport,
/// The Flattener Preview panel's model.