-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtmlayout.go
More file actions
588 lines (522 loc) · 14.2 KB
/
htmlayout.go
File metadata and controls
588 lines (522 loc) · 14.2 KB
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
package gohl
import (
"errors"
"log"
"runtime/cgo"
"syscall"
"unsafe"
)
type NotifyHandler struct {
Behaviors map[string]*EventHandler
OnCreateControl func(params *NmhlCreateControl) uintptr
OnControlCreated func(params *NmhlCreateControl) uintptr
OnDestroyControl func(params *NmhlDestroyControl) uintptr
OnLoadData func(params *NmhlLoadData) uintptr
OnDataLoaded func(params *NmhlDataLoaded) uintptr
OnDocumentComplete func() uintptr
}
type EventHandler struct {
OnAttached func(he HELEMENT)
OnDetached func(he HELEMENT)
OnMouse func(he HELEMENT, params *MouseParams) bool
OnKey func(he HELEMENT, params *KeyParams) bool
OnFocus func(he HELEMENT, params *FocusParams) bool
OnDraw func(he HELEMENT, params *DrawParams) bool
OnTimer func(he HELEMENT, params *TimerParams) bool
OnBehaviorEvent func(he HELEMENT, params *BehaviorEventParams) bool
OnMethodCall func(he HELEMENT, params *MethodParams) bool
OnDataArrived func(he HELEMENT, params *DataArrivedParams) bool
OnSize func(he HELEMENT)
OnScroll func(he HELEMENT, params *ScrollParams) bool
OnExchange func(he HELEMENT, params *ExchangeParams) bool
OnGesture func(he HELEMENT, params *GestureParams) bool
handle cgo.Handle
}
func (e *EventHandler) AllSubscription() uint32 {
var subscription uint32 = 0
add := func(f interface{}, flag uint32) {
if f != nil {
subscription |= flag
}
}
// OnAttached and OnDetached purposely omitted, since we must receive these events
add(e.OnMouse, HANDLE_MOUSE)
add(e.OnKey, HANDLE_KEY)
add(e.OnFocus, HANDLE_FOCUS)
add(e.OnDraw, HANDLE_DRAW)
add(e.OnTimer, HANDLE_TIMER)
add(e.OnBehaviorEvent, HANDLE_BEHAVIOR_EVENT)
add(e.OnMethodCall, HANDLE_METHOD_CALL)
add(e.OnDataArrived, HANDLE_DATA_ARRIVED)
add(e.OnSize, HANDLE_SIZE)
add(e.OnScroll, HANDLE_SCROLL)
add(e.OnExchange, HANDLE_EXCHANGE)
add(e.OnGesture, HANDLE_GESTURE)
return subscription
}
// cstringToString converts a *byte (C char*) to a Go string
func cstringToString(cstr *byte) string {
if cstr == nil {
return ""
}
// Convert *byte to []byte
b := make([]byte, 0)
for p := uintptr(unsafe.Pointer(cstr)); ; p++ {
ch := *(*byte)(unsafe.Pointer(p))
if ch == 0 {
break
}
b = append(b, ch)
}
return string(b)
}
const (
TRUE = 1
FALSE = 0
)
var (
notifyHandlers = make(map[uintptr]*NotifyHandler, 8)
windowEventHandlers = make(map[uint32]*EventHandler, 8)
windowEventHandles = make(map[uint32]cgo.Handle, 8)
behaviors = make(map[*EventHandler]int, 32)
)
type HELEMENT uintptr
type HLDOM_RESULT int
type VALUE_RESULT uint32
type Point struct {
X int32
Y int32
}
type Size struct {
Cx int32
Cy int32
}
type Rect struct {
Left int32
Top int32
Right int32
Bottom int32
}
type JsonValue struct {
T uint32
U uint32
D uint64
}
type InitializationParams struct {
Cmd uint32
}
type MouseParams struct {
Cmd uint32 // MouseEvents
Target HELEMENT
Pos Point
DocumentPos Point
ButtonState uint32
AltState uint32
CursorType uint32
IsOnIcon int32
Dragging HELEMENT
DraggingMode uint32
}
type KeyParams struct {
Cmd uint32 // KeyEvents
Target HELEMENT
KeyCode uint32
AltState uint32
}
type FocusParams struct {
Cmd uint32 // FocusEvents
Target HELEMENT
ByMouseClick int32 // boolean
Cancel int32 // boolean
}
type DrawParams struct {
Cmd uint32 // DrawEvents
Hdc uintptr
Area Rect
reserved uint32
}
type TimerParams struct {
TimerId uint64
}
type BehaviorEventParams struct {
Cmd uint32 // Behavior events
Target HELEMENT
Source HELEMENT
Reason uint32
Data JsonValue
}
type MethodParams struct {
MethodId uint32
}
type DataArrivedParams struct {
Initiator HELEMENT
Data *byte
DataSize uint32
DataType uint32
Status uint32
Uri *uint16
}
type ScrollParams struct {
Cmd uint32
Target HELEMENT
Pos int32
Vertical int32 // bool
}
type ExchangeParams struct {
Cmd uint32
Target HELEMENT
Pos Point
PosView Point
DataTypes uint32
DragCmd uint32
FetchData uintptr // func pointer: typedef BOOL CALLBACK FETCH_EXCHANGE_DATA(EXCHANGE_PARAMS* params, UINT data_type, LPCBYTE* ppDataStart, UINT* pDataLength );
}
type GestureParams struct {
Cmd uint32
Target HELEMENT
Pos Point
PosView Point
Flags uint32
DeltaTime uint32
DeltaXY Size
DeltaV float64
}
// Notify structures
type NMHDR struct {
HwndFrom uint32
IdFrom uintptr
Code uint32
}
type NmhlCreateControl struct {
Header NMHDR
Element HELEMENT
InHwndParent uint32
OutHwndControl uint32
reserved1 int32
reserved2 int32
}
type NmhlDestroyControl struct {
Header NMHDR
Element HELEMENT
InOutHwndControl uint32
reserved1 int32
}
type NmhlLoadData struct {
Header NMHDR
Uri *uint16
OutData uintptr
OutDataSize int32
DataType uint32
Principal HELEMENT
Initiator HELEMENT
}
type NmhlDataLoaded struct {
Header NMHDR
Uri *uint16
Data uintptr
DataSize int32
DataType uint32
Status uint32
}
type NmhlAttachBehavior struct {
Header NMHDR
Element HELEMENT
BehaviorName *byte
ElementProc uintptr
ElementTag uintptr
ElementEvents uint32
}
// Main event handler that dispatches to the right element handler
var goElementProc = syscall.NewCallback(func(tag uintptr, he unsafe.Pointer, evtg uint32, params unsafe.Pointer) uintptr {
defer func() {
if r := recover(); r != nil {
log.Printf("[goElementProc] PANIC: %v", r)
}
}()
if tag == 0 || params == nil || he == nil {
return 0
}
handlerVal := cgo.Handle(tag).Value()
if handlerVal == nil {
return 0
}
handler, ok := handlerVal.(*EventHandler)
if !ok || handler == nil {
return 0
}
handled := false
switch evtg {
case HANDLE_INITIALIZATION:
if p := (*InitializationParams)(params); p.Cmd == BEHAVIOR_ATTACH {
if handler.OnAttached != nil {
handler.OnAttached(HELEMENT(he))
}
} else if p.Cmd == BEHAVIOR_DETACH {
if handler.OnDetached != nil {
handler.OnDetached(HELEMENT(he))
}
if behaviorRefCount, exists := behaviors[handler]; exists {
behaviorRefCount--
if behaviorRefCount == 0 {
delete(behaviors, handler)
} else {
behaviors[handler] = behaviorRefCount
}
}
cgo.Handle(tag).Delete()
}
handled = true
case HANDLE_MOUSE:
if handler.OnMouse != nil {
p := (*MouseParams)(params)
handled = handler.OnMouse(HELEMENT(he), p)
}
case HANDLE_KEY:
if handler.OnKey != nil {
p := (*KeyParams)(params)
handled = handler.OnKey(HELEMENT(he), p)
}
case HANDLE_FOCUS:
if handler.OnFocus != nil {
p := (*FocusParams)(params)
handled = handler.OnFocus(HELEMENT(he), p)
}
case HANDLE_DRAW:
if handler.OnDraw != nil {
p := (*DrawParams)(params)
handled = handler.OnDraw(HELEMENT(he), p)
}
case HANDLE_TIMER:
if handler.OnTimer != nil {
p := (*TimerParams)(params)
handled = handler.OnTimer(HELEMENT(he), p)
}
case HANDLE_BEHAVIOR_EVENT:
if handler.OnBehaviorEvent != nil {
p := (*BehaviorEventParams)(params)
handled = handler.OnBehaviorEvent(HELEMENT(he), p)
}
case HANDLE_METHOD_CALL:
if handler.OnMethodCall != nil {
p := (*MethodParams)(params)
handled = handler.OnMethodCall(HELEMENT(he), p)
}
case HANDLE_DATA_ARRIVED:
if handler.OnDataArrived != nil {
p := (*DataArrivedParams)(params)
handled = handler.OnDataArrived(HELEMENT(he), p)
}
case HANDLE_SIZE:
if handler.OnSize != nil {
handler.OnSize(HELEMENT(he))
}
case HANDLE_SCROLL:
if handler.OnScroll != nil {
p := (*ScrollParams)(params)
handled = handler.OnScroll(HELEMENT(he), p)
}
case HANDLE_EXCHANGE:
if handler.OnExchange != nil {
p := (*ExchangeParams)(params)
handled = handler.OnExchange(HELEMENT(he), p)
}
case HANDLE_GESTURE:
if handler.OnGesture != nil {
p := (*GestureParams)(params)
handled = handler.OnGesture(HELEMENT(he), p)
}
default:
// Unknown event type, just ignore
}
if handled {
return uintptr(TRUE)
}
return uintptr(FALSE)
})
var goNotifyProc = syscall.NewCallback(func(msg uint32, wparam uintptr, lparam uintptr, vparam uintptr) uintptr {
if lparam == 0 {
return 0
}
handler, exists := notifyHandlers[vparam]
if !exists || handler == nil {
return 0
}
phdr := (*NMHDR)(unsafe.Pointer(lparam))
if phdr == nil {
return 0
}
switch phdr.Code {
case HLN_CREATE_CONTROL:
if handler.OnCreateControl != nil {
return handler.OnCreateControl((*NmhlCreateControl)(unsafe.Pointer(lparam)))
}
case HLN_CONTROL_CREATED:
if handler.OnControlCreated != nil {
return handler.OnControlCreated((*NmhlCreateControl)(unsafe.Pointer(lparam)))
}
case HLN_DESTROY_CONTROL:
if handler.OnDestroyControl != nil {
return handler.OnDestroyControl((*NmhlDestroyControl)(unsafe.Pointer(lparam)))
}
case HLN_LOAD_DATA:
if handler.OnLoadData != nil {
return handler.OnLoadData((*NmhlLoadData)(unsafe.Pointer(lparam)))
}
case HLN_DATA_LOADED:
if handler.OnDataLoaded != nil {
return handler.OnDataLoaded((*NmhlDataLoaded)(unsafe.Pointer(lparam)))
}
case HLN_DOCUMENT_COMPLETE:
if handler.OnDocumentComplete != nil {
return handler.OnDocumentComplete()
}
case HLN_ATTACH_BEHAVIOR:
params := (*NmhlAttachBehavior)(unsafe.Pointer(lparam))
key := cstringToString(params.BehaviorName)
log.Printf("[HLN_ATTACH_BEHAVIOR] behaviorName=%s", key)
if params.Element == BAD_HELEMENT {
return 0
}
// 1. 先检查用户自定义 behaviors
if handler.Behaviors != nil {
if behavior, exists := handler.Behaviors[key]; exists {
log.Printf("[HLN_ATTACH_BEHAVIOR] Found custom behavior: %s", key)
if refCount, exists := behaviors[behavior]; exists {
behaviors[behavior] = refCount + 1
} else {
behaviors[behavior] = 1
}
tag := cgo.NewHandle(behavior)
params.ElementProc = uintptr(goElementProc)
params.ElementTag = uintptr(tag)
params.ElementEvents = behavior.AllSubscription()
return 1
}
}
// 2. 检查内置 behaviors (tabs, popup, etc.)
if behavior, exists := builtinBehaviors[key]; exists {
log.Printf("[HLN_ATTACH_BEHAVIOR] Found builtin behavior: %s", key)
if refCount, exists := behaviors[behavior]; exists {
behaviors[behavior] = refCount + 1
} else {
behaviors[behavior] = 1
}
tag := cgo.NewHandle(behavior)
params.ElementProc = uintptr(goElementProc)
params.ElementTag = uintptr(tag)
params.ElementEvents = behavior.AllSubscription()
return 1
}
log.Printf("[HLN_ATTACH_BEHAVIOR] Behavior not found: %s", key)
return 0
}
return 0
})
var goSelectCallback = syscall.NewCallback(func(he unsafe.Pointer, param uintptr) uintptr {
if he == nil || param == 0 {
return 0
}
slicePtr := cgo.Handle(param).Value().(*[]*Element)
e := NewElementFromHandle(HELEMENT(he))
*slicePtr = append(*slicePtr, e)
return 0
})
var goElementComparator = syscall.NewCallback(func(he1 unsafe.Pointer, he2 unsafe.Pointer, arg uintptr) int {
if he1 == nil || he2 == nil || arg == 0 {
return 0
}
cmp := *(*func(*Element, *Element) int)(unsafe.Pointer(arg))
return cmp(NewElementFromHandle(HELEMENT(he1)), NewElementFromHandle(HELEMENT(he2)))
})
func ProcNoDefault(hwnd, msg uint32, wparam, lparam uintptr) (uintptr, bool) {
var handled int32 = 0
result := HTMLayoutProcND(uintptr(hwnd), msg, wparam, lparam, &handled)
return uintptr(result), handled != 0
}
// Load html contents into window
func LoadHtml(hwnd uint32, data []byte, baseUrl string) error {
if len(data) > 0 {
baseUrlPtr := stringToUtf16Ptr(baseUrl)
if !HTMLayoutLoadHtmlEx(uintptr(hwnd), &data[0], uint32(len(data)), baseUrlPtr) {
return errors.New("HTMLayoutLoadHtmlEx failed")
}
}
return nil
}
// Load resource (file or url) into window
func LoadResource(hwnd uint32, uri string) error {
uriPtr := stringToUtf16Ptr(uri)
if !HTMLayoutLoadFile(uintptr(hwnd), uriPtr) {
return errors.New("HTMLayoutLoadFile failed")
}
return nil
}
func SetOption(hwnd uint32, option uint, value uint) bool {
return HTMLayoutSetOption(uintptr(hwnd), uint32(option), uint32(value))
}
const (
HTMLAYOUT_SMOOTH_SCROLL = 1
HTMLAYOUT_CONNECTION_TIMEOUT = 2
HTMLAYOUT_HTTPS_ERROR = 3
HTMLAYOUT_FONT_SMOOTHING = 4
HTMLAYOUT_ANIMATION_THREAD = 5
HTMLAYOUT_TRANSPARENT_WINDOW = 6
)
func DataReady(hwnd uint32, uri *uint16, data []byte) bool {
return HTMLayoutDataReady(uintptr(hwnd), uri, &data[0], uint32(len(data)))
}
func AttachWindowEventHandler(hwnd uint32, handler *EventHandler) {
key := uintptr(hwnd)
if oldHandle, exists := windowEventHandles[hwnd]; exists {
oldTag := uintptr(oldHandle)
HTMLayoutWindowDetachEventHandler(key, uintptr(goElementProc), oldTag)
delete(windowEventHandlers, hwnd)
delete(windowEventHandles, hwnd)
oldHandle.Delete()
log.Printf("[AttachWindowEventHandler] hwnd=%d old handler detached", hwnd)
}
handle := cgo.NewHandle(handler)
tag := uintptr(handle)
windowEventHandlers[hwnd] = handler
windowEventHandles[hwnd] = handle
subscription := handler.AllSubscription()
subscription &= ^uint32(DISABLE_INITIALIZATION & 0xffffffff)
ret := HTMLayoutWindowAttachEventHandler(key, uintptr(goElementProc), tag, subscription)
if ret != HLDOM_OK {
domPanic(ret, "Failed to attach event handler to window")
}
log.Printf("[AttachWindowEventHandler] hwnd=%d attached successfully, handle=%d", hwnd, handle)
}
func DetachWindowEventHandler(hwnd uint32) {
key := uintptr(hwnd)
handle, exists := windowEventHandles[hwnd]
if !exists {
return
}
tag := uintptr(handle)
delete(windowEventHandlers, hwnd)
delete(windowEventHandles, hwnd)
HTMLayoutWindowDetachEventHandler(key, uintptr(goElementProc), tag)
func() {
defer func() {
recover()
}()
handle.Delete()
}()
}
func AttachNotifyHandler(hwnd uint32, handler *NotifyHandler) {
key := uintptr(hwnd)
notifyHandlers[key] = handler
HTMLayoutSetCallback(key, uintptr(goNotifyProc), key)
}
func DetachNotifyHandler(hwnd uint32) {
key := uintptr(hwnd)
if _, exists := notifyHandlers[key]; exists {
HTMLayoutSetCallback(key, 0, 0)
delete(notifyHandlers, key)
}
}
func DumpObjectCounts() {
log.Print("Window notify handlers (", len(notifyHandlers), "): ", notifyHandlers)
log.Print("Window event handlers (", len(windowEventHandlers), "): ", windowEventHandlers)
}