mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
gesture: support multiple click actions
Double click or tap actions are common in user interfaces and this commit adds support for it in the gesture package. ClickEvent has now a new field NumClicks that contains the number of successive clicks that occurred within 200ms. Fixes gio#101 Signed-off-by: Pierre.Curto <pierre.curto@gmail.com>
This commit is contained in:
+19
-1
@@ -22,11 +22,20 @@ import (
|
||||
"gioui.org/unit"
|
||||
)
|
||||
|
||||
// The duration is somewhat arbitrary.
|
||||
const doubleClickDuration = 200 * time.Millisecond
|
||||
|
||||
// Click detects click gestures in the form
|
||||
// of ClickEvents.
|
||||
type Click struct {
|
||||
// state tracks the gesture state.
|
||||
state ClickState
|
||||
// clickedAt is the timestamp at which
|
||||
// the last click occurred.
|
||||
clickedAt time.Duration
|
||||
// clicks is incremented if successive clicks
|
||||
// are performed within a fixed duration.
|
||||
clicks int
|
||||
}
|
||||
|
||||
type ClickState uint8
|
||||
@@ -39,6 +48,9 @@ type ClickEvent struct {
|
||||
Position f32.Point
|
||||
Source pointer.Source
|
||||
Modifiers key.Modifiers
|
||||
// NumClicks records successive clicks occurring
|
||||
// within a short duration of each other.
|
||||
NumClicks int
|
||||
}
|
||||
|
||||
type ClickType uint8
|
||||
@@ -122,7 +134,13 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
|
||||
wasPressed := c.state == StatePressed
|
||||
c.state = StateNormal
|
||||
if wasPressed {
|
||||
events = append(events, ClickEvent{Type: TypeClick, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers})
|
||||
if e.Time-c.clickedAt < doubleClickDuration {
|
||||
c.clicks++
|
||||
} else {
|
||||
c.clicks = 1
|
||||
}
|
||||
c.clickedAt = e.Time
|
||||
events = append(events, ClickEvent{Type: TypeClick, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers, NumClicks: c.clicks})
|
||||
}
|
||||
case pointer.Cancel:
|
||||
c.state = StateNormal
|
||||
|
||||
Reference in New Issue
Block a user