mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
b981ccf9ed
Change input.Events interface to return one event at a time until the queue is empty. Change text.Editor and gestures to match. Re-add Editor.Submit while we're here; we don't want to enable submit mode always. Signed-off-by: Elias Naur <mail@eliasnaur.com>
60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package input
|
|
|
|
import (
|
|
"gioui.org/ui"
|
|
"gioui.org/ui/key"
|
|
"gioui.org/ui/pointer"
|
|
)
|
|
|
|
// Queue is an Events implementation that merges events from
|
|
// all available input sources.
|
|
type Queue struct {
|
|
pqueue pointerQueue
|
|
kqueue keyQueue
|
|
|
|
handlers handlerEvents
|
|
}
|
|
|
|
type handlerEvents map[Key][]Event
|
|
|
|
func (q *Queue) Next(k Key) (Event, bool) {
|
|
events := q.handlers[k]
|
|
if len(events) == 0 {
|
|
return nil, false
|
|
}
|
|
e := events[0]
|
|
q.handlers[k] = events[1:]
|
|
return e, true
|
|
}
|
|
|
|
func (q *Queue) Frame(ops *ui.Ops) {
|
|
q.init()
|
|
for k := range q.handlers {
|
|
delete(q.handlers, k)
|
|
}
|
|
q.pqueue.Frame(ops, q.handlers)
|
|
q.kqueue.Frame(ops, q.handlers)
|
|
}
|
|
|
|
func (q *Queue) Add(e Event) {
|
|
q.init()
|
|
switch e := e.(type) {
|
|
case pointer.Event:
|
|
q.pqueue.Push(e, q.handlers)
|
|
case key.Edit, key.Chord, key.Focus:
|
|
q.kqueue.Push(e, q.handlers)
|
|
}
|
|
}
|
|
|
|
func (q *Queue) InputState() key.TextInputState {
|
|
return q.kqueue.InputState()
|
|
}
|
|
|
|
func (q *Queue) init() {
|
|
if q.handlers == nil {
|
|
q.handlers = make(handlerEvents)
|
|
}
|
|
}
|