Alt+right-drag resizes the brush and changes its hardness (#297) (#370) (#370)

Merged with #360's right-click layer menu: Cmd/Ctrl+right-click lists layers, Alt+right-drag resizes, plain right-click keeps the brush picker / erase.

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Brandon ThomasandClaude Opus 5.5 authored and GitHub committed 2026-10-06 18:22:35 -04:00
1 parent fbcd0c238e
commit 0940f82d34
5 files changed
+216 -21

No files matched your search

+175 -6
View File
@@ -1,5 +1,6 @@
//! Photoshop's on-canvas brush resize (#231): with a painting tool, Control+Alt+drag (Windows,
//! Linux) or Control+Option+drag (macOS) changes the brush instead of painting. Left and right
//! Photoshop's on-canvas brush resize (#231, #297): with a painting tool, Alt+right-drag
//! (Windows, Linux) or Control+Option+drag (macOS; Control+Alt+drag works everywhere) changes the
//! brush instead of painting. Left and right
//! set the diameter (the circle's edge follows the pointer), up and down the hardness (down makes
//! it harder). The brush circle stays where the drag began, tinted to show the hardness, with a
//! Diameter/Hardness readout beside the pointer.
@@ -31,6 +32,8 @@ pub struct Resize {
pub start: (f32, f32),
/// The `coalesce` key of this drag's `tools.setBrush` calls.
key: u64,
/// Begun by an Alt+right-drag (#297): it ends when the right button is up.
pub secondary: bool,
}
/// Tools the gesture resizes: every painting tool, and Quick Selection (it has a brush too).
@@ -43,6 +46,12 @@ pub fn is_gesture(mods: egui::Modifiers) -> bool {
mods.ctrl && mods.alt
}
/// Alt (Option) with the right button: Photoshop's Windows gesture (#297). The left button with
/// Alt alone stays the painting tools' eyedropper.
pub fn is_right_gesture(mods: egui::Modifiers) -> bool {
mods.alt
}
/// The diameter and hardness a drag from `start` by (`dx`, `dy`) document pixels gives at `zoom`.
pub fn resized(start: (f32, f32), dx: f64, dy: f64, zoom: f32) -> (f32, f32) {
let dx = if dx.is_finite() { dx as f32 } else { 0.0 };
@@ -55,16 +64,19 @@ pub fn resized(start: (f32, f32), dx: f64, dy: f64, zoom: f32) -> (f32, f32) {
}
/// Route a tool event: true when it belongs to a resize drag (begun here with Control+Alt held,
/// or already in progress), so the tool never sees it.
pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool {
/// or an Alt+right-drag `armed` for this press, or already in progress), so the tool never sees
/// it.
pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers, armed: bool) -> bool {
static KEY: AtomicU64 = AtomicU64::new(0);
match ev {
ToolEvent::Down { x, y, .. } => {
if app.drag.is_some() || !applies(app.ui.tool) || !is_gesture(mods) {
// A new press ends any resize whose release was never seen.
app.brush_resize = None;
if app.drag.is_some() || !applies(app.ui.tool) || !(armed || is_gesture(mods)) {
return false;
}
let b = &app.session.tools.brush;
app.brush_resize = Some(Resize { anchor: [x, y], start: (b.size, b.hardness), key: KEY.fetch_add(1, Ordering::Relaxed) });
app.brush_resize = Some(Resize { anchor: [x, y], start: (b.size, b.hardness), key: KEY.fetch_add(1, Ordering::Relaxed), secondary: armed });
true
}
ToolEvent::Move { x, y, .. } => match app.brush_resize {
@@ -84,6 +96,14 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) ->
}
}
/// End an Alt+right-drag resize whose button is no longer down (its release went elsewhere, e.g.
/// outside the window), so it can't swallow the next stroke.
pub fn release_stale(app: &mut PhotocraftApp, secondary_down: bool) {
if !secondary_down && app.brush_resize.is_some_and(|r| r.secondary) {
app.brush_resize = None;
}
}
fn update(app: &mut PhotocraftApp, r: Resize, x: f64, y: f64) {
// Right is bigger on screen, also in a flipped view (View › Flip Horizontal).
let dx = (x - r.anchor[0]) * if app.ui.view.flip_horizontal { -1.0 } else { 1.0 };
@@ -271,4 +291,153 @@ mod tests {
assert!(!h.state().session.journal.iter().any(|(id, _)| id == "paint.stroke"), "nothing painted");
assert_eq!(history(h.state()), undo0, "no undo step");
}
fn canvas(a: PhotocraftApp) -> Harness<'static, PhotocraftApp> {
let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
let ctx = ui.ctx().clone();
if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
return;
}
egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
},
a,
);
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default());
h.run_steps(4);
h
}
fn button(h: &mut Harness<'static, PhotocraftApp>, pos: Pos2, button: PointerButton, pressed: bool, modifiers: Modifiers) {
h.event(egui::Event::PointerButton { pos, button, pressed, modifiers });
h.run_steps(2);
}
/// Alt+right-drag from the canvas centre, six steps of `step` screen points.
fn alt_right_drag(h: &mut Harness<'static, PhotocraftApp>, step: egui::Vec2) -> Pos2 {
let c = h.state().last_canvas_rect.center();
h.event(egui::Event::ModifiersChanged(Modifiers::ALT));
h.event(egui::Event::PointerMoved(c));
h.run_steps(1);
button(h, c, PointerButton::Secondary, true, Modifiers::ALT);
let mut p = c;
for _ in 0..6 {
p += step;
h.event(egui::Event::PointerMoved(p));
h.run_steps(1);
}
p
}
#[test]
fn alt_right_drag_resizes_with_the_readout_and_never_paints() {
for erase_pref in [false, true] {
let mut a = app(Tool::Brush);
if erase_pref {
a.run("prefs.set", json!({"path": "tools.rightClickWithPaintingTools", "value": "erase"})).unwrap();
}
a.sync_views();
let mut h = canvas(a);
let (undo0, size0) = (history(h.state()), h.state().session.tools.brush.size);
let end = alt_right_drag(&mut h, vec2(10.0, 8.0));
let r = h.state().brush_resize.expect("resizing");
assert!(r.secondary);
assert!(h.state().session.tools.brush.size > size0, "right grows the brush");
assert!(h.state().session.tools.brush.hardness > 0.2, "down hardens it");
assert!(h.ctx.memory(|m| m.area_rect(egui::Id::new("brush-resize-readout"))).is_some(), "the readout shows");
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::None);
button(&mut h, end, PointerButton::Secondary, false, Modifiers::ALT);
h.event(egui::Event::ModifiersChanged(Modifiers::NONE));
h.run_steps(2);
let app = h.state();
assert!(app.brush_resize.is_none() && !app.brush_resize_armed && app.ui.brush_picker.is_none());
assert!(!app.session.journal.iter().any(|(id, _)| id == "paint.stroke"), "nothing painted or erased (erase pref {erase_pref})");
assert_eq!(history(app), undo0, "no undo step");
let sets = app.session.journal.iter().filter(|(id, _)| id == "tools.setBrush").count();
assert_eq!(sets, 2, "the setup call plus one coalesced resize");
}
}
#[test]
fn plain_right_click_still_opens_the_picker() {
let mut a = app(Tool::Brush);
a.sync_views();
let mut h = canvas(a);
let c = h.state().last_canvas_rect.center();
h.event(egui::Event::PointerMoved(c));
h.run_steps(1);
button(&mut h, c, PointerButton::Secondary, true, Modifiers::NONE);
button(&mut h, c, PointerButton::Secondary, false, Modifiers::NONE);
assert!(h.state().ui.brush_picker.is_some() && h.state().brush_resize.is_none());
// ⌥ alone on the left button stays the eyedropper, not a resize.
assert!(!is_gesture(Modifiers::ALT) && is_right_gesture(Modifiers::ALT));
}
#[test]
fn a_missed_release_never_leaves_the_resize_stuck() {
let mut a = app(Tool::Brush);
a.sync_views();
let mut h = canvas(a);
// An Alt+right resize whose button-up never reached the canvas (released over another
// window, focus lost): the next canvas frame sees the right button up and ends it.
h.state_mut().brush_resize_armed = true;
tool_event(h.state_mut(), ToolEvent::Down { x: 100.0, y: 100.0, pressure: 1.0 }, Modifiers::NONE);
assert!(h.state().brush_resize.is_some_and(|r| r.secondary));
h.run_steps(2);
assert!(h.state().brush_resize.is_none(), "released when the right button is up");
// A plain left drag paints again.
let c = h.state().last_canvas_rect.center();
h.event(egui::Event::WindowFocused(true));
h.event(egui::Event::PointerMoved(c));
h.run_steps(1);
button(&mut h, c, PointerButton::Primary, true, Modifiers::NONE);
for k in 1..=5 {
h.event(egui::Event::PointerMoved(c + vec2(12.0 * k as f32, 0.0)));
h.run_steps(1);
}
button(&mut h, c + vec2(60.0, 0.0), PointerButton::Primary, false, Modifiers::NONE);
assert!(h.state().session.journal.iter().any(|(id, _)| id == "paint.stroke"), "the stroke paints");
}
#[test]
fn an_armed_press_consumed_by_another_handler_disarms() {
// Free Transform is active: it takes every press, including an armed Alt+right one.
let mut a = app(Tool::Brush);
a.run("layer.new.layer", json!({})).unwrap();
a.run("edit.fill", json!({"color": [1.0, 0.0, 0.0, 1.0]})).unwrap();
let ctx = egui::Context::default();
crate::transform_tool::begin(&mut a, &ctx).unwrap();
assert!(a.ui.transform.is_some());
a.brush_resize_armed = true;
tool_event(&mut a, ToolEvent::Down { x: 50.0, y: 50.0, pressure: 1.0 }, Modifiers::ALT);
tool_event(&mut a, ToolEvent::Up { x: 50.0, y: 50.0 }, Modifiers::ALT);
assert!(!a.brush_resize_armed, "the armed flag never outlives its press");
assert!(a.brush_resize.is_none());
crate::transform_tool::cancel(&mut a);
assert!(a.ui.transform.is_none());
// The next plain press paints rather than resizing.
tool_event(&mut a, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 }, Modifiers::NONE);
assert!(a.brush_resize.is_none());
tool_event(&mut a, ToolEvent::Move { x: 60.0, y: 20.0, pressure: 1.0 }, Modifiers::NONE);
tool_event(&mut a, ToolEvent::Up { x: 60.0, y: 20.0 }, Modifiers::NONE);
assert!(a.session.journal.iter().any(|(id, _)| id == "paint.stroke"));
}
#[test]
fn agents_resize_with_an_alt_right_pointer_drag() {
let mut a = app(Tool::Brush);
let ctx = egui::Context::default();
let events = json!([{"kind": "down", "x": 100, "y": 100}, {"kind": "move", "x": 120, "y": 100}, {"kind": "up", "x": 130, "y": 100}]);
let (req, _rx) = crate::control::ControlRequest::new("ui.pointer", json!({"button": "right", "alt": true, "events": events}));
let _ = crate::control::handle(&mut a, &ctx, &req);
assert_eq!(a.session.tools.brush.size, 100.0, "40 px + 2 × 30");
assert!(a.brush_resize.is_none() && !a.brush_resize_armed);
assert!(!a.session.journal.iter().any(|(id, _)| id == "paint.stroke"));
// Without Alt, a right press with the Brush opens the picker and paints nothing.
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!({"button": "right", "events": events}));
let _ = crate::control::handle(&mut a, &ctx, &req);
assert_eq!(a.session.tools.brush.size, 100.0);
assert!(a.ui.brush_picker.is_some());
}
}
+6 -2
View File
@@ -2069,6 +2069,9 @@ fn tool_move(app: &mut PhotocraftApp, x: f64, y: f64, pressure: f32, mods: egui:
/// Tool state machine. Shared by mouse input and automation.
pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) {
// An Alt+right-drag armed for this press (`paint_mouse`): taken before anything else can
// consume the event, so it never outlives the press it was armed for (#297).
let armed = std::mem::take(&mut app.brush_resize_armed);
// View › Snap / Snap To and smart guides (snap_ui.rs).
let raw = ev;
let ev = crate::snap_ui::filter_event(app, ev, mods);
@@ -2080,8 +2083,9 @@ 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);
// Control+Alt-drag with a painting tool resizes the brush instead of painting (#231).
if crate::brush_resize::pointer(app, ev, mods) {
// 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) {
return;
}
// Move tool: ⇧ locks the axis, ⌥ duplicates (move_mods.rs).
+2 -1
View File
@@ -350,7 +350,8 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest
}
continue;
}
if matches!(s("button"), Some("secondary" | "right")) && !crate::paint_mouse::pointer_secondary(app, matches!(ev, ToolEvent::Down { .. })) {
if matches!(s("button"), Some("secondary" | "right")) && !crate::paint_mouse::pointer_secondary(app, matches!(ev, ToolEvent::Down { .. }), mods)
{
continue;
}
// A simulated pen: tilt/rotation reach the stroke like a real stylus's (see `stylus`).
+4
View File
@@ -244,6 +244,9 @@ 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>,
/// 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,
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).
@@ -368,6 +371,7 @@ impl PhotocraftApp {
defer_live_stroke: false,
last_stroke_end: None,
brush_resize: None,
brush_resize_armed: false,
control_rx: None,
pending_screenshots: Vec::new(),
queued_screenshots: Vec::new(),
+29 -12
View File
@@ -58,12 +58,23 @@ pub fn right_erases(app: &PhotocraftApp, tool: Tool) -> bool {
matches!(tool, Tool::Brush | Tool::Eraser) && app.session.prefs().tools.right_click_with_painting_tools == RightClickPaint::Erase
}
/// Route the canvas response's buttons: the left one drives the tool; the right one erases (Erase
/// preference) or opens the Brush Preset picker. Arms `secondary_erase` for this frame's `Down`.
/// Route the canvas response's buttons: the left one drives the tool; the right one resizes the
/// brush with Alt held (#297), erases (Erase preference) or opens the Brush Preset picker. Arms
/// `secondary_erase` or `brush_resize_armed` for this frame's `Down`.
pub fn canvas_buttons(app: &mut PhotocraftApp, response: &Response, tool: Tool) -> Buttons {
let (mods, right_down) = response.ctx.input(|i| (i.modifiers, i.pointer.secondary_down()));
// Alt+right-drag resizes the brush (brush_resize.rs); its events reach `tool_event` like a
// left drag's, and nothing paints. A resize whose release was missed ends here.
crate::brush_resize::release_stale(app, right_down || response.drag_stopped_by(PointerButton::Secondary));
let resize_start = crate::brush_resize::applies(tool)
&& app.drag.is_none()
&& crate::brush_resize::is_right_gesture(crate::workspace_ui::sticky_mods(app, mods))
&& response.drag_started_by(PointerButton::Secondary);
let resizing = app.brush_resize.is_some_and(|r| r.secondary);
app.brush_resize_armed = resize_start;
// ⌘/Ctrl+right-click lists the layers under the pointer instead (layer_pick_ui.rs, #307).
let layer_menu = crate::layer_pick_ui::is_gesture(tool, response.ctx.input(|i| i.modifiers));
let erase = right_erases(app, tool) && !layer_menu;
let layer_menu = crate::layer_pick_ui::is_gesture(tool, mods);
let erase = right_erases(app, tool) && !resize_start && !resizing && !layer_menu;
let right_stroke = erase && app.drag.is_some();
let right_start = erase && response.drag_started_by(PointerButton::Secondary);
let right_click = response.secondary_clicked();
@@ -77,19 +88,25 @@ pub fn canvas_buttons(app: &mut PhotocraftApp, response: &Response, tool: Tool)
}
let erase_click = erase && right_click;
app.secondary_erase = right_start || erase_click;
let right_drag = right_stroke || resizing;
Buttons {
started: response.drag_started_by(PointerButton::Primary) || right_start,
dragged: response.dragged_by(PointerButton::Primary) || (right_stroke && response.dragged_by(PointerButton::Secondary)),
stopped: response.drag_stopped_by(PointerButton::Primary) || (right_stroke && response.drag_stopped_by(PointerButton::Secondary)),
started: response.drag_started_by(PointerButton::Primary) || right_start || resize_start,
dragged: response.dragged_by(PointerButton::Primary) || (right_drag && response.dragged_by(PointerButton::Secondary)),
stopped: response.drag_stopped_by(PointerButton::Primary) || (right_drag && response.drag_stopped_by(PointerButton::Secondary)),
clicked: response.clicked() || erase_click,
}
}
/// `ui.pointer` with `"button": "secondary"`: true when its events should reach the tool (an
/// erasing right stroke; `secondary_erase` is armed for its `Down`). Otherwise a right-click with
/// a painting tool opens the Brush Preset picker over the canvas, and nothing paints.
pub fn pointer_secondary(app: &mut PhotocraftApp, down: bool) -> bool {
/// Alt+right-drag brush resize, `brush_resize_armed` for its `Down`; or an erasing right stroke,
/// `secondary_erase` armed for its `Down`). Otherwise a right-click with a painting tool opens
/// the Brush Preset picker over the canvas, and nothing paints.
pub fn pointer_secondary(app: &mut PhotocraftApp, down: bool, mods: egui::Modifiers) -> bool {
let tool = app.ui.tool;
if crate::brush_resize::applies(tool) && (crate::brush_resize::is_right_gesture(mods) || app.brush_resize.is_some_and(|r| r.secondary)) {
app.brush_resize_armed = down && app.drag.is_none();
return true;
}
if right_erases(app, tool) {
app.secondary_erase = down;
return true;
@@ -284,7 +301,7 @@ mod tests {
app.run("prefs.set", json!({"path": "tools.rightClickWithPaintingTools", "value": "erase"})).unwrap();
app.ui.tool = Tool::Brush;
let m = Modifiers::NONE;
assert!(pointer_secondary(&mut app, true));
assert!(pointer_secondary(&mut app, true, egui::Modifiers::NONE));
tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 40.0, pressure: 0.4 }, m);
assert!(!app.secondary_erase, "armed for one stroke only");
tool_event(&mut app, ToolEvent::Move { x: 100.0, y: 40.0, pressure: 0.8 }, m);
@@ -297,7 +314,7 @@ mod tests {
assert_eq!(st.doc.layers[0].surface().unwrap().rgba(50, 40), [1.0, 1.0, 1.0, 1.0]);
// With the default preference the right button opens the picker and nothing paints.
app.run("prefs.set", json!({"path": "tools.rightClickWithPaintingTools", "value": "brushPicker"})).unwrap();
assert!(!pointer_secondary(&mut app, true));
assert!(!pointer_secondary(&mut app, true, egui::Modifiers::NONE));
assert!(app.ui.brush_picker.is_some() && !app.secondary_erase);
}