diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index ac471a9269..556c98ea97 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -62,8 +62,10 @@ pub mod zoom_window_fit; #[cfg(test)] pub(crate) use cursors::eyedropper_ring_colors; -pub(crate) use cursors::{alt_picks, alt_samples, composite_color, paint_bucket_cursor, pen_cursor, pencil_cursor_rect, pencil_tip_is_round, pipette_cursor}; -use cursors::{brush_tip_centre, eyedropper_ring, precise_other_cursor, visible_crosshair}; +pub(crate) use cursors::{ + alt_picks, alt_samples, composite_color, paint_bucket_cursor, pen_cursor, pencil_cursor_rect, pencil_tip_is_round, pipette_cursor, precise_other_cursor, +}; +use cursors::{brush_tip_centre, eyedropper_ring, visible_crosshair}; pub(crate) use display::GPU_OUTPUT; use display::texture_key; pub(crate) use display::{cache_key, canvas_display, display_doc, display_image}; diff --git a/crates/ui-egui/src/canvas/cursors.rs b/crates/ui-egui/src/canvas/cursors.rs index e692c4fe07..30370f780f 100644 --- a/crates/ui-egui/src/canvas/cursors.rs +++ b/crates/ui-egui/src/canvas/cursors.rs @@ -120,7 +120,7 @@ pub(crate) fn alt_samples(tool: Tool, mods: egui::Modifiers) -> bool { /// Preferences › Cursors › Other Cursors asks for the precise crosshair, or Caps Lock swaps the /// standard cursor for it (and back), as in Photoshop (#4639). -pub(super) fn precise_other_cursor(app: &PhotocraftApp) -> bool { +pub(crate) fn precise_other_cursor(app: &PhotocraftApp) -> bool { (app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise) != app.caps_lock } diff --git a/crates/ui-egui/src/tools/magnetic_lasso_ui.rs b/crates/ui-egui/src/tools/magnetic_lasso_ui.rs index 29d3861d9b..6a5574aadf 100644 --- a/crates/ui-egui/src/tools/magnetic_lasso_ui.rs +++ b/crates/ui-egui/src/tools/magnetic_lasso_ui.rs @@ -451,9 +451,10 @@ pub fn draw(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, hover: } /// The cursor over the canvas: a crosshair; Preferences › Cursors › Other Cursors › Precise also -/// shows the detection width around it. +/// shows the detection width around it, and Caps Lock swaps the preference either way, as in +/// Photoshop (#11690). pub fn cursor(app: &PhotocraftApp, painter: &egui::Painter, p: Pos2, zoom: f32) -> egui::CursorIcon { - if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise { + if crate::canvas::precise_other_cursor(app) { let r = (app.ui.tool_options.magnetic_width.clamp(1.0, 256.0) * zoom).max(2.0); painter.circle_stroke(p, r + 0.5, Stroke::new(1.0, Color32::from_black_alpha(140))); painter.circle_stroke(p, r, Stroke::new(1.0, Color32::from_white_alpha(220))); @@ -463,3 +464,6 @@ pub fn cursor(app: &PhotocraftApp, painter: &egui::Painter, p: Pos2, zoom: f32) #[cfg(test)] mod tests; + +#[cfg(test)] +mod caps_lock_cursor_tests; diff --git a/crates/ui-egui/src/tools/magnetic_lasso_ui/caps_lock_cursor_tests.rs b/crates/ui-egui/src/tools/magnetic_lasso_ui/caps_lock_cursor_tests.rs new file mode 100644 index 0000000000..39cd778e36 --- /dev/null +++ b/crates/ui-egui/src/tools/magnetic_lasso_ui/caps_lock_cursor_tests.rs @@ -0,0 +1,59 @@ +//! Caps Lock and the Magnetic Lasso's cursor (#11690): it swaps Preferences › Cursors › Other +//! Cursors, so with Standard and Caps Lock on the pointer shows a circle as big as the detection +//! Width (scaled with zoom), and with Precise and Caps Lock on it doesn't. + +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::Tool; + +fn app(other: &str, caps_lock: bool) -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.ui.tool = Tool::MagneticLasso; + app.ui.tool_options.magnetic_width = 10.0; + app.run("prefs.set", json!({"path": "cursors.other", "value": other})).unwrap(); + app.caps_lock = caps_lock; + app +} + +/// The radii of the circles `cursor` draws at `zoom`, and the OS cursor it asks for. +fn drawn(app: &PhotocraftApp, zoom: f32) -> (Vec, egui::CursorIcon) { + let ctx = egui::Context::default(); + ctx.begin_pass(egui::RawInput::default()); + let painter = ctx.layer_painter(egui::LayerId::background()); + let icon = super::cursor(app, &painter, egui::pos2(100.0, 100.0), zoom); + let mut out = ctx.end_pass(); + out.textures_delta.clear(); + let radii = out + .shapes + .iter() + .filter_map(|c| match &c.shape { + egui::Shape::Circle(c) => Some(c.radius), + _ => None, + }) + .collect(); + (radii, icon) +} + +#[test] +fn caps_lock_on_standard_draws_the_width_circle_scaled_with_zoom() { + let app = app("standard", true); + let (radii, icon) = drawn(&app, 1.0); + assert!(radii.iter().any(|r| (r - 10.0).abs() < 1e-3), "a 10 px circle at Width 10, 100 %: {radii:?}"); + assert_eq!(icon, egui::CursorIcon::Crosshair, "with a crosshair in the middle"); + let (radii, _) = drawn(&app, 2.0); + assert!(radii.iter().any(|r| (r - 20.0).abs() < 1e-3), "twice as big at 200 %: {radii:?}"); +} + +#[test] +fn caps_lock_on_precise_swaps_back_to_no_circle() { + let (radii, _) = drawn(&app("precise", true), 1.0); + assert!(radii.is_empty(), "no width circle: {radii:?}"); +} + +#[test] +fn without_caps_lock_the_preference_decides() { + assert!(drawn(&app("standard", false), 1.0).0.is_empty(), "Standard: no circle"); + assert!(!drawn(&app("precise", false), 1.0).0.is_empty(), "Precise: the width circle"); +}