From 34fb0596f9f39c403e56e762f6a108141a2e5a64 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 17:44:22 +0100 Subject: [PATCH 1/2] WIP: Gradient tool: keep the Classic mode out of tool presets (#17494) From 74f137ffdbdafef88ad7877f57d78166f8898aba Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 17:49:00 +0100 Subject: [PATCH 2/2] Gradient tool: keep the Classic mode out of tool presets (#17494) A Gradient tool preset neither stores nor applies gradient_classic, so choosing one leaves the Gradient / Classic gradient mode as it is, also for presets saved by older builds that still hold the key. The other options still apply, and Reset Tool / Reset All Tools still reset the mode. Co-Authored-By: Claude Opus 5.5 --- .../gradient_preset_classic_mode_tests.rs | 114 ++++++++++++++++++ .../src/panels/preset_panels/tool_presets.rs | 17 ++- 2 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 crates/ui-egui/src/panels/preset_panels/gradient_preset_classic_mode_tests.rs diff --git a/crates/ui-egui/src/panels/preset_panels/gradient_preset_classic_mode_tests.rs b/crates/ui-egui/src/panels/preset_panels/gradient_preset_classic_mode_tests.rs new file mode 100644 index 0000000000..e9b01687ec --- /dev/null +++ b/crates/ui-egui/src/panels/preset_panels/gradient_preset_classic_mode_tests.rs @@ -0,0 +1,114 @@ +//! #17494: a Gradient tool preset neither stores nor changes the Gradient / Classic gradient mode, as in the +//! reference application, and still restores the tool's other options. Reset Tool and Reset All Tools still +//! put the mode back to Gradient. + +use photocraft_engine::presets::PresetsExt; +use serde_json::{Value, json}; + +use super::{new_params, select}; +use crate::PhotocraftApp; +use crate::state::Tool; + +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Gradient; + app +} + +/// The + button's preset, made from the current Gradient tool options; its name and stored options. +fn save(app: &mut PhotocraftApp) -> (String, serde_json::Map) { + let name = app.run("tool.presets.new", new_params(app, Tool::Gradient)).unwrap()["name"].as_str().unwrap().to_string(); + let stored = app.session.presets().tool_presets.iter().find(|t| t.name == name).unwrap().options["toolOptions"].as_object().unwrap().clone(); + (name, stored) +} + +#[test] +fn a_new_gradient_preset_does_not_store_the_mode() { + for classic in [false, true] { + let mut app = app(); + app.ui.tool_options.gradient_classic = classic; + let (_, stored) = save(&mut app); + assert!(!stored.contains_key("gradient_classic"), "classic {classic}: {stored:?}"); + assert!(stored.contains_key("gradient_style"), "the tool's own options are still stored: {stored:?}"); + } +} + +/// Both ways: made in one mode, chosen in the other, the mode stays. +#[test] +fn choosing_a_gradient_preset_keeps_the_mode() { + for made_classic in [false, true] { + let mut app = app(); + app.ui.tool_options.gradient_classic = made_classic; + let (name, _) = save(&mut app); + app.ui.tool_options.gradient_classic = !made_classic; + select(&mut app, &name); + assert_eq!(app.ui.tool, Tool::Gradient); + assert_eq!(app.ui.tool_options.gradient_classic, !made_classic, "a preset made with classic {made_classic} changed the mode"); + } +} + +/// A preset saved by an older build still holds the key: choosing it leaves the mode alone too. +#[test] +fn an_older_preset_holding_the_mode_leaves_it_alone() { + for stored_classic in [false, true] { + let mut app = app(); + app.run( + "tool.presets.new", + json!({"name": "Old", "tool": "gradient", "options": {"toolOptions": {"gradient_classic": stored_classic, "gradient_style": "radial"}}}), + ) + .unwrap(); + app.ui.tool_options.gradient_classic = !stored_classic; + select(&mut app, "Old"); + assert_eq!(app.ui.tool_options.gradient_classic, !stored_classic, "an old preset with classic {stored_classic} changed the mode"); + assert_eq!(app.ui.tool_options.gradient_style, "radial", "its other options still apply"); + } +} + +#[test] +fn choosing_a_gradient_preset_restores_the_other_options() { + let mut app = app(); + let o = &mut app.ui.tool_options; + o.gradient_style = "radial".into(); + o.gradient_reverse = !o.gradient_reverse; + o.gradient_dither = !o.gradient_dither; + o.gradient_blend_mode = photocraft_color::BlendMode::Multiply; + o.fill_opacity = 50.0; + let made = (*app.ui.tool_options).clone(); + let (name, _) = save(&mut app); + + let o = &mut app.ui.tool_options; + o.gradient_style = "linear".into(); + o.gradient_reverse = !made.gradient_reverse; + o.gradient_dither = !made.gradient_dither; + o.gradient_blend_mode = photocraft_color::BlendMode::Normal; + o.fill_opacity = 100.0; + select(&mut app, &name); + let o = &app.ui.tool_options; + assert_eq!(o.gradient_style, made.gradient_style); + assert_eq!(o.gradient_reverse, made.gradient_reverse); + assert_eq!(o.gradient_dither, made.gradient_dither); + assert_eq!(o.gradient_blend_mode, made.gradient_blend_mode); + assert_eq!(o.fill_opacity, made.fill_opacity); +} + +/// Reset Tool with the Gradient tool and Reset All Tools still put the mode back to Gradient; Reset Tool on +/// another tool leaves it. +#[test] +fn reset_still_resets_the_mode() { + let mut app = app(); + let ctx = app.ctx().clone(); + app.ui.tool_options.gradient_classic = true; + app.ui.tool = Tool::Brush; + crate::menus::invoke(&mut app, &ctx, crate::tool_reset::RESET, json!({})).unwrap(); + assert!(app.ui.tool_options.gradient_classic, "the Brush's Reset Tool is not the Gradient tool's"); + + app.ui.tool = Tool::Gradient; + crate::menus::invoke(&mut app, &ctx, crate::tool_reset::RESET, json!({})).unwrap(); + assert!(!app.ui.tool_options.gradient_classic); + + app.ui.tool_options.gradient_classic = true; + crate::menus::invoke(&mut app, &ctx, crate::tool_reset::RESET_ALL, json!({"confirmed": true})).unwrap(); + assert!(!app.ui.tool_options.gradient_classic); +} diff --git a/crates/ui-egui/src/panels/preset_panels/tool_presets.rs b/crates/ui-egui/src/panels/preset_panels/tool_presets.rs index 69a3b6bd85..aa97a057e4 100644 --- a/crates/ui-egui/src/panels/preset_panels/tool_presets.rs +++ b/crates/ui-egui/src/panels/preset_panels/tool_presets.rs @@ -6,6 +6,10 @@ use super::run; use crate::PhotocraftApp; use crate::state::Tool; +/// Options a tool preset neither stores nor applies, though Reset Tool resets them: the +/// Gradient / Classic gradient mode sits outside the reference application's tool presets (#17494). +const NOT_IN_PRESETS: &[&str] = &["gradient_classic"]; + /// Apply a tool preset: the engine sets the brush/colour, the shell switches tool and options. pub fn select(app: &mut PhotocraftApp, name: &str) { // PLAN: assumes `ToolOptions::crop` is `Clone + PartialEq` (tools-w45). @@ -27,7 +31,7 @@ pub fn select(app: &mut PhotocraftApp, name: &str) { crate::tool_memory::upgrade(&mut stored); let mut cur = serde_json::to_value(&app.ui.tool_options).unwrap_or_default(); if let (Some(c), Some(o)) = (cur.as_object_mut(), stored.as_object()) { - for (k, v) in o { + for (k, v) in o.iter().filter(|(k, _)| !NOT_IN_PRESETS.contains(&k.as_str())) { // An object option (`sample_all`, one per tool) takes only the entries the // preset stored, so the other tools' keep theirs (#5524). match (c.get_mut(k), v) { @@ -53,8 +57,11 @@ pub fn select(app: &mut PhotocraftApp, name: &str) { /// preset later leaves the other tools' settings alone. pub(crate) fn new_params(app: &PhotocraftApp, tool: Tool) -> Value { let all = serde_json::to_value(&app.ui.tool_options).unwrap_or_default(); - let mut own: serde_json::Map = - crate::tool_reset::option_keys(tool).iter().filter_map(|k| Some(((*k).to_string(), all.get(*k)?.clone()))).collect(); + let mut own: serde_json::Map = crate::tool_reset::option_keys(tool) + .iter() + .filter(|k| !NOT_IN_PRESETS.contains(k)) + .filter_map(|k| Some(((*k).to_string(), all.get(*k)?.clone()))) + .collect(); // Sample All Layers is each tool's own (#5524): store `tool`'s alone. if let Some(k) = crate::tool_reset::sample_all_key(tool) && let Some(v) = all.get("sample_all").and_then(|m| m.get(&k)) @@ -64,6 +71,10 @@ pub(crate) fn new_params(app: &PhotocraftApp, tool: Tool) -> Value { json!({"name": tool.label(), "tool": tool.id(), "options": {"toolOptions": own}}) } +#[cfg(test)] +#[path = "gradient_preset_classic_mode_tests.rs"] +mod gradient_preset_classic_mode_tests; + #[cfg(test)] mod tests { use super::*;