ui: add package input for merged input

To avoid passing a queue type for each kind of input (pointer, key),
introduce package input for mapping a handler key to all input events.

Future input sources can be added without changes to programs, and
as an added bonus, event ordering is preserved across input sources.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-06-07 21:41:09 +02:00
parent 28dd25736f
commit a35118d522
10 changed files with 172 additions and 130 deletions
+11 -2
View File
@@ -9,6 +9,7 @@ import (
"gioui.org/ui"
"gioui.org/ui/f32"
"gioui.org/ui/input"
"gioui.org/ui/pointer"
)
@@ -80,9 +81,13 @@ func (c *Click) Add(ops *ui.Ops) {
op.Add(ops)
}
func (c *Click) Update(q pointer.Events) []ClickEvent {
func (c *Click) Update(q input.Events) []ClickEvent {
var events []ClickEvent
for _, e := range q.For(c) {
e, ok := e.(pointer.Event)
if !ok {
continue
}
switch e.Type {
case pointer.Release:
if c.State == StatePressed {
@@ -124,13 +129,17 @@ func (s *Scroll) Dragging() bool {
return s.dragging
}
func (s *Scroll) Update(cfg *ui.Config, q pointer.Events, axis Axis) int {
func (s *Scroll) Update(cfg *ui.Config, q input.Events, axis Axis) int {
if s.axis != axis {
s.axis = axis
return 0
}
total := 0
for _, e := range q.For(s) {
e, ok := e.(pointer.Event)
if !ok {
continue
}
switch e.Type {
case pointer.Press:
if s.dragging || e.Source != pointer.Touch {