Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Cursors: the pen tools show their icon, and Caps Lock swaps it for the crosshair (#4639) (#4650)

Dan Hiornsf8cfc1131afd
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index fd76d5d02..b05291688 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -3644,32 +3644,41 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
}
}
// The Eyedropper: the comparison ring follows the pointer while it samples (#213);
- // otherwise its pipette, or the crosshair for Precise Other Cursors.
+ // otherwise its pipette, or the crosshair for precise Other Cursors.
Tool::Eyedropper => {
if let Some(icon) = eyedropper_ring(app, &painter, &xf, p, response.is_pointer_button_down_on()) {
icon
- } else if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise {
+ } else if precise_other_cursor(app) {
egui::CursorIcon::Crosshair
} else {
pipette_cursor(ui.ctx(), p)
}
}
- // The Color Sampler: a pipette, or the crosshair for Precise Other Cursors.
+ // The Color Sampler: a pipette, or the crosshair for precise Other Cursors.
Tool::ColorSampler => {
- if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise {
+ if precise_other_cursor(app) {
egui::CursorIcon::Crosshair
} else {
pipette_cursor(ui.ctx(), p)
}
}
- // The Paint Bucket: a bucket, or the crosshair for Precise Other Cursors (#3968).
+ // The Paint Bucket: a bucket, or the crosshair for precise Other Cursors (#3968).
Tool::PaintBucket => {
- if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise {
+ if precise_other_cursor(app) {
egui::CursorIcon::Crosshair
} else {
paint_bucket_cursor(ui.ctx(), p)
}
}
+ // The pen tools: their own icon with its nib on the point, or the crosshair for
+ // precise Other Cursors (#4639).
+ Tool::Pen | Tool::FreeformPen | Tool::CurvaturePen | Tool::AddAnchorPoint | Tool::DeleteAnchorPoint | Tool::ConvertPoint => {
+ if precise_other_cursor(app) {
+ egui::CursorIcon::Crosshair
+ } else {
+ pen_cursor(ui.ctx(), tool, p)
+ }
+ }
// Preferences › Cursors › Other Cursors: Precise shows a crosshair for every tool.
Tool::Move | Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask
if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise =>
@@ -4517,6 +4526,26 @@ pub(crate) fn alt_samples(tool: Tool, mods: egui::Modifiers) -> bool {
mods.alt && !mods.ctrl && matches!(tool, Tool::Brush | Tool::Pencil | Tool::Gradient | Tool::PaintBucket)
}
+/// Preferences › Cursors › Other Cursors asks for the precise crosshair, or Caps Lock swaps the
+/// standard cursor for it (and back), as in Photoshop (#4639).
+fn precise_other_cursor(app: &PhotocraftApp) -> bool {
+ (app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise) != app.caps_lock
+}
+
+/// A pen tool's standard cursor: its toolbar icon with the nib (the Convert Point tool: the
+/// anchor) on `p`. Draws it at `p` and returns the OS cursor to set (hidden).
+pub(crate) fn pen_cursor(ctx: &egui::Context, tool: Tool, p: Pos2) -> egui::CursorIcon {
+ // Where each icon's point is in its 24-unit box: the full-size pen's nib at (2.3, 2.3); the
+ // Freeform and Curvature pens draw it at 0.68 × from (7, 7); Convert Point's apex anchor.
+ let hot = match tool {
+ Tool::FreeformPen | Tool::CurvaturePen => vec2(8.56, 8.56),
+ Tool::ConvertPoint => vec2(12.0, 7.0),
+ _ => vec2(2.3, 2.3),
+ };
+ crate::icons::cursor(ctx, crate::icons::tool_icon(tool), p, hot / 24.0, 22.0);
+ egui::CursorIcon::None
+}
+
/// The sampling cursor: a pipette whose tip is the sampled pixel. Draws it at `p` and returns the
/// OS cursor to set (hidden).
pub(crate) fn pipette_cursor(ctx: &egui::Context, p: Pos2) -> egui::CursorIcon {
diff --git a/crates/ui-egui/src/caps_lock_tests.rs b/crates/ui-egui/src/caps_lock_tests.rs
index f9edc587f..1e57c4092 100644
--- a/crates/ui-egui/src/caps_lock_tests.rs
+++ b/crates/ui-egui/src/caps_lock_tests.rs
@@ -3,7 +3,9 @@
//! tools (Brush, Pencil, Clone Stamp, Healing, Eraser, …): Move, Marquee, Crop and Zoom keep
//! their own cursors. Tests drive the real canvas (`document_area`) and compare the painted
//! cursor that caps lock produces with the one the Precise preference draws — they must match,
-//! and the Standard preference (off) must not.
+//! and the Standard preference (off) must not. The tools under Cursors › Other Cursors with a
+//! cursor of their own (the pens, Eyedropper, Color Sampler, Paint Bucket) swap it for the
+//! crosshair and back with caps lock (#4639).
use egui::CursorIcon;
use egui_kittest::Harness;
@@ -113,3 +115,42 @@ fn caps_lock_leaves_non_painting_tools_cursors_alone() {
caps(&mut h, false);
}
}
+
+fn other(h: &mut Harness<'_, PhotocraftApp>, value: &str) {
+ h.state_mut().run("prefs.set", json!({"path": "cursors.other", "value": value})).unwrap();
+ h.run_steps(2);
+}
+
+/// #4639: the pen tools show their own icon under Standard Other Cursors and the crosshair
+/// under Precise, and caps lock swaps one for the other either way, as in Photoshop. The
+/// Eyedropper, Color Sampler and Paint Bucket, which already followed the preference, swap too.
+#[test]
+fn caps_lock_swaps_the_other_cursors_between_their_icon_and_the_crosshair() {
+ for tool in [
+ Tool::Pen,
+ Tool::FreeformPen,
+ Tool::CurvaturePen,
+ Tool::AddAnchorPoint,
+ Tool::DeleteAnchorPoint,
+ Tool::ConvertPoint,
+ Tool::Eyedropper,
+ Tool::ColorSampler,
+ Tool::PaintBucket,
+ ] {
+ let mut h = capslock_harness();
+ h.state_mut().ui.tool = tool;
+ h.run_steps(2);
+ other(&mut h, "standard");
+ let standard = fp(&h);
+ assert_eq!(standard.icon, CursorIcon::None, "{tool:?}: the standard cursor is the tool's own icon, drawn");
+ other(&mut h, "precise");
+ let precise = fp(&h);
+ assert_ne!(standard, precise, "{tool:?}: Standard and Precise differ");
+ caps(&mut h, true);
+ assert_eq!(fp(&h), standard, "{tool:?}: caps lock turns the precise crosshair back into the icon");
+ other(&mut h, "standard");
+ assert_eq!(fp(&h), precise, "{tool:?}: caps lock turns the icon into the precise crosshair");
+ caps(&mut h, false);
+ assert_eq!(fp(&h), standard, "{tool:?}: releasing caps lock restores the icon");
+ }
+}