gesture: report one event at a time

Events are now delivered one at a time, and this change makes the
corresponding change to gestures.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-11-25 16:38:48 -06:00
parent ab9f42c820
commit 8e209fd2eb
9 changed files with 82 additions and 36 deletions
+17 -13
View File
@@ -173,9 +173,8 @@ func (c *Click) Pressed() bool {
return c.pressed
}
// Update state and return the click events.
func (c *Click) Update(q input.Source) []ClickEvent {
var events []ClickEvent
// Update state and return the next click events, if any.
func (c *Click) Update(q input.Source) (ClickEvent, bool) {
for {
evt, ok := q.Event(c, pointer.Filter{Kinds: pointer.Press | pointer.Release | pointer.Enter | pointer.Leave})
if !ok {
@@ -192,9 +191,15 @@ func (c *Click) Update(q input.Source) []ClickEvent {
}
c.pressed = false
if !c.entered || c.hovered {
events = append(events, ClickEvent{Kind: KindClick, Position: e.Position.Round(), Source: e.Source, Modifiers: e.Modifiers, NumClicks: c.clicks})
return ClickEvent{
Kind: KindClick,
Position: e.Position.Round(),
Source: e.Source,
Modifiers: e.Modifiers,
NumClicks: c.clicks,
}, true
} else {
events = append(events, ClickEvent{Kind: KindCancel})
return ClickEvent{Kind: KindCancel}, true
}
case pointer.Cancel:
wasPressed := c.pressed
@@ -202,7 +207,7 @@ func (c *Click) Update(q input.Source) []ClickEvent {
c.hovered = false
c.entered = false
if wasPressed {
events = append(events, ClickEvent{Kind: KindCancel})
return ClickEvent{Kind: KindCancel}, true
}
case pointer.Press:
if c.pressed {
@@ -224,7 +229,7 @@ func (c *Click) Update(q input.Source) []ClickEvent {
c.clicks = 1
}
c.clickedAt = e.Time
events = append(events, ClickEvent{Kind: KindPress, Position: e.Position.Round(), Source: e.Source, Modifiers: e.Modifiers, NumClicks: c.clicks})
return ClickEvent{Kind: KindPress, Position: e.Position.Round(), Source: e.Source, Modifiers: e.Modifiers, NumClicks: c.clicks}, true
case pointer.Leave:
if !c.pressed {
c.pid = e.PointerID
@@ -242,7 +247,7 @@ func (c *Click) Update(q input.Source) []ClickEvent {
}
}
}
return events
return ClickEvent{}, false
}
func (ClickEvent) ImplementsEvent() {}
@@ -364,9 +369,8 @@ func (d *Drag) Add(ops *op.Ops) {
event.InputOp(ops, d)
}
// Update state and return the drag events.
func (d *Drag) Update(cfg unit.Metric, q input.Source, axis Axis) []pointer.Event {
var events []pointer.Event
// Update state and return the next drag event, if any.
func (d *Drag) Update(cfg unit.Metric, q input.Source, axis Axis) (pointer.Event, bool) {
for {
ev, ok := q.Event(d, pointer.Filter{Kinds: pointer.Press | pointer.Drag | pointer.Release})
if !ok {
@@ -416,10 +420,10 @@ func (d *Drag) Update(cfg unit.Metric, q input.Source, axis Axis) []pointer.Even
d.dragging = false
}
events = append(events, e)
return e, true
}
return events
return pointer.Event{}, false
}
// Dragging reports whether it is currently in use.
+10 -12
View File
@@ -77,8 +77,16 @@ func TestMouseClicks(t *testing.T) {
r.Frame(&ops)
r.Queue(tc.events...)
events := click.Update(r.Source())
clicks := filterMouseClicks(events)
var clicks []ClickEvent
for {
ev, ok := click.Update(r.Source())
if !ok {
break
}
if ev.Kind == KindClick {
clicks = append(clicks, ev)
}
}
if got, want := len(clicks), len(tc.clicks); got != want {
t.Fatalf("got %d mouse clicks, expected %d", got, want)
}
@@ -108,13 +116,3 @@ func mouseClickEvents(times ...time.Duration) []event.Event {
}
return events
}
func filterMouseClicks(events []ClickEvent) []ClickEvent {
var clicks []ClickEvent
for _, ev := range events {
if ev.Kind == KindClick {
clicks = append(clicks, ev)
}
}
return clicks
}