-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevents.go
286 lines (247 loc) · 6.37 KB
/
events.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
package main
import (
"image"
"math"
"strings"
"time"
"github.com/aarzilli/yacco/util"
"golang.org/x/exp/shiny/screen"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/mouse"
"golang.org/x/mobile/event/size"
)
func FilterEvents(queue screen.EventDeque, altingList []util.AltingEntry, keyConversion map[string]key.Event) chan interface{} {
var em eventMachine
em.dblclickp = image.Point{0, 0}
em.dblclickc = 0
em.dblclickbtn = mouse.ButtonLeft
em.dblclickt = time.Now()
em.alting = false
em.altingSeq = ""
em.events = make([]eventWrapper, 0, 10)
rout := make(chan interface{})
lastResize := time.Unix(0, 0)
lastMouse := time.Unix(0, 0)
lastWheel := time.Unix(0, 0)
var ticker *time.Ticker = nil
in := make(chan interface{})
go func() {
for {
event := queue.NextEvent()
in <- event
switch e := event.(type) {
case lifecycle.Event:
if e.To == lifecycle.StageDead {
return
}
}
}
}()
go func() {
for {
if ticker != nil {
select {
case ei := <-in:
em.processEvent(ei, altingList, keyConversion)
case <-ticker.C:
ticker.Stop()
ticker = nil
}
} else if len(em.events) > 0 {
d := int64(-1)
switch em.events[0].et {
case ET_MOUSE_DRAG, ET_MOUSE_MOVE:
d = int64(30*time.Millisecond - time.Now().Sub(lastMouse))
case ET_RESIZE:
d = int64(30*time.Millisecond - time.Now().Sub(lastResize))
case ET_WHEEL:
d = int64(60*time.Millisecond - time.Now().Sub(lastWheel))
}
if d > 0 {
ticker = time.NewTicker(time.Duration(d))
} else {
select {
case rout <- em.events[0].ei:
switch em.events[0].et {
case ET_MOUSE_DRAG, ET_MOUSE_MOVE:
lastMouse = time.Now()
case ET_RESIZE:
lastResize = time.Now()
case ET_WHEEL:
lastWheel = time.Now()
}
copy(em.events, em.events[1:])
em.events = em.events[:len(em.events)-1]
case ei := <-in:
em.processEvent(ei, altingList, keyConversion)
}
}
} else {
ei := <-in
em.processEvent(ei, altingList, keyConversion)
}
}
}()
return rout
}
type eventType int
const (
ET_OTHER = iota
ET_MOUSE_MOVE
ET_MOUSE_DRAG
ET_RESIZE
ET_WHEEL
)
type eventWrapper struct {
et eventType
ei interface{}
}
type eventMachine struct {
dblclickp image.Point
dblclickc int
dblclickbtn mouse.Button
dblclickt time.Time
alting bool
altingSeq string
events []eventWrapper
}
func (em *eventMachine) processEvent(ei interface{}, altingList []util.AltingEntry, keyConversion map[string]key.Event) {
switch e := ei.(type) {
case key.Event:
switch e.Direction {
case key.DirPress, key.DirNone:
if em.alting && (e.Rune >= 0) {
em.altingSeq += "+" + string(e.Rune)
//println("altingSeq:", altingSeq)
keepAlting := false
for _, altingEntry := range altingList {
if altingEntry.Seq == em.altingSeq {
//println("Emitting:", altingEntry.Glyph)
em.appendEventOther(ET_OTHER, key.Event{Rune: altingEntry.Glyph})
em.alting = false
break
}
if strings.HasPrefix(altingEntry.Seq, em.altingSeq) {
keepAlting = true
}
}
if !keepAlting {
//println("Alting end")
em.alting = false
}
} else if conv, ok := keyConversion[util.KeyEvent(e)]; ok {
e.Rune = conv.Rune
e.Code = conv.Code
e.Modifiers = conv.Modifiers
em.appendEventOther(ET_OTHER, e)
} else {
em.appendEventOther(ET_OTHER, e)
}
//println("Typed:", e.Glyph, e.Chord, "alting:", alting)
case key.DirRelease:
if e.Code == key.CodeRightAlt || e.Code == key.CodeCompose {
em.alting = true
em.altingSeq = ""
}
em.appendEventOther(ET_OTHER, ei)
}
case mouse.Event:
switch e.Direction {
case mouse.DirNone:
if e.Button == mouse.ButtonNone {
em.appendMouseMovedEvent(e)
} else {
em.appendMouseDraggedEvent(e)
}
case mouse.DirStep:
where := image.Point{int(e.X), int(e.Y)}
switch e.Button {
case mouse.ButtonWheelUp:
em.appendWheelEvent(where, -1)
case mouse.ButtonWheelDown:
em.appendWheelEvent(where, +1)
}
case mouse.DirPress:
if e.Button == mouse.ButtonNone {
break
}
where := image.Point{int(e.X), int(e.Y)}
now := time.Now()
dist := math.Sqrt(float64(em.dblclickp.X-where.X)*float64(em.dblclickp.X-where.X) + float64(em.dblclickp.Y-where.Y)*float64(em.dblclickp.Y-where.Y))
if (e.Button == em.dblclickbtn) && (dist < 5) && (now.Sub(em.dblclickt) < time.Duration(200*time.Millisecond)) {
em.dblclickt = now
em.dblclickc++
} else {
em.dblclickbtn = e.Button
em.dblclickp = where
em.dblclickt = now
em.dblclickc = 1
}
if em.dblclickc > 3 {
em.dblclickc = 1
}
em.appendEventOther(ET_OTHER, e)
em.appendEventOther(ET_OTHER, util.MouseDownEvent{
Where: where,
Which: e.Button,
Count: em.dblclickc,
Modifiers: e.Modifiers,
})
case mouse.DirRelease:
if e.Button == mouse.ButtonNone {
break
}
em.appendEventOther(ET_OTHER, e)
}
case size.Event:
em.appendResizeEvent(e)
default:
em.appendEventOther(ET_OTHER, ei)
}
}
func (em *eventMachine) appendEventOther(et eventType, ei interface{}) {
em.events = append(em.events, eventWrapper{et, ei})
}
func (em *eventMachine) appendMouseDraggedEvent(e mouse.Event) {
for i := range em.events {
if em.events[i].et == ET_MOUSE_DRAG {
em.removeAndReaddEvent(i, e, ET_MOUSE_DRAG)
return
}
}
em.appendEventOther(ET_MOUSE_DRAG, e)
}
func (em *eventMachine) appendMouseMovedEvent(e mouse.Event) {
for i := range em.events {
if em.events[i].et == ET_MOUSE_MOVE {
em.removeAndReaddEvent(i, e, ET_MOUSE_MOVE)
return
}
}
em.appendEventOther(ET_MOUSE_MOVE, e)
}
func (em *eventMachine) appendWheelEvent(w image.Point, d int) {
for i := range em.events {
if em.events[i].et == ET_WHEEL {
e := em.events[i].ei.(util.WheelEvent)
e.Count += d
em.removeAndReaddEvent(i, e, ET_WHEEL)
return
}
}
em.appendEventOther(ET_WHEEL, util.WheelEvent{Count: d, Where: w})
}
func (em *eventMachine) appendResizeEvent(e size.Event) {
for i := range em.events {
if em.events[i].et == ET_RESIZE {
em.removeAndReaddEvent(i, e, ET_RESIZE)
return
}
}
em.appendEventOther(ET_RESIZE, e)
}
func (em *eventMachine) removeAndReaddEvent(i int, e interface{}, et eventType) {
copy(em.events[i:], em.events[i+1:])
em.events[len(em.events)-1] = eventWrapper{et, e}
}