PhotoCraft: An open-source, photo editor written by scratch in pure Rust
feat(ui): the brush HUD honors Vary Round Brush Hardness and Brush Preview Color (#204) (#2640)
Reynold Hufa2fe1c90fa7
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rsindex 2df7e853b..9ffc0fc28 100644--- a/crates/engine/src/prefs.rs+++ b/crates/engine/src/prefs.rs@@ -990,7 +990,6 @@ pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &["workspace.enableFloatingDocumentWindowDocking","workspace.enableNarrowOptionsBar","tools.enableFlickPanning",- "tools.varyRoundBrushHardnessOnHud","tools.showTransformationValues","fileHandling.imagePreviews","fileHandling.saveInBackground",@@ -1000,7 +999,6 @@ pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &["performance.effectCacheMb","performance.legacyCompositing","scratchDisks.disks",- "cursors.brushPreviewColor","unitsAndRulers.columnWidth","unitsAndRulers.gutter","unitsAndRulers.printResolution",diff --git a/crates/ui-egui/src/brush_resize.rs b/crates/ui-egui/src/brush_resize.rsindex 7e73adc63..c4239a358 100644--- a/crates/ui-egui/src/brush_resize.rs+++ b/crates/ui-egui/src/brush_resize.rs@@ -29,7 +29,8 @@ const SIZE: std::ops::RangeInclusive<f32> = 1.0..=MAX_BRUSH_SIZE;pub struct Resize {/// Where the drag began, in document pixels: the circle stays there.pub anchor: [f64; 2],- /// The brush's diameter and hardness when it began.+ /// The brush's diameter and vertical value when it began (its hardness, or its opacity with+ /// "Vary Round Brush Hardness on HUD" off).pub start: (f32, f32),/// The `coalesce` key of this drag's `tools.setBrush` calls.key: u64,@@ -76,8 +77,12 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers, arif app.drag.is_some() || !applies(app.ui.tool) || !(armed || is_gesture(mods)) {return false;}- let b = &app.session.tools.brush;- app.brush_resize = Some(Resize { anchor: [x, y], start: (b.size, b.hardness), key: KEY.fetch_add(1, Ordering::Relaxed), secondary: armed });+ let vary = app.session.prefs().tools.vary_round_brush_hardness_on_hud;+ let (size, v) = {+ let b = &app.session.tools.brush;+ (b.size, if vary { b.hardness } else { b.opacity })+ };+ app.brush_resize = Some(Resize { anchor: [x, y], start: (size, v), key: KEY.fetch_add(1, Ordering::Relaxed), secondary: armed });true}ToolEvent::Move { x, y, .. } => match app.brush_resize {@@ -108,41 +113,53 @@ pub fn release_stale(app: &mut PhotocraftApp, secondary_down: bool) {fn update(app: &mut PhotocraftApp, r: Resize, x: f64, y: f64) {// Right is bigger on screen, also in a flipped view (View › Flip Horizontal).let dx = (x - r.anchor[0]) * if app.ui.view.flip_horizontal { -1.0 } else { 1.0 };- let (size, hardness) = resized(r.start, dx, y - r.anchor[1], app.point_zoom());+ let (size, v) = resized(r.start, dx, y - r.anchor[1], app.point_zoom());+ // "Vary Round Brush Hardness on HUD" (Preferences ▸ Tools, on by default): off, the vertical+ // movement takes the opacity instead of the hardness, as in Photoshop.+ let vary = app.session.prefs().tools.vary_round_brush_hardness_on_hud;let b = &app.session.tools.brush;- if b.size == size && b.hardness == hardness {+ if b.size == size && if vary { b.hardness == v } else { b.opacity == v } {return;}- let p = json!({ "brush": { "size": size, "hardness": hardness }, "coalesce": format!("brush-resize:{}", r.key) });+ let brush = if vary { json!({ "size": size, "hardness": v }) } else { json!({ "size": size, "opacity": v }) };+ let p = json!({ "brush": brush, "coalesce": format!("brush-resize:{}", r.key) });if let Err(e) = app.run("tools.setBrush", p) {app.ui.status = e;}}-/// Draw the resize feedback: the brush circle at the anchor, filled with a red tip preview whose-/// solid core shows the hardness (Photoshop's), and the readout beside the pointer. Returns true-/// while a resize is in progress (the canvas then hides its cursor).+/// Draw the resize feedback: the brush circle at the anchor, filled with the brush preview+/// colour whose solid core shows the hardness (Photoshop's), and the readout beside the pointer.+/// Returns true while a resize is in progress (the canvas then hides its cursor).pub fn draw(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) -> bool {let Some(r) = app.brush_resize else { return false };let b = &app.session.tools.brush;let c = xf.to_screen(r.anchor[0] as f32, r.anchor[1] as f32);let radius = (b.size / 2.0 * xf.zoom).max(1.0);- tip_preview(painter, c, radius, b.hardness, Color32::from_rgba_unmultiplied(255, 0, 0, 110));+ tip_preview(painter, c, radius, b.hardness, preview_tint(app));painter.circle_stroke(c, radius + 0.5, Stroke::new(1.0, Color32::from_black_alpha(140)));painter.circle_stroke(c, radius, Stroke::new(1.0, Color32::from_white_alpha(220)));// Photoshop hides the pointer while the circle stays put.painter.ctx().set_cursor_icon(egui::CursorIcon::None);let at = painter.ctx().pointer_latest_pos().unwrap_or(c);- crate::canvas::draw_readout(- painter.ctx(),- "brush-resize-readout",- at,- [tl!("Diameter:"), tl!("Hardness:")],- [format!("{} px", b.size.round() as i64), format!("{}%", (b.hardness * 100.0).round() as i64)],- );+ let (label, value) = if app.session.prefs().tools.vary_round_brush_hardness_on_hud {+ (tl!("Hardness:"), format!("{}%", (b.hardness * 100.0).round() as i64))+ } else {+ (tl!("Opacity:"), format!("{}%", (b.opacity * 100.0).round() as i64))+ };+ crate::canvas::draw_readout(painter.ctx(), "brush-resize-readout", at, [tl!("Diameter:"), label], [format!("{} px", b.size.round() as i64), value]);true}+/// The tip preview's tint: Preferences ▸ Cursors ▸ Brush Preview colour (red by default, as in+/// Photoshop), at the overlay's alpha. Unreadable values keep the red.+fn preview_tint(app: &PhotocraftApp) -> Color32 {+ let alpha = 110;+ crate::color_picker_ui::parse_hex(&app.session.prefs().cursors.brush_preview_color).map_or(Color32::from_rgba_unmultiplied(255, 0, 0, alpha), |c| {+ Color32::from_rgba_unmultiplied((c[0] * 255.0).round() as u8, (c[1] * 255.0).round() as u8, (c[2] * 255.0).round() as u8, alpha)+ })+}+/// A round tip of `radius` screen points: solid out to `hardness` of the radius, fading to clear/// at the edge. One mesh (two rings and a centre), so its cost doesn't grow with the brush.fn tip_preview(painter: &egui::Painter, c: Pos2, radius: f32, hardness: f32, color: Color32) {@@ -441,4 +458,40 @@ mod tests {assert_eq!(a.session.tools.brush.size, 100.0);assert!(a.ui.brush_picker.is_some());}++ /// Preferences ▸ Tools ▸ Vary Round Brush Hardness on HUD (on by default): off, the HUD's+ /// vertical movement takes the brush opacity instead of its hardness.+ #[test]+ fn vary_round_brush_hardness_on_hud_switches_the_vertical_target() {+ for vary in [true, false] {+ let mut app = app(Tool::Brush);+ app.run("tools.setBrush", json!({"brush": {"opacity": 0.5}})).unwrap();+ if !vary {+ app.run("prefs.set", json!({"path": "tools.varyRoundBrushHardnessOnHud", "value": false})).unwrap();+ }+ let (h0, o0) = (app.session.tools.brush.hardness, app.session.tools.brush.opacity);+ tool_event(&mut app, ToolEvent::Down { x: 100.0, y: 100.0, pressure: 1.0 }, CTRL_ALT);+ tool_event(&mut app, ToolEvent::Move { x: 100.0, y: 140.0, pressure: 1.0 }, CTRL_ALT);+ tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 140.0 }, CTRL_ALT);+ let b = &app.session.tools.brush;+ if vary {+ assert_eq!((b.hardness, b.opacity), (h0 + 0.2, o0), "hardness moves with the drag");+ assert_eq!(app.session.journal.last().unwrap().1["brush"], json!({"size": 40.0f32, "hardness": 0.4f32}));+ } else {+ assert_eq!((b.hardness, b.opacity), (h0, o0 + 0.2), "opacity moves instead");+ assert_eq!(app.session.journal.last().unwrap().1["brush"], json!({"size": 40.0f32, "opacity": 0.7f32}));+ }+ }+ }++ /// Preferences ▸ Cursors ▸ Brush Preview colour tints the HUD's tip preview (red by default).+ #[test]+ fn the_tip_preview_uses_the_brush_preview_colour() {+ let mut app = app(Tool::Brush);+ assert_eq!(preview_tint(&app), Color32::from_rgba_unmultiplied(255, 0, 0, 110));+ app.run("prefs.set", json!({"path": "cursors.brushPreviewColor", "value": "#00ff80"})).unwrap();+ assert_eq!(preview_tint(&app), Color32::from_rgba_unmultiplied(0, 255, 128, 110));+ app.session.prefs.edit(|p| p.cursors.brush_preview_color = "nonsense".into());+ assert_eq!(preview_tint(&app), Color32::from_rgba_unmultiplied(255, 0, 0, 110), "unreadable values keep the red");+ }}diff --git a/docs/scorecard.md b/docs/scorecard.mdindex 54f831f88..cc7c4c89c 100644--- a/docs/scorecard.md+++ b/docs/scorecard.md@@ -9,7 +9,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js| [Performance](#performance) | 3 | 22 | 22 | 56 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 16 not run, 6 not measurable yet (baseline 2026-10-05) || [File compatibility](#file-compatibility) | 5 | 11 | 11 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 236/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 133/256, round trip ≥ 258/256 || [Tools](#tools) | 21 | 17 | 4 | |-| [Workspace and UI](#workspace-and-ui) | 6 | 7 | 10 | settings that do nothing: 41 of 152 (target 0) |+| [Workspace and UI](#workspace-and-ui) | 6 | 7 | 10 | settings that do nothing: 39 of 152 (target 0) || [Type](#type) | 0 | 3 | 6 | || [Automation](#automation) | 3 | 2 | 6 | 630 of 630 menu items live (100.0%) || [Reliability](#reliability) | 5 | 2 | 0 | never-crash lints on 28 of 28 crates; panic_hunt present |@@ -175,9 +175,9 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t## Workspace and UI-**Settings that do nothing: 41 of 152** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.+**Settings that do nothing: 39 of 152** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.-Unread: `general.color_picker`, `general.beep_when_done`, `general.always_create_smart_objects_when_placing`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `tools.enable_flick_panning`, `tools.vary_round_brush_hardness_on_hud`, `tools.show_transformation_values`, `file_handling.image_previews`, `file_handling.save_in_background`, `file_handling.ignore_exif_profile_tag`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `cursors.brush_preview_color`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `units_and_rulers.print_resolution`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.missing_glyph_protection`, `type_.show_font_names_in_english`, `type_.text_engine`, `type_.font_preview`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.+Unread: `general.color_picker`, `general.beep_when_done`, `general.always_create_smart_objects_when_placing`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `tools.enable_flick_panning`, `tools.show_transformation_values`, `file_handling.image_previews`, `file_handling.save_in_background`, `file_handling.ignore_exif_profile_tag`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `units_and_rulers.print_resolution`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.missing_glyph_protection`, `type_.show_font_names_in_english`, `type_.text_engine`, `type_.font_preview`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.| Id | Target | Status | Issue | Note ||---|---|---|---|---|