PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Eyedropper cursor: a pipette instead of the crosshair when sampling (#1028)
jason4short2c9ae1fce6a7
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 93f0125b9..19bfcbcbd 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -1800,9 +1800,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise {ctx.set_cursor_icon(egui::CursorIcon::Crosshair);} else {- // The tip of the icon's pipette is at (2, 22) of its 24-unit box.- crate::icons::cursor(&ctx, "pipette", p, vec2(2.0, 22.0) / 24.0, 20.0);- ctx.set_cursor_icon(egui::CursorIcon::None);+ ctx.set_cursor_icon(pipette_cursor(&ctx, p));}if let Some(p) = crate::dialogs::free_press(&ctx, rect) {let d = xf.to_doc(p);@@ -2015,9 +2013,14 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:let icon = match tool {// Resizing the brush: the circle stays where the drag began (`brush_resize`).t if resizing && crate::brush_resize::applies(t) => egui::CursorIcon::None,- // ⌥ turns a painting tool into the Eyedropper (`alt_eyedropper`).+ // ⌥ turns a painting tool into the Eyedropper (`alt_eyedropper`): its cursor too,+ // unless Preferences › Cursors › Other Cursors asks for the precise crosshair.t if app.alt_sampling || (app.drag.is_none() && alt_samples(t, crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)))) => {- egui::CursorIcon::Crosshair+ if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise {+ egui::CursorIcon::Crosshair+ } else {+ pipette_cursor(ui.ctx(), p)+ }}t if t.is_brushlike() || t == Tool::QuickSelection => {// Preferences › Cursors: brush tip outline (normal = the 50% contour, or@@ -2089,6 +2092,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:}}Tool::Type | Tool::VerticalType => egui::CursorIcon::Text,+ Tool::Eyedropper => pipette_cursor(ui.ctx(), p),Tool::MagneticLasso => crate::magnetic_lasso_ui::cursor(app, &painter, p, view.zoom),_ => egui::CursorIcon::Crosshair,};@@ -2386,6 +2390,14 @@ fn alt_samples(tool: Tool, mods: egui::Modifiers) -> bool {mods.alt && !mods.ctrl && matches!(tool, Tool::Brush | Tool::Pencil | Tool::Gradient | Tool::PaintBucket)}+/// 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 {+ // The tip of the icon's pipette is at (2, 22) of its 24-unit box.+ crate::icons::cursor(ctx, "pipette", p, vec2(2.0, 22.0) / 24.0, 20.0);+ egui::CursorIcon::None+}+/// Decided when the press starts, so ⌥ pressed or released mid-stroke never switches between/// painting and sampling.fn alt_eyedropper(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool {diff --git a/crates/ui-egui/src/input_tests.rs b/crates/ui-egui/src/input_tests.rsindex c232f1ff1..98fdde7ca 100644--- a/crates/ui-egui/src/input_tests.rs+++ b/crates/ui-egui/src/input_tests.rs@@ -366,3 +366,26 @@ fn kerning_field_values() {assert_eq!(crate::type_tool::parse_kerning(s), None, "{s}");}}++/// Sampling shows a pipette instead of the crosshair: the Eyedropper tool, and a painting tool+/// with ⌥ held. Preferences › Cursors › Other Cursors = Precise keeps the crosshair.+#[test]+fn eyedropper_and_alt_sampling_show_a_pipette() {+ let mut h = harness();+ let p = h.state().last_canvas_rect.center();+ let cursor = |h: &mut Harness<'static, PhotocraftApp>| {+ h.hover_at(p);+ h.run_steps(2);+ h.output().platform_output.cursor_icon+ };+ h.state_mut().ui.tool = crate::state::Tool::Eyedropper;+ assert_eq!(cursor(&mut h), egui::CursorIcon::None, "the pipette replaces the pointer");+ h.state_mut().ui.tool = crate::state::Tool::Brush;+ h.event(egui::Event::ModifiersChanged(Modifiers::ALT));+ assert_eq!(cursor(&mut h), egui::CursorIcon::None, "⌥ samples with a pipette");+ h.state_mut().run("prefs.set", json!({"values": {"cursors.other": "precise"}})).unwrap();+ assert_eq!(cursor(&mut h), egui::CursorIcon::Crosshair, "Precise keeps the crosshair");+ h.event(egui::Event::ModifiersChanged(Modifiers::NONE));+ h.state_mut().ui.tool = crate::state::Tool::Eyedropper;+ assert_eq!(cursor(&mut h), egui::CursorIcon::Crosshair);+}