From 609d953ea7a42f3cddde11d294c5d0d0d6050b20 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 19:21:03 -0800 Subject: [PATCH 1/3] Remember the Current Tool Only filter in Tool Presets --- crates/ui-egui/src/panels/preset_browser.rs | 8 +- crates/ui-egui/src/panels/preset_panels.rs | 28 ++++++- .../tool_presets_current_tool_only_tests.rs | 77 +++++++++++++++++++ .../src/panels/tool_presets_row_icon_tests.rs | 2 + 4 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 crates/ui-egui/src/panels/tool_presets_current_tool_only_tests.rs diff --git a/crates/ui-egui/src/panels/preset_browser.rs b/crates/ui-egui/src/panels/preset_browser.rs index 0d8fd3e08c2..c2495fe0a05 100644 --- a/crates/ui-egui/src/panels/preset_browser.rs +++ b/crates/ui-egui/src/panels/preset_browser.rs @@ -31,7 +31,7 @@ use serde_json::{Value, json}; use std::hash::{DefaultHasher, Hash as _, Hasher as _}; use std::sync::Arc; -use super::preset_panels::{PresetUi, run, tool_presets}; +use super::preset_panels::{PresetUi, current_tool_only, remember_current_tool_only, run, tool_presets}; use crate::PhotocraftApp; use crate::state::Tool; @@ -798,7 +798,7 @@ pub fn finish_custom_shape(app: &mut PhotocraftApp, rect: [f64; 4], keep: bool, /// Window › Tool Presets: a click applies the preset's tool and options. Current Tool Only lists the current tool's /// presets. pub fn tool_presets_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { - let only = app.ui.presets_ui.current_tool_only; + let only = current_tool_only(app); let current = app.ui.tool; let presets: Vec<(String, String)> = app .session @@ -820,12 +820,12 @@ pub fn tool_presets_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let look = Look { list: true, flat: true, - empty: Some(if only { tl!("No presets for the current tool.") } else { tl!("No tool presets.") }), + empty: Some(if only { tl!("No tool presets are defined for the current tool.") } else { tl!("No tool presets.") }), leading: Some(Box::new(leading)), ..Look::default() }; let (events, keys) = show(&mut app.ui.presets_ui, zone, ui, TOOL_PRESETS, &source, look); - app.ui.presets_ui.current_tool_only = flag.get(); + remember_current_tool_only(app, flag.get()); apply(app, ui.ctx(), TOOL_PRESETS, &[], &keys, events); } diff --git a/crates/ui-egui/src/panels/preset_panels.rs b/crates/ui-egui/src/panels/preset_panels.rs index fa41b34ae35..957afb08fbc 100644 --- a/crates/ui-egui/src/panels/preset_panels.rs +++ b/crates/ui-egui/src/panels/preset_panels.rs @@ -33,8 +33,6 @@ pub struct PresetUi { pub clone_cursor: Option<(photocraft_doc::DocId, [f64; 2])>, /// Selected preset per panel (by name / pattern id). pub selected: BTreeMap, - /// Tool Presets: Current Tool Only. - pub current_tool_only: bool, /// Custom Shape tool: the shape it draws. pub custom_shape: String, /// Custom Shape tool: the layer its last drag placed, in that document; with that tool active the @@ -117,6 +115,28 @@ impl PresetUi { } } +const TOOL_PRESETS_SECTION: &str = "photocraft.tool-presets"; + +/// Tool Presets starts filtered unless the user has explicitly changed it. +pub(super) fn current_tool_only(app: &PhotocraftApp) -> bool { + app.session + .prefs() + .sections + .get(TOOL_PRESETS_SECTION) + .and_then(|section| section.get("currentToolOnly")) + .and_then(Value::as_bool) + .unwrap_or(true) +} + +/// Save a changed filter and invalidate the dock body that reads it. +pub(super) fn remember_current_tool_only(app: &mut PhotocraftApp, on: bool) { + if current_tool_only(app) != on { + app.session.prefs.edit(|prefs| { + prefs.sections.insert(TOOL_PRESETS_SECTION.into(), serde_json::json!({"currentToolOnly": on})); + }); + } +} + /// Runs the engine command `id`; a failure goes to the status line. pub(crate) fn run(app: &mut PhotocraftApp, id: &str, p: Value) -> Option { match app.run(id, p) { @@ -129,6 +149,10 @@ pub(crate) fn run(app: &mut PhotocraftApp, id: &str, p: Value) -> Option } } +#[cfg(all(test, not(target_arch = "wasm32")))] +#[path = "tool_presets_current_tool_only_tests.rs"] +mod tool_presets_current_tool_only_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/crates/ui-egui/src/panels/tool_presets_current_tool_only_tests.rs b/crates/ui-egui/src/panels/tool_presets_current_tool_only_tests.rs new file mode 100644 index 00000000000..4af7272d17a --- /dev/null +++ b/crates/ui-egui/src/panels/tool_presets_current_tool_only_tests.rs @@ -0,0 +1,77 @@ +//! The Tool Presets panel filters a fresh profile and keeps a user's choice across restarts. + +use egui::accesskit::{Role, Toggled}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use photocraft_engine::presets::{PresetsExt, tools::ToolPreset}; +use serde_json::json; + +use crate::input::shortcut_dispatch::tests::app_harness; +use crate::{PhotocraftApp, Services, Tool}; + +const FILTER: &str = "Current Tool Only"; +const BRUSH: &str = "Filter fixture brush"; +const CROP: &str = "Filter fixture crop"; +const EMPTY: &str = "No tool presets are defined for the current tool."; + +type H = Harness<'static, Box>; + +fn open_panel(services: Services) -> H { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.ui.tool = Tool::Brush; + app.session.presets_mut().tool_presets = + [(BRUSH, "brush"), (CROP, "crop")].map(|(name, tool)| ToolPreset { name: name.into(), tool: tool.into(), options: json!({}) }).into(); + let mut h = app_harness(app); + let ctx = h.ctx.clone(); + crate::menus::invoke(h.state_mut(), &ctx, "window.panel.toolPresets", json!({"show": true})).unwrap(); + crate::menus::invoke(h.state_mut(), &ctx, "window.panel.window", json!({"panel": "toolPresets", "native": false})).unwrap(); + h.run_steps(6); + h +} + +fn filter_state(h: &H) -> Option { + h.get_by_role_and_label(Role::CheckBox, FILTER).accesskit_node().toggled() +} + +#[test] +fn tool_presets_filter_a_fresh_profile_and_explain_an_empty_current_tool() { + let mut h = open_panel(Services::default()); + assert_eq!(filter_state(&h), Some(Toggled::True)); + assert!(h.query_by_label_contains(BRUSH).is_some()); + assert!(h.query_by_label_contains(CROP).is_none()); + assert!(h.query_by_label(EMPTY).is_none()); + + h.state_mut().ui.tool = Tool::RectMarquee; + h.run_steps(4); + assert_eq!(filter_state(&h), Some(Toggled::True)); + assert!(h.query_by_label_contains(BRUSH).is_none()); + assert!(h.query_by_label_contains(CROP).is_none()); + assert!(h.query_by_label(EMPTY).is_some()); +} + +#[test] +fn tool_presets_keep_the_unchecked_filter_after_a_restart() { + use std::sync::Arc; + + let files = Arc::new(craft::settings::testing::MemoryFiles::new()); + let dir = std::env::temp_dir().join("photocraft-tool-presets-current-tool-only"); + let launch = || { + let options = photocraft_engine::prefs::store_options(Some(&dir)).files(files.clone()); + open_panel(Services { settings: Some(craft::settings::SettingsStore::open_with(options)), ..Default::default() }) + }; + let mut h = launch(); + h.get_by_role_and_label(Role::CheckBox, FILTER).click(); + h.run_steps(4); + assert_eq!(filter_state(&h), Some(Toggled::False)); + assert!(h.query_by_label_contains(BRUSH).is_some()); + assert!(h.query_by_label_contains(CROP).is_some()); + + crate::prefs_ui::save_preferences(h.state_mut()).unwrap(); + drop(h); + + let restarted = launch(); + assert_eq!(filter_state(&restarted), Some(Toggled::False)); + assert!(restarted.query_by_label_contains(BRUSH).is_some()); + assert!(restarted.query_by_label_contains(CROP).is_some()); +} diff --git a/crates/ui-egui/src/panels/tool_presets_row_icon_tests.rs b/crates/ui-egui/src/panels/tool_presets_row_icon_tests.rs index 3ac8d99c57b..1fde5a64680 100644 --- a/crates/ui-egui/src/panels/tool_presets_row_icon_tests.rs +++ b/crates/ui-egui/src/panels/tool_presets_row_icon_tests.rs @@ -30,6 +30,8 @@ fn tool_preset_rows_paint_distinct_tool_glyphs_and_a_fallback() { crate::menus::invoke(h.state_mut(), &ctx, "window.panel.toolPresets", json!({})).unwrap(); crate::menus::invoke(h.state_mut(), &ctx, "window.panel.window", json!({"panel": "toolPresets", "native": false})).unwrap(); h.run_steps(6); + h.get_by_role_and_label(egui::accesskit::Role::CheckBox, "Current Tool Only").click(); + h.run_steps(4); let textures = names.map(|name| { let row = h.query_by_label_contains(name).expect("the preset is exposed as a row in the panel"); From f69aa937415df41940ddabf9c90df5ee3ef5418a Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 19:28:23 -0800 Subject: [PATCH 2/3] Match hosted formatting in the Tool Presets preference helper --- crates/ui-egui/src/panels/preset_panels.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/ui-egui/src/panels/preset_panels.rs b/crates/ui-egui/src/panels/preset_panels.rs index 957afb08fbc..6ea75072e9b 100644 --- a/crates/ui-egui/src/panels/preset_panels.rs +++ b/crates/ui-egui/src/panels/preset_panels.rs @@ -119,13 +119,7 @@ const TOOL_PRESETS_SECTION: &str = "photocraft.tool-presets"; /// Tool Presets starts filtered unless the user has explicitly changed it. pub(super) fn current_tool_only(app: &PhotocraftApp) -> bool { - app.session - .prefs() - .sections - .get(TOOL_PRESETS_SECTION) - .and_then(|section| section.get("currentToolOnly")) - .and_then(Value::as_bool) - .unwrap_or(true) + app.session.prefs().sections.get(TOOL_PRESETS_SECTION).and_then(|section| section.get("currentToolOnly")).and_then(Value::as_bool).unwrap_or(true) } /// Save a changed filter and invalidate the dock body that reads it. From fc1352841e805a9db970840833d31930e1bc9a11 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 19:41:02 -0800 Subject: [PATCH 3/3] Propagate Tool Presets fixture errors through the tests --- .../tool_presets_current_tool_only_tests.rs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/panels/tool_presets_current_tool_only_tests.rs b/crates/ui-egui/src/panels/tool_presets_current_tool_only_tests.rs index 4af7272d17a..4b36c76e9b4 100644 --- a/crates/ui-egui/src/panels/tool_presets_current_tool_only_tests.rs +++ b/crates/ui-egui/src/panels/tool_presets_current_tool_only_tests.rs @@ -16,18 +16,18 @@ const EMPTY: &str = "No tool presets are defined for the current tool."; type H = Harness<'static, Box>; -fn open_panel(services: Services) -> H { +fn open_panel(services: Services) -> Result { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services); - app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.run("file.new", json!({"width": 64, "height": 48}))?; app.ui.tool = Tool::Brush; app.session.presets_mut().tool_presets = [(BRUSH, "brush"), (CROP, "crop")].map(|(name, tool)| ToolPreset { name: name.into(), tool: tool.into(), options: json!({}) }).into(); let mut h = app_harness(app); let ctx = h.ctx.clone(); - crate::menus::invoke(h.state_mut(), &ctx, "window.panel.toolPresets", json!({"show": true})).unwrap(); - crate::menus::invoke(h.state_mut(), &ctx, "window.panel.window", json!({"panel": "toolPresets", "native": false})).unwrap(); + crate::menus::invoke(h.state_mut(), &ctx, "window.panel.toolPresets", json!({"show": true}))?; + crate::menus::invoke(h.state_mut(), &ctx, "window.panel.window", json!({"panel": "toolPresets", "native": false}))?; h.run_steps(6); - h + Ok(h) } fn filter_state(h: &H) -> Option { @@ -35,8 +35,8 @@ fn filter_state(h: &H) -> Option { } #[test] -fn tool_presets_filter_a_fresh_profile_and_explain_an_empty_current_tool() { - let mut h = open_panel(Services::default()); +fn tool_presets_filter_a_fresh_profile_and_explain_an_empty_current_tool() -> Result<(), String> { + let mut h = open_panel(Services::default())?; assert_eq!(filter_state(&h), Some(Toggled::True)); assert!(h.query_by_label_contains(BRUSH).is_some()); assert!(h.query_by_label_contains(CROP).is_none()); @@ -48,10 +48,11 @@ fn tool_presets_filter_a_fresh_profile_and_explain_an_empty_current_tool() { assert!(h.query_by_label_contains(BRUSH).is_none()); assert!(h.query_by_label_contains(CROP).is_none()); assert!(h.query_by_label(EMPTY).is_some()); + Ok(()) } #[test] -fn tool_presets_keep_the_unchecked_filter_after_a_restart() { +fn tool_presets_keep_the_unchecked_filter_after_a_restart() -> Result<(), String> { use std::sync::Arc; let files = Arc::new(craft::settings::testing::MemoryFiles::new()); @@ -60,18 +61,19 @@ fn tool_presets_keep_the_unchecked_filter_after_a_restart() { let options = photocraft_engine::prefs::store_options(Some(&dir)).files(files.clone()); open_panel(Services { settings: Some(craft::settings::SettingsStore::open_with(options)), ..Default::default() }) }; - let mut h = launch(); + let mut h = launch()?; h.get_by_role_and_label(Role::CheckBox, FILTER).click(); h.run_steps(4); assert_eq!(filter_state(&h), Some(Toggled::False)); assert!(h.query_by_label_contains(BRUSH).is_some()); assert!(h.query_by_label_contains(CROP).is_some()); - crate::prefs_ui::save_preferences(h.state_mut()).unwrap(); + crate::prefs_ui::save_preferences(h.state_mut())?; drop(h); - let restarted = launch(); + let restarted = launch()?; assert_eq!(filter_state(&restarted), Some(Toggled::False)); assert!(restarted.query_by_label_contains(BRUSH).is_some()); assert!(restarted.query_by_label_contains(CROP).is_some()); + Ok(()) }