energy/cef/types-frame-handler.go

126 lines
3.4 KiB
Go
Raw Normal View History

2023-05-31 17:41:14 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package cef
import (
"github.com/energye/energy/v2/cef/internal/def"
2023-05-31 18:00:34 +08:00
"github.com/energye/energy/v2/common/imports"
2023-05-31 17:41:14 +08:00
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/api"
"unsafe"
)
// ************************** creates ************************** //
// FrameHandlerRef -> ICefFrameHandler
var FrameHandlerRef frameHandler
type frameHandler uintptr
func (*frameHandler) New() *ICefFrameHandler {
var result uintptr
imports.Proc(def.CefFrameHandlerRef_Create).Call(uintptr(unsafe.Pointer(&result)))
2023-05-31 17:41:14 +08:00
if result != 0 {
return &ICefFrameHandler{instance: unsafe.Pointer(result)}
}
return nil
}
// ************************** impl ************************** //
// Instance 实例
func (m *ICefFrameHandler) Instance() uintptr {
if m == nil {
return 0
}
return uintptr(m.instance)
}
func (m *ICefFrameHandler) Free() {
if m.instance != nil {
m.base.Free(m.Instance())
m.instance = nil
}
}
func (m *ICefFrameHandler) IsValid() bool {
if m == nil || m.instance == nil {
return false
}
return m.instance != nil
}
func (m *ICefFrameHandler) SetOnFrameCreated(fn onFrameCreated) {
if !m.IsValid() {
return
}
imports.Proc(def.CefFrameHandler_OnFrameCreated).Call(m.Instance(), api.MakeEventDataPtr(fn))
2023-05-31 17:41:14 +08:00
}
func (m *ICefFrameHandler) SetOnFrameAttached(fn onFrameAttached) {
if !m.IsValid() {
return
}
imports.Proc(def.CefFrameHandler_OnFrameAttached).Call(m.Instance(), api.MakeEventDataPtr(fn))
2023-05-31 17:41:14 +08:00
}
func (m *ICefFrameHandler) SetOnFrameDetached(fn onFrameDetached) {
if !m.IsValid() {
return
}
imports.Proc(def.CefFrameHandler_OnFrameDetached).Call(m.Instance(), api.MakeEventDataPtr(fn))
2023-05-31 17:41:14 +08:00
}
func (m *ICefFrameHandler) SetOnMainFrameChanged(fn onMainFrameChanged) {
if !m.IsValid() {
return
}
imports.Proc(def.CefFrameHandler_OnMainFrameChanged).Call(m.Instance(), api.MakeEventDataPtr(fn))
2023-05-31 17:41:14 +08:00
}
// ************************** events ************************** //
type onFrameCreated func(browser *ICefBrowser, frame *ICefFrame)
type onFrameAttached func(browser *ICefBrowser, frame *ICefFrame, reattached bool)
type onFrameDetached func(browser *ICefBrowser, frame *ICefFrame)
type onMainFrameChanged func(browser *ICefBrowser, oldFrame, newFrame *ICefFrame)
func init() {
lcl.RegisterExtEventCallback(func(fn interface{}, getVal func(idx int) uintptr) bool {
getPtr := func(i int) unsafe.Pointer {
return unsafe.Pointer(getVal(i))
}
switch fn.(type) {
case onFrameCreated:
browse := &ICefBrowser{instance: getPtr(0)}
frame := &ICefFrame{instance: getPtr(1)}
fn.(onFrameCreated)(browse, frame)
case onFrameAttached:
browse := &ICefBrowser{instance: getPtr(0)}
frame := &ICefFrame{instance: getPtr(1)}
reattached := api.GoBool(getVal(2))
fn.(onFrameAttached)(browse, frame, reattached)
case onFrameDetached:
browse := &ICefBrowser{instance: getPtr(0)}
frame := &ICefFrame{instance: getPtr(1)}
fn.(onFrameDetached)(browse, frame)
case onMainFrameChanged:
browse := &ICefBrowser{instance: getPtr(0)}
oldFrame := &ICefFrame{instance: getPtr(1)}
newFrame := &ICefFrame{instance: getPtr(2)}
fn.(onMainFrameChanged)(browse, oldFrame, newFrame)
default:
return false
}
return true
})
}