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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
package tui
import (
"fmt"
"github.com/grindlemire/go-tui/internal/debug"
)
type focusQuerier interface {
IsFocused() bool
}
type dispatchEntry struct {
pattern KeyPattern
handler func(KeyEvent)
stop bool
preempt bool
position int
focusCheck func() bool
}
type dispatchTable struct {
entries []dispatchEntry
}
func buildDispatchTable(rootComp Component, root *Element) (*dispatchTable, error) {
table := &dispatchTable{}
position := 0
walkComponents(rootComp, root, func(comp Component) {
kl, ok := comp.(KeyListener)
if !ok {
return
}
km := kl.KeyMap()
if km == nil {
return
}
fq, hasFocusQuery := comp.(focusQuerier)
for _, binding := range km {
entry := dispatchEntry{
pattern: binding.Pattern,
handler: binding.Handler,
stop: binding.Stop,
preempt: binding.Preempt,
position: position,
}
if binding.Pattern.FocusRequired && hasFocusQuery {
entry.focusCheck = fq.IsFocused
}
table.entries = append(table.entries, entry)
}
position++
})
if err := table.validate(); err != nil {
return nil, err
}
return table, nil
}
func (e *dispatchEntry) matchesKey(ke KeyEvent) bool {
p := e.pattern
if p.AnyKey {
return true
}
if p.ExcludeMods != 0 && ke.Mod&p.ExcludeMods != 0 {
return false
}
if p.Mod != 0 && ke.Mod != p.Mod {
return false
}
if p.AnyRune && ke.Key == KeyRune {
return true
}
if p.Rune != 0 && ke.Rune == p.Rune && ke.Key == KeyRune {
return true
}
if p.Key != 0 && ke.Key == p.Key {
return true
}
return false
}
func (e *dispatchEntry) matches(ke KeyEvent) bool {
if e.pattern.FocusRequired && e.focusCheck != nil {
if !e.focusCheck() {
return false
}
}
return e.matchesKey(ke)
}
func (dt *dispatchTable) dispatch(ke KeyEvent) bool {
if dt == nil {
return false
}
debug.Topic("dispatch", "Key=%s Rune=%q Mod=%s (entries=%d)", ke.Key, ke.Rune, ke.Mod, len(dt.entries))
for i := range dt.entries {
e := &dt.entries[i]
if e.pattern.FocusRequired && e.stop && e.focusCheck != nil && e.focusCheck() {
if e.matchesKey(ke) {
e.handler(ke)
return true
}
}
}
for i := range dt.entries {
if !dt.entries[i].preempt {
continue
}
if dt.entries[i].matches(ke) {
dt.entries[i].handler(ke)
if dt.entries[i].stop {
return true
}
}
}
for i := range dt.entries {
if dt.entries[i].preempt {
continue
}
if dt.entries[i].matches(ke) {
debug.Topic("dispatch", "matched entry[%d] pattern={Key=%s Rune=%q Mod=%v ExcludeMods=%v} stop=%v",
i, dt.entries[i].pattern.Key, dt.entries[i].pattern.Rune, dt.entries[i].pattern.Mod, dt.entries[i].pattern.ExcludeMods, dt.entries[i].stop)
dt.entries[i].handler(ke)
if dt.entries[i].stop {
return true
}
}
}
return false
}
func (dt *dispatchTable) validate() error {
type stopInfo struct {
position int
}
stopPatterns := make(map[KeyPattern]stopInfo)
for _, entry := range dt.entries {
if !entry.stop {
continue
}
if entry.pattern.FocusRequired {
continue
}
if entry.preempt {
continue
}
comparePattern := entry.pattern
comparePattern.FocusRequired = false
if existing, conflict := stopPatterns[comparePattern]; conflict {
return fmt.Errorf(
"conflicting stop handlers for key pattern %+v at tree positions %d and %d",
entry.pattern, existing.position, entry.position,
)
}
stopPatterns[comparePattern] = stopInfo{position: entry.position}
}
return nil
}
|