mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Merge pull request #9488 from dotCooCoo/fix/ui-saved-names-document
Keep saved Selection and View edits with their document
This commit is contained in:
1 file changed
+72
-7
@@ -137,6 +137,7 @@ impl ItemSource<usize> for Shown<'_> {}
|
||||
/// The saved-names dialog of one kind.
|
||||
pub(crate) struct SavedNamesDialog {
|
||||
id: DialogId,
|
||||
document: u64,
|
||||
names: SavedNames,
|
||||
draft: NamesDraft,
|
||||
/// The picked row, by its index in the draft; one at most.
|
||||
@@ -147,10 +148,10 @@ pub(crate) struct SavedNamesDialog {
|
||||
|
||||
impl SavedNamesDialog {
|
||||
/// The dialog over `list`, with the first name picked.
|
||||
fn new(names: SavedNames, list: Vec<String>) -> Self {
|
||||
fn new(document: u64, names: SavedNames, list: Vec<String>) -> Self {
|
||||
let selection = if list.is_empty() { Selection::default() } else { Selection::single(0) };
|
||||
let rows = list.into_iter().map(|name| NameRow { orig: name.clone(), name, deleted: false }).collect();
|
||||
SavedNamesDialog { id: id_of(names.kind), names, draft: NamesDraft { rows }, selection, held: HeldAnswer::new() }
|
||||
SavedNamesDialog { id: id_of(names.kind), document, names, draft: NamesDraft { rows }, selection, held: HeldAnswer::new() }
|
||||
}
|
||||
|
||||
/// OK as its button gives it: with nothing edited the dialog closes at once (`Some`); with edits they wait for
|
||||
@@ -326,8 +327,9 @@ impl StackDialog for SavedNamesDialog {
|
||||
/// Opens the dialog of `names` on `list`, the saved names in their order (the first `names.max` of them).
|
||||
pub(crate) fn open(app: &mut VectorcraftApp, names: SavedNames, mut list: Vec<String>) -> Result<(), String> {
|
||||
list.truncate(names.max);
|
||||
let document = app.session.active().ok_or("no document open")?.uid;
|
||||
let dialogs = app.dialogs().ok_or("no window")?;
|
||||
dialogs.with(|host| host.open(SavedNamesDialog::new(names, list)));
|
||||
dialogs.with(|host| host.open(SavedNamesDialog::new(document, names, list)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -335,18 +337,22 @@ pub(crate) fn open(app: &mut VectorcraftApp, names: SavedNames, mut list: Vec<St
|
||||
/// closes when the command took the edits, and stays open with the reason when it refused them.
|
||||
pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
let Some(dialogs) = app.dialogs() else { return };
|
||||
let filed: Vec<(DialogId, &'static str, Vec<Value>)> = dialogs.with(|host| {
|
||||
let filed: Vec<_> = dialogs.with(|host| {
|
||||
host.open_dialogs()
|
||||
.into_iter()
|
||||
.filter_map(|open| {
|
||||
let dialog = host.get_mut_by_id::<SavedNamesDialog>(open.id)?;
|
||||
let edits = dialog.held.take()?;
|
||||
Some((open.id, dialog.names.command, edits))
|
||||
Some((open.id, dialog.document, dialog.names.command, edits))
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
for (id, command, edits) in filed {
|
||||
let outcome = app.run(command, json!({ "edits": edits }));
|
||||
for (id, document, command, edits) in filed {
|
||||
let outcome = if app.session.active().is_some_and(|active| active.uid == document) {
|
||||
app.run(command, json!({ "edits": edits }))
|
||||
} else {
|
||||
Err(tl!("Saved names can only be edited in the document this dialog was opened for.").to_owned())
|
||||
};
|
||||
if let Err(reason) = &outcome {
|
||||
app.status(reason.clone());
|
||||
}
|
||||
@@ -357,3 +363,62 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn saved_names_edits_stay_with_their_document() {
|
||||
for is_view in [false, true] {
|
||||
let mut app = crate::testing::windowed(VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()));
|
||||
for size in [100, 200] {
|
||||
app.run("file.new", json!({"width": size, "height": size})).unwrap();
|
||||
app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
|
||||
for name in ["Shared", "Spare"] {
|
||||
app.run(if is_view { "view.saved.new" } else { "select.save" }, json!({"name": name})).unwrap();
|
||||
}
|
||||
}
|
||||
let names = |app: &mut VectorcraftApp| {
|
||||
if is_view {
|
||||
let views = &app.session.active().unwrap().doc.views;
|
||||
json!(views.iter().map(|view| &view.name).collect::<Vec<_>>())
|
||||
} else {
|
||||
app.run("select.savedList", json!({})).unwrap()
|
||||
}
|
||||
};
|
||||
let activate = |app: &mut VectorcraftApp, index| app.run("document.activate", json!({"index": index})).unwrap();
|
||||
activate(&mut app, 0);
|
||||
let kind = if is_view {
|
||||
crate::dialogs::edit_views::open(&mut app).unwrap();
|
||||
crate::dialogs::edit_views::KIND
|
||||
} else {
|
||||
crate::dialogs::edit_selection::open(&mut app).unwrap();
|
||||
crate::dialogs::edit_selection::KIND
|
||||
};
|
||||
let id = id_of(kind);
|
||||
let ctx = app.ui_ctx.clone().unwrap();
|
||||
let host = app.dialogs().unwrap();
|
||||
host.with(|h| {
|
||||
h.set_json_field(&ctx, id, "name0", json!("Revised")).unwrap();
|
||||
h.set_json_field(&ctx, id, "deleted1", json!(true)).unwrap();
|
||||
h.confirm_by_id(&ctx, id);
|
||||
});
|
||||
|
||||
activate(&mut app, 1);
|
||||
let b_undo = app.session.active().unwrap().history.undo.len();
|
||||
pump(&mut app, &ctx);
|
||||
assert_eq!(names(&mut app), json!(["Shared", "Spare"]), "the destination's saved names stay unchanged for {kind}");
|
||||
assert_eq!(app.session.active().unwrap().history.undo.len(), b_undo);
|
||||
assert!(matches!(host.with(|h| h.held_state(id)), Some(HeldOk::Refused(_))));
|
||||
|
||||
activate(&mut app, 0);
|
||||
let a_undo = app.session.active().unwrap().history.undo.len();
|
||||
host.with(|h| h.confirm_by_id(&ctx, id));
|
||||
pump(&mut app, &ctx);
|
||||
assert!(matches!(host.with(|h| h.held_state(id)), Some(HeldOk::Accepted)));
|
||||
assert_eq!(names(&mut app), json!(["Revised"]));
|
||||
assert_eq!(app.session.active().unwrap().history.undo.len(), a_undo + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user