-
Notifications
You must be signed in to change notification settings - Fork 2
/
freeMiniblink.go
613 lines (562 loc) · 15.6 KB
/
freeMiniblink.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package GoMiniblink
import (
"fmt"
"image"
"math"
"strconv"
"strings"
"time"
"unsafe"
fm "github.com/hujun528/GoMiniblink/forms"
cs "github.com/hujun528/GoMiniblink/forms/controls"
win "github.com/hujun528/GoMiniblink/forms/windows/win32"
)
var fnCall = "fn" + strconv.FormatInt(time.Now().UnixNano(), 32)
func init() {
BindJsFunc(JsFnBinding{
Name: fnCall,
Fn: execGoFunc,
})
}
func execGoFunc(ctx GoFnContext) interface{} {
wkeName := ctx.Param[0].(string)
fnName := ctx.Param[1].(string)
rsName := ctx.Param[2].(string)
if num, err := strconv.ParseUint(wkeName, 10, 64); err == nil {
wke := wkeHandle(uintptr(num))
mb := views[wke]
if impl, implOk := mb.(*freeMiniblink); implOk {
fn := impl.fnMap[fnName]
rs := fn.Call(mb, ctx.Param[3:])
es := mbApi.wkeGlobalExec(mb.GetHandle())
v := toJsValue(mb, es, rs)
mbApi.jsSetGlobal(es, rsName, v)
}
}
return nil
}
type freeMiniblink struct {
view *cs.Control
wke wkeHandle
fnMap map[string]JsFnBinding
jsIsReady bool
frames []FrameContext
reqMap map[wkeNetJob]*freeRequestBeforeEvArgs
isLockMouse bool
isBmpPaint bool
onRequest RequestBeforeCallback
onJsReady JsReadyCallback
onConsole ConsoleCallback
documentReady DocumentReadyCallback
paintUpdated PaintUpdatedCallback
}
func (_this *freeMiniblink) init(control *cs.Control) *freeMiniblink {
_this.view = control
_this.fnMap = make(map[string]JsFnBinding)
_this.reqMap = make(map[wkeNetJob]*freeRequestBeforeEvArgs)
_this.setView()
_this.mbInit()
return _this
}
func (_this *freeMiniblink) SetProxy(info ProxyInfo) {
mbApi.wkeSetViewProxy(_this.wke, info)
}
func (_this *freeMiniblink) MouseIsEnable() bool {
return _this.isLockMouse == false
}
func (_this *freeMiniblink) MouseEnable(b bool) {
_this.isLockMouse = b == false
}
func (_this *freeMiniblink) CallJsFunc(name string, param []interface{}) interface{} {
es := mbApi.wkeGlobalExec(_this.wke)
var ps []jsValue
for i := 0; i < len(param); i++ {
v := toJsValue(_this, es, param[i])
ps = append(ps, v)
}
fn := mbApi.jsGetGlobal(es, name)
rs := mbApi.jsCall(es, fn, mbApi.jsUndefined(), ps)
return toGoValue(_this, es, rs)
}
func (_this *freeMiniblink) JsFunc(name string, fn GoFn, state interface{}) {
_this.fnMap[name] = JsFnBinding{
Name: name,
State: state,
Fn: fn,
}
if _this.jsIsReady {
for _, f := range _this.frames {
f.RunJs(_this.getJsBindingScript(f.IsMain()))
}
}
}
func (_this *freeMiniblink) getJsBindingScript(isMain bool) string {
rsName := "rs" + strconv.FormatUint(uint64(_this.wke), 32)
call := fnCall
if isMain == false {
call = fmt.Sprintf("window.top[%q]", call)
}
var list []string
for k := range _this.fnMap {
js := fmt.Sprintf(`window[%q]=function(){
var rs=%q;
var arr = Array.prototype.slice.call(arguments);
var args = [%q,%q,rs].concat(arr);
%s.apply(null,args);
return window.top[rs];
};`,
k, rsName, strconv.FormatUint(uint64(_this.wke), 10), k, call)
list = append(list, js)
}
return strings.Join(list, ";")
}
func (_this *freeMiniblink) RunJs(script string) interface{} {
es := mbApi.wkeGlobalExec(_this.wke)
rs := mbApi.jsEval(es, script)
return toGoValue(_this, es, rs)
}
func (_this *freeMiniblink) SetOnDocumentReady(callback DocumentReadyCallback) {
_this.documentReady = callback
}
func (_this *freeMiniblink) SetOnConsole(callback ConsoleCallback) {
_this.onConsole = callback
}
func (_this *freeMiniblink) SetOnJsReady(callback JsReadyCallback) {
_this.onJsReady = callback
}
func (_this *freeMiniblink) SetOnRequestBefore(callback RequestBeforeCallback) {
_this.onRequest = callback
}
func (_this *freeMiniblink) SetOnPaintUpdated(callback PaintUpdatedCallback) {
_this.paintUpdated = callback
}
func (_this *freeMiniblink) mbInit() {
_this.wke = createWebView(_this)
_this.viewResize(_this.view.GetBound().Rect)
mbApi.wkeSetHandle(_this.wke, _this.view.GetHandle())
mbApi.wkeOnPaintBitUpdated(_this.wke, _this.onPaintBitUpdated, 0)
mbApi.wkeOnLoadUrlBegin(_this.wke, _this.onUrlBegin, 0)
mbApi.wkeOnLoadUrlEnd(_this.wke, _this.onUrlEnd, 0)
mbApi.wkeOnLoadUrlFail(_this.wke, _this.onUrlFail, 0)
mbApi.wkeOnDidCreateScriptContext(_this.wke, _this.onDidCreateScriptContext, 0)
mbApi.wkeOnConsole(_this.wke, _this.jsConsole, 0)
mbApi.wkeOnDocumentReady(_this.wke, _this.onDocumentReady, 0)
}
func (_this *freeMiniblink) onDocumentReady(_ wkeHandle, _ uintptr, frame wkeFrame) uintptr {
args := new(freeDocumentReadyEvArgs).init(_this, frame)
if _this.documentReady != nil {
_this.documentReady(args)
}
return 0
}
func (_this *freeMiniblink) jsConsole(_ wkeHandle, _ uintptr, level int32, msg, name wkeString, line uint32, stack wkeString) uintptr {
if _this.onConsole == nil {
return 0
}
args := new(freeConsoleMessageEvArgs).init()
args.line = int(line)
args.message = mbApi.wkeGetString(msg)
args.name = mbApi.wkeGetString(name)
args.stack = mbApi.wkeGetString(stack)
lv := wkeConsoleLevel(level)
switch lv {
case wkeConsoleLevel_Log:
args.level = "log"
case wkeConsoleLevel_Warning:
args.level = "warning"
case wkeConsoleLevel_Error:
args.level = "error"
case wkeConsoleLevel_Debug:
args.level = "debug"
case wkeConsoleLevel_Info:
args.level = "info"
case wkeConsoleLevel_RevokedError:
args.level = "revoke"
default:
panic("无法识别的类型:" + strconv.Itoa(int(lv)))
}
_this.onConsole(args)
return 0
}
func (_this *freeMiniblink) onDidCreateScriptContext(_ wkeHandle, _ uintptr, frame wkeFrame, _ uintptr, _, _ int) uintptr {
_this.jsIsReady = true
args := new(wkeJsReadyEvArgs).init(_this, frame)
_this.frames = append(_this.frames, args)
args.RunJs(_this.getJsBindingScript(args.IsMain()))
if _this.onJsReady == nil {
return 0
}
_this.onJsReady(args)
return 0
}
func (_this *freeMiniblink) onUrlBegin(_ wkeHandle, _, _ uintptr, job wkeNetJob) uintptr {
if _this.onRequest == nil {
return 0
}
e := new(freeRequestBeforeEvArgs).init(_this, job)
e.EvFinish().AddEx(func() {
delete(_this.reqMap, job)
})
_this.onRequest(e)
e.onBegin()
_this.reqMap[job] = e
return 0
}
func (_this *freeMiniblink) onUrlEnd(_ wkeHandle, _, _ uintptr, job wkeNetJob, buf uintptr, len int32) uintptr {
if req, ok := _this.reqMap[job]; ok {
data := (*[1 << 30]byte)(unsafe.Pointer(buf))
rs := make([]byte, int(len))
for i := int32(0); i < len; i++ {
rs[i] = data[i]
}
req.onResponse(rs)
}
return 0
}
func (_this *freeMiniblink) onUrlFail(_ wkeHandle, _, _ uintptr, job wkeNetJob) uintptr {
if req, ok := _this.reqMap[job]; ok {
req.onFail()
}
return 0
}
func (_this *freeMiniblink) setView() {
bakFocus := _this.view.OnFocus
_this.view.OnFocus = func() {
_this.viewFocus()
if bakFocus != nil {
bakFocus()
}
}
bakLostFocus := _this.view.OnLostFocus
_this.view.OnLostFocus = func() {
_this.viewLostFocus()
if bakLostFocus != nil {
bakLostFocus()
}
}
bakResize := _this.view.OnResize
_this.view.OnResize = func(e fm.Rect) {
_this.viewResize(e)
if bakResize != nil {
bakResize(e)
}
}
bakPaint := _this.view.OnPaint
_this.view.OnPaint = func(e fm.PaintEvArgs) {
_this.viewPaint(e)
if bakPaint != nil {
bakPaint(e)
}
}
bakMouseMove := _this.view.OnMouseMove
_this.view.OnMouseMove = func(e *fm.MouseEvArgs) {
if _this.isLockMouse == false {
_this.viewMouseMove(e)
}
if bakMouseMove != nil {
bakMouseMove(e)
}
}
bakMouseDown := _this.view.OnMouseDown
_this.view.OnMouseDown = func(e *fm.MouseEvArgs) {
if _this.isLockMouse == false {
_this.viewMouseDown(e)
}
if bakMouseDown != nil {
bakMouseDown(e)
}
}
bakMouseUp := _this.view.OnMouseUp
_this.view.OnMouseUp = func(e *fm.MouseEvArgs) {
if _this.isLockMouse == false {
_this.viewMouseUp(e)
}
if bakMouseUp != nil {
bakMouseUp(e)
}
}
bakMouseWheel := _this.view.OnMouseWheel
_this.view.OnMouseWheel = func(e *fm.MouseEvArgs) {
if _this.isLockMouse == false {
_this.viewMouseWheel(e)
}
if bakMouseWheel != nil {
bakMouseWheel(e)
}
}
bakSetCursor := _this.view.OnSetCursor
_this.view.OnSetCursor = func() bool {
b := _this.viewSetCursor()
if !b && bakSetCursor != nil {
b = bakSetCursor()
}
return b
}
bakKeyDown := _this.view.OnKeyDown
_this.view.OnKeyDown = func(e *fm.KeyEvArgs) {
_this.viewKeyDown(e)
if bakKeyDown != nil {
bakKeyDown(e)
}
}
bakKeyUp := _this.view.OnKeyUp
_this.view.OnKeyUp = func(e *fm.KeyEvArgs) {
_this.viewKeyUp(e)
if bakKeyUp != nil {
bakKeyUp(e)
}
}
bakKeyPress := _this.view.OnKeyPress
_this.view.OnKeyPress = func(e *fm.KeyPressEvArgs) {
_this.viewKeyPress(e)
if bakKeyPress != nil {
bakKeyPress(e)
}
}
bakImeStart := _this.view.OnImeStartComposition
_this.view.OnImeStartComposition = func() bool {
b := _this.viewImeStart()
if !b && bakImeStart != nil {
b = bakImeStart()
}
return b
}
}
func (_this *freeMiniblink) viewImeStart() bool {
rect := mbApi.wkeGetCaretRect(_this.wke)
comp := win.COMPOSITIONFORM{
DwStyle: win.CFS_POINT | win.CFS_FORCE_POSITION,
Pos: win.POINT{
X: rect.x,
Y: rect.y,
},
}
h := win.HWND(_this.view.GetHandle())
imc := win.ImmGetContext(h)
win.ImmSetCompositionWindow(imc, &comp)
win.ImmReleaseContext(h, imc)
return true
}
func (_this *freeMiniblink) viewKeyPress(e *fm.KeyPressEvArgs) {
if mbApi.wkeFireKeyPressEvent(_this.wke, int([]rune(e.KeyChar)[0]), uint32(wkeKeyFlags_Repeat), e.IsSys) {
e.IsHandle = true
}
}
func (_this *freeMiniblink) viewKeyUp(e *fm.KeyEvArgs) {
if _this.viewKeyEvent(e, false) {
e.IsHandle = true
}
}
func (_this *freeMiniblink) viewKeyDown(e *fm.KeyEvArgs) {
if _this.viewKeyEvent(e, true) {
e.IsHandle = true
}
}
func (_this *freeMiniblink) viewKeyEvent(e *fm.KeyEvArgs, isDown bool) bool {
flags := int(wkeKeyFlags_Repeat)
switch e.Key {
case fm.Keys_Insert, fm.Keys_Delete, fm.Keys_Home, fm.Keys_End, fm.Keys_PageUp,
fm.Keys_PageDown, fm.Keys_Left, fm.Keys_Right, fm.Keys_Up, fm.Keys_Down:
flags |= int(wkeKeyFlags_Extend)
}
if isDown {
return mbApi.wkeFireKeyDownEvent(_this.wke, uint32(e.Value), uint32(flags), e.IsSys)
} else {
return mbApi.wkeFireKeyUpEvent(_this.wke, uint32(e.Value), uint32(flags), e.IsSys)
}
}
func (_this *freeMiniblink) LoadUri(uri string) {
mbApi.wkeLoadURL(_this.wke, uri)
}
func (_this *freeMiniblink) WkeRunMessageLoop() {
mbApi.wkeRunMessageLoop()
}
func (_this *freeMiniblink) SetDebugConfig(debugString string, param string) {
mbApi.wkeSetDebugConfig(_this.wke, debugString, param)
}
func (_this *freeMiniblink) viewSetCursor() bool {
cur := mbApi.wkeGetCursorInfoType(_this.wke)
newCur := fm.CursorType_Default
switch cur {
case wkeCursorType_Pointer:
newCur = fm.CursorType_ARROW
case wkeCursorType_Hand:
newCur = fm.CursorType_HAND
case wkeCursorType_IBeam:
newCur = fm.CursorType_IBEAM
case wkeCursorType_ColumnResize:
newCur = fm.CursorType_SIZEWE
case wkeCursorType_RowResize:
newCur = fm.CursorType_SIZENS
case wkeCursorType_Cross:
newCur = fm.CursorType_CROSS
default:
fmt.Println("未实现的鼠标指针类型:" + strconv.Itoa(int(cur)))
}
_this.view.SetCursor(newCur)
return true
}
func (_this *freeMiniblink) viewMouseWheel(e *fm.MouseEvArgs) {
flags := wkeMouseFlags_None
keys := cs.App.ModifierKeys()
if s, ok := keys[fm.Keys_Ctrl]; ok && s {
flags |= wkeMouseFlags_CONTROL
}
if s, ok := keys[fm.Keys_Shift]; ok && s {
flags |= wkeMouseFlags_SHIFT
}
if e.Button&fm.MouseButtons_Left != 0 {
flags |= wkeMouseFlags_LBUTTON
}
if e.Button&fm.MouseButtons_Right != 0 {
flags |= wkeMouseFlags_RBUTTON
}
if e.Button&fm.MouseButtons_Middle != 0 {
flags |= wkeMouseFlags_MBUTTON
}
if mbApi.wkeFireMouseWheelEvent(_this.wke, int32(e.X), int32(e.Y), int32(e.Delta), int32(flags)) {
e.IsHandle = true
}
}
func (_this *freeMiniblink) viewMouseUp(e *fm.MouseEvArgs) {
_this.viewMouseEvent(e, false)
}
func (_this *freeMiniblink) viewMouseDown(e *fm.MouseEvArgs) {
_this.viewMouseEvent(e, true)
}
func (_this *freeMiniblink) viewMouseEvent(e *fm.MouseEvArgs, isDown bool) {
flags := wkeMouseFlags_None
keys := cs.App.ModifierKeys()
if s, ok := keys[fm.Keys_Ctrl]; ok && s {
flags |= wkeMouseFlags_CONTROL
}
if s, ok := keys[fm.Keys_Shift]; ok && s {
flags |= wkeMouseFlags_SHIFT
}
msg := 0
if e.Button&fm.MouseButtons_Left != 0 {
flags |= wkeMouseFlags_LBUTTON
if e.IsDouble {
msg = win.WM_LBUTTONDBLCLK
} else if isDown {
msg = win.WM_LBUTTONDOWN
} else {
msg = win.WM_LBUTTONUP
}
}
if e.Button&fm.MouseButtons_Right != 0 {
flags |= wkeMouseFlags_RBUTTON
if e.IsDouble {
msg = win.WM_RBUTTONDBLCLK
} else if isDown {
msg = win.WM_RBUTTONDOWN
} else {
msg = win.WM_RBUTTONUP
}
}
if e.Button&fm.MouseButtons_Middle != 0 {
flags |= wkeMouseFlags_MBUTTON
if e.IsDouble {
msg = win.WM_MBUTTONDBLCLK
} else if isDown {
msg = win.WM_MBUTTONDOWN
} else {
msg = win.WM_MBUTTONUP
}
}
if msg != 0 && mbApi.wkeFireMouseEvent(_this.wke, int32(msg), int32(e.X), int32(e.Y), int32(flags)) {
e.IsHandle = true
}
}
func (_this *freeMiniblink) viewMouseMove(e *fm.MouseEvArgs) {
flags := wkeMouseFlags_None
if e.Button&fm.MouseButtons_Left != 0 {
flags |= wkeMouseFlags_LBUTTON
}
if e.Button&fm.MouseButtons_Right != 0 {
flags |= wkeMouseFlags_RBUTTON
}
if mbApi.wkeFireMouseEvent(_this.wke, int32(win.WM_MOUSEMOVE), int32(e.X), int32(e.Y), int32(flags)) {
e.IsHandle = true
}
}
func (_this *freeMiniblink) ToBitmap() *image.RGBA {
w := mbApi.wkeGetWidth(_this.wke)
h := mbApi.wkeGetHeight(_this.wke)
w = uint32(math.Max(float64(w), 0))
h = uint32(math.Max(float64(h), 0))
view := image.NewRGBA(image.Rect(0, 0, int(w), int(h)))
mbApi.wkePaint(_this.wke, view.Pix, 0)
return view
}
func (_this *freeMiniblink) viewPaint(e fm.PaintEvArgs) {
if _this.isBmpPaint {
img := _this.ToBitmap()
if img.Bounds().Empty() == false {
e.Graphics.DrawImage(img, e.Clip.X, e.Clip.Y, e.Clip.Width, e.Clip.Height, e.Clip.X, e.Clip.Y)
}
} else {
hdc := win.HDC(e.Graphics.GetHandle())
mdc := win.HDC(mbApi.wkeGetViewDC(_this.wke))
win.BitBlt(hdc, int32(e.Clip.X), int32(e.Clip.Y), int32(e.Clip.Width), int32(e.Clip.Height), mdc, int32(e.Clip.X), int32(e.Clip.Y), win.SRCCOPY)
}
}
func (_this *freeMiniblink) onPaintBitUpdated(wke wkeHandle, _, bits uintptr, rect *wkeRect, width, _ int32) uintptr {
bx, by := int(rect.x), int(rect.y)
bw, bh := int(math.Min(float64(rect.w), float64(width))), int(math.Min(float64(rect.h), float64(mbApi.wkeGetHeight(wke))))
var bmp *image.RGBA
if _this.isBmpPaint {
bmp = image.NewRGBA(image.Rect(0, 0, bw, bh))
stride := int(width) * 4
pixs := (*[1 << 30]byte)(unsafe.Pointer(bits))
for y := 0; y < bh; y++ {
for x := 0; x < bw*4; x++ {
sp := bmp.Stride*y + x
dp := stride*(by+y) + bx*4 + x
bmp.Pix[sp] = pixs[dp]
}
}
}
args := new(freePaintUpdatedEvArgs).init(bmp, fm.Bound{
Point: fm.Point{
X: bx,
Y: by,
},
Rect: fm.Rect{
Width: bw,
Height: bh,
},
})
if _this.paintUpdated != nil {
_this.paintUpdated(args)
}
if args.IsCancel() {
return 0
} else if _this.isBmpPaint {
_this.view.CreateGraphics().DrawImage(bmp, 0, 0, bw, bh, bx, by).Close()
} else {
r := win.RECT{
Left: rect.x,
Top: rect.y,
Right: rect.x + rect.w,
Bottom: rect.y + rect.h,
}
win.InvalidateRect(win.HWND(_this.view.GetHandle()), &r, false)
}
return 0
}
func (_this *freeMiniblink) SetBmpPaintMode(b bool) {
_this.isBmpPaint = b
}
func (_this *freeMiniblink) viewResize(e fm.Rect) {
mbApi.wkeResize(_this.wke, uint32(e.Width), uint32(e.Height))
}
func (_this *freeMiniblink) viewLostFocus() {
mbApi.wkeKillFocus(_this.wke)
}
func (_this *freeMiniblink) viewFocus() {
mbApi.wkeSetFocus(_this.wke)
}
func (_this *freeMiniblink) GetHandle() wkeHandle {
return _this.wke
}