mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
The + button in Window › Tool Presets stored the whole ToolOptions struct, so choosing the preset later wrote every other tool's options back (e.g. Crop's Delete Cropped Pixels). Reset Tool's per-tool field lists become `option_keys`, which the + button now uses to store only the tool's own options. Brush/Pencil Clear, the Eraser's mode, the Remove tool's options and Content-Aware Move's Transform On Drop join their tools' lists, and a test checks that every option belongs to a tool. Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
5f648126c7
commit
06d4d31dd4
2 files changed
+79
-11
No files matched your search
@@ -1025,6 +1025,15 @@ pub fn select_tool_preset(app: &mut PhotocraftApp, name: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The `tool.presets.new` params for the + button: `tool`'s own options only, so choosing the
|
||||
/// preset later leaves the other tools' settings alone.
|
||||
fn new_tool_preset_params(app: &PhotocraftApp, tool: Tool) -> Value {
|
||||
let all = serde_json::to_value(&app.ui.tool_options).unwrap_or_default();
|
||||
let 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();
|
||||
json!({"name": tool.label(), "tool": tool_id(tool), "options": {"toolOptions": own}})
|
||||
}
|
||||
|
||||
pub fn tool_presets_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let only = app.ui.presets_ui.current_tool_only;
|
||||
@@ -1093,9 +1102,7 @@ pub fn tool_presets_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
action = Some(("tool.presets.edit".into(), json!({"action": "delete", "preset": n})));
|
||||
}
|
||||
if crate::icons::button(ui, "plus", 24.0, false, tl!("Create new tool preset")).clicked() {
|
||||
let label = cur.label().to_string();
|
||||
let opts = json!({"toolOptions": serde_json::to_value(&app.ui.tool_options).unwrap_or_default()});
|
||||
action = Some(("tool.presets.new".into(), json!({"name": label, "tool": tool_id(cur), "options": opts})));
|
||||
action = Some(("tool.presets.new".into(), new_tool_preset_params(app, cur)));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1262,6 +1269,7 @@ pub fn clone_sample_point(app: &PhotocraftApp, at: Option<[f64; 2]>) -> Option<[
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::menus::invoke;
|
||||
use crate::state::ToolOptions;
|
||||
|
||||
fn app() -> (PhotocraftApp, egui::Context) {
|
||||
let ctx = egui::Context::default();
|
||||
@@ -1311,6 +1319,28 @@ mod tests {
|
||||
assert_eq!(Tool::from_name(&tool_id(Tool::CustomShape)), Some(Tool::CustomShape));
|
||||
}
|
||||
|
||||
/// #7356: a preset made with + keeps only its own tool's options, so choosing it doesn't
|
||||
/// reset the Crop tool's Delete Cropped Pixels or the Move tool's Auto-Select.
|
||||
#[test]
|
||||
fn new_tool_preset_stores_only_its_tools_options() {
|
||||
let (mut app, _) = app();
|
||||
app.ui.tool = Tool::RectMarquee;
|
||||
app.ui.tool_options.feather = 5.0;
|
||||
let p = new_tool_preset_params(&app, Tool::RectMarquee);
|
||||
let r = app.run("tool.presets.new", p).unwrap();
|
||||
let name = r["name"].as_str().unwrap().to_string();
|
||||
let stored = &app.session.presets.tool_presets.iter().find(|t| t.name == name).unwrap().options["toolOptions"];
|
||||
assert_eq!(stored["feather"], json!(5.0));
|
||||
assert!(stored.as_object().unwrap().keys().all(|k| !k.starts_with("crop_") && !k.starts_with("move_")), "{stored}");
|
||||
app.ui.tool_options.feather = 0.0;
|
||||
app.ui.tool_options.crop_delete = !ToolOptions::default().crop_delete;
|
||||
app.ui.tool_options.move_auto_select = !ToolOptions::default().move_auto_select;
|
||||
select_tool_preset(&mut app, &name);
|
||||
assert_eq!(app.ui.tool_options.feather, 5.0);
|
||||
assert_eq!(app.ui.tool_options.crop_delete, !ToolOptions::default().crop_delete);
|
||||
assert_eq!(app.ui.tool_options.move_auto_select, !ToolOptions::default().move_auto_select);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_shape_tool_places_the_picked_shape() {
|
||||
let (mut app, _) = app();
|
||||
|
||||
@@ -113,9 +113,32 @@ fn is_selection_tool(tool: Tool) -> bool {
|
||||
|
||||
/// Put `tool`'s fields of `o` back to their defaults.
|
||||
pub fn reset_options(o: &mut ToolOptions, tool: Tool) {
|
||||
let d = ToolOptions::default();
|
||||
let keys = option_keys(tool);
|
||||
if keys.is_empty() {
|
||||
return;
|
||||
}
|
||||
let (Ok(Value::Object(mut cur)), Ok(Value::Object(d))) = (serde_json::to_value(&*o), serde_json::to_value(ToolOptions::default())) else {
|
||||
return;
|
||||
};
|
||||
for k in keys {
|
||||
if let Some(v) = d.get(*k) {
|
||||
cur.insert((*k).to_string(), v.clone());
|
||||
}
|
||||
}
|
||||
if let Ok(n) = serde_json::from_value(Value::Object(cur)) {
|
||||
*o = n;
|
||||
}
|
||||
}
|
||||
|
||||
/// The [`ToolOptions`] fields `tool` shows, by their serialized names: what Reset Tool resets and
|
||||
/// what a tool preset made with + stores (#7356).
|
||||
pub fn option_keys(tool: Tool) -> &'static [&'static str] {
|
||||
// `reset!` names each field once; one that is misspelt or gone fails to compile.
|
||||
macro_rules! reset {
|
||||
($($f:ident),* $(,)?) => {{ $( o.$f = d.$f.clone(); )* }};
|
||||
($($f:ident),* $(,)?) => {{
|
||||
let _ = |o: &ToolOptions| { $(let _ = &o.$f;)* };
|
||||
&[$(stringify!($f)),*]
|
||||
}};
|
||||
}
|
||||
match tool {
|
||||
Tool::Move => reset!(move_auto_select, move_target, move_show_transform),
|
||||
@@ -144,7 +167,10 @@ pub fn reset_options(o: &mut ToolOptions, tool: Tool) {
|
||||
),
|
||||
Tool::PerspectiveCrop => reset!(crop_width, crop_height, crop_resolution, crop_resolution_unit, perspective_crop_grid),
|
||||
Tool::Eyedropper => reset!(eyedropper_size, eyedropper_sample, eyedropper_ring),
|
||||
Tool::Pencil => reset!(pencil_auto_erase),
|
||||
Tool::Brush => reset!(brush_clear),
|
||||
Tool::Pencil => reset!(pencil_auto_erase, pencil_clear),
|
||||
Tool::Eraser => reset!(eraser_mode),
|
||||
Tool::Remove => reset!(remove_new_layer, remove_after_each_stroke),
|
||||
Tool::BackgroundEraser => reset!(bg_sampling, bg_limits, bg_tolerance, bg_protect_fg),
|
||||
Tool::ColorReplacement => reset!(cr_mode, cr_sampling, cr_limits, cr_tolerance, cr_anti_alias),
|
||||
Tool::MagicEraser => reset!(tolerance, anti_alias, contiguous, sample_all_layers, magic_eraser_opacity),
|
||||
@@ -155,7 +181,7 @@ pub fn reset_options(o: &mut ToolOptions, tool: Tool) {
|
||||
Tool::SpotHealing => reset!(spot_type, sample_all_layers),
|
||||
Tool::Healing => reset!(clone_aligned, clone_sample),
|
||||
Tool::Patch => reset!(patch_mode, patch_content_aware, patch_structure, patch_color, sample_all_layers),
|
||||
Tool::ContentAwareMove => reset!(cam_mode, cam_structure, cam_color, sample_all_layers),
|
||||
Tool::ContentAwareMove => reset!(cam_mode, cam_structure, cam_color, cam_transform_on_drop, sample_all_layers),
|
||||
Tool::RedEye => reset!(red_eye_pupil_size, red_eye_darken),
|
||||
Tool::CloneStamp => reset!(clone_aligned, clone_sample),
|
||||
Tool::PatternStamp => reset!(pattern_stamp_aligned, pattern_stamp_impressionist, pattern_stamp_scale, pattern_stamp_angle),
|
||||
@@ -173,12 +199,9 @@ pub fn reset_options(o: &mut ToolOptions, tool: Tool) {
|
||||
| Tool::Ruler
|
||||
| Tool::Note
|
||||
| Tool::Count
|
||||
| Tool::Brush
|
||||
| Tool::MixerBrush
|
||||
| Tool::Eraser
|
||||
| Tool::Hand
|
||||
| Tool::RotateView
|
||||
| Tool::Remove
|
||||
| Tool::HistoryBrush
|
||||
| Tool::PathSelection
|
||||
| Tool::DirectSelection
|
||||
@@ -188,7 +211,7 @@ pub fn reset_options(o: &mut ToolOptions, tool: Tool) {
|
||||
| Tool::DeleteAnchorPoint
|
||||
| Tool::ConvertPoint
|
||||
| Tool::Artboard
|
||||
| Tool::Frame => {}
|
||||
| Tool::Frame => &[],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,6 +225,21 @@ mod tests {
|
||||
PhotocraftApp::new(s, crate::Services::default())
|
||||
}
|
||||
|
||||
/// Every option belongs to a tool, so Reset Tool and tool presets (#7356) miss none, and every
|
||||
/// listed name is a real serialized field.
|
||||
#[test]
|
||||
fn every_option_belongs_to_a_tool() {
|
||||
let all = serde_json::to_value(ToolOptions::default()).unwrap();
|
||||
let all = all.as_object().unwrap();
|
||||
let listed: std::collections::BTreeSet<&str> = Tool::ALL.iter().flat_map(|t| option_keys(*t).iter().copied()).collect();
|
||||
for k in all.keys() {
|
||||
assert!(listed.contains(k.as_str()), "{k} is no tool's option");
|
||||
}
|
||||
for k in listed {
|
||||
assert!(all.contains_key(k), "{k} is not a ToolOptions field");
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset Tool resets only the current tool's options; Reset All Tools asks, then resets all.
|
||||
#[test]
|
||||
fn reset_tool_and_reset_all_tools() {
|
||||
|
||||
Reference in new issue
Block a user