gesture: remove Click.State and cancel Click gesture on outside Release

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-06-09 00:12:05 +02:00
parent 1c6809142f
commit 5e37a64ff1
+32 -39
View File
@@ -36,6 +36,12 @@ type Click struct {
// clicks is incremented if successive clicks // clicks is incremented if successive clicks
// are performed within a fixed duration. // are performed within a fixed duration.
clicks int clicks int
// pressed tracks whether the pointer is pressed.
pressed bool
// entered tracks whether the pointer is inside the gesture.
entered bool
// pid is the pointer.ID.
pid pointer.ID
} }
type ClickState uint8 type ClickState uint8
@@ -79,16 +85,6 @@ const (
Vertical Vertical
) )
const (
// StateNormal is the default click state.
StateNormal ClickState = iota
// StateFocused is reported when a pointer
// is hovering over the handler.
StateFocused
// StatePressed is then a pointer is pressed.
StatePressed
)
const ( const (
// TypePress is reported for the first pointer // TypePress is reported for the first pointer
// press. // press.
@@ -122,11 +118,6 @@ func (c *Click) Add(ops *op.Ops) {
op.Add(ops) op.Add(ops)
} }
// State reports the click state.
func (c *Click) State() ClickState {
return c.state
}
// Events returns the next click event, if any. // Events returns the next click event, if any.
func (c *Click) Events(q event.Queue) []ClickEvent { func (c *Click) Events(q event.Queue) []ClickEvent {
var events []ClickEvent var events []ClickEvent
@@ -137,9 +128,11 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
} }
switch e.Type { switch e.Type {
case pointer.Release: case pointer.Release:
wasPressed := c.state == StatePressed if !c.pressed || c.pid != e.PointerID {
c.state = StateNormal break
if wasPressed { }
c.pressed = false
if c.entered {
if e.Time-c.clickedAt < doubleClickDuration { if e.Time-c.clickedAt < doubleClickDuration {
c.clicks++ c.clicks++
} else { } else {
@@ -151,27 +144,40 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
events = append(events, ClickEvent{Type: TypeCancel}) events = append(events, ClickEvent{Type: TypeCancel})
} }
case pointer.Cancel: case pointer.Cancel:
wasPressed := c.state == StatePressed wasPressed := c.pressed
c.state = StateNormal c.pressed = false
c.entered = false
if wasPressed { if wasPressed {
events = append(events, ClickEvent{Type: TypeCancel}) events = append(events, ClickEvent{Type: TypeCancel})
} }
case pointer.Press: case pointer.Press:
if c.state == StatePressed { if c.pressed {
break break
} }
if e.Source == pointer.Mouse && e.Buttons != pointer.ButtonLeft { if e.Source == pointer.Mouse && e.Buttons != pointer.ButtonLeft {
break break
} }
c.state = StatePressed if !c.entered {
c.pid = e.PointerID
}
if c.pid != e.PointerID {
break
}
c.pressed = true
events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers}) events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers})
case pointer.Leave: case pointer.Leave:
if c.state == StatePressed { if !c.pressed {
c.state = StateNormal c.pid = e.PointerID
}
if c.pid == e.PointerID {
c.entered = false
} }
case pointer.Enter: case pointer.Enter:
if c.state < StateFocused { if !c.pressed {
c.state = StateFocused c.pid = e.PointerID
}
if c.pid == e.PointerID {
c.entered = true
} }
} }
} }
@@ -313,19 +319,6 @@ func (ct ClickType) String() string {
} }
} }
func (cs ClickState) String() string {
switch cs {
case StateNormal:
return "StateNormal"
case StateFocused:
return "StateFocused"
case StatePressed:
return "StatePressed"
default:
panic("invalid ClickState")
}
}
func (s ScrollState) String() string { func (s ScrollState) String() string {
switch s { switch s {
case StateIdle: case StateIdle: