v2.3.58 systray

This commit is contained in:
杨红岩 2023-01-25 17:47:55 +08:00
parent 5958a26ce3
commit bf4d6f9115
4 changed files with 109 additions and 111 deletions

View File

@ -12,6 +12,7 @@ import (
"energye/systray"
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/types"
"sync"
)
//TMouseEvent 鼠标事件
@ -32,23 +33,18 @@ type TrayICONClick func()
//
//4. SysTray 对Windows、MacOSX和Linux支持较好
type ITray interface {
SetTitle(title string) //SetTitle 设置标题
SetVisible(v bool) //SetVisible 设置显示和隐藏托盘图标
Visible() bool //Visible 托盘的显示和隐藏状态
Show() //Show 显示托盘菜单窗口 windows有效
Hide() //Hide 隐藏托盘菜单窗口 windows有效
close() //close 关闭托盘菜单窗口 windows有效
SetOnClick(fn TrayICONClick) //SetOnClick 单击事件
SetOnDblClick(fn TrayICONClick) //SetOnDblClick 双击事件 linux 和 macos 可能不启作用
SetIconFS(iconResourcePath string) //SetIconFS 设置托盘图标
SetIcon(iconResourcePath string) //SetIcon 设置托盘图标
SetHint(value string) //SetHint 设置托盘hint(鼠标移动到托盘图标显示的文字)
ShowBalloon() //ShowBalloon 显示托盘气泡
SetBalloon(title, content string, timeout int32) ITray //SetBalloon 设置托盘气泡内容
AsSysTray() *SysTray //AsSysTray 尝试转换为 SysTray 组件托盘如果创建的是其它类型托盘返回nil
AsViewsFrameTray() *ViewsFrameTray //AsViewsFrameTray 尝试转换为 views framework 组件托盘, 如果创建的是其它类型托盘返回nil
AsCEFTray() *CEFTray //AsCEFTray 尝试转换为 LCL+CEF 组件托盘, 如果创建的是其它类型托盘返回nil
AsLCLTray() *LCLTray //AsLCLTray 尝试转换为 LCL 组件托盘, 如果创建的是其它类型托盘返回nil
SetTitle(title string) //SetTitle 设置标题
Show() //Show 显示/启动 托盘
close() //
SetOnClick(fn TrayICONClick) //SetOnClick 单击事件
SetOnDblClick(fn TrayICONClick) //SetOnDblClick 双击事件
SetIconFS(iconResourcePath string) //SetIconFS 设置托盘图标
SetIcon(iconResourcePath string) //SetIcon 设置托盘图标
SetHint(value string) //SetHint 设置托盘hint(鼠标移动到托盘图标显示的文字)
AsSysTray() *SysTray //AsSysTray 尝试转换为 SysTray 组件托盘如果创建的是其它类型托盘返回nil
AsViewsFrameTray() *ViewsFrameTray //AsViewsFrameTray 尝试转换为 views framework 组件托盘, 如果创建的是其它类型托盘返回nil
AsCEFTray() *CEFTray //AsCEFTray 尝试转换为 LCL+CEF 组件托盘, 如果创建的是其它类型托盘返回nil
AsLCLTray() *LCLTray //AsLCLTray 尝试转换为 LCL 组件托盘, 如果创建的是其它类型托盘返回nil
}
//LCLTray LCL组件 托盘
@ -82,6 +78,7 @@ type CEFTray struct {
//SysTray 系统原生
type SysTray struct {
once sync.Once
menu *SysMenu
icon []byte
title, tooltip string

View File

@ -10,6 +10,7 @@ package cef
import (
"energye/systray"
"github.com/energye/energy/common"
"github.com/energye/golcl/energy/emfs"
"github.com/energye/golcl/energy/tools"
"io/ioutil"
@ -44,15 +45,19 @@ func (m *SysTray) onReady() {
if m.tooltip != "" {
systray.SetTooltip(m.tooltip)
}
//重置菜单
systray.ResetMenu()
//刷新并生成菜单
m.refreshSystray(m.menu.items, nil)
go func() {
for {
select {}
}
}()
//go func() {
// for {
//
// select {}
// }
//}()
}
//refreshSystray 刷新并生成菜单
func (m *SysTray) refreshSystray(items []*SysMenuItem, parent *systray.MenuItem) {
for _, item := range items {
mItem := itemForMenuItem(item, parent)
@ -65,8 +70,11 @@ func (m *SysTray) refreshSystray(items []*SysMenuItem, parent *systray.MenuItem)
}
}
//onExit 退出回调事件
func (m *SysTray) onExit() {
systray.Quit()
if !common.IsDarwin() {
systray.Quit()
}
}
func (m *SysTray) AsSysTray() *SysTray {
@ -85,18 +93,83 @@ func (m *SysTray) AsLCLTray() *LCLTray {
return nil
}
//Show 显示/启动 托盘
func (m *SysTray) Show() {
if m.start == nil {
m.start, m.stop = systray.RunWithExternalLoop(m.onReady, m.onExit)
m.start()
}
}
func (m *SysTray) Hide() {
m.once.Do(func() {
if m.start == nil {
go func() {
m.start, m.stop = systray.RunWithExternalLoop(m.onReady, m.onExit)
m.start()
}()
}
})
}
func (m *SysTray) close() {
m.onExit()
if !common.IsDarwin() {
if m.stop != nil {
m.stop()
m.start = nil
m.stop = nil
}
}
}
//SetOnDblClick 鼠标双击事件
func (m *SysTray) SetOnDblClick(fn TrayICONClick) {
m.dClick = fn
}
//SetOnClick 鼠标单击事件
func (m *SysTray) SetOnClick(fn TrayICONClick) {
m.click = fn
}
//SetOnRClick 鼠标右键
func (m *SysTray) SetOnRClick(fn func(menu systray.IMenu)) {
m.rClick = fn
}
//SetHint 设置托盘提示
func (m *SysTray) SetHint(value string) {
m.tooltip = value
if m.start != nil {
systray.SetTooltip(value)
}
}
//SetTitle 设置托盘标题
func (m *SysTray) SetTitle(title string) {
m.title = title
if m.start != nil {
systray.SetTitle(title)
}
}
//SetIconFS 设置托盘图标
func (m *SysTray) SetIconFS(iconResourcePath string) {
if emfs.IsExist(iconResourcePath) {
data, err := emfs.GetResources(iconResourcePath)
if err == nil {
m.icon = data
if m.start != nil {
systray.SetIcon(m.icon)
}
}
}
}
//SetIcon 设置托盘图标
func (m *SysTray) SetIcon(iconResourcePath string) {
if tools.IsExist(iconResourcePath) {
data, err := ioutil.ReadFile(iconResourcePath)
if err == nil {
m.icon = data
if m.start != nil {
systray.SetIcon(m.icon)
}
}
}
}
// CreateMenu 创建托盘菜单, 如果托盘菜单是空, 把菜单项添加到托盘
@ -145,77 +218,3 @@ func (m *SysTray) AddMenuItemSeparator() {
func (m *SysTray) NewMenuItem(label string, onClick MenuItemClick) *SysMenuItem {
return &SysMenuItem{label: label, click: onClick}
}
//SetOnDblClick 鼠标双击事件
func (m *SysTray) SetOnDblClick(fn TrayICONClick) {
m.dClick = fn
}
//SetOnClick 鼠标单击事件
func (m *SysTray) SetOnClick(fn TrayICONClick) {
m.click = fn
}
//SetOnRClick 鼠标右键
func (m *SysTray) SetOnRClick(fn func(menu systray.IMenu)) {
m.rClick = fn
}
func (m *SysTray) SetHint(value string) {
m.tooltip = value
if m.start != nil {
systray.SetTooltip(value)
}
}
func (m *SysTray) SetTitle(title string) {
m.title = title
if m.start != nil {
systray.SetTitle(title)
}
}
//SetIconFS 设置托盘图标
func (m *SysTray) SetIconFS(iconResourcePath string) {
if emfs.IsExist(iconResourcePath) {
data, err := emfs.GetResources(iconResourcePath)
if err == nil {
m.icon = data
if m.start != nil {
systray.SetIcon(m.icon)
}
}
}
}
//SetIcon 设置托盘图标
func (m *SysTray) SetIcon(iconResourcePath string) {
if tools.IsExist(iconResourcePath) {
data, err := ioutil.ReadFile(iconResourcePath)
if err == nil {
m.icon = data
if m.start != nil {
systray.SetIcon(m.icon)
}
}
}
}
func (m *SysTray) Visible() bool {
return false
}
func (m *SysTray) SetVisible(v bool) {
}
//设置托盘气泡
//title 气泡标题
//content 气泡内容
//timeout 显示时间(毫秒)
func (m *SysTray) SetBalloon(title, content string, timeout int32) ITray {
return nil
}
//显示托盘气泡
func (m *SysTray) ShowBalloon() {
}

View File

@ -418,17 +418,18 @@ func cefTray(browserWindow cef.IBrowserWindow) {
window := browserWindow.AsLCLBrowserWindow().BrowserWindow()
var url = "http://localhost:22022/min-browser-tray.html"
tray := browserWindow.NewCefTray(250, 300, url)
asCEFTray := tray.AsCEFTray()
tray.SetTitle("任务管理器里显示的标题")
tray.SetHint("这里是文字\n文字啊")
tray.SetIconFS("resources/icon.ico")
tray.SetOnClick(func() {
fmt.Println("SetOnClick")
})
tray.SetBalloon("气泡标题", "气泡内容", 2000)
asCEFTray.SetBalloon("气泡标题", "气泡内容", 2000)
ipc.IPC.Browser().On("tray-show-balloon", func(context ipc.IIPCContext) {
fmt.Println("tray-show-balloon")
tray.ShowBalloon()
tray.Hide()
asCEFTray.ShowBalloon()
asCEFTray.Hide()
})
ipc.IPC.Browser().On("tray-show-main-window", func(context ipc.IIPCContext) {
vb := !window.Visible()
@ -439,7 +440,7 @@ func cefTray(browserWindow cef.IBrowserWindow) {
}
window.Focused()
}
tray.Hide()
asCEFTray.Hide()
})
ipc.IPC.Browser().On("tray-close-main-window", func(context ipc.IIPCContext) {
browserWindow.CloseBrowserWindow()
@ -448,7 +449,7 @@ func cefTray(browserWindow cef.IBrowserWindow) {
cef.QueueAsyncCall(func(id int) {
lcl.ShowMessage("tray-show-message-box 提示消息")
})
tray.Hide()
asCEFTray.Hide()
})
//托盘 end
}
@ -467,7 +468,7 @@ func tray(browserWindow cef.IBrowserWindow) {
lcl.ShowMessage("子菜单点击 提示消息")
}))
tray.AddMenuItem("显示气泡", func() {
newTray.ShowBalloon()
tray.ShowBalloon()
})
tray.AddMenuItem("显示/隐藏", func() {
vis := window.Visible()

View File

@ -110,6 +110,7 @@ func sysTray(browserWindow cef.IBrowserWindow) {
for {
time.Sleep(time.Second)
b = !b
fmt.Println("bool", b)
if b {
sysTray.SetHint(fmt.Sprintf("%d\n%v", time.Now().Second(), b))
sysTray.SetIconFS("resources/icon_1.ico")