diff --git a/crates/ui-egui/src/canvas/art_history_shift_click_tests.rs b/crates/ui-egui/src/canvas/art_history_shift_click_tests.rs index 011e4a4cd2..22caf24343 100644 --- a/crates/ui-egui/src/canvas/art_history_shift_click_tests.rs +++ b/crates/ui-egui/src/canvas/art_history_shift_click_tests.rs @@ -105,7 +105,9 @@ fn shift_click_still_connects_brush_pencil_history_brush_and_other_painting_tool tool_event(&mut app, ToolEvent::Up { x: 20.0, y: 40.0 }, none); tool_event(&mut app, ToolEvent::Down { x: 120.0, y: 40.0, pressure: 1.0 }, shift); tool_event(&mut app, ToolEvent::Up { x: 120.0, y: 40.0 }, shift); - assert_eq!(last_points(&app, cmd), vec![[20.0, 40.0], [120.0, 40.0]], "{tool:?}"); + // The Pencil's 1 px tip paints whole pixels, so its points sit on pixel centres (#12200). + let want = if tool == Tool::Pencil { vec![[20.5, 40.5], [120.5, 40.5]] } else { vec![[20.0, 40.0], [120.0, 40.0]] }; + assert_eq!(last_points(&app, cmd), want, "{tool:?}"); assert!(red(&app, 70, 40) > 0.9, "{tool:?} painted the connecting line midpoint"); } } diff --git a/crates/ui-egui/src/canvas/paint_mouse.rs b/crates/ui-egui/src/canvas/paint_mouse.rs index b85c146e23..da1ed4a635 100644 --- a/crates/ui-egui/src/canvas/paint_mouse.rs +++ b/crates/ui-egui/src/canvas/paint_mouse.rs @@ -47,6 +47,9 @@ pub fn sync_tool_brush(app: &mut PhotocraftApp) { None if matches!(tool, Tool::BackgroundEraser | Tool::Healing | Tool::Burn | Tool::Dodge | Tool::Sponge | Tool::CloneStamp | Tool::Smudge) => { fresh_brush(tool, ¤t) } + // The Pencil starts with its own 1 px hard round (#12200) regardless of the Brush, keeping + // the outgoing opacity. + None if tool == Tool::Pencil => BrushSettings { opacity: current.opacity, ..fresh_brush(tool, ¤t) }, None => { let opacity = if tool == Tool::ArtHistoryBrush { BrushSettings::default().opacity } else { current.opacity }; // The Remove tool starts at its measured 19 px; the rest is the current brush (#12548). @@ -82,6 +85,9 @@ const BRUSH_TOOL_TIP: (f32, f32, f32) = (13.0, 0.0, 0.25); const BACKGROUND_ERASER_TIP: (f32, f32, f32) = (13.0, 1.0, 0.25); /// The Healing Brush's tip after Reset Tool (#12099): a 19 px hard round at 25 % spacing. const HEALING_BRUSH_TIP: (f32, f32, f32) = (19.0, 1.0, 0.25); +/// The Pencil tool's starting and Reset Tool tip, as the reference measures it (#12200): size, +/// hardness and spacing of a 1 px hard round at 25 % spacing. +const PENCIL_TOOL_TIP: (f32, f32, f32) = (1.0, 1.0, 0.25); /// The Burn, Dodge and Sponge tools' tip after Reset Tool (#12671, #12684, #13029): a 65 px soft /// round at 25 % spacing. const TONING_TOOL_TIP: (f32, f32, f32) = (65.0, 0.0, 0.25); @@ -98,13 +104,15 @@ const SMUDGE_TOOL_TIP: (f32, f32, f32) = (13.0, 0.0, 0.25); /// [`BRUSH_TOOL_TIP`], the Background Eraser's [`BACKGROUND_ERASER_TIP`], the Healing Brush's /// [`HEALING_BRUSH_TIP`], the Burn, Dodge and Sponge tools' [`TONING_TOOL_TIP`], the Clone Stamp's /// [`CLONE_STAMP_TIP`] with no smoothing, the Remove tool's size [`REMOVE_TOOL_SIZE`], the Smudge -/// tool's [`SMUDGE_TOOL_TIP`]; the other tools keep the default tip. +/// tool's [`SMUDGE_TOOL_TIP`] and the Pencil's [`PENCIL_TOOL_TIP`]; the other tools keep the +/// default tip. pub(crate) fn fresh_brush(tool: Tool, current: &BrushSettings) -> BrushSettings { let mut brush = BrushSettings { smoothing: first_smoothing(), ..BrushSettings::default() }; match tool { Tool::Brush => (brush.size, brush.hardness, brush.spacing) = BRUSH_TOOL_TIP, Tool::BackgroundEraser => (brush.size, brush.hardness, brush.spacing) = BACKGROUND_ERASER_TIP, Tool::Healing => (brush.size, brush.hardness, brush.spacing) = HEALING_BRUSH_TIP, + Tool::Pencil => (brush.size, brush.hardness, brush.spacing) = PENCIL_TOOL_TIP, Tool::Burn | Tool::Dodge | Tool::Sponge => (brush.size, brush.hardness, brush.spacing) = TONING_TOOL_TIP, Tool::Smudge => (brush.size, brush.hardness, brush.spacing) = SMUDGE_TOOL_TIP, Tool::CloneStamp => { @@ -568,3 +576,7 @@ pub fn show_picker(app: &mut PhotocraftApp, ctx: &egui::Context) { #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "../pencil_reset_defaults_tests.rs"] +mod pencil_reset_defaults_tests; diff --git a/crates/ui-egui/src/control/dispatch.rs b/crates/ui-egui/src/control/dispatch.rs index d4e90ff90c..2aa71a6915 100644 --- a/crates/ui-egui/src/control/dispatch.rs +++ b/crates/ui-egui/src/control/dispatch.rs @@ -1273,6 +1273,9 @@ fn pointer(app: &mut PhotocraftApp, ctx: &egui::Context, p: &Value) -> Outcome { }; if let Some(t) = parsed.tool { crate::tool_memory::select(app, t); + // The tool's own brush now, not at the next frame, so a paint command right after uses + // it (the Pencil's 1 px tip, #12200; any tool's remembered brush). + crate::paint_mouse::sync_tool_brush(app); } // Space held: the Crop tool moves the frame being drawn, a marquee, lasso or shape // being drawn moves instead of growing (hold_keys.rs). Like a modifier flag, it may be diff --git a/crates/ui-egui/src/input/hold_keys/tests.rs b/crates/ui-egui/src/input/hold_keys/tests.rs index 3fc3e2e030..109405eb72 100644 --- a/crates/ui-egui/src/input/hold_keys/tests.rs +++ b/crates/ui-egui/src/input/hold_keys/tests.rs @@ -782,7 +782,14 @@ fn tilde_held_with_the_brush_or_pencil_erases_with_its_brush() { drag_with(&mut h, Modifiers::NONE, 110.0, 130.0, 150.0, 130.0); key(&mut h, Key::Backtick, false, Modifiers::NONE); assert_eq!(last_stroke_erases(&h), Some(true), "{tool:?}"); - assert_eq!(rgba(&h, 130, 130)[3], 0.0, "{tool:?}: erased to transparency"); + // `drag_with` bends through `(118, 136)` on the way from `(110, 130)` to `(150, 130)`, + // so the Pencil's 1 px tip (#12200) no longer reaches `(130, 130)`; check `(110, 130)` + // at the stroke start instead. + assert_eq!(rgba(&h, 110, 130)[3], 0.0, "{tool:?}: erased to transparency"); + // The whole drag erases, not only its first dab: a 1 px Pencil line along the 40 px drag + // clears far more pixels than one dab. + let cleared = (100..160).flat_map(|x| (120..146).map(move |y| (x, y))).filter(|&(x, y)| rgba(&h, x, y)[3] == 0.0).count(); + assert!(cleared >= 20, "{tool:?}: erased along the drag, {cleared} pixels"); assert_eq!(last_stroke(h.state())["mode"], json!("clear"), "{tool:?}"); assert_eq!(h.state().session.active().unwrap().history.undo_label(), Some(label)); assert_eq!(h.state().ui.tool, tool, "the tool stays"); diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 43e2214504..3471e4404e 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -106,6 +106,9 @@ fn frame_convert_dialog(app: &mut PhotocraftApp, id: &str) -> Result Option> { if let Some(tool) = crate::tool_shortcuts::tool(id) { crate::tool_memory::select(app, tool); + // The tool's own brush now, not at the next frame, so a paint command right after uses + // it: a first Pencil pick its 1 px tip (#12200), any other tool its remembered brush. + crate::paint_mouse::sync_tool_brush(app); return Some(Ok(json!({"tool": tool}))); } if id == "layer.frame.convert" && params.as_object().is_none_or(|o| o.is_empty()) { diff --git a/crates/ui-egui/src/pencil_reset_defaults_tests.rs b/crates/ui-egui/src/pencil_reset_defaults_tests.rs new file mode 100644 index 0000000000..37ebd78b4e --- /dev/null +++ b/crates/ui-egui/src/pencil_reset_defaults_tests.rs @@ -0,0 +1,154 @@ +//! #12200: Reset Tool, Reset All Tools and a fresh first pick give the Pencil a 1 px hard round +//! tip (size 1, hardness 100 %, angle 0, roundness 100 %, spacing 25 %, both pressure overrides +//! off, options-bar chip "1"), and a default click at 100.2,100.2 paints only pixel 100,100. + +use egui::{Context, Modifiers, Pos2, RawInput, Rect, Shape, vec2}; +use serde_json::{Value, json}; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::control::{ControlRequest, handle}; +use crate::state::Tool; + +/// Creates a test app with a 200x200 document. +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.sync_views(); + app +} + +/// Dispatches a control request and syncs document views. +fn control(app: &mut PhotocraftApp, method: &str, params: Value) { + let ctx = Context::default(); + let (req, _rx) = ControlRequest::new(method, params); + let _ = handle(app, &ctx, &req); + app.sync_views(); +} + +/// Switches to `tool` and synchronizes its remembered brush. +fn pick(app: &mut PhotocraftApp, tool: Tool) { + app.ui.tool = tool; + crate::paint_mouse::sync_tool_brush(app); + app.sync_views(); +} + +/// Scrambles the active brush tip and pressure overrides away from defaults. +fn scramble(app: &mut PhotocraftApp) { + let b = &mut app.session.tools.brush; + (b.size, b.hardness, b.spacing, b.angle, b.roundness) = (42.0, 0.3, 0.6, 25.0, 0.5); + (b.pressure_size, b.pressure_opacity) = (true, true); +} + +/// Asserts that `brush.get` reports the Pencil's 1 px hard round default tip. +fn assert_pencil_tip(app: &mut PhotocraftApp) { + let b = app.run("brush.get", json!({})).unwrap(); + let tip = ["size", "hardness", "angle", "roundness", "spacing"].map(|k| (b[k].as_f64().unwrap() * 1e4).round() / 1e4); + assert_eq!(tip, [1.0, 1.0, 0.0, 1.0, 0.25], "{b}"); + assert_eq!(b["pressureSize"], json!(false), "{b}"); + assert_eq!(b["pressureOpacity"], json!(false), "{b}"); +} + +/// Renders the options bar and returns whether the brush preset chip displays `"1"`. +fn chip_shows_one(app: &mut PhotocraftApp) -> bool { + fn has_one(shape: &Shape, rect: Rect) -> bool { + match shape { + Shape::Text(t) => rect.contains(t.pos) && t.galley.text() == "1", + Shape::Vec(v) => v.iter().any(|s| has_one(s, rect)), + _ => false, + } + } + let ctx = Context::default(); + PhotocraftApp::setup_context(&ctx, Default::default()); + let screen_rect = Some(Rect::from_min_size(Pos2::ZERO, vec2(1400.0, 300.0))); + let mut out = ctx.run_ui(RawInput { screen_rect, ..Default::default() }, |ui| crate::panels::options_bar(app, ui)); + out.textures_delta.clear(); + let chip = ctx.read_response(crate::brush_picker::chip_id()).unwrap().rect; + out.shapes.iter().any(|s| has_one(&s.shape, chip)) +} + +/// Verifies that `tool.reset` and `tool.resetAll` give the Pencil its 1 px hard tip and `"1"` chip. +#[test] +fn reset_tool_and_reset_all_give_the_pencil_a_one_pixel_hard_tip() { + let mut a = app(); + pick(&mut a, Tool::Pencil); + scramble(&mut a); + a.session.tools.brush.opacity = 0.4; + control(&mut a, "ui.menu.invoke", json!({"id": "tool.reset"})); + assert_pencil_tip(&mut a); + assert_eq!(a.session.tools.brush.opacity, 1.0); + assert!(chip_shows_one(&mut a)); + + scramble(&mut a); + control(&mut a, "ui.menu.invoke", json!({"id": "tool.resetAll", "params": {"confirmed": true}})); + assert_pencil_tip(&mut a); + assert!(chip_shows_one(&mut a)); +} + +/// Verifies that first selecting the Pencil (including via `tools.select.pencil` before a UI pass) +/// uses its 1 px hard tip while preserving the outgoing brush opacity. +#[test] +fn pencil_starts_with_its_own_tip_whatever_the_brush_had() { + let mut a = app(); + pick(&mut a, Tool::Brush); + scramble(&mut a); + a.session.tools.brush.opacity = 0.4; + control(&mut a, "ui.menu.invoke", json!({"id": "tools.select.pencil"})); + assert_pencil_tip(&mut a); + assert!((a.session.tools.brush.opacity - 0.4).abs() < 1e-5); + assert!(chip_shows_one(&mut a)); +} + +/// Selecting the Brush again by command, before a UI pass, gives back the Brush's own tip: the +/// Pencil's 1 px tip is not left for the next brush command, and the Brush's edit is not saved to +/// the Pencil. +#[test] +fn selecting_back_to_the_brush_by_command_restores_its_tip_at_once() { + let mut a = app(); + pick(&mut a, Tool::Brush); + a.session.tools.brush.size = 42.0; + control(&mut a, "ui.menu.invoke", json!({"id": "tools.select.pencil"})); + assert_pencil_tip(&mut a); + control(&mut a, "ui.menu.invoke", json!({"id": "tools.select.brush"})); + assert_eq!(a.session.tools.brush.size, 42.0, "the Brush's own size, before any frame"); +} + +/// Verifies that a default Pencil click at `(100.2, 100.2)` paints only pixel `(100, 100)`. +#[test] +fn a_default_click_at_100_2_paints_only_pixel_100_100() { + let mut a = app(); + a.run("layer.new.layer", json!({})).unwrap(); + pick(&mut a, Tool::Pencil); + tool_event(&mut a, ToolEvent::Down { x: 100.2, y: 100.2, pressure: 1.0 }, Modifiers::NONE); + tool_event(&mut a, ToolEvent::Up { x: 100.2, y: 100.2 }, Modifiers::NONE); + let st = a.session.active().unwrap(); + let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + assert_eq!(surf.content_bounds(), photocraft_geom::Rect::new(100, 100, 101, 101)); + assert_eq!(surf.rgba(100, 100)[3], 1.0); +} + +/// Verifies that `tool.resetAll` resets a saved Pencil brush even when invoked from another tool, +/// while preserving the Brush and Eraser reset tips. +#[test] +fn brush_and_other_painting_tools_keep_their_reset_tips() { + let mut a = app(); + pick(&mut a, Tool::Eraser); + scramble(&mut a); + pick(&mut a, Tool::Pencil); + scramble(&mut a); + pick(&mut a, Tool::Brush); + scramble(&mut a); + control(&mut a, "ui.menu.invoke", json!({"id": "tool.resetAll", "params": {"confirmed": true}})); + let b = a.run("brush.get", json!({})).unwrap(); + let brush_tip = ["size", "hardness", "spacing"].map(|k| (b[k].as_f64().unwrap() * 1e4).round() / 1e4); + assert_eq!(brush_tip, [13.0, 0.0, 0.25]); + + pick(&mut a, Tool::Pencil); + assert_pencil_tip(&mut a); + assert!(chip_shows_one(&mut a)); + + pick(&mut a, Tool::Eraser); + let e = a.run("brush.get", json!({})).unwrap(); + let eraser_tip = ["size", "hardness", "spacing"].map(|k| (e[k].as_f64().unwrap() * 1e4).round() / 1e4); + assert_eq!(eraser_tip, [20.0, 1.0, 0.1]); +} diff --git a/crates/ui-egui/src/pencil_tests.rs b/crates/ui-egui/src/pencil_tests.rs index 3223128a17..25ca0a1887 100644 --- a/crates/ui-egui/src/pencil_tests.rs +++ b/crates/ui-egui/src/pencil_tests.rs @@ -13,12 +13,11 @@ fn app() -> PhotocraftApp { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); app.run("file.new", json!({"width": 120, "height": 80, "background": "transparent"})).unwrap(); app.run("tools.setColors", json!({"foreground": "#000000", "background": "#ffffff"})).unwrap(); - app.run("tools.setBrush", json!({"brush": {"size": 3, "hardness": 0.0}})).unwrap(); // Each tool keeps its own brush (#218): switch first, then set the Pencil's options, as the - // options bar does (its first frame takes the Brush's tip at Photoshop's 10 % smoothing). + // options bar does. app.ui.tool = Tool::Pencil; crate::paint_mouse::sync_tool_brush(&mut app); - app.run("tools.setBrush", json!({"brush": {"smoothing": {"amount": 0.0}}})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 3, "hardness": 0.0, "smoothing": {"amount": 0.0}}})).unwrap(); app } diff --git a/crates/ui-egui/src/tools/remove_default_size_tests.rs b/crates/ui-egui/src/tools/remove_default_size_tests.rs index ddc5af2bc0..5bcd0b5dc0 100644 --- a/crates/ui-egui/src/tools/remove_default_size_tests.rs +++ b/crates/ui-egui/src/tools/remove_default_size_tests.rs @@ -66,10 +66,11 @@ fn reset_all_tools_gives_it_19_px_active_or_picked_later() { assert_eq!(tip(&mut app), REMOVE_TIP, "picked after the reset"); } +/// The Pencil resets to its 1 px hard round (#12200). #[test] fn other_tools_keep_their_reset_sizes() { for (tool, want) in - [(Tool::Brush, 13.0), (Tool::Pencil, 20.0), (Tool::Eraser, 20.0), (Tool::SpotHealing, 20.0), (Tool::Healing, 19.0), (Tool::CloneStamp, 21.0)] + [(Tool::Brush, 13.0), (Tool::Pencil, 1.0), (Tool::Eraser, 20.0), (Tool::SpotHealing, 20.0), (Tool::Healing, 19.0), (Tool::CloneStamp, 21.0)] { let mut a = app(); pick(&mut a, tool); diff --git a/crates/ui-egui/tests/it/background_eraser_reset_tip.rs b/crates/ui-egui/tests/it/background_eraser_reset_tip.rs index f971412bc4..c47e7ce053 100644 --- a/crates/ui-egui/tests/it/background_eraser_reset_tip.rs +++ b/crates/ui-egui/tests/it/background_eraser_reset_tip.rs @@ -96,7 +96,7 @@ fn a_first_use_starts_with_the_tip() { #[test] fn other_tools_and_session_strokes_keep_their_tips() { - for tool in [Tool::Pencil, Tool::Eraser] { + for tool in [Tool::Eraser, Tool::Blur] { let mut a = app(); pick(&mut a, tool); scramble(&mut a); diff --git a/crates/ui-egui/tests/it/brush_reset_defaults.rs b/crates/ui-egui/tests/it/brush_reset_defaults.rs index 0c596a2102..745de07950 100644 --- a/crates/ui-egui/tests/it/brush_reset_defaults.rs +++ b/crates/ui-egui/tests/it/brush_reset_defaults.rs @@ -85,7 +85,7 @@ fn reset_all_tools_from_the_brush_gives_the_other_tools_the_default_tip() { let mut app = app(); pick(&mut app, Tool::Brush); control(&mut app, "ui.menu.invoke", json!({"id": "tool.resetAll", "params": {"confirmed": true}})); - for tool in [Tool::Pencil, Tool::Eraser] { + for tool in [Tool::Eraser, Tool::Blur] { pick(&mut app, tool); assert_eq!(tip(&mut app), DEFAULT_TIP, "{tool:?}"); } @@ -93,7 +93,7 @@ fn reset_all_tools_from_the_brush_gives_the_other_tools_the_default_tip() { #[test] fn other_painting_tools_keep_the_default_reset_tip() { - for tool in [Tool::Pencil, Tool::Eraser] { + for tool in [Tool::Eraser, Tool::Blur] { let mut a = app(); pick(&mut a, tool); scramble(&mut a);