energy/cef/browser-main-window.go

468 lines
13 KiB
Go
Raw Normal View History

//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
2023-05-31 17:41:14 +08:00
// energy 全局窗口管理, 窗口初始化、事件注册、属性配置
package cef
import (
2023-05-31 18:00:34 +08:00
"github.com/energye/energy/v2/cef/internal/ipc"
. "github.com/energye/energy/v2/cef/process"
. "github.com/energye/energy/v2/consts"
"github.com/energye/golcl/lcl"
)
// 浏览器包装结构体
2023-03-04 11:19:45 +08:00
type browserWindow struct {
mainBrowserWindow *lclBrowserWindow //LCL 主浏览器窗口
mainVFBrowserWindow *ViewsFrameworkBrowserWindow //Views Frameworks 主浏览器窗口
popupWindow IBrowserWindow //弹出的子窗口
browserEvent *BrowserEvent //浏览器全局事件, 已默认实现事件
Config *browserConfig //浏览器和窗口配置
windowInfo map[int32]IBrowserWindow //窗口信息集合
}
2023-05-31 17:41:14 +08:00
// BrowserEvent 浏览器全局事件监听-已被默认实现事件
// 该结构中的对象属性, 是已被默认实现的
type BrowserEvent struct {
chromium IChromiumEvent //chromium event
onBeforePopup chromiumEventOnBeforePopupEx //default
onDragEnter chromiumEventOnDragEnterEx //default
onDraggableRegionsChanged chromiumEventOnDraggableRegionsChangedEx //default
onLoadEnd chromiumEventOnLoadEnd //default
onAfterCreated chromiumEventOnAfterCreatedEx //default
onBeforeBrowser chromiumEventOnBeforeBrowserEx //default
onBeforeClose chromiumEventOnBeforeCloseEx //default
onClose chromiumEventOnCloseEx //default
onMainFrameChanged chromiumEventOnMainFrameChangedEx //default
onBeforeDownload chromiumEventOnBeforeDownload //default can cover
onKeyEvent chromiumEventOnKeyEventEx //default can cover
onProcessMessageReceived BrowseProcessMessageReceived //default
onTitleChange chromiumEventOnTitleChangeEx //default
onContextMenuCommand chromiumEventOnContextMenuCommand //default can cover
onBeforeContextMenu chromiumEventOnBeforeContextMenuEx //default can cover
onBeforeResourceLoad chromiumEventOnBeforeResourceLoad //default
onRenderCompMsg chromiumEventOnCompMsg //default windows
}
// LCLBrowserWindow
2023-03-04 11:19:45 +08:00
type lclBrowserWindow struct {
LCLBrowserWindow
}
// OnFormCreate LCL窗口组件窗口创建回调
2023-03-04 11:19:45 +08:00
func (m *lclBrowserWindow) OnFormCreate(sender lcl.IObject) {
m.windowProperty = &BrowserWindow.Config.WindowProperty
m.SetWindowType(WT_MAIN_BROWSER)
m.FormCreate()
m.defaultWindowEvent()
m.defaultWindowCloseEvent()
m.ChromiumCreate(BrowserWindow.Config.ChromiumConfig(), BrowserWindow.Config.Url)
m.defaultChromiumEvent()
2023-05-31 17:41:14 +08:00
m.setProperty()
if BrowserWindow.Config.browserWindowOnEventCallback != nil {
2023-05-31 17:41:14 +08:00
BrowserWindow.browserEvent.chromium = m.Chromium()
2023-07-07 15:27:26 +08:00
BrowserWindow.Config.browserWindowOnEventCallback(BrowserWindow.browserEvent, m)
2023-05-31 17:41:14 +08:00
m.setProperty() //再次设置可能修改属性
}
//browserWindowOnEventCallback 执行完后注册CompMsgEvent
m.registerWindowsCompMsgEvent()
2023-05-31 17:41:14 +08:00
//自定义窗口标题栏
m.cwcap = &customWindowCaption{
bw: &m.LCLBrowserWindow,
}
2023-05-31 17:41:14 +08:00
//设置 CEF Chromium IPC
ipc.SetProcessMessage(m.Chromium().(*TCEFChromium))
2023-06-14 17:30:45 +08:00
// 如果开启了开发者工具,需要在这里初始化开发者工具窗口
2023-06-19 12:41:52 +08:00
if m.Chromium().Config().EnableDevTools() {
2023-06-14 17:30:45 +08:00
m.createAuxTools()
2023-06-15 18:29:23 +08:00
m.GetAuxTools().SetDevTools(createDevtoolsWindow(&m.LCLBrowserWindow))
2023-06-14 17:30:45 +08:00
}
}
2023-05-31 17:41:14 +08:00
// MainWindow 获取主浏窗口
// 返回LCL或VF窗口组件实例
// Window和MacOS平台LCL窗口组件
// Linux平台VF窗口组件
2023-03-04 11:19:45 +08:00
func (m *browserWindow) MainWindow() IBrowserWindow {
if m.mainVFBrowserWindow != nil {
return m.mainVFBrowserWindow
} else if m.mainBrowserWindow != nil {
return m.mainBrowserWindow
}
return nil
}
2023-05-31 17:41:14 +08:00
// SetBrowserInit 主窗口初始化时回调
// LCL: 可以对主窗体属性设置、创建各种LCL子组件
// VF : 有很大限制不能使用LCL的组件
// event : 浏览器事件
// window : 窗口信息对象
2023-03-04 11:19:45 +08:00
func (m *browserWindow) SetBrowserInit(fn browserWindowOnEventCallback) {
m.Config.setBrowserWindowInitOnEvent(fn)
}
2023-05-31 17:41:14 +08:00
// createNextLCLPopupWindow 预创建下一个弹出的子窗口
func (m *browserWindow) createNextLCLPopupWindow() {
if m.popupWindow == nil {
2023-07-20 10:00:08 +08:00
if mw := m.MainWindow(); mw != nil {
m.popupWindow = NewLCLWindow(m.Config.WindowProperty, mw.AsLCLBrowserWindow().BrowserWindow())
}
}
}
2023-05-31 17:41:14 +08:00
// getNextLCLPopupWindow 拿到预创建的下一个弹出的子窗口
func (m *browserWindow) getNextLCLPopupWindow() IBrowserWindow {
bw := m.popupWindow
m.popupWindow = nil
return bw
}
// GetWindowInfo 根据浏览器窗口ID获取窗口信息
2023-03-04 11:19:45 +08:00
func (m *browserWindow) GetWindowInfo(browserId int32) IBrowserWindow {
if winInfo, ok := m.windowInfo[browserId]; ok {
return winInfo
}
return nil
}
// GetWindowInfos 获得所有窗口信息
2023-03-04 11:19:45 +08:00
func (m *browserWindow) GetWindowInfos() map[int32]IBrowserWindow {
return m.windowInfo
}
// putWindowInfo 创建一个窗口这后会添加到windowInfo中
2023-03-04 11:19:45 +08:00
func (m *browserWindow) putWindowInfo(browserId int32, windowInfo IBrowserWindow) {
m.windowInfo[browserId] = windowInfo
}
// removeWindowInfo 窗口关闭会从windowInfo移除
2023-03-04 11:19:45 +08:00
func (m *browserWindow) removeWindowInfo(browseId int32) {
delete(m.windowInfo, browseId)
}
// GetBrowser 获取窗口Browser
2023-03-04 11:19:45 +08:00
func (m *browserWindow) GetBrowser(browseId int32) *ICefBrowser {
if winInfo, ok := m.windowInfo[browseId]; ok {
return winInfo.Browser()
}
return nil
}
// SetOnAfterCreated
func (m *BrowserEvent) SetOnAfterCreated(event chromiumEventOnAfterCreatedEx) {
if Args.IsMain() {
m.onAfterCreated = event
}
}
// SetOnBeforeBrowser
func (m *BrowserEvent) SetOnBeforeBrowser(event chromiumEventOnBeforeBrowserEx) {
if Args.IsMain() {
m.onBeforeBrowser = event
}
}
// SetOnAddressChange
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnAddressChange(event chromiumEventOnAddressChange) {
if Args.IsMain() {
m.chromium.SetOnAddressChange(event)
}
}
// SetOnBeforeClose
func (m *BrowserEvent) SetOnBeforeClose(event chromiumEventOnBeforeCloseEx) {
if Args.IsMain() {
m.onBeforeClose = event
}
}
// SetOnClose
func (m *BrowserEvent) SetOnClose(event chromiumEventOnCloseEx) {
if Args.IsMain() {
m.onClose = event
}
}
// SetOnPdfPrintFinished
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnPdfPrintFinished(event chromiumEventOnPdfPrintFinished) {
if Args.IsMain() {
m.chromium.SetOnPdfPrintFinished(event)
}
}
// SetOnZoomPctAvailable
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnZoomPctAvailable(event chromiumEventOnResultFloat) {
if Args.IsMain() {
m.chromium.SetOnZoomPctAvailable(event)
}
}
// SetOnLoadStart
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnLoadStart(event chromiumEventOnLoadStart) {
if Args.IsMain() {
m.chromium.SetOnLoadStart(event)
}
}
// SetOnLoadingStateChange
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnLoadingStateChange(event chromiumEventOnLoadingStateChange) {
if Args.IsMain() {
m.chromium.SetOnLoadingStateChange(event)
}
}
// SetOnLoadingProgressChange
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnLoadingProgressChange(event chromiumEventOnLoadingProgressChange) {
if Args.IsMain() {
m.chromium.SetOnLoadingProgressChange(event)
}
}
// SetOnLoadError
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnLoadError(event chromiumEventOnLoadError) {
if Args.IsMain() {
m.chromium.SetOnLoadError(event)
}
}
// SetOnLoadEnd
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnLoadEnd(event chromiumEventOnLoadEnd) {
if Args.IsMain() {
m.onLoadEnd = event
}
}
// SetOnBeforeDownload
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnBeforeDownload(event chromiumEventOnBeforeDownload) {
if Args.IsMain() {
m.onBeforeDownload = event
}
}
// SetOnDownloadUpdated
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnDownloadUpdated(event chromiumEventOnDownloadUpdated) {
if Args.IsMain() {
m.chromium.SetOnDownloadUpdated(event)
}
}
// SetOnFullScreenModeChange
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnFullScreenModeChange(event chromiumEventOnFullScreenModeChange) {
if Args.IsMain() {
m.chromium.SetOnFullScreenModeChange(event)
}
}
// SetOnKeyEvent
func (m *BrowserEvent) SetOnKeyEvent(event chromiumEventOnKeyEventEx) {
if Args.IsMain() {
m.onKeyEvent = event
}
}
// SetOnTitleChange
func (m *BrowserEvent) SetOnTitleChange(event chromiumEventOnTitleChangeEx) {
if Args.IsMain() {
m.onTitleChange = event
}
}
// SetOnRenderCompMsg windows
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnRenderCompMsg(event chromiumEventOnCompMsg) {
if Args.IsMain() {
m.onRenderCompMsg = event
}
}
2023-06-02 12:53:38 +08:00
func (m *BrowserEvent) ChromiumEvent() IChromiumEvent {
return m.chromium
}
// SetOnWidgetCompMsg windows
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnWidgetCompMsg(event chromiumEventOnCompMsg) {
if Args.IsMain() {
m.chromium.SetOnWidgetCompMsg(event)
}
}
// SetOnBrowserCompMsg windows
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnBrowserCompMsg(event chromiumEventOnCompMsg) {
if Args.IsMain() {
m.chromium.SetOnBrowserCompMsg(event)
}
}
// SetOnRenderProcessTerminated
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnRenderProcessTerminated(event chromiumEventOnRenderProcessTerminated) {
if Args.IsMain() {
m.chromium.SetOnRenderProcessTerminated(event)
}
}
// SetOnRenderViewReady
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnRenderViewReady(event chromiumEventOnRenderViewReady) {
if Args.IsMain() {
m.chromium.SetOnRenderViewReady(event)
}
}
// SetOnScrollOffsetChanged
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnScrollOffsetChanged(event chromiumEventOnScrollOffsetChanged) {
if Args.IsMain() {
m.chromium.SetOnScrollOffsetChanged(event)
}
}
// SetOnBrowseProcessMessageReceived
func (m *BrowserEvent) SetOnBrowseProcessMessageReceived(event BrowseProcessMessageReceived) {
if Args.IsMain() {
m.onProcessMessageReceived = event
}
}
// SetOnBeforeResourceLoad
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnBeforeResourceLoad(event chromiumEventOnBeforeResourceLoad) {
if Args.IsMain() {
m.onBeforeResourceLoad = event
}
}
// SetOnResourceResponse
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnResourceResponse(event chromiumEventOnResourceResponse) {
if Args.IsMain() {
m.chromium.SetOnResourceResponse(event)
}
}
// SetOnResourceRedirect
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnResourceRedirect(event chromiumEventOnResourceRedirect) {
if Args.IsMain() {
m.chromium.SetOnResourceRedirect(event)
}
}
// SetOnResourceLoadComplete
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnResourceLoadComplete(event chromiumEventOnResourceLoadComplete) {
if Args.IsMain() {
m.chromium.SetOnResourceLoadComplete(event)
}
}
// SetOnCookieSet
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnCookieSet(event chromiumEventOnCookieSet) {
if Args.IsMain() {
m.chromium.SetOnCookieSet(event)
}
}
// SetOnCookiesDeleted
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnCookiesDeleted(event chromiumEventOnCookiesDeleted) {
if Args.IsMain() {
m.chromium.SetOnCookiesDeleted(event)
}
}
// SetOnCookiesFlushed
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnCookiesFlushed(event chromiumEventOnCookiesFlushed) {
if Args.IsMain() {
m.chromium.SetOnCookiesFlushed(event)
}
}
// SetOnCookiesVisited
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnCookiesVisited(event chromiumEventOnCookiesVisited) {
if Args.IsMain() {
m.chromium.SetOnCookiesVisited(event)
}
}
// SetOnCookieVisitorDestroyed
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnCookieVisitorDestroyed(event chromiumEventOnCookieVisitorDestroyed) {
if Args.IsMain() {
m.chromium.SetOnCookieVisitorDestroyed(event)
}
}
// SetOnBeforeContextMenu
func (m *BrowserEvent) SetOnBeforeContextMenu(event chromiumEventOnBeforeContextMenuEx) {
if Args.IsMain() {
m.onBeforeContextMenu = event
}
}
// SetOnContextMenuCommand
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnContextMenuCommand(event chromiumEventOnContextMenuCommand) {
if Args.IsMain() {
m.onContextMenuCommand = event
}
}
// SetOnContextMenuDismissed
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnContextMenuDismissed(event chromiumEventOnContextMenuDismissed) {
if Args.IsMain() {
m.chromium.SetOnContextMenuDismissed(event)
}
}
// SetOnFrameAttached
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnFrameAttached(event chromiumEventOnFrameAttached) {
if Args.IsMain() {
m.chromium.SetOnFrameAttached(event)
}
}
// SetOnMainFrameChanged
func (m *BrowserEvent) SetOnMainFrameChanged(event chromiumEventOnMainFrameChangedEx) {
if Args.IsMain() {
m.onMainFrameChanged = event
}
}
// SetOnBeforePopup
// 弹出窗口, 已被默认实现的函数
// 函数返回值
// false: 窗口会以默认行为管理
// true: 需要你自己管理窗口行为
func (m *BrowserEvent) SetOnBeforePopup(event chromiumEventOnBeforePopupEx) {
if Args.IsMain() {
m.onBeforePopup = event
}
}
// SetOnOpenUrlFromTab
2023-05-31 17:41:14 +08:00
//func (m *BrowserEvent) SetOnOpenUrlFromTab(event chromiumEventOnOpenUrlFromTab) {
// if Args.IsMain() {
// m.chromium.SetOnOpenUrlFromTab(event)
// }
//}
// SetOnFindResult
2023-05-31 17:41:14 +08:00
func (m *BrowserEvent) SetOnFindResult(event chromiumEventOnFindResult) {
if Args.IsMain() {
m.chromium.SetOnFindResult(event)
}
}
// SetOnDragEnter
func (m *BrowserEvent) SetOnDragEnter(event chromiumEventOnDragEnterEx) {
if Args.IsMain() {
m.onDragEnter = event
}
}
// SetOnDraggableRegionsChanged
func (m *BrowserEvent) SetOnDraggableRegionsChanged(event chromiumEventOnDraggableRegionsChangedEx) {
if Args.IsMain() {
m.onDraggableRegionsChanged = event
}
}