From 2b5f0f598489bbabdcf198ef9d9b2d56f919902b Mon Sep 17 00:00:00 2001 From: funkvay-star <71777367+funkvay-star@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:20:04 +0400 Subject: [PATCH] Levels: Alt turns Cancel into Reset (#3328) Holding Alt in the Image > Adjustments > Levels dialog turns Cancel into Reset, as in Color Balance: every channel's input, gamma and output levels go back to the values the dialog opened with (RGB, Gray, CMYK inks or Lab), the Channel popup returns to the composite, and the dialog stays open. Other kinds keep Cancel under Alt. --- crates/ui-egui/src/adjust_dialog.rs | 86 +++++++++++++++++++++++++--- crates/ui-egui/src/adjust_editors.rs | 16 +++++- 2 files changed, 93 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/adjust_dialog.rs b/crates/ui-egui/src/adjust_dialog.rs index a4b831ed2f..2c2175154d 100644 --- a/crates/ui-egui/src/adjust_dialog.rs +++ b/crates/ui-egui/src/adjust_dialog.rs @@ -131,20 +131,23 @@ fn dialog_mem(kind: &str) -> egui::Id { egui::Id::new(("adjust-dialog", kind)) } -/// Whether holding Alt turns the dialog's Cancel into Reset. Measured in Photoshop 25.4 for Color -/// Balance; other kinds keep Cancel until checked against Photoshop. +/// Whether holding Alt turns the dialog's Cancel into Reset, as in Photoshop's Color Balance +/// (measured in 25.4) and Levels dialogs; other kinds keep Cancel until checked against Photoshop. pub fn resets(fields: &Map) -> bool { - fields.get("__adjust").and_then(Value::as_str) == Some("colorBalance") + matches!(fields.get("__adjust").and_then(Value::as_str), Some("colorBalance" | "levels")) } /// Alt+Cancel (Reset): back to the defaults Photoshop's Reset restores, dialog stays open. pub fn reset(ctx: &egui::Context, fields: &mut Map) { - if !resets(fields) { - return; - } let mut values = crate::filter_dialog::params_of(fields); - adjust_editors::reset_color_balance(ctx, dialog_mem("colorBalance"), &mut values); + match fields.get("__adjust").and_then(Value::as_str) { + Some("colorBalance") => adjust_editors::reset_color_balance(ctx, dialog_mem("colorBalance"), &mut values), + Some("levels") => adjust_editors::reset_levels(ctx, dialog_mem("levels"), &mut values), + _ => return, + } if let Value::Object(v) = values { + // Replace the parameter keys, keep the private ones (as `body` does). + fields.retain(|k, _| k.starts_with("__")); fields.extend(v); } } @@ -393,12 +396,79 @@ mod tests { use egui_kittest::kittest::Queryable; let mut h = Harness::builder().with_size(egui::vec2(1200.0, 900.0)).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app_with_image()); PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); - open(h.state_mut(), "image.adjustments.levels").unwrap(); + open(h.state_mut(), "image.adjustments.curves").unwrap(); h.event(egui::Event::ModifiersChanged(egui::Modifiers::ALT)); h.run_steps(3); assert!(h.query_by_label("Cancel").is_some() && h.query_by_label("Reset").is_none()); } + /// #3328: holding Alt turns the Levels dialog's Cancel into Reset, as in Photoshop: every + /// channel's levels back to neutral and the Channel choice back to the composite, with the + /// dialog still open and the document untouched. + #[test] + fn levels_dialog_alt_cancel_resets_every_channel() { + use egui_kittest::kittest::Queryable; + let mut h = Harness::builder() + .with_size(egui::vec2(1200.0, 900.0)) + .with_step_dt(1.0 / 60.0) + .build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app_with_image()); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + let original = h.state().session.active().unwrap().doc.clone(); + let id = open(h.state_mut(), "image.adjustments.levels").unwrap(); + let defaults = crate::filter_dialog::params_of(&h.state_mut().ui.dialog_mut(id).unwrap().fields); + let d = h.state_mut().ui.dialog_mut(id).unwrap(); + d.fields.insert("inBlack".into(), json!(30)); + d.fields.insert("red".into(), json!({"inBlack": 0, "gamma": 1.0, "inWhite": 200, "outBlack": 0, "outWhite": 255})); + d.fields.insert("green".into(), json!({"inBlack": 0, "gamma": 1.4, "inWhite": 255, "outBlack": 0, "outWhite": 255})); + d.fields.insert("blue".into(), json!({"inBlack": 0, "gamma": 1.0, "inWhite": 255, "outBlack": 20, "outWhite": 255})); + let channel = egui::Id::new(("adjust-dialog", "levels")).with("levels-ch"); + h.ctx.data_mut(|d| d.insert_temp(channel, 2usize)); + h.run_steps(3); + assert!(h.query_by_label("Cancel").is_some() && h.query_by_label("Reset").is_none()); + h.event(egui::Event::ModifiersChanged(egui::Modifiers::ALT)); + h.run_steps(2); + let reset = h.get_by_label("Reset").rect(); + assert!(h.query_by_label("Cancel").is_none()); + let click = + |pressed| egui::Event::PointerButton { pos: reset.center(), button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::ALT }; + input(&mut h, egui::Modifiers::ALT, &[egui::Event::PointerMoved(reset.center()), click(true), click(false)]); + let fields = h.state().ui.dialogs.iter().find(|d| d.id == id).map(|d| d.fields.clone()).expect("Reset keeps the dialog open"); + assert_eq!(crate::filter_dialog::params_of(&fields), defaults, "every channel back to neutral"); + assert_eq!(fields.get("__preview"), Some(&json!(true)), "Reset keeps Preview"); + assert_eq!(h.ctx.data(|d| d.get_temp::(channel)), Some(0), "Reset shows the composite channel"); + assert!(std::sync::Arc::ptr_eq(&h.state().session.active().unwrap().doc, &original), "Reset leaves the document alone"); + // Without Alt it is Cancel again, and it closes. + let cancel = h.get_by_label("Cancel").rect(); + let click = + |pressed| egui::Event::PointerButton { pos: cancel.center(), button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + input(&mut h, egui::Modifiers::NONE, &[egui::Event::PointerMoved(cancel.center()), click(true), click(false)]); + assert!(h.state().ui.dialogs.is_empty(), "Cancel closes"); + assert!(std::sync::Arc::ptr_eq(&h.state().session.active().unwrap().doc, &original)); + } + + /// Levels' Reset writes the parameter set the dialog opened with in every colour mode (RGB + /// channels, Gray's composite, CMYK's inks, Lab's lightness/a/b), dropping stray keys. + #[test] + fn levels_reset_restores_the_opening_parameters_in_every_mode() { + let ctx = egui::Context::default(); + for mode in ["rgb", "gray", "cmyk", "lab"] { + let mut app = app_in_mode(mode); + let id = open(&mut app, "image.adjustments.levels").unwrap(); + let fields = &mut app.ui.dialog_mut(id).unwrap().fields; + let defaults = crate::filter_dialog::params_of(fields); + fields.insert("inBlack".into(), json!(40)); + fields.insert("gray".into(), json!({"inWhite": 100})); + for key in ["red", "cyan", "a"] { + if fields.contains_key(key) { + fields.insert(key.into(), json!({"inBlack": 10, "gamma": 2.0, "inWhite": 180, "outBlack": 5, "outWhite": 250})); + } + } + reset(&ctx, fields); + assert_eq!(crate::filter_dialog::params_of(fields), defaults, "{mode}"); + assert_eq!(fields.get("__adjust"), Some(&json!("levels")), "{mode}: private keys kept"); + } + } + fn sample_params(kind: &str) -> Value { match kind { "brightnessContrast" => json!({"brightness": 40}), diff --git a/crates/ui-egui/src/adjust_editors.rs b/crates/ui-egui/src/adjust_editors.rs index 53964d8939..d72fb90f06 100644 --- a/crates/ui-egui/src/adjust_editors.rs +++ b/crates/ui-egui/src/adjust_editors.rs @@ -526,13 +526,27 @@ fn nearest(xs: &[f32], x: f32) -> Option { xs.iter().enumerate().min_by(|a, b| (a.1 - x).abs().total_cmp(&(b.1 - x).abs())).map(|(i, _)| i as u8) } +fn levels_channel_id(mem: egui::Id) -> egui::Id { + mem.with("levels-ch") +} + +/// Levels reset to defaults (Alt+Cancel in the dialog): every channel's input, gamma and output +/// levels back to neutral in the values' channel space, and the Channel choice back to the +/// composite, as the dialog opens. +pub fn reset_levels(ctx: &egui::Context, mem: egui::Id, v: &mut Value) { + let neutral = photocraft_doc::adjust::LevelsChannel::default(); + let space = space_of(v); + *v = adjust_params::to_params(&Adjustment::Levels { master: neutral.clone(), per_channel: Default::default(), space, black: neutral }); + ctx.data_mut(|d| d.insert_temp(levels_channel_id(mem), 0usize)); +} + fn levels(ui: &mut egui::Ui, v: &mut Value, cx: &EditorCx) -> Edit { let t = Tokens::get(ui.ctx()); let chans = tone_channels(space_of(v), cx.gray); let mut ch = 0; let mut auto = false; ui.horizontal(|ui| { - ch = channel_picker(ui, cx.mem.with("levels-ch"), chans, tl!("Channel:")); + ch = channel_picker(ui, levels_channel_id(cx.mem), chans, tl!("Channel:")); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { auto = widgets::secondary_button(ui, tl!("Auto"), 0.0).clicked(); });