ui: merge package input

Event handling is as fundamental as operations, so move the input
package declarations to package ui.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-09-24 22:11:44 +02:00
parent 2782436ffc
commit 3944ef4b2e
15 changed files with 108 additions and 122 deletions
+3 -3
View File
@@ -57,9 +57,9 @@ For example, to display a blank but otherwise functional window:
Event queue
A Window's Queue method returns an input.Queue implementation that distributes
incoming events to the input handlers declared in the latest call to Update.
See the gioui.org/ui/input package for more information about input handlers.
A Window's Queue method returns an ui.Queue implementation that distributes
incoming events to the event handlers declared in the latest call to Update.
See the gioui.org/ui package for more information about event handlers.
*/
package app
+8 -9
View File
@@ -4,7 +4,6 @@ package input
import (
"gioui.org/ui"
"gioui.org/ui/input"
"gioui.org/ui/internal/opconst"
"gioui.org/ui/internal/ops"
"gioui.org/ui/key"
@@ -13,8 +12,8 @@ import (
type TextInputState uint8
type keyQueue struct {
focus input.Key
handlers map[input.Key]*keyHandler
focus ui.Key
handlers map[ui.Key]*keyHandler
reader ops.Reader
state TextInputState
}
@@ -46,7 +45,7 @@ func (q *keyQueue) InputState() TextInputState {
func (q *keyQueue) Frame(root *ui.Ops, events *handlerEvents) {
if q.handlers == nil {
q.handlers = make(map[input.Key]*keyHandler)
q.handlers = make(map[ui.Key]*keyHandler)
}
for _, h := range q.handlers {
h.active = false
@@ -83,14 +82,14 @@ func (q *keyQueue) Frame(root *ui.Ops, events *handlerEvents) {
}
}
func (q *keyQueue) Push(e input.Event, events *handlerEvents) {
func (q *keyQueue) Push(e ui.Event, events *handlerEvents) {
if q.focus != nil {
events.Add(q.focus, e)
}
}
func (q *keyQueue) resolveFocus(events *handlerEvents) (input.Key, listenerPriority, bool) {
var k input.Key
func (q *keyQueue) resolveFocus(events *handlerEvents) (ui.Key, listenerPriority, bool) {
var k ui.Key
var pri listenerPriority
var hide bool
loop:
@@ -116,7 +115,7 @@ loop:
h = new(keyHandler)
q.handlers[op.Key] = h
// Reset the handler on (each) first appearance.
events.Set(op.Key, []input.Event{key.FocusEvent{Focus: false}})
events.Set(op.Key, []ui.Event{key.FocusEvent{Focus: false}})
}
h.active = true
case opconst.TypeHideInput:
@@ -145,6 +144,6 @@ func decodeKeyInputOp(d []byte, refs []interface{}) key.InputOp {
}
return key.InputOp{
Focus: d[1] != 0,
Key: refs[0].(input.Key),
Key: refs[0].(ui.Key),
}
}
+9 -10
View File
@@ -8,7 +8,6 @@ import (
"gioui.org/ui"
"gioui.org/ui/f32"
"gioui.org/ui/input"
"gioui.org/ui/internal/opconst"
"gioui.org/ui/internal/ops"
"gioui.org/ui/pointer"
@@ -17,10 +16,10 @@ import (
type pointerQueue struct {
hitTree []hitNode
areas []areaNode
handlers map[input.Key]*pointerHandler
handlers map[ui.Key]*pointerHandler
pointers []pointerInfo
reader ops.Reader
scratch []input.Key
scratch []ui.Key
}
type hitNode struct {
@@ -30,13 +29,13 @@ type hitNode struct {
pass bool
// For handler nodes.
key input.Key
key ui.Key
}
type pointerInfo struct {
id pointer.ID
pressed bool
handlers []input.Key
handlers []ui.Key
}
type pointerHandler struct {
@@ -101,7 +100,7 @@ func (q *pointerQueue) collectHandlers(r *ops.Reader, events *handlerEvents, t u
if !ok {
h = new(pointerHandler)
q.handlers[op.Key] = h
events.Set(op.Key, []input.Event{pointer.Event{Type: pointer.Cancel}})
events.Set(op.Key, []ui.Event{pointer.Event{Type: pointer.Cancel}})
}
h.active = true
h.area = area
@@ -111,7 +110,7 @@ func (q *pointerQueue) collectHandlers(r *ops.Reader, events *handlerEvents, t u
}
}
func (q *pointerQueue) opHit(handlers *[]input.Key, pos f32.Point) {
func (q *pointerQueue) opHit(handlers *[]ui.Key, pos f32.Point) {
// Track whether we're passing through hits.
pass := true
idx := len(q.hitTree) - 1
@@ -154,7 +153,7 @@ func (a *areaNode) hit(p f32.Point) bool {
func (q *pointerQueue) init() {
if q.handlers == nil {
q.handlers = make(map[input.Key]*pointerHandler)
q.handlers = make(map[ui.Key]*pointerHandler)
}
}
@@ -176,7 +175,7 @@ func (q *pointerQueue) Frame(root *ui.Ops, events *handlerEvents) {
}
}
func (q *pointerQueue) dropHandler(k input.Key) {
func (q *pointerQueue) dropHandler(k ui.Key) {
for i := range q.pointers {
p := &q.pointers[i]
for i := len(p.handlers) - 1; i >= 0; i-- {
@@ -320,7 +319,7 @@ func decodePointerInputOp(d []byte, refs []interface{}) pointer.InputOp {
}
return pointer.InputOp{
Grab: d[1] != 0,
Key: refs[0].(input.Key),
Key: refs[0].(ui.Key),
}
}
+9 -10
View File
@@ -7,7 +7,6 @@ import (
"time"
"gioui.org/ui"
"gioui.org/ui/input"
"gioui.org/ui/internal/opconst"
"gioui.org/ui/internal/ops"
"gioui.org/ui/key"
@@ -30,15 +29,15 @@ type Router struct {
wakeupTime time.Time
// ProfileOp summary.
profHandlers []input.Key
profHandlers []ui.Key
}
type handlerEvents struct {
handlers map[input.Key][]input.Event
handlers map[ui.Key][]ui.Event
updated bool
}
func (q *Router) Next(k input.Key) (input.Event, bool) {
func (q *Router) Next(k ui.Key) (ui.Event, bool) {
return q.handlers.Next(k)
}
@@ -57,7 +56,7 @@ func (q *Router) Frame(ops *ui.Ops) {
}
}
func (q *Router) Add(e input.Event) bool {
func (q *Router) Add(e ui.Event) bool {
switch e := e.(type) {
case pointer.Event:
q.pqueue.Push(e, &q.handlers)
@@ -103,17 +102,17 @@ func (q *Router) WakeupTime() (time.Time, bool) {
func (h *handlerEvents) init() {
if h.handlers == nil {
h.handlers = make(map[input.Key][]input.Event)
h.handlers = make(map[ui.Key][]ui.Event)
}
}
func (h *handlerEvents) Set(k input.Key, evts []input.Event) {
func (h *handlerEvents) Set(k ui.Key, evts []ui.Event) {
h.init()
h.handlers[k] = evts
h.updated = true
}
func (h *handlerEvents) Add(k input.Key, e input.Event) {
func (h *handlerEvents) Add(k ui.Key, e ui.Event) {
h.init()
h.handlers[k] = append(h.handlers[k], e)
h.updated = true
@@ -125,7 +124,7 @@ func (h *handlerEvents) Updated() bool {
return u
}
func (h *handlerEvents) Next(k input.Key) (input.Event, bool) {
func (h *handlerEvents) Next(k ui.Key) (ui.Event, bool) {
events := h.handlers[k]
if len(events) == 0 {
return nil, false
@@ -146,7 +145,7 @@ func decodeProfileOp(d []byte, refs []interface{}) system.ProfileOp {
panic("invalid op")
}
return system.ProfileOp{
Key: refs[0].(input.Key),
Key: refs[0].(ui.Key),
}
}
+15 -17
View File
@@ -10,8 +10,7 @@ import (
"gioui.org/ui"
"gioui.org/ui/app/internal/gpu"
iinput "gioui.org/ui/app/internal/input"
"gioui.org/ui/input"
"gioui.org/ui/app/internal/input"
"gioui.org/ui/system"
)
@@ -32,8 +31,8 @@ type Window struct {
drawStart time.Time
gpu *gpu.GPU
out chan input.Event
in chan input.Event
out chan ui.Event
in chan ui.Event
ack chan struct{}
invalidates chan struct{}
frames chan *ui.Ops
@@ -47,11 +46,10 @@ type Window struct {
queue Queue
}
// Queue is an input.Queue implementation that distributes
// system input events to the input handlers declared in the
// most recent call to Update.
// Queue is an ui.Queue implementation that distributes system events
// to the input handlers declared in the most recent call to Update.
type Queue struct {
q iinput.Router
q input.Router
}
// driverEvent is sent when a new native driver
@@ -71,7 +69,7 @@ var _ interface {
} = (*window)(nil)
// Pre-allocate the ack event to avoid garbage.
var ackEvent input.Event
var ackEvent ui.Event
// NewWindow creates a new window for a set of window
// options. The options are hints; the platform is free to
@@ -96,8 +94,8 @@ func NewWindow(options ...WindowOption) *Window {
}
w := &Window{
in: make(chan input.Event),
out: make(chan input.Event),
in: make(chan ui.Event),
out: make(chan ui.Event),
ack: make(chan struct{}),
invalidates: make(chan struct{}, 1),
frames: make(chan *ui.Ops),
@@ -107,7 +105,7 @@ func NewWindow(options ...WindowOption) *Window {
}
// Events returns the channel where events are delivered.
func (w *Window) Events() <-chan input.Event {
func (w *Window) Events() <-chan ui.Event {
return w.out
}
@@ -135,9 +133,9 @@ func (w *Window) draw(size image.Point, frame *ui.Ops) {
w.queue.q.Frame(frame)
now := time.Now()
switch w.queue.q.TextInputState() {
case iinput.TextInputOpen:
case input.TextInputOpen:
w.driver.showTextInput(true)
case iinput.TextInputClose:
case input.TextInputClose:
w.driver.showTextInput(false)
}
frameDur := now.Sub(w.lastFrame)
@@ -196,7 +194,7 @@ func (w *Window) setDriver(d *window) {
w.event(driverEvent{d})
}
func (w *Window) event(e input.Event) {
func (w *Window) event(e ui.Event) {
w.in <- e
<-w.ack
}
@@ -313,7 +311,7 @@ func (w *Window) run(opts *windowOptions) {
w.out <- e2
w.ack <- struct{}{}
return
case input.Event:
case ui.Event:
if w.queue.q.Add(e2) {
w.setNextFrame(time.Time{})
w.updateAnimation()
@@ -325,7 +323,7 @@ func (w *Window) run(opts *windowOptions) {
}
}
func (q *Queue) Next(k input.Key) (input.Event, bool) {
func (q *Queue) Next(k ui.Key) (ui.Event, bool) {
return q.q.Next(k)
}