mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Fit Image opens with the values it last ran with (#13853)
A successful file.automate.fitImage run, with or without its dialog, keeps its width, height and Don't Enlarge in the preferences' dialogs map; the dialog opens with them, else with the document's size. Cancel stores nothing and a corrupt stored value is ignored.
This commit is contained in:
1 parent
5bdbcc1181
commit
9a5aa40a85
4 files changed
+130
-2
No files matched your search
@@ -242,6 +242,24 @@ fn clear_canvas_guides(s: &mut Session) -> Result<Value> {
|
||||
|
||||
/// Register File-menu commands with their parameter contracts and enablement predicates.
|
||||
/// Filesystem operations are native-only; document operations remain available on the web.
|
||||
/// Where Fit Image's last values are kept: the preferences' `dialogs` map, by command id.
|
||||
pub const FIT_IMAGE: &str = "file.automate.fitImage";
|
||||
|
||||
/// Keep the width, height and Don't Enlarge a Fit Image ran with, with or without its dialog,
|
||||
/// for the next opening of the dialog (#13853), as the reference application does.
|
||||
fn remember_fit_image(s: &mut Session, p: &Value) {
|
||||
let mut kept = serde_json::Map::new();
|
||||
for key in ["width", "height"] {
|
||||
if let Some(v) = p.get(key).and_then(Value::as_f64).filter(|v| v.is_finite() && (1.0..=300_000.0).contains(v)) {
|
||||
kept.insert(key.into(), json!(v.round() as u64));
|
||||
}
|
||||
}
|
||||
kept.insert("dontEnlarge".into(), json!(p.get("dontEnlarge").and_then(Value::as_bool).unwrap_or(false)));
|
||||
if s.prefs().dialogs.get(FIT_IMAGE) != Some(&Value::Object(kept.clone())) {
|
||||
s.prefs.edit(|prefs| prefs.dialogs.insert(FIT_IMAGE.into(), Value::Object(kept)));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
macro_rules! spec {
|
||||
($id:expr, $label:expr, $menu:expr, $sc:expr, $params:expr, $enabled:expr, $run:expr) => {
|
||||
@@ -352,7 +370,11 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
None,
|
||||
r##"{"width":px,"height":px,"dontEnlarge":bool=false,"resample":"automatic|preserveDetails|bicubicSmoother|bicubicSharper|bicubicAutomatic|bicubic|nearest|bilinear|lanczos"="bicubic"}"##,
|
||||
has_doc,
|
||||
fit_image
|
||||
|s, p| {
|
||||
let r = fit_image(s, p)?;
|
||||
remember_fit_image(s, p);
|
||||
Ok(r)
|
||||
}
|
||||
),
|
||||
spec!(
|
||||
"file.automate.conditionalModeChange",
|
||||
|
||||
@@ -229,3 +229,7 @@ mod form_reset_tests {
|
||||
#[cfg(test)]
|
||||
#[path = "view_cmds/tests.rs"]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "view_cmds/fit_image_sticky_tests.rs"]
|
||||
mod fit_image_sticky_tests;
|
||||
@@ -482,7 +482,16 @@ pub(super) fn front(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, para
|
||||
}
|
||||
"file.automate.fitImage" => {
|
||||
let (w, h, _) = doc?;
|
||||
dialog(app, json!({"width": w, "height": h, "dontEnlarge": false, "__compact": true}), json!({}))
|
||||
// The values Fit Image last ran with, else the document's size (#13853). A stored
|
||||
// value that isn't a usable size is ignored.
|
||||
let kept = app.session.prefs().dialogs.get(photocraft_engine::file_cmds::FIT_IMAGE).cloned().unwrap_or_default();
|
||||
let size = |key: &str| kept.get(key).and_then(Value::as_u64).filter(|v| (1..=300_000).contains(v));
|
||||
let enlarge = kept.get("dontEnlarge").and_then(Value::as_bool).unwrap_or(false);
|
||||
dialog(
|
||||
app,
|
||||
json!({"width": size("width").unwrap_or(w.into()), "height": size("height").unwrap_or(h.into()), "dontEnlarge": enlarge, "__compact": true}),
|
||||
json!({}),
|
||||
)
|
||||
}
|
||||
"file.automate.conditionalModeChange" => dialog(
|
||||
app,
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
//! #13853: Fit Image opens with the width, height and Don't Enlarge it last ran with (OK, or a
|
||||
//! run without the dialog), not the document's size; Cancel stores nothing.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
|
||||
const FIT_IMAGE: &str = "file.automate.fitImage";
|
||||
|
||||
fn app(width: u32, height: u32) -> PhotocraftApp {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": width, "height": height})).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
/// Opens Fit Image and returns the dialog's id.
|
||||
fn open(app: &mut PhotocraftApp) -> u64 {
|
||||
crate::menus::invoke(app, &egui::Context::default(), FIT_IMAGE, json!({})).unwrap()["dialog"].as_u64().unwrap()
|
||||
}
|
||||
|
||||
/// The dialog's Width, Height and Don't Enlarge.
|
||||
fn shown(app: &PhotocraftApp, id: u64) -> (Value, Value, Value) {
|
||||
let f = &app.ui.dialogs.iter().find(|d| d.id == id).unwrap().fields;
|
||||
(f["width"].clone(), f["height"].clone(), f["dontEnlarge"].clone())
|
||||
}
|
||||
|
||||
fn set(app: &mut PhotocraftApp, id: u64, width: u64, height: u64, dont_enlarge: bool) {
|
||||
let f = &mut app.ui.dialog_mut(id).unwrap().fields;
|
||||
f.insert("width".into(), json!(width));
|
||||
f.insert("height".into(), json!(height));
|
||||
f.insert("dontEnlarge".into(), json!(dont_enlarge));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ok_values_are_what_the_next_opening_shows_on_any_document() {
|
||||
let mut app = app(400, 300);
|
||||
let id = open(&mut app);
|
||||
assert_eq!(shown(&app, id), (json!(400), json!(300), json!(false)), "nothing stored yet: the document's size");
|
||||
set(&mut app, id, 250, 180, true);
|
||||
crate::dialogs::confirm(&mut app, id).unwrap();
|
||||
let size = app.session.active().unwrap().doc.size;
|
||||
assert_eq!((size.width, size.height), (240, 180), "it ran");
|
||||
// Another document, another size: the stored values still come back.
|
||||
app.run("file.new", json!({"width": 640, "height": 480})).unwrap();
|
||||
let id = open(&mut app);
|
||||
assert_eq!(shown(&app, id), (json!(250), json!(180), json!(true)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancel_leaves_the_stored_values() {
|
||||
let mut app = app(400, 300);
|
||||
let id = open(&mut app);
|
||||
set(&mut app, id, 250, 180, true);
|
||||
crate::dialogs::confirm(&mut app, id).unwrap();
|
||||
let id = open(&mut app);
|
||||
set(&mut app, id, 90, 70, false);
|
||||
crate::dialogs::cancel(&mut app, id).unwrap();
|
||||
let id = open(&mut app);
|
||||
assert_eq!(shown(&app, id), (json!(250), json!(180), json!(true)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_run_without_the_dialog_stores_its_values_too() {
|
||||
let mut app = app(400, 300);
|
||||
app.run(FIT_IMAGE, json!({"width": 320, "height": 200, "dontEnlarge": true})).unwrap();
|
||||
let id = open(&mut app);
|
||||
assert_eq!(shown(&app, id), (json!(320), json!(200), json!(true)));
|
||||
// An action step stores them as well.
|
||||
app.session.execute("actions.record", json!({"name": "Fit"})).unwrap();
|
||||
app.run(FIT_IMAGE, json!({"width": 150, "height": 150})).unwrap();
|
||||
app.session.execute("actions.stop", json!({})).unwrap();
|
||||
app.run(FIT_IMAGE, json!({"width": 500, "height": 500})).unwrap();
|
||||
app.session.execute("actions.play", json!({"action": "Fit"})).unwrap();
|
||||
let id = open(&mut app);
|
||||
assert_eq!(shown(&app, id), (json!(150), json!(150), json!(false)), "the played step's values");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kept_across_a_restart_and_corrupt_values_fall_back_to_the_document_size() {
|
||||
let mut app = app(400, 300);
|
||||
app.run(FIT_IMAGE, json!({"width": 250, "height": 180, "dontEnlarge": true})).unwrap();
|
||||
let saved = app.session.prefs_value().to_string();
|
||||
let mut again = self::app(400, 300);
|
||||
again.session.load_prefs_json(&saved).unwrap();
|
||||
let id = open(&mut again);
|
||||
assert_eq!(shown(&again, id), (json!(250), json!(180), json!(true)), "from the settings file");
|
||||
for corrupt in [json!({"width": "wide", "height": 0, "dontEnlarge": "yes"}), json!("not an object"), json!({"width": 900_000, "height": -4})] {
|
||||
let mut app = self::app(400, 300);
|
||||
app.session.prefs.edit(|p| p.dialogs.insert(FIT_IMAGE.into(), corrupt.clone()));
|
||||
let id = open(&mut app);
|
||||
assert_eq!(shown(&app, id), (json!(400), json!(300), json!(false)), "{corrupt}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user