energy/example/dev-test/vf-browser/vf-browser.go

93 lines
3.8 KiB
Go
Raw Normal View History

2023-01-25 09:31:07 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under GNU General Public License v3.0
//
//----------------------------------------
2022-12-30 18:22:21 +08:00
package main
import (
"embed"
"fmt"
2022-12-30 18:22:21 +08:00
"github.com/energye/energy/cef"
"github.com/energye/energy/common/assetserve"
2023-01-28 12:52:36 +08:00
"github.com/energye/energy/example/dev-test/traydemo"
"github.com/energye/golcl/lcl"
2022-12-30 18:22:21 +08:00
)
//go:embed resources
var resources embed.FS
2022-12-30 18:22:21 +08:00
func main() {
//全局初始化 每个应用都必须调用的
cef.GlobalInit(nil, &resources)
2022-12-30 18:22:21 +08:00
//创建应用
config := cef.NewApplicationConfig()
config.SetMultiThreadedMessageLoop(false)
config.SetExternalMessagePump(false)
config.SetRemoteDebuggingPort(33333)
cefApp := cef.NewApplication(config)
2022-12-30 18:22:21 +08:00
//指定一个URL地址或本地html文件目录
cef.BrowserWindow.Config.Url = "http://localhost:22022/index.html"
cef.BrowserWindow.Config.IconFS = "resources/icon.png"
cef.BrowserWindow.Config.EnableWebkitAppRegion = true
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
//window.DisableResize()
2023-01-28 11:56:24 +08:00
window.SetCenterWindow(true)
window.SetTitle("这里改变了窗口标题")
2023-01-28 11:46:09 +08:00
window.SetSize(1024, 900)
fmt.Println("cef.BrowserWindow.SetViewFrameBrowserInit", window)
fmt.Println("LCL", window.AsLCLBrowserWindow(), "VF", window.AsViewsFrameworkBrowserWindow())
event.SetOnDraggableRegionsChanged(func(sender lcl.IObject, browser *cef.ICefBrowser, frame *cef.ICefFrame, regions *cef.TCefDraggableRegions) {
fmt.Println("SetOnDraggableRegionsChanged", regions.RegionsCount(), "frame:", frame.Id, frame.Url)
})
event.SetOnBeforePopup(func(sender lcl.IObject, browser *cef.ICefBrowser, frame *cef.ICefFrame, beforePopupInfo *cef.BeforePopupInfo, popupWindow cef.IBrowserWindow, noJavascriptAccess *bool) bool {
fmt.Println("IsViewsFramework:", popupWindow.IsViewsFramework())
popupWindow.SetTitle("修改了标题: " + beforePopupInfo.TargetUrl)
popupWindow.EnableResize()
2023-01-28 11:46:09 +08:00
popupWindow.DisableMaximize()
popupWindow.DisableResize()
popupWindow.DisableMinimize()
2023-01-28 11:56:24 +08:00
popupWindow.SetSize(1000, 1000)
browserWindow := popupWindow.AsViewsFrameworkBrowserWindow()
browserWindow.SetOnWindowCreated(func(sender lcl.IObject, window *cef.ICefWindow) {
fmt.Println("popupWindow.SetOnWindowCreated", window)
})
2023-01-28 11:46:09 +08:00
browserWindow.SetOnGetInitialBounds(func(sender lcl.IObject, window *cef.ICefWindow, aResult *cef.TCefRect) {
fmt.Println("popupWindow.SetOnGetInitialBounds", *aResult)
})
//browserWindow.BrowserWindow().CreateTopLevelWindow()
//browserWindow.BrowserWindow().HideTitle()
fmt.Println("browserWindow:", browserWindow, browserWindow.WindowComponent().WindowHandle())
return false
})
window.AsViewsFrameworkBrowserWindow().WindowComponent().SetOnWindowActivationChanged(func(sender lcl.IObject, window *cef.ICefWindow, active bool) {
fmt.Println("SetOnWindowActivationChanged", active)
})
window.Show()
fmt.Println("SetBrowserInit 结束")
})
cef.BrowserWindow.SetBrowserInitAfter(func(window cef.IBrowserWindow) {
2023-01-28 12:52:36 +08:00
//traydemo.SysTrayDemo(window)
//traydemo.LCLTrayDemo(window) //VF窗口组件中无法创建或使用LCL组件
traydemo.LCLCefTrayDemo(window)
println("browser init after end")
})
//在主进程启动成功之后执行
//在这里启动内置http服务
//内置http服务需要使用 go:embed resources 内置资源到执行程序中
cef.SetBrowserProcessStartAfterCallback(func(b bool) {
fmt.Println("主进程启动 创建一个内置http服务")
//通过内置http服务加载资源
server := assetserve.NewAssetsHttpServer()
server.PORT = 22022 //服务端口号
server.AssetsFSName = "resources" //必须设置目录名和资源文件夹同名
server.Assets = &resources
go server.StartHttpServer()
})
2022-12-30 18:22:21 +08:00
//运行应用
cef.Run(cefApp)
}