From 9366cd58ec2d889a96033d1bd3e80befd0bdfd99 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 27 Oct 2026 23:47:05 +0000 Subject: [PATCH] Refactor UI canvas for strict Clippy checks --- crates/ui-egui/src/canvas.rs | 2621 +++++++++-------- crates/ui-egui/src/canvas/cursors.rs | 2 +- crates/ui-egui/src/canvas/display.rs | 2 +- crates/ui-egui/src/canvas/drag.rs | 2 +- crates/ui-egui/src/canvas/filter_preview.rs | 4 +- crates/ui-egui/src/canvas/gesture.rs | 2 +- .../ui-egui/src/canvas/gpu_canvas/budget.rs | 2 +- .../ui-egui/src/canvas/gpu_canvas/callback.rs | 2 +- .../src/canvas/gpu_canvas/doc_textures.rs | 2 +- crates/ui-egui/src/canvas/gpu_canvas/perf.rs | 2 +- .../src/canvas/gpu_canvas/resources.rs | 2 +- crates/ui-egui/src/canvas/gpu_canvas/setup.rs | 2 +- .../ui-egui/src/canvas/gpu_canvas/texels.rs | 2 +- crates/ui-egui/src/canvas/navigator.rs | 2 +- crates/ui-egui/src/canvas/paint.rs | 2 +- crates/ui-egui/src/canvas/selection.rs | 2 +- crates/ui-egui/src/canvas/start_screen.rs | 2 +- crates/ui-egui/src/canvas/strokes.rs | 2 +- crates/ui-egui/src/canvas/tabs.rs | 2 +- crates/ui-egui/src/canvas/tests.rs | 6 +- crates/ui-egui/src/canvas/textures.rs | 2 +- crates/ui-egui/src/canvas/tool_input.rs | 235 +- .../ui-egui/src/canvas/transform_controls.rs | 2 +- crates/ui-egui/src/canvas/view_xform.rs | 2 +- 24 files changed, 1608 insertions(+), 1298 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 6912a149e6..bfddadad51 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -201,6 +201,1393 @@ pub fn mode_label(doc: &Document) -> &'static str { } /// Draw one canvas view and handle its input. `primary` = main window (tools active). +struct CanvasPaintInput<'a> { + ctx: &'a egui::Context, + rect: Rect, + painter: &'a egui::Painter, + ppp: f32, + border: photocraft_engine::prefs::CanvasBorder, + doc: &'a std::sync::Arc, + idx: usize, + output: Option, + size: [u32; 2], + primary: bool, + flip: bool, + aspect: f32, + render_zoom: f32, + target_xf: ViewXform, + pixel_grid: bool, +} + +struct SelectionPaintInput<'a> { + ctx: &'a egui::Context, + painter: &'a egui::Painter, + ui: &'a egui::Ui, + view: &'a View, + xf: ViewXform, + rect: Rect, + doc: &'a Document, + idx: usize, +} + +fn paint_selection_outline(app: &mut PhotocraftApp, input: SelectionPaintInput<'_>) { + // Select and Mask outlines its refined selection (on its preview proxy, reduced by `k`) in + // the Marching Ants view and hides the ants in the others. + let refined = crate::select_mask_view::ants(app, input.idx); + // A floating piece's ants hug the pixels being moved. + let floated = floating_outline(app, input.idx); + let (outlined, k) = match &refined { + None => (floated.as_ref().or(input.doc.selection.as_ref()), 1), + Some(None) => (None, 1), + Some(Some((proxy, k))) => (proxy.selection.as_ref(), (*k).max(1)), + }; + // Selection outline: true boundary, animated marching ants (cached per revision). + if let Some(sel) = outlined.filter(|_| app.ui.view.shows(app.ui.view.show.selection_edges) && !polygon_replaces_selection(app)) { + // Trace at display resolution over the visible part only; key by the mask's tile identity + // (not the document revision) so unrelated edits don't re-trace it. The step is in + // device pixels per document pixel (`View::zoom`, not the point zoom), so a 1 px + // selection still traces at 100% on a scaled display. + let step = (1.0 / (input.view.zoom * k as f32).max(1e-3)).log2().floor().exp2().clamp(1.0, 64.0) as u32; + let corners = [input.rect.min, pos2(input.rect.max.x, input.rect.min.y), input.rect.max, pos2(input.rect.min.x, input.rect.max.y)]; + let docs = [input.xf.to_doc(corners[0]), input.xf.to_doc(corners[1]), input.xf.to_doc(corners[2]), input.xf.to_doc(corners[3])]; + let mut x0 = docs[0][0]; + let mut y0 = docs[0][1]; + let mut x1 = x0; + let mut y1 = y0; + for d in docs { + x0 = x0.min(d[0]); + y0 = y0.min(d[1]); + x1 = x1.max(d[0]); + y1 = y1.max(d[1]); + } + let (x0, y0, x1, y1) = (x0 / f64::from(k), y0 / f64::from(k), x1 / f64::from(k), y1 / f64::from(k)); + let q = 256 * step as i32; // quantise the region so small pans reuse the cache + // The view centre can be any finite number (`ui.set`, #980). Past ±2³⁰ there is no document + // to outline, and the padding below (at most 2 · 256 · 64) can't overflow i32 from there. + let px = |v: f64| v.clamp(-f64::from(1 << 30), f64::from(1 << 30)) as i32; + // A Free Transform carries the selection to wherever its box goes (#5297), so all of + // it is outlined: the part now in view may have started out of it. + let carried = app.session.active_index() == Some(input.idx) && crate::transform_tool::carries_selection_of(app, input.doc.id); + let vis = if carried { + photocraft_geom::Rect::new(-(1 << 30), -(1 << 30), 1 << 30, 1 << 30) + } else { + photocraft_geom::Rect::new( + px(x0.floor()).div_euclid(q) * q - q, + px(y0.floor()).div_euclid(q) * q - q, + (px(x1.ceil()).div_euclid(q) + 2) * q, + (px(y1.ceil()).div_euclid(q) + 2) * q, + ) + }; + let bounds_key = if k > 1 { u64::MAX / 2 - input.doc.id.0 } else { u64::MAX - input.doc.id.0 }; + let carried_bounds = carried.then(|| app.cached_bounds(bounds_key, sel).intersect(&vis)); + let step = carried_bounds.map_or(step, |b| crate::outline::bounded_step(b, step)); + let key = crate::surface_fingerprint(sel) + ^ u64::from(step).rotate_left(56) + ^ input.doc.id.0.rotate_left(17) + ^ (vis.x0 as u64) << 8 + ^ (vis.y0 as u64) << 24 + ^ (vis.x1 as u64) << 36 + ^ (vis.y1 as u64) << 48 + ^ u64::from(k).rotate_left(5); + let fresh = matches!(&app.outline_cache, Some((d, k, _)) if *d == input.doc.id && *k == key); + if !fresh { + let t0 = crate::gpu_canvas::now_ms(); + let b = carried_bounds.unwrap_or_else(|| app.cached_bounds(bounds_key, sel).intersect(&vis)); + // Filters can occupy every worker in Rayon's global pool. A parallel scan here would + // block the UI thread waiting for those workers, preventing job cancellation (#1554). + let mut segs = + if app.session.has_jobs() { crate::outline::outline_scaled_serial(sel, b, step) } else { crate::outline::outline_scaled(sel, b, step) }; + if k > 1 { + let s = i32::try_from(k).unwrap_or(1); + let up = |p: [i32; 2]| [p[0].saturating_mul(s), p[1].saturating_mul(s)]; + segs.iter_mut().for_each(|(a, b)| (*a, *b) = (up(*a), up(*b))); + } + app.perf.span("outline", crate::gpu_canvas::now_ms() - t0); + app.outline_cache = Some((input.doc.id, key, std::sync::Arc::new(segs))); + } + // Under Content-Aware Move's Transform On Drop box the ants are drawn where it puts them. + if let Some((_, _, segs)) = app.outline_cache.as_ref().filter(|_| !crate::cam_transform::active(app)) { + let time = input.ui.input(|i| i.time); + // A floating piece (and a selection being dragged) draws its ants where it is shown. + let mut shown = input.xf; + if let Some((dx, dy)) = selection_shown_offset(app) { + shown.center = [input.xf.center[0] - dx as f32, input.xf.center[1] - dy as f32]; + } + // Under a Free Transform box they are where the box puts them (#5297). + if !(carried && crate::transform_tool::draw_selection(app, input.painter, &input.xf, input.doc.id, segs, time)) { + marching_ants_segments(input.painter, &shown, segs, time); + } + input.ctx.request_repaint_after(std::time::Duration::from_millis(100)); + } + } +} + +fn paint_canvas(app: &mut PhotocraftApp, ui: &egui::Ui, view: &View, xf: ViewXform, input: &CanvasPaintInput<'_>) -> (bool, egui::layers::ShapeIdx, Rect) { + let ctx = input.ctx; + let rect = input.rect; + let painter = input.painter; + let ppp = input.ppp; + let border = input.border; + let doc = input.doc; + let idx = input.idx; + let output = input.output; + let size = input.size; + let primary = input.primary; + let flip = input.flip; + let aspect = input.aspect; + let render_zoom = input.render_zoom; + let target_xf = input.target_xf; + let pixel_grid = input.pixel_grid; + + let point_zoom = view.zoom / ppp; + let drop_shadow = border == photocraft_engine::prefs::CanvasBorder::DropShadow; + // Drop shadow, checkerboard, document image. + let img_rect = xf.doc_rect(doc.bounds()); + // The canvas edge the Line border outlines: a size-changing preview's own (CPU canvas). + let mut edge_rect = img_rect; + // The document area the image is drawn over: a size-changing preview's own on the CPU + // canvas (Image › Rotation). Overlays drawn from the same preview follow it. + let mut shown_area = doc.bounds(); + // Crop tool: the layers' pixels past the canvas while a frame is edited, under the canvas + // (whose edge blends into them) and without its shadow. + let beyond = draw_beyond_canvas(app, painter, &xf, idx, output); + // View › Pattern Preview (#1067): copies of the document around it, from the same texture. + let pattern = if app.ui.view.pattern_preview { + let v = visible_doc_rect(&xf); + crate::pattern_preview::copies([v.x0, v.y0, v.x1, v.y1].map(f64::from), size) + } else { + Vec::new() + }; + let drop_shadow = drop_shadow && !beyond && !app.ui.view.pattern_preview; + // Live adjustment previews on big documents use a downsampled proxy (see proxy.rs), unless + // Preferences › Performance › Low Resolution Previews is off. + let mut on_gpu = false; + // A flipped or pixel-aspect-corrected view draws through the CPU path (the GPU canvas shader + // has no mirroring or non-square pixels). Rotation stays on the GPU: it is a view uniform. + let gpu_ok = crate::gpu_canvas::gpu_view_ok(flip, view.rotation, aspect); + if app.gpu.is_some() + && gpu_ok + && let Some((k, key, preview_size)) = + ensure_adjust_proxy(app, idx, render_zoom).or_else(|| ensure_filter_preview(app, idx, ctx)).or_else(|| ensure_proxy_preview(app, idx, render_zoom)) + { + on_gpu = true; + app.navigator_gpu.remove(&doc.id); + let original_size = [doc.size.width.div_ceil(k), doc.size.height.div_ceil(k)]; + let shown = filter_preview_display_doc(app, doc, key); + let params = crate::gpu_canvas::ViewParams { + doc: key, + doc_size: preview_size, + zoom: view.zoom * k as f32 / ppp, + // A size-changing preview grows around the old image center; keep the view's pan. + center: [ + xf.center[0] / k as f32 + (preview_size[0] as f32 - original_size[0] as f32) / 2.0, + xf.center[1] / k as f32 + (preview_size[1] as f32 - original_size[1] as f32) / 2.0, + ], + rotation: if view.rotation.is_finite() { view.rotation.to_radians() } else { 0.0 }, + shadow: { + let t = crate::theme::Tokens::get(ctx); + !t.bevel && !t.pro && drop_shadow + }, + pixel_grid: false, + view_key: egui::Id::unique(("pc-canvas-proxy", ctx.viewport_id(), idx)).value(), + display: sync_display_lut(app, &shown, key, output), + output: output.unwrap_or(0), + hdr: hdr_preview(app, doc), + offset: [0.0, 0.0], + }; + paint_gpu_view(painter, rect, params, &pattern); + } else if gpu_ok && ensure_gpu(app, ctx, idx, redraw_focus(&target_xf, &xf)) { + on_gpu = true; + app.perf.gpu = true; + // Shadow, checkerboard, document and pixel grid in one custom shader (gpu_canvas.rs). + let params = crate::gpu_canvas::ViewParams { + doc: doc.id.0, + doc_size: [doc.size.width, doc.size.height], + zoom: point_zoom, + center: xf.center, + rotation: if view.rotation.is_finite() { view.rotation.to_radians() } else { 0.0 }, + shadow: { + let t = crate::theme::Tokens::get(ctx); + !t.bevel && !t.pro && drop_shadow + }, + pixel_grid, + view_key: egui::Id::unique(("pc-canvas", ctx.viewport_id(), idx)).value(), + display: sync_display_lut(app, doc, doc.id.0, output), + output: output.unwrap_or(0), + hdr: hdr_preview(app, doc), + offset: [0.0, 0.0], + }; + if app.navigator_plain.contains(&doc.id) { + app.navigator_gpu.insert(doc.id, params); + } else { + app.navigator_gpu.remove(&doc.id); + } + paint_gpu_view(painter, rect, params, &pattern); + } else { + app.navigator_gpu.remove(&doc.id); + // A document too large for one full-resolution texture also gets the part in view at + // the screen's resolution, drawn over the reduced whole (#7568). + let in_view = + InView { visible: visible_doc_rect(&xf), zoom: view.zoom, key: egui::Id::unique(("pc-canvas-detail", ctx.viewport_id(), idx, primary)).value() }; + let textures = ensure_texture_in_view(app, ctx, idx, output, Some(in_view)); + let tex = textures.as_ref().map(|t| t.texture); + // The area the image covers: a size-changing preview's own (Image › Rotation). + let area = textures.as_ref().map_or(doc.bounds(), |t| t.bounds); + shown_area = area; + if area != doc.bounds() { + edge_rect = xf.doc_rect(area); + } + if !crate::theme::Tokens::get(ctx).bevel && drop_shadow { + let shadow_rect = edge_rect; + painter.add(egui::Shadow { offset: [0, 8], blur: 28, spread: 0, color: Color32::from_black_alpha(150) }.as_shape(shadow_rect, 0)); + } + let rotated = view.rotation.abs() > 0.05; + let checkerboard = app.session.prefs().transparency_and_gamut.square().map(|square| (checker(app, ctx), square)); + // Repeated-quad coverage is not proven identical on all Pattern Preview adapters. + // Retain its legacy checker submissions, including the central document's. + let opaque = !app.ui.view.pattern_preview && app.canvases.get(&cache_key(doc.id, output)).is_some_and(|c| c.texture_opaque) && tex.is_some(); + paint_cpu_document(painter, &xf, area, rotated, checkerboard, tex, opaque); + if let Some((detail, area)) = textures.and_then(|t| t.detail) { + paint_cpu_detail(painter, &xf, doc.bounds(), rotated, checkerboard, detail, area); + } + // Pattern Preview tiles what is shown: a size-changing preview's area, from its origin. + let (tile, copies) = if area == doc.bounds() || !app.ui.view.pattern_preview { + (size, pattern.clone()) + } else { + let tile = [area.width(), area.height()]; + let v = visible_doc_rect(&xf).translate(-area.x0, -area.y0); + (tile, crate::pattern_preview::copies([v.x0, v.y0, v.x1, v.y1].map(f64::from), tile)) + }; + for &c in &copies { + paint_cpu_document(painter, &pattern_xf(&xf, c, tile), area, rotated, checkerboard, tex, opaque); + } + } + // Channels panel: alpha / Quick Mask overlays and single-channel views (channel_view.rs). + if let Some(tex) = crate::channel_view::ensure(app, ctx, idx) { + // Rendered from the live preview, so over the area the preview is shown in. + let a = shown_area; + if view.rotation.abs() > 0.05 { + paint_mapped_image_at( + painter, + &xf, + tex, + Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)), + [a.x0 as f32, a.y0 as f32], + [a.width() as f32, a.height() as f32], + Color32::WHITE, + ); + } else { + let uv = if flip { Rect::from_min_max(pos2(1.0, 0.0), pos2(0.0, 1.0)) } else { Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)) }; + painter.image(tex, xf.doc_rect(a), uv, Color32::WHITE); + } + } + crate::color_range_ui::paint_selection_preview(app, ctx, painter, doc.id, img_rect, flip); + // Artboards: pasteboard between the boards, outlines and names (artboard_ui.rs). + if doc.has_artboards() { + let t = crate::theme::Tokens::get(ctx); + let pasteboard = crate::prefs_ui::pasteboard_color(app); + let dot_tex = dots(ctx, &t); + // Cut around the layers as they are shown, so a loose layer dragged with the Move tool + // (or otherwise previewed) is not covered where it now draws. + let shown = display_doc(app, idx).0; + // Each piece is painted as its own quad (the painter clips to the canvas), so a + // rotated view does not paint a piece's bounding box over the boards or loose work. + let fill = |q: &[Pos2; 4], tex: Option, color: Color32| { + let mut mesh = tex.map_or_else(egui::Mesh::default, egui::Mesh::with_texture); + for &p in q { + // Same phase as the dots around the document. + let uv = if tex.is_some() { ((p - rect.min) / 22.0).to_pos2() } else { egui::epaint::WHITE_UV }; + mesh.vertices.push(egui::epaint::Vertex { pos: p, uv, color }); + } + mesh.add_triangle(0, 1, 2); + mesh.add_triangle(0, 2, 3); + painter.add(mesh); + }; + for q in crate::artboard_ui::pasteboard_quads(&xf, &shown) { + if !Rect::from_points(&q).intersects(rect) { + continue; + } + match pasteboard { + Some(c) => fill(&q, None, c), + None => { + fill(&q, None, t.canvas); + if let Some(id) = dot_tex { + fill(&q, Some(id), Color32::WHITE); + } + } + } + } + if primary { + crate::artboard_ui::draw_frames(app, painter, &xf); + } + } + + // Pixel grid at high zoom, over pixels with content only (the GPU path draws its own). + if !on_gpu && pixel_grid && crate::pixel_grid::shows_at(view.zoom) { + crate::pixel_grid::paint(app, painter, &xf, idx, output, visible_doc_rect(&xf)); + } + + // Developer tile grid (`ui.set {"tileGrid": true}`): the active layer's storage tiles. + // Not tied to View › Extras (⌘H), which hides only the View › Show overlays. + if app.ui.view.tile_grid { + crate::tile_overlay::paint(app, ctx, painter, &xf, idx, visible_doc_rect(&xf)); + } + + // View › Show › Layer Edges: drawn at this depth, but only once this frame's tool events are + // in (`layer_edges_rect`), so a press that Auto-Selects a layer outlines that layer. + let layer_edges = painter.add(egui::Shape::Noop); + + paint_selection_outline(app, SelectionPaintInput { ctx, painter, ui, view, xf, rect, doc, idx }); + + (beyond, layer_edges, edge_rect) +} + +struct CanvasNavigationInput<'a> { + ctx: &'a egui::Context, + response: &'a egui::Response, + free_hover: bool, + wheel_owner: egui::Id, + xf: ViewXform, + ppp: f32, + idx: usize, +} + +fn update_canvas_navigation(app: &mut PhotocraftApp, view: &mut View, input: CanvasNavigationInput<'_>) { + // Navigation (wheel_nav.rs): scroll pans (⇧ or ⌘/Ctrl sideways); pinch and ⌥-scroll zoom around the pointer. + // The wheel also scrolls over the scrollbars drawn on top of the canvas (last frame's hover). + let bars_id = egui::Id::unique(("pc-canvas-bars-hover", input.idx)); + let over_bars = input.ctx.data(|d| d.get_temp::(bars_id)).unwrap_or(false); + let nav_hovered = input.response.hovered() || input.free_hover || over_bars; + // Interface › Enable Animations off: the wheel moves the canvas without a glide (#5721). + // General › Animated Zoom: the wheel zoom eases over the frames of a notch, or is one instant step. + // Enhanced Controls › Enable Gestures off: two fingers on a touch screen neither zoom nor + // turn the view (#204); the mouse and trackpad are unaffected. + let touch_gestures = app.session.prefs().enhanced_controls.touch_gestures; + let wheel = crate::wheel_nav::read_with( + input.ctx, + app.session.prefs().general.zoom_with_scroll_wheel, + app.session.prefs().enhanced_controls.zoom_with_trackpad_pinch, + app.session.prefs().general.animated_zoom, + input.wheel_owner, + nav_hovered, + app.session.prefs().interface.enable_animations, + touch_gestures, + ); + if nav_hovered { + let pointer = input.ctx.input(|i| i.pointer.hover_pos()); + match (wheel, pointer) { + (Some(crate::wheel_nav::Wheel::Zoom(f)), Some(p)) => { + // At a limit the view stays put, as in Photoshop: a notch past 12800 % neither + // zooms nor slides the image towards the pointer. + let nz = crate::zoom_levels::clamp(view.zoom * f, view.doc_size); + if nz != view.zoom * f { + crate::wheel_nav::settle(input.ctx, input.wheel_owner); + } + if nz != view.zoom { + // Over the pasteboard the anchor is the image's nearest point (#3720). + let at = wheel_anchor(&input.xf, p, view.doc_size); + zoom_about(view, &input.xf, at, nz, false, input.ppp); + } + } + (Some(crate::wheel_nav::Wheel::SnapZoom(n)), Some(p)) => { + let dir = if n > 0 { 1 } else { -1 }; + let mut nz = view.zoom; + for _ in 0..n.unsigned_abs() { + nz = crate::zoom_levels::step(nz, dir, view.doc_size); + } + if nz != view.zoom { + zoom_about(view, &input.xf, p, nz, false, input.ppp); + } + } + (Some(crate::wheel_nav::Wheel::Pan(scroll)), _) => { + let d = input.xf.unmap_vec(scroll) / (view.zoom / input.ppp); + view.center[0] -= d.x; + view.center[1] -= d.y; + } + _ => {} + } + let rotate_trackpad = app.session.prefs().enhanced_controls.rotate_view_with_trackpad; + if let Some(delta) = crate::rotate_view::trackpad_delta(input.ctx, rotate_trackpad, touch_gestures) { + view.zoom_animating = false; + view.zoom_anchor = None; + let next = view.rotation + crate::rotate_view::screen_delta(delta, input.xf.flip); + if next.is_finite() { + view.rotation = crate::rotate_view::wrap_deg(next); + } + } + } +} + +struct DialogCanvasInput<'a> { + ctx: &'a egui::Context, + view: &'a mut View, + xf: &'a ViewXform, + rect: Rect, + painter: &'a egui::Painter, + ppp: f32, + primary: bool, + middle: bool, + space_pan: bool, + temporary: Option, + free_hover: bool, + img_rect: Rect, +} + +fn handle_dialog_canvas_input(app: &mut PhotocraftApp, input: DialogCanvasInput<'_>) { + let sticky_alt = app.ui.shell.sticky_alt; + let zoom_out = |alt: bool| match input.temporary { + Some(crate::hold_keys::Temporary::ZoomOut) => true, + Some(crate::hold_keys::Temporary::ZoomIn) => false, + _ => alt || sticky_alt, + }; + + // With a colour dialog picker armed the image is its eyedropper, whatever the tool; Space + // and the middle button still pan. Curves and Replace Color use the same merged-composite + // sampler as the Color Picker, never the reduced adjustment preview. + let curves_picking = crate::adjust_dialog::picker_armed(app); + let replace_picking = crate::filter_dialog::color_eyedropper_armed(app); + let picking = input.primary && (crate::color_picker_ui::top(app).is_some() || curves_picking || replace_picking); + let range_picking = + input.primary && app.ui.dialogs.last().is_some_and(|d| crate::color_range_ui::owns(&d.fields) && crate::color_range_ui::controls(&d.fields).sampling); + // Select and Mask's Refine Edge Brush paints on the image. + let refine_brushing = + input.primary && app.ui.dialogs.last().is_some_and(|d| crate::select_mask_view::owns(&d.fields) && crate::select_mask_view::brush_on(&d.fields)); + // Field Blur's pins are placed and dragged on the image (`blur_pins`). + let pinning = input.primary && app.ui.dialogs.last().is_some_and(|d| crate::blur_pins::owns(&d.fields)); + // Layer Style on a Drop or Inner Shadow page: a drag on the image moves the shadow (#5701). + let shadow_drag = + !picking && app.ui.dialogs.last().is_some_and(|d| d.kind == crate::state::DialogKind::LayerStyle && crate::layer_style::drags_shadow(&d.fields)); + // ⌘/Ctrl-click with Curves open: a point at the pixel's tone (#7325), whatever the tool. The + // press's own modifiers decide, and only the press adds: a drag would add one per frame. + let curve_point_press = (input.primary && !input.middle && crate::adjust_dialog::curves_on_top(app)) + .then(|| { + input.ctx.input(|i| { + i.events.iter().rev().find_map(|e| match e { + egui::Event::PointerButton { button: PointerButton::Primary, pressed: true, modifiers, .. } => Some(*modifiers), + _ => None, + }) + }) + }) + .flatten() + .map(|m| crate::workspace_ui::sticky_mods(app, m)) + .filter(|m| m.command); + let dialog_hand = app.ui.tool == Tool::Hand && !picking && !range_picking && !refine_brushing && !pinning && !shadow_drag && curve_point_press.is_none(); + // ⌘Space / ⌥Space zoom under the dialog as they do without one, before Space pans (#6460). + // With the Hand tool, ⌘ alone is Zoom In (#6434); under Curves that press is the dialog's + // point (#7325), not a zoom. + let curve_point_with_hand = curve_point_press.is_some() && app.ui.tool == Tool::Hand; + let held_zoom = + !input.middle && !curve_point_with_hand && matches!(input.temporary, Some(crate::hold_keys::Temporary::ZoomIn | crate::hold_keys::Temporary::ZoomOut)); + if held_zoom { + let alt = input.ctx.input(|i| i.modifiers.alt); + if input.free_hover { + input.ctx.set_cursor_icon(crate::tool_cursor::zoom(input.ctx, zoom_out(alt))); + } + let b = crate::dialogs::free_buttons(input.ctx, input.rect); + let pointer = input.ctx.input(|i| i.pointer.latest_pos()); + crate::zoom_tool::drag(app, input.ctx, input.view, input.xf, &b, pointer); + if b.clicked + && let Some(p) = pointer + { + let nz = crate::zoom_levels::step(input.view.zoom, if zoom_out(alt) { -1 } else { 1 }, input.view.doc_size); + zoom_about(input.view, input.xf, p, nz, app.session.prefs().tools.zoom_clicked_point_to_center, input.ppp); + } + } else if let Some(d) = crate::dialogs::pan_delta(input.ctx, input.rect, dialog_hand) { + let d = input.xf.unmap_vec(d) / (input.view.zoom / input.ppp); + input.view.center[0] -= d.x; + input.view.center[1] -= d.y; + input.ctx.set_cursor_icon(crate::tool_cursor::hand(input.ctx, true)); + } else if input.free_hover && (input.space_pan || dialog_hand) { + input.ctx.set_cursor_icon(crate::tool_cursor::hand(input.ctx, false)); + } else if shadow_drag && !input.middle && crate::dialogs::free_pointer_over(input.ctx, input.rect).is_some() { + input.ctx.set_cursor_icon(egui::CursorIcon::Move); + if let Some(p) = crate::dialogs::free_press(input.ctx, input.rect) { + let (start, origin) = input.ctx.input(|i| (i.pointer.primary_pressed(), i.pointer.press_origin().unwrap_or(p))); + let total = input.xf.unmap_vec(p - origin) / (input.view.zoom / input.ppp); + if let Some(d) = app.ui.dialogs.last_mut() { + crate::layer_style::drag_shadow(&mut d.fields, start, [f64::from(total.x), f64::from(total.y)]); + } + } + } else if picking && let Some(p) = crate::dialogs::free_pointer_over(input.ctx, input.rect) { + if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise { + input.ctx.set_cursor_icon(egui::CursorIcon::Crosshair); + } else { + input.ctx.set_cursor_icon(pipette_cursor(input.ctx, p)); + } + if let Some(p) = crate::dialogs::free_press(input.ctx, input.rect) { + let d = input.xf.to_doc(p); + if curves_picking { + crate::adjust_dialog::sample_at(app, d[0], d[1]); + } else if replace_picking { + crate::filter_dialog::color_eyedropper_sample(app, d[0], d[1]); + } else { + crate::color_picker_ui::sample_at(app, d[0], d[1]); + } + } + } else if pinning + && !input.middle + && let Some(p) = crate::dialogs::free_pointer_over(input.ctx, input.rect) + { + let press = crate::dialogs::free_press(input.ctx, input.rect); + crate::blur_pins::canvas(app, input.ctx, input.xf, p, press); + } else if refine_brushing + && !input.middle + && let Some(p) = crate::dialogs::free_pointer_over(input.ctx, input.rect) + { + let press = crate::dialogs::free_press(input.ctx, input.rect).map(|q| input.xf.to_doc(q)); + crate::select_mask_view::canvas_brush(app, input.painter, p, press, input.xf.zoom_x()); + } else if let Some(mods) = curve_point_press + && let Some(p) = crate::dialogs::free_press(input.ctx, input.rect) + { + let d = input.xf.to_doc(p); + crate::adjust_dialog::add_point_at(app, input.ctx, d[0], d[1], mods.shift); + } else if input.primary + && !input.middle + && let Some(p) = crate::dialogs::free_pointer_over(input.ctx, input.rect).filter(|p| input.img_rect.contains(*p)) + { + // Color Range samples colours with its eyedropper on the image itself. + let press = crate::dialogs::free_press(input.ctx, input.rect) + .filter(|p| input.img_rect.contains(*p) && input.ctx.input(|i| i.pointer.primary_pressed() || i.pointer.delta() != egui::Vec2::ZERO)) + .map(|q| input.xf.to_doc(q)); + crate::color_range_ui::canvas_eyedropper(app, input.ctx, p, press); + } +} + +struct CanvasToolInput<'a> { + ctx: &'a egui::Context, + response: &'a egui::Response, + xf: &'a ViewXform, + rect: Rect, + full: Rect, + tool: Tool, + idx: usize, + primary: bool, + transform_owns_pointer: bool, + hand_pan: bool, + ppp: f32, + under_dialog: bool, + zoom_out: &'a dyn Fn(bool) -> bool, + display_zoom: f32, +} + +fn handle_primary_canvas_input(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &mut View, input: &CanvasToolInput<'_>) { + let ctx = input.ctx; + let response = input.response; + let xf = input.xf; + let rect = input.rect; + let tool = input.tool; + let idx = input.idx; + let under_dialog = input.under_dialog; + let mods = ui.input(|i| i.modifiers); + // An ⌥-click the window manager took never arrives; say so (alt_grab.rs). + crate::alt_grab::watch(app, &ctx, egui::Id::unique(("pc-alt-grab", idx)), rect, tool, !under_dialog); + // Tools follow the left button; the right one opens the Brush Preset picker or erases + // (Preferences › Tools, `paint_mouse`). + crate::paint_mouse::sync_tool_brush(app); + let mut buttons = crate::paint_mouse::canvas_buttons(app, &response, tool); + // The Crop tool's frame is edited from the press (before egui's drag threshold), so the + // pixels past the canvas show at once, as in Photoshop. + if tool == Tool::Crop && response.is_pointer_button_down_on() && ui.input(|i| i.pointer.primary_down()) && crate::crop_ui::press(app) { + ctx.request_repaint(); + } + // Capture intent at the press, before egui's drag threshold or click replay on release: + // Type transforms keep Command; selections keep Add/Subtract if Shift/Alt is released. + let capture_press = + tool.is_type() || matches!(tool, Tool::RectMarquee | Tool::EllipseMarquee | Tool::SingleRowMarquee | Tool::SingleColumnMarquee | Tool::ObjectSelection); + let tool_press = egui::Id::unique(("tool-pointer-press-modifiers", idx)); + if capture_press + && let Some((p, press_mods)) = ui.input(|i| { + i.events.iter().find_map(|e| match e { + egui::Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers } if rect.contains(*pos) => Some((*pos, *modifiers)), + _ => None, + }) + }) + { + let press_mods = crate::workspace_ui::sticky_mods(app, press_mods); + ctx.data_mut(|d| d.insert_temp(tool_press, press_mods)); + if tool.is_type() && press_mods.command && app.ui.text_edit.is_some() { + let d = xf.to_doc(p); + tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, press_mods); + } + } + + handle_canvas_tool_menus(app, input, mods, &mut buttons); + handle_canvas_tool_navigation(app, ui, view, input, &mut buttons); + handle_canvas_tool_stroke(app, ui, input, mods, capture_press, tool_press, &mut buttons); + handle_canvas_tool_clicks(app, ui, view, input, mods, tool_press, &buttons); +} + +fn handle_canvas_tool_menus(app: &mut PhotocraftApp, input: &CanvasToolInput<'_>, mods: egui::Modifiers, buttons: &mut crate::paint_mouse::Buttons) { + let ctx = input.ctx; + let response = input.response; + let xf = input.xf; + let tool = input.tool; + let transform_owns_pointer = input.transform_owns_pointer; + // Right-click while transforming: switch the box's mode (Free Transform, Scale, Rotate, + // Skew, Distort, Perspective, Warp), or rotate or flip the box. + let transforming = app.ui.transform.as_ref().is_some_and(|t| t.warp.is_none()); + if response.secondary_clicked() + && transforming + && let Some(p) = response.interact_pointer_pos() + { + crate::canvas_tool_menu::open_transform(app, &ctx, [p.x, p.y]); + } + let lasso_retracted = response.secondary_clicked() + && match tool { + Tool::Lasso | Tool::FreeformPen => crate::lasso_ui::undo_last_vertex(app), + Tool::PolygonLasso => polygon_retract(app), + _ => false, + }; + // The Lasso (and the Freeform Pen, which draws the same way) reads its own presses and + // drops the moves of a drag it didn't start, so an open Free Transform box takes the + // usual drag path instead (#2153). + if crate::lasso_ui::owns(tool) && !transform_owns_pointer { + crate::lasso_ui::canvas_input(app, &ctx, &xf, &response); + (buttons.started, buttons.dragged, buttons.stopped, buttons.clicked) = (false, false, false, false); + } + // Right-click on a guide where it could be dragged (the Move tool, or ⌘ with a selection + // tool): select it and open the guide menu, as Photoshop does. + let guide_menu = response.secondary_clicked() + && !lasso_retracted + && !transforming + && response.interact_pointer_pos().is_some_and(|p| crate::canvas_tool_menu::open_guide(app, &ctx, tool, mods, [p.x, p.y], xf.to_doc(p))); + // Right-click with the Move tool, or ⌘/Ctrl+right-click: the layers under the pointer. + if response.secondary_clicked() + && !lasso_retracted + && !transforming + && !guide_menu + && crate::layer_pick_ui::is_gesture(tool, mods) + && let Some(p) = response.interact_pointer_pos() + { + let d = xf.to_doc(p); + app.ui.canvas_tool_menu = None; + // No layers there (the pasteboard, with the Move tool): its colour menu instead. + if !crate::layer_pick_ui::open(app, &ctx, [p.x, p.y], d[0], d[1]) && !mods.command && crate::canvas_tool_menu::pasteboard_at(app, tool, d) { + crate::canvas_tool_menu::open_pasteboard(app, &ctx, tool, [p.x, p.y]); + } + } + if response.secondary_clicked() + && !lasso_retracted + && !transforming + && !guide_menu + && !crate::layer_pick_ui::is_gesture(tool, mods) + && let Some(p) = response.interact_pointer_pos() + { + // A pending crop: its menu, also outside the image, where the frame can reach (#6561). + // Outside the image: the pasteboard colour menu (#2462), else the tool's own. The view + // menu belongs to the selected Zoom or Hand, not to Space or a held zoom key. A crop + // frame or Straighten line still dragged by the left button gets no menu over it: the + // gesture and the held button both, as either alone can outlive a lost release. A + // finger held still (egui's press-and-hold right-click) is down too, but drags nothing. + let crop_gesture = tool == Tool::Crop + && !response.long_touched() + && (app.crop.drag.is_some() || app.crop.straighten.line.is_some()) + && ctx.input(|i| i.pointer.primary_down()); + if crop_gesture || crate::canvas_tool_menu::open_crop(app, &ctx, tool, [p.x, p.y]) { + } else if crate::canvas_tool_menu::pasteboard_at(app, tool, xf.to_doc(p)) { + crate::canvas_tool_menu::open_pasteboard(app, &ctx, tool, [p.x, p.y]); + } else if crate::canvas_tool_menu::is_view_tool(tool) { + if tool == app.ui.tool { + crate::canvas_tool_menu::open_view(app, &ctx, tool, [p.x, p.y], xf.to_doc(p)); + } + } else { + crate::canvas_tool_menu::open_at(app, &ctx, tool, [p.x, p.y], Some(xf.to_doc(p))); + } + } +} + +fn handle_canvas_tool_navigation( + app: &mut PhotocraftApp, + _ui: &mut egui::Ui, + view: &mut View, + input: &CanvasToolInput<'_>, + buttons: &mut crate::paint_mouse::Buttons, +) { + let ctx = input.ctx; + let response = input.response; + let xf = input.xf; + let tool = input.tool; + let idx = input.idx; + let primary = input.primary; + let transform_owns_pointer = input.transform_owns_pointer; + let hand_pan = input.hand_pan; + let ppp = input.ppp; + let zoom_out = input.zoom_out; + // The (temporary) Hand pans above; its gestures never reach the tool underneath. While a + // Free Transform box is open the Hand does not pan, so the box gets these events (#2302). + if hand_pan { + (buttons.started, buttons.dragged, buttons.stopped) = (false, false, false); + } + // Rotate View: drag around the view centre. Compass clicks are not a rotate drag. + let over_compass = crate::rotate_view::compass_visible(view.rotation) + && response.interact_pointer_pos().is_some_and(|p| crate::rotate_view::compass_rect(xf.rect).contains(p)); + if tool == Tool::RotateView && !over_compass && crate::rotate_view::drag(&ctx, view, &xf, &buttons, response.interact_pointer_pos()) { + (buttons.started, buttons.dragged, buttons.stopped, buttons.clicked) = (false, false, false, false); + } + // Animated Zoom: a Zoom press held still zooms continuously (zoom_hold.rs); while it does, + // it owns the press: no scrub, and its release is no click step. + if tool == Tool::Zoom && !transform_owns_pointer { + let out = zoom_out(_ui.input(|i| i.modifiers.alt)); + let on_canvas = false; + let hold = crate::zoom_hold::hold_id(idx, primary, ctx.viewport_id()); + if crate::zoom_hold::update(&ctx, view, &xf, app.session.prefs().general.animated_zoom, out, ppp, on_canvas, hold) { + (buttons.started, buttons.dragged, buttons.stopped, buttons.clicked) = (false, false, false, false); + } + } + // Zoom tool drags: scrubby zoom or a zoom rectangle (zoom_tool.rs); clicks step below. + // Like the Hand, the selected Zoom yields to an open Free Transform box (#4799). + if tool == Tool::Zoom && !transform_owns_pointer && crate::zoom_tool::drag(app, &ctx, view, &xf, &buttons, response.interact_pointer_pos()) { + (buttons.started, buttons.dragged, buttons.stopped) = (false, false, false); + } +} + +fn handle_canvas_tool_stroke( + app: &mut PhotocraftApp, + ui: &mut egui::Ui, + input: &CanvasToolInput<'_>, + mods: egui::Modifiers, + capture_press: bool, + tool_press: egui::Id, + buttons: &mut crate::paint_mouse::Buttons, +) { + let ctx = input.ctx; + let response = input.response; + let xf = input.xf; + let rect = input.rect; + let full = input.full; + let tool = input.tool; + // A drag is only recognised once the pointer has moved past egui's click distance: the + // gesture starts where the button went down, not where it is now (#123). + let gesture_active_before = app.drag.is_some(); + // Stroke timing: egui's events carry no timestamps, so a press takes the frame time and + // the moves below are spread between the previous point and this frame (`spread_ms`). + let now_ms = ui.input(|i| i.time) * 1000.0; + app.stylus.clock_ms = now_ms; + // Live painting tools start on the press, not once the pointer passes egui's click + // distance: the first dab shows at once. The drag recognised later continues that stroke, + // and a click (or a long press that never moved) just ends it. + let physical_right = app.services.right_button_down.as_ref().is_some_and(|f| f()); + let right_down = ui.input(|i| i.pointer.secondary_down()) || physical_right; + if strokes_live(tool) + && !right_down + && app.drag.is_none() + && response.is_pointer_button_down_on() + && ui.input(|i| i.pointer.primary_pressed()) + && let Some(p) = ui.input(|i| i.pointer.press_origin()).filter(|p| rect.contains(*p)) + { + let d = xf.to_doc(p); + // The press's own modifiers: a ⇧ that arrives with the click still connects the line. + let press_mods = ui.input(|i| pointer_button_modifiers(&i.events, PointerButton::Primary)).unwrap_or(mods); + tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, press_mods); + app.press_stroke = app.drag.is_some(); + } + let press_stroke = app.press_stroke; + // A live tool's press that went to the ⌥ eyedropper keeps sampling for the whole press: the + // drag recognised later must not decide again with ⌥ already let go and paint (#7802). + if press_stroke || (strokes_live(tool) && (app.alt_sampling || app.mixer_sampling)) { + buttons.started = false; + } + if buttons.started + && let Some(p) = ui.input(|i| i.pointer.press_origin()).filter(|p| rect.contains(*p)).or(response.interact_pointer_pos()) + { + if tool == Tool::Move && app.ui.transform.is_none() { + begin_transform_controls_at(app, &ctx, &xf, p); + } + let d = xf.to_doc(p); + let press_mods = if capture_press { ctx.data(|d| d.get_temp(tool_press)).unwrap_or(mods) } else { mods }; + tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, press_mods); + } + // A stroke started on the press takes its moves from the press on, not only once egui + // recognises a drag (past its click distance): otherwise the first few points are lost + // and the stroke starts with a straight segment and no pressure ramp. + let press_stroke_moving = press_stroke && app.drag.is_some(); + if buttons.dragged || buttons.stopped || press_stroke_moving { + // Feed every pointer move the OS delivered this frame, not just the latest position, so + // a fast stroke is sampled densely and renders as a smooth curve instead of a coarse + // polyline. egui-winit pushes one `PointerMoved` per `CursorMoved`, and they accumulate + // while a frame is slow, so reading them all recovers the moves a per-frame + // `interact_pointer_pos()` would drop. Only freehand tools take the whole batch; the + // rest follow the pointer's latest position. The moves are bounded to the gesture's own + // press..release interval (`pointer_moves`), so the start and stop frames keep their + // valid samples without leaking a move from outside the gesture. + let events = ui.input(|i| i.events.clone()); + let button = if response.dragged_by(PointerButton::Secondary) + || response.drag_started_by(PointerButton::Secondary) + || response.drag_stopped_by(PointerButton::Secondary) + { + PointerButton::Secondary + } else { + PointerButton::Primary + }; + let press_this_frame = events.iter().any(|e| matches!(e, egui::Event::PointerButton { button: b, pressed: true, .. } if *b == button)); + let down_at_start = !press_this_frame && (gesture_active_before || buttons.started); + let mut positions = if app.brush_resize.is_some() { + response.interact_pointer_pos().into_iter().collect() + } else if freehand_tool(tool) { + pointer_moves(&events, button, down_at_start) + } else if buttons.dragged { + response.interact_pointer_pos().into_iter().collect() + } else { + Vec::new() + }; + // A frame with no raw move still tracks a held gesture (a still pointer, a modifier + // change): fall back to the latest position, as the old code always did. + if positions.is_empty() + && (buttons.dragged || press_stroke_moving) + && let Some(p) = response.interact_pointer_pos().or(ui.input(|i| i.pointer.latest_pos())) + { + positions.push(p); + } + app.defer_live_stroke = true; + let base = app.stylus.last_point_ms().map_or(now_ms, |t| t.min(now_ms)); + let n = positions.len(); + for (k, p) in positions.into_iter().enumerate() { + app.stylus.clock_ms = spread_ms(base, now_ms, k, n); + // This move's share of the pen samples the frame brought (pressure, tilt, rotation). + app.stylus.select(k, n); + let d = xf.to_doc(p); + tool_event(app, ToolEvent::Move { x: d[0], y: d[1], pressure: app.stylus.pressure() }, mods); + } + app.stylus.clear_selection(); + app.stylus.clock_ms = now_ms; + // A held pen keeps the clock running for airbrush build-up and smoothing catch-up. + if app.drag.is_some() && stroke_wants_time(app) { + ctx.request_repaint_after(std::time::Duration::from_millis(HOLD_REPEAT_MS as u64)); + } + app.defer_live_stroke = false; + // One live-stroke update for the whole frame, not one per recovered sample. + feed_live_stroke(app); + crate::magnetic_lasso_ui::flush(app); + } + if buttons.stopped { + // The live painting press already created the stroke and this release commits it. + // egui may also report clicked on the same release; replaying that click paints a + // second full-pressure dab over the real (possibly light-pressure) pen dab. + if press_stroke { + buttons.clicked = false; + } + let at = response.interact_pointer_pos(); + // #6286: a guide let go over a ruler is removed, whichever ruler it is. Tested here in + // screen space, where the rulers are: on the guide's own axis the release can still be + // inside the document, which is what `finish_drag` sees. + if app.guide_drag.is_some() { + let on_ruler = at.is_some_and(|p| crate::rulers::dropped_on_a_ruler(app, full, p)); + if let Some(g) = app.guide_drag.as_mut() { + g.over_ruler = on_ruler; + } + } + let p = at.map(|p| xf.to_doc(p)).or_else(|| app.drag.as_ref().and_then(|d| d.points.last().map(|q| [q[0], q[1]]))); + if let Some(d) = p { + tap_at_peak_pressure(app); + tool_event(app, ToolEvent::Up { x: d[0], y: d[1] }, mods); + } + } + // A stroke started on the press ends with the release, whether egui saw a drag, a click, + // or neither (a long press that never moved). + if press_stroke && !buttons.stopped && ui.input(|i| i.pointer.primary_released()) { + buttons.clicked = false; + let p = response.interact_pointer_pos().map(|p| xf.to_doc(p)).or_else(|| app.drag.as_ref().and_then(|d| d.points.last().map(|q| [q[0], q[1]]))); + if let Some(d) = p + && app.drag.is_some() + { + tap_at_peak_pressure(app); + tool_event(app, ToolEvent::Up { x: d[0], y: d[1] }, mods); + } + } + if press_stroke && (app.drag.is_none() || !ui.input(|i| i.pointer.primary_down())) { + app.press_stroke = false; + } +} + +fn handle_canvas_tool_clicks( + app: &mut PhotocraftApp, + ui: &mut egui::Ui, + view: &mut View, + input: &CanvasToolInput<'_>, + mods: egui::Modifiers, + tool_press: egui::Id, + buttons: &crate::paint_mouse::Buttons, +) { + let ctx = input.ctx; + let response = input.response; + let xf = input.xf; + let rect = input.rect; + let tool = input.tool; + let transform_owns_pointer = input.transform_owns_pointer; + let ppp = input.ppp; + let zoom_out = input.zoom_out; + let display_zoom = input.display_zoom; + if buttons.clicked + && let Some(p) = response.interact_pointer_pos() + { + let click_button = if app.secondary_erase { PointerButton::Secondary } else { PointerButton::Primary }; + let click_mods = ui.input(|i| pointer_button_modifiers(&i.events, click_button)).unwrap_or(mods); + let click_mods = if tool == Tool::ObjectSelection && click_button == PointerButton::Primary { + ctx.data(|d| d.get_temp(tool_press)).unwrap_or(click_mods) + } else { + click_mods + }; + let d = xf.to_doc(p); + match tool { + Tool::Zoom if !transform_owns_pointer => { + let nz = crate::zoom_levels::step(view.zoom, if zoom_out(click_mods.alt) { -1 } else { 1 }, view.doc_size); + let center = app.session.prefs().tools.zoom_clicked_point_to_center; + let doc_anchor = xf.to_doc(p); + let screen_anchor = p; + zoom_about(view, &xf, p, nz, center, ppp); + view.request_zoom_animation(app.session.prefs().general.animated_zoom && nz != display_zoom); + view.zoom_anchor = (view.zoom_animating && !center).then_some((doc_anchor, [screen_anchor.x, screen_anchor.y])); + } + // A click with the (temporary) Hand does nothing, never the tool underneath. + Tool::Hand | Tool::RotateView => {} + // Double-clicking closes the polygonal lasso: the first click placed the last + // vertex (or already closed it), so the second never starts a new polygon. egui + // reports it as a triple click when a vertex went down shortly before. + Tool::PolygonLasso if response.double_clicked() || response.triple_clicked() => commit_polygon(app), + // The same for the Magnetic Lasso: along the edges, or straight with ⌥. + Tool::MagneticLasso if response.double_clicked() || response.triple_clicked() => crate::magnetic_lasso_ui::close(app, mods.alt), + // The first click placed (or grabbed) the point; the second toggles it. + Tool::CurvaturePen if response.double_clicked() => crate::vector_ui::curvature_toggle(app, d[0], d[1]), + // Double-clicking a guide opens Preferences › Guides, Grid & Slices, as in Photoshop. + Tool::Move if response.double_clicked() && crate::rulers::guide_at(app, d[0], d[1]).is_some() => { + if let Err(e) = crate::menus::invoke(app, &ctx, "edit.preferences.guidesGridAndSlices", json!({})) { + app.ui.status = e; + } + } + _ => { + if tool == Tool::Move && app.ui.transform.is_none() { + begin_transform_controls_at(app, &ctx, &xf, p); + } + // If this tool was not handled on pointer-down, fall back to a + // click-sized gesture while preserving pen pressure (mouse stays at 1). + tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, click_mods); + tap_at_peak_pressure(app); + tool_event(app, ToolEvent::Up { x: d[0], y: d[1] }, click_mods); + } + } + } + // Magnetic Lasso: between clicks the border follows the pointer with the button up. + if tool == Tool::MagneticLasso && app.ui.magnetic.active() && !(buttons.started || buttons.dragged || buttons.stopped) && response.hovered() { + let moves: Vec = ui.input(|i| i.events.iter().filter_map(|e| if let egui::Event::PointerMoved(p) = e { Some(*p) } else { None }).collect()); + app.defer_live_stroke = true; + for p in moves.into_iter().filter(|p| rect.contains(*p)) { + let d = xf.to_doc(p); + tool_event(app, ToolEvent::Move { x: d[0], y: d[1], pressure: app.stylus.pressure() }, mods); + } + app.defer_live_stroke = false; + crate::magnetic_lasso_ui::flush(app); + } + // Move tool: a double-click on type edits it, as in Photoshop (#1132). Only the selected + // Move tool, not ⌘ held over another tool; an open transform box keeps its double-click + // (commit, below). + if tool == Tool::Move + && app.ui.tool == Tool::Move + && app.ui.transform.is_none() + && response.double_clicked() + && let Some(p) = response.interact_pointer_pos() + { + let d = xf.to_doc(p); + crate::type_tool::edit_at(app, d[0], d[1]); + } + if app.ui.transform.is_some() && response.double_clicked() { + crate::transform_tool::commit(app); + } + if crate::cam_transform::active(app) && response.double_clicked() { + crate::cam_transform::commit(app); + } + // Crop: a double-click inside the frame commits it, as ↵ does (#1792). + if tool == Tool::Crop && response.double_clicked() && response.interact_pointer_pos().is_some_and(|p| crate::crop_ui::commits_at(app, xf.to_doc(p))) { + commit_crop(app); + } + // Type: 2 clicks select a word, 3 the line, 4 the paragraph, 5 all text (Photoshop). + if tool.is_type() + && response.clicked() + && let Some(p) = response.interact_pointer_pos() + { + if crate::type_transform::visible(app, crate::workspace_ui::sticky_mods(app, mods)) { + app.type_clicks = None; + } else { + let n = if response.triple_clicked() { + 3 + } else if response.double_clicked() { + 2 + } else { + 1 + }; + let d = xf.to_doc(p); + crate::type_tool::multi_click(app, n, d[0], d[1]); + } + } +} + +struct CanvasOverlayInput<'a> { + ctx: &'a egui::Context, + painter: &'a egui::Painter, + xf: &'a ViewXform, + response: &'a egui::Response, + doc: &'a std::sync::Arc, + tool: Tool, + idx: usize, + output: Option, + border: photocraft_engine::prefs::CanvasBorder, + beyond: bool, + edge_rect: Rect, + panning: bool, + zoom_out: &'a dyn Fn(bool) -> bool, +} + +fn paint_primary_overlays(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &mut View, input: &CanvasOverlayInput<'_>) { + let ctx = input.ctx; + let painter = input.painter; + let xf = input.xf; + let response = input.response; + let doc = input.doc; + let idx = input.idx; + let border = input.border; + let beyond = input.beyond; + let edge_rect = input.edge_rect; + // The transform preview is image content: it goes under the grid and guides, which + // stay visible across a layer being transformed (#5086); the box is drawn later. + crate::transform_tool::draw_content(app, &painter, &xf); + // So are Puppet Warp's, Perspective Warp's and Vanishing Point's warped pixels (#7781). + crate::distort_ui::draw_content(app, &painter, &xf); + if app.ui.extras.grid && app.ui.view.extras { + crate::rulers::draw_grid(app, &painter, &xf, &doc); + } + if app.ui.view.shows(app.ui.view.show.canvas_guides) || crate::guide_layout_ui::preview(app, &doc).is_some() { + crate::rulers::draw_guides(app, &painter, &xf, &doc); + } + // The pending crop frame shows on its own document only, not on the other tiles (#1918). + let foreign_crop = if app.session.active_index() == Some(idx) { None } else { app.ui.crop_rect.take() }; + draw_drag_preview(app, &painter, &xf); + if app.session.active_index() == Some(idx) { + crate::move_box::draw(app, &painter, &xf); + } + if foreign_crop.is_some() { + app.ui.crop_rect = foreign_crop; + } + crate::zoom_tool::draw(&ctx, &painter); + let resizing = crate::brush_resize::draw(app, &painter, &xf); + draw_transform_controls(app, &painter, &xf); + crate::move_hover::draw(app, &painter, &xf, idx, response.hover_pos()); + crate::object_finder::draw(app, &painter, &xf, idx, response.hover_pos()); + crate::layer_pick_ui::show(app, &ctx); + let shown = app.ui.views.get(idx).cloned(); + crate::canvas_tool_menu::show(app, &ctx); + // A view menu row (Zoom In, Fit on Screen…) changed the camera: it wins over this + // frame's copy, which is written back at the end. + if let Some(now) = app.ui.views.get(idx) + && Some(now) != shown.as_ref() + { + *view = now.clone(); + ctx.request_repaint(); + } + crate::snap_ui::draw(app, &painter, &xf); + crate::move_distance::draw(app, &painter, &xf, idx, response.hover_pos()); + // The canvas edge isn't outlined while the pixels past it show (Photoshop's crop preview), + // nor while the Crop tool hides everything outside its box (Show Cropped Area off). + if border == photocraft_engine::prefs::CanvasBorder::Line && !beyond && !crate::crop_shield::hides_outside(app) { + painter.rect_stroke(edge_rect, 0.0, Stroke::new(1.0, Color32::from_gray(20)), egui::StrokeKind::Outside); + } + crate::type_tool::draw_overlay(app, &painter, &xf); + crate::transform_tool::draw_overlay(app, &painter, &xf); + crate::cam_transform::draw(app, &painter, &xf); + crate::cam_preview::wake(app, painter.ctx()); + crate::distort_ui::draw_overlay(app, &painter, &xf); + crate::perspective_crop_ui::draw_overlay(app, &painter, &xf); + crate::retouch_ui::draw_source_marker(app, &painter, &xf); + crate::vector_ui::draw_overlay(app, &painter, &xf, &doc); + crate::symmetry_ui::draw(app, &painter, &xf); + crate::analysis_ui::draw_overlay(app, &painter, &xf); + crate::gradient_ui::draw_overlay(app, &painter, &xf); + crate::slice_ui::draw_overlay(app, &painter, &xf); + crate::frame_tool::draw_overlay(app, &painter, &xf); + crate::artboard_tool::draw_overlay(app, &painter, &xf); + + draw_canvas_tool_cursor(app, ui, input, resizing); +} + +fn draw_canvas_tool_cursor(app: &mut PhotocraftApp, ui: &mut egui::Ui, input: &CanvasOverlayInput<'_>, resizing: bool) { + let ctx = input.ctx; + let xf = input.xf; + let response = input.response; + let tool = input.tool; + let panning = input.panning; + // Tool cursors (Photoshop-style). + let guide_hover = response.hover_pos().and_then(|p| { + let d = xf.to_doc(p); + if tool == Tool::Move { + return crate::rulers::guide_at(app, d[0], d[1]); + } + command_guide_at(app, tool, d, crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers))) + }); + if panning { + // The fist set with the pan above stays (#2196). + } else if crate::tool_cursor::pointer_elsewhere(&ctx, &crate::panel_docking::native_viewports(app, &ctx)) { + // The pointer is over a floating panel's own window; this window's last position of + // it is stale, so no tool cursor or brush outline is painted at it. + } else if let Some((vertical, _)) = guide_hover { + ui.ctx().set_cursor_icon(crate::rulers::guide_cursor(&xf, vertical)); + } else if let Some((p, mods, c)) = response.hover_pos().and_then(|p| { + let mods = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)); + let c = crate::transform_tool::cursor(app, &xf, xf.to_doc(p), mods)?; + Some((p, mods, c)) + }) { + ui.ctx().set_cursor_icon(c); + // CursorIcon::None hides the pointer; the curved arrow has to be drawn or the + // rotate zone looks like a plain cursor (#4050). + let d = xf.to_doc(p); + if let Some(k) = crate::transform_tool::rotate_cursor_dir(app, d) { + 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 c == egui::CursorIcon::None { + // Likewise the distort and skew pointers over a handle (#5405). + crate::transform_tool::draw_handle_cursor(app, &ctx, &xf, p, mods); + } + } else if let Some(c) = response.hover_pos().and_then(|p| crate::symmetry_ui::cursor(app, &xf, xf.to_doc(p))) { + ui.ctx().set_cursor_icon(c); + } else if let Some(c) = response.hover_pos().and_then(|p| crate::cam_transform::cursor(app, &ctx, &xf, p)) { + ui.ctx().set_cursor_icon(c); + } else if let Some(p) = response.hover_pos().filter(|p| crate::corner_radius::hovers(app, xf.to_doc(*p))) { + // Over a live rectangle's corner widget (or dragging one): the arrow with a rounded + // corner badge, as Photoshop's corner radius cursor. + ui.ctx().set_cursor_icon(egui::CursorIcon::Default); + crate::icons::cursor_badge(&ctx, "scan", p); + } else if let Some((r, p, c)) = response + .hover_pos() + .filter(|_| !transform_controls_several(app)) + .and_then(|p| transform_controls_rect(app, &xf).and_then(|r| transform_controls_cursor(r, p).map(|c| (r, p, c)))) + { + // The Move tool draws handles without opening a Free Transform session. + // Give them their resize/rotate cursors before the first pointer press (#2871). + ui.ctx().set_cursor_icon(c); + if let Some(toward) = transform_controls_turn(r, p) { + crate::crop_ui::draw_turn_cursor(&ctx, p, toward); + } + } else if let Some(c) = response.hover_pos().filter(|_| tool.is_type()).and_then(|p| { + let mods = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)); + crate::type_transform::cursor(app, &ctx, &xf, p, mods) + }) { + ui.ctx().set_cursor_icon(c); + } else if let Some(c) = response.hover_pos().filter(|_| tool == Tool::Artboard).and_then(|p| crate::artboard_tool::cursor(app, &xf, xf.to_doc(p))) { + ui.ctx().set_cursor_icon(c); + } else if let Some((p, c)) = response.hover_pos().filter(|_| tool == Tool::Crop).and_then(|p| Some((p, crate::crop_ui::cursor(app, xf.to_doc(p))?))) { + ui.ctx().set_cursor_icon(c); + let d = xf.to_doc(p); + if let Some(k) = crate::crop_ui::turn_cursor_dir(app, d) { + 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 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 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() { + draw_default_tool_cursor(app, ui, input, resizing, p); + } +} + +fn draw_default_tool_cursor(app: &mut PhotocraftApp, ui: &mut egui::Ui, input: &CanvasOverlayInput<'_>, resizing: bool, p: Pos2) { + let painter = input.painter; + let xf = input.xf; + let response = input.response; + let tool = input.tool; + let output = input.output; + let zoom_out = input.zoom_out; + + let alt = ui.input(|i| i.modifiers.alt); + if !resizing && !alt && !app.ui.shell.sticky_alt { + draw_clone_preview(app, &painter, &xf, xf.to_doc(p), output); + } + // The Type tool over a path shows Photoshop's area-type or type-on-a-path pointer (#218). + // Not while a type edit is open or a text box is dragged out, nor over type a click + // would edit (`hover_path_target`). + let type_path = if matches!(tool, Tool::Type | Tool::VerticalType) { + let [x, y] = xf.to_doc(p); + let shift = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)).shift; + crate::type_tool::hover_path_target(app, x, y, shift) + } else { + None + }; + 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`): its cursor too, + // unless Preferences › Cursors › Other Cursors asks for the precise crosshair. + t if app.alt_sampling + || (app.drag.is_none() + && !app.services.right_button_down.as_ref().is_some_and(|f| f()) + && alt_samples(t, crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)))) => + { + // While it samples (button held), the comparison ring replaces the cursor (#213). + if let Some(icon) = eyedropper_ring(app, &painter, &xf, p, response.is_pointer_button_down_on()) { + icon + } else 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 + // full size), precise crosshair, or the standard pointer. + use photocraft_engine::prefs::PaintingCursor; + let cur = app.session.prefs().cursors.clone(); + // ⇪ forces the precise crosshair, whatever the cursor preference (#1758). + if app.caps_lock { + crate::tool_cursor::crosshair(&painter, p, 6.0, 0.0) + } else { + let painting = app.drag.is_some(); + let brush = &app.session.tools.brush; + let full = (brush.size / 2.0 * xf.zoom).max(1.0); + match cur.painting { + PaintingCursor::Standard => egui::CursorIcon::Default, + PaintingCursor::Precise => crate::tool_cursor::crosshair(&painter, p, 6.0, 0.0), + _ if painting && cur.show_only_crosshair_while_painting => crate::tool_cursor::crosshair(&painter, p, 5.0, 0.0), + // The Pencil (and the Eraser in Pencil mode): the outline of the + // whole pixels its dab fills, on the pixel grid. A round tip is a + // disc, a tiny one the whole square (#2662). The Block-mode Eraser: + // its square, 16 screen pixels at any zoom (#2770). + _ if tool == Tool::Pencil || crate::eraser_ui::pencil_mode(app, tool) || crate::eraser_ui::block_mode(app, tool) => { + let block = crate::eraser_ui::block_mode(app, tool); + let size = if block { crate::eraser_ui::block_size(app) } else { brush.size }; + let ppp = painter.ctx().pixels_per_point(); + let sq = pencil_cursor_rect(&xf, xf.to_doc(p), size, ppp); + let px = 1.0 / ppp; + let (dark, light) = (Stroke::new(px, Color32::from_black_alpha(160)), Stroke::new(px, Color32::from_white_alpha(230))); + if !block && pencil_tip_is_round(size) { + let r = sq.width() / 2.0; + painter.circle_stroke(sq.center(), r + px / 2.0, dark); + painter.circle_stroke(sq.center(), (r - px / 2.0).max(0.0), light); + } else { + painter.rect_stroke(sq, 0.0, dark, egui::StrokeKind::Outside); + painter.rect_stroke(sq, 0.0, light, egui::StrokeKind::Inside); + } + // Too small to see where it is: the hotspot as well. + if cur.show_crosshair_in_brush_tip || sq.width() < 6.0 { + crate::tool_cursor::crosshair(&painter, p, 4.0, 0.0); + } + egui::CursorIcon::None + } + // A shaped tip (sampled bitmap, or a rotated/flattened round tip) + // traces its outline, turned and flipped with the view; a plain round + // tip keeps the circle. + _ => { + let centre = brush_tip_centre(tool, alt || app.ui.shell.sticky_alt, cur.show_crosshair_in_brush_tip); + let axes = crate::brush_cursor::pen_axes(&xf, brush, app.stylus.sample()); + crate::brush_cursor::cursor(&painter, p, brush, full, axes, cur.painting == PaintingCursor::NormalTip, centre) + } + } + } + } + // The Eyedropper: the comparison ring follows the pointer while it samples (#213); + // otherwise its pipette, or the crosshair for precise Other Cursors. + Tool::Eyedropper => { + if let Some(icon) = eyedropper_ring(app, &painter, &xf, p, response.is_pointer_button_down_on()) { + icon + } else if precise_other_cursor(app) { + egui::CursorIcon::Crosshair + } else { + pipette_cursor(ui.ctx(), p) + } + } + // The Color Sampler: a pipette, or the crosshair for precise Other Cursors. + Tool::ColorSampler => { + if precise_other_cursor(app) { + egui::CursorIcon::Crosshair + } else { + pipette_cursor(ui.ctx(), p) + } + } + // The Paint Bucket: a bucket, or the crosshair for precise Other Cursors (#3968). + Tool::PaintBucket => { + if precise_other_cursor(app) { + egui::CursorIcon::Crosshair + } else { + paint_bucket_cursor(ui.ctx(), p) + } + } + // Over the first anchor, the close-path pointer (#4596), before the pens' own cursors. + Tool::Pen | Tool::CurvaturePen + if { + let [x, y] = xf.to_doc(p); + crate::vector_ui::closes_path_at(app, tool, x, y) + } => + { + crate::tool_cursor::close_path(&painter, p) + } + // The pen tools: their own icon with its nib on the point, or the crosshair for + // precise Other Cursors (#4639). + Tool::Pen | Tool::FreeformPen | Tool::CurvaturePen | Tool::AddAnchorPoint | Tool::DeleteAnchorPoint | Tool::ConvertPoint => { + if precise_other_cursor(app) { egui::CursorIcon::Crosshair } else { pen_cursor(ui.ctx(), tool, p) } + } + // Preferences › Cursors › Other Cursors: Precise shows a crosshair for every tool. + Tool::Move | Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask + if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise => + { + egui::CursorIcon::Crosshair + } + // Photoshop's Move tool keeps the standard arrow pointer (#5646): the movable + // layer under it is marked by the hover outline, not by a different cursor. + Tool::Move => egui::CursorIcon::Default, + // Dragging is handled with the pan itself, above. + Tool::Hand => crate::tool_cursor::hand(ui.ctx(), false), + Tool::RotateView => crate::rotate_view::cursor(response.dragged()), + Tool::Zoom => crate::tool_cursor::zoom(ui.ctx(), zoom_out(alt)), + Tool::Type | Tool::VerticalType if type_path.is_some() => match type_path { + Some(crate::type_tool::PathClick::Area { .. }) => crate::tool_cursor::area_type(&painter, p), + _ => crate::tool_cursor::path_type(&painter, p), + }, + Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask => egui::CursorIcon::Text, + Tool::MagneticLasso => crate::magnetic_lasso_ui::cursor(app, &painter, p, xf.zoom), + Tool::RedEye => { + let pupil = app.ui.tool_options.red_eye_pupil_size.clamp(1.0, 100.0); + let r_doc = photocraft_algo::redeye::search_radius(pupil); + let r = (r_doc * xf.zoom).max(2.0); + painter.circle_stroke(p, r + 0.5, Stroke::new(1.0, Color32::from_black_alpha(140))); + painter.circle_stroke(p, r, Stroke::new(1.0, Color32::from_white_alpha(220))); + for (w, c) in [(2.5, Color32::from_black_alpha(140)), (1.0, Color32::from_white_alpha(220))] { + painter.line_segment([p - vec2(3.0, 0.0), p + vec2(3.0, 0.0)], Stroke::new(w, c)); + painter.line_segment([p - vec2(0.0, 3.0), p + vec2(0.0, 3.0)], Stroke::new(w, c)); + } + egui::CursorIcon::None + } + _ => egui::CursorIcon::Crosshair, + }; + let icon = visible_crosshair(icon, &painter, p, cfg!(not(target_arch = "wasm32"))); + ui.ctx().set_cursor_icon(icon); + // Selection tools: + / − / × badge for the effective mode (#170). A gesture keeps the + // mode it started with (⇧ then constrains the marquee instead of adding). + let held = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)); + let mods = app.drag.as_ref().map_or(held, |d| d.modifiers); + if let Some(b) = crate::tool_feedback::badge(app, tool, mods) { + crate::tool_feedback::draw_badge(&painter, p, b, tool == Tool::QuickSelection); + } + // ⇧ after a stroke: the straight line a click would paint (#257). + crate::stroke_constraint::draw_line_preview(app, &painter, &xf, p, tool, held.shift); +} + pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: Rect, mut view: View, primary: bool) -> View { let ctx = ui.ctx().clone(); // The camera as the app holds it before this frame's input. The canvas works on its own copy @@ -305,302 +1692,24 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: // Default-mode Crop changes its camera during input. Defer its image until the new camera // is known; other tools keep their existing paint order and texture/outline snapshots. let crop_camera = primary && crate::crop_mode::moves_image(app); - let paint_canvas = |app: &mut PhotocraftApp, ui: &egui::Ui, view: &View, xf: ViewXform| { - let point_zoom = view.zoom / ppp; - let drop_shadow = border == photocraft_engine::prefs::CanvasBorder::DropShadow; - // Drop shadow, checkerboard, document image. - let img_rect = xf.doc_rect(doc.bounds()); - // The canvas edge the Line border outlines: a size-changing preview's own (CPU canvas). - let mut edge_rect = img_rect; - // The document area the image is drawn over: a size-changing preview's own on the CPU - // canvas (Image › Rotation). Overlays drawn from the same preview follow it. - let mut shown_area = doc.bounds(); - // Crop tool: the layers' pixels past the canvas while a frame is edited, under the canvas - // (whose edge blends into them) and without its shadow. - let beyond = draw_beyond_canvas(app, &painter, &xf, idx, output); - // View › Pattern Preview (#1067): copies of the document around it, from the same texture. - let pattern = if app.ui.view.pattern_preview { - let v = visible_doc_rect(&xf); - crate::pattern_preview::copies([v.x0, v.y0, v.x1, v.y1].map(f64::from), size) - } else { - Vec::new() - }; - let drop_shadow = drop_shadow && !beyond && !app.ui.view.pattern_preview; - // Live adjustment previews on big documents use a downsampled proxy (see proxy.rs), unless - // Preferences › Performance › Low Resolution Previews is off. - let mut on_gpu = false; - // A flipped or pixel-aspect-corrected view draws through the CPU path (the GPU canvas shader - // has no mirroring or non-square pixels). Rotation stays on the GPU: it is a view uniform. - let gpu_ok = crate::gpu_canvas::gpu_view_ok(flip, view.rotation, aspect); - if app.gpu.is_some() - && gpu_ok - && let Some((k, key, preview_size)) = ensure_adjust_proxy(app, idx, render_zoom) - .or_else(|| ensure_filter_preview(app, idx, &ctx)) - .or_else(|| ensure_proxy_preview(app, idx, render_zoom)) - { - on_gpu = true; - app.navigator_gpu.remove(&doc.id); - let original_size = [doc.size.width.div_ceil(k), doc.size.height.div_ceil(k)]; - let shown = filter_preview_display_doc(app, &doc, key); - let params = crate::gpu_canvas::ViewParams { - doc: key, - doc_size: preview_size, - zoom: view.zoom * k as f32 / ppp, - // A size-changing preview grows around the old image center; keep the view's pan. - center: [ - xf.center[0] / k as f32 + (preview_size[0] as f32 - original_size[0] as f32) / 2.0, - xf.center[1] / k as f32 + (preview_size[1] as f32 - original_size[1] as f32) / 2.0, - ], - rotation: if view.rotation.is_finite() { view.rotation.to_radians() } else { 0.0 }, - shadow: { - let t = crate::theme::Tokens::get(&ctx); - !t.bevel && !t.pro && drop_shadow - }, - pixel_grid: false, - view_key: egui::Id::unique(("pc-canvas-proxy", ctx.viewport_id(), idx)).value(), - display: sync_display_lut(app, &shown, key, output), - output: output.unwrap_or(0), - hdr: hdr_preview(app, &doc), - offset: [0.0, 0.0], - }; - paint_gpu_view(&painter, rect, params, &pattern); - } else if gpu_ok && ensure_gpu(app, &ctx, idx, redraw_focus(&target_xf, &xf)) { - on_gpu = true; - app.perf.gpu = true; - // Shadow, checkerboard, document and pixel grid in one custom shader (gpu_canvas.rs). - let params = crate::gpu_canvas::ViewParams { - doc: doc.id.0, - doc_size: [doc.size.width, doc.size.height], - zoom: point_zoom, - center: xf.center, - rotation: if view.rotation.is_finite() { view.rotation.to_radians() } else { 0.0 }, - shadow: { - let t = crate::theme::Tokens::get(&ctx); - !t.bevel && !t.pro && drop_shadow - }, - pixel_grid, - view_key: egui::Id::unique(("pc-canvas", ctx.viewport_id(), idx)).value(), - display: sync_display_lut(app, &doc, doc.id.0, output), - output: output.unwrap_or(0), - hdr: hdr_preview(app, &doc), - offset: [0.0, 0.0], - }; - if app.navigator_plain.contains(&doc.id) { - app.navigator_gpu.insert(doc.id, params); - } else { - app.navigator_gpu.remove(&doc.id); - } - paint_gpu_view(&painter, rect, params, &pattern); - } else { - app.navigator_gpu.remove(&doc.id); - // A document too large for one full-resolution texture also gets the part in view at - // the screen's resolution, drawn over the reduced whole (#7568). - let in_view = InView { - visible: visible_doc_rect(&xf), - zoom: view.zoom, - key: egui::Id::unique(("pc-canvas-detail", ctx.viewport_id(), idx, primary)).value(), - }; - let textures = ensure_texture_in_view(app, &ctx, idx, output, Some(in_view)); - let tex = textures.as_ref().map(|t| t.texture); - // The area the image covers: a size-changing preview's own (Image › Rotation). - let area = textures.as_ref().map_or(doc.bounds(), |t| t.bounds); - shown_area = area; - if area != doc.bounds() { - edge_rect = xf.doc_rect(area); - } - if !crate::theme::Tokens::get(&ctx).bevel && drop_shadow { - let shadow_rect = edge_rect; - painter.add(egui::Shadow { offset: [0, 8], blur: 28, spread: 0, color: Color32::from_black_alpha(150) }.as_shape(shadow_rect, 0)); - } - let rotated = view.rotation.abs() > 0.05; - let checkerboard = app.session.prefs().transparency_and_gamut.square().map(|square| (checker(app, &ctx), square)); - // Repeated-quad coverage is not proven identical on all Pattern Preview adapters. - // Retain its legacy checker submissions, including the central document's. - let opaque = !app.ui.view.pattern_preview && app.canvases.get(&cache_key(doc.id, output)).is_some_and(|c| c.texture_opaque) && tex.is_some(); - paint_cpu_document(&painter, &xf, area, rotated, checkerboard, tex, opaque); - if let Some((detail, area)) = textures.and_then(|t| t.detail) { - paint_cpu_detail(&painter, &xf, doc.bounds(), rotated, checkerboard, detail, area); - } - // Pattern Preview tiles what is shown: a size-changing preview's area, from its origin. - let (tile, copies) = if area == doc.bounds() || !app.ui.view.pattern_preview { - (size, pattern.clone()) - } else { - let tile = [area.width(), area.height()]; - let v = visible_doc_rect(&xf).translate(-area.x0, -area.y0); - (tile, crate::pattern_preview::copies([v.x0, v.y0, v.x1, v.y1].map(f64::from), tile)) - }; - for &c in &copies { - paint_cpu_document(&painter, &pattern_xf(&xf, c, tile), area, rotated, checkerboard, tex, opaque); - } - } - // Channels panel: alpha / Quick Mask overlays and single-channel views (channel_view.rs). - if let Some(tex) = crate::channel_view::ensure(app, &ctx, idx) { - // Rendered from the live preview, so over the area the preview is shown in. - let a = shown_area; - if view.rotation.abs() > 0.05 { - paint_mapped_image_at( - &painter, - &xf, - tex, - Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)), - [a.x0 as f32, a.y0 as f32], - [a.width() as f32, a.height() as f32], - Color32::WHITE, - ); - } else { - let uv = if flip { Rect::from_min_max(pos2(1.0, 0.0), pos2(0.0, 1.0)) } else { Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)) }; - painter.image(tex, xf.doc_rect(a), uv, Color32::WHITE); - } - } - crate::color_range_ui::paint_selection_preview(app, &ctx, &painter, doc.id, img_rect, flip); - // Artboards: pasteboard between the boards, outlines and names (artboard_ui.rs). - if doc.has_artboards() { - let t = crate::theme::Tokens::get(&ctx); - let pasteboard = crate::prefs_ui::pasteboard_color(app); - let dot_tex = dots(&ctx, &t); - // Cut around the layers as they are shown, so a loose layer dragged with the Move tool - // (or otherwise previewed) is not covered where it now draws. - let shown = display_doc(app, idx).0; - // Each piece is painted as its own quad (the painter clips to the canvas), so a - // rotated view does not paint a piece's bounding box over the boards or loose work. - let fill = |q: &[Pos2; 4], tex: Option, color: Color32| { - let mut mesh = tex.map_or_else(egui::Mesh::default, egui::Mesh::with_texture); - for &p in q { - // Same phase as the dots around the document. - let uv = if tex.is_some() { ((p - rect.min) / 22.0).to_pos2() } else { egui::epaint::WHITE_UV }; - mesh.vertices.push(egui::epaint::Vertex { pos: p, uv, color }); - } - mesh.add_triangle(0, 1, 2); - mesh.add_triangle(0, 2, 3); - painter.add(mesh); - }; - for q in crate::artboard_ui::pasteboard_quads(&xf, &shown) { - if !Rect::from_points(&q).intersects(rect) { - continue; - } - match pasteboard { - Some(c) => fill(&q, None, c), - None => { - fill(&q, None, t.canvas); - if let Some(id) = dot_tex { - fill(&q, Some(id), Color32::WHITE); - } - } - } - } - if primary { - crate::artboard_ui::draw_frames(app, &painter, &xf); - } - } - - // Pixel grid at high zoom, over pixels with content only (the GPU path draws its own). - if !on_gpu && pixel_grid && crate::pixel_grid::shows_at(view.zoom) { - crate::pixel_grid::paint(app, &painter, &xf, idx, output, visible_doc_rect(&xf)); - } - - // Developer tile grid (`ui.set {"tileGrid": true}`): the active layer's storage tiles. - // Not tied to View › Extras (⌘H), which hides only the View › Show overlays. - if app.ui.view.tile_grid { - crate::tile_overlay::paint(app, &ctx, &painter, &xf, idx, visible_doc_rect(&xf)); - } - - // View › Show › Layer Edges: drawn at this depth, but only once this frame's tool events are - // in (`layer_edges_rect`), so a press that Auto-Selects a layer outlines that layer. - let layer_edges = painter.add(egui::Shape::Noop); - - // Select and Mask outlines its refined selection (on its preview proxy, reduced by `k`) in - // the Marching Ants view and hides the ants in the others. - let refined = crate::select_mask_view::ants(app, idx); - // A floating piece's ants hug the pixels being moved. - let floated = floating_outline(app, idx); - let (outlined, k) = match &refined { - None => (floated.as_ref().or(doc.selection.as_ref()), 1), - Some(None) => (None, 1), - Some(Some((proxy, k))) => (proxy.selection.as_ref(), (*k).max(1)), - }; - // Selection outline: true boundary, animated marching ants (cached per revision). - if let Some(sel) = outlined.filter(|_| app.ui.view.shows(app.ui.view.show.selection_edges) && !polygon_replaces_selection(app)) { - // Trace at display resolution over the visible part only; key by the mask's tile identity - // (not the document revision) so unrelated edits don't re-trace it. The step is in - // device pixels per document pixel (`View::zoom`, not the point zoom), so a 1 px - // selection still traces at 100% on a scaled display. - let step = (1.0 / (view.zoom * k as f32).max(1e-3)).log2().floor().exp2().clamp(1.0, 64.0) as u32; - let corners = [rect.min, pos2(rect.max.x, rect.min.y), rect.max, pos2(rect.min.x, rect.max.y)]; - let docs = [xf.to_doc(corners[0]), xf.to_doc(corners[1]), xf.to_doc(corners[2]), xf.to_doc(corners[3])]; - let mut x0 = docs[0][0]; - let mut y0 = docs[0][1]; - let mut x1 = x0; - let mut y1 = y0; - for d in docs { - x0 = x0.min(d[0]); - y0 = y0.min(d[1]); - x1 = x1.max(d[0]); - y1 = y1.max(d[1]); - } - let (x0, y0, x1, y1) = (x0 / f64::from(k), y0 / f64::from(k), x1 / f64::from(k), y1 / f64::from(k)); - let q = 256 * step as i32; // quantise the region so small pans reuse the cache - // The view centre can be any finite number (`ui.set`, #980). Past ±2³⁰ there is no document - // to outline, and the padding below (at most 2 · 256 · 64) can't overflow i32 from there. - let px = |v: f64| v.clamp(-f64::from(1 << 30), f64::from(1 << 30)) as i32; - // A Free Transform carries the selection to wherever its box goes (#5297), so all of - // it is outlined: the part now in view may have started out of it. - let carried = app.session.active_index() == Some(idx) && crate::transform_tool::carries_selection_of(app, doc.id); - let vis = if carried { - photocraft_geom::Rect::new(-(1 << 30), -(1 << 30), 1 << 30, 1 << 30) - } else { - photocraft_geom::Rect::new( - px(x0.floor()).div_euclid(q) * q - q, - px(y0.floor()).div_euclid(q) * q - q, - (px(x1.ceil()).div_euclid(q) + 2) * q, - (px(y1.ceil()).div_euclid(q) + 2) * q, - ) - }; - let bounds_key = if k > 1 { u64::MAX / 2 - doc.id.0 } else { u64::MAX - doc.id.0 }; - let carried_bounds = carried.then(|| app.cached_bounds(bounds_key, sel).intersect(&vis)); - let step = carried_bounds.map_or(step, |b| crate::outline::bounded_step(b, step)); - let key = crate::surface_fingerprint(sel) - ^ u64::from(step).rotate_left(56) - ^ doc.id.0.rotate_left(17) - ^ (vis.x0 as u64) << 8 - ^ (vis.y0 as u64) << 24 - ^ (vis.x1 as u64) << 36 - ^ (vis.y1 as u64) << 48 - ^ u64::from(k).rotate_left(5); - let fresh = matches!(&app.outline_cache, Some((d, k, _)) if *d == doc.id && *k == key); - if !fresh { - let t0 = crate::gpu_canvas::now_ms(); - let b = carried_bounds.unwrap_or_else(|| app.cached_bounds(bounds_key, sel).intersect(&vis)); - // Filters can occupy every worker in Rayon's global pool. A parallel scan here would - // block the UI thread waiting for those workers, preventing job cancellation (#1554). - let mut segs = - if app.session.has_jobs() { crate::outline::outline_scaled_serial(sel, b, step) } else { crate::outline::outline_scaled(sel, b, step) }; - if k > 1 { - let s = i32::try_from(k).unwrap_or(1); - let up = |p: [i32; 2]| [p[0].saturating_mul(s), p[1].saturating_mul(s)]; - segs.iter_mut().for_each(|(a, b)| (*a, *b) = (up(*a), up(*b))); - } - app.perf.span("outline", crate::gpu_canvas::now_ms() - t0); - app.outline_cache = Some((doc.id, key, std::sync::Arc::new(segs))); - } - // Under Content-Aware Move's Transform On Drop box the ants are drawn where it puts them. - if let Some((_, _, segs)) = app.outline_cache.as_ref().filter(|_| !crate::cam_transform::active(app)) { - let time = ui.input(|i| i.time); - // A floating piece (and a selection being dragged) draws its ants where it is shown. - let mut shown = xf; - if let Some((dx, dy)) = selection_shown_offset(app) { - shown.center = [xf.center[0] - dx as f32, xf.center[1] - dy as f32]; - } - // Under a Free Transform box they are where the box puts them (#5297). - if !(carried && crate::transform_tool::draw_selection(app, &painter, &xf, doc.id, segs, time)) { - marching_ants_segments(&painter, &shown, segs, time); - } - ctx.request_repaint_after(std::time::Duration::from_millis(100)); - } - } - - (beyond, layer_edges, edge_rect) + let paint_input = CanvasPaintInput { + ctx: &ctx, + rect, + painter: &painter, + ppp, + border, + doc: &doc, + idx, + output, + size, + primary, + flip, + aspect, + render_zoom, + target_xf, + pixel_grid, }; - let painted = if crop_camera { None } else { Some(paint_canvas(app, ui, &display_view, xf)) }; + let painted = if crop_camera { None } else { Some(paint_canvas(app, ui, &display_view, xf, &paint_input)) }; let img_rect = xf.doc_rect(doc.bounds()); // Under an open dialog the canvas widget is inert, but the image still pans and zooms. @@ -610,69 +1719,8 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: if !under_dialog && ctx.input(|i| i.pointer.any_pressed() && i.pointer.interact_pos().is_some_and(|p| rect.contains(p))) { crate::spring_tools::note_canvas_press(app); } - // Navigation (wheel_nav.rs): scroll pans (⇧ or ⌘/Ctrl sideways); pinch and ⌥-scroll zoom around the pointer. - // The wheel also scrolls over the scrollbars drawn on top of the canvas (last frame's hover). let bars_id = egui::Id::unique(("pc-canvas-bars-hover", idx)); - let over_bars = ctx.data(|d| d.get_temp::(bars_id)).unwrap_or(false); - let nav_hovered = response.hovered() || free_hover || over_bars; - // Interface › Enable Animations off: the wheel moves the canvas without a glide (#5721). - // General › Animated Zoom: the wheel zoom eases over the frames of a notch, or is one instant step. - // Enhanced Controls › Enable Gestures off: two fingers on a touch screen neither zoom nor - // turn the view (#204); the mouse and trackpad are unaffected. - let touch_gestures = app.session.prefs().enhanced_controls.touch_gestures; - let wheel = crate::wheel_nav::read_with( - &ctx, - app.session.prefs().general.zoom_with_scroll_wheel, - app.session.prefs().enhanced_controls.zoom_with_trackpad_pinch, - app.session.prefs().general.animated_zoom, - wheel_owner, - nav_hovered, - app.session.prefs().interface.enable_animations, - touch_gestures, - ); - if nav_hovered { - let pointer = ui.input(|i| i.pointer.hover_pos()); - match (wheel, pointer) { - (Some(crate::wheel_nav::Wheel::Zoom(f)), Some(p)) => { - // At a limit the view stays put, as in Photoshop: a notch past 12800 % neither - // zooms nor slides the image towards the pointer. - let nz = crate::zoom_levels::clamp(view.zoom * f, view.doc_size); - if nz != view.zoom * f { - crate::wheel_nav::settle(&ctx, wheel_owner); - } - if nz != view.zoom { - // Over the pasteboard the anchor is the image's nearest point (#3720). - let at = wheel_anchor(&xf, p, view.doc_size); - zoom_about(&mut view, &xf, at, nz, false, ppp); - } - } - (Some(crate::wheel_nav::Wheel::SnapZoom(n)), Some(p)) => { - let dir = if n > 0 { 1 } else { -1 }; - let mut nz = view.zoom; - for _ in 0..n.unsigned_abs() { - nz = crate::zoom_levels::step(nz, dir, view.doc_size); - } - if nz != view.zoom { - zoom_about(&mut view, &xf, p, nz, false, ppp); - } - } - (Some(crate::wheel_nav::Wheel::Pan(scroll)), _) => { - let d = xf.unmap_vec(scroll) / (view.zoom / ppp); - view.center[0] -= d.x; - view.center[1] -= d.y; - } - _ => {} - } - let rotate_trackpad = app.session.prefs().enhanced_controls.rotate_view_with_trackpad; - if let Some(delta) = crate::rotate_view::trackpad_delta(&ctx, rotate_trackpad, touch_gestures) { - view.zoom_animating = false; - view.zoom_anchor = None; - let next = view.rotation + crate::rotate_view::screen_delta(delta, xf.flip); - if next.is_finite() { - view.rotation = crate::rotate_view::wrap_deg(next); - } - } - } + update_canvas_navigation(app, &mut view, CanvasNavigationInput { ctx: &ctx, response: &response, free_hover, wheel_owner, xf, ppp, idx }); // Pen pressure/tilt for this frame's tool events (mouse = 1.0), unless Preferences › Tools › // Use Tablet Pressure is off; the pen's eraser end selects the Eraser. @@ -722,117 +1770,25 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: }; if under_dialog { - // With a colour dialog picker armed the image is its eyedropper, whatever the tool; Space - // and the middle button still pan. Curves and Replace Color use the same merged-composite - // sampler as the Color Picker, never the reduced adjustment preview. - let curves_picking = crate::adjust_dialog::picker_armed(app); - let replace_picking = crate::filter_dialog::color_eyedropper_armed(app); - let picking = primary && (crate::color_picker_ui::top(app).is_some() || curves_picking || replace_picking); - let range_picking = - primary && app.ui.dialogs.last().is_some_and(|d| crate::color_range_ui::owns(&d.fields) && crate::color_range_ui::controls(&d.fields).sampling); - // Select and Mask's Refine Edge Brush paints on the image. - let refine_brushing = - primary && app.ui.dialogs.last().is_some_and(|d| crate::select_mask_view::owns(&d.fields) && crate::select_mask_view::brush_on(&d.fields)); - // Field Blur's pins are placed and dragged on the image (`blur_pins`). - let pinning = primary && app.ui.dialogs.last().is_some_and(|d| crate::blur_pins::owns(&d.fields)); - // Layer Style on a Drop or Inner Shadow page: a drag on the image moves the shadow (#5701). - let shadow_drag = - !picking && app.ui.dialogs.last().is_some_and(|d| d.kind == crate::state::DialogKind::LayerStyle && crate::layer_style::drags_shadow(&d.fields)); - // ⌘/Ctrl-click with Curves open: a point at the pixel's tone (#7325), whatever the tool. The - // press's own modifiers decide, and only the press adds: a drag would add one per frame. - let curve_point_press = (primary && !middle && crate::adjust_dialog::curves_on_top(app)) - .then(|| { - ctx.input(|i| { - i.events.iter().rev().find_map(|e| match e { - egui::Event::PointerButton { button: PointerButton::Primary, pressed: true, modifiers, .. } => Some(*modifiers), - _ => None, - }) - }) - }) - .flatten() - .map(|m| crate::workspace_ui::sticky_mods(app, m)) - .filter(|m| m.command); - let hand = app.ui.tool == Tool::Hand && !picking && !range_picking && !refine_brushing && !pinning && !shadow_drag && curve_point_press.is_none(); - // ⌘Space / ⌥Space zoom under the dialog as they do without one, before Space pans (#6460). - // With the Hand tool, ⌘ alone is Zoom In (#6434); under Curves that press is the dialog's - // point (#7325), not a zoom. - let curve_point_with_hand = curve_point_press.is_some() && app.ui.tool == Tool::Hand; - let held_zoom = - !middle && !curve_point_with_hand && matches!(temporary, Some(crate::hold_keys::Temporary::ZoomIn | crate::hold_keys::Temporary::ZoomOut)); - if held_zoom { - let alt = ctx.input(|i| i.modifiers.alt); - if free_hover { - ctx.set_cursor_icon(crate::tool_cursor::zoom(&ctx, zoom_out(alt))); - } - let b = crate::dialogs::free_buttons(&ctx, rect); - let pointer = ctx.input(|i| i.pointer.latest_pos()); - crate::zoom_tool::drag(app, &ctx, &mut view, &xf, &b, pointer); - if b.clicked - && let Some(p) = pointer - { - let nz = crate::zoom_levels::step(view.zoom, if zoom_out(alt) { -1 } else { 1 }, view.doc_size); - zoom_about(&mut view, &xf, p, nz, app.session.prefs().tools.zoom_clicked_point_to_center, ppp); - } - } else if let Some(d) = crate::dialogs::pan_delta(&ctx, rect, hand) { - let d = xf.unmap_vec(d) / (view.zoom / ppp); - view.center[0] -= d.x; - view.center[1] -= d.y; - ctx.set_cursor_icon(crate::tool_cursor::hand(&ctx, true)); - } else if free_hover && (space_pan || hand) { - ctx.set_cursor_icon(crate::tool_cursor::hand(&ctx, false)); - } else if shadow_drag && !middle && crate::dialogs::free_pointer_over(&ctx, rect).is_some() { - ctx.set_cursor_icon(egui::CursorIcon::Move); - if let Some(p) = crate::dialogs::free_press(&ctx, rect) { - let (start, origin) = ctx.input(|i| (i.pointer.primary_pressed(), i.pointer.press_origin().unwrap_or(p))); - let total = xf.unmap_vec(p - origin) / (view.zoom / ppp); - if let Some(d) = app.ui.dialogs.last_mut() { - crate::layer_style::drag_shadow(&mut d.fields, start, [f64::from(total.x), f64::from(total.y)]); - } - } - } else if picking && let Some(p) = crate::dialogs::free_pointer_over(&ctx, rect) { - if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise { - ctx.set_cursor_icon(egui::CursorIcon::Crosshair); - } else { - ctx.set_cursor_icon(pipette_cursor(&ctx, p)); - } - if let Some(p) = crate::dialogs::free_press(&ctx, rect) { - let d = xf.to_doc(p); - if curves_picking { - crate::adjust_dialog::sample_at(app, d[0], d[1]); - } else if replace_picking { - crate::filter_dialog::color_eyedropper_sample(app, d[0], d[1]); - } else { - crate::color_picker_ui::sample_at(app, d[0], d[1]); - } - } - } else if pinning - && !middle - && let Some(p) = crate::dialogs::free_pointer_over(&ctx, rect) - { - let press = crate::dialogs::free_press(&ctx, rect); - crate::blur_pins::canvas(app, &ctx, &xf, p, press); - } else if refine_brushing - && !middle - && let Some(p) = crate::dialogs::free_pointer_over(&ctx, rect) - { - let press = crate::dialogs::free_press(&ctx, rect).map(|q| xf.to_doc(q)); - crate::select_mask_view::canvas_brush(app, &painter, p, press, xf.zoom_x()); - } else if let Some(mods) = curve_point_press - && let Some(p) = crate::dialogs::free_press(&ctx, rect) - { - let d = xf.to_doc(p); - crate::adjust_dialog::add_point_at(app, &ctx, d[0], d[1], mods.shift); - } else if primary - && !middle - && let Some(p) = crate::dialogs::free_pointer_over(&ctx, rect).filter(|p| img_rect.contains(*p)) - { - // Color Range samples colours with its eyedropper on the image itself. - let press = crate::dialogs::free_press(&ctx, rect) - .filter(|p| img_rect.contains(*p) && ctx.input(|i| i.pointer.primary_pressed() || i.pointer.delta() != egui::Vec2::ZERO)) - .map(|q| xf.to_doc(q)); - crate::color_range_ui::canvas_eyedropper(app, &ctx, p, press); - } + handle_dialog_canvas_input( + app, + DialogCanvasInput { + ctx: &ctx, + view: &mut view, + xf: &xf, + rect, + painter: &painter, + ppp, + primary, + middle, + space_pan, + temporary, + free_hover, + img_rect, + }, + ); } + // The selected Hand yields to an open Free Transform box in this (primary) window: the box // owns the pointer, so the tool behind it cannot steal the resize drag (#2302). The held Hand // (Space) and a middle-button drag still pan, and a secondary window's Hand never stops, as the @@ -881,379 +1837,23 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: crate::layer_picker::sample_at(app, d[0], d[1]); } } else if primary && !stroke_dismissal { - let mods = ui.input(|i| i.modifiers); - // An ⌥-click the window manager took never arrives; say so (alt_grab.rs). - crate::alt_grab::watch(app, &ctx, egui::Id::unique(("pc-alt-grab", idx)), rect, tool, !under_dialog); - // Tools follow the left button; the right one opens the Brush Preset picker or erases - // (Preferences › Tools, `paint_mouse`). - crate::paint_mouse::sync_tool_brush(app); - let mut buttons = crate::paint_mouse::canvas_buttons(app, &response, tool); - // The Crop tool's frame is edited from the press (before egui's drag threshold), so the - // pixels past the canvas show at once, as in Photoshop. - if tool == Tool::Crop && response.is_pointer_button_down_on() && ui.input(|i| i.pointer.primary_down()) && crate::crop_ui::press(app) { - ctx.request_repaint(); - } - // Capture intent at the press, before egui's drag threshold or click replay on release: - // Type transforms keep Command; selections keep Add/Subtract if Shift/Alt is released. - let capture_press = tool.is_type() - || matches!(tool, Tool::RectMarquee | Tool::EllipseMarquee | Tool::SingleRowMarquee | Tool::SingleColumnMarquee | Tool::ObjectSelection); - let tool_press = egui::Id::unique(("tool-pointer-press-modifiers", idx)); - if capture_press - && let Some((p, press_mods)) = ui.input(|i| { - i.events.iter().find_map(|e| match e { - egui::Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers } if rect.contains(*pos) => { - Some((*pos, *modifiers)) - } - _ => None, - }) - }) - { - let press_mods = crate::workspace_ui::sticky_mods(app, press_mods); - ctx.data_mut(|d| d.insert_temp(tool_press, press_mods)); - if tool.is_type() && press_mods.command && app.ui.text_edit.is_some() { - let d = xf.to_doc(p); - tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, press_mods); - } - } - // Right-click while transforming: switch the box's mode (Free Transform, Scale, Rotate, - // Skew, Distort, Perspective, Warp), or rotate or flip the box. - let transforming = app.ui.transform.as_ref().is_some_and(|t| t.warp.is_none()); - if response.secondary_clicked() - && transforming - && let Some(p) = response.interact_pointer_pos() - { - crate::canvas_tool_menu::open_transform(app, &ctx, [p.x, p.y]); - } - let lasso_retracted = response.secondary_clicked() - && match tool { - Tool::Lasso | Tool::FreeformPen => crate::lasso_ui::undo_last_vertex(app), - Tool::PolygonLasso => polygon_retract(app), - _ => false, - }; - // The Lasso (and the Freeform Pen, which draws the same way) reads its own presses and - // drops the moves of a drag it didn't start, so an open Free Transform box takes the - // usual drag path instead (#2153). - if crate::lasso_ui::owns(tool) && !transform_owns_pointer { - crate::lasso_ui::canvas_input(app, &ctx, &xf, &response); - (buttons.started, buttons.dragged, buttons.stopped, buttons.clicked) = (false, false, false, false); - } - // Right-click on a guide where it could be dragged (the Move tool, or ⌘ with a selection - // tool): select it and open the guide menu, as Photoshop does. - let guide_menu = response.secondary_clicked() - && !lasso_retracted - && !transforming - && response.interact_pointer_pos().is_some_and(|p| crate::canvas_tool_menu::open_guide(app, &ctx, tool, mods, [p.x, p.y], xf.to_doc(p))); - // Right-click with the Move tool, or ⌘/Ctrl+right-click: the layers under the pointer. - if response.secondary_clicked() - && !lasso_retracted - && !transforming - && !guide_menu - && crate::layer_pick_ui::is_gesture(tool, mods) - && let Some(p) = response.interact_pointer_pos() - { - let d = xf.to_doc(p); - app.ui.canvas_tool_menu = None; - // No layers there (the pasteboard, with the Move tool): its colour menu instead. - if !crate::layer_pick_ui::open(app, &ctx, [p.x, p.y], d[0], d[1]) && !mods.command && crate::canvas_tool_menu::pasteboard_at(app, tool, d) { - crate::canvas_tool_menu::open_pasteboard(app, &ctx, tool, [p.x, p.y]); - } - } - if response.secondary_clicked() - && !lasso_retracted - && !transforming - && !guide_menu - && !crate::layer_pick_ui::is_gesture(tool, mods) - && let Some(p) = response.interact_pointer_pos() - { - // A pending crop: its menu, also outside the image, where the frame can reach (#6561). - // Outside the image: the pasteboard colour menu (#2462), else the tool's own. The view - // menu belongs to the selected Zoom or Hand, not to Space or a held zoom key. A crop - // frame or Straighten line still dragged by the left button gets no menu over it: the - // gesture and the held button both, as either alone can outlive a lost release. A - // finger held still (egui's press-and-hold right-click) is down too, but drags nothing. - let crop_gesture = tool == Tool::Crop - && !response.long_touched() - && (app.crop.drag.is_some() || app.crop.straighten.line.is_some()) - && ctx.input(|i| i.pointer.primary_down()); - if crop_gesture || crate::canvas_tool_menu::open_crop(app, &ctx, tool, [p.x, p.y]) { - } else if crate::canvas_tool_menu::pasteboard_at(app, tool, xf.to_doc(p)) { - crate::canvas_tool_menu::open_pasteboard(app, &ctx, tool, [p.x, p.y]); - } else if crate::canvas_tool_menu::is_view_tool(tool) { - if tool == app.ui.tool { - crate::canvas_tool_menu::open_view(app, &ctx, tool, [p.x, p.y], xf.to_doc(p)); - } - } else { - crate::canvas_tool_menu::open_at(app, &ctx, tool, [p.x, p.y], Some(xf.to_doc(p))); - } - } - // The (temporary) Hand pans above; its gestures never reach the tool underneath. While a - // Free Transform box is open the Hand does not pan, so the box gets these events (#2302). - if hand_pan { - (buttons.started, buttons.dragged, buttons.stopped) = (false, false, false); - } - // Rotate View: drag around the view centre. Compass clicks are not a rotate drag. - let over_compass = crate::rotate_view::compass_visible(view.rotation) - && response.interact_pointer_pos().is_some_and(|p| crate::rotate_view::compass_rect(xf.rect).contains(p)); - if tool == Tool::RotateView && !over_compass && crate::rotate_view::drag(&ctx, &mut view, &xf, &buttons, response.interact_pointer_pos()) { - (buttons.started, buttons.dragged, buttons.stopped, buttons.clicked) = (false, false, false, false); - } - // Animated Zoom: a Zoom press held still zooms continuously (zoom_hold.rs); while it does, - // it owns the press: no scrub, and its release is no click step. - if tool == Tool::Zoom && !transform_owns_pointer { - let out = zoom_out(ui.input(|i| i.modifiers.alt)); - let on_canvas = !under_dialog && !over_compass && response.is_pointer_button_down_on(); - let hold = crate::zoom_hold::hold_id(idx, primary, ctx.viewport_id()); - if crate::zoom_hold::update(&ctx, &mut view, &xf, app.session.prefs().general.animated_zoom, out, ppp, on_canvas, hold) { - (buttons.started, buttons.dragged, buttons.stopped, buttons.clicked) = (false, false, false, false); - } - } - // Zoom tool drags: scrubby zoom or a zoom rectangle (zoom_tool.rs); clicks step below. - // Like the Hand, the selected Zoom yields to an open Free Transform box (#4799). - if tool == Tool::Zoom && !transform_owns_pointer && crate::zoom_tool::drag(app, &ctx, &mut view, &xf, &buttons, response.interact_pointer_pos()) { - (buttons.started, buttons.dragged, buttons.stopped) = (false, false, false); - } - // A drag is only recognised once the pointer has moved past egui's click distance: the - // gesture starts where the button went down, not where it is now (#123). - let gesture_active_before = app.drag.is_some(); - // Stroke timing: egui's events carry no timestamps, so a press takes the frame time and - // the moves below are spread between the previous point and this frame (`spread_ms`). - let now_ms = ui.input(|i| i.time) * 1000.0; - app.stylus.clock_ms = now_ms; - // Live painting tools start on the press, not once the pointer passes egui's click - // distance: the first dab shows at once. The drag recognised later continues that stroke, - // and a click (or a long press that never moved) just ends it. - let physical_right = app.services.right_button_down.as_ref().is_some_and(|f| f()); - let right_down = ui.input(|i| i.pointer.secondary_down()) || physical_right; - if strokes_live(tool) - && !right_down - && app.drag.is_none() - && response.is_pointer_button_down_on() - && ui.input(|i| i.pointer.primary_pressed()) - && let Some(p) = ui.input(|i| i.pointer.press_origin()).filter(|p| rect.contains(*p)) - { - let d = xf.to_doc(p); - // The press's own modifiers: a ⇧ that arrives with the click still connects the line. - let press_mods = ui.input(|i| pointer_button_modifiers(&i.events, PointerButton::Primary)).unwrap_or(mods); - tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, press_mods); - app.press_stroke = app.drag.is_some(); - } - let press_stroke = app.press_stroke; - // A live tool's press that went to the ⌥ eyedropper keeps sampling for the whole press: the - // drag recognised later must not decide again with ⌥ already let go and paint (#7802). - if press_stroke || (strokes_live(tool) && (app.alt_sampling || app.mixer_sampling)) { - buttons.started = false; - } - if buttons.started - && let Some(p) = ui.input(|i| i.pointer.press_origin()).filter(|p| rect.contains(*p)).or(response.interact_pointer_pos()) - { - if tool == Tool::Move && app.ui.transform.is_none() { - begin_transform_controls_at(app, &ctx, &xf, p); - } - let d = xf.to_doc(p); - let press_mods = if capture_press { ctx.data(|d| d.get_temp(tool_press)).unwrap_or(mods) } else { mods }; - tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, press_mods); - } - // A stroke started on the press takes its moves from the press on, not only once egui - // recognises a drag (past its click distance): otherwise the first few points are lost - // and the stroke starts with a straight segment and no pressure ramp. - let press_stroke_moving = press_stroke && app.drag.is_some(); - if buttons.dragged || buttons.stopped || press_stroke_moving { - // Feed every pointer move the OS delivered this frame, not just the latest position, so - // a fast stroke is sampled densely and renders as a smooth curve instead of a coarse - // polyline. egui-winit pushes one `PointerMoved` per `CursorMoved`, and they accumulate - // while a frame is slow, so reading them all recovers the moves a per-frame - // `interact_pointer_pos()` would drop. Only freehand tools take the whole batch; the - // rest follow the pointer's latest position. The moves are bounded to the gesture's own - // press..release interval (`pointer_moves`), so the start and stop frames keep their - // valid samples without leaking a move from outside the gesture. - let events = ui.input(|i| i.events.clone()); - let button = if response.dragged_by(PointerButton::Secondary) - || response.drag_started_by(PointerButton::Secondary) - || response.drag_stopped_by(PointerButton::Secondary) - { - PointerButton::Secondary - } else { - PointerButton::Primary - }; - let press_this_frame = events.iter().any(|e| matches!(e, egui::Event::PointerButton { button: b, pressed: true, .. } if *b == button)); - let down_at_start = !press_this_frame && (gesture_active_before || buttons.started); - let mut positions = if app.brush_resize.is_some() { - response.interact_pointer_pos().into_iter().collect() - } else if freehand_tool(tool) { - pointer_moves(&events, button, down_at_start) - } else if buttons.dragged { - response.interact_pointer_pos().into_iter().collect() - } else { - Vec::new() - }; - // A frame with no raw move still tracks a held gesture (a still pointer, a modifier - // change): fall back to the latest position, as the old code always did. - if positions.is_empty() - && (buttons.dragged || press_stroke_moving) - && let Some(p) = response.interact_pointer_pos().or(ui.input(|i| i.pointer.latest_pos())) - { - positions.push(p); - } - app.defer_live_stroke = true; - let base = app.stylus.last_point_ms().map_or(now_ms, |t| t.min(now_ms)); - let n = positions.len(); - for (k, p) in positions.into_iter().enumerate() { - app.stylus.clock_ms = spread_ms(base, now_ms, k, n); - // This move's share of the pen samples the frame brought (pressure, tilt, rotation). - app.stylus.select(k, n); - let d = xf.to_doc(p); - tool_event(app, ToolEvent::Move { x: d[0], y: d[1], pressure: app.stylus.pressure() }, mods); - } - app.stylus.clear_selection(); - app.stylus.clock_ms = now_ms; - // A held pen keeps the clock running for airbrush build-up and smoothing catch-up. - if app.drag.is_some() && stroke_wants_time(app) { - ctx.request_repaint_after(std::time::Duration::from_millis(HOLD_REPEAT_MS as u64)); - } - app.defer_live_stroke = false; - // One live-stroke update for the whole frame, not one per recovered sample. - feed_live_stroke(app); - crate::magnetic_lasso_ui::flush(app); - } - if buttons.stopped { - // The live painting press already created the stroke and this release commits it. - // egui may also report clicked on the same release; replaying that click paints a - // second full-pressure dab over the real (possibly light-pressure) pen dab. - if press_stroke { - buttons.clicked = false; - } - let at = response.interact_pointer_pos(); - // #6286: a guide let go over a ruler is removed, whichever ruler it is. Tested here in - // screen space, where the rulers are: on the guide's own axis the release can still be - // inside the document, which is what `finish_drag` sees. - if app.guide_drag.is_some() { - let on_ruler = at.is_some_and(|p| crate::rulers::dropped_on_a_ruler(app, full, p)); - if let Some(g) = app.guide_drag.as_mut() { - g.over_ruler = on_ruler; - } - } - let p = at.map(|p| xf.to_doc(p)).or_else(|| app.drag.as_ref().and_then(|d| d.points.last().map(|q| [q[0], q[1]]))); - if let Some(d) = p { - tap_at_peak_pressure(app); - tool_event(app, ToolEvent::Up { x: d[0], y: d[1] }, mods); - } - } - // A stroke started on the press ends with the release, whether egui saw a drag, a click, - // or neither (a long press that never moved). - if press_stroke && !buttons.stopped && ui.input(|i| i.pointer.primary_released()) { - buttons.clicked = false; - let p = response.interact_pointer_pos().map(|p| xf.to_doc(p)).or_else(|| app.drag.as_ref().and_then(|d| d.points.last().map(|q| [q[0], q[1]]))); - if let Some(d) = p - && app.drag.is_some() - { - tap_at_peak_pressure(app); - tool_event(app, ToolEvent::Up { x: d[0], y: d[1] }, mods); - } - } - if press_stroke && (app.drag.is_none() || !ui.input(|i| i.pointer.primary_down())) { - app.press_stroke = false; - } - if buttons.clicked - && let Some(p) = response.interact_pointer_pos() - { - let click_button = if app.secondary_erase { PointerButton::Secondary } else { PointerButton::Primary }; - let click_mods = ui.input(|i| pointer_button_modifiers(&i.events, click_button)).unwrap_or(mods); - let click_mods = if tool == Tool::ObjectSelection && click_button == PointerButton::Primary { - ctx.data(|d| d.get_temp(tool_press)).unwrap_or(click_mods) - } else { - click_mods - }; - let d = xf.to_doc(p); - match tool { - Tool::Zoom if !transform_owns_pointer => { - let nz = crate::zoom_levels::step(view.zoom, if zoom_out(click_mods.alt) { -1 } else { 1 }, view.doc_size); - let center = app.session.prefs().tools.zoom_clicked_point_to_center; - let doc_anchor = xf.to_doc(p); - let screen_anchor = p; - zoom_about(&mut view, &xf, p, nz, center, ppp); - view.request_zoom_animation(app.session.prefs().general.animated_zoom && nz != display_view.zoom); - view.zoom_anchor = (view.zoom_animating && !center).then_some((doc_anchor, [screen_anchor.x, screen_anchor.y])); - } - // A click with the (temporary) Hand does nothing, never the tool underneath. - Tool::Hand | Tool::RotateView => {} - // Double-clicking closes the polygonal lasso: the first click placed the last - // vertex (or already closed it), so the second never starts a new polygon. egui - // reports it as a triple click when a vertex went down shortly before. - Tool::PolygonLasso if response.double_clicked() || response.triple_clicked() => commit_polygon(app), - // The same for the Magnetic Lasso: along the edges, or straight with ⌥. - Tool::MagneticLasso if response.double_clicked() || response.triple_clicked() => crate::magnetic_lasso_ui::close(app, mods.alt), - // The first click placed (or grabbed) the point; the second toggles it. - Tool::CurvaturePen if response.double_clicked() => crate::vector_ui::curvature_toggle(app, d[0], d[1]), - // Double-clicking a guide opens Preferences › Guides, Grid & Slices, as in Photoshop. - Tool::Move if response.double_clicked() && crate::rulers::guide_at(app, d[0], d[1]).is_some() => { - if let Err(e) = crate::menus::invoke(app, &ctx, "edit.preferences.guidesGridAndSlices", json!({})) { - app.ui.status = e; - } - } - _ => { - if tool == Tool::Move && app.ui.transform.is_none() { - begin_transform_controls_at(app, &ctx, &xf, p); - } - // If this tool was not handled on pointer-down, fall back to a - // click-sized gesture while preserving pen pressure (mouse stays at 1). - tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, click_mods); - tap_at_peak_pressure(app); - tool_event(app, ToolEvent::Up { x: d[0], y: d[1] }, click_mods); - } - } - } - // Magnetic Lasso: between clicks the border follows the pointer with the button up. - if tool == Tool::MagneticLasso && app.ui.magnetic.active() && !(buttons.started || buttons.dragged || buttons.stopped) && response.hovered() { - let moves: Vec = ui.input(|i| i.events.iter().filter_map(|e| if let egui::Event::PointerMoved(p) = e { Some(*p) } else { None }).collect()); - app.defer_live_stroke = true; - for p in moves.into_iter().filter(|p| rect.contains(*p)) { - let d = xf.to_doc(p); - tool_event(app, ToolEvent::Move { x: d[0], y: d[1], pressure: app.stylus.pressure() }, mods); - } - app.defer_live_stroke = false; - crate::magnetic_lasso_ui::flush(app); - } - // Move tool: a double-click on type edits it, as in Photoshop (#1132). Only the selected - // Move tool, not ⌘ held over another tool; an open transform box keeps its double-click - // (commit, below). - if tool == Tool::Move - && app.ui.tool == Tool::Move - && app.ui.transform.is_none() - && response.double_clicked() - && let Some(p) = response.interact_pointer_pos() - { - let d = xf.to_doc(p); - crate::type_tool::edit_at(app, d[0], d[1]); - } - if app.ui.transform.is_some() && response.double_clicked() { - crate::transform_tool::commit(app); - } - if crate::cam_transform::active(app) && response.double_clicked() { - crate::cam_transform::commit(app); - } - // Crop: a double-click inside the frame commits it, as ↵ does (#1792). - if tool == Tool::Crop && response.double_clicked() && response.interact_pointer_pos().is_some_and(|p| crate::crop_ui::commits_at(app, xf.to_doc(p))) { - commit_crop(app); - } - // Type: 2 clicks select a word, 3 the line, 4 the paragraph, 5 all text (Photoshop). - if tool.is_type() - && response.clicked() - && let Some(p) = response.interact_pointer_pos() - { - if crate::type_transform::visible(app, crate::workspace_ui::sticky_mods(app, mods)) { - app.type_clicks = None; - } else { - let n = if response.triple_clicked() { - 3 - } else if response.double_clicked() { - 2 - } else { - 1 - }; - let d = xf.to_doc(p); - crate::type_tool::multi_click(app, n, d[0], d[1]); - } - } + let input = CanvasToolInput { + ctx: &ctx, + response: &response, + xf: &xf, + rect, + full, + tool, + idx, + primary, + transform_owns_pointer, + hand_pan, + ppp, + under_dialog, + zoom_out: &zoom_out, + display_zoom: display_view.zoom, + }; + handle_primary_canvas_input(app, ui, &mut view, &input); } if crop_camera && let (Some((center, rotation)), Some(now)) = (camera, app.ui.views.get(idx)) { if now.center != center { @@ -1274,335 +1874,28 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: } else { xf }; - let (beyond, layer_edges, edge_rect) = painted.unwrap_or_else(|| paint_canvas(app, ui, &display_view, xf)); + let (beyond, layer_edges, edge_rect) = painted.unwrap_or_else(|| paint_canvas(app, ui, &display_view, xf, &paint_input)); // A Hand drag (the tool, Space or the middle button) keeps every overlay on screen, as in // Photoshop: the Pen path being drawn, paths, guides, grid, transform box (#6592). Only the // tool cursors below give way to the fist. let panning = hand_pan && response.dragged(); if primary { - // The transform preview is image content: it goes under the grid and guides, which - // stay visible across a layer being transformed (#5086); the box is drawn later. - crate::transform_tool::draw_content(app, &painter, &xf); - // So are Puppet Warp's, Perspective Warp's and Vanishing Point's warped pixels (#7781). - crate::distort_ui::draw_content(app, &painter, &xf); - if app.ui.extras.grid && app.ui.view.extras { - crate::rulers::draw_grid(app, &painter, &xf, &doc); - } - if app.ui.view.shows(app.ui.view.show.canvas_guides) || crate::guide_layout_ui::preview(app, &doc).is_some() { - crate::rulers::draw_guides(app, &painter, &xf, &doc); - } - // The pending crop frame shows on its own document only, not on the other tiles (#1918). - let foreign_crop = if app.session.active_index() == Some(idx) { None } else { app.ui.crop_rect.take() }; - draw_drag_preview(app, &painter, &xf); - if app.session.active_index() == Some(idx) { - crate::move_box::draw(app, &painter, &xf); - } - if foreign_crop.is_some() { - app.ui.crop_rect = foreign_crop; - } - crate::zoom_tool::draw(&ctx, &painter); - let resizing = crate::brush_resize::draw(app, &painter, &xf); - draw_transform_controls(app, &painter, &xf); - crate::move_hover::draw(app, &painter, &xf, idx, response.hover_pos()); - crate::object_finder::draw(app, &painter, &xf, idx, response.hover_pos()); - crate::layer_pick_ui::show(app, &ctx); - let shown = app.ui.views.get(idx).cloned(); - crate::canvas_tool_menu::show(app, &ctx); - // A view menu row (Zoom In, Fit on Screen…) changed the camera: it wins over this - // frame's copy, which is written back at the end. - if let Some(now) = app.ui.views.get(idx) - && Some(now) != shown.as_ref() - { - view = now.clone(); - ctx.request_repaint(); - } - crate::snap_ui::draw(app, &painter, &xf); - crate::move_distance::draw(app, &painter, &xf, idx, response.hover_pos()); - // The canvas edge isn't outlined while the pixels past it show (Photoshop's crop preview), - // nor while the Crop tool hides everything outside its box (Show Cropped Area off). - if border == photocraft_engine::prefs::CanvasBorder::Line && !beyond && !crate::crop_shield::hides_outside(app) { - painter.rect_stroke(edge_rect, 0.0, Stroke::new(1.0, Color32::from_gray(20)), egui::StrokeKind::Outside); - } - crate::type_tool::draw_overlay(app, &painter, &xf); - crate::transform_tool::draw_overlay(app, &painter, &xf); - crate::cam_transform::draw(app, &painter, &xf); - crate::cam_preview::wake(app, painter.ctx()); - crate::distort_ui::draw_overlay(app, &painter, &xf); - crate::perspective_crop_ui::draw_overlay(app, &painter, &xf); - crate::retouch_ui::draw_source_marker(app, &painter, &xf); - crate::vector_ui::draw_overlay(app, &painter, &xf, &doc); - crate::symmetry_ui::draw(app, &painter, &xf); - crate::analysis_ui::draw_overlay(app, &painter, &xf); - crate::gradient_ui::draw_overlay(app, &painter, &xf); - crate::slice_ui::draw_overlay(app, &painter, &xf); - crate::frame_tool::draw_overlay(app, &painter, &xf); - crate::artboard_tool::draw_overlay(app, &painter, &xf); - // Tool cursors (Photoshop-style). - let guide_hover = response.hover_pos().and_then(|p| { - let d = xf.to_doc(p); - if tool == Tool::Move { - return crate::rulers::guide_at(app, d[0], d[1]); - } - command_guide_at(app, tool, d, crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers))) - }); - if panning { - // The fist set with the pan above stays (#2196). - } else if crate::tool_cursor::pointer_elsewhere(&ctx, &crate::panel_docking::native_viewports(app, &ctx)) { - // The pointer is over a floating panel's own window; this window's last position of - // it is stale, so no tool cursor or brush outline is painted at it. - } else if let Some((vertical, _)) = guide_hover { - ui.ctx().set_cursor_icon(crate::rulers::guide_cursor(&xf, vertical)); - } else if let Some((p, mods, c)) = response.hover_pos().and_then(|p| { - let mods = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)); - let c = crate::transform_tool::cursor(app, &xf, xf.to_doc(p), mods)?; - Some((p, mods, c)) - }) { - ui.ctx().set_cursor_icon(c); - // CursorIcon::None hides the pointer; the curved arrow has to be drawn or the - // rotate zone looks like a plain cursor (#4050). - let d = xf.to_doc(p); - if let Some(k) = crate::transform_tool::rotate_cursor_dir(app, d) { - 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 c == egui::CursorIcon::None { - // Likewise the distort and skew pointers over a handle (#5405). - crate::transform_tool::draw_handle_cursor(app, &ctx, &xf, p, mods); - } - } else if let Some(c) = response.hover_pos().and_then(|p| crate::symmetry_ui::cursor(app, &xf, xf.to_doc(p))) { - ui.ctx().set_cursor_icon(c); - } else if let Some(c) = response.hover_pos().and_then(|p| crate::cam_transform::cursor(app, &ctx, &xf, p)) { - ui.ctx().set_cursor_icon(c); - } else if let Some(p) = response.hover_pos().filter(|p| crate::corner_radius::hovers(app, xf.to_doc(*p))) { - // Over a live rectangle's corner widget (or dragging one): the arrow with a rounded - // corner badge, as Photoshop's corner radius cursor. - ui.ctx().set_cursor_icon(egui::CursorIcon::Default); - crate::icons::cursor_badge(&ctx, "scan", p); - } else if let Some((r, p, c)) = response - .hover_pos() - .filter(|_| !transform_controls_several(app)) - .and_then(|p| transform_controls_rect(app, &xf).and_then(|r| transform_controls_cursor(r, p).map(|c| (r, p, c)))) - { - // The Move tool draws handles without opening a Free Transform session. - // Give them their resize/rotate cursors before the first pointer press (#2871). - ui.ctx().set_cursor_icon(c); - if let Some(toward) = transform_controls_turn(r, p) { - crate::crop_ui::draw_turn_cursor(&ctx, p, toward); - } - } else if let Some(c) = response.hover_pos().filter(|_| tool.is_type()).and_then(|p| { - let mods = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)); - crate::type_transform::cursor(app, &ctx, &xf, p, mods) - }) { - ui.ctx().set_cursor_icon(c); - } else if let Some(c) = response.hover_pos().filter(|_| tool == Tool::Artboard).and_then(|p| crate::artboard_tool::cursor(app, &xf, xf.to_doc(p))) { - ui.ctx().set_cursor_icon(c); - } else if let Some((p, c)) = response.hover_pos().filter(|_| tool == Tool::Crop).and_then(|p| Some((p, crate::crop_ui::cursor(app, xf.to_doc(p))?))) { - ui.ctx().set_cursor_icon(c); - let d = xf.to_doc(p); - if let Some(k) = crate::crop_ui::turn_cursor_dir(app, d) { - 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 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 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 { - draw_clone_preview(app, &painter, &xf, xf.to_doc(p), output); - } - // The Type tool over a path shows Photoshop's area-type or type-on-a-path pointer (#218). - // Not while a type edit is open or a text box is dragged out, nor over type a click - // would edit (`hover_path_target`). - let type_path = if matches!(tool, Tool::Type | Tool::VerticalType) { - let [x, y] = xf.to_doc(p); - let shift = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)).shift; - crate::type_tool::hover_path_target(app, x, y, shift) - } else { - None - }; - 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`): its cursor too, - // unless Preferences › Cursors › Other Cursors asks for the precise crosshair. - t if app.alt_sampling - || (app.drag.is_none() - && !app.services.right_button_down.as_ref().is_some_and(|f| f()) - && alt_samples(t, crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)))) => - { - // While it samples (button held), the comparison ring replaces the cursor (#213). - if let Some(icon) = eyedropper_ring(app, &painter, &xf, p, response.is_pointer_button_down_on()) { - icon - } else 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 - // full size), precise crosshair, or the standard pointer. - use photocraft_engine::prefs::PaintingCursor; - let cur = app.session.prefs().cursors.clone(); - // ⇪ forces the precise crosshair, whatever the cursor preference (#1758). - if app.caps_lock { - crate::tool_cursor::crosshair(&painter, p, 6.0, 0.0) - } else { - let painting = app.drag.is_some(); - let brush = &app.session.tools.brush; - let full = (brush.size / 2.0 * xf.zoom).max(1.0); - match cur.painting { - PaintingCursor::Standard => egui::CursorIcon::Default, - PaintingCursor::Precise => crate::tool_cursor::crosshair(&painter, p, 6.0, 0.0), - _ if painting && cur.show_only_crosshair_while_painting => crate::tool_cursor::crosshair(&painter, p, 5.0, 0.0), - // The Pencil (and the Eraser in Pencil mode): the outline of the - // whole pixels its dab fills, on the pixel grid. A round tip is a - // disc, a tiny one the whole square (#2662). The Block-mode Eraser: - // its square, 16 screen pixels at any zoom (#2770). - _ if tool == Tool::Pencil || crate::eraser_ui::pencil_mode(app, tool) || crate::eraser_ui::block_mode(app, tool) => { - let block = crate::eraser_ui::block_mode(app, tool); - let size = if block { crate::eraser_ui::block_size(app) } else { brush.size }; - let ppp = painter.ctx().pixels_per_point(); - let sq = pencil_cursor_rect(&xf, xf.to_doc(p), size, ppp); - let px = 1.0 / ppp; - let (dark, light) = (Stroke::new(px, Color32::from_black_alpha(160)), Stroke::new(px, Color32::from_white_alpha(230))); - if !block && pencil_tip_is_round(size) { - let r = sq.width() / 2.0; - painter.circle_stroke(sq.center(), r + px / 2.0, dark); - painter.circle_stroke(sq.center(), (r - px / 2.0).max(0.0), light); - } else { - painter.rect_stroke(sq, 0.0, dark, egui::StrokeKind::Outside); - painter.rect_stroke(sq, 0.0, light, egui::StrokeKind::Inside); - } - // Too small to see where it is: the hotspot as well. - if cur.show_crosshair_in_brush_tip || sq.width() < 6.0 { - crate::tool_cursor::crosshair(&painter, p, 4.0, 0.0); - } - egui::CursorIcon::None - } - // A shaped tip (sampled bitmap, or a rotated/flattened round tip) - // traces its outline, turned and flipped with the view; a plain round - // tip keeps the circle. - _ => { - let centre = brush_tip_centre(tool, alt || app.ui.shell.sticky_alt, cur.show_crosshair_in_brush_tip); - let axes = crate::brush_cursor::pen_axes(&xf, brush, app.stylus.sample()); - crate::brush_cursor::cursor(&painter, p, brush, full, axes, cur.painting == PaintingCursor::NormalTip, centre) - } - } - } - } - // The Eyedropper: the comparison ring follows the pointer while it samples (#213); - // otherwise its pipette, or the crosshair for precise Other Cursors. - Tool::Eyedropper => { - if let Some(icon) = eyedropper_ring(app, &painter, &xf, p, response.is_pointer_button_down_on()) { - icon - } else if precise_other_cursor(app) { - egui::CursorIcon::Crosshair - } else { - pipette_cursor(ui.ctx(), p) - } - } - // The Color Sampler: a pipette, or the crosshair for precise Other Cursors. - Tool::ColorSampler => { - if precise_other_cursor(app) { - egui::CursorIcon::Crosshair - } else { - pipette_cursor(ui.ctx(), p) - } - } - // The Paint Bucket: a bucket, or the crosshair for precise Other Cursors (#3968). - Tool::PaintBucket => { - if precise_other_cursor(app) { - egui::CursorIcon::Crosshair - } else { - paint_bucket_cursor(ui.ctx(), p) - } - } - // Over the first anchor, the close-path pointer (#4596), before the pens' own cursors. - Tool::Pen | Tool::CurvaturePen - if { - let [x, y] = xf.to_doc(p); - crate::vector_ui::closes_path_at(app, tool, x, y) - } => - { - crate::tool_cursor::close_path(&painter, p) - } - // The pen tools: their own icon with its nib on the point, or the crosshair for - // precise Other Cursors (#4639). - Tool::Pen | Tool::FreeformPen | Tool::CurvaturePen | Tool::AddAnchorPoint | Tool::DeleteAnchorPoint | Tool::ConvertPoint => { - if precise_other_cursor(app) { - egui::CursorIcon::Crosshair - } else { - pen_cursor(ui.ctx(), tool, p) - } - } - // Preferences › Cursors › Other Cursors: Precise shows a crosshair for every tool. - Tool::Move | Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask - if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise => - { - egui::CursorIcon::Crosshair - } - // Photoshop's Move tool keeps the standard arrow pointer (#5646): the movable - // layer under it is marked by the hover outline, not by a different cursor. - Tool::Move => egui::CursorIcon::Default, - // Dragging is handled with the pan itself, above. - Tool::Hand => crate::tool_cursor::hand(ui.ctx(), false), - Tool::RotateView => crate::rotate_view::cursor(response.dragged()), - Tool::Zoom => crate::tool_cursor::zoom(ui.ctx(), zoom_out(alt)), - Tool::Type | Tool::VerticalType if type_path.is_some() => match type_path { - Some(crate::type_tool::PathClick::Area { .. }) => crate::tool_cursor::area_type(&painter, p), - _ => crate::tool_cursor::path_type(&painter, p), - }, - Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask => egui::CursorIcon::Text, - Tool::MagneticLasso => crate::magnetic_lasso_ui::cursor(app, &painter, p, xf.zoom), - Tool::RedEye => { - let pupil = app.ui.tool_options.red_eye_pupil_size.clamp(1.0, 100.0); - let r_doc = photocraft_algo::redeye::search_radius(pupil); - let r = (r_doc * xf.zoom).max(2.0); - painter.circle_stroke(p, r + 0.5, Stroke::new(1.0, Color32::from_black_alpha(140))); - painter.circle_stroke(p, r, Stroke::new(1.0, Color32::from_white_alpha(220))); - for (w, c) in [(2.5, Color32::from_black_alpha(140)), (1.0, Color32::from_white_alpha(220))] { - painter.line_segment([p - vec2(3.0, 0.0), p + vec2(3.0, 0.0)], Stroke::new(w, c)); - painter.line_segment([p - vec2(0.0, 3.0), p + vec2(0.0, 3.0)], Stroke::new(w, c)); - } - egui::CursorIcon::None - } - _ => egui::CursorIcon::Crosshair, - }; - let icon = visible_crosshair(icon, &painter, p, cfg!(not(target_arch = "wasm32"))); - ui.ctx().set_cursor_icon(icon); - // Selection tools: + / − / × badge for the effective mode (#170). A gesture keeps the - // mode it started with (⇧ then constrains the marquee instead of adding). - let held = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)); - let mods = app.drag.as_ref().map_or(held, |d| d.modifiers); - if let Some(b) = crate::tool_feedback::badge(app, tool, mods) { - crate::tool_feedback::draw_badge(&painter, p, b, tool == Tool::QuickSelection); - } - // ⇧ after a stroke: the straight line a click would paint (#257). - crate::stroke_constraint::draw_line_preview(app, &painter, &xf, p, tool, held.shift); - } + let input = CanvasOverlayInput { + ctx: &ctx, + painter: &painter, + xf: &xf, + response: &response, + doc: &doc, + tool, + idx, + output, + border, + beyond, + edge_rect, + panning, + zoom_out: &zoom_out, + }; + paint_primary_overlays(app, ui, &mut view, &input); } app.tool_override = None; if let Some(b) = layer_edges_rect(app, idx) { diff --git a/crates/ui-egui/src/canvas/cursors.rs b/crates/ui-egui/src/canvas/cursors.rs index a7048ea3c5..06819c5ea1 100644 --- a/crates/ui-egui/src/canvas/cursors.rs +++ b/crates/ui-egui/src/canvas/cursors.rs @@ -1,6 +1,6 @@ use super::tool_input::ToolEvent; use super::view_xform::ViewXform; -use super::*; +use super::{Color32, PhotocraftApp, Pos2, Rect, Stroke, Tool, json, pos2, vec2}; /// Windows' crosshair cursor inverts the pixels under it, so over mid-grey (the pasteboard, many /// photos) it vanishes (#737), and the Linux themes' and macOS's differ. Use the same black-and-white diff --git a/crates/ui-egui/src/canvas/display.rs b/crates/ui-egui/src/canvas/display.rs index 1cfae7a46d..3b111232bd 100644 --- a/crates/ui-egui/src/canvas/display.rs +++ b/crates/ui-egui/src/canvas/display.rs @@ -1,5 +1,5 @@ use super::strokes::live_stroke; -use super::*; +use super::{Color32, Document, LayerContent, PhotocraftApp, TextureOptions}; pub(super) fn checker(app: &mut PhotocraftApp, ctx: &egui::Context) -> egui::TextureId { // Preferences › Transparency & Gamut colours (the texture is rebuilt when they change). diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs index a58e8db11b..6083756416 100644 --- a/crates/ui-egui/src/canvas/drag.rs +++ b/crates/ui-egui/src/canvas/drag.rs @@ -1,5 +1,5 @@ use super::view_xform::ViewXform; -use super::*; +use super::{PhotocraftApp, Pos2, Rect, Stroke, Tool, vec2}; /// An in-progress pointer gesture on the canvas. #[derive(Clone, Debug)] diff --git a/crates/ui-egui/src/canvas/filter_preview.rs b/crates/ui-egui/src/canvas/filter_preview.rs index 6940b7b720..a7d3f8dc94 100644 --- a/crates/ui-egui/src/canvas/filter_preview.rs +++ b/crates/ui-egui/src/canvas/filter_preview.rs @@ -1,6 +1,6 @@ use super::display::canvas_display; use super::textures::texture_buffer; -use super::*; +use super::{Document, LayerContent, PhotocraftApp}; /// The proxy factor of a live preview of filter `cmd` with `params` on `doc`, the active document: /// reduced on a large document (Preferences › Performance › Low Resolution Previews), except for @@ -125,7 +125,7 @@ pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &e let buffer = photocraft_compose::flatten(&doc); (std::sync::Arc::new(doc), buffer) }); - crate::filter_preview_worker::Computed { result, ms: crate::gpu_canvas::now_ms() - t0 } + crate::dialogs::filter_preview_worker::Computed { result, ms: crate::gpu_canvas::now_ms() - t0 } }) { app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: request.clone(), result: None, committing: false }); crate::notices::error(app, error); diff --git a/crates/ui-egui/src/canvas/gesture.rs b/crates/ui-egui/src/canvas/gesture.rs index 81c0b231a4..251b6c9205 100644 --- a/crates/ui-egui/src/canvas/gesture.rs +++ b/crates/ui-egui/src/canvas/gesture.rs @@ -4,7 +4,7 @@ use super::strokes::{ EngineStroke, commit_brush_stroke, feed_live_stroke, live_retouch_command, offer_live_retouch, paint_target, snap_aliased_tip, stroke_params, }; use super::textures::shown_as_document; -use super::*; +use super::{PhotocraftApp, Tool, json}; pub(crate) fn finish_gesture(app: &mut PhotocraftApp, d: Drag) { // Control can select a different tool and release before the next frame cancels the drag. diff --git a/crates/ui-egui/src/canvas/gpu_canvas/budget.rs b/crates/ui-egui/src/canvas/gpu_canvas/budget.rs index c735918576..9f5df3ebd6 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/budget.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/budget.rs @@ -1,5 +1,5 @@ use super::doc_textures::mip_count; -use super::*; +use super::wgpu; /// Floor of the compositor's memory budget: enough for a viewport's pages of a dozen layers. pub const MIN_GPU_BUDGET: u64 = 512 << 20; diff --git a/crates/ui-egui/src/canvas/gpu_canvas/callback.rs b/crates/ui-egui/src/canvas/gpu_canvas/callback.rs index c128a8f297..23e11399b0 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/callback.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/callback.rs @@ -1,5 +1,5 @@ use super::resources::{CanvasStyle, Resources, ViewGpu, f32_bytes}; -use super::*; +use super::{CallbackResources, CallbackTrait, ScreenDescriptor, VIEW_FLOATS, VIEW_UNIFORM_SIZE, ViewParams, wgpu}; // --------------------------------------------------------------------------------------------- // Per-frame callback diff --git a/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs b/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs index 05a40faa1a..fb09f08e14 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs @@ -1,7 +1,7 @@ use super::budget::canvas_texture_bytes; use super::resources::{DocTextures, Resources, Tile, f32_bytes}; use super::texels::texel_bytes; -use super::*; +use super::{Ordering, WHOLE_MIP_SIDE, wgpu}; pub(super) fn mip_count(w: u32, h: u32) -> u32 { 32 - w.max(h).max(1).leading_zeros() diff --git a/crates/ui-egui/src/canvas/gpu_canvas/perf.rs b/crates/ui-egui/src/canvas/gpu_canvas/perf.rs index 8c22dfbf89..4e9068ebeb 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/perf.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/perf.rs @@ -1,7 +1,7 @@ use super::budget::video_memory; use super::setup::Refresh; use super::texels::LayerFit; -use super::*; +use super::wgpu; /// Milliseconds since the process (or page) started, from a monotonic clock. /// diff --git a/crates/ui-egui/src/canvas/gpu_canvas/resources.rs b/crates/ui-egui/src/canvas/gpu_canvas/resources.rs index 27606b7c05..721377e928 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/resources.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/resources.rs @@ -2,7 +2,7 @@ use super::budget::{canvas_budget, physical_memory}; use super::doc_textures::init_buffer; use super::shaders::{CANVAS_WGSL, ENCODE_WGSL, MIP_WGSL}; use super::texels::LayerFit; -use super::*; +use super::{FORMAT, FORMAT_HIGH, HashMap, NonZeroU64, TILE_UNIFORM_SIZE, VIEW_FLOATS, VIEW_UNIFORM_SIZE, wgpu}; pub(super) struct Resources { pub(super) view_bgl: wgpu::BindGroupLayout, diff --git a/crates/ui-egui/src/canvas/gpu_canvas/setup.rs b/crates/ui-egui/src/canvas/gpu_canvas/setup.rs index 8195148a45..d36b97df82 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/setup.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/setup.rs @@ -1,5 +1,5 @@ use super::texels::texels; -use super::*; +use super::{egui_wgpu, wgpu}; /// A CPU composite as the canvas texture stores it (`display`'s encoding; as is without one). pub(super) fn texture_buffer<'a>( diff --git a/crates/ui-egui/src/canvas/gpu_canvas/texels.rs b/crates/ui-egui/src/canvas/gpu_canvas/texels.rs index 966b23c40c..5350ea5fae 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/texels.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/texels.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{FORMAT, FORMAT_HIGH, wgpu}; /// Straight-alpha f32 RGBA (sRGB-encoded) → premultiplied RGBA8, split across threads on native. pub fn premultiply_rgba8(px: &[[f32; 4]]) -> Vec { diff --git a/crates/ui-egui/src/canvas/navigator.rs b/crates/ui-egui/src/canvas/navigator.rs index d95c9a20ec..413bf82686 100644 --- a/crates/ui-egui/src/canvas/navigator.rs +++ b/crates/ui-egui/src/canvas/navigator.rs @@ -1,7 +1,7 @@ use super::display::{canvas_display, display_doc, display_image}; use super::strokes::live_stroke; use super::textures::{effect_reach, ensure_texture}; -use super::*; +use super::{Document, PhotocraftApp, Rect, TextureOptions}; /// Longest side of the Navigator panel's image (about twice the panel's width, for HiDPI). pub const NAVIGATOR_SIDE: u32 = 512; diff --git a/crates/ui-egui/src/canvas/paint.rs b/crates/ui-egui/src/canvas/paint.rs index b9a040257e..415bb071a7 100644 --- a/crates/ui-egui/src/canvas/paint.rs +++ b/crates/ui-egui/src/canvas/paint.rs @@ -6,7 +6,7 @@ use super::gesture::constrained_polygon_point; use super::strokes::{EngineStroke, SPOT_HEALING_TRAIL, draw_smoothing_string, strokes_live}; use super::textures::texture_side; use super::view_xform::ViewXform; -use super::*; +use super::{Color32, DRect, Document, Mesh, PhotocraftApp, Pos2, Rect, Stroke, TextureOptions, Tool, json, pos2, vec2}; /// A texture mapped over the `size` document area starting at `origin` (follows a rotated view). pub(super) fn paint_mapped_image_at(painter: &egui::Painter, xf: &ViewXform, tex: egui::TextureId, uv: Rect, origin: [f32; 2], size: [f32; 2], tint: Color32) { diff --git a/crates/ui-egui/src/canvas/selection.rs b/crates/ui-egui/src/canvas/selection.rs index fe096f1159..67577e2a27 100644 --- a/crates/ui-egui/src/canvas/selection.rs +++ b/crates/ui-egui/src/canvas/selection.rs @@ -1,5 +1,5 @@ use super::tool_input::selection_drag_kind; -use super::*; +use super::{PhotocraftApp, Tool, json}; /// Photoshop's cursor over a selection: what a press (or the drag under way) would do. #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/crates/ui-egui/src/canvas/start_screen.rs b/crates/ui-egui/src/canvas/start_screen.rs index d22295886c..e8b6633e5a 100644 --- a/crates/ui-egui/src/canvas/start_screen.rs +++ b/crates/ui-egui/src/canvas/start_screen.rs @@ -1,5 +1,5 @@ use super::tabs::{open_on_double_click, paint_dots}; -use super::*; +use super::{PhotocraftApp, Rect, Sense}; /// The Home screen's card is this wide when the canvas has room, and as wide as the canvas when it doesn't. const HOME_CARD_WIDTH: f32 = 460.0; diff --git a/crates/ui-egui/src/canvas/strokes.rs b/crates/ui-egui/src/canvas/strokes.rs index c970c39a20..11a288eb6a 100644 --- a/crates/ui-egui/src/canvas/strokes.rs +++ b/crates/ui-egui/src/canvas/strokes.rs @@ -2,7 +2,7 @@ use super::display::{canvas_display, display_image}; use super::drag::Drag; use super::textures::{canvas_texture_options, shown_as_document}; use super::view_xform::ViewXform; -use super::*; +use super::{Color32, DRect, PhotocraftApp, Pos2, Stroke, Tool, json, pos2}; pub(crate) struct ClonePreviewCache { pub(super) key: String, diff --git a/crates/ui-egui/src/canvas/tabs.rs b/crates/ui-egui/src/canvas/tabs.rs index 5f59f3c028..82a1e2089f 100644 --- a/crates/ui-egui/src/canvas/tabs.rs +++ b/crates/ui-egui/src/canvas/tabs.rs @@ -1,6 +1,6 @@ use super::start_screen::start_screen; use super::textures::retain_gpu_documents; -use super::*; +use super::{Color32, PhotocraftApp, Pos2, Rect, Sense, Stroke, TextureOptions, canvas_view, fmt_zoom, json, mode_label, pos2}; /// Tabs + canvas for the active document, or the start screen; then drops layers dragged onto /// another document (`layer_transfer`). diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index a90df6af98..f213a67d9d 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -285,7 +285,7 @@ fn every_filter_previews_off_the_ui_thread_and_a_newer_value_cancels_the_old() { std::thread::sleep(std::time::Duration::from_millis(1)); } let _ = told.send(job.cancelled()); - crate::filter_preview_worker::Computed { result: None, ms: 0.0 } + crate::dialogs::filter_preview_worker::Computed { result: None, ms: 0.0 } }) .unwrap(); // The slider moves on and stops: once the value settles, the stale preview is cancelled @@ -352,7 +352,7 @@ fn gated_blur_preview(app: &mut PhotocraftApp, ctx: &egui::Context) -> (std::syn let buffer = photocraft_compose::flatten(&d); (std::sync::Arc::new(d), buffer) }); - crate::filter_preview_worker::Computed { result, ms: 0.0 } + crate::dialogs::filter_preview_worker::Computed { result, ms: 0.0 } }) .expect("the worker starts"); (release, radius) @@ -482,7 +482,7 @@ fn a_mid_drag_empty_result_leaves_the_preview_alone() { app.filter_preview_worker .start(key, ctx.clone(), move |_| { wait.recv_timeout(std::time::Duration::from_secs(60)).expect("released"); - crate::filter_preview_worker::Computed { result: None, ms: 0.0 } + crate::dialogs::filter_preview_worker::Computed { result: None, ms: 0.0 } }) .expect("the worker starts"); app.ui.dialogs[0].fields.insert("radius".into(), json!(moved + 4.0)); diff --git a/crates/ui-egui/src/canvas/textures.rs b/crates/ui-egui/src/canvas/textures.rs index 17b6f1bf26..5c6ad3f12a 100644 --- a/crates/ui-egui/src/canvas/textures.rs +++ b/crates/ui-egui/src/canvas/textures.rs @@ -3,7 +3,7 @@ use super::filter_preview::filter_channel_preview; use super::navigator::navigator_waits_for_drag; use super::strokes::live_stroke; use super::view_xform::ViewXform; -use super::*; +use super::{CanvasCache, CpuTextures, DRect, DetailTexture, Document, InView, LayerContent, PhotocraftApp, TextureOptions, pos2}; /// Largest texture side we upload; bigger documents are uploaded downsampled, with the part in /// view added at the screen's resolution when zoomed in (`detail_plan`). diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs index a58555037a..d193bf6e00 100644 --- a/crates/ui-egui/src/canvas/tool_input.rs +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -3,7 +3,7 @@ use super::drag::Drag; use super::gesture::{finish_gesture, polygon_click, selection_mode}; use super::selection::finish_selection_drag; use super::strokes::{HOLD_REPEAT_MS, begin_live_stroke, feed_live_stroke, paint_target, snap_aliased_tip, stroke_params, strokes_live}; -use super::*; +use super::{PhotocraftApp, Tool, json}; /// Abstract tool event, produced by the mouse or by automation (`ui.pointer`). #[derive(Clone, Copy, Debug, PartialEq)] @@ -52,8 +52,7 @@ 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) { +fn preflight_tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> Option<(bool, egui::Modifiers)> { // A press anywhere but on a guide deselects the selected guide (`guide_select`). if let ToolEvent::Down { x, y, .. } = ev && app.ui.selected_guide.is_some() @@ -62,7 +61,7 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) crate::guide_select::clear(app); } if crate::symmetry_ui::pointer(app, ev, crate::workspace_ui::sticky_mods(app, mods)) { - return; + return None; } // 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). @@ -70,11 +69,11 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) crate::transform_tool::end_if_left(app); let mods = crate::workspace_ui::sticky_mods(app, mods); if app.ui.transform.is_none() && crate::type_transform::pointer(app, ev, mods) { - return; + return None; } // Content-Aware Move's Transform On Drop box takes every press until ↩ or Esc. if crate::cam_transform::pointer(app, ev, mods) { - return; + return None; } // A Mixer Brush press that loaded paint stays a sample press until it is released, with ⌥ // let go or not (`mixer_sampling`; `canvas_view` holds back egui's later drag start too). @@ -82,9 +81,9 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) ToolEvent::Down { .. } => app.mixer_sampling = false, ToolEvent::Up { .. } if app.mixer_sampling => { app.mixer_sampling = false; - return; + return None; } - _ if app.mixer_sampling => return, + _ if app.mixer_sampling => return None, _ => {} } // The tool in use, so a held temporary tool (⌘ = Move) with ⌥ doesn't load paint. @@ -98,13 +97,17 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) app.ui.status_error = true; } } - return; + return None; } + Some((armed, mods)) +} + +fn filter_event_before_tool(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers, armed: bool) -> Option<(ToolEvent, ToolEvent, egui::Modifiers)> { // View › Snap / Snap To and smart guides (snap_ui.rs). let raw = ev; // The Move tool's marquee selecting layers takes its moves and release unsnapped (move_box.rs). if crate::move_box::pointer(app, raw) { - return; + return None; } // A press anywhere but on the floating piece (or with ⇧ / ⌥, to draw) drops it first; the // Move tool (⌘ with a painting tool too, `event_tool`) drags it from anywhere. @@ -120,29 +123,29 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) // can land outside the corner's grab area and turn a corner drag into a move of the whole box. let transform_ev = if matches!(raw, ToolEvent::Down { .. }) { raw } else { ev }; if crate::transform_tool::pointer(app, transform_ev, mods) { - return; + return None; } if crate::distort_ui::pointer(app, ev, mods) { - return; + return None; } if crate::perspective_crop_ui::pointer(app, ev) { - return; + return None; } // 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; + return None; } // 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; + return None; } // ⌥ (Alt) with a painting tool is the Eyedropper for that press. if alt_eyedropper(app, ev, mods) { - return; + return None; } // Move tool: ⇧ locks the axis, ⌥ duplicates (move_mods.rs). let ev = crate::move_mods::filter_event(app, ev, mods); @@ -150,39 +153,39 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) // transform controls). Unsnapped, as Free Transform's handles: snapping would pull the press // off a widget onto a nearby vertex or guide, and the shape tool would draw a new shape. if crate::corner_radius::pointer(app, raw, mods) { - return; + return None; } // Direct Selection, and the Pen's ⌘ (Direct Selection) and ⌥ (Convert Point) modes (#790). if crate::direct_select::pointer(app, ev, mods) { - return; + return None; } // Add / Delete Anchor Point tools (#1044). if crate::anchor_tools::pointer(app, ev, mods) { - return; + return None; } // Ruler, Count and Note tools. if crate::analysis_ui::pointer(app, ev, mods) { - return; + return None; } // Slice and Slice Select tools. if crate::slice_ui::pointer(app, ev, mods) { - return; + return None; } // Frame tool: draw rectangular and elliptical frames. if crate::frame_tool::pointer(app, ev, mods) { - return; + return None; } // Artboard tool: draw, select, move and resize boards. if crate::artboard_tool::pointer(app, ev, mods) { - return; + return None; } // Crop tool: draw, move and resize the frame. if crate::crop_ui::pointer(app, ev, mods) { - return; + return None; } // Gradient tool, live mode: draw and edit Gradient Fill layers. if crate::gradient_ui::pointer(app, ev, mods) { - return; + return None; } // ⌘ over a guide takes the guide (#2690, #6288); the guide drag below moves it. if let ToolEvent::Down { x, y, .. } = raw @@ -190,15 +193,107 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) { app.guide_drag = Some(crate::rulers::GuideDrag::existing(vertical, i, [x, y])); crate::snap_ui::begin_guide(app, [x, y]); - return; + return None; } if crate::lasso_ui::pointer(app, ev, mods) { - return; + return None; } // Magnetic Lasso: fastening points and the border following the pointer. if crate::magnetic_lasso_ui::pointer(app, ev, mods) { - return; + return None; } + Some((raw, ev, mods)) +} + +fn handle_move_pointer(app: &mut PhotocraftApp, ev: ToolEvent, tool: Tool, mods: egui::Modifiers) -> bool { + match ev { + ToolEvent::Down { x, y, pressure } if tool == Tool::Move => { + if let Some((vertical, i)) = crate::rulers::guide_at(app, x, y) { + // ⌥-click turns the guide, through the point clicked, as in Photoshop. + if mods.alt && !mods.shift && !mods.command { + crate::guide_select::flip(app, vertical, i, [x, y]); + return true; + } + app.guide_drag = Some(crate::rulers::GuideDrag::existing(vertical, i, [x, y])); + return true; + } + // Auto-Select (or ⌘-click while it is off) picks the layer under the pointer first + // (not when the selected pixels move: those are the active layer's). + if !crate::move_ui::moves_selected_pixels_with(app, tool) && app.ui.tool_options.move_auto_select != mods.command { + let target = app.ui.tool_options.move_target.clone(); + let mode = if mods.shift { "add" } else { "replace" }; + let before = app.session.active().map(|st| st.selected_layers()); + // A locked layer under the pointer (the Background) can't move. Photoshop draws + // the layer marquee instead of reporting the lock (#4058). Probe without + // selecting so a locked hit doesn't become the selection first. + let probe = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "select": false})); + let locked_hit = probe.as_ref().ok().and_then(|v| v["layer"].as_u64()).is_some_and(|id| { + app.session.active().is_some_and(|st| { + let id = photocraft_doc::LayerId(id); + let locks = st.doc.effective_locks(id); + locks.position || locks.all || st.doc.is_background(id) + }) + }); + if locked_hit || (probe.as_ref().is_ok_and(|v| v["layer"].is_null()) && crate::move_box::empty_at(app, [x, y])) { + if locked_hit { + // A click still selects the locked layer, as in Photoshop; a drag's + // marquee then replaces that selection on release. + let _ = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "mode": mode})); + } + crate::move_box::begin(app, [x, y], mods.shift); + return true; + } + let picked = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "mode": mode})); + // No pixels there at all (an empty spot, or off the canvas): drag a marquee that + // selects the layers it touches instead (move_box.rs). + if picked.is_ok_and(|v| v["layer"].is_null()) && crate::move_box::empty_at(app, [x, y]) { + crate::move_box::begin(app, [x, y], mods.shift); + return true; + } + // Snapping (and the drag's box) started before the pick: point it at what moves. + if app.session.active().map(|st| st.selected_layers()) != before { + crate::snap_ui::retarget_move(app, [x, y], mods); + } + } + // A locked layer: no drag, and Photoshop's message once the pointer moves (`move_lock`). + if crate::move_lock::blocked(app, tool, [x, y], mods) { + app.move_blocked = true; + return true; + } + // With a selection: cut the selected pixels (⌥ copies them) and drag them as a floating + // piece, from anywhere, as a marquee ⌘-drag does. + if crate::move_ui::moves_selected_pixels_with(app, tool) { + if crate::move_ui::float_selected(app, mods.alt, 0.0, 0.0) { + let mut d = Drag::new(tool, [x, y], vec![[x, y, pressure as f64]], mods, false); + d.sel_move = Some(true); + app.drag = Some(d); + } + return true; + } + } + ToolEvent::Move { x, y, .. } => { + if let Some(d) = app.guide_drag.as_mut().filter(|d| d.index.is_some()) { + d.pos = if d.vertical { x } else { y }; + return true; + } + } + ToolEvent::Up { x, y } => { + if let Some(mut d) = app.guide_drag.filter(|d| d.index.is_some()) { + app.guide_drag = None; + d.pos = if d.vertical { x } else { y }; + crate::rulers::finish_drag(app, d); + return true; + } + } + _ => {} + } + false +} + +/// Tool state machine. Shared by mouse input and automation. +pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) { + let Some((armed, mods)) = preflight_tool_event(app, ev, mods) else { return }; + let Some((raw, ev, mods)) = filter_event_before_tool(app, ev, mods, armed) else { return }; let tool = event_tool(app, mods); if tool == Tool::Eyedropper { match ev { @@ -228,88 +323,10 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) } _ => {} } - // Move tool over a guide drags the guide (off the canvas deletes it). - match ev { - ToolEvent::Down { x, y, pressure } if tool == Tool::Move => { - if let Some((vertical, i)) = crate::rulers::guide_at(app, x, y) { - // ⌥-click turns the guide, through the point clicked, as in Photoshop. - if mods.alt && !mods.shift && !mods.command { - crate::guide_select::flip(app, vertical, i, [x, y]); - return; - } - app.guide_drag = Some(crate::rulers::GuideDrag::existing(vertical, i, [x, y])); - return; - } - // Auto-Select (or ⌘-click while it is off) picks the layer under the pointer first - // (not when the selected pixels move: those are the active layer's). - if !crate::move_ui::moves_selected_pixels_with(app, tool) && app.ui.tool_options.move_auto_select != mods.command { - let target = app.ui.tool_options.move_target.clone(); - let mode = if mods.shift { "add" } else { "replace" }; - let before = app.session.active().map(|st| st.selected_layers()); - // A locked layer under the pointer (the Background) can't move. Photoshop draws - // the layer marquee instead of reporting the lock (#4058). Probe without - // selecting so a locked hit doesn't become the selection first. - let probe = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "select": false})); - let locked_hit = probe.as_ref().ok().and_then(|v| v["layer"].as_u64()).is_some_and(|id| { - app.session.active().is_some_and(|st| { - let id = photocraft_doc::LayerId(id); - let locks = st.doc.effective_locks(id); - locks.position || locks.all || st.doc.is_background(id) - }) - }); - if locked_hit || (probe.as_ref().is_ok_and(|v| v["layer"].is_null()) && crate::move_box::empty_at(app, [x, y])) { - if locked_hit { - // A click still selects the locked layer, as in Photoshop; a drag's - // marquee then replaces that selection on release. - let _ = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "mode": mode})); - } - crate::move_box::begin(app, [x, y], mods.shift); - return; - } - let picked = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "mode": mode})); - // No pixels there at all (an empty spot, or off the canvas): drag a marquee that - // selects the layers it touches instead (move_box.rs). - if picked.is_ok_and(|v| v["layer"].is_null()) && crate::move_box::empty_at(app, [x, y]) { - crate::move_box::begin(app, [x, y], mods.shift); - return; - } - // Snapping (and the drag's box) started before the pick: point it at what moves. - if app.session.active().map(|st| st.selected_layers()) != before { - crate::snap_ui::retarget_move(app, [x, y], mods); - } - } - // A locked layer: no drag, and Photoshop's message once the pointer moves (`move_lock`). - if crate::move_lock::blocked(app, tool, [x, y], mods) { - app.move_blocked = true; - return; - } - // With a selection: cut the selected pixels (⌥ copies them) and drag them as a floating - // piece, from anywhere, as a marquee ⌘-drag does. - if crate::move_ui::moves_selected_pixels_with(app, tool) { - if crate::move_ui::float_selected(app, mods.alt, 0.0, 0.0) { - let mut d = Drag::new(tool, [x, y], vec![[x, y, pressure as f64]], mods, false); - d.sel_move = Some(true); - app.drag = Some(d); - } - return; - } - } - ToolEvent::Move { x, y, .. } => { - if let Some(d) = app.guide_drag.as_mut().filter(|d| d.index.is_some()) { - d.pos = if d.vertical { x } else { y }; - return; - } - } - ToolEvent::Up { x, y } => { - if let Some(mut d) = app.guide_drag.filter(|d| d.index.is_some()) { - app.guide_drag = None; - d.pos = if d.vertical { x } else { y }; - crate::rulers::finish_drag(app, d); - return; - } - } - _ => {} + if handle_move_pointer(app, ev, tool, mods) { + return; } + match ev { ToolEvent::Down { x, y, pressure } => { // Painting a type, shape, Smart Object or fill layer asks to rasterize it first diff --git a/crates/ui-egui/src/canvas/transform_controls.rs b/crates/ui-egui/src/canvas/transform_controls.rs index 9d583cd604..fccc2c9751 100644 --- a/crates/ui-egui/src/canvas/transform_controls.rs +++ b/crates/ui-egui/src/canvas/transform_controls.rs @@ -1,6 +1,6 @@ use super::selection::floating_outline; use super::view_xform::ViewXform; -use super::*; +use super::{Color32, LayerContent, PhotocraftApp, Pos2, Rect, Stroke, Tool, Vec2, pos2, vec2}; /// View › Show › Layer Edges: the colour of the active layer's outline. pub(crate) const LAYER_EDGES: Color32 = Color32::from_rgb(0x2d, 0x8c, 0xeb); diff --git a/crates/ui-egui/src/canvas/view_xform.rs b/crates/ui-egui/src/canvas/view_xform.rs index ec3df91d13..3a0b42ca4e 100644 --- a/crates/ui-egui/src/canvas/view_xform.rs +++ b/crates/ui-egui/src/canvas/view_xform.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{DRect, Document, PhotocraftApp, Pos2, Rect, Vec2, View, pos2, vec2}; /// Document ↔ screen mapping for a canvas rect and a view. #[derive(Clone, Copy, Debug)]