-
Notifications
You must be signed in to change notification settings - Fork 31
/
webview.go
287 lines (246 loc) · 6.7 KB
/
webview.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
287
package blink
//#include "webview.h"
import "C"
import (
"github.com/CHH/eventemitter"
"github.com/lxn/win"
"unsafe"
)
type WebView struct {
eventemitter.EventEmitter
window C.wkeWebView
handle win.HWND
autoTitle bool
jsFunc map[string]interface{}
jsData map[string]string
//事件channels
DocumentReady chan interface{} //文档ready
Destroy chan interface{} //webview销毁
IsDestroy bool
}
func NewWebView(isTransparent bool, bounds ...int) *WebView {
view := &WebView{
autoTitle: true,
jsFunc: make(map[string]interface{}),
jsData: make(map[string]string),
DocumentReady: make(chan interface{}),
Destroy: make(chan interface{}),
IsDestroy: false,
}
//初始化event emitter
view.Init()
width, height, x, y := 800, 600, 200, 200
if len(bounds) >= 2 {
width = bounds[0]
height = bounds[1]
}
if len(bounds) >= 4 {
x = bounds[2]
y = bounds[3]
}
done := make(chan bool)
jobQueue <- func() {
view.window = C.createWebWindow(C.bool(isTransparent), C.int(x), C.int(y), C.int(width), C.int(height))
view.handle = win.HWND(uintptr(unsafe.Pointer(C.getWindowHandle(view.window))))
close(done)
}
<-done
//初始化各种事件
//destroy的时候需要设置标志位
view.On("destroy", func(v *WebView) {
//关闭destroy,如果已经关闭了,则无需关闭
select {
case <-v.Destroy:
break
default:
close(v.Destroy)
}
v.IsDestroy = true
})
view.On("documentReady", func(v *WebView) {
select {
case <-v.DocumentReady:
break
default:
close(v.DocumentReady)
}
})
//同步网页标题到窗口
view.On("titleChanged", func(view *WebView, title string) {
if view.autoTitle {
view.SetWindowTitle(title)
}
})
//注入预置的API给js调用
view.Inject("MoveToCenter", view.MoveToCenter)
view.Inject("SetWindowTitle", view.SetWindowTitle)
view.Inject("EnableAutoTitle", view.EnableAutoTitle)
view.Inject("DisableAutoTitle", view.DisableAutoTitle)
view.Inject("ShowDockIcon", view.ShowDockIcon)
view.Inject("HideDockIcon", view.HideDockIcon)
view.Inject("ShowWindow", view.ShowWindow)
view.Inject("HideWindow", view.HideWindow)
view.Inject("ShowDevTools", view.ShowDevTools)
view.Inject("ToTop", view.ToTop)
view.Inject("MostTop", view.MostTop)
view.Inject("MinimizeWindow", view.MinimizeWindow)
view.Inject("MaximizeWindow", view.MaximizeWindow)
view.Inject("RestoreWindow", view.RestoreWindow)
view.Inject("DestroyWindow", view.DestroyWindow)
//把webview添加到池中
addViewToPool(view)
return view
}
func (view *WebView) processMessage(msg *win.MSG) bool {
//TODO:临时监听一波键盘事件,并直接处理了,以后要分发到标准的事件中去的
if isDebug {
if msg.Message == win.WM_KEYDOWN {
switch msg.WParam {
case 0x74: //F5
go view.Reload()
break
case 0x7b: //F12
go view.ShowDevTools()
break
}
}
}
return true
}
func (view *WebView) MoveToCenter() {
var width int32 = 0
var height int32 = 0
{
rect := &win.RECT{}
win.GetWindowRect(view.handle, rect)
width = rect.Right - rect.Left
height = rect.Bottom - rect.Top
}
var parentWidth int32 = 0
var parentHeight int32 = 0
if win.GetWindowLong(view.handle, win.GWL_STYLE) == win.WS_CHILD {
parent := win.GetParent(view.handle)
rect := &win.RECT{}
win.GetClientRect(parent, rect)
parentWidth = rect.Right - rect.Left
parentHeight = rect.Bottom - rect.Top
} else {
parentWidth = win.GetSystemMetrics(win.SM_CXSCREEN)
parentHeight = win.GetSystemMetrics(win.SM_CYSCREEN)
}
x := (parentWidth - width) / 2
y := (parentHeight - height) / 2
win.MoveWindow(view.handle, x, y, width, height, false)
}
func (view *WebView) SetWindowTitle(title string) {
done := make(chan bool)
jobQueue <- func() {
C.setWindowTitle(view.window, C.CString(title))
close(done)
}
<-done
}
func (view *WebView) EnableAutoTitle() {
view.autoTitle = true
view.SetWindowTitle(view.GetWebTitle())
}
func (view *WebView) DisableAutoTitle() {
view.autoTitle = false
}
func (view *WebView) GetWebTitle() string {
//等待document ready,文档没有ready,网页的标题获取不到
<-view.DocumentReady
done := make(chan string)
jobQueue <- func() {
done <- C.GoString(C.getWebTitle(view.window))
close(done)
}
return <-done
}
func (view *WebView) LoadURL(url string) {
done := make(chan bool)
jobQueue <- func() {
C.loadURL(view.window, C.CString(url))
close(done)
}
<-done
}
func (view *WebView) ShowCaption() {
style := win.GetWindowLongPtr(view.handle, win.GWL_STYLE)
win.SetWindowLongPtr(view.handle, win.GWL_STYLE, style|win.WS_CAPTION|win.WS_SYSMENU|win.WS_SIZEBOX)
}
func (view *WebView) HideCaption() {
style := win.GetWindowLongPtr(view.handle, win.GWL_STYLE)
win.SetWindowLongPtr(view.handle, win.GWL_STYLE, style&^win.WS_CAPTION&^win.WS_SYSMENU&^win.WS_SIZEBOX)
}
func (view *WebView) ShowDockIcon() {
style := win.GetWindowLong(view.handle, win.GWL_EXSTYLE)
win.SetWindowLong(view.handle, win.GWL_EXSTYLE, style&^win.WS_EX_TOOLWINDOW)
}
func (view *WebView) HideDockIcon() {
style := win.GetWindowLong(view.handle, win.GWL_EXSTYLE)
win.SetWindowLong(view.handle, win.GWL_EXSTYLE, style|win.WS_EX_TOOLWINDOW)
}
func (view *WebView) ShowWindow() {
win.ShowWindow(view.handle, win.SW_SHOW)
}
func (view *WebView) HideWindow() {
win.ShowWindow(view.handle, win.SW_HIDE)
}
func (view *WebView) ShowDevTools() {
done := make(chan bool)
jobQueue <- func() {
C.showDevTools(view.window)
close(done)
}
<-done
}
func (view *WebView) Reload() {
done := make(chan bool)
jobQueue <- func() {
C.reloadURL(view.window)
close(done)
}
<-done
}
func (view *WebView) ToTop() {
rect := &win.RECT{}
win.GetWindowRect(view.handle, rect)
win.SetWindowPos(view.handle, win.HWND_TOP, rect.Left, rect.Top, rect.Right-rect.Left, rect.Bottom-rect.Top, 0)
}
func (view *WebView) MostTop(isTop bool) {
rect := &win.RECT{}
win.GetWindowRect(view.handle, rect)
if isTop {
win.SetWindowPos(view.handle, win.HWND_TOPMOST, rect.Left, rect.Top, rect.Right-rect.Left, rect.Bottom-rect.Top, 0)
} else {
win.SetWindowPos(view.handle, win.HWND_NOTOPMOST, rect.Left, rect.Top, rect.Right-rect.Left, rect.Bottom-rect.Top, 0)
}
}
func (view *WebView) MaximizeWindow() {
win.ShowWindow(view.handle, win.SW_MAXIMIZE)
}
func (view *WebView) MinimizeWindow() {
win.ShowWindow(view.handle, win.SW_MINIMIZE)
}
func (view *WebView) RestoreWindow() {
win.ShowWindow(view.handle, win.SW_RESTORE)
}
func (view *WebView) DestroyWindow() {
if !view.IsDestroy {
done := make(chan bool)
jobQueue <- func() {
//关闭destroy,如果已经关闭了,则无需关闭
select {
case <-view.Destroy:
break
default:
close(view.Destroy)
}
view.IsDestroy = true
C.destroyWindow(view.window)
close(done)
}
<-done
}
}