mirror of
https://gitee.com/energye/energy.git
synced 2024-11-29 18:28:06 +08:00
U: 系统托盘示例
This commit is contained in:
parent
7a6bb8acd7
commit
9b278924f5
@ -8,22 +8,23 @@ import (
|
||||
"github.com/energye/energy/v2/cef/ipc"
|
||||
"github.com/energye/energy/v2/cef/ipc/callback"
|
||||
"github.com/energye/energy/v2/cef/ipc/target"
|
||||
"github.com/energye/energy/v2/example/common/tray"
|
||||
"github.com/energye/energy/v2/logger"
|
||||
"github.com/energye/golcl/lcl"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:embed assets
|
||||
var assets embed.FS
|
||||
//go:embed resources
|
||||
var resources embed.FS
|
||||
|
||||
// go build -ldflags "-s -w"
|
||||
func main() {
|
||||
logger.SetEnable(true)
|
||||
logger.SetLevel(logger.CefLog_Debug)
|
||||
//全局初始化 每个应用都必须调用的
|
||||
cef.GlobalInit(nil, &assets)
|
||||
cef.GlobalInit(nil, &resources)
|
||||
exception.SetOnException(func(message string) {
|
||||
fmt.Println("Exception message", message)
|
||||
fmt.Println("Global Exception message", message)
|
||||
})
|
||||
//创建应用
|
||||
app := cef.NewApplication()
|
||||
@ -31,33 +32,22 @@ func main() {
|
||||
//app.SetExternalMessagePump(false)
|
||||
//app.SetMultiThreadedMessageLoop(false)
|
||||
cef.BrowserWindow.Config.Title = "Energy - ipc multiple-window"
|
||||
// 禁用主窗口配置, 默认开启, 禁用后如果有多个窗口同时存在, 在关闭主窗口时应用进程不会结束,直到最后一个窗口关闭才结束应用进程
|
||||
// 禁用主窗口配置, 该属性默认开启, 禁用后如果有多个窗口同时存在, 在关闭主窗口时应用进程不会结束,直到最后一个窗口关闭才结束应用进程
|
||||
cef.BrowserWindow.Config.EnableMainWindow = false
|
||||
|
||||
//本地资源加载
|
||||
cef.BrowserWindow.Config.LocalResource(cef.LocalLoadConfig{
|
||||
ResRootDir: "assets",
|
||||
FS: &assets,
|
||||
ResRootDir: "resources",
|
||||
FS: &resources,
|
||||
Home: "multiple-window.html",
|
||||
}.Build())
|
||||
|
||||
// 多窗口接收消息
|
||||
// 多窗口发送/接收消息
|
||||
ipc.On("sendMessage", func(channel callback.IChannel, type_ int) {
|
||||
var ok bool
|
||||
// 获得所有窗口
|
||||
infos := cef.BrowserWindow.GetWindowInfos()
|
||||
fmt.Println("windows-count:", len(infos))
|
||||
for _, info := range infos {
|
||||
// 将消息发送到目标窗口, 类型为JS接收
|
||||
iTarget := info.Target(target.TgJs)
|
||||
ok = ipc.EmitTarget("receiveMessage", iTarget, time.Now().String())
|
||||
println("ipc.EmitTarget", ok, iTarget.BrowserId())
|
||||
ok = ipc.EmitTargetAndCallback("receiveMessage", iTarget, []any{"带有callback的触发事件: " + time.Now().String()}, func() {
|
||||
fmt.Println("target callback")
|
||||
})
|
||||
println("ipc.EmitTargetAndCallback", ok, iTarget.BrowserId())
|
||||
}
|
||||
|
||||
fmt.Println("window-count:", len(infos))
|
||||
for _, info := range infos {
|
||||
// 将消息发送到目标窗口, 类型为JS接收
|
||||
iTarget := info.Target(target.TgJs)
|
||||
@ -77,6 +67,7 @@ func main() {
|
||||
})
|
||||
println("ipc.EmitAndCallback", ok)
|
||||
})
|
||||
// 刷新所有窗口
|
||||
ipc.On("refresh", func() {
|
||||
infos := cef.BrowserWindow.GetWindowInfos()
|
||||
for _, info := range infos {
|
||||
@ -86,8 +77,19 @@ func main() {
|
||||
println("ipc.EmitTarget-refresh", ok)
|
||||
}
|
||||
})
|
||||
// 创建窗口
|
||||
ipc.On("createWindow", func() {
|
||||
infos := cef.BrowserWindow.GetWindowInfos()
|
||||
for _, info := range infos {
|
||||
// 将消息发送到目标窗口, 类型为JS接收
|
||||
iTarget := info.Target(target.TgJs)
|
||||
var ok = ipc.EmitTarget("refresh", iTarget)
|
||||
println("ipc.EmitTarget-refresh", ok)
|
||||
}
|
||||
})
|
||||
|
||||
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
|
||||
// 弹出窗口处理
|
||||
event.SetOnBeforePopup(func(sender lcl.IObject, browser *cef.ICefBrowser, frame *cef.ICefFrame, beforePopupInfo *cef.BeforePopupInfo, popupWindow cef.IBrowserWindow, noJavascriptAccess *bool) bool {
|
||||
fmt.Println("BeforePopup", browser.Identifier())
|
||||
popupWindow.RunOnMainThread(func() {
|
||||
@ -110,6 +112,14 @@ func main() {
|
||||
count++
|
||||
}
|
||||
}()
|
||||
// 托盘
|
||||
if window.IsLCL() {
|
||||
// LCL窗口 Window, MacOS
|
||||
tray.LCLTray(window)
|
||||
} else {
|
||||
// VF窗口 默认Linux
|
||||
tray.SYSTray(window)
|
||||
}
|
||||
})
|
||||
//运行应用
|
||||
cef.Run(app)
|
||||
|
@ -1,69 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"github.com/energye/energy/v2/cef"
|
||||
"github.com/energye/energy/v2/common"
|
||||
"github.com/energye/golcl/lcl"
|
||||
"github.com/energye/energy/v2/example/common/tray"
|
||||
)
|
||||
|
||||
//go:embed resources
|
||||
var resources embed.FS
|
||||
|
||||
func main() {
|
||||
cef.GlobalInit(nil, &resources)
|
||||
cef.GlobalInit(nil, nil)
|
||||
cefApp := cef.NewApplication()
|
||||
cef.BrowserWindow.Config.Url = "https://www.baidu.com"
|
||||
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
|
||||
if window.IsLCL() {
|
||||
trayDemo(window)
|
||||
tray.LCLTray(window)
|
||||
}
|
||||
})
|
||||
cef.Run(cefApp)
|
||||
}
|
||||
|
||||
// LCL组件托盘, 适用windows和macosx
|
||||
// 目前 不支持linux因GTK2和GTK3共存问题, 以后解决GTK3问题将支持
|
||||
// 或linux cef 106以前的版本使用GTK2的的也可使用该托盘
|
||||
func trayDemo(browserWindow cef.IBrowserWindow) {
|
||||
window := browserWindow.AsLCLBrowserWindow().BrowserWindow()
|
||||
//托盘 windows linux macos 系统托盘
|
||||
newTray := window.NewTray()
|
||||
newTray.SetTitle("任务管理器里显示的标题")
|
||||
newTray.SetHint("这里是文字\n文字啊")
|
||||
if common.IsLinux() {
|
||||
newTray.SetIconFS("resources/icon.png")
|
||||
} else {
|
||||
newTray.SetIconFS("resources/icon_menu.ico")
|
||||
}
|
||||
tray := newTray.AsLCLTray()
|
||||
menu1 := tray.AddMenuItem("父菜单", nil)
|
||||
//带图标的菜单
|
||||
iconItem := tray.NewMenuItem("带个图标", nil)
|
||||
iconItem.Bitmap().SetSize(16, 16) //图标情况调整大小
|
||||
iconItem.Bitmap().SetTransparent(true) //透明
|
||||
icon := lcl.NewIcon()
|
||||
icon.LoadFromFSFile("resources/icon_menu.ico")
|
||||
iconItem.Bitmap().Canvas().Draw(0, 0, icon) //画上去
|
||||
tray.TrayMenu().Items().Add(iconItem)
|
||||
|
||||
menu1.Add(tray.NewMenuItem("子菜单", func() {
|
||||
lcl.ShowMessage("子菜单点击 提示消息")
|
||||
}))
|
||||
tray.AddMenuItem("显示气泡", func() {
|
||||
tray.Notice("气泡标题", "气泡内容", 2000)
|
||||
})
|
||||
tray.AddMenuItem("显示/隐藏", func() {
|
||||
window.SetVisible(!window.Visible())
|
||||
if window.Visible() { //之后的显示状态
|
||||
window.RunOnMainThread(func() {
|
||||
window.SetFocus()
|
||||
})
|
||||
}
|
||||
})
|
||||
tray.AddMenuItem("退出", func() {
|
||||
browserWindow.CloseBrowserWindow()
|
||||
})
|
||||
//托盘 end
|
||||
tray.Show()
|
||||
}
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 1.1 KiB |
Binary file not shown.
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 765 B |
Binary file not shown.
Before Width: | Height: | Size: 1.1 KiB |
Binary file not shown.
Before Width: | Height: | Size: 66 KiB |
@ -2,10 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"github.com/energye/energy/v2/cef"
|
||||
"github.com/energye/energy/v2/common"
|
||||
"time"
|
||||
"github.com/energye/energy/v2/example/common/tray"
|
||||
)
|
||||
|
||||
//go:embed resources
|
||||
@ -16,74 +14,7 @@ func main() {
|
||||
cefApp := cef.NewApplication()
|
||||
cef.BrowserWindow.Config.Url = "https://www.baidu.com"
|
||||
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
|
||||
trayDemo(window)
|
||||
tray.SYSTray(window)
|
||||
})
|
||||
cef.Run(cefApp)
|
||||
}
|
||||
|
||||
//系统托盘 和LCL组件差不多,但不如LCL组件的好用,适用 windows,linux, macosx
|
||||
//
|
||||
//主要给linux提供的,推荐linux中使用, 非linux 使用 lcl 实现的它更好用
|
||||
func trayDemo(browserWindow cef.IBrowserWindow) {
|
||||
sysTray := browserWindow.NewSysTray()
|
||||
if common.IsLinux() {
|
||||
sysTray.SetIconFS("resources/icon.png")
|
||||
} else {
|
||||
sysTray.SetIconFS("resources/icon.ico")
|
||||
}
|
||||
sysTray.SetHint("中文hint\n换行中文")
|
||||
sysTray.SetOnClick(func() {
|
||||
fmt.Println("SetOnClick")
|
||||
})
|
||||
tray := sysTray.AsSysTray()
|
||||
check := tray.AddMenuItem("check")
|
||||
check.Check()
|
||||
not := tray.AddMenuItem("通知")
|
||||
not.Click(func() {
|
||||
tray.Notice("标题", "内notice 是一个跨平台的系统通知库\nnotice 是一个跨平台的系统通知库", 1000)
|
||||
})
|
||||
enable := tray.AddMenuItem("启用/禁用")
|
||||
enable.Click(func() {
|
||||
fmt.Println("启用/禁用 点击")
|
||||
})
|
||||
tray.AddSeparator()
|
||||
menuItem := tray.AddMenuItem("1级菜单1", func() {
|
||||
fmt.Println("1级菜单1")
|
||||
})
|
||||
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()
|
||||
})
|
||||
|
||||
sysTray.Show()
|
||||
//测试图标切换
|
||||
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()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user