-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
327 lines (298 loc) · 6.2 KB
/
event.go
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package edit
import (
"errors"
"log"
"strings"
"github.com/gdamore/tcell/v2"
)
type EventType int
const (
NoEvent EventType = iota
Rune
Key
Mouse
Resize
Paste
)
func (t EventType) Name() string {
switch t {
case Rune:
return "Rune"
case Key:
return "Key"
case Mouse:
return "Mouse"
case Resize:
return "Resize"
case Paste:
return "Paste"
default:
return ""
}
}
type Modifiers int
const (
Shift Modifiers = 1 << iota
Control
Alt
Meta
)
type MouseButtons int
const (
Button1 MouseButtons = 1 << iota
Button2
Button3
WheelDown
WheelUp
WheelLeft
WheelRight
)
func (mb MouseButtons) Has(mb1 MouseButtons) bool {
return mb&mb1 == mb1
}
func (mb MouseButtons) Empty() bool {
return mb == 0
}
func (mb MouseButtons) Name() string {
switch {
case mb.Has(Button1):
return "Button1"
case mb.Has(Button2):
return "Button2"
case mb.Has(Button3):
return "Button3"
case mb.Has(WheelDown):
return "WheelUp"
case mb.Has(WheelUp):
return "WheelDown"
case mb.Has(WheelLeft):
return "WheelLeft"
case mb.Has(WheelRight):
return "WheelRight"
default:
return ""
}
}
type MouseData struct {
Position
Buttons MouseButtons
ButtonsPressed MouseButtons
ButtonsReleased MouseButtons
}
type KeyData tcell.Key
type Event struct {
EventType
KeyData
Rune rune
Modifiers
MouseData
Size
PasteString string
}
func (e Event) nameWithoutMod() string {
switch e.EventType {
case Rune:
if e.Rune == ' ' {
return "Space"
}
return string(e.Rune)
case Key:
return tcell.KeyNames[tcell.Key(e.KeyData)]
case Mouse:
if !e.ButtonsPressed.Empty() {
return "MousePress-" + e.ButtonsPressed.Name()
}
if !e.ButtonsReleased.Empty() {
return "MouseRelease-" + e.ButtonsReleased.Name()
}
if !e.Buttons.Empty() {
return "MouseDrag-" + e.Buttons.Name()
}
return "MouseMove"
case Resize:
return "Resize"
case Paste:
return "Paste"
default:
return ""
}
}
func (e Event) Name() string {
name := e.nameWithoutMod()
if e.Modifiers&Control != 0 {
name = addPrefix(name, "Ctrl-")
}
if e.Modifiers&Meta != 0 {
name = addPrefix(name, "Meta+")
}
if e.Modifiers&Alt != 0 {
name = addPrefix(name, "Alt+")
}
if e.Modifiers&Shift != 0 {
name = addPrefix(name, "Shift+")
}
return name
}
func addPrefix(s, p string) string {
if strings.HasPrefix(s, p) {
return s
}
return p + s
}
type Action func(win *Window)
type ActionMaker func([]interface{}) Action
type stateDef struct {
action ActionMaker
eventFields []string
transitions map[string]string
}
type EventHandler struct {
states map[string]stateDef
currentState string
events []Event
}
func NewEventHandler() *EventHandler {
return &EventHandler{
states: map[string]stateDef{},
}
}
func convertEvents(events []Event, fields []string) []interface{} {
var out []interface{}
for i, evt := range events {
switch fields[i] {
case "Rune":
out = append(out, evt.Rune)
case "Position":
out = append(out, evt.Position)
case "Size":
out = append(out, evt.Size)
case "PasteString":
out = append(out, evt.PasteString)
}
}
return out
}
// HandleEvent transitions the state of the event handler and returns any action
// that may be triggered.
func (h *EventHandler) HandleEvent(evt Event) (Action, error) {
if h == nil {
return nil, nil
}
sDef := h.states[h.currentState]
evtName := evt.Name()
trans := evtName
newState, ok := sDef.transitions[trans]
if !ok {
trans = evt.EventType.Name()
newState, ok = sDef.transitions[trans]
}
if !ok {
if evt.EventType == Mouse && evtName == "MouseMove" {
// Discard a mouse event that has no button presses or releases
return nil, nil
}
h.Reset()
return nil, errors.New("no known transition for event")
}
sDef = h.states[newState]
h.currentState = newState
h.events = append(h.events, evt)
var action Action
if sDef.action != nil {
action = sDef.action(convertEvents(h.events, sDef.eventFields))
h.Reset()
}
return action, nil
}
// RegisterAction associates the given action with the event sequence
// represented by seq. E.g. RegisterAction("x y z", action) will create the
// following transitions:
//
// "" -x-> x
// x -y-> x:y
// x:y -z-> x:y:z
//
// It will also record that the state x:y:z triggers action x:y:z triggers
// action
func (h *EventHandler) RegisterAction(seq string, action ActionMaker) error {
if h == nil {
return errors.New("cannot register an action on a nil handler")
}
events := strings.Split(seq, " ")
s := ""
var eventFields []string
var eventNames []string
for _, event := range events {
parts := strings.SplitN(event, ".", 2)
eventName := parts[0]
eventField := ""
if len(parts) == 2 {
eventField = parts[1]
}
sDef := h.states[s]
if sDef.action != nil {
return errors.New("action already exists")
}
s = childState(s, eventName)
eventNames = append(eventNames, eventName)
eventFields = append(eventFields, eventField)
}
sDef, ok := h.states[s]
if ok && len(sDef.transitions) > 0 {
return errors.New("seq is prefix to existing seq")
}
// All is well
s = ""
log.Printf("Action for %s: %v", seq, eventNames)
for _, eventName := range eventNames {
sDef := h.states[s]
if sDef.transitions == nil {
sDef.transitions = map[string]string{}
h.states[s] = sDef
}
s = childState(s, eventName)
sDef.transitions[eventName] = s
}
sDef = h.states[s]
sDef.action = action
sDef.eventFields = eventFields
h.states[s] = sDef
return nil
}
func (h *EventHandler) UnregisterAction(seq string) error {
if h == nil {
return nil
}
events := strings.Split(seq, " ")
var eventNames []string
for _, event := range events {
parts := strings.SplitN(event, ".", 2)
eventName := parts[0]
eventNames = append(eventNames, eventName)
}
s := ""
log.Printf("Action for %s: %v", seq, eventNames)
for _, eventName := range eventNames {
sDef := h.states[s]
if sDef.transitions != nil {
delete(sDef.transitions, eventName)
if len(sDef.transitions) == 0 {
delete(h.states, s)
}
}
s = childState(s, eventName)
}
delete(h.states, s)
return nil
}
// Reset the handler so any ongoing sequence is aborted.
func (h *EventHandler) Reset() {
if h == nil {
return
}
h.currentState = ""
h.events = nil
}
func childState(s, t string) string {
return s + " " + t
}