io/router: expose the event router

For integrating with external window implementations (replacing
package app), access to the event router is required. Extract it
and put it into the new package router.

Router may belong in package io/event, but can't without introducing
import cycles.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-02-07 20:41:04 +01:00
parent 6e98132ebb
commit 34c6a2f735
4 changed files with 27 additions and 9 deletions
+4 -4
View File
@@ -8,10 +8,10 @@ import (
"image"
"time"
"gioui.org/app/internal/input"
"gioui.org/app/internal/window"
"gioui.org/io/event"
"gioui.org/io/profile"
"gioui.org/io/router"
"gioui.org/io/system"
"gioui.org/op"
"gioui.org/unit"
@@ -56,7 +56,7 @@ type callbacks struct {
// Queue is an event.Queue implementation that distributes system events
// to the input handlers declared in the most recent frame.
type Queue struct {
q input.Router
q router.Router
}
// driverEvent is sent when a new native driver
@@ -128,9 +128,9 @@ func (w *Window) draw(frameStart time.Time, size image.Point, frame *op.Ops) {
sync := w.loop.Draw(size, frame)
w.queue.q.Frame(frame)
switch w.queue.q.TextInputState() {
case input.TextInputOpen:
case router.TextInputOpen:
w.driver.ShowTextInput(true)
case input.TextInputClose:
case router.TextInputClose:
w.driver.ShowTextInput(false)
}
if w.queue.q.Profiling() {
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Unlicense OR MIT
package input
package router
import (
"gioui.org/internal/opconst"
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Unlicense OR MIT
package input
package router
import (
"encoding/binary"
@@ -1,6 +1,14 @@
// SPDX-License-Identifier: Unlicense OR MIT
package input
/*
Package router implements Router, a event.Queue implementation
that that disambiguates and routes events to handlers declared
in operation lists.
Router is used by app.Window and is otherwise only useful for
using Gio with external window implementations.
*/
package router
import (
"encoding/binary"
@@ -15,8 +23,8 @@ import (
"gioui.org/op"
)
// Router is a Queue implementation that routes events from
// all available input sources to registered handlers.
// Router is a Queue implementation that routes events
// to handlers declared in operation lists.
type Router struct {
pqueue pointerQueue
kqueue keyQueue
@@ -40,6 +48,7 @@ type handlerEvents struct {
hadEvents bool
}
// Events returns the available events for the handler key.
func (q *Router) Events(k event.Key) []event.Event {
events := q.handlers.Events(k)
if _, isprof := q.profHandlers[k]; isprof {
@@ -49,6 +58,9 @@ func (q *Router) Events(k event.Key) []event.Event {
return events
}
// Frame replaces the declared handlers from the supplied
// operation list. The text input state, wakeup time and whether
// there are active profile handlers is also saved.
func (q *Router) Frame(ops *op.Ops) {
q.handlers.Clear()
q.wakeup = false
@@ -79,6 +91,8 @@ func (q *Router) Add(e event.Event) bool {
return q.handlers.HadEvents()
}
// TextInputState returns the input state from the most recent
// call to Frame.
func (q *Router) TextInputState() TextInputState {
return q.kqueue.InputState()
}
@@ -103,10 +117,14 @@ func (q *Router) collect() {
}
}
// Profiling reports whether there was profile handlers in the
// most recent Frame call.
func (q *Router) Profiling() bool {
return q.profiling
}
// WakeupTime returns the most recent time for doing another frame,
// as determined from the last call to Frame.
func (q *Router) WakeupTime() (time.Time, bool) {
return q.wakeupTime, q.wakeup
}