From 6fbb1871f8d246b823a39ca5557515a3a0c72dab Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 21:45:32 -0800 Subject: [PATCH] Save for Web: Enter in the Optimize to File Size rows runs the search While the Desired File Size rows are open, Enter runs the search, as the rows' OK does, instead of saving with the current settings. An Enter that an open dropdown takes, that a field refuses the value for, or that is pressed with Ctrl, Cmd or Alt runs no search and does not save either. --- crates/ui-egui/src/dialogs/save_for_web.rs | 22 ++++++- .../ui-egui/src/dialogs/tests_save_for_web.rs | 65 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs/save_for_web.rs b/crates/ui-egui/src/dialogs/save_for_web.rs index 92e05b02b5..c34e0acccd 100644 --- a/crates/ui-egui/src/dialogs/save_for_web.rs +++ b/crates/ui-egui/src/dialogs/save_for_web.rs @@ -542,6 +542,7 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { side(app, ui, d, settings.as_ref().ok()); }); }); + optimize_on_enter(ui.ctx(), d); if browser && let Err(e) = app.run("file.saveForWeb.browser", params(d)) { app.status(e); } @@ -842,7 +843,7 @@ fn previewed_bytes(app: &VectorcraftApp, ui: &egui::Ui, s: &WebSettings) -> Opti } /// Optimize to File Size…: the button opens rows ([`open_optimize_rows`]) for the desired size in -/// K, Start With and, with slices, Use; OK runs [`optimize_to_size`] and Cancel +/// K, Start With and, with slices, Use; OK or Enter runs [`optimize_to_size`] and Cancel /// closes the rows. A line gives the size it found and whether that is within the desired size /// ([`optimized_result`]). fn optimize_rows(ui: &mut egui::Ui, d: &mut Dialog, s: Option<&WebSettings>, art: &str) { @@ -888,6 +889,25 @@ fn optimize_rows(ui: &mut egui::Ui, d: &mut Dialog, s: Option<&WebSettings>, art } } +/// An Enter while the Optimize to File Size rows ([`optimize_rows`]) are open is their OK: it does +/// not save. Run after every control of the dialog has drawn, it runs the search unless a control +/// took the press (an open dropdown), a field refused the value typed in it, or Ctrl, Cmd or Alt was +/// held, which the size field does not commit. +pub(super) fn optimize_on_enter(ctx: &egui::Context, d: &mut Dialog) { + if !d.fields.contains_key("__optimizeK") || !crate::shortcuts::pressed_anew(ctx, egui::Key::Enter) { + return; + } + let held = ctx.input(|i| { + i.events.iter().any(|e| matches!(e, egui::Event::Key { key: egui::Key::Enter, pressed: true, modifiers, .. } if modifiers.ctrl || modifiers.command || modifiers.alt)) + }); + let refused = ctx.data(|m| m.get_temp::(widgets::dialog_reject_commit_flag())).unwrap_or(false); + widgets::reject_dialog_commit(ctx); + if !held && !refused { + d.fields.insert("__optimizeGo".into(), json!(true)); + ctx.request_repaint(); + } +} + /// The last Optimize to File Size result (`__optimized`) while it still holds: the settings are the /// ones it found and the art (`art`, from [`art_key`]) is the art it measured. pub(super) fn optimized_result<'a>(d: &'a Dialog, s: Option<&WebSettings>, art: &str) -> Option<&'a Value> { diff --git a/crates/ui-egui/src/dialogs/tests_save_for_web.rs b/crates/ui-egui/src/dialogs/tests_save_for_web.rs index 5c99f72d50..9514dbd8e0 100644 --- a/crates/ui-egui/src/dialogs/tests_save_for_web.rs +++ b/crates/ui-egui/src/dialogs/tests_save_for_web.rs @@ -785,6 +785,71 @@ fn optimize_to_file_size_takes_the_settings_that_fit() { assert!(!shown(&app)); } +/// Enter while the Optimize to File Size rows are open runs the search and writes nothing; once the +/// search has closed the rows, Enter saves with the settings it found. +#[test] +fn enter_in_the_optimize_rows_runs_the_search_and_saves_nothing() { + let dir = vectorcraft_testkit::temp_dir("sfw-optimize-enter"); + let (mut app, _) = app(&dir, false); + app.run("file.saveForWeb", json!({})).unwrap(); + set(&mut app, "format", json!("jpg")); + set(&mut app, "quality", json!(100)); + frame(&mut app); + let full = app.session.execute("document.exportForWeb.preview", &json!({"format": "jpg", "quality": 100})).unwrap()["bytes"].as_u64().unwrap(); + set(&mut app, "__optimizeK", json!(full as f64 * 0.8 / 1024.0)); + set(&mut app, "__optimizeStart", json!("current")); + let files = |dir: &std::path::Path| std::fs::read_dir(dir).map_or(0, |r| r.count()); + let enter_with = |app: &mut VectorcraftApp, modifiers: egui::Modifiers| { + let key = egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers }; + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + let mut out = ctx.run_ui(egui::RawInput { events: vec![key], ..Default::default() }, |ui| show(app, ui.ctx())); + out.textures_delta.clear(); + }; + let enter = |app: &mut VectorcraftApp| enter_with(app, egui::Modifiers::NONE); + // Ctrl+Enter, which the size field does not commit, neither searches nor saves. + enter_with(&mut app, egui::Modifiers::CTRL); + frame(&mut app); + assert_eq!(kind(&app), Some(save_for_web::KIND), "Ctrl+Enter in the rows does not save"); + assert!(app.ui.dialog.as_ref().unwrap().fields.contains_key("__optimizeK"), "the rows stay open"); + assert!(!app.ui.dialog.as_ref().unwrap().fields.contains_key("__optimized")); + assert_eq!(files(&dir), 0); + enter(&mut app); + assert_eq!(kind(&app), Some(save_for_web::KIND), "Enter in the rows does not save"); + assert_eq!(files(&dir), 0); + frame(&mut app); + let d = app.ui.dialog.as_ref().unwrap(); + let found = d.fields.get("__optimized").cloned().unwrap(); + assert_eq!(found["achieved"], json!(true), "{found}"); + assert!(!d.fields.contains_key("__optimizeK"), "the search closed the rows"); + enter(&mut app); + assert!(app.ui.dialog.is_none()); + assert_eq!(files(&dir), 1); +} + +/// An Enter in the Optimize to File Size rows runs no search when a field of the dialog, one drawn +/// after the rows included, refused the value typed in it on that press, or when a control drawn +/// after the rows (an open dropdown) took the press. +#[test] +fn enter_in_the_optimize_rows_runs_nothing_when_a_later_control_refused_or_took_it() { + // `before` stands for the controls drawn after the rows, which act on the press first. + let go = |before: fn(&egui::Context)| { + let mut d = Dialog::new(save_for_web::KIND, json!({"__optimizeK": 10})); + let key = egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() }; + let ctx = egui::Context::default(); + let mut out = ctx.run_ui(egui::RawInput { events: vec![key], ..Default::default() }, |ui| { + before(ui.ctx()); + save_for_web::optimize_on_enter(ui.ctx(), &mut d); + }); + out.textures_delta.clear(); + assert!(d.fields.contains_key("__optimizeK"), "the rows stay open"); + d.fields.contains_key("__optimizeGo") + }; + assert!(go(|_| {}), "a plain Enter runs the search"); + assert!(!go(crate::widgets::reject_dialog_commit), "a refused value runs nothing"); + assert!(!go(|ctx| ctx.input_mut(|i| _ = i.consume_key(egui::Modifiers::NONE, egui::Key::Enter))), "a taken press runs nothing"); +} + #[test] fn optimize_to_file_size_starts_at_the_size_of_the_slices_files() { let dir = vectorcraft_testkit::temp_dir("sfw-optimize-slices");