Merge pull request #18925

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4andautocraft-bot[bot] authored and autocraft-bot[bot] committed 2026-12-02 08:06:59 +00:00
commit c15359ba16
4 files changed
+105 -6

No files matched your search

+4 -4
View File
@@ -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);
}
+20 -2
View File
@@ -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<String, String>,
/// 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,22 @@ 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<Value> {
match app.run(id, p) {
@@ -129,6 +143,10 @@ pub(crate) fn run(app: &mut PhotocraftApp, id: &str, p: Value) -> Option<Value>
}
}
#[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::*;
@@ -0,0 +1,79 @@
//! 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<PhotocraftApp>>;
fn open_panel(services: Services) -> Result<H, String> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services);
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}))?;
crate::menus::invoke(h.state_mut(), &ctx, "window.panel.window", json!({"panel": "toolPresets", "native": false}))?;
h.run_steps(6);
Ok(h)
}
fn filter_state(h: &H) -> Option<Toggled> {
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() -> 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());
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());
Ok(())
}
#[test]
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());
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())?;
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());
Ok(())
}
@@ -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");