Skip to content

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

Alt-click with the Brush, Pencil, Gradient or Paint Bucket samples a colour (#417) (#450)

BFlatb92667754909
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index f5611afb7..48a083c75 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1689,6 +1689,10 @@ 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`).
+ 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
+ }
t if t.is_brushlike() || t == Tool::QuickSelection => {
// Preferences › Cursors: brush tip outline (normal = the 50% contour, or
// full size), precise crosshair, or the standard pointer.
@@ -2026,6 +2030,30 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &View
}
}
+/// Tools on which holding ⌥ (Alt) switches to the Eyedropper: a click or drag sets the
+/// foreground colour, as with the Eyedropper itself (#417).
+fn alt_samples(tool: Tool, mods: egui::Modifiers) -> bool {
+ // Control+Alt is the brush-resize drag (`brush_resize`), not sampling.
+ mods.alt && !mods.ctrl && matches!(tool, Tool::Brush | Tool::Pencil | Tool::Gradient | Tool::PaintBucket)
+}
+
+/// 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 {
+ if matches!(ev, ToolEvent::Down { .. }) {
+ app.alt_sampling = alt_samples(app.ui.tool, mods);
+ }
+ if !app.alt_sampling {
+ return false;
+ }
+ match ev {
+ // Without ⌥: the sample sets the foreground colour, whatever the Eyedropper would do.
+ ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } => sample_eyedropper(app, x, y, egui::Modifiers::NONE),
+ ToolEvent::Up { .. } => app.alt_sampling = false,
+ }
+ true
+}
+
fn sample_eyedropper(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers) {
if let Ok(v) = app.run("document.pixel", json!({"x": x.floor(), "y": y.floor()})) {
let color: Vec<f32> = serde_json::from_value(v).unwrap_or_default();
@@ -2084,6 +2112,10 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
if crate::brush_resize::pointer(app, ev, mods, armed) {
return;
}
+ // ⌥ (Alt) with a painting tool is the Eyedropper for that press.
+ if alt_eyedropper(app, ev, mods) {
+ return;
+ }
// Move tool: ⇧ locks the axis, ⌥ duplicates (move_mods.rs).
let ev = crate::move_mods::filter_event(app, ev, mods);
// Ruler, Count and Note tools.
@@ -2481,6 +2513,48 @@ mod tests {
assert!(app.session.tools.foreground[1] > 0.99 && app.session.tools.foreground[0] < 0.01);
}
+ #[test]
+ fn alt_with_a_painting_tool_samples_the_foreground_instead_of_painting() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
+ app.run("file.new", json!({"width": 40, "height": 20, "background": "transparent"})).unwrap();
+ app.run("shape.create", json!({"kind": "rect", "rect": [0, 0, 20, 20], "fill": "#ff0000"})).unwrap();
+ app.run("shape.create", json!({"kind": "rect", "rect": [20, 0, 20, 20], "fill": "#00ff00"})).unwrap();
+ let alt = egui::Modifiers::ALT;
+ for tool in [Tool::Brush, Tool::Pencil, Tool::Gradient, Tool::PaintBucket] {
+ app.ui.tool = tool;
+ app.run("tools.setColors", json!({"foreground": [0.0, 0.0, 1.0, 1.0], "background": [1.0, 1.0, 1.0, 1.0]})).unwrap();
+ let rev = app.session.active().unwrap().revision;
+ tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, alt);
+ assert_eq!(app.session.tools.foreground, [1.0, 0.0, 0.0, 1.0], "{tool:?}: ⌥-click sets the foreground");
+ // A drag keeps sampling, even after ⌥ is let go; the background is untouched.
+ tool_event(&mut app, ToolEvent::Move { x: 30.0, y: 10.0, pressure: 1.0 }, egui::Modifiers::NONE);
+ assert_eq!(app.session.tools.foreground, [0.0, 1.0, 0.0, 1.0], "{tool:?}");
+ tool_event(&mut app, ToolEvent::Up { x: 30.0, y: 10.0 }, egui::Modifiers::NONE);
+ assert_eq!(app.session.tools.background, [1.0, 1.0, 1.0, 1.0]);
+ assert_eq!(app.session.active().unwrap().revision, rev, "{tool:?}: sampling must not edit the document");
+ assert!(app.drag.is_none() && !app.alt_sampling);
+ }
+ // Agents get the same through `ui.pointer` (MCP `ui_pointer`).
+ app.ui.tool = Tool::Brush;
+ let ctx = egui::Context::default();
+ let events = json!([{"kind": "down", "x": 10, "y": 10}, {"kind": "up", "x": 10, "y": 10}]);
+ let (req, _rx) = crate::control::ControlRequest::new("ui.pointer", json!({"modifiers": {"alt": true}, "events": events}));
+ let _ = crate::control::handle(&mut app, &ctx, &req);
+ assert_eq!(app.session.tools.foreground, [1.0, 0.0, 0.0, 1.0]);
+ app.run("tools.setColors", json!({"foreground": [0.0, 1.0, 0.0, 1.0]})).unwrap();
+ // Without ⌥ the Brush paints again, and ⌥ pressed mid-stroke doesn't switch to sampling.
+ app.run("layer.new.layer", json!({})).unwrap();
+ let rev = app.session.active().unwrap().revision;
+ tool_event(&mut app, ToolEvent::Down { x: 5.0, y: 5.0, pressure: 1.0 }, egui::Modifiers::NONE);
+ tool_event(&mut app, ToolEvent::Move { x: 30.0, y: 5.0, pressure: 1.0 }, alt);
+ tool_event(&mut app, ToolEvent::Up { x: 30.0, y: 5.0 }, alt);
+ assert_eq!(app.session.tools.foreground, [0.0, 1.0, 0.0, 1.0]);
+ assert!(app.session.active().unwrap().revision > rev, "the stroke was painted");
+ // Control+Alt stays the brush-resize gesture, never a sample.
+ assert!(!alt_samples(Tool::Brush, egui::Modifiers { alt: true, ctrl: true, ..Default::default() }));
+ assert!(!alt_samples(Tool::Eraser, alt), "⌥ with the Eraser is not the Eyedropper");
+ }
+
#[test]
fn brush_drag_shows_the_real_stroke_and_commits_it() {
// The drag used to draw a hard, flat stand-in and only showed the soft brush on release.
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 95ac2eb9a..52bd5a880 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -248,6 +248,9 @@ pub struct PhotocraftApp {
/// The next tool `Down` is an Alt+right-drag that resizes the brush (#297). `tool_event`
/// takes it on every event, so a press another handler consumes can't leave it set.
pub(crate) brush_resize_armed: bool,
+ /// This press began with ⌥ (Alt) held on a painting tool, so it samples colours instead of
+ /// painting until it is released (`canvas::alt_eyedropper`, #417).
+ pub(crate) alt_sampling: bool,
control_rx: Option<Receiver<ControlRequest>>,
pending_screenshots: Vec<(u64, Option<String>, Sender<ControlResponse>)>,
/// Screenshots not yet requested from the viewport: (token, earliest time in ms, frames seen).
@@ -373,6 +376,7 @@ impl PhotocraftApp {
last_stroke_end: None,
brush_resize: None,
brush_resize_armed: false,
+ alt_sampling: false,
control_rx: None,
pending_screenshots: Vec::new(),
queued_screenshots: Vec::new(),