mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 23:55:39 +00:00
a35118d522
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>
94 lines
1.6 KiB
Go
94 lines
1.6 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package key
|
|
|
|
import (
|
|
"gioui.org/ui"
|
|
"gioui.org/ui/internal/ops"
|
|
)
|
|
|
|
type OpHandler struct {
|
|
Key Key
|
|
Focus bool
|
|
}
|
|
|
|
type OpHideInput struct{}
|
|
|
|
type Key interface{}
|
|
|
|
type Focus struct {
|
|
Focus bool
|
|
}
|
|
|
|
type Chord struct {
|
|
Name rune
|
|
Modifiers Modifiers
|
|
}
|
|
|
|
type Edit struct {
|
|
Text string
|
|
}
|
|
|
|
type Modifiers uint32
|
|
|
|
type TextInputState uint8
|
|
|
|
const (
|
|
ModCommand Modifiers = 1 << iota
|
|
)
|
|
|
|
const (
|
|
TextInputKeep TextInputState = iota
|
|
TextInputFocus
|
|
TextInputClosed
|
|
TextInputOpen
|
|
)
|
|
|
|
const (
|
|
NameLeftArrow = '←'
|
|
NameRightArrow = '→'
|
|
NameUpArrow = '↑'
|
|
NameDownArrow = '↓'
|
|
NameReturn = '⏎'
|
|
NameEnter = '⌤'
|
|
NameEscape = '⎋'
|
|
NameHome = '⇱'
|
|
NameEnd = '⇲'
|
|
NameDeleteBackward = '⌫'
|
|
NameDeleteForward = '⌦'
|
|
NamePageUp = '⇞'
|
|
NamePageDown = '⇟'
|
|
)
|
|
|
|
func (h OpHandler) Add(o *ui.Ops) {
|
|
data := make([]byte, ops.TypeKeyHandlerLen)
|
|
data[0] = byte(ops.TypeKeyHandler)
|
|
if h.Focus {
|
|
data[1] = 1
|
|
}
|
|
o.Write(data, h.Key)
|
|
}
|
|
|
|
func (h *OpHandler) Decode(d []byte, refs []interface{}) {
|
|
if ops.OpType(d[0]) != ops.TypeKeyHandler {
|
|
panic("invalid op")
|
|
}
|
|
*h = OpHandler{
|
|
Focus: d[1] != 0,
|
|
Key: refs[0].(Key),
|
|
}
|
|
}
|
|
|
|
func (h OpHideInput) Add(o *ui.Ops) {
|
|
data := make([]byte, ops.TypeHideInputLen)
|
|
data[0] = byte(ops.TypeHideInput)
|
|
o.Write(data)
|
|
}
|
|
|
|
func (Edit) ImplementsEvent() {}
|
|
func (Chord) ImplementsEvent() {}
|
|
func (Focus) ImplementsEvent() {}
|
|
func (Edit) ImplementsInputEvent() {}
|
|
func (Chord) ImplementsInputEvent() {}
|
|
func (Focus) ImplementsInputEvent() {}
|