From 8fbc6fa707b216bf7cdcad410432755ecc75e6fa Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 29 Nov 2026 23:26:05 +0100 Subject: [PATCH] Fill: grey History in Contents when its source does not match (#17464) The Fill dialog asks the engine whether `edit.fill` with History would find a usable source (history_source::fill_source_usable, built on checked_source) and greys History in Contents when not, so the dropdown and the command never disagree. Today that is a source of another canvas size; a mode or depth refusal added to checked_source follows on its own. Co-Authored-By: Claude Opus 5.5 --- crates/engine-paint/src/history_source.rs | 7 + crates/engine-paint/src/lib.rs | 2 +- crates/engine/src/facade.rs | 4 +- .../src/dialogs/fill_history_greyed_tests.rs | 138 ++++++++++++++++++ crates/ui-egui/src/dialogs/fill_ui.rs | 13 +- 5 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 crates/ui-egui/src/dialogs/fill_history_greyed_tests.rs diff --git a/crates/engine-paint/src/history_source.rs b/crates/engine-paint/src/history_source.rs index ab82fbf3b0..4bf85dbf11 100644 --- a/crates/engine-paint/src/history_source.rs +++ b/crates/engine-paint/src/history_source.rs @@ -51,6 +51,13 @@ pub fn checked_source(st: &DocState, p: &Value, cmd: &str) -> Result bool { + matches!(checked_source(st, &Value::Null, "edit.fill"), Ok(Ok(_))) +} + /// The History Brush's refusals after Image › Mode, with Photoshop's alert texts (#12842): only /// the History Brush's pairs were measured (RGB to Grayscale, a depth change), so any other change, /// or both at once, converts the source as before. Document modes are compared: the pixel format diff --git a/crates/engine-paint/src/lib.rs b/crates/engine-paint/src/lib.rs index e52e2c9524..a723360351 100644 --- a/crates/engine-paint/src/lib.rs +++ b/crates/engine-paint/src/lib.rs @@ -17,7 +17,7 @@ use photocraft_engine_core::{ use photocraft_engine_core::{command_specs, hidden_target, param_schema, prefs}; pub mod cmd; -mod history_source; +pub mod history_source; mod install; pub use cmd::adjustment_brush_cmds; diff --git a/crates/engine/src/facade.rs b/crates/engine/src/facade.rs index 87d81460e7..b719e0322d 100644 --- a/crates/engine/src/facade.rs +++ b/crates/engine/src/facade.rs @@ -16,8 +16,8 @@ pub use photocraft_engine_core::Session as CoreSession; pub use photocraft_engine_core::*; // The painting commands' modules, at the paths they had inside the engine. pub use photocraft_engine_paint::{ - adjustment_brush_cmds, brush_cmds, brush_key_cmds, eraser_cmds, face_cmds, fill_cmds, fill_key_cmds, paint_cmds, pick_cmds, redeye_cmds, retouch_cmds, - sampler_cmds, stamp_cmds, + adjustment_brush_cmds, brush_cmds, brush_key_cmds, eraser_cmds, face_cmds, fill_cmds, fill_key_cmds, history_source, paint_cmds, pick_cmds, redeye_cmds, + retouch_cmds, sampler_cmds, stamp_cmds, }; // The vector-area commands' modules, likewise. pub use photocraft_engine_vector::{ diff --git a/crates/ui-egui/src/dialogs/fill_history_greyed_tests.rs b/crates/ui-egui/src/dialogs/fill_history_greyed_tests.rs new file mode 100644 index 0000000000..0f70e874c7 --- /dev/null +++ b/crates/ui-egui/src/dialogs/fill_history_greyed_tests.rs @@ -0,0 +1,138 @@ +//! #17464: the Fill dialog greys History in Contents whenever `edit.fill` would refuse the History source (another +//! canvas size after Image Size, Canvas Size, Crop or Rotate 90°), by the engine's own check; with a matching source it +//! stays enabled. The other entries keep their order and state. + +use craft::ui::patterns::form_dialogs::HostedParamDialog; +use photocraft_engine::history_source::fill_source_usable; +use serde_json::{Value, json}; + +use super::*; +use crate::smart_filter_dialog::tests::cancel; + +/// A white 40×30 document with a pixel layer "Paint" active and a selection, so only History's rule can grey an entry. +/// The current state is the History source, so the source has the layer Fill paints on. +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 40, "height": 30, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({"name": "Paint"})).unwrap(); + app.run("select.rect", json!({"x": 5, "y": 5, "width": 20, "height": 15})).unwrap(); + source_current(&mut app); + app +} + +/// Make the current state the History source. +fn source_current(app: &mut PhotocraftApp) { + let current = app.session.active().unwrap().history.entries().len() - 1; + app.run("history.setSource", json!({"state": current})).unwrap(); +} + +/// The canvas changes that leave the opening state's source at another size. Each is run by [`resized`]. +fn resizes() -> Vec<(&'static str, Value)> { + vec![ + ("image.imageSize", json!({"width": 20, "height": 15})), + ("image.canvasSize", json!({"width": 50, "height": 30})), + ("image.crop", json!({"x": 0, "y": 0, "width": 30, "height": 20})), + ("image.imageRotation.90cw", json!({})), + ] +} + +/// [`app`] after `cmd`, with a selection again (Image Size drops it), so Content-Aware's own rule stays out. +fn resized(cmd: &str, p: Value) -> Box { + let mut app = app(); + app.run(cmd, p).unwrap(); + app.run("select.rect", json!({"x": 2, "y": 2, "width": 8, "height": 6})).unwrap(); + app +} + +fn contents_row(app: &PhotocraftApp) -> Param { + rows(app, [0.0; 3], None, None).into_iter().find(|r| r.key == "contents").expect("a Contents row") +} + +fn keys(row: &Param) -> Vec { + let ParamKind::Choice { options, .. } = &row.kind else { panic!("Contents is a choice row") }; + options.iter().map(|(k, _)| k.clone()).collect() +} + +fn choosable(app: &mut PhotocraftApp, id: DialogId, key: &str) -> bool { + let ctx = app.ctx().clone(); + app.framework.dialogs.set_json_field(&ctx, id, "contents", json!(key)).is_ok() +} + +fn contents(app: &PhotocraftApp, id: DialogId) -> Option { + let dialog = app.framework.dialogs.get_by_id::(id).expect("a hosted dialog"); + dialog.fields().text("contents").map(str::to_string) +} + +/// Whether `edit.fill` with History succeeds, undone again so the document stays as it was. +fn history_fill_succeeds(app: &mut PhotocraftApp) -> bool { + let ok = app.run(COMMAND, json!({"contents": "history"})).is_ok(); + if ok { + app.run("edit.undo", json!({})).unwrap(); + } + ok +} + +#[test] +fn history_is_enabled_on_the_unchanged_document() { + let mut app = app(); + assert!(contents_row(&app).option_enabled("history")); + let id = open(&mut app); + assert!(choosable(&mut app, id, "history")); + cancel(&mut app, id); +} + +#[test] +fn a_source_of_another_canvas_size_greys_history_and_it_cannot_be_chosen() { + for (cmd, p) in resizes() { + let mut app = resized(cmd, p); + let row = contents_row(&app); + assert!(!row.option_enabled("history"), "{cmd}: History greyed"); + let order: Vec = CONTENTS.iter().map(|(k, _)| k.to_string()).collect(); + assert_eq!(keys(&row), order, "{cmd}: every entry listed, in the same order"); + for (key, _) in CONTENTS.iter().filter(|(k, _)| *k != "history") { + assert!(row.option_enabled(key), "{cmd}: {key} stays enabled"); + } + + let id = open(&mut app); + let before = contents(&app, id); + assert!(!choosable(&mut app, id, "history"), "{cmd}: History cannot be chosen"); + assert_eq!(contents(&app, id), before, "{cmd}: the choice is unchanged"); + cancel(&mut app, id); + } +} + +#[test] +fn a_matching_source_keeps_history_enabled() { + for (cmd, p) in resizes() { + let mut app = resized(cmd, p); + // The current state, which has the new size, becomes the source. + source_current(&mut app); + assert!(contents_row(&app).option_enabled("history"), "{cmd}: History enabled with a source of this size"); + } +} + +#[test] +fn the_dropdown_and_the_command_agree() { + let mut cases = vec![app()]; + for (cmd, p) in resizes() { + cases.push(resized(cmd, p)); + } + for mut app in cases { + let usable = fill_source_usable(app.session.active().unwrap()); + assert_eq!(contents_row(&app).option_enabled("history"), usable); + assert_eq!(history_fill_succeeds(&mut app), usable, "History offered exactly when the fill succeeds"); + } +} + +/// The framework restores only the remembered values a row accepts: a remembered History is dropped once greyed and the +/// row opens on its default, as it does for any unavailable choice; with a usable source it is kept. +#[test] +fn a_remembered_history_choice_keeps_todays_fallback_once_greyed() { + let mut saved = DialogFields::new(); + saved.set("contents", "history"); + let unchanged = ParamDialog::remembered(&rows(&app(), [0.0; 3], None, None), &saved); + assert_eq!(unchanged.text("contents"), Some("history"), "kept while usable"); + let app = resized("image.imageSize", json!({"width": 20, "height": 15})); + let greyed = ParamDialog::remembered(&rows(&app, [0.0; 3], None, None), &saved); + assert_eq!(greyed.text("contents"), None, "dropped once greyed: the row opens on Foreground Color"); +} diff --git a/crates/ui-egui/src/dialogs/fill_ui.rs b/crates/ui-egui/src/dialogs/fill_ui.rs index 8450823468..4fa192b0e9 100644 --- a/crates/ui-egui/src/dialogs/fill_ui.rs +++ b/crates/ui-egui/src/dialogs/fill_ui.rs @@ -44,10 +44,14 @@ pub(crate) fn hex(c: [f32; 3]) -> String { fn rows(app: &PhotocraftApp, foreground: [f32; 3], first_pattern: Option, preserve: Option) -> Vec { let contents = CONTENTS.iter().map(|(k, l)| (k.to_string(), Label::from(tl!(l)))).collect(); let mut contents = Param::new("contents", tl!("Contents:"), ParamKind::choice(contents, Some("foreground".into()))); + let st = app.session.active(); // Content-Aware fills a selection from around it: without one it is listed greyed, as in the reference (#17462). - if app.session.active().is_none_or(|st| st.doc.selection.is_none()) { - contents = contents.disabled_options(["contentAware"]); - } + // History is greyed whenever `edit.fill` would refuse its source, by the engine's own check (#17464). + let greyed = [ + ("contentAware", st.is_none_or(|st| st.doc.selection.is_none())), + ("history", st.is_none_or(|st| !photocraft_engine::history_source::fill_source_usable(st))), + ]; + contents = contents.disabled_options(greyed.into_iter().filter(|(_, off)| *off).map(|(key, _)| key)); let mut rows = vec![ contents, Param::new("color", tl!("Color:"), ParamKind::color(Some(foreground))).visible_when("contents", "color"), @@ -176,6 +180,9 @@ pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamRes #[cfg(test)] mod content_aware_greyed_tests; #[cfg(test)] +#[path = "fill_history_greyed_tests.rs"] +mod fill_history_greyed_tests; +#[cfg(test)] mod opacity_alert_tests; #[cfg(test)] mod preserve_transparency_state_tests;