Crop: a drag keeps the started look once it has changed the frame, and the untouched dashes stay anchored to the corners

This commit is contained in:
Hamza committed 2026-11-04 12:32:34 +05:00
1 parent f593160537
commit a4ff5aa673
4 files changed
+83 -8

No files matched your search

+48 -4
View File
@@ -285,9 +285,10 @@ fn crop_frame_outline(painter: &egui::Painter, q: [Pos2; 4], look: crate::crop_o
let view = painter.clip_rect().expand(4.0);
for i in 0..4 {
let (a, b) = (q[i], q[(i + 1) % 4]);
if let Some([a, b]) = clip_segment(a, b, view) {
painter.line_segment([a, b], Stroke::new(1.0, Color32::from_gray(210)));
painter.extend(egui::Shape::dashed_line(&[a, b], Stroke::new(1.0, Color32::BLACK), 4.0, 4.0));
if let Some([from, to]) = clip_segment(a, b, view) {
painter.line_segment([from, to], Stroke::new(1.0, Color32::from_gray(210)));
let offset = dash_offset(a, from, DASH);
painter.extend(egui::Shape::dashed_line_with_offset(&[from, to], Stroke::new(1.0, Color32::BLACK), &[DASH], &[DASH], offset));
}
}
grey
@@ -299,6 +300,19 @@ fn crop_frame_outline(painter: &egui::Painter, q: [Pos2; 4], look: crate::crop_o
}
}
/// Length of the untouched frame's dashes and of the gaps between them.
const DASH: f32 = 4.0;
/// Where the dashes of an edge cut to the view begin, so they keep the places they have counted
/// from the edge's `corner`: started afresh at the cut, the pattern slid along the edge as the view
/// panned (noted in review). The dashes start every `2 * dash` from the corner; `dashes_from_line`
/// can only begin on a dash, so a dash the cut divides starts a period later -- that piece is in
/// the margin the view is grown by, out of sight.
fn dash_offset(corner: Pos2, start: Pos2, dash: f32) -> f32 {
let period = 2.0 * dash;
(period - (start - corner).length() % period) % period
}
/// The part of the segment `a`–`b` inside `r` (Liang–Barsky), or None when none of it is.
fn clip_segment(a: Pos2, b: Pos2, r: Rect) -> Option<[Pos2; 2]> {
let d = b - a;
@@ -695,7 +709,37 @@ pub(super) fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter
#[cfg(test)]
mod clip_segment_tests {
use super::{Pos2, Rect, clip_segment, pos2};
use super::{DASH, Pos2, Rect, clip_segment, dash_offset, pos2};
/// Where each dash of the edge from `corner`, cut to `view`, begins, as distances from the corner.
fn dash_starts(corner: Pos2, end: Pos2, view: Rect) -> Vec<f32> {
let Some([from, to]) = clip_segment(corner, end, view) else { return Vec::new() };
let shapes =
egui::Shape::dashed_line_with_offset(&[from, to], egui::Stroke::new(1.0, egui::Color32::BLACK), &[DASH], &[DASH], dash_offset(corner, from, DASH));
shapes
.iter()
.filter_map(|s| match s {
egui::Shape::LineSegment { points, .. } => Some((points[0] - corner).length()),
_ => None,
})
.collect()
}
/// The dashes of an edge cut to the view stay where they are counted from the frame's corner,
/// however the view sits on the edge, so panning does not slide them along it (noted in review).
#[test]
fn the_dashes_keep_their_places_as_the_view_pans() {
let (corner, end) = (pos2(0.0, 50.0), pos2(10_000.0, 50.0));
for left in [0.0f32, 1.0, 3.3, 4.0, 5.9, 123.45, 4_001.7] {
let view = Rect::from_min_max(pos2(left, 0.0), pos2(left + 300.0, 100.0));
let starts = dash_starts(corner, end, view);
assert!(!starts.is_empty(), "the view at {left} shows dashes");
for d in starts {
let off = d % (2.0 * DASH);
assert!(off < 0.01 || off > 2.0 * DASH - 0.01, "view at {left}: a dash starts {d} from the corner, off the pattern by {off}");
}
}
}
fn view() -> Rect {
Rect::from_min_max(pos2(0.0, 0.0), pos2(100.0, 50.0))
+6 -2
View File
@@ -250,14 +250,18 @@ pub fn current_guides(app: &PhotocraftApp, w: f32, h: f32) -> Vec<Polyline> {
/// only the frame, with no shield and no overlay in any show mode; both start with the first change
/// and stay until the crop is committed or cancelled (#10930). Classic Mode wasn't measured and
/// keeps the behaviour it had.
///
/// Within a drag, the first change is what counts: a drag that changes the frame and brings it back
/// to where it started keeps the started look until it ends, rather than flickering back to the
/// untouched one (noted in review). Once it ends, `crop_ui::release` decides, as before.
pub fn crop_started(app: &PhotocraftApp) -> bool {
app.ui.tool_options.crop_shield.classic_mode || !app.crop.default_frame || drag_changed(app)
app.ui.tool_options.crop_shield.classic_mode || !app.crop.default_frame || (app.crop.drag.is_some() && app.crop.drag_moved) || drag_changed(app)
}
/// Whether the drag in progress has changed the frame since its press. A press that hasn't moved
/// anything yet is not the first change, so it leaves the untouched look (#10930). The tests mirror
/// the ones `crop_ui` uses when a drag ends.
fn drag_changed(app: &PhotocraftApp) -> bool {
pub(crate) fn drag_changed(app: &PhotocraftApp) -> bool {
use crate::crop_ui::CropDrag;
match &app.crop.drag {
None => false,
+12 -2
View File
@@ -53,6 +53,10 @@ pub struct CropState {
frame_for: Option<(usize, u32, u32)>,
/// The frame is being edited (pressed since it was made): the canvas shows what lies past it.
pub editing: bool,
/// The drag in progress has changed the frame at some point, even if it has since come back to
/// where it started: the crop has started, and its look stays for the rest of the drag
/// (`crop_overlay::crop_started`, #10930).
pub drag_moved: bool,
/// The document the pending frame (or gesture) belongs to ([`cancel_stale`], #1918).
doc: Option<photocraft_doc::DocId>,
/// The options-bar Straighten button and ⌘-drag (`crop_straighten`).
@@ -779,8 +783,14 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool
/// Press, drag and release of the frame. Returns true when the frame needs syncing to the view.
fn track(app: &mut PhotocraftApp, ev: ToolEvent, p: [f64; 2], mods: Modifiers) -> bool {
match ev {
ToolEvent::Down { .. } => begin_drag(app, p),
ToolEvent::Move { .. } => update(app, p, mods),
ToolEvent::Down { .. } => {
app.crop.drag_moved = false;
begin_drag(app, p);
}
ToolEvent::Move { .. } => {
update(app, p, mods);
app.crop.drag_moved |= crate::crop_overlay::drag_changed(app);
}
ToolEvent::Up { .. } => {
update(app, p, mods);
let Some(drag) = app.crop.drag.take() else { return false };
@@ -117,3 +117,20 @@ fn show_cropped_area_off_still_hides_what_is_outside_an_untouched_frame() {
assert!(photocraft_ui_egui::crop_shield::current(&app, &ctx).is_some(), "the opaque outside fill stays");
assert!(current_guides(&app, 300.0, 200.0).is_empty(), "but no overlay before the first change");
}
/// Within a drag the first change counts: a drag that changes the frame and comes back to where it
/// started keeps the started look, rather than flickering back to the untouched one mid-drag
/// (noted in review). Released, the crop is pending as after any drawn frame.
#[test]
fn a_drag_brought_back_to_its_start_keeps_the_started_look_until_it_ends() {
for show in MODES {
let mut app = crop_app(show);
control(&mut app, "ui.pointer", json!({"tool": "Crop", "events": [{"kind": "down", "x": 50, "y": 40}, {"kind": "move", "x": 150, "y": 90}]}));
assert_eq!(drawn(&app).2, FrameLook::Started, "{show:?}: the first change");
control(&mut app, "ui.pointer", json!({"tool": "Crop", "events": [{"kind": "move", "x": 50, "y": 40}]}));
assert_eq!(drawn(&app).2, FrameLook::Started, "{show:?}: back where it started, still the started look");
assert!(drawn(&app).1, "{show:?}: and the shield");
control(&mut app, "ui.pointer", json!({"tool": "Crop", "events": [{"kind": "up", "x": 50, "y": 40}]}));
assert_eq!(drawn(&app).2, FrameLook::Started, "{show:?}: released, the crop is pending");
}
}