LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
#323: hold the render sizes while a pinch or two-finger scroll runs
Nathaniel Ledford6a07c3543138
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 51efb214f..1c84db438 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -205,6 +205,8 @@ pub struct LightcraftApp {pub region_view: Option<region::RegionView>,/// The same for the Before side of a Before/After view.pub region_before_view: Option<region::RegionView>,+ /// The loupe's render sizes while a pinch or two-finger scroll runs.+ pub(crate) size_hold: region::SizeHold,pub(crate) region_tiles: std::collections::HashMap<u64, region::RegionView>,/// Photo Merge dialog previews and background merges.pub merge: merge::MergeState,@@ -275,6 +277,7 @@ impl LightcraftApp {loupe_shown: None,region_view: None,region_before_view: None,+ size_hold: Default::default(),region_tiles: Default::default(),merge: merge::MergeState::default(),import: None,diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rsindex f31fc5304..3d03899c8 100644--- a/crates/ui-egui/src/panels/detail.rs+++ b/crates/ui-egui/src/panels/detail.rs@@ -384,6 +384,12 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {draft_scale: scale,},);+ // a pinch changes the zoom every frame: keep the sizes of before it until it is quiet+ let now = ui.input(|i| i.time);+ let plan = app.size_hold.apply(id, now, navigating, plan);+ if app.size_hold.holding(now) {+ ui.ctx().request_repaint_after(std::time::Duration::from_secs_f64(crate::region::HOLD_SECS));+ }let want = plan.main_edge;let (rw, rh) = if aspect >= 1.0 { (want, (want as f32 / aspect) as usize) } else { ((want as f32 * aspect) as usize, want) };// hover and before renders are stand-ins: the preview size is plenty and keeps them cheapdiff --git a/crates/ui-egui/src/region.rs b/crates/ui-egui/src/region.rsindex ac0c357ef..41bd79e86 100644--- a/crates/ui-egui/src/region.rs+++ b/crates/ui-egui/src/region.rs@@ -102,6 +102,46 @@ pub fn plan(settings: &crate::state::AppSettings, v: ViewSizes) -> LoupePlan {LoupePlan { main_edge, window_edge }}+/// How long the render sizes stay as they were after a pinch or two-finger scroll stops (s).+pub const HOLD_SECS: f64 = 0.25;++/// Holds the loupe's render sizes while a gesture runs (a continuous pinch changes the zoom every+/// frame; re-planning each time would be a stream of renders of every size in between). The view+/// keeps being drawn from the renders it has, magnified by the GPU, and is re-planned once the+/// gesture has been quiet for [`HOLD_SECS`].+#[derive(Clone, Debug, Default)]+pub struct SizeHold {+ photo: Option<lightcraft_catalog::PhotoId>,+ plan: Option<LoupePlan>,+ until: f64,+}++impl SizeHold {+ /// The plan to render this frame: `fresh` normally; the plan from before the gesture while+ /// one is running (`gesturing`) and for [`HOLD_SECS`] after. `now` is in seconds.+ pub fn apply(&mut self, photo: lightcraft_catalog::PhotoId, now: f64, gesturing: bool, fresh: LoupePlan) -> LoupePlan {+ if self.photo != Some(photo) {+ *self = SizeHold { photo: Some(photo), plan: Some(fresh), until: f64::NEG_INFINITY };+ return fresh;+ }+ if gesturing {+ self.until = now + HOLD_SECS;+ }+ match self.plan {+ Some(held) if gesturing || now < self.until => held,+ _ => {+ self.plan = Some(fresh);+ fresh+ }+ }+ }++ /// Whether sizes are being held at `now` (the caller asks for a repaint when it ends).+ pub fn holding(&self, now: f64) -> bool {+ now < self.until+ }+}+/// Whether the picture on screen has the frame's aspect (to the one pixel a texture's whole-pixel/// size is off by): a window is a part of the frame, so it can only be placed over a picture that/// is the frame (an unsupported raw's embedded JPEG may be cropped differently).@@ -241,6 +281,38 @@ mod tests {assert_eq!(p.window_edge, None);}+ // Given a pinch from fit to 400 %, the sizes of the first frame hold until the gesture is quiet+ #[test]+ fn a_pinch_holds_the_render_sizes() {+ use lightcraft_catalog::PhotoId;+ let (fit, zoomed) = (plan(&auto(), sizes(1400.0, 1400.0, 6000)), plan(&auto(), sizes(24000.0, 1400.0, 6000)));+ assert_ne!(fit, zoomed);+ let mut hold = SizeHold::default();+ let p = PhotoId(1);+ assert_eq!(hold.apply(p, 0.0, false, fit), fit);+ // the pinch runs for a second: every frame would plan something else+ for k in 1..60 {+ assert_eq!(hold.apply(p, k as f64 / 60.0, true, zoomed), fit, "frame {k}");+ }+ // it stopped at t = 59/60: still held for a moment (no flicker between two events)…+ assert!(hold.holding(1.1));+ assert_eq!(hold.apply(p, 1.1, false, zoomed), fit);+ // …then the view is planned for where it ended+ assert!(!hold.holding(59.0 / 60.0 + HOLD_SECS + 0.01));+ assert_eq!(hold.apply(p, 1.3, false, zoomed), zoomed);+ assert_eq!(hold.apply(p, 1.4, false, zoomed), zoomed);+ }++ // Given another photo, the held sizes are forgotten+ #[test]+ fn another_photo_is_planned_afresh() {+ use lightcraft_catalog::PhotoId;+ let (fit, zoomed) = (plan(&auto(), sizes(1400.0, 1400.0, 6000)), plan(&auto(), sizes(24000.0, 1400.0, 6000)));+ let mut hold = SizeHold::default();+ hold.apply(PhotoId(1), 0.0, false, fit);+ assert_eq!(hold.apply(PhotoId(2), 0.1, true, zoomed), zoomed);+ }+// Hostile numbers give a plan, never a panic#[test]fn hostile_sizes_give_a_usable_plan() {diff --git a/crates/ui-egui/src/tests_preview_limit.rs b/crates/ui-egui/src/tests_preview_limit.rsindex af1c36e91..f27eae4c3 100644--- a/crates/ui-egui/src/tests_preview_limit.rs+++ b/crates/ui-egui/src/tests_preview_limit.rs@@ -425,4 +425,28 @@ mod in_the_loupe {assert_eq!(h.app.region_before_view, None);assert!(tile(&h, Slot::Region).is_some());}++ // Given a trackpad pinch from fit to well past 100 %, the render sizes are held while it runs:+ // no stream of renders of every size in between, and the sharp window arrives once it stops+ #[test]+ fn a_pinch_holds_the_render_sizes_until_it_stops() {+ let (mut h, native) = detail();+ let main_at_fit = rendered_long_edge(&h);+ let built = h.app.renderer.completed;+ let at = h.app.canvas_rect.unwrap().center();+ h.request("ui.move", json!({"x": at.x, "y": at.y}), T);+ for _ in 0..40 {+ h.request("ui.zoom", json!({"factor": 1.08}), T);+ assert_eq!(h.app.region_view, None, "no window while the pinch runs");+ }+ assert!(matches!(h.app.ui.zoom, crate::state::Zoom::Percent(p) if p > 100.0), "{:?}", h.app.ui.zoom);+ assert_eq!(rendered_long_edge(&h), main_at_fit, "the whole-frame render keeps its size");+ assert!(h.app.renderer.completed <= built + 2, "{} renders finished during the pinch", h.app.renderer.completed - built);+ for _ in 0..60 {+ h.step();+ }+ h.settle(SETTLE);+ let region = h.app.region_view.expect("the window arrives when the pinch is over");+ assert_eq!(region.full.0.max(region.full.1), native);+ }}