-
Notifications
You must be signed in to change notification settings - Fork 71
/
event.go
266 lines (244 loc) · 6.98 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
// Copyright 2023 Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build js
package web
import (
"image"
"slices"
"strings"
"syscall/js"
"cogentcore.org/core/base/errors"
"cogentcore.org/core/events"
"cogentcore.org/core/events/key"
"cogentcore.org/core/math32"
"cogentcore.org/core/system"
)
func (a *App) AddEventListeners() {
g := js.Global()
AddEventListener(g, "mousedown", a.OnMouseDown)
// see https://chromestatus.com/feature/5093566007214080
AddEventListener(g, "touchstart", a.OnTouchStart, map[string]any{"passive": false})
AddEventListener(g, "mouseup", a.OnMouseUp)
AddEventListener(g, "touchend", a.OnTouchEnd)
AddEventListener(g, "mousemove", a.OnMouseMove)
// see https://chromestatus.com/feature/5093566007214080
AddEventListener(g, "touchmove", a.OnTouchMove, map[string]any{"passive": false})
// see https://chromestatus.com/feature/6662647093133312
AddEventListener(g, "wheel", a.OnWheel, map[string]any{"passive": false})
AddEventListener(g, "contextmenu", a.OnContextMenu)
AddEventListener(g, "keydown", a.OnKeyDown)
AddEventListener(g, "keyup", a.OnKeyUp)
AddEventListener(g, "beforeinput", a.OnBeforeInput)
AddEventListener(g, "resize", a.OnResize)
}
func AddEventListener(v js.Value, nm string, fn func(this js.Value, args []js.Value) any, opts ...map[string]any) {
if len(opts) > 0 {
v.Call("addEventListener", nm, js.FuncOf(fn), opts[0])
} else {
v.Call("addEventListener", nm, js.FuncOf(fn))
}
}
// EventPos returns the appropriate position for the given event,
// multiplying the x and y components by the device pixel ratio
// so that they line up correctly with the canvas.
func (a *App) EventPos(e js.Value) image.Point {
return a.EventPosFor(e.Get("clientX"), e.Get("clientY"))
}
// EventPosFor transforms the given position by
// multiplying the x and y components by the device pixel ratio
// so that they line up correctly with the canvas.
func (a *App) EventPosFor(x, y js.Value) image.Point {
xi, yi := x.Int(), y.Int()
xi = int(float32(xi) * a.Scrn.DevicePixelRatio)
yi = int(float32(yi) * a.Scrn.DevicePixelRatio)
return image.Pt(xi, yi)
}
func (a *App) OnMouseDown(this js.Value, args []js.Value) any {
e := args[0]
but := e.Get("button").Int()
var ebut events.Buttons
switch but {
case 0:
ebut = events.Left
case 1:
ebut = events.Middle
case 2:
ebut = events.Right
}
where := a.EventPos(e)
a.Event.MouseButton(events.MouseDown, ebut, where, a.KeyMods)
e.Call("preventDefault")
return nil
}
func (a *App) OnTouchStart(this js.Value, args []js.Value) any {
e := args[0]
touches := e.Get("changedTouches")
for i := 0; i < touches.Length(); i++ {
touch := touches.Index(i)
where := a.EventPos(touch)
a.Event.MouseButton(events.MouseDown, events.Left, where, 0)
}
e.Call("preventDefault")
return nil
}
func (a *App) OnMouseUp(this js.Value, args []js.Value) any {
e := args[0]
but := e.Get("button").Int()
var ebut events.Buttons
switch but {
case 0:
ebut = events.Left
case 1:
ebut = events.Middle
case 2:
ebut = events.Right
}
where := a.EventPos(e)
a.Event.MouseButton(events.MouseUp, ebut, where, a.KeyMods)
e.Call("preventDefault")
return nil
}
func (a *App) OnTouchEnd(this js.Value, args []js.Value) any {
e := args[0]
touches := e.Get("changedTouches")
for i := 0; i < touches.Length(); i++ {
touch := touches.Index(i)
where := a.EventPos(touch)
a.Event.MouseButton(events.MouseUp, events.Left, where, 0)
}
e.Call("preventDefault")
return nil
}
func (a *App) OnMouseMove(this js.Value, args []js.Value) any {
e := args[0]
where := a.EventPos(e)
a.Event.MouseMove(where)
e.Call("preventDefault")
return nil
}
func (a *App) OnTouchMove(this js.Value, args []js.Value) any {
e := args[0]
touches := e.Get("changedTouches")
for i := 0; i < touches.Length(); i++ {
touch := touches.Index(i)
where := a.EventPos(touch)
a.Event.MouseMove(where)
}
e.Call("preventDefault")
return nil
}
func (a *App) OnWheel(this js.Value, args []js.Value) any {
e := args[0]
delta := a.EventPosFor(e.Get("deltaX"), e.Get("deltaY"))
a.Event.Scroll(a.EventPos(e), math32.Vector2FromPoint(delta).DivScalar(8))
e.Call("preventDefault")
return nil
}
func (a *App) OnContextMenu(this js.Value, args []js.Value) any {
// no-op (we handle elsewhere), but needed to prevent browser
// from making its own context menus on right clicks
e := args[0]
e.Call("preventDefault")
return nil
}
// ShouldProcessKey returns whether the given key string should be processed by Cogent Core.
func (a *App) ShouldProcessKey(k string) bool {
if k == "Unidentified" {
return false
}
k = a.KeyMods.ModifiersString() + k
if a.SystemPlatform() == system.MacOS {
k = strings.ReplaceAll(k, "Meta", "Command")
}
if slices.Contains(system.ReservedWebShortcuts, k) {
return false
}
if a.SystemPlatform() != system.MacOS {
// system.ReservedWebShortcuts uses "Command", so also try that
k = strings.ReplaceAll(k, "Control", "Command")
if slices.Contains(system.ReservedWebShortcuts, k) {
return false
}
}
return true
}
// RuneAndCodeFromKey returns the rune and key code corresponding to the given key string.
// down is whether this is from a keyDown event (as opposed to a keyUp one)
func (a *App) RuneAndCodeFromKey(k string, down bool) (rune, key.Codes) {
switch k {
case "Shift":
a.KeyMods.SetFlag(down, key.Shift)
return 0, key.CodeLeftShift
case "Control":
a.KeyMods.SetFlag(down, key.Control)
return 0, key.CodeLeftControl
case "Alt":
a.KeyMods.SetFlag(down, key.Alt)
return 0, key.CodeLeftAlt
case "Meta":
a.KeyMods.SetFlag(down, key.Meta)
return 0, key.CodeLeftMeta
case "Enter":
return 0, key.CodeReturnEnter
case "ArrowDown":
return 0, key.CodeDownArrow
case "ArrowLeft":
return 0, key.CodeLeftArrow
case "ArrowRight":
return 0, key.CodeRightArrow
case "ArrowUp":
return 0, key.CodeUpArrow
case "Spacebar":
return ' ', 0
default:
r := []rune(k)
// if there is more than one rune, we assume it is a key code
if len(r) > 1 {
kc := key.Codes(0)
err := kc.SetString(k)
if errors.Log(err) == nil {
return 0, kc
}
}
return r[0], 0
}
}
func (a *App) OnKeyDown(this js.Value, args []js.Value) any {
e := args[0]
k := e.Get("key").String()
if !a.ShouldProcessKey(k) {
return nil
}
r, c := a.RuneAndCodeFromKey(k, true)
a.Event.Key(events.KeyDown, r, c, a.KeyMods)
e.Call("preventDefault")
return nil
}
func (a *App) OnKeyUp(this js.Value, args []js.Value) any {
e := args[0]
k := e.Get("key").String()
if !a.ShouldProcessKey(k) {
return nil
}
r, c := a.RuneAndCodeFromKey(k, false)
a.Event.Key(events.KeyUp, r, c, a.KeyMods)
e.Call("preventDefault")
return nil
}
func (a *App) OnBeforeInput(this js.Value, args []js.Value) any {
e := args[0]
data := e.Get("data").String()
if data == "" {
return nil
}
for _, r := range data {
a.Event.KeyChord(r, 0, a.KeyMods)
}
e.Call("preventDefault")
return nil
}
func (a *App) OnResize(this js.Value, args []js.Value) any {
a.Resize()
return nil
}