energy/cef/types-command-handler.go

91 lines
2.2 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"
"github.com/energye/energy/v2/consts"
2023-05-31 17:41:14 +08:00
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/api"
"unsafe"
)
// ************************** creates ************************** //
// CommandHandlerRef -> ICefCommandHandler
var CommandHandlerRef commandHandler
type commandHandler uintptr
func (*commandHandler) New() *ICefCommandHandler {
var result uintptr
imports.Proc(def.CefCommandHandlerRef_Create).Call(uintptr(unsafe.Pointer(&result)))
2023-05-31 17:41:14 +08:00
if result != 0 {
return &ICefCommandHandler{instance: unsafe.Pointer(result)}
}
return nil
}
// ************************** impl ************************** //
// Instance 实例
func (m *ICefCommandHandler) Instance() uintptr {
if m == nil {
return 0
}
return uintptr(m.instance)
}
func (m *ICefCommandHandler) Free() {
if m.instance != nil {
m.base.Free(m.Instance())
m.instance = nil
}
}
func (m *ICefCommandHandler) IsValid() bool {
if m == nil || m.instance == nil {
return false
}
return m.instance != nil
}
func (m *ICefCommandHandler) SetOnChromeCommand(fn onChromeCommand) {
if !m.IsValid() {
return
}
imports.Proc(def.CefCommandHandler_OnChromeCommand).Call(m.Instance(), api.MakeEventDataPtr(fn))
2023-05-31 17:41:14 +08:00
}
// ************************** events ************************** //
type onChromeCommand func(browser *ICefBrowser, commandId int32, disposition consts.TCefWindowOpenDisposition) bool
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 onChromeCommand:
browse := &ICefBrowser{instance: getPtr(0)}
params := (int32)(getVal(1))
disposition := consts.TCefWindowOpenDisposition(getVal(2))
result := (*bool)(getPtr(3))
*result = fn.(onChromeCommand)(browse, params, disposition)
default:
return false
}
return true
})
}