-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
158 lines (134 loc) · 3.93 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
package webapi
import (
"log"
"syscall/js"
"github.com/codeation/canvas/jsw"
)
type window struct {
id int
frameID int
x, y int
canvas js.Value
canvasCtx js.Value
}
func (w *webAPI) WindowNew(windowID int, frameID int, x, y, width, height int) {
w.mutex.Lock()
defer w.mutex.Unlock()
frame, ok := w.frames[frameID]
if !ok {
log.Printf("WindowNew: frame not found: %d", frameID)
return
}
offsetX, offsetY := w.frameOffset(frameID)
x += offsetX
y += offsetY
canvas := w.document.Call(jsw.CreateElement, jsw.Canvas)
canvasStyle := canvas.Get(jsw.Style)
canvasStyle.Set(jsw.Position, jsw.Fixed)
canvasStyle.Set(jsw.Left, px(x))
canvasStyle.Set(jsw.Top, px(y))
canvasStyle.Set(jsw.Width, px(width))
canvasStyle.Set(jsw.Height, px(height))
canvas.Set(jsw.Width, width*2)
canvas.Set(jsw.Height, height*2)
canvasCtx := canvas.Call(jsw.GetContext, jsw.Context2D)
canvasCtx.Call(jsw.Scale, 2, 2)
frame.div.Call(jsw.AppendChild, canvas)
w.windows[windowID] = &window{
id: windowID,
frameID: frameID,
canvas: canvas,
canvasCtx: canvasCtx,
x: x,
y: y,
}
}
func (w *webAPI) WindowDrop(windowID int) {
w.mutex.Lock()
defer w.mutex.Unlock()
window, ok := w.windows[windowID]
if !ok {
log.Printf("WindowDrop: window not found: %d", windowID)
return
}
frame, ok := w.frames[window.frameID]
if !ok {
log.Printf("WindowDrop: frame not found: %d", window.frameID)
return
}
frame.div.Call(jsw.RemoveChild, window.canvas)
window.canvas.Call(jsw.Remove)
delete(w.windows, windowID)
}
func (w *webAPI) WindowRaise(windowID int) {}
func (w *webAPI) WindowClear(windowID int) {}
func (w *webAPI) WindowShow(windowID int) {}
func (w *webAPI) WindowSize(windowID int, x, y, width, height int) {
w.mutex.RLock()
defer w.mutex.RUnlock()
window, ok := w.windows[windowID]
if !ok {
log.Printf("WindowSize: window not found: %d", windowID)
return
}
window.x = x
window.y = y
offsetX, offsetY := w.frameOffset(window.frameID)
x += offsetX
y += offsetY
canvasStyle := window.canvas.Get(jsw.Style)
canvasStyle.Set(jsw.Left, px(x))
canvasStyle.Set(jsw.Top, px(y))
if window.canvas.Get(jsw.Width).Int() != width*2 ||
window.canvas.Get(jsw.Height).Int() != height*2 {
canvasStyle.Set(jsw.Width, px(width))
canvasStyle.Set(jsw.Height, px(height))
window.canvas.Set(jsw.Width, width*2)
window.canvas.Set(jsw.Height, height*2)
window.canvasCtx.Call(jsw.Scale, 2, 2)
}
}
func (w *webAPI) WindowFill(windowID int, x, y, width, height int, r, g, b, a uint16) {
w.mutex.RLock()
defer w.mutex.RUnlock()
window, ok := w.windows[windowID]
if !ok {
log.Printf("WindowFill: window not found: %d", windowID)
return
}
window.canvasCtx.Set(jsw.FillStyle, color(r, g, b, a))
window.canvasCtx.Call(jsw.FillRect, x, y, width, height)
}
func (w *webAPI) WindowLine(windowID int, x0, y0, x1, y1 int, r, g, b, a uint16) {
w.mutex.RLock()
defer w.mutex.RUnlock()
window, ok := w.windows[windowID]
if !ok {
log.Printf("WindowLine: window not found: %d", windowID)
return
}
window.canvasCtx.Set(jsw.StrokeStyle, color(r, g, b, a))
window.canvasCtx.Set(jsw.LineWidth, 1)
window.canvasCtx.Call(jsw.BeginPath)
window.canvasCtx.Call(jsw.MoveTo, float64(x0)+0.5, float64(y0)+0.5)
window.canvasCtx.Call(jsw.LineTo, float64(x1)+0.5, float64(y1)+0.5)
window.canvasCtx.Call(jsw.Stroke)
}
func (w *webAPI) WindowText(windowID int, x, y int, r, g, b, a uint16, fontID int, text string) {
w.mutex.RLock()
defer w.mutex.RUnlock()
window, ok := w.windows[windowID]
if !ok {
log.Printf("WindowText: window not found: %d", windowID)
return
}
font, ok := w.fonts[fontID]
if !ok {
log.Printf("WindowText: font not found %d", fontID)
return
}
window.canvasCtx.Set(jsw.Font, font.cssValue)
window.canvasCtx.Set(jsw.FillStyle, color(r, g, b, a))
window.canvasCtx.Call(jsw.FillText, text, x, y+font.baseline)
}
func (w *webAPI) WindowImage(windowID int, x, y, width, height int, imageID int) {}