ui/gesture,ui/internal/fling: extract fling logic into its own package

We're going to re-use fling extrapolation and animation for Wayland touchpads.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-09-13 18:04:10 +02:00
parent a68d97f947
commit e6f0e0582d
4 changed files with 127 additions and 98 deletions
+8 -85
View File
@@ -11,12 +11,11 @@ package gesture
import (
"math"
"runtime"
"time"
"gioui.org/ui"
"gioui.org/ui/f32"
"gioui.org/ui/input"
"gioui.org/ui/internal/fling"
"gioui.org/ui/pointer"
)
@@ -46,8 +45,8 @@ type ClickType uint8
type Scroll struct {
dragging bool
axis Axis
estimator estimator
flinger flinger
estimator fling.Extrapolation
flinger fling.Animation
pid pointer.ID
grab bool
last int
@@ -57,15 +56,6 @@ type Scroll struct {
type ScrollState uint8
type flinger struct {
// Current offset in pixels.
x float32
// Initial time.
t0 time.Time
// Initial velocity in pixels pr second.
v0 float32
}
type Axis uint8
const (
@@ -102,16 +92,7 @@ const (
StateFlinging
)
var (
touchSlop = ui.Dp(3)
// Pixels/second.
minFlingVelocity = ui.Dp(50)
maxFlingVelocity = ui.Dp(8000)
)
const (
thresholdVelocity = 1
)
var touchSlop = ui.Dp(3)
// Add the handler to the operation list to receive click events.
func (c *Click) Add(ops *ui.Ops) {
@@ -168,7 +149,7 @@ func (s *Scroll) Add(ops *ui.Ops) {
// Stop any remaining fling movement.
func (s *Scroll) Stop() {
s.flinger = flinger{}
s.flinger = fling.Animation{}
}
// Scroll detects the scrolling distance from the available events and
@@ -190,7 +171,7 @@ func (s *Scroll) Scroll(cfg ui.Config, q input.Queue, axis Axis) int {
break
}
s.Stop()
s.estimator = estimator{}
s.estimator = fling.Extrapolation{}
v := s.val(e.Position)
s.last = int(math.Round(float64(v)))
s.estimator.Sample(e.Time, v)
@@ -201,16 +182,8 @@ func (s *Scroll) Scroll(cfg ui.Config, q input.Queue, axis Axis) int {
break
}
fling := s.estimator.Estimate()
if slop, d := float32(cfg.Px(touchSlop)), fling.Distance; d >= slop || -slop >= d {
if min, v := float32(cfg.Px(minFlingVelocity)), fling.Velocity; v >= min || -min >= v {
max := float32(cfg.Px(maxFlingVelocity))
if v > max {
v = max
} else if v < -max {
v = -max
}
s.flinger.Init(cfg.Now(), v)
}
if slop, d := float32(cfg.Px(touchSlop)), fling.Distance; d < -slop || d > slop {
s.flinger.Start(cfg, fling.Velocity)
}
fallthrough
case pointer.Cancel:
@@ -270,56 +243,6 @@ func (s *Scroll) State() ScrollState {
}
}
func (f *flinger) Init(now time.Time, v0 float32) {
f.t0 = now
f.v0 = v0
f.x = 0
}
func (f *flinger) Active() bool {
return f.v0 != 0
}
// Tick computes and returns a fling distance since
// the last time Tick was called.
func (f *flinger) Tick(now time.Time) int {
if !f.Active() {
return 0
}
var k float32
if runtime.GOOS == "darwin" {
k = -2 // iOS
} else {
k = -4.2 // Android and default
}
t := now.Sub(f.t0)
// The acceleration x''(t) of a point mass with a drag
// force, f, proportional with velocity, x'(t), is
// governed by the equation
//
// x''(t) = kx'(t)
//
// Given the starting position x(0) = 0, the starting
// velocity x'(0) = v0, the position is then
// given by
//
// x(t) = v0*e^(k*t)/k - v0/k
//
ekt := float32(math.Exp(float64(k) * t.Seconds()))
x := f.v0*ekt/k - f.v0/k
dist := x - f.x
idist := int(math.Round(float64(dist)))
f.x += float32(idist)
// Solving for the velocity x'(t) gives us
//
// x'(t) = v0*e^(k*t)
v := f.v0 * ekt
if v < thresholdVelocity && v > -thresholdVelocity {
f.v0 = 0
}
return idist
}
func (a Axis) String() string {
switch a {
case Horizontal: