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 (
|
2022-12-31 13:16:30 +08:00
|
|
|
|
"embed"
|
2022-12-31 15:46:17 +08:00
|
|
|
|
"fmt"
|
2022-12-30 18:22:21 +08:00
|
|
|
|
"github.com/energye/energy/cef"
|
2022-12-31 15:46:17 +08:00
|
|
|
|
"github.com/energye/energy/common/assetserve"
|
2023-01-01 23:24:00 +08:00
|
|
|
|
"github.com/energye/golcl/lcl"
|
2023-01-25 22:26:08 +08:00
|
|
|
|
"time"
|
2022-12-30 18:22:21 +08:00
|
|
|
|
)
|
|
|
|
|
|
2022-12-31 13:16:30 +08:00
|
|
|
|
//go:embed resources
|
|
|
|
|
var resources embed.FS
|
|
|
|
|
|
2022-12-30 18:22:21 +08:00
|
|
|
|
func main() {
|
|
|
|
|
//全局初始化 每个应用都必须调用的
|
2023-01-11 22:11:13 +08:00
|
|
|
|
cef.GlobalInit(nil, &resources)
|
2022-12-30 18:22:21 +08:00
|
|
|
|
//创建应用
|
2022-12-31 13:16:30 +08:00
|
|
|
|
config := cef.NewApplicationConfig()
|
|
|
|
|
config.SetMultiThreadedMessageLoop(false)
|
|
|
|
|
config.SetExternalMessagePump(false)
|
2023-01-11 22:26:21 +08:00
|
|
|
|
config.SetRemoteDebuggingPort(33333)
|
2022-12-31 13:16:30 +08:00
|
|
|
|
cefApp := cef.NewApplication(config)
|
2022-12-30 18:22:21 +08:00
|
|
|
|
//指定一个URL地址,或本地html文件目录
|
2023-01-03 17:53:01 +08:00
|
|
|
|
cef.BrowserWindow.Config.Url = "http://localhost:22022/index.html"
|
2023-01-03 18:50:12 +08:00
|
|
|
|
cef.BrowserWindow.Config.IconFS = "resources/icon.png"
|
2023-01-23 10:52:40 +08:00
|
|
|
|
cef.BrowserWindow.Config.EnableWebkitAppRegion = true
|
2023-01-06 18:26:40 +08:00
|
|
|
|
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
|
2023-01-11 22:11:13 +08:00
|
|
|
|
//window.DisableResize()
|
2023-01-28 11:56:24 +08:00
|
|
|
|
window.SetCenterWindow(true)
|
2023-01-07 18:42:17 +08:00
|
|
|
|
window.SetTitle("这里改变了窗口标题")
|
2023-01-28 11:46:09 +08:00
|
|
|
|
window.SetSize(1024, 900)
|
2023-01-01 22:19:26 +08:00
|
|
|
|
fmt.Println("cef.BrowserWindow.SetViewFrameBrowserInit", window)
|
2023-01-06 19:04:36 +08:00
|
|
|
|
fmt.Println("LCL", window.AsLCLBrowserWindow(), "VF", window.AsViewsFrameworkBrowserWindow())
|
2023-01-08 18:05:03 +08:00
|
|
|
|
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)
|
2023-01-08 17:04:39 +08:00
|
|
|
|
})
|
2023-01-06 18:26:40 +08:00
|
|
|
|
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())
|
2023-01-06 18:55:13 +08:00
|
|
|
|
popupWindow.SetTitle("修改了标题: " + beforePopupInfo.TargetUrl)
|
2023-01-07 18:42:17 +08:00
|
|
|
|
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)
|
2023-01-06 23:08:10 +08:00
|
|
|
|
browserWindow := popupWindow.AsViewsFrameworkBrowserWindow()
|
2023-01-07 21:29:08 +08:00
|
|
|
|
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)
|
|
|
|
|
})
|
2023-01-07 11:48:19 +08:00
|
|
|
|
//browserWindow.BrowserWindow().CreateTopLevelWindow()
|
|
|
|
|
//browserWindow.BrowserWindow().HideTitle()
|
2023-01-06 23:08:10 +08:00
|
|
|
|
fmt.Println("browserWindow:", browserWindow, browserWindow.WindowComponent().WindowHandle())
|
2023-01-06 18:26:40 +08:00
|
|
|
|
return false
|
|
|
|
|
})
|
2023-01-07 11:48:19 +08:00
|
|
|
|
window.AsViewsFrameworkBrowserWindow().WindowComponent().SetOnWindowActivationChanged(func(sender lcl.IObject, window *cef.ICefWindow, active bool) {
|
|
|
|
|
fmt.Println("SetOnWindowActivationChanged", active)
|
|
|
|
|
})
|
2023-01-07 18:42:17 +08:00
|
|
|
|
window.Show()
|
2023-01-08 17:04:39 +08:00
|
|
|
|
fmt.Println("SetBrowserInit 结束")
|
2023-01-01 22:19:26 +08:00
|
|
|
|
})
|
2023-01-07 21:29:08 +08:00
|
|
|
|
cef.BrowserWindow.SetBrowserInitAfter(func(window cef.IBrowserWindow) {
|
|
|
|
|
bw := window.AsViewsFrameworkBrowserWindow().BrowserWindow()
|
|
|
|
|
fmt.Println("handle", bw.WindowComponent().WindowHandle().ToPtr())
|
2023-01-19 14:31:46 +08:00
|
|
|
|
//设置隐藏窗口标题
|
|
|
|
|
//window.HideTitle()
|
2023-01-10 09:40:03 +08:00
|
|
|
|
fmt.Println("SetBrowserInitAfter 结束")
|
2023-01-26 21:47:55 +08:00
|
|
|
|
sysTray(window)
|
2023-01-07 21:29:08 +08:00
|
|
|
|
})
|
2022-12-31 15:46:17 +08:00
|
|
|
|
//在主进程启动成功之后执行
|
|
|
|
|
//在这里启动内置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)
|
|
|
|
|
}
|
2023-01-06 19:27:12 +08:00
|
|
|
|
|
2023-01-25 22:26:08 +08:00
|
|
|
|
func sysTray(browserWindow cef.IBrowserWindow) {
|
|
|
|
|
sysTray := browserWindow.NewSysTray()
|
|
|
|
|
sysTray.SetIconFS("resources/icon.ico")
|
|
|
|
|
sysTray.SetHint("中文hint\n换行中文")
|
|
|
|
|
sysTray.SetOnClick(func() {
|
|
|
|
|
fmt.Println("SetOnClick")
|
2023-01-06 19:27:12 +08:00
|
|
|
|
})
|
2023-01-25 22:26:08 +08:00
|
|
|
|
tray := sysTray.AsSysTray()
|
|
|
|
|
check := tray.AddMenuItem("check")
|
|
|
|
|
check.Check()
|
|
|
|
|
not := tray.AddMenuItem("通知")
|
|
|
|
|
not.Click(func() {
|
2023-01-27 21:34:56 +08:00
|
|
|
|
tray.Notice("标题", "内notice 是一个跨平台的系统通知库\nnotice 是一个跨平台的系统通知库", 1000)
|
2023-01-06 19:27:12 +08:00
|
|
|
|
})
|
2023-01-25 22:26:08 +08:00
|
|
|
|
enable := tray.AddMenuItem("启用/禁用")
|
|
|
|
|
enable.Click(func() {
|
|
|
|
|
fmt.Println("启用/禁用 点击")
|
2023-01-06 19:27:12 +08:00
|
|
|
|
})
|
2023-01-25 22:26:08 +08:00
|
|
|
|
tray.AddSeparator()
|
|
|
|
|
menuItem := tray.AddMenuItem("1级菜单1", func() {
|
|
|
|
|
fmt.Println("1级菜单1")
|
2023-01-06 19:27:12 +08:00
|
|
|
|
})
|
2023-01-25 22:26:08 +08:00
|
|
|
|
menuItem.SetIconFS("resources/icon.ico")
|
|
|
|
|
tray.AddSeparator()
|
|
|
|
|
item := tray.AddMenuItem("1级菜单2")
|
|
|
|
|
item.AddSubMenu("2级子菜单1")
|
|
|
|
|
sub2Menu := item.AddSubMenu("2级子菜单2")
|
|
|
|
|
sub2Menu.AddSubMenu("3级子菜单1")
|
|
|
|
|
tray.AddSeparator()
|
|
|
|
|
tray.AddMenuItem("退出", func() {
|
|
|
|
|
fmt.Println("退出")
|
|
|
|
|
browserWindow.CloseBrowserWindow()
|
2023-01-06 19:27:12 +08:00
|
|
|
|
})
|
2023-01-25 22:26:08 +08:00
|
|
|
|
|
|
|
|
|
sysTray.Show()
|
|
|
|
|
return
|
|
|
|
|
//测试图标切换
|
|
|
|
|
go func() {
|
|
|
|
|
var b bool
|
|
|
|
|
for {
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
b = !b
|
|
|
|
|
if b {
|
|
|
|
|
sysTray.SetHint(fmt.Sprintf("%d\n%v", time.Now().Second(), b))
|
|
|
|
|
sysTray.SetIconFS("resources/icon_1.ico")
|
|
|
|
|
menuItem.SetIconFS("resources/icon_1.ico")
|
|
|
|
|
enable.SetLabel(fmt.Sprintf("%d\n%v", time.Now().Second(), b))
|
|
|
|
|
enable.Enable()
|
|
|
|
|
check.Check()
|
|
|
|
|
} else {
|
|
|
|
|
sysTray.SetHint(fmt.Sprintf("%d\n%v", time.Now().Second(), b))
|
|
|
|
|
sysTray.SetIconFS("resources/icon.ico")
|
|
|
|
|
menuItem.SetIconFS("resources/icon.ico")
|
|
|
|
|
enable.SetLabel(fmt.Sprintf("%d\n%v", time.Now().Second(), b))
|
|
|
|
|
enable.Disable()
|
|
|
|
|
check.Uncheck()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
2023-01-06 19:27:12 +08:00
|
|
|
|
}
|