mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Reject stale Brush confirmations after File Revert
This commit is contained in:
1 parent
23c05774b7
commit
7dbc4d2e14
4 files changed
+90
-7
No files matched your search
@@ -21,7 +21,7 @@ super::confirm::wrapped_dialog!(BrushDeleteQuestion, StandardPrompt, KIND);
|
||||
/// What the question is about: the document it was asked in, the `brush.delete` params and whether a
|
||||
/// stroke uses one of the brushes.
|
||||
struct BrushDeleteAsk {
|
||||
doc: Option<u64>,
|
||||
doc: Option<(u64, u64)>,
|
||||
params: Value,
|
||||
used: bool,
|
||||
}
|
||||
@@ -33,7 +33,7 @@ pub fn ask(app: &mut VectorcraftApp, params: Value, used: bool) {
|
||||
return;
|
||||
};
|
||||
let (message, yes, no) = words(used);
|
||||
let doc = app.session.active().map(|d| d.uid);
|
||||
let doc = app.session.active().map(|d| (d.uid, d.reverts));
|
||||
let prompt =
|
||||
StandardPrompt::custom(message).button(PromptChoice::Proceed, yes).button(PromptChoice::Cancel, no).tag(BrushDeleteAsk { doc, params, used });
|
||||
dialogs.with(|host| host.open(BrushDeleteQuestion(prompt)));
|
||||
@@ -89,7 +89,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
/// Yes on brushes that a stroke has started to use since the question opened asks again with Remove
|
||||
/// Strokes and Cancel. When another document has become the active one, nothing is deleted.
|
||||
fn confirm(app: &mut VectorcraftApp, question: BrushDeleteAsk) {
|
||||
if question.doc != app.session.active().map(|doc| doc.uid) {
|
||||
if !confirmation_is_current(app, question.doc) {
|
||||
return;
|
||||
}
|
||||
if !question.used {
|
||||
@@ -111,5 +111,19 @@ pub(crate) fn open_question(app: &VectorcraftApp) -> Option<(String, bool)> {
|
||||
Some((words(used).0.to_string(), used))
|
||||
}
|
||||
|
||||
/// Whether the opening Brush confirmation still owns the active drawing's contents.
|
||||
pub(super) fn confirmation_is_current(app: &mut VectorcraftApp, opening: Option<(u64, u64)>) -> bool {
|
||||
let current = app.session.active().map(|st| (st.uid, st.reverts));
|
||||
if opening == current {
|
||||
return true;
|
||||
}
|
||||
if let (Some((document, _)), Some((active, _))) = (opening, current)
|
||||
&& document == active
|
||||
{
|
||||
app.status(tl!("The document was reverted. Reopen this dialog to continue."));
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(super) mod tests;
|
||||
@@ -10,6 +10,15 @@ pub(in crate::dialogs) fn app() -> VectorcraftApp {
|
||||
crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default()))
|
||||
}
|
||||
|
||||
/// Writes a native file for the test to revert to.
|
||||
pub(in crate::dialogs) fn save_for_revert(app: &mut VectorcraftApp, tag: &str) -> std::path::PathBuf {
|
||||
let file = std::env::temp_dir().join(format!("vectorcraft-brush-{tag}-{}.vectorcraft", std::process::id()));
|
||||
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|err| err.to_string())));
|
||||
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|err| err.to_string())));
|
||||
app.run("file.saveAs", json!({"path": file.to_string_lossy()})).unwrap();
|
||||
file
|
||||
}
|
||||
|
||||
/// Answers the open question `D` the way its button does (`Some(choice)`: the button of that choice,
|
||||
/// `None`: Cancel's close), then runs the module's `pump`.
|
||||
pub(in crate::dialogs) fn answer<D: StackDialog>(app: &mut VectorcraftApp, choice: Option<&str>, pump: fn(&mut VectorcraftApp, &egui::Context)) {
|
||||
@@ -84,3 +93,36 @@ fn remove_strokes_runs_the_delete_and_cancel_changes_nothing() {
|
||||
assert!(stroke.width > 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stale_delete_approval_keeps_the_saved_brush_after_revert() {
|
||||
let mut app = app();
|
||||
app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
|
||||
let slim = app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 40, "height": 8})).unwrap()["id"].as_u64().unwrap();
|
||||
let tall = app.run("shape.rectangle", json!({"x": 10, "y": 30, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("brush.new", json!({"type": "art", "name": "Bar", "ids": [slim]})).unwrap();
|
||||
let file = save_for_revert(&mut app, "delete-revert");
|
||||
let brush_height = |app: &mut VectorcraftApp| {
|
||||
let brush = app.run("brush.get", json!({"name": "Bar"})).expect("the saved brush still exists");
|
||||
let art: vectorcraft_doc::Node = serde_json::from_value(brush["art"].clone()).unwrap();
|
||||
art.geometric_bounds().unwrap().height()
|
||||
};
|
||||
let saved = brush_height(&mut app);
|
||||
let st = app.session.active().unwrap();
|
||||
let (uid, reverts) = (st.uid, st.reverts);
|
||||
|
||||
app.run("brush.replaceArt", json!({"name": "Bar", "ids": [tall], "applyToStrokes": true})).unwrap();
|
||||
assert!((brush_height(&mut app) - saved).abs() > 1e-6);
|
||||
ask(&mut app, json!({"names": ["Bar"]}), false);
|
||||
app.run("file.revert", json!({"confirmed": true})).unwrap();
|
||||
let _ = std::fs::remove_file(file);
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!((st.uid, st.reverts), (uid, reverts + 1));
|
||||
assert!((brush_height(&mut app) - saved).abs() < 1e-6);
|
||||
|
||||
let undo = app.session.active().unwrap().history.undo.len();
|
||||
answer::<BrushDeleteQuestion>(&mut app, Some("proceed"), pump);
|
||||
assert!((brush_height(&mut app) - saved).abs() < 1e-6);
|
||||
assert_eq!(app.session.active().unwrap().history.undo.len(), undo);
|
||||
assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue.");
|
||||
}
|
||||
@@ -22,7 +22,7 @@ super::confirm::wrapped_dialog!(ReplaceArtQuestion, StandardPrompt, KIND);
|
||||
|
||||
/// What the question is about: the document it was asked in, the brush and the art.
|
||||
struct ReplaceArtAsk {
|
||||
doc: Option<u64>,
|
||||
doc: Option<(u64, u64)>,
|
||||
name: String,
|
||||
ids: Vec<u64>,
|
||||
}
|
||||
@@ -59,7 +59,7 @@ pub fn ask(app: &mut VectorcraftApp, name: &str, ids: &[u64]) {
|
||||
app.status("no window");
|
||||
return;
|
||||
};
|
||||
let doc = app.session.active().map(|d| d.uid);
|
||||
let doc = app.session.active().map(|d| (d.uid, d.reverts));
|
||||
let prompt = StandardPrompt::custom(tl!(
|
||||
"Strokes use this brush. Apply the new art to those strokes, or leave them as they are and add a copy of the brush with the new art?"
|
||||
))
|
||||
@@ -86,7 +86,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
|
||||
/// Runs `brush.replaceArt`. When another document has become the active one, no brush changes.
|
||||
fn confirm(app: &mut VectorcraftApp, question: ReplaceArtAsk, apply: bool) {
|
||||
if question.doc != app.session.active().map(|doc| doc.uid) {
|
||||
if !super::brush_delete::confirmation_is_current(app, question.doc) {
|
||||
return;
|
||||
}
|
||||
let params = json!({"name": question.name, "ids": question.ids, "applyToStrokes": apply});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::NodeId;
|
||||
|
||||
use super::super::brush_delete::tests::{answer, app};
|
||||
use super::super::brush_delete::tests::{answer, app, save_for_revert};
|
||||
use super::*;
|
||||
|
||||
fn rect(app: &mut VectorcraftApp, x: f64, y: f64, w: f64, h: f64) -> u64 {
|
||||
@@ -95,3 +95,30 @@ fn a_button_changes_no_brush_after_another_document_became_active() {
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
assert!((art_height(&mut app, "Bar") - 8.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stale_replace_approval_keeps_the_saved_brush_art_after_revert() {
|
||||
let mut app = app();
|
||||
let (_, tall) = used_brush(&mut app);
|
||||
let thick = rect(&mut app, 10.0, 65.0, 40.0, 30.0);
|
||||
let file = save_for_revert(&mut app, "replace-revert");
|
||||
let st = app.session.active().unwrap();
|
||||
let (uid, reverts) = (st.uid, st.reverts);
|
||||
|
||||
app.run("brush.replaceArt", json!({"name": "Bar", "ids": [tall], "applyToStrokes": true})).unwrap();
|
||||
assert!((art_height(&mut app, "Bar") - 20.0).abs() < 1e-6);
|
||||
replace(&mut app, "Bar", &[thick]);
|
||||
assert!(asks(&app));
|
||||
app.run("file.revert", json!({"confirmed": true})).unwrap();
|
||||
let _ = std::fs::remove_file(file);
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!((st.uid, st.reverts), (uid, reverts + 1));
|
||||
assert!(st.doc.node(NodeId(thick)).is_some(), "the restored drawing shares the same local source ID");
|
||||
assert!((art_height(&mut app, "Bar") - 8.0).abs() < 1e-6);
|
||||
|
||||
let undo = app.session.active().unwrap().history.undo.len();
|
||||
answer::<ReplaceArtQuestion>(&mut app, Some("proceed"), pump);
|
||||
assert!((art_height(&mut app, "Bar") - 8.0).abs() < 1e-6, "the stale answer leaves the restored brush intact");
|
||||
assert_eq!(app.session.active().unwrap().history.undo.len(), undo);
|
||||
assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue.");
|
||||
}
|
||||
Reference in new issue
Block a user