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
+5 -1
View File
@@ -116,7 +116,11 @@ func (b *Clickable) Update(gtx layout.Context) (Click, bool) {
NumClicks: c,
}, true
}
for _, e := range b.click.Update(gtx.Source) {
for {
e, ok := b.click.Update(gtx.Source)
if !ok {
break
}
switch e.Kind {
case gesture.KindClick:
if l := len(b.history); l > 0 {
+5 -1
View File
@@ -53,7 +53,11 @@ func (d *Draggable) Dragging() bool {
// requested to offer data, if any
func (d *Draggable) Update(gtx layout.Context) (mime string, requested bool) {
pos := d.pos
for _, ev := range d.drag.Update(gtx.Metric, gtx.Source, gesture.Both) {
for {
ev, ok := d.drag.Update(gtx.Metric, gtx.Source, gesture.Both)
if !ok {
break
}
switch ev.Kind {
case pointer.Press:
d.click = ev.Position
+10 -2
View File
@@ -316,10 +316,18 @@ func (e *Editor) processPointer(gtx layout.Context) {
func (e *Editor) clickDragEvents(gtx layout.Context) []event.Event {
var combinedEvents []event.Event
for _, evt := range e.clicker.Update(gtx.Source) {
for {
evt, ok := e.clicker.Update(gtx.Source)
if !ok {
break
}
combinedEvents = append(combinedEvents, evt)
}
for _, evt := range e.dragger.Update(gtx.Metric, gtx.Source, gesture.Both) {
for {
evt, ok := e.dragger.Update(gtx.Metric, gtx.Source, gesture.Both)
if !ok {
break
}
combinedEvents = append(combinedEvents, evt)
}
return combinedEvents
+5 -1
View File
@@ -47,7 +47,11 @@ func (e *Enum) Update(gtx layout.Context) bool {
e.hovering = false
changed := false
for _, state := range e.keys {
for _, ev := range state.click.Update(gtx.Source) {
for {
ev, ok := state.click.Update(gtx.Source)
if !ok {
break
}
switch ev.Kind {
case gesture.KindPress:
if ev.Source == pointer.Mouse {
+5 -1
View File
@@ -48,7 +48,11 @@ func (f *Float) Layout(gtx layout.Context, axis layout.Axis, pointerMargin unit.
// The range of f is set by the minimum constraints main axis value.
func (f *Float) Update(gtx layout.Context) bool {
changed := false
for _, e := range f.drag.Update(gtx.Metric, gtx.Source, gesture.Axis(f.axis)) {
for {
e, ok := f.drag.Update(gtx.Metric, gtx.Source, gesture.Axis(f.axis))
if !ok {
break
}
if f.length > 0 && (e.Kind == pointer.Press || e.Kind == pointer.Drag) {
pos := e.Position.X
if f.axis == layout.Vertical {
+15 -3
View File
@@ -61,7 +61,11 @@ func (s *Scrollbar) Update(gtx layout.Context, axis layout.Axis, viewportStart,
}
// Jump to a click in the track.
for _, event := range s.track.Update(gtx.Source) {
for {
event, ok := s.track.Update(gtx.Source)
if !ok {
break
}
if event.Kind != gesture.KindClick ||
event.Modifiers != key.Modifiers(0) ||
event.NumClicks > 1 {
@@ -80,7 +84,11 @@ func (s *Scrollbar) Update(gtx layout.Context, axis layout.Axis, viewportStart,
}
// Offset to account for any drags.
for _, event := range s.drag.Update(gtx.Metric, gtx.Source, gesture.Axis(axis)) {
for {
event, ok := s.drag.Update(gtx.Metric, gtx.Source, gesture.Axis(axis))
if !ok {
break
}
switch event.Kind {
case pointer.Drag:
case pointer.Release, pointer.Cancel:
@@ -136,7 +144,11 @@ func (s *Scrollbar) Update(gtx layout.Context, axis layout.Axis, viewportStart,
// Process events from the indicator so that hover is
// detected properly.
_ = s.indicator.Update(gtx.Source)
for {
if _, ok := s.indicator.Update(gtx.Source); !ok {
break
}
}
}
// AddTrack configures the track click listener for the scrollbar to use
+10 -2
View File
@@ -283,10 +283,18 @@ func (e *Selectable) processPointer(gtx layout.Context) {
func (e *Selectable) clickDragEvents(gtx layout.Context) []event.Event {
var combinedEvents []event.Event
for _, evt := range e.clicker.Update(gtx.Source) {
for {
evt, ok := e.clicker.Update(gtx.Source)
if !ok {
break
}
combinedEvents = append(combinedEvents, evt)
}
for _, evt := range e.dragger.Update(gtx.Metric, gtx.Source, gesture.Both) {
for {
evt, ok := e.dragger.Update(gtx.Metric, gtx.Source, gesture.Both)
if !ok {
break
}
combinedEvents = append(combinedEvents, evt)
}
return combinedEvents