energy/cef/cef-browser.go

334 lines
10 KiB
Go
Raw Normal View History

2022-10-04 13:21:05 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
2022-10-04 16:38:43 +08:00
// Licensed under GNU General Public License v3.0
2022-10-04 13:21:05 +08:00
//
//----------------------------------------
package cef
import (
. "github.com/energye/energy/common"
2022-10-04 22:34:57 +08:00
. "github.com/energye/energy/consts"
2022-10-04 13:21:05 +08:00
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/api"
"github.com/energye/golcl/lcl/types"
"unsafe"
)
//type ICefBrowser
type ICefBrowser struct {
chromium unsafe.Pointer
2022-10-04 13:21:05 +08:00
browseId int32
}
type frameNamesPtr struct {
Name uintptr
Value uintptr
}
type FrameNames struct {
Name string
Value string
}
func (m *ICefBrowser) GetBrowserId() int32 {
return m.browseId
}
func (m *ICefBrowser) GetFrameId() int64 {
if mainFrame := m.MainFrame(); mainFrame != nil {
return mainFrame.Id
}
return 0
}
//浏览器ID号
2022-10-04 13:21:05 +08:00
func (m *ICefBrowser) Identifier() int32 {
return m.browseId
}
//HostWindowHandle
2022-10-04 13:21:05 +08:00
func (m *ICefBrowser) HostWindowHandle() types.HWND {
r1, _, _ := Proc(internale_CEFBrowser_GetHostWindowHandle).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
return r1
}
//CloseBrowser
func (m *ICefBrowser) CloseBrowser(forceClose bool) {
Proc(internale_CEFBrowser_CloseBrowser).Call(uintptr(m.browseId), api.PascalBool(forceClose))
2022-10-04 13:21:05 +08:00
}
//TryCloseBrowser
func (m *ICefBrowser) TryCloseBrowser() bool {
r1, _, _ := Proc(internale_CEFBrowser_TryCloseBrowser).Call(uintptr(m.browseId))
2022-12-03 21:56:51 +08:00
return api.GoBool(r1)
2022-10-04 13:21:05 +08:00
}
//SetFocus
func (m *ICefBrowser) SetFocus(focus bool) {
Proc(internale_CEFBrowser_SetFocus).Call(uintptr(m.browseId), api.PascalBool(focus))
2022-10-04 13:21:05 +08:00
}
//GetZoomLevel
func (m *ICefBrowser) GetZoomLevel() (result float64) {
Proc(internale_CEFBrowser_GetZoomLevel).Call(uintptr(m.browseId), uintptr(unsafe.Pointer(&result)))
return result
2022-10-04 13:21:05 +08:00
}
//SetZoomLevel
func (m *ICefBrowser) SetZoomLevel(zoomLevel float64) {
Proc(internale_CEFBrowser_SetZoomLevel).Call(uintptr(m.browseId), uintptr(unsafe.Pointer(&zoomLevel)))
2022-10-04 13:21:05 +08:00
}
//RunFileDialog
func (m *ICefBrowser) RunFileDialog(mode int32, title, defaultFilePath string, acceptFilters *lcl.TStrings) {
Proc(internale_CEFBrowser_RunFileDialog).Call(uintptr(m.browseId), uintptr(mode), api.PascalStr(title), api.PascalStr(defaultFilePath), acceptFilters.Instance())
2022-10-04 13:21:05 +08:00
}
//StartDownload
func (m *ICefBrowser) StartDownload(url string) {
Proc(internale_CEFBrowser_StartDownload).Call(uintptr(m.browseId), api.PascalStr(url))
2022-10-04 13:21:05 +08:00
}
//DownloadImage
func (m *ICefBrowser) DownloadImage(imageUrl string, isFavicon bool, maxImageSize int32, bypassCache bool) {
Proc(internale_CEFBrowser_DownloadImage).Call(uintptr(m.browseId), api.PascalStr(imageUrl), api.PascalBool(isFavicon), uintptr(maxImageSize), api.PascalBool(bypassCache))
2022-10-04 13:21:05 +08:00
}
//Print
func (m *ICefBrowser) Print() {
Proc(internale_CEFBrowser_Print).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) GetFocusedFrame() *ICefFrame {
r1, _, _ := Proc(internale_CEFBrowser_GetFocusedFrame).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
cf := (*cefFrame)(unsafe.Pointer(r1))
if cf != nil {
ret := &ICefFrame{}
ret.Browser = m
2022-12-03 21:56:51 +08:00
ret.Id = StrToInt64(api.GoStr(cf.Identifier))
ret.Name = api.GoStr(cf.Name)
ret.Url = api.GoStr(cf.Url)
2022-10-04 13:21:05 +08:00
return ret
}
return nil
}
func (m *ICefBrowser) MainFrame() *ICefFrame {
r1, _, _ := Proc(internale_CEFBrowser_GetMainFrame).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
cf := (*cefFrame)(unsafe.Pointer(r1))
if cf != nil {
ret := &ICefFrame{}
ret.Browser = m
2022-12-03 21:56:51 +08:00
ret.Id = StrToInt64(api.GoStr(cf.Identifier))
ret.Name = api.GoStr(cf.Name)
ret.Url = api.GoStr(cf.Url)
2022-10-04 13:21:05 +08:00
return ret
}
return nil
}
func (m *ICefBrowser) GetFrameById(frameId int64) *ICefFrame {
r1, _, _ := Proc(internale_CEFBrowser_GetFrameById).Call(uintptr(m.browseId), uintptr(unsafe.Pointer(&frameId)))
2022-10-04 13:21:05 +08:00
cf := (*cefFrame)(unsafe.Pointer(r1))
if cf != nil {
ret := &ICefFrame{}
ret.Browser = m
2022-12-03 21:56:51 +08:00
ret.Id = StrToInt64(api.GoStr(cf.Identifier))
ret.Name = api.GoStr(cf.Name)
ret.Url = api.GoStr(cf.Url)
2022-10-04 13:21:05 +08:00
cf = nil
return ret
}
return nil
}
func (m *ICefBrowser) GetFrameByName(frameName string) *ICefFrame {
r1, _, _ := Proc(internale_CEFBrowser_GetFrameByName).Call(uintptr(m.browseId), api.PascalStr(frameName))
2022-10-04 13:21:05 +08:00
cf := (*cefFrame)(unsafe.Pointer(r1))
if cf != nil {
ret := &ICefFrame{}
ret.Browser = m
2022-12-03 21:56:51 +08:00
ret.Id = StrToInt64(api.GoStr(cf.Identifier))
ret.Name = api.GoStr(cf.Name)
ret.Url = api.GoStr(cf.Url)
2022-10-04 13:21:05 +08:00
cf = nil
return ret
}
return nil
}
//PrintToPdf
//func (m *ICefBrowser) PrintToPdf(path string) {
// Proc(internale_CEFBrowser_PrintToPdf).Call(uintptr(m.browseId), api.PascalStr(path))
2022-10-04 13:21:05 +08:00
//}
//ExecuteDevToolsMethod
func (m *ICefBrowser) ExecuteDevToolsMethod(messageId int32, method string, dictionaryValue *ICefDictionaryValue) {
var data = []byte{}
var dataPtr unsafe.Pointer
var dataLen int = 0
var argsLen int = 0
if dictionaryValue != nil && dictionaryValue.dataLen > 0 {
defer func() {
dictionaryValue.Clear()
dictionaryValue = nil
data = nil
dataPtr = nil
}()
data = dictionaryValue.Package()
argsLen = dictionaryValue.dataLen
dataPtr = unsafe.Pointer(&data[0])
dataLen = len(data) - 1
} else {
dataPtr = unsafe.Pointer(&data)
}
Proc(internale_CEFBrowser_ExecuteDevToolsMethod).Call(uintptr(m.browseId), uintptr(messageId), api.PascalStr(method), uintptr(argsLen), uintptr(dataPtr), uintptr(dataLen))
2022-10-04 13:21:05 +08:00
}
//SendKeyEvent
func (m *ICefBrowser) SendKeyEvent(event *TCefKeyEvent) {
Proc(internale_CEFBrowser_SendKeyEvent).Call(uintptr(m.browseId), uintptr(unsafe.Pointer(event)))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) SetAudioMuted(mute bool) {
Proc(internale_CEFBrowser_SetAudioMuted).Call(uintptr(m.browseId), api.PascalBool(mute))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) IsAudioMuted() bool {
r1, _, _ := Proc(internale_CEFBrowser_IsAudioMuted).Call(uintptr(m.browseId))
2022-12-03 21:56:51 +08:00
return api.GoBool(r1)
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) SetAutoResizeEnabled(enabled bool, minSize, maxSize *TCefSize) {
Proc(internale_CEFBrowser_SetAutoResizeEnabled).Call(uintptr(m.browseId), api.PascalBool(enabled), uintptr(unsafe.Pointer(minSize)), uintptr(unsafe.Pointer(maxSize)))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) SetAccessibilityState(accessibilityState TCefState) {
Proc(internale_CEFBrowser_SetAccessibilityState).Call(uintptr(m.browseId), uintptr(accessibilityState))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) NotifyMoveOrResizeStarted() {
Proc(internale_CEFBrowser_NotifyMoveOrResizeStarted).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) NotifyScreenInfoChanged() {
Proc(internale_CEFBrowser_NotifyScreenInfoChanged).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) SendCaptureLostEvent() {
Proc(internale_CEFBrowser_SendCaptureLostEvent).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) SendTouchEvent(event *TCefTouchEvent) {
Proc(internale_CEFBrowser_SendTouchEvent).Call(uintptr(m.browseId), uintptr(unsafe.Pointer(event)))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) SendMouseWheelEvent(event *TCefMouseEvent, deltaX, deltaY int32) {
Proc(internale_CEFBrowser_SendMouseWheelEvent).Call(uintptr(m.browseId), uintptr(unsafe.Pointer(event)), uintptr(deltaX), uintptr(deltaY))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) SendMouseMoveEvent(event *TCefMouseEvent, mouseLeave bool) {
Proc(internale_CEFBrowser_SendMouseMoveEvent).Call(uintptr(m.browseId), uintptr(unsafe.Pointer(event)), api.PascalBool(mouseLeave))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) SendMouseClickEvent(event *TCefMouseEvent, type_ TCefMouseButtonType, mouseUp bool, clickCount int32) {
Proc(internale_CEFBrowser_SendMouseClickEvent).Call(uintptr(m.browseId), uintptr(unsafe.Pointer(event)), uintptr(type_), api.PascalBool(mouseUp), uintptr(clickCount))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) ViewSource(frame *ICefFrame) {
m.createBrowserViewSource(frame)
2022-10-04 13:21:05 +08:00
}
//显示开发者工具
func (m *ICefBrowser) ShowDevTools() {
if browserWinInfo := BrowserWindow.GetWindowInfo(m.Identifier()); browserWinInfo != nil {
m.createBrowserDevTools(browserWinInfo)
2022-10-04 13:21:05 +08:00
}
}
//关闭开发者工具
func (m *ICefBrowser) CloseDevTools() {
Proc(internale_CEFBrowser_CloseDevTools).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) HasDevTools() bool {
r1, _, _ := Proc(internale_CEFBrowser_HasDevTools).Call(uintptr(m.browseId))
2022-12-03 21:56:51 +08:00
return api.GoBool(r1)
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) CanGoBack() bool {
r1, _, _ := Proc(internale_CEFBrowser_CanGoBack).Call(uintptr(m.browseId))
2022-12-03 21:56:51 +08:00
return api.GoBool(r1)
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) GoBack() {
if m.CanGoBack() {
Proc(internale_CEFBrowser_GoBack).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
}
func (m *ICefBrowser) CanGoForward() bool {
r1, _, _ := Proc(internale_CEFBrowser_CanGoForward).Call(uintptr(m.browseId))
2022-12-03 21:56:51 +08:00
return api.GoBool(r1)
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) GoForward() {
if m.CanGoForward() {
Proc(internale_CEFBrowser_GoForward).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
}
func (m *ICefBrowser) IsLoading() bool {
r1, _, _ := Proc(internale_CEFBrowser_IsLoading).Call(uintptr(m.browseId))
2022-12-03 21:56:51 +08:00
return api.GoBool(r1)
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) Reload() {
Proc(internale_CEFBrowser_Reload).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) ReloadIgnoreCache() {
Proc(internale_CEFBrowser_ReloadIgnoreCache).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) StopLoad() {
Proc(internale_CEFBrowser_StopLoad).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) FrameCount() int {
r1, _, _ := Proc(internale_CEFBrowser_FrameCount).Call(uintptr(m.browseId))
2022-10-04 13:21:05 +08:00
return int(r1)
}
func (m *ICefBrowser) GetFrameNames() []*FrameNames {
var result uintptr
var resultSize int32
Proc(internale_CEFBrowser_GetFrameNames).Call(uintptr(m.browseId), uintptr(unsafe.Pointer(&result)), uintptr(unsafe.Pointer(&resultSize)))
2022-10-04 13:21:05 +08:00
frameNames := make([]*FrameNames, resultSize, resultSize)
for i := 0; i < int(resultSize); i++ {
2022-10-04 22:34:57 +08:00
fnsPtr := (*frameNamesPtr)(unsafe.Pointer(GetParamOf(i, result)))
2022-10-04 13:21:05 +08:00
frameNames[i] = &FrameNames{
2022-12-03 21:56:51 +08:00
Name: api.GoStr(fnsPtr.Name),
Value: api.GoStr(fnsPtr.Value),
2022-10-04 13:21:05 +08:00
}
}
result = 0
resultSize = 0
return frameNames
}
func (m *ICefBrowser) Find(searchText string, forward, matchCase, findNext bool) {
Proc(internale_CEFBrowser_Find).Call(uintptr(m.browseId), api.PascalStr(searchText), api.PascalBool(forward), api.PascalBool(matchCase), api.PascalBool(findNext))
2022-10-04 13:21:05 +08:00
}
func (m *ICefBrowser) StopFinding(clearSelection bool) {
Proc(internale_CEFBrowser_StopFinding).Call(uintptr(m.browseId), api.PascalBool(clearSelection))
2022-10-04 13:21:05 +08:00
}
2022-12-05 10:44:24 +08:00
// ICefBrowser _CEFBrowser_ShowDevTools
func _CEFBrowser_ShowDevTools(chromium, browser, windowParent, name uintptr) {
Proc(internale_CEFBrowser_ShowDevTools).Call(chromium, browser, windowParent, name)
2022-12-05 10:44:24 +08:00
}