mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Merge pull request #9875 from Hassan1305/fix-9848-mosaic-stale-warning
Object Mosaic: clear the away warning on return to the original image (#9848)
This commit is contained in:
1 file changed
+104
-3
@@ -94,6 +94,15 @@ pub struct Mosaic {
|
||||
delete_raster: bool,
|
||||
}
|
||||
|
||||
/// The warnings the dialog gave for being away from its image, one for each place it shows one.
|
||||
#[derive(Default)]
|
||||
struct Away {
|
||||
/// OK's held refusal.
|
||||
held: Option<Label>,
|
||||
/// Use Ratio's note.
|
||||
note: Option<Label>,
|
||||
}
|
||||
|
||||
/// Create Object Mosaic on the dialog host.
|
||||
pub struct ObjectMosaicDialog {
|
||||
id: DialogId,
|
||||
@@ -108,6 +117,9 @@ pub struct ObjectMosaicDialog {
|
||||
note: Option<Label>,
|
||||
/// Use Ratio was pressed; [`pump`] works out the counts.
|
||||
use_ratio: bool,
|
||||
/// Why OK and why Use Ratio last found the dialog away from its image (another drawing, or the
|
||||
/// image not selected), each warning on its own. Back on the image, [`sync`] takes them away.
|
||||
away: Away,
|
||||
/// OK, until [`pump`] has run the command on it.
|
||||
held: HeldAnswer<Mosaic>,
|
||||
}
|
||||
@@ -202,10 +214,15 @@ pub(crate) fn sync(app: &mut VectorcraftApp) {
|
||||
let Some(dialogs) = app.dialogs() else { return };
|
||||
let selected = selected_image(app);
|
||||
let lengths = crate::canvas::general_lengths(app);
|
||||
let Some(source) = dialogs.with(|host| host.get_mut::<ObjectMosaicDialog>().map(|dialog| dialog.source)) else { return };
|
||||
let back = source.size(app).is_ok();
|
||||
dialogs.with(|host| {
|
||||
if let Some(dialog) = host.get_mut::<ObjectMosaicDialog>() {
|
||||
let size = selected.filter(|(source, _)| *source == dialog.source).map(|(_, size)| size);
|
||||
dialog.sync(size, lengths);
|
||||
if back {
|
||||
dialog.came_back();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -234,7 +251,9 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &Context) {
|
||||
match work {
|
||||
None => {}
|
||||
Some(Work::Ratio(mut values, source)) => {
|
||||
let counts = source.size(app).and_then(|image| {
|
||||
let image = source.size(app);
|
||||
let away = image.as_ref().err().map(|why| Label::from(why.clone()));
|
||||
let counts = image.and_then(|image| {
|
||||
follow_ratio(&mut values, image);
|
||||
let mut p = params(&values, image);
|
||||
p["useRatio"] = json!(values.side.key());
|
||||
@@ -242,6 +261,9 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &Context) {
|
||||
});
|
||||
dialogs.with(|host| {
|
||||
let Some(dialog) = host.get_mut::<ObjectMosaicDialog>() else { return };
|
||||
if away.is_some() {
|
||||
dialog.away.note = away;
|
||||
}
|
||||
match counts {
|
||||
Ok((cols, rows)) => {
|
||||
dialog.values.columns = cols as f64;
|
||||
@@ -253,8 +275,9 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &Context) {
|
||||
});
|
||||
}
|
||||
Some(Work::Run(mut values, source)) => {
|
||||
let answer = source
|
||||
.size(app)
|
||||
let image = source.size(app);
|
||||
let away = image.as_ref().err().map(|why| Label::from(why.clone()));
|
||||
let answer = image
|
||||
.and_then(|image| {
|
||||
follow_ratio(&mut values, image);
|
||||
app.run(COMMAND, params(&values, image)).map(|_| ())
|
||||
@@ -262,6 +285,9 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &Context) {
|
||||
.map_err(Label::from);
|
||||
dialogs.with(|host| {
|
||||
if let Some(dialog) = host.get_mut::<ObjectMosaicDialog>() {
|
||||
if away.is_some() {
|
||||
dialog.away.held = away;
|
||||
}
|
||||
dialog.held.answer(answer);
|
||||
}
|
||||
});
|
||||
@@ -280,6 +306,7 @@ impl ObjectMosaicDialog {
|
||||
lengths,
|
||||
note: None,
|
||||
use_ratio: false,
|
||||
away: Away::default(),
|
||||
held: HeldAnswer::new(),
|
||||
}
|
||||
}
|
||||
@@ -296,6 +323,21 @@ impl ObjectMosaicDialog {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The dialog is on its image again: the warning that being away gave goes. A refusal of the
|
||||
/// values themselves, and one after Revert or Undo (which never comes back), stays.
|
||||
fn came_back(&mut self) {
|
||||
if let Some(away) = self.away.note.take()
|
||||
&& self.note.as_ref().is_some_and(|n| n.as_str() == away.as_str())
|
||||
{
|
||||
self.note = None;
|
||||
}
|
||||
if let Some(away) = self.away.held.take()
|
||||
&& self.held.refusal().is_some_and(|(reason, _)| reason.as_str() == away.as_str())
|
||||
{
|
||||
self.held.clear_refusal();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The image's size with no zero side, for dividing by.
|
||||
@@ -968,4 +1010,63 @@ mod tests {
|
||||
assert_eq!(mosaic(&app), Some((100.0, 50.0, 4)));
|
||||
assert!(!is_open(&mut app));
|
||||
}
|
||||
|
||||
/// The warning in the dialog's notice: a held refusal, or a refused Use Ratio's note.
|
||||
fn warning(app: &mut VectorcraftApp) -> Option<String> {
|
||||
with_dialog(app, |d| d.note.clone().or_else(|| d.held.refusal().map(|(reason, _)| reason.clone())).map(|l| l.as_str().to_owned())).flatten()
|
||||
}
|
||||
|
||||
/// Use Ratio as a click: the dialog asks, and the app works out the counts.
|
||||
fn use_ratio(app: &mut VectorcraftApp) {
|
||||
with_dialog(app, |d| d.use_ratio = true).expect("an open dialog");
|
||||
crate::dialogs::host::pump(app, &crate::testing::ui_context());
|
||||
}
|
||||
|
||||
/// The warning that OK or Use Ratio gave in another drawing shows only while the dialog is away
|
||||
/// from its image: back on it, with the fields unchanged, the warning goes.
|
||||
#[test]
|
||||
fn a_warning_from_another_drawing_goes_on_return_to_the_original_image() {
|
||||
let mut app = app_with_image();
|
||||
open(&mut app, &json!({"columns": 2, "rows": 2})).unwrap();
|
||||
add_image(&mut app);
|
||||
let away = tl!("Return to the original drawing to create the mosaic.");
|
||||
|
||||
ok(&mut app);
|
||||
assert_eq!(warning(&mut app).as_deref(), Some(away));
|
||||
sync(&mut app);
|
||||
assert_eq!(warning(&mut app).as_deref(), Some(away), "still away, the warning stays");
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
sync(&mut app);
|
||||
assert_eq!(warning(&mut app), None, "back on the image, OK's warning goes");
|
||||
|
||||
app.run("document.activate", json!({"index": 1})).unwrap();
|
||||
use_ratio(&mut app);
|
||||
assert_eq!(warning(&mut app).as_deref(), Some(away));
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
sync(&mut app);
|
||||
assert_eq!(warning(&mut app), None, "back on the image, Use Ratio's warning goes");
|
||||
|
||||
ok(&mut app);
|
||||
assert_eq!(mosaic(&app), Some((100.0, 50.0, 4)));
|
||||
}
|
||||
|
||||
/// OK's warning and Use Ratio's are taken away each on its own: a later, different one from
|
||||
/// Use Ratio leaves OK's to go as well once the dialog is back on its image.
|
||||
#[test]
|
||||
fn both_away_warnings_go_on_return_to_the_original_image() {
|
||||
let mut app = app_with_image();
|
||||
let source = image_id(&app);
|
||||
open(&mut app, &json!({"columns": 2, "rows": 2})).unwrap();
|
||||
add_image(&mut app);
|
||||
ok(&mut app);
|
||||
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
app.run("select.set", json!({"ids": []})).unwrap();
|
||||
use_ratio(&mut app);
|
||||
sync(&mut app);
|
||||
assert!(warning(&mut app).is_some(), "with no image selected the dialog is still away");
|
||||
app.run("select.set", json!({"ids": [source]})).unwrap();
|
||||
sync(&mut app);
|
||||
assert_eq!(with_dialog(&mut app, |d| d.held.refusal().is_none() && d.note.is_none()), Some(true), "{:?}", warning(&mut app));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user