From 90816ab768a94bb973c16c4e1095859d1e81d4eb Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 13 Nov 2026 15:43:58 +0100 Subject: [PATCH] Brush smoothing: a timed stroke stays timed on a zero-time report (#12246) Once time had passed between two input points, a later report with the same timestamp still took the untimed per-point pull (90 % of the way at 10 % smoothing), so the brush jumped. A timed stroke now remembers it is timed, and a report with no time since the last one leaves the brush where it is. Untimed input keeps its per-point pull. CodeRabbit finding on #12246. Co-Authored-By: Hector Villarreal Co-Authored-By: Claude Opus 5.5 --- crates/paint/src/dynamics.rs | 13 +++++++++++-- crates/paint/src/smoothing_lag_tests.rs | 13 +++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rs index 9e45ca175d..343e2cbda1 100644 --- a/crates/paint/src/dynamics.rs +++ b/crates/paint/src/dynamics.rs @@ -117,12 +117,15 @@ pub struct Smoother { zoom: f64, pos: Option, last_input: Option, + /// Has time passed between two input points? Then the stroke is timed, and a later point + /// with no time since the last one moves the brush no further (not by the untimed pull). + timed: bool, } impl Smoother { /// `zoom` is the view zoom (only used with `adjust_for_zoom`). pub fn new(cfg: &Smoothing, zoom: f32) -> Self { - Self { cfg: cfg.clone(), zoom: f64::from(zoom.max(0.01)), pos: None, last_input: None } + Self { cfg: cfg.clone(), zoom: f64::from(zoom.max(0.01)), pos: None, last_input: None, timed: false } } fn active(&self) -> bool { @@ -171,7 +174,13 @@ impl Smoother { // Without Stroke Catch-Up the brush stops while the pointer pauses (Photoshop). return; } - let (iters, step) = if dt > 0.0 && dt.is_finite() { + let has_time = dt > 0.0 && dt.is_finite(); + self.timed |= has_time; + if self.timed && !has_time { + // No time passed (two reports in one millisecond): the brush stays where it is. + return; + } + let (iters, step) = if has_time { let steps = dt / SMOOTHING_STEP_MS; let iters = steps.ceil().clamp(1.0, 64.0); // Each of `iters` moves covers `steps / iters` steps' worth of pull. diff --git a/crates/paint/src/smoothing_lag_tests.rs b/crates/paint/src/smoothing_lag_tests.rs index f6b8435be5..30d278e80e 100644 --- a/crates/paint/src/smoothing_lag_tests.rs +++ b/crates/paint/src/smoothing_lag_tests.rs @@ -72,3 +72,16 @@ fn strokes_without_timestamps_keep_their_per_point_pull() { let x = out.last().map_or(f64::NAN, |p| p.x); assert!((x - 90.0).abs() < 1e-4, "a 10 % untimed step reached {x}, want 90"); } + +/// A timed stroke stays timed: a report with no time since the last one (two events in one +/// millisecond) leaves the brush where it is, instead of jumping it by the untimed per-point pull. +#[test] +fn a_report_with_no_time_passed_does_not_move_a_timed_brush() { + let cfg = Smoothing { amount: 0.1, catch_up: true, catch_up_on_end: false, ..Default::default() }; + let at = |x: f64, time: f64| StrokePoint { time, ..StrokePoint::new(x, 0.0, 1.0) }; + let timed = smooth_points(&cfg, 1.0, &[at(0.0, 0.0), at(100.0, 10.0)]); + let twice = smooth_points(&cfg, 1.0, &[at(0.0, 0.0), at(100.0, 10.0), at(100.0, 10.0)]); + let (before, after) = (timed.last().map_or(f64::NAN, |p| p.x), twice.last().map_or(f64::NAN, |p| p.x)); + assert!(before > 0.0 && before < 90.0, "the timed move lags: {before}"); + assert_eq!(after, before, "no time passed, so the brush stays"); +}