Photo Filter: reopen with the choice its colour implies; complete recorded settings

Review follow-ups for #9879:

- Photo Filter's Filter / Color choice lives in the editor memory, so an
  opening that switched it and was cancelled left the next opening on
  Filter with a custom colour (the colour button disabled). Each opening
  now forgets it once, and the editor derives it from the colour again.
- A smart filter's recorded adjustment settings are completed through
  the engine's parser before they fill the dialog, so a recorded Photo
  Filter that names only its `filter` shows and saves that preset's
  colour instead of the dialog's neutral one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
ljkxandClaude Opus 5.5 committed 2026-10-28 17:08:52 +01:00
1 parent a9b878834d
commit 744651bb4d
5 files changed
+68 -2

No files matched your search

@@ -17,6 +17,8 @@ use crate::tone::{self, HistSource};
const PREFIX: &str = "image.adjustments.";
const CURVE_PICKER: &str = "__curvePicker";
const CURVE_PICKER_BASE: &str = "__curvePickerBase";
/// Set once a Photo Filter dialog has drawn: its editor memory was refreshed for this opening.
const PHOTO_FILTER_FRESH: &str = "__photoFilterFresh";
fn picker(value: Option<&Value>) -> Option<photocraft_engine::adjust_params::CurvesEyedropper> {
use photocraft_engine::adjust_params::CurvesEyedropper;
@@ -306,6 +308,15 @@ pub fn open_last(app: &mut PhotocraftApp, command: &str) -> Option<u64> {
Some(id)
}
/// `params` of the adjustment `command` with every key filled in as the engine reads them: a
/// recorded Photo Filter that names only its `filter` gets that preset's `color`, not the dialog's
/// neutral one. `None` for other commands and for params the engine rejects.
pub(crate) fn complete_params(app: &PhotocraftApp, command: &str, params: &Value) -> Option<Value> {
let kind = command.strip_prefix(PREFIX).filter(|k| adjust_editors::has_editor(k))?;
let adjustment = photocraft_engine::adjust_params::from_params(kind, params, None, curve_mode(app, kind)).ok()?;
Some(photocraft_engine::adjust_params::to_params(&adjustment))
}
/// Remember an adjustment dialog's settings after a successful OK, for [`open_last`]. Editing a
/// smart filter's settings is not a use of the dialog's own (as `filter_dialog::remember`).
pub(crate) fn remember(app: &mut PhotocraftApp, fields: &Map<String, Value>) {
@@ -340,6 +351,12 @@ fn curve_mode(app: &PhotocraftApp, kind: &str) -> photocraft_doc::ColorMode {
pub fn body(app: &mut PhotocraftApp, ui: &mut egui::Ui, fields: &mut Map<String, Value>) {
let kind = fields.get("__adjust").and_then(Value::as_str).unwrap_or_default().to_string();
let mode = curve_mode(app, &kind);
// Each opening starts from the settings it opened at: Photo Filter's Filter / Color choice
// follows its colour, whatever a cancelled opening left in the editor memory.
if kind == "photoFilter" && !fields.contains_key(PHOTO_FILTER_FRESH) {
adjust_editors::forget_photo_filter_mode(ui.ctx(), dialog_mem(&kind));
fields.insert(PHOTO_FILTER_FRESH.into(), json!(false));
}
if matches!(kind.as_str(), "levels" | "curves") {
picker_controls(ui, fields, mode);
ui.add_space(4.0);
@@ -885,3 +885,38 @@ fn photo_filter_opens_at_its_last_settings() {
photocraft_engine::adjust_params::to_params(&photocraft_engine::adjust_params::default_for("levels", photocraft_doc::ColorMode::Rgb).unwrap());
assert_eq!(app.ui.dialog_mut(again).unwrap().fields["inBlack"], defaults["inBlack"], "Levels opens neutral without ⌥");
}
/// #9879 review: a custom colour confirmed in Photo Filter reopens with Color chosen, even after an
/// opening that switched to Filter and was cancelled (the choice lives in the editor memory).
#[test]
fn photo_filter_reopens_with_the_choice_its_colour_implies() {
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]);
let cmd = "image.adjustments.photoFilter";
let id = open(h.state_mut(), cmd).unwrap();
h.state_mut().ui.dialog_mut(id).unwrap().fields.insert("color".into(), json!("#123456"));
h.run_steps(2);
crate::dialogs::confirm(h.state_mut(), id).unwrap();
// The next opening chooses Filter (what clicking its radio stores), then is cancelled.
let mode = dialog_mem("photoFilter").with(crate::adjust_editors::PHOTO_FILTER_MODE);
let id = open(h.state_mut(), cmd).unwrap();
h.run_steps(2);
h.ctx.data_mut(|d| d.insert_temp(mode, false));
h.state_mut().ui.close_dialog(id);
h.run_steps(1);
let id = open(h.state_mut(), cmd).unwrap();
h.run_steps(2);
assert_eq!(h.state_mut().ui.dialog_mut(id).unwrap().fields["color"], json!("#123456"));
assert_eq!(h.ctx.data(|d| d.get_temp::<bool>(mode)), Some(true), "Color is chosen for the custom colour");
}
/// #9879 review: a recorded Photo Filter that names only its preset completes to that preset's
/// colour, so editing it as a smart filter doesn't swap in the dialog's neutral colour.
#[test]
fn recorded_photo_filter_settings_complete_to_their_preset_colour() {
let app = app_with_image();
let done = complete_params(&app, "image.adjustments.photoFilter", &json!({"filter": "cooling80", "density": 60})).unwrap();
assert_eq!((done["color"].as_str(), done["density"].as_f64()), (Some("#006dff"), Some(60.0)));
assert!(complete_params(&app, "filter.blur.gaussian", &json!({"radius": 2})).is_none(), "not an adjustment");
assert!(complete_params(&app, "image.adjustments.photoFilter", &json!({"filter": "nope"})).is_none(), "rejected params");
}
@@ -30,6 +30,7 @@ mod hue_saturation;
mod levels;
mod session;
pub(crate) use basic::PHOTO_FILTER_MODE;
#[cfg(test)]
pub(crate) use basic::row_value;
#[cfg(test)]
@@ -71,6 +72,12 @@ pub fn has_editor(kind: &str) -> bool {
KINDS.contains(&kind)
}
/// Forget Photo Filter's Filter / Color choice in the editor memory `mem`, so the editor derives it
/// from the colour again: a choice changed and then cancelled must not outlive the dialog (#9879).
pub(crate) fn forget_photo_filter_mode(ctx: &egui::Context, mem: egui::Id) {
ctx.data_mut(|d| d.remove::<bool>(mem.with(PHOTO_FILTER_MODE)));
}
/// Kinds that draw a histogram behind their controls.
pub fn needs_histogram(kind: &str) -> bool {
matches!(kind, "levels" | "curves" | "threshold")
@@ -194,12 +194,16 @@ pub(super) fn black_white(ui: &mut egui::Ui, v: &mut Value, cx: &EditorCx) -> Ed
// ---------------------------------------------------------------------------------------------
// Photo Filter
/// The editor memory key of Photo Filter's Filter / Color choice (unset: Color for a colour that
/// is no preset).
pub(crate) const PHOTO_FILTER_MODE: &str = "pf-mode";
pub(super) fn photo_filter(ui: &mut egui::Ui, v: &mut Value, cx: &EditorCx) -> Edit {
let mut e = Edit::default();
let mut c = rgb_of(v, "color", [236.0 / 255.0, 138.0 / 255.0, 0.0]);
let hex = adjust_params::hex(c);
let preset = PHOTO_FILTERS.iter().find(|f| adjust_params::hex(f.2.map(|x| f32::from(x) / 255.0)) == hex).map(|f| f.0);
let mode_id = cx.mem.with("pf-mode");
let mode_id = cx.mem.with(PHOTO_FILTER_MODE);
let mut use_color: bool = ui.data(|d| d.get_temp(mode_id)).unwrap_or(preset.is_none());
ui.horizontal(|ui| {
if ui.radio(!use_color, tl!("Filter:")).clicked() {
+4 -1
View File
@@ -495,9 +495,12 @@ pub fn open_editor(app: &mut PhotocraftApp, ctx: &egui::Context, layer: u64, sm:
}) else {
return;
};
// An adjustment's recorded settings are completed first, so a key the record lacks shows (and
// saves) what the engine reads for it rather than the dialog's neutral value.
let params = crate::adjust_dialog::complete_params(app, &f.command, &f.params).unwrap_or_else(|| f.params.clone());
if let Some(d) = app.ui.dialog_mut(id) {
d.fields.insert("__smartFilter".into(), target);
if let Value::Object(p) = &f.params {
if let Value::Object(p) = &params {
for (k, v) in p {
if !k.starts_with("__") {
d.fields.insert(k.clone(), v.clone());