Skip to content

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

⌘⌥⌃-click on the canvas selects the topmost layer under the pointer (#813)

jason4short684f85e2e52d
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 8bda726bb..e9b00f5a7 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2331,6 +2331,11 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
}
// Window › Modifier Keys: sticky Shift/⌘/⌥ act as held keys.
let mods = crate::workspace_ui::sticky_mods(app, mods);
+ // ⌘⌥⌃-click with any tool selects the topmost layer with pixels there (quick_pick.rs); it
+ // must run before the ⌃⌥ brush resize below, which the same keys would trigger.
+ if crate::quick_pick::pointer(app, ev, mods) {
+ return;
+ }
// Control+Alt-drag or Alt+right-drag with a painting tool resizes the brush instead of
// painting (#231, #297).
if crate::brush_resize::pointer(app, ev, mods, armed) {
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index c5dcb27bd..34c33ca32 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -96,6 +96,7 @@ pub mod preset_panels;
pub mod props_layout;
pub mod proxy;
pub mod puppet_ui;
+pub mod quick_pick;
pub mod rasterize_prompt;
pub mod retouch_ui;
mod rgb_histogram;
@@ -301,6 +302,8 @@ pub struct PhotocraftApp {
last_stroke_end: Option<(DocId, [f64; 2])>,
/// Control+Alt-drag brush resize in progress (`brush_resize`, #231).
pub(crate) brush_resize: Option<brush_resize::Resize>,
+ /// A ⌘⌥⌃-click layer pick is in progress; its drag and release are swallowed (`quick_pick`).
+ pub(crate) quick_pick: bool,
/// 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,
@@ -437,6 +440,7 @@ impl PhotocraftApp {
defer_live_stroke: false,
last_stroke_end: None,
brush_resize: None,
+ quick_pick: false,
brush_resize_armed: false,
alt_sampling: false,
opacity_keys: None,
diff --git a/crates/ui-egui/src/quick_pick.rs b/crates/ui-egui/src/quick_pick.rs
new file mode 100644
index 000000000..c6c32e1f2
--- /dev/null
+++ b/crates/ui-egui/src/quick_pick.rs
@@ -0,0 +1,116 @@
+//! ⌘⌥⌃-click on the canvas, with any tool: select the topmost visible layer with pixels under the
+//! pointer (`layer.pickAt`), without switching to the Move tool. The whole press–drag–release is
+//! swallowed so the active tool never sees it; it runs before the ⌃⌥ brush-resize gesture, which
+//! would otherwise claim it.
+//!
+//! Platforms: the gesture is macOS-only, because ⌘ is the only key that tells it apart from the
+//! ⌃⌥ brush resize. egui never reports `mac_cmd` on Windows or Linux, so there it is a no-op and
+//! ⌃⌥-drag keeps resizing the brush; the Move tool's Auto-Select picks layers on every platform.
+//! A failed pick is shown as a status-bar error and never panics.
+
+use serde_json::json;
+
+use crate::PhotocraftApp;
+use crate::canvas::ToolEvent;
+
+/// ⌘ + ⌥ + ⌃ held together. Always false off macOS, where egui never sets `mac_cmd`.
+pub fn is_gesture(mods: egui::Modifiers) -> bool {
+ mods.mac_cmd && mods.alt && mods.ctrl
+}
+
+/// Handles one canvas pointer event; true when it was consumed.
+pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool {
+ match ev {
+ ToolEvent::Down { x, y, .. } => {
+ if app.drag.is_some() || app.session.active().is_none() || !is_gesture(mods) {
+ return false;
+ }
+ if let Err(e) = app.run("layer.pickAt", json!({"x": x, "y": y, "target": "layer", "mode": "replace"})) {
+ app.ui.status = e;
+ app.ui.status_error = true;
+ }
+ app.quick_pick = true;
+ true
+ }
+ ToolEvent::Move { .. } => app.quick_pick,
+ ToolEvent::Up { .. } => std::mem::take(&mut app.quick_pick),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use egui::Modifiers;
+ use photocraft_geom::Rect;
+ use serde_json::json;
+
+ use super::*;
+ use crate::canvas::tool_event;
+ use crate::state::Tool;
+
+ const PICK: Modifiers = Modifiers { alt: true, ctrl: true, shift: false, mac_cmd: true, command: true };
+
+ /// A 64×64 document with A painted at (8..24)² and B at (40..56)²; B is active.
+ fn app() -> (PhotocraftApp, photocraft_doc::LayerId) {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.session.execute("file.new", json!({"width": 64, "height": 64})).unwrap();
+ app.sync_views();
+ let mut first = None;
+ for at in [8, 40] {
+ app.session.execute("layer.new.layer", json!({})).unwrap();
+ app.session
+ .edit("paint", |doc, a| {
+ doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(at, at, at + 16, at + 16), &[1.0, 0.0, 0.0, 1.0]);
+ Ok(())
+ })
+ .unwrap();
+ first.get_or_insert(app.session.active().unwrap().active_layer.unwrap());
+ }
+ (app, first.unwrap())
+ }
+
+ #[test]
+ fn picks_the_layer_under_the_pointer_with_a_painting_tool() {
+ let (mut app, a) = app();
+ app.ui.tool = Tool::Brush;
+ let before = app.session.active().unwrap().history.past_len();
+ tool_event(&mut app, ToolEvent::Down { x: 12.0, y: 12.0, pressure: 1.0 }, PICK);
+ tool_event(&mut app, ToolEvent::Move { x: 20.0, y: 20.0, pressure: 1.0 }, PICK);
+ tool_event(&mut app, ToolEvent::Up { x: 20.0, y: 20.0 }, PICK);
+ let st = app.session.active().unwrap();
+ assert_eq!(st.active_layer, Some(a));
+ assert!(app.drag.is_none() && app.brush_resize.is_none(), "neither painted nor resized the brush");
+ assert_eq!(st.history.past_len(), before, "no paint stroke was recorded");
+ }
+
+ #[test]
+ fn needs_all_three_modifiers() {
+ let (mut app, a) = app();
+ app.ui.tool = Tool::RectMarquee;
+ let no_ctrl = Modifiers { ctrl: false, ..PICK };
+ assert!(!pointer(&mut app, ToolEvent::Down { x: 12.0, y: 12.0, pressure: 1.0 }, no_ctrl));
+ assert_ne!(app.session.active().unwrap().active_layer, Some(a));
+ }
+
+ /// Windows and Linux report Ctrl+Alt (+Shift) but never `mac_cmd`: the click falls through to
+ /// the tool and the ⌃⌥ brush resize instead of picking a layer.
+ #[test]
+ fn is_a_no_op_without_the_mac_command_key() {
+ let (mut app, a) = app();
+ app.ui.tool = Tool::Brush;
+ let pc = Modifiers { alt: true, ctrl: true, shift: false, mac_cmd: false, command: true };
+ for mods in [pc, Modifiers { shift: true, ..pc }] {
+ assert!(!pointer(&mut app, ToolEvent::Down { x: 12.0, y: 12.0, pressure: 1.0 }, mods));
+ assert!(!app.quick_pick);
+ assert_ne!(app.session.active().unwrap().active_layer, Some(a));
+ }
+ }
+
+ #[test]
+ fn a_failed_pick_is_a_status_error_and_swallows_the_click() {
+ let (mut app, _) = app();
+ // Far outside the canvas no layer has pixels: the pick fails gracefully.
+ assert!(pointer(&mut app, ToolEvent::Down { x: 1e9, y: -1e9, pressure: 1.0 }, PICK));
+ assert!(pointer(&mut app, ToolEvent::Up { x: 1e9, y: -1e9 }, PICK));
+ assert!(!app.quick_pick);
+ }
+}
diff --git a/docs/ui-design.md b/docs/ui-design.md
index 03ed8c1cd..f282de653 100644
--- a/docs/ui-design.md
+++ b/docs/ui-design.md
@@ -40,6 +40,7 @@ curl -sfL -o assets/icons/<name>.svg https://raw.githubusercontent.com/lucide-ic
|---|---|---|
| Type tool | `type_tool.rs` | Click: point text with the placeholder "Lorem Ipsum" selected. Drag: paragraph box. Inline caret and selection drawn from the text engine layout. ⌥/⌘ word and line navigation, ↩ newline, ⌘↩ or Esc commits, a click outside commits. One history step per session (`coalesce`). A new layer is named after its text; an empty one is deleted. |
| Free Transform | `transform_tool.rs` | ⌘T. Corners scale proportionally (⇧ frees them); edges scale one axis; ⌥ scales about the reference point; ⌘-corner distorts; dragging outside rotates (⇧ snaps to 15°); dragging inside moves. Preview = document without the moving pixels + a textured 24×24 mesh. ↩ or a double-click commits via `edit.transform {rect, quad}`. |
+| Quick layer pick | `quick_pick.rs` | macOS: ⌘⌥⌃-click with any tool selects the topmost visible layer with pixels under the pointer (`layer.pickAt`) without switching tools; the drag and release are swallowed. Windows/Linux have no third modifier distinct from the ⌃⌥ brush resize, so the gesture is a no-op there (use the Move tool's Auto-Select). A failed pick is a status-bar error. |
| Layers rows | `panels.rs` | Double-click: on the name renames in place; on the Background makes it a normal layer; on an adjustment or fill thumbnail opens its Properties; on a Smart Object thumbnail opens its contents (Edit Contents); anywhere else on the row opens Layer Style. |
| Layer masks | `panels.rs` | Clicking the mask thumbnail targets the mask (corner-bracket frame; the tab reads "Layer, Layer Mask/8"). Brush, eraser (paints background colour), gradient and bucket then send `"target": "mask"`. Adjustment and fill layers target their mask automatically. |
| Levels / Curves | `tone.rs` | Histogram of the image *below* the adjustment. Curves: click to add a point, drag out to delete. Every change is a coalesced `layer.setAdjustment`, so one drag = one undo step and the canvas updates at full resolution on the GPU. |