mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
fw sync: L-321: MiddleAutoscrollFeel::Reader opt-in (production's speed, short-hold latch, ends on canvas blur)
NavigationOptions::middle_autoscroll, Edge (upstream's Autoscroll::step on the offset past the dead zone, a release after a drag ends the run) by default. Reader is the port's MiddleScroll feel PdfCraft's production tests pin: speed |d|^2.2 x 0.04 pt/s from the whole distance, a release ends the run only after two stepped held passes past the dead zone, and the run ends when the canvas loses the focus with the button up. Trial: a_reader_autoscroll_keeps_a_short_hold_and_speeds_up_with_the_whole_distance.
This commit is contained in:
@@ -72,8 +72,8 @@ mod zoom_motion;
|
||||
|
||||
pub use momentary::{Momentary, MomentaryCommands, MomentaryTool};
|
||||
pub use navigate::{
|
||||
Autoscroll, FlickPan, MiddleButton, NavigateBar, NavigateBarOutput, NavigationOptions, PageKey, WheelAction, WheelInput, WheelNavigation,
|
||||
WheelScheme, pan,
|
||||
Autoscroll, FlickPan, MiddleAutoscrollFeel, MiddleButton, NavigateBar, NavigateBarOutput, NavigationOptions, PageKey, WheelAction, WheelInput,
|
||||
WheelNavigation, WheelScheme, pan,
|
||||
};
|
||||
pub use rotate::{RotateView, RotateViewOutput, RotateViewStyle, ViewTurn};
|
||||
pub use tools::{HandTool, LayerBounds, RotateViewTool, ViewCommands, ViewRequest, ViewTarget, ZoomTool};
|
||||
@@ -664,7 +664,11 @@ impl<'a> Canvas<'a> {
|
||||
let left = crate::runtime::pointer::with_pass_pointer(&ctx, |pointer| {
|
||||
pointer.focus_lost || pointer.events.iter().any(|event| matches!(event, craft_behavior::PointerEvent::Gone))
|
||||
});
|
||||
let unfocused = left || !ctx.input(|i| i.focused);
|
||||
let feel = self.navigation.middle_autoscroll;
|
||||
// The reader's run lives while the canvas holds the focus: a modal over it or another control taking
|
||||
// the keys ends it once the button is up.
|
||||
let canvas_left = feel == MiddleAutoscrollFeel::Reader && !run.held() && !cx.is_focused();
|
||||
let unfocused = left || !ctx.input(|i| i.focused) || canvas_left;
|
||||
let off = self.navigation.middle_button != MiddleButton::AutoScroll;
|
||||
if keyed || ended || down || input.press.is_some() || unfocused || input.pos.is_none() || off {
|
||||
// Escape only stops the scroll; any other key also does its own job.
|
||||
@@ -680,7 +684,7 @@ impl<'a> Canvas<'a> {
|
||||
if phase.start().is_some_and(|start| start.button == PointerButton::Middle) {
|
||||
match phase {
|
||||
PressPhase::DragStart { .. } | PressPhase::Drag { .. } => run.drag(),
|
||||
PressPhase::Release { .. } | PressPhase::Cancel { .. } => run.release(),
|
||||
PressPhase::Release { .. } | PressPhase::Cancel { .. } => run.release(feel),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -694,7 +698,7 @@ impl<'a> Canvas<'a> {
|
||||
cx.focus_own();
|
||||
}
|
||||
let Some(pointer) = input.pos.filter(|_| run.mark().is_some()) else { break 'pass false };
|
||||
let step = run.step(pointer, frame::clock(&ctx));
|
||||
let step = run.step(pointer, frame::clock(&ctx), self.navigation.middle_autoscroll);
|
||||
let step = if self.navigation.invert_middle { -step } else { step };
|
||||
crate::runtime::cursor::request(&ctx, run.cursor(Some(pointer)).into(), CursorPriority::Drag);
|
||||
frame::request_repaint(&ctx);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! `MiddleAutoscroll`: the press, dead zone, mark and end of a middle-button autoscroll.
|
||||
|
||||
use super::Autoscroll;
|
||||
use super::{Autoscroll, MiddleAutoscrollFeel};
|
||||
use crate::canvas::frame_step;
|
||||
use crate::primitives::{Draw, PaintShape, StrokeWidth};
|
||||
use crate::style::StyleColor;
|
||||
@@ -43,11 +43,21 @@ pub(crate) struct MiddleAutoscroll {
|
||||
/// A press ended the run: it and its release reach no tool and start nothing.
|
||||
swallow: bool,
|
||||
last: Option<f64>,
|
||||
/// Under [`MiddleAutoscrollFeel::Reader`], the stepped passes the held press rested past the dead zone.
|
||||
held_passes: u32,
|
||||
}
|
||||
|
||||
impl MiddleAutoscroll {
|
||||
/// Pointer distance around the mark, in screen points, that does not scroll.
|
||||
pub(crate) const DEAD_ZONE: f32 = 15.0;
|
||||
/// Under [`MiddleAutoscrollFeel::Reader`], the held passes past the dead zone after which a release ends the run.
|
||||
const READER_HELD_PASSES: u32 = 2;
|
||||
/// Under [`MiddleAutoscrollFeel::Reader`], the power of the pointer's distance the speed grows with.
|
||||
const READER_EXPONENT: f32 = 2.2;
|
||||
/// Under [`MiddleAutoscrollFeel::Reader`], points per second per unit of the distance raised to the exponent.
|
||||
const READER_GAIN: f32 = 0.04;
|
||||
/// The farthest distance the reader's speed is computed from, so the power of a hostile pointer stays finite.
|
||||
const READER_MAX_DISTANCE: f32 = 1_000_000.0;
|
||||
|
||||
/// Starts the autoscroll with the mark at `at`, the point of the press that is now held. A point that is not finite
|
||||
/// starts nothing.
|
||||
@@ -57,6 +67,7 @@ impl MiddleAutoscroll {
|
||||
self.held = true;
|
||||
self.dragged = false;
|
||||
self.last = None;
|
||||
self.held_passes = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,25 +78,56 @@ impl MiddleAutoscroll {
|
||||
|
||||
/// The press that started the run came up (or was cancelled): after a drag that ends the run, after a click the run
|
||||
/// goes on.
|
||||
pub(crate) fn release(&mut self) {
|
||||
let dragged = self.held && self.dragged;
|
||||
///
|
||||
/// Under [`MiddleAutoscrollFeel::Reader`] the drag threshold does not count: the release ends the run after the held
|
||||
/// press rested past the dead zone for two stepped passes, and keeps it after a shorter hold.
|
||||
pub(crate) fn release(&mut self, feel: MiddleAutoscrollFeel) {
|
||||
let ends = match feel {
|
||||
MiddleAutoscrollFeel::Reader => self.held && self.held_passes >= Self::READER_HELD_PASSES,
|
||||
_ => self.held && self.dragged,
|
||||
};
|
||||
self.held = false;
|
||||
self.dragged = false;
|
||||
if dragged {
|
||||
if ends {
|
||||
self.end();
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the press that started the run is still down.
|
||||
pub(crate) fn held(&self) -> bool {
|
||||
self.held
|
||||
}
|
||||
|
||||
/// The view's step this pass for the pointer at `pointer`, `clock` being the pass clock in seconds: screen points
|
||||
/// toward the pointer, zero inside the dead zone, while stopped and in the first pass after the press. Later passes
|
||||
/// of one frame share the clock, so they step zero.
|
||||
pub(crate) fn step(&mut self, pointer: Pos2, clock: f64) -> Vec2 {
|
||||
///
|
||||
/// Under [`MiddleAutoscrollFeel::Reader`] the speed is the pointer's whole vertical distance from the mark raised to
|
||||
/// the power 2.2, times 0.04, in points per second, and a held press resting past the dead zone counts its stepped
|
||||
/// passes for [`release`](Self::release).
|
||||
pub(crate) fn step(&mut self, pointer: Pos2, clock: f64, feel: MiddleAutoscrollFeel) -> Vec2 {
|
||||
let Some(origin) = self.origin else { return Vec2::ZERO };
|
||||
let dt = self.last.map_or(0.0, |last| frame_step(clock - last));
|
||||
if clock.is_finite() {
|
||||
self.last = Some(clock);
|
||||
}
|
||||
Autoscroll::step(Self::past(origin, pointer), dt as f32)
|
||||
let past = Self::past(origin, pointer);
|
||||
match feel {
|
||||
MiddleAutoscrollFeel::Reader => {
|
||||
if past == Vec2::ZERO || dt <= 0.0 {
|
||||
return Vec2::ZERO;
|
||||
}
|
||||
// A frame's later passes take no time: counting them would count one frame's hold twice.
|
||||
if self.held {
|
||||
self.held_passes = self.held_passes.saturating_add(1);
|
||||
}
|
||||
let dy = pointer.y - origin.y;
|
||||
let speed = dy.abs().min(Self::READER_MAX_DISTANCE).powf(Self::READER_EXPONENT) * Self::READER_GAIN;
|
||||
let step = dy.signum() * speed * dt as f32;
|
||||
if step.is_finite() { Vec2::new(0.0, step) } else { Vec2::ZERO }
|
||||
}
|
||||
_ => Autoscroll::step(past, dt as f32),
|
||||
}
|
||||
}
|
||||
|
||||
/// The pointer's vertical offset from `origin` beyond the dead zone; zero inside it.
|
||||
@@ -103,6 +145,7 @@ impl MiddleAutoscroll {
|
||||
self.held = false;
|
||||
self.dragged = false;
|
||||
self.last = None;
|
||||
self.held_passes = 0;
|
||||
}
|
||||
|
||||
/// The press that ended the run is still down: it is the autoscroll's to its release, so a stopping click cannot
|
||||
|
||||
@@ -17,7 +17,7 @@ mod wheel;
|
||||
pub(crate) use autoscroll::{MiddleAutoscroll, paint_mark};
|
||||
pub use bar::{NavigateBar, NavigateBarOutput};
|
||||
pub use keys::PageKey;
|
||||
pub use options::{MiddleButton, NavigationOptions, WheelScheme};
|
||||
pub use options::{MiddleAutoscrollFeel, MiddleButton, NavigationOptions, WheelScheme};
|
||||
pub use wheel::{WheelAction, WheelInput, WheelNavigation};
|
||||
|
||||
use super::frame_step;
|
||||
|
||||
@@ -44,6 +44,23 @@ pub struct NavigationOptions {
|
||||
pub middle_button: craft_behavior::MiddleButton,
|
||||
/// The middle-button scroll runs the other way. Off.
|
||||
pub invert_middle: bool,
|
||||
/// How a middle-button autoscroll feels under [`MiddleButton::AutoScroll`]. [`MiddleAutoscrollFeel::Edge`].
|
||||
pub middle_autoscroll: MiddleAutoscrollFeel,
|
||||
}
|
||||
|
||||
/// How a middle-button autoscroll runs: its speed, what its release does and what ends it.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum MiddleAutoscrollFeel {
|
||||
/// The canvas's one autoscroll rule (`Autoscroll::step`) on the pointer's offset past the dead zone; letting go
|
||||
/// after a drag past the drag threshold ends the run.
|
||||
#[default]
|
||||
Edge,
|
||||
/// A document reader's (PdfCraft): the speed grows as the pointer's whole distance from the mark raised to the
|
||||
/// power 2.2, times 0.04, in points per second; letting go ends the run only after the held pointer rested past
|
||||
/// the dead zone for two stepped passes (a shorter hold is a click, which keeps it); and the run ends when the
|
||||
/// canvas loses the focus (a modal over it, another control taking the keys).
|
||||
Reader,
|
||||
}
|
||||
|
||||
/// What a plain and a modified wheel do over a canvas.
|
||||
@@ -86,6 +103,7 @@ impl Default for NavigationOptions {
|
||||
touchpad_glide: false,
|
||||
middle_button: craft_behavior::MiddleButton::default(),
|
||||
invert_middle: false,
|
||||
middle_autoscroll: MiddleAutoscrollFeel::Edge,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ use craft_behavior::kurbo::{Point, Size};
|
||||
use craft_behavior::{Axis, CancelReason, CanvasView, GESTURE_GAP, MotionMode, ZoomLadder};
|
||||
use craft_keymap::{Mods, Platform};
|
||||
use craft_ui::canvas::{
|
||||
Autoscroll, Canvas, CanvasSpace, CanvasTool, Guide, GuideKey, MiddleButton, Momentary, MomentaryCommands, MomentaryTool, NavigationOptions,
|
||||
ToolAction, ToolContext, ToolHost, ToolPath, ToolPointer, ViewCommands, ViewRequest, WheelNavigation, WheelScheme, tool_pass,
|
||||
Autoscroll, Canvas, CanvasSpace, CanvasTool, Guide, GuideKey, MiddleAutoscrollFeel, MiddleButton, Momentary, MomentaryCommands, MomentaryTool,
|
||||
NavigationOptions, ToolAction, ToolContext, ToolHost, ToolPath, ToolPointer, ViewCommands, ViewRequest, WheelNavigation, WheelScheme, tool_pass,
|
||||
};
|
||||
use craft_ui::canvas_controls::Guides;
|
||||
use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _};
|
||||
@@ -500,6 +500,55 @@ fn a_latched_middle_scroll_ends_on_escape_a_second_press_or_the_wheel_and_a_held
|
||||
/// The seconds of one fixture frame.
|
||||
const FRAME: f64 = 1.0 / 60.0;
|
||||
|
||||
/// A canvas over a large document whose middle button autoscrolls with `feel`: the fixture and the view it shows.
|
||||
fn feel_rig(feel: MiddleAutoscrollFeel) -> (Fixture, Rc<Cell<CanvasView>>) {
|
||||
let mut options = NavigationOptions::default();
|
||||
options.middle_button = MiddleButton::AutoScroll;
|
||||
options.middle_autoscroll = feel;
|
||||
let view = Rc::new(Cell::new(CanvasView::new(egui::Rect::ZERO, Point::new(2000.0, 2000.0), 1.0)));
|
||||
let shown = Rc::clone(&view);
|
||||
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));
|
||||
});
|
||||
fixture.settle();
|
||||
(fixture, view)
|
||||
}
|
||||
|
||||
/// Under `MiddleAutoscrollFeel::Reader` (a document reader's autoscroll) a held press dragged past the drag threshold
|
||||
/// but let go inside the dead zone keeps the run, where the default feel ends it; and the speed grows with the
|
||||
/// pointer's whole distance from the mark, |d|^2.2 x 0.04 points per second.
|
||||
#[trial]
|
||||
fn a_reader_autoscroll_keeps_a_short_hold_and_speeds_up_with_the_whole_distance() {
|
||||
for feel in [MiddleAutoscrollFeel::Edge, MiddleAutoscrollFeel::Reader] {
|
||||
let (mut fixture, view) = feel_rig(feel);
|
||||
let anchor = view.get().rect.center();
|
||||
let near = anchor + Vec2::new(0.0, 12.0);
|
||||
middle(&mut fixture, anchor, true);
|
||||
fixture.frame(vec![pointer::moved(anchor + Vec2::new(0.0, 6.0))]);
|
||||
fixture.frame(vec![pointer::moved(near)]);
|
||||
middle(&mut fixture, near, false);
|
||||
fixture.frame(vec![pointer::moved(anchor + Vec2::new(0.0, 80.0))]);
|
||||
let reader = feel == MiddleAutoscrollFeel::Reader;
|
||||
assert_eq!(scrolls(&mut fixture, &view), reader, "{feel:?}: a release inside the dead zone keeps the run only for the reader");
|
||||
fixture.key(Key::Escape, Modifiers::NONE);
|
||||
}
|
||||
|
||||
let (mut fixture, view) = feel_rig(MiddleAutoscrollFeel::Reader);
|
||||
let anchor = view.get().rect.center();
|
||||
middle(&mut fixture, anchor, true);
|
||||
middle(&mut fixture, anchor, false);
|
||||
fixture.frame(vec![pointer::moved(anchor + Vec2::new(0.0, 50.0))]);
|
||||
fixture.idle(2);
|
||||
let before = view.get().center.y;
|
||||
fixture.idle(60);
|
||||
let travelled = view.get().center.y - before;
|
||||
let expected = 50f64.powf(2.2) * 0.04 * 60.0 * FRAME;
|
||||
assert!((travelled - expected).abs() < 1.0, "a second 50 points below the mark scrolls {expected}, not {travelled}");
|
||||
}
|
||||
|
||||
type EasedRig = (Fixture, Rc<Cell<CanvasView>>, Rc<Cell<bool>>);
|
||||
|
||||
/// A canvas over a large document with the eased wheel, whose app reruns the pass of a frame while the flag is set:
|
||||
|
||||
Reference in New Issue
Block a user