-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.go
221 lines (199 loc) · 4.42 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
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
package webview2
import (
"runtime"
"sync"
"unsafe"
"github.com/eyasliu/desktop/go-webview2/internal/w32"
"github.com/eyasliu/desktop/tray"
)
type eventName byte
const (
eventInit eventName = iota
eventTerminate
eventDispatch
eventDestroy
eventSetTitle
eventSetSize
eventNavigate
eventSetHtml
eventEval
eventBind
eventHide
eventShow
)
type winEvent struct {
name eventName
data any
}
type setSizeParam struct {
width int
height int
hint Hint
}
type bindParam struct {
name string
fn any
}
type Window struct {
webview *webview
ready bool
readyMu sync.Mutex
preReady []func()
hasTray bool
}
func NewWin(option WebViewOptions, trayOpt *tray.Tray) *Window {
runtime.LockOSThread()
win := &Window{
hasTray: trayOpt != nil,
}
win.webview = NewWithOptions(option).(*webview)
return win
}
// 启动事件循环
func (w *Window) Run() {
w.preRun()
var msg w32.Msg
for {
_, _, _ = w32.User32GetMessageW.Call(
uintptr(unsafe.Pointer(&msg)),
0,
0,
0,
)
if msg.Message == w32.WMApp {
w.webview.m.Lock()
q := append([]func(){}, w.webview.dispatchq...)
w.webview.dispatchq = []func(){}
w.webview.m.Unlock()
for _, v := range q {
v()
}
} else if msg.Message == w32.WMQuit {
break
} else if msg.Message == 10086 {
event := (*winEvent)(unsafe.Pointer(msg.WParam))
switch event.name {
case eventInit:
js := event.data.(string)
w.webview.Init(js)
case eventTerminate:
w.webview.Terminate()
case eventDispatch:
fn := event.data.(func())
w.webview.Dispatch(fn)
case eventDestroy:
if w.hasTray {
tray.Quit()
}
w.webview.Destroy()
case eventSetTitle:
title := event.data.(string)
w.webview.SetTitle(title)
case eventSetSize:
size := event.data.(setSizeParam)
w.webview.SetSize(size.width, size.height, size.hint)
case eventNavigate:
u := event.data.(string)
w.webview.Navigate(u)
case eventSetHtml:
html := event.data.(string)
w.webview.SetHtml(html)
case eventEval:
js := event.data.(string)
w.webview.Eval(js)
case eventBind:
b := event.data.(bindParam)
w.webview.Bind(b.name, b.fn)
case eventHide:
w.webview.Hide()
case eventShow:
w.webview.Show()
}
continue
}
r, _, _ := w32.User32GetAncestor.Call(uintptr(msg.Hwnd), w32.GARoot)
r, _, _ = w32.User32IsDialogMessage.Call(r, uintptr(unsafe.Pointer(&msg)))
if r != 0 {
continue
}
_, _, _ = w32.User32TranslateMessage.Call(uintptr(unsafe.Pointer(&msg)))
_, _, _ = w32.User32DispatchMessageW.Call(uintptr(unsafe.Pointer(&msg)))
}
runtime.UnlockOSThread()
}
func (w *Window) onReady() {
if w.ready {
return
}
w.readyMu.Lock()
w.ready = true
q := append([]func(){}, w.preReady...)
w.preReady = nil // 用完后就没用了
w.readyMu.Unlock()
for _, v := range q {
v()
}
}
// 为了触发 w.onReady
func (w *Window) preRun() {
w.webview.onNavigationCompleted(func(args *navigationCompletedArg) {
w.onReady()
})
}
// 当 webview 启动了,但是没有完全启动好,就 postmessage 的话,会导致初始化异常,导致奇怪bug
// 所以用 w.ready 之前的消息先存起来,等准备好了之后一起发送
func (w *Window) dispatch(name eventName, data any) {
if !w.ready {
w.readyMu.Lock()
w.preReady = append(w.preReady, func() {
w.dispatch(name, data)
})
w.readyMu.Unlock()
return
}
w32.User32PostThreadMessageW.Call(
w.webview.mainthread,
10086,
uintptr(unsafe.Pointer(&winEvent{name, data})),
0,
)
}
func (w *Window) Terminate() {
w.dispatch(eventTerminate, nil)
}
func (w *Window) Dispatch(f func()) {
w.dispatch(eventDispatch, f)
}
func (w *Window) Destroy() {
w.dispatch(eventDestroy, nil)
}
func (w *Window) Window() unsafe.Pointer {
return w.webview.Window()
}
func (w *Window) SetTitle(title string) {
w.dispatch(eventSetTitle, title)
}
func (w *Window) SetSize(width int, height int, hint Hint) {
w.dispatch(eventSetSize, setSizeParam{width, height, hint})
}
func (w *Window) Init(js string) {
w.dispatch(eventInit, js)
}
func (w *Window) Bind(name string, f interface{}) {
w.dispatch(eventBind, bindParam{name, f})
}
func (w *Window) Navigate(url string) {
w.dispatch(eventNavigate, url)
}
func (w *Window) SetHtml(html string) {
w.dispatch(eventSetHtml, html)
}
func (w *Window) Eval(js string) {
w.dispatch(eventEval, js)
}
func (w *Window) Show() {
w.dispatch(eventShow, nil)
}
func (w *Window) Hide() {
w.dispatch(eventHide, nil)
}