ui/gesture: add Scroll.State method

And move Click.State to a method.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-08-11 18:15:59 +02:00
parent 5d28f9e690
commit 40091c5918
3 changed files with 57 additions and 21 deletions
+55 -19
View File
@@ -23,8 +23,8 @@ import (
// Click detects click gestures in the form // Click detects click gestures in the form
// of ClickEvents. // of ClickEvents.
type Click struct { type Click struct {
// State tracks the current state // state tracks the gesture state.
State ClickState state ClickState
} }
type ClickState uint8 type ClickState uint8
@@ -55,6 +55,8 @@ type Scroll struct {
scroll float32 scroll float32
} }
type ScrollState uint8
type flinger struct { type flinger struct {
// Current offset in pixels. // Current offset in pixels.
x float32 x float32
@@ -72,8 +74,12 @@ const (
) )
const ( const (
// StateNormal is the default click state.
StateNormal ClickState = iota StateNormal ClickState = iota
// StateFocused is reported when a pointer
// is hovering over the handler.
StateFocused StateFocused
// StatePressed is then a pointer is pressed.
StatePressed StatePressed
) )
@@ -86,6 +92,16 @@ const (
TypeClick TypeClick
) )
const (
// StateIdle is the default scroll state.
StateIdle ScrollState = iota
// StateDrag is reported during drag gestures.
StateDragging
// StateFlinging is reported when a fling is
// in progress.
StateFlinging
)
var ( var (
touchSlop = ui.Dp(3) touchSlop = ui.Dp(3)
// Pixels/second. // Pixels/second.
@@ -103,6 +119,11 @@ func (c *Click) Add(ops *ui.Ops) {
op.Add(ops) op.Add(ops)
} }
// State reports the click state.
func (c *Click) State() ClickState {
return c.state
}
// Events reports all click events for the available events. // Events reports all click events for the available events.
func (c *Click) Events(q input.Queue) []ClickEvent { func (c *Click) Events(q input.Queue) []ClickEvent {
var events []ClickEvent var events []ClickEvent
@@ -113,24 +134,24 @@ func (c *Click) Events(q input.Queue) []ClickEvent {
} }
switch e.Type { switch e.Type {
case pointer.Release: case pointer.Release:
wasPressed := c.State == StatePressed wasPressed := c.state == StatePressed
c.State = StateNormal c.state = StateNormal
if wasPressed { if wasPressed {
events = append(events, ClickEvent{Type: TypeClick, Position: e.Position, Source: e.Source}) events = append(events, ClickEvent{Type: TypeClick, Position: e.Position, Source: e.Source})
} }
case pointer.Cancel: case pointer.Cancel:
c.State = StateNormal c.state = StateNormal
case pointer.Press: case pointer.Press:
if c.State == StatePressed || !e.Hit { if c.state == StatePressed || !e.Hit {
break break
} }
c.State = StatePressed c.state = StatePressed
events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source}) events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source})
case pointer.Move: case pointer.Move:
if c.State == StatePressed && !e.Hit { if c.state == StatePressed && !e.Hit {
c.State = StateNormal c.state = StateNormal
} else if c.State < StateFocused { } else if c.state < StateFocused {
c.State = StateFocused c.state = StateFocused
} }
} }
} }
@@ -151,11 +172,6 @@ func (s *Scroll) Stop() {
s.flinger = flinger{} s.flinger = flinger{}
} }
// Dragging reports whether the user is dragging the Scroll.
func (s *Scroll) Dragging() bool {
return s.dragging
}
// Scroll detects the scrolling distance from the available events and // Scroll detects the scrolling distance from the available events and
// ongoing fling gestures. // ongoing fling gestures.
func (s *Scroll) Scroll(cfg ui.Config, q input.Queue, axis Axis) int { func (s *Scroll) Scroll(cfg ui.Config, q input.Queue, axis Axis) int {
@@ -243,9 +259,16 @@ func (s *Scroll) val(p f32.Point) float32 {
} }
} }
// Active reports whether a fling gesture is in progress. // State reports the scroll state.
func (s *Scroll) Active() bool { func (s *Scroll) State() ScrollState {
return s.flinger.Active() switch {
case s.flinger.Active():
return StateFlinging
case s.dragging:
return StateDragging
default:
return StateIdle
}
} }
func (f *flinger) Init(now time.Time, v0 float32) { func (f *flinger) Init(now time.Time, v0 float32) {
@@ -332,3 +355,16 @@ func (cs ClickState) String() string {
panic("invalid ClickState") panic("invalid ClickState")
} }
} }
func (s ScrollState) String() string {
switch s {
case StateIdle:
return "StateIdle"
case StateDragging:
return "StateDragging"
case StateFlinging:
return "StateFlinging"
default:
panic("unreachable")
}
}
+1 -1
View File
@@ -87,7 +87,7 @@ func (l *List) Init(cfg ui.Config, q input.Queue, ops *ui.Ops, cs Constraints, l
// Dragging reports whether the List is being dragged. // Dragging reports whether the List is being dragged.
func (l *List) Dragging() bool { func (l *List) Dragging() bool {
return l.scroll.Dragging() return l.scroll.State() == gesture.StateDragging
} }
func (l *List) update() { func (l *List) update() {
+1 -1
View File
@@ -105,7 +105,7 @@ func (e *Editor) Next(cfg ui.Config, queue input.Queue) (EditorEvent, bool) {
Y: int(math.Round(float64(evt.Position.Y))), Y: int(math.Round(float64(evt.Position.Y))),
}) })
e.requestFocus = true e.requestFocus = true
if !e.scroller.Active() { if e.scroller.State() != gesture.StateFlinging {
e.scrollToCaret(cfg) e.scrollToCaret(cfg)
} }
} }