/
window.go
167 lines (158 loc) · 3.78 KB
/
window.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
//go:build js
// +build js
package jsdriver
import (
"image"
"image/color"
"image/draw"
"syscall/js"
"github.com/oakmound/oak/v3/shiny/driver/internal/event"
"github.com/oakmound/oak/v3/shiny/screen"
"golang.org/x/image/math/f64"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/mouse"
)
type windowImpl struct {
screen *screenImpl
cvs *Canvas2D
event.Deque
}
func (w *windowImpl) Release() {}
func (w *windowImpl) Draw(src2dst f64.Aff3, src screen.Texture, sr image.Rectangle, op draw.Op) {}
func (w *windowImpl) DrawUniform(src2dst f64.Aff3, src color.Color, sr image.Rectangle, op draw.Op) {}
func (w *windowImpl) Copy(dp image.Point, src screen.Texture, sr image.Rectangle, op draw.Op) {}
func (w *windowImpl) Scale(dr image.Rectangle, src screen.Texture, sr image.Rectangle, op draw.Op) {
rgba := src.(*textureImpl).rgba
js.CopyBytesToJS(w.cvs.copybuff, rgba.Pix)
w.cvs.imgData.Get("data").Call("set", w.cvs.copybuff)
w.cvs.ctx.Call("putImageData", w.cvs.imgData, 0, 0)
}
func (w *windowImpl) Upload(dp image.Point, src screen.Image, sr image.Rectangle) {}
func (w *windowImpl) Fill(dr image.Rectangle, src color.Color, op draw.Op) {}
func (w *windowImpl) Publish() screen.PublishResult {
return screen.PublishResult{}
}
func (w *windowImpl) sendMouseEvent(mouseEvent js.Value, dir mouse.Direction) {
x, y := mouseEvent.Get("offsetX"), mouseEvent.Get("offsetY")
button := mouseEvent.Get("button")
var mButton mouse.Button
switch button.Int() {
case 0:
mButton = mouse.ButtonLeft
case 1:
mButton = mouse.ButtonMiddle
case 2:
mButton = mouse.ButtonRight
default:
mButton = mouse.ButtonNone
}
w.Send(mouse.Event{
Button: mButton,
X: float32(x.Float()),
Y: float32(y.Float()),
Direction: dir,
})
}
func (w *windowImpl) sendKeyEvent(keyEvent js.Value, dir key.Direction) {
var mods key.Modifiers
if keyEvent.Get("shiftKey").Bool() {
mods |= key.ModShift
}
if keyEvent.Get("metaKey").Bool() {
mods |= key.ModMeta
}
if keyEvent.Get("ctrlKey").Bool() {
mods |= key.ModControl
}
if keyEvent.Get("altKey").Bool() {
mods |= key.ModAlt
}
if keyEvent.Get("repeat").Bool() {
dir = key.DirNone
}
w.Send(key.Event{
Modifiers: mods,
Code: parseKeyCode(keyEvent.Get("code").String()),
Rune: rune(keyEvent.Get("key").String()[0]),
Direction: dir,
})
}
func parseKeyCode(cd string) key.Code {
switch cd {
case "ArrowLeft":
return key.CodeLeftArrow
case "ArrowRight":
return key.CodeRightArrow
case "ArrowUp":
return key.CodeUpArrow
case "ArrowDown":
return key.CodeDownArrow
case "KeyA":
return key.CodeA
case "KeyB":
return key.CodeB
case "KeyC":
return key.CodeC
case "KeyD":
return key.CodeD
case "KeyE":
return key.CodeE
case "KeyF":
return key.CodeF
case "KeyG":
return key.CodeG
case "KeyH":
return key.CodeH
case "KeyI":
return key.CodeI
case "KeyJ":
return key.CodeJ
case "KeyK":
return key.CodeK
case "KeyL":
return key.CodeL
case "KeyM":
return key.CodeM
case "KeyN":
return key.CodeN
case "KeyO":
return key.CodeO
case "KeyP":
return key.CodeP
case "KeyQ":
return key.CodeQ
case "KeyR":
return key.CodeR
case "KeyS":
return key.CodeS
case "KeyT":
return key.CodeT
case "KeyU":
return key.CodeU
case "KeyV":
return key.CodeV
case "KeyW":
return key.CodeW
case "KeyX":
return key.CodeX
case "KeyY":
return key.CodeY
case "KeyZ":
return key.CodeZ
case "Enter":
return key.CodeReturnEnter
case "Escape":
return key.CodeEscape
case "Space":
return key.CodeSpacebar
case "ShiftLeft":
return key.CodeLeftShift
case "ShiftRight":
return key.CodeRightShift
case "Backspace":
return key.CodeDeleteBackspace
// TODO: more keys
default:
return key.CodeUnknown
}
}