~eliasnaur/gio

ref: b981ccf9eda43816ae9d809466fe96c8e0c2475b gio/ui/key/key.go -rw-r--r-- 1.7 KiB
b981ccf9Elias Naur ui/input: support single stepping through events 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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
	ModShift
)

const (
	TextInputKeep TextInputState = iota
	TextInputFocus
	TextInputClosed
	TextInputOpen
)

const (
	NameLeftArrow      = '←'
	NameRightArrow     = '→'
	NameUpArrow        = '↑'
	NameDownArrow      = '↓'
	NameReturn         = '⏎'
	NameEnter          = '⌤'
	NameEscape         = '⎋'
	NameHome           = '⇱'
	NameEnd            = '⇲'
	NameDeleteBackward = '⌫'
	NameDeleteForward  = '⌦'
	NamePageUp         = '⇞'
	NamePageDown       = '⇟'
)

func (m Modifiers) Contain(m2 Modifiers) bool {
	return m&m2 == m2
}

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() {}