From b57ec9f729ae55722dc44ae7cd2801ada7821729 Mon Sep 17 00:00:00 2001 From: Firstbober Date: Wed, 28 Oct 2026 11:48:58 +0100 Subject: [PATCH] fix: preserve indexed matte when picker is canceled --- crates/ui-egui/src/dialogs/color_picker_ui.rs | 10 +++++- crates/ui-egui/src/dialogs/filter_dialog.rs | 9 ++++-- .../src/dialogs/filter_dialog/tests.rs | 31 +++++++++++++++++++ docs/development.md | 8 +++-- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/dialogs/color_picker_ui.rs b/crates/ui-egui/src/dialogs/color_picker_ui.rs index 1c0ce157e8..d713fbf9d7 100644 --- a/crates/ui-egui/src/dialogs/color_picker_ui.rs +++ b/crates/ui-egui/src/dialogs/color_picker_ui.rs @@ -271,6 +271,10 @@ fn set_field(f: &mut Map, field: &str, value: Value) -> Result<() /// Opens the Color Picker dialog `id`'s body asked for ([`request`], [`request_field`]), on its /// colour: OK sets that field of the dialog (not the foreground), Cancel leaves it as it was. +/// +/// A dialog can provide `__pickColorDefault` when its field is a non-colour choice that has not +/// yet been replaced by the picked colour. The temporary default seeds the picker without +/// changing the parent field; it is removed before the picker opens. pub fn open_requested(app: &mut PhotocraftApp, id: u64) { let Some(d) = app.ui.dialog_mut(id) else { return }; let (title, field) = match d.fields.remove(REQUEST) { @@ -282,7 +286,11 @@ pub fn open_requested(app: &mut PhotocraftApp, id: u64) { } _ => return, }; - let rgb = field_value(&d.fields, &field).and_then(parse_hex).unwrap_or([0.0; 3]); + let rgb = field_value(&d.fields, &field) + .and_then(parse_hex) + .or_else(|| d.fields.get("__pickColorDefault").and_then(Value::as_str).and_then(parse_hex)) + .unwrap_or([0.0; 3]); + d.fields.remove("__pickColorDefault"); open_for_field(app, &title, rgb, id, &field); } diff --git a/crates/ui-egui/src/dialogs/filter_dialog.rs b/crates/ui-egui/src/dialogs/filter_dialog.rs index 47eec78cbf..4a91bdf81b 100644 --- a/crates/ui-egui/src/dialogs/filter_dialog.rs +++ b/crates/ui-egui/src/dialogs/filter_dialog.rs @@ -870,8 +870,13 @@ fn indexed_matte_row(ui: &mut egui::Ui, fields: &mut Map) { }); let custom = choice == "custom"; if changed { - let next = if custom { crate::color_picker_ui::hex(color) } else { choice.to_owned() }; - fields.insert("matte".into(), json!(next)); + if custom { + // Keep the previous matte until the picker confirms; the picker uses this only as + // its initial colour when switching from a named matte or None. + fields.insert("__pickColorDefault".into(), json!(crate::color_picker_ui::hex(color))); + } else { + fields.insert("matte".into(), json!(choice.to_owned())); + } } if custom && (changed || pick) { crate::color_picker_ui::request_field(fields, "matte", &crate::color_picker_ui::title_for("Matte")); diff --git a/crates/ui-egui/src/dialogs/filter_dialog/tests.rs b/crates/ui-egui/src/dialogs/filter_dialog/tests.rs index 4225bdedf1..db0f606677 100644 --- a/crates/ui-egui/src/dialogs/filter_dialog/tests.rs +++ b/crates/ui-egui/src/dialogs/filter_dialog/tests.rs @@ -1686,3 +1686,34 @@ fn indexed_matte_dropdown_and_picker_change_the_applied_edge_color() { assert_eq!(surface.rgba(1, 0)[3], 0.0); } } + +/// Selecting Other… must not replace None until the custom colour picker is confirmed. +#[test] +fn indexed_matte_cancel_keeps_the_previous_choice() { + use egui::accesskit::Role; + use egui_kittest::{Harness, kittest::Queryable}; + + let mut session = photocraft_engine::Session::new(); + session.execute("file.new", json!({"width": 2, "height": 1, "depth": 32, "background": "transparent"})).unwrap(); + let mut app = PhotocraftApp::new(session, crate::Services::default()); + let mut harness = Harness::builder().with_size(egui::vec2(900.0, 1000.0)).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), &mut app); + PhotocraftApp::setup_context(&harness.ctx, crate::theme::ThemeKind::ALL[0]); + let id = open(harness.state_mut(), "image.mode.indexedColor").unwrap(); + harness.run_steps(3); + harness.query_all_by_role(Role::ComboBox).find(|node| node.value().as_deref() == Some("None")).expect("Matte dropdown").click(); + harness.run_steps(2); + harness.get_by_label("Other…").click(); + harness.run_steps(3); + + let picker = harness.state().ui.dialogs.last().expect("custom matte picker").id; + assert_ne!(picker, id); + let parent = harness.state().ui.dialogs.iter().find(|dialog| dialog.id == id).expect("Indexed Color dialog"); + assert_eq!(parent.fields.get("matte").and_then(Value::as_str), None, "opening Other… does not commit a matte"); + assert_eq!(harness.state().ui.dialogs.last().unwrap().fields["color"], "#ffffff", "the previous default still seeds the picker"); + + crate::dialogs::cancel(harness.state_mut(), picker).unwrap(); + harness.run_steps(3); + let parent = harness.state().ui.dialogs.iter().find(|dialog| dialog.id == id).expect("Indexed Color dialog"); + assert_eq!(parent.fields.get("matte").and_then(Value::as_str), None, "cancel restores the previous None choice"); + assert!(!parent.fields.contains_key("__pickColorDefault")); +} diff --git a/docs/development.md b/docs/development.md index 0d907fb97e..836b68f85f 100644 --- a/docs/development.md +++ b/docs/development.md @@ -662,9 +662,11 @@ except Place, which is a job that `poll_jobs` steps on the UI thread (a *coopera ## Indexed Color timings The stock Indexed Color dialog exposes Matte: None, Foreground Color, Background Color, -White, Black and Other… (the shared Color Picker). Automation accepts `matte` as `none`, -`foreground`, `background`, `white`, `black` or `#rrggbb`. A selected matte blends all -partially transparent edges; with Transparency on, fully clear pixels stay transparent. +White, Black and Other… (the shared Color Picker). Choosing Other… opens the picker without +replacing the current matte; Cancel leaves that choice unchanged, and OK commits the picked +`#rrggbb`. Automation accepts `matte` as `none`, `foreground`, `background`, +`white`, `black` or `#rrggbb`. A selected matte blends all partially transparent +edges; with Transparency on, fully clear pixels stay transparent. With Transparency off, the selected matte also fills clear pixels. None retains the existing hard-edged transparency/default flattening behavior. Indexed conversion retains the document's ICC profile and encoded RGB sample representation; matte colours are interpreted in the document