Merge pull request #18391 from davidmann4/fix/17494-gradient-preset-classic-mode

Gradient tool: keep the Classic mode out of tool presets (#17494)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-30 23:35:38 +01:00
commit 2d8bd1f5a2
2 files changed
+128 -3

No files matched your search

@@ -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<PhotocraftApp> {
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<String, Value>) {
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);
}
@@ -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<String, Value> =
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<String, Value> = 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::*;