Skip to content

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

Selection cursors: Photoshop's badges for cutting, copying and dragging the selected pixels (#2250)

texjer95006995f646
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 78cc42134..a804e3b18 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2854,11 +2854,31 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
let toward = xf.to_screen((d[0] + k[0] * 16.0) as f32, (d[1] + k[1] * 16.0) as f32) - p;
crate::crop_ui::draw_turn_cursor(&ctx, p, toward);
}
- } else if app.drag.as_ref().is_some_and(|d| d.sel_move.is_some())
- || response.hover_pos().is_some_and(|p| app.drag.is_none() && selection_drag_kind(app, tool, xf.to_doc(p), ui.input(|i| i.modifiers)).is_some())
+ } else if let Some(p) = response.hover_pos()
+ && let Some(c) = selection_cursor(app, tool, xf.to_doc(p), crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)))
{
- // Over the ants with a marquee or lasso: a press drags the selection.
- ui.ctx().set_cursor_icon(egui::CursorIcon::Move);
+ // Over a selection (Photoshop's cursors): the move cursor where a press drags the
+ // outline (#1428), the arrow with a badge for what a press does to the selected
+ // pixels, a hollow arrowhead while they are dragged, a plain arrow over a floating
+ // piece.
+ let icon = match c {
+ SelCursor::Outline => egui::CursorIcon::Move,
+ SelCursor::Dragging => {
+ // The tip of the arrowhead is at (4, 4) of its 24-unit box.
+ crate::icons::cursor(ui.ctx(), "mouse-pointer-2", p, vec2(4.0, 4.0) / 24.0, 18.0);
+ egui::CursorIcon::None
+ }
+ SelCursor::Piece => egui::CursorIcon::Default,
+ SelCursor::Cut => {
+ crate::icons::cursor_badge(ui.ctx(), "scissors", p);
+ egui::CursorIcon::Default
+ }
+ SelCursor::Copy => {
+ crate::icons::cursor_copy_badge(ui.ctx(), p);
+ egui::CursorIcon::Default
+ }
+ };
+ ui.ctx().set_cursor_icon(icon);
} else if let Some(p) = response.hover_pos() {
let alt = ui.input(|i| i.modifiers.alt);
if !resizing && !alt && !app.ui.shell.sticky_alt {
@@ -3931,6 +3951,55 @@ fn command_moves_layer(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui:
selection_tool && mods.command && !mods.shift && !floating && app.ui.polygon.is_empty() && !inside_selection(app, p)
}
+/// Photoshop's cursor over a selection: what a press (or the drag under way) would do.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum SelCursor {
+ /// A marquee or lasso inside the selection moves the outline (the move cursor, #1428), also
+ /// while it is being dragged.
+ Outline,
+ /// Arrow with scissors: the Move tool (⌘ with a marquee) cuts and moves the selected pixels.
+ Cut,
+ /// Arrow with a second arrow: ⌥ lifts a copy of them instead.
+ Copy,
+ /// Plain arrow: a floating piece, moved again by a plain drag until it is dropped.
+ Piece,
+ /// Hollow arrowhead: the selected pixels are being dragged.
+ Dragging,
+}
+
+/// The cursor at document point `p` with `tool` in effect and `mods` held (`None`: not over a
+/// selection the press would move). The Move tool moves the selected pixels from anywhere
+/// (`move_ui::moves_selected_pixels`); a selection tool from inside the selection, or on the
+/// floating piece (`selection_drag_kind`).
+pub fn selection_cursor(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui::Modifiers) -> Option<SelCursor> {
+ if let Some(d) = &app.drag {
+ return match d.sel_move {
+ Some(true) => Some(SelCursor::Dragging),
+ Some(false) => Some(SelCursor::Outline),
+ None => None,
+ };
+ }
+ let floating = app.session.active().and_then(photocraft_engine::float_cmds::floating).is_some();
+ let pixels = if tool == Tool::Move {
+ crate::move_ui::moves_selected_pixels_with(app, tool)
+ } else {
+ match selection_drag_kind(app, tool, p, mods)? {
+ false => return Some(SelCursor::Outline),
+ true => true,
+ }
+ };
+ if !pixels {
+ return None;
+ }
+ // ⌥ (⌘⌥ with a selection tool) floats a copy, of the layer's pixels or of the piece.
+ let copy = mods.alt && (tool == Tool::Move || mods.command);
+ Some(match (floating, copy) {
+ (_, true) => SelCursor::Copy,
+ (true, false) => SelCursor::Piece,
+ (false, false) => SelCursor::Cut,
+ })
+}
+
/// Whole-pixel offset of a selection drag in progress (`Drag::sel_move`).
pub(crate) fn selection_drag_delta(app: &PhotocraftApp) -> Option<(i32, i32)> {
let d = app.drag.as_ref().filter(|d| d.sel_move.is_some())?;
diff --git a/crates/ui-egui/src/icons.rs b/crates/ui-egui/src/icons.rs
index a5135c04b..fbdc700ad 100644
--- a/crates/ui-egui/src/icons.rs
+++ b/crates/ui-egui/src/icons.rs
@@ -57,6 +57,39 @@ pub fn cursor(ctx: &egui::Context, name: &str, p: egui::Pos2, hot: Vec2, size: f
});
}
+/// A small icon beside the OS arrow pointer at `p` (Photoshop's cursor badges: scissors for
+/// cutting the selected pixels). Below-right of the arrow, white with a dark outline. The caller
+/// keeps the arrow (`CursorIcon::Default`).
+pub fn cursor_badge(ctx: &egui::Context, name: &str, p: egui::Pos2) {
+ badge(ctx, name, p, false);
+}
+
+/// Photoshop's duplicate badge: a second, dark arrowhead behind a white one.
+pub fn cursor_copy_badge(ctx: &egui::Context, p: egui::Pos2) {
+ badge(ctx, "mouse-pointer-2", p, true);
+}
+
+fn badge(ctx: &egui::Context, name: &str, p: egui::Pos2, doubled: bool) {
+ const SIZE: f32 = 12.0;
+ let rect = Rect::from_center_size(p + egui::vec2(20.0, 19.0), Vec2::splat(SIZE));
+ let area = egui::Area::new(egui::Id::new("pc-cursor-badge")).order(egui::Order::Tooltip).fixed_pos(rect.min).constrain(false).interactable(false);
+ area.show(ctx, |ui| {
+ let outline = image(name, SIZE, Color32::from_black_alpha(220));
+ let halo = [(-1.0, 0.0), (1.0, 0.0), (0.0, -1.0), (0.0, 1.0), (-1.0, -1.0), (1.0, 1.0), (-1.0, 1.0), (1.0, -1.0)];
+ if doubled {
+ let behind = rect.translate(egui::vec2(4.0, 4.0));
+ for (dx, dy) in halo {
+ image(name, SIZE, Color32::WHITE).paint_at(ui, behind.translate(egui::vec2(dx, dy)));
+ }
+ image(name, SIZE, Color32::BLACK).paint_at(ui, behind);
+ }
+ for (dx, dy) in halo {
+ outline.paint_at(ui, rect.translate(egui::vec2(dx, dy)));
+ }
+ image(name, SIZE, Color32::WHITE).paint_at(ui, rect);
+ });
+}
+
pub fn tool_icon(t: Tool) -> &'static str {
match t {
Tool::Move => "move",
diff --git a/crates/ui-egui/src/marquee_tests.rs b/crates/ui-egui/src/marquee_tests.rs
index ae5b754be..4e8cc26d2 100644
--- a/crates/ui-egui/src/marquee_tests.rs
+++ b/crates/ui-egui/src/marquee_tests.rs
@@ -408,3 +408,41 @@ fn alt_with_nothing_selected_draws_a_new_selection() {
let after = h.state().session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds());
assert!(after.is_none_or(|r| r.is_empty() || r != before), "⌥ subtracted: {before:?} -> {after:?}");
}
+
+/// Photoshop's cursor over a selection: the move cursor where a marquee press moves the outline,
+/// scissors where ⌘ (or the Move tool) cuts the selected pixels, a double arrow where ⌥ copies
+/// them, a hollow arrowhead while they are dragged, and a plain arrow over the floating piece.
+#[test]
+fn selection_cursor_follows_the_modifiers_and_the_floating_piece() {
+ use crate::canvas::{SelCursor, selection_cursor};
+ let (mut app, _) = painted();
+ let (inside, outside) = ([20.0, 20.0], [60.0, 50.0]);
+ let none = Modifiers::NONE;
+ let cmd_alt = Modifiers::COMMAND | Modifiers::ALT;
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, inside, none), Some(SelCursor::Outline));
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, outside, none), None);
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, inside, Modifiers::SHIFT), None);
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, inside, Modifiers::COMMAND), Some(SelCursor::Cut));
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, inside, cmd_alt), Some(SelCursor::Copy));
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, inside, Modifiers::ALT), None, "⌥ alone subtracts");
+ assert_eq!(selection_cursor(&app, Tool::Move, inside, none), Some(SelCursor::Cut));
+ assert_eq!(selection_cursor(&app, Tool::Move, outside, none), Some(SelCursor::Cut), "the Move tool moves them from anywhere");
+ assert_eq!(selection_cursor(&app, Tool::Move, inside, Modifiers::ALT), Some(SelCursor::Copy));
+ assert_eq!(selection_cursor(&app, Tool::Brush, inside, none), None);
+ // While the selected pixels are dragged, then once they float.
+ use crate::canvas::{ToolEvent, tool_event};
+ tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 }, Modifiers::COMMAND);
+ tool_event(&mut app, ToolEvent::Move { x: 35.0, y: 20.0, pressure: 1.0 }, Modifiers::COMMAND);
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, [35.0, 20.0], Modifiers::COMMAND), Some(SelCursor::Dragging));
+ tool_event(&mut app, ToolEvent::Up { x: 35.0, y: 20.0 }, Modifiers::COMMAND);
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, [35.0, 20.0], none), Some(SelCursor::Piece));
+ assert_eq!(selection_cursor(&app, Tool::Move, [35.0, 20.0], none), Some(SelCursor::Piece));
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, [35.0, 20.0], cmd_alt), Some(SelCursor::Copy), "a copy of the piece");
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, inside, none), None, "where it was cut from");
+ assert_eq!(selection_cursor(&app, Tool::Brush, [35.0, 20.0], none), None);
+ // The outline drag keeps the move cursor (#1428).
+ let (mut app, _) = painted();
+ tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 }, none);
+ tool_event(&mut app, ToolEvent::Move { x: 30.0, y: 20.0, pressure: 1.0 }, none);
+ assert_eq!(selection_cursor(&app, Tool::RectMarquee, [30.0, 20.0], none), Some(SelCursor::Outline));
+}
diff --git a/crates/ui-egui/src/move_ui.rs b/crates/ui-egui/src/move_ui.rs
index 46996ffdc..c288bf5d0 100644
--- a/crates/ui-egui/src/move_ui.rs
+++ b/crates/ui-egui/src/move_ui.rs
@@ -21,8 +21,13 @@ use crate::state::Tool;
/// float (type, shape, Smart Object, group, position-locked), a hidden layer or several selected
/// layers still move whole, as without one.
pub(crate) fn moves_selected_pixels(app: &PhotocraftApp) -> bool {
+ moves_selected_pixels_with(app, app.ui.tool)
+}
+
+/// [`moves_selected_pixels`] with `tool` in effect (the canvas cursor asks before the press).
+pub(crate) fn moves_selected_pixels_with(app: &PhotocraftApp, tool: Tool) -> bool {
let Some(st) = app.session.active() else { return false };
- app.ui.tool == Tool::Move
+ tool == Tool::Move
&& app.ui.transform.is_none()
&& st.doc.selection.is_some()
&& st.selected_layers().len() <= 1