mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
fw sync: opt-in eased wheel zoom on the canvas (NavigationOptions::eased_wheel_zoom)
Upstream bd3641bf3 reads the pass's zoom from its own events, and the canvas applies a Cmd/Ctrl-wheel
notch at once. Production eased a notch over the frames after it (egui's wheel smoothing), and its
find-during-zoom behaviour relies on that. With eased_wheel_zoom (off by default) a zoom-modifier wheel
notch is owed and paid by the pass clock as egui eases a wheel (90% in a tenth of a second); a pinch, a
trackpad and steps under 8 points zoom at once; a press, a key move or a view command drops what is owed.
Trial: canvas::navigate::an_eased_wheel_zoom_spreads_a_notch_over_the_frames_after_it_and_lands_where_the_plain_one_jumps.
This commit is contained in:
@@ -117,7 +117,7 @@ use craft_behavior::geometry::crop::normalized_angle;
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect, Size};
|
||||
use craft_behavior::{
|
||||
CanvasTransform, CanvasView, CursorIcon, CursorPriority, EditEvent, EditorInput, HitLevel, HitPriority, HitShape, Input, Intent, Metrics, Mixed,
|
||||
PartKey, PointerButton, PressPhase, PressState, Sense, Wheel, WheelNotch, WheelUnit,
|
||||
PartKey, PointerButton, PressPhase, PressState, Sense, Wheel, WheelNotch, WheelSource, WheelUnit,
|
||||
};
|
||||
use egui::{Context, Pos2, Rect, Response, Vec2};
|
||||
use navigate::MiddleAutoscroll;
|
||||
@@ -460,6 +460,8 @@ impl<'a> Canvas<'a> {
|
||||
let (press, on_region) = self.pressed(cx);
|
||||
// A press, a zoom, a key move and a view command end the touchpad glide and the eased wheel's owed motion.
|
||||
let mut halt = matches!(press.phase, PressPhase::Down { .. });
|
||||
// A press, a key move and a view command also drop the eased wheel zoom's owed part; its own zoom does not.
|
||||
let mut stop_zoom_ease = halt;
|
||||
let mut jumped = false;
|
||||
let mut line_pan = Vec2::ZERO;
|
||||
// The screen point a wheel or pinch zoom of this pass kept still, before the clamp below can move the centre.
|
||||
@@ -473,6 +475,19 @@ impl<'a> Canvas<'a> {
|
||||
zoom_anchor = read.zoom_anchor.or(zoom_anchor);
|
||||
halt |= read.zoomed;
|
||||
}
|
||||
// The eased wheel zoom pays this pass's part of what its notches owe (`NavigationOptions::eased_wheel_zoom`).
|
||||
if let Some((factor, at)) = motion.wheel_zoom.step(now) {
|
||||
let action = WheelAction::Zoom(factor);
|
||||
let (anchor, zoom) = (WheelNavigation::zoom_anchor(&view, action, at, size), view.zoom);
|
||||
jumped |= WheelNavigation::apply(&mut view, action, at, size, pixels_per_point);
|
||||
if view.zoom != zoom {
|
||||
zoom_anchor = anchor.or(zoom_anchor);
|
||||
}
|
||||
halt = true;
|
||||
}
|
||||
if motion.wheel_zoom.owes() {
|
||||
frame::request_repaint(&ctx);
|
||||
}
|
||||
// Every pass feeds the ease, the line pan or nothing, so owed motion keeps paying; the service takes the time
|
||||
// from its own clock, so a later pass of the same frame pays nothing.
|
||||
if self.navigation.eased_wheel && !halt {
|
||||
@@ -535,6 +550,7 @@ impl<'a> Canvas<'a> {
|
||||
}
|
||||
moves.extend(cx.take_scroll_moves());
|
||||
halt |= !moves.is_empty();
|
||||
stop_zoom_ease |= !moves.is_empty();
|
||||
for (step, mods) in moves {
|
||||
if to_body {
|
||||
keys.page = Some((step, mods));
|
||||
@@ -559,6 +575,7 @@ impl<'a> Canvas<'a> {
|
||||
}
|
||||
view = next;
|
||||
halt = true;
|
||||
stop_zoom_ease = true;
|
||||
match motion {
|
||||
CommandMotion::Ease if self.navigation.animated_zoom => {
|
||||
// A zoom that keeps the centre eases about it; one that moves the centre (Fit on Screen) eases
|
||||
@@ -578,6 +595,9 @@ impl<'a> Canvas<'a> {
|
||||
motion.glide.stop();
|
||||
cx.stop_wheel_ease();
|
||||
}
|
||||
if stop_zoom_ease {
|
||||
motion.wheel_zoom.stop();
|
||||
}
|
||||
// A view that moves during a wheel gesture marks it moved, so a body that turns pages at the edge does not turn
|
||||
// one after a pan or a go-to.
|
||||
if target != at_start {
|
||||
@@ -611,6 +631,13 @@ impl<'a> Canvas<'a> {
|
||||
match action {
|
||||
// The eased wheel hands a line's pan to the wheel service, which pays it out over the passes after.
|
||||
WheelAction::Pan(delta) if self.navigation.eased_wheel && wheel.unit == WheelUnit::Line => read.line_pan = delta,
|
||||
// The eased wheel zoom owes a notch's zoom, which `settle` pays over this pass and the ones after.
|
||||
WheelAction::Zoom(factor) if self.navigation.eased_wheel_zoom && eases_zoom(wheel) => {
|
||||
let at = wheel.at.unwrap_or_else(|| view.rect.center());
|
||||
let frame_dt = f64::from(cx.core.ctx.input(|input| input.stable_dt));
|
||||
motion.wheel_zoom.feed(factor, at, now, frame_dt);
|
||||
read.zoomed = true;
|
||||
}
|
||||
action => {
|
||||
read.zoomed = !matches!(action, WheelAction::Pan(_));
|
||||
// The pointer the wheel came at: the canvas's own widget is not hovered where an app widget in
|
||||
@@ -1090,6 +1117,15 @@ fn clamped(mut view: CanvasView, document: Option<Size>, navigation: &Navigation
|
||||
view
|
||||
}
|
||||
|
||||
/// Whether the eased wheel zoom takes `wheel`'s zoom: a wheel notch under the zoom modifier (Cmd or Ctrl), whose
|
||||
/// deltas the wheel service turns into the share's zoom and never into scroll. A pinch (no modifier), a trackpad's
|
||||
/// steps and a step under 8 points of egui's wheel zoom speed (which egui leaves unsmoothed too) zoom at once.
|
||||
fn eases_zoom(wheel: &Wheel) -> bool {
|
||||
/// The log of the factor of 8 points at egui's default wheel zoom speed (1/200 a point).
|
||||
const SMALLEST: f32 = 8.0 / 200.0;
|
||||
(wheel.mods.cmd || wheel.mods.ctrl) && wheel.source != WheelSource::Trackpad && wheel.zoom.ln().abs() >= SMALLEST
|
||||
}
|
||||
|
||||
/// What a pass's wheel did to the view ([`Canvas::read_wheel`]).
|
||||
#[derive(Default)]
|
||||
struct WheelRead {
|
||||
|
||||
@@ -38,6 +38,9 @@ pub struct NavigationOptions {
|
||||
pub key_line: f32,
|
||||
/// A wheel pan eases to its target through the wheel service instead of jumping. Off.
|
||||
pub eased_wheel: bool,
|
||||
/// A wheel zoom (a zoom-modifier wheel notch, not a pinch or a trackpad's small steps) eases to its factor over the
|
||||
/// passes after the notch, as egui eases a wheel, instead of zooming at once. Off.
|
||||
pub eased_wheel_zoom: bool,
|
||||
/// A touchpad's scroll glides on after the fingers lift. Off.
|
||||
pub touchpad_glide: bool,
|
||||
/// What the middle button does over the canvas background. `Pan`.
|
||||
@@ -83,6 +86,7 @@ impl Default for NavigationOptions {
|
||||
wheel_line: 16.0,
|
||||
key_line: PageKey::SMALL_STEP,
|
||||
eased_wheel: false,
|
||||
eased_wheel_zoom: false,
|
||||
touchpad_glide: false,
|
||||
middle_button: craft_behavior::MiddleButton::default(),
|
||||
invert_middle: false,
|
||||
|
||||
@@ -136,6 +136,71 @@ pub(crate) struct ViewMotion {
|
||||
pub(crate) autoscroll: super::navigate::MiddleAutoscroll,
|
||||
/// The frame of the autoscroll's last pass: an autoscroll whose canvas missed a frame (another tab showed) ends.
|
||||
pub(crate) scrolled_in: Option<u64>,
|
||||
/// The wheel zoom still owed under `NavigationOptions::eased_wheel_zoom`.
|
||||
pub(crate) wheel_zoom: WheelZoomEase,
|
||||
}
|
||||
|
||||
/// A wheel zoom paid out over the passes after its notch (`NavigationOptions::eased_wheel_zoom`), as egui eases the
|
||||
/// wheel: each pass pays the part of what is owed that reaches 90% in a tenth of a second, by the pass clock, so a
|
||||
/// later pass of the same frame pays nothing, and the last sliver is paid whole.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub(crate) struct WheelZoomEase {
|
||||
/// The zoom still owed, as the natural log of its factor.
|
||||
owed: f64,
|
||||
/// The screen point the zoom keeps still: the pointer of the latest notch.
|
||||
anchor: Pos2,
|
||||
/// The pass clock of the last payment.
|
||||
paid_at: f64,
|
||||
}
|
||||
|
||||
impl WheelZoomEase {
|
||||
/// The owed zoom paid whole: one point of egui's default wheel zoom speed.
|
||||
const SLIVER: f64 = 0.005;
|
||||
/// The longest time one payment counts, in seconds, as egui caps its wheel's frame time.
|
||||
const LONGEST_STEP: f64 = 0.1;
|
||||
|
||||
/// Adds a notch's `factor` about `anchor` at the pass clock `now`. An ease that owed nothing starts one frame
|
||||
/// (`frame_dt` seconds) back, so the notch's own pass pays its first part, as egui's wheel does.
|
||||
pub(crate) fn feed(&mut self, factor: f64, anchor: Pos2, now: f64, frame_dt: f64) {
|
||||
let log = factor.ln();
|
||||
if !log.is_finite() || log == 0.0 {
|
||||
return;
|
||||
}
|
||||
if self.owed == 0.0 {
|
||||
let back = if frame_dt.is_finite() { frame_dt.clamp(0.0, Self::LONGEST_STEP) } else { 0.0 };
|
||||
self.paid_at = now - back;
|
||||
}
|
||||
self.owed += log;
|
||||
if !self.owed.is_finite() {
|
||||
self.owed = 0.0;
|
||||
}
|
||||
self.anchor = anchor;
|
||||
}
|
||||
|
||||
/// This pass's payment at the clock `now`: the factor to zoom by and the point it keeps still, if any.
|
||||
pub(crate) fn step(&mut self, now: f64) -> Option<(f64, Pos2)> {
|
||||
if self.owed == 0.0 {
|
||||
return None;
|
||||
}
|
||||
let dt = if (now - self.paid_at).is_finite() { (now - self.paid_at).clamp(0.0, Self::LONGEST_STEP) } else { 0.0 };
|
||||
self.paid_at = now;
|
||||
let pay = if self.owed.abs() < Self::SLIVER { self.owed } else { self.owed * (1.0 - 0.1_f64.powf(dt / Self::LONGEST_STEP)) };
|
||||
if pay == 0.0 || !pay.is_finite() {
|
||||
return None;
|
||||
}
|
||||
self.owed -= pay;
|
||||
Some((pay.exp(), self.anchor))
|
||||
}
|
||||
|
||||
/// Whether zoom is still owed.
|
||||
pub(crate) fn owes(&self) -> bool {
|
||||
self.owed != 0.0
|
||||
}
|
||||
|
||||
/// Drops what is owed.
|
||||
pub(crate) fn stop(&mut self) {
|
||||
self.owed = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
impl ViewMotion {
|
||||
|
||||
@@ -571,6 +571,79 @@ fn an_eased_wheel_pays_the_same_distance_at_any_frame_rate_keeps_paying_after_th
|
||||
assert_eq!(view.get().center, paged, "a key move ends the owed motion");
|
||||
}
|
||||
|
||||
/// A canvas over a large document whose app reruns the pass of a frame while the flag is set, with `eased_wheel_zoom`
|
||||
/// as `eased`: the fixture, the view it shows and the flag.
|
||||
fn zoom_rig(eased: bool) -> EasedRig {
|
||||
let mut options = NavigationOptions::default();
|
||||
options.eased_wheel_zoom = eased;
|
||||
let view = Rc::new(Cell::new(CanvasView::new(egui::Rect::ZERO, Point::new(2000.0, 2000.0), 1.0)));
|
||||
let rerun = Rc::new(Cell::new(false));
|
||||
let (shown, again) = (Rc::clone(&view), Rc::clone(&rerun));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut given = shown.get();
|
||||
given.rect = ui.available_rect_before_wrap();
|
||||
let canvas = Canvas::new("Document").document(Size::new(4000.0, 4000.0)).navigation(options).view(given).show(ui, |_, _| {});
|
||||
shown.set(canvas.output.view.unwrap_or(given));
|
||||
if again.replace(false) && frame::discardable(ui.ctx()) {
|
||||
frame::discard_pass(ui.ctx(), "the trial's second pass");
|
||||
}
|
||||
});
|
||||
fixture.settle();
|
||||
(fixture, view, rerun)
|
||||
}
|
||||
|
||||
/// A zoom-modifier wheel notch of 60 points.
|
||||
fn zoom_notch() -> egui::Event {
|
||||
egui::Event::MouseWheel {
|
||||
unit: egui::MouseWheelUnit::Point,
|
||||
delta: Vec2::new(0.0, 60.0),
|
||||
phase: egui::TouchPhase::Move,
|
||||
modifiers: Modifiers::COMMAND,
|
||||
source: egui::MouseWheelSource::Wheel,
|
||||
}
|
||||
}
|
||||
|
||||
/// The zoom after each of the notch's frame and the `frames` frames after it, each run as two passes when `twice`.
|
||||
fn zooms_after_notch(eased: bool, frames: usize, twice: bool) -> Vec<f64> {
|
||||
let (mut fixture, view, rerun) = zoom_rig(eased);
|
||||
fixture.frame(vec![pointer::moved(view.get().rect.center())]);
|
||||
let mut zooms = Vec::new();
|
||||
eased_frame(&mut fixture, &rerun, twice, vec![zoom_notch()]);
|
||||
zooms.push(view.get().zoom);
|
||||
for _ in 0..frames {
|
||||
eased_frame(&mut fixture, &rerun, twice, vec![]);
|
||||
zooms.push(view.get().zoom);
|
||||
}
|
||||
zooms
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn an_eased_wheel_zoom_spreads_a_notch_over_the_frames_after_it_and_lands_where_the_plain_one_jumps() {
|
||||
let plain = zooms_after_notch(false, 3, false);
|
||||
assert!(plain[0] > 1.0, "the notch zooms in: {plain:?}");
|
||||
assert!(plain.windows(2).all(|pair| pair[0] == pair[1]), "upstream's default zooms at once: {plain:?}");
|
||||
|
||||
let eased = zooms_after_notch(true, 60, false);
|
||||
let (first, last) = (eased[0], eased[eased.len() - 1]);
|
||||
assert!(first > 1.0 && first < plain[0], "the notch's frame pays part of it: {first} of {}", plain[0]);
|
||||
assert!(eased[2] > eased[1], "a frame after the notch still zooms: {eased:?}");
|
||||
assert!((last / plain[0] - 1.0).abs() < 1e-3, "the ease lands on the plain zoom: {last} and {}", plain[0]);
|
||||
|
||||
let rerun = zooms_after_notch(true, 60, true);
|
||||
assert!((rerun[1] - eased[1]).abs() < 1e-9, "a frame's second pass pays nothing more: {} and {}", rerun[1], eased[1]);
|
||||
|
||||
// A key move drops what is still owed.
|
||||
let (mut fixture, view, _) = zoom_rig(true);
|
||||
let canvas = fixture.by_role(Role::CANVAS)[0];
|
||||
fixture.click(&canvas);
|
||||
fixture.frame(vec![pointer::moved(view.get().rect.center())]);
|
||||
fixture.frame(vec![zoom_notch()]);
|
||||
fixture.key(Key::PageDown, Modifiers::NONE);
|
||||
let paged = view.get().zoom;
|
||||
fixture.idle(5);
|
||||
assert_eq!(view.get().zoom, paged, "a key move ends the owed zoom");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_held_alt_key_is_in_the_wheels_modifiers_when_the_wheel_events_miss_it() {
|
||||
// Windows can report a held Alt in its key events and not in the wheel's modifier state.
|
||||
|
||||
Reference in New Issue
Block a user