energy/cef/cef-tray-sys-commponent.go

257 lines
5.5 KiB
Go
Raw Normal View History

2022-10-04 13:21:05 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
2022-10-04 13:21:05 +08:00
//
//----------------------------------------
2023-02-20 14:42:17 +08:00
// 系统托盘
2022-10-04 13:21:05 +08:00
package cef
2023-01-24 18:55:02 +08:00
2023-01-24 19:46:20 +08:00
import (
2023-01-25 17:47:55 +08:00
"github.com/energye/energy/common"
2023-01-26 21:47:55 +08:00
"github.com/energye/energy/consts"
2023-02-02 14:38:35 +08:00
"github.com/energye/energy/pkgs/notice"
"github.com/energye/energy/pkgs/systray"
2023-01-24 19:46:20 +08:00
"github.com/energye/golcl/energy/emfs"
"github.com/energye/golcl/energy/tools"
"io/ioutil"
)
2023-01-24 18:55:02 +08:00
// 创建系统托盘
2023-01-24 18:55:02 +08:00
func newSysTray() *SysTray {
2023-01-28 12:52:36 +08:00
notice.SetUniqueID(BrowserWindow.Config.WindowProperty.Title)
2023-01-25 14:59:28 +08:00
tray := &SysTray{
2023-01-24 23:08:39 +08:00
menu: &SysMenu{
2023-01-25 14:59:28 +08:00
items: make([]*SysMenuItem, 0, 0),
2023-01-24 23:08:39 +08:00
},
}
2023-01-25 14:59:28 +08:00
return tray
}
func (m *SysTray) onReady() {
if len(m.icon) > 0 {
2023-01-25 22:28:49 +08:00
systray.SetIcon(m.icon)
2023-01-25 14:59:28 +08:00
}
if m.click != nil {
systray.SetOnClick(m.click)
}
if m.dClick != nil {
systray.SetOnDClick(m.dClick)
}
if m.rClick != nil {
systray.SetOnRClick(m.rClick)
}
if m.title != "" {
systray.SetTitle(m.title)
}
if m.tooltip != "" {
systray.SetTooltip(m.tooltip)
}
2023-01-25 17:47:55 +08:00
//重置菜单
2023-01-25 14:59:28 +08:00
systray.ResetMenu()
2023-01-25 17:47:55 +08:00
//刷新并生成菜单
2023-01-25 14:59:28 +08:00
m.refreshSystray(m.menu.items, nil)
}
// refreshSystray 刷新并生成菜单
2023-01-25 14:59:28 +08:00
func (m *SysTray) refreshSystray(items []*SysMenuItem, parent *systray.MenuItem) {
for _, item := range items {
mItem := itemForMenuItem(item, parent)
if mItem == nil {
continue
}
if item.childMenu != nil && len(item.childMenu.items) > 0 {
m.refreshSystray(item.childMenu.items, mItem)
}
}
}
// onExit 退出回调事件
2023-01-25 14:59:28 +08:00
func (m *SysTray) onExit() {
2023-01-25 17:47:55 +08:00
if !common.IsDarwin() {
systray.Quit()
}
2023-01-24 18:55:02 +08:00
}
2023-02-20 14:42:17 +08:00
// AsSysTray 尝试转换为 SysTray 组件托盘如果创建的是其它类型托盘返回nil
2023-01-24 18:55:02 +08:00
func (m *SysTray) AsSysTray() *SysTray {
return m
}
2023-02-20 14:42:17 +08:00
// AsViewsFrameTray 尝试转换为 views framework 组件托盘, 如果创建的是其它类型托盘返回nil
2023-01-24 18:55:02 +08:00
func (m *SysTray) AsViewsFrameTray() *ViewsFrameTray {
return nil
}
2023-02-20 14:42:17 +08:00
// AsCEFTray 尝试转换为 LCL+CEF 组件托盘, 如果创建的是其它类型托盘返回nil
2023-01-24 18:55:02 +08:00
func (m *SysTray) AsCEFTray() *CEFTray {
return nil
}
2023-02-20 14:42:17 +08:00
// AsLCLTray 尝试转换为 LCL 组件托盘, 如果创建的是其它类型托盘返回nil
2023-01-24 18:55:02 +08:00
func (m *SysTray) AsLCLTray() *LCLTray {
return nil
}
// Show 显示/启动 托盘
2023-01-24 18:55:02 +08:00
func (m *SysTray) Show() {
2023-02-02 15:12:14 +08:00
m.lock.Lock()
defer m.lock.Unlock()
if m.start == nil {
var runLoop = func() {
m.start, m.stop = systray.RunWithExternalLoop(m.onReady, m.onExit)
m.start()
}
if common.IsDarwin() {
// view framework
if consts.IsMessageLoop {
runLoop()
2023-01-26 12:33:19 +08:00
} else {
2023-02-02 15:12:14 +08:00
//LCL
QueueAsyncCall(func(id int) {
runLoop()
})
2023-01-26 12:33:19 +08:00
}
2023-02-02 15:12:14 +08:00
} else {
//windows linux
go runLoop()
2023-01-25 17:47:55 +08:00
}
2023-02-02 15:12:14 +08:00
}
2023-01-24 18:55:02 +08:00
}
func (m *SysTray) close() {
2023-01-25 17:47:55 +08:00
if !common.IsDarwin() {
if m.stop != nil {
m.stop()
m.start = nil
m.stop = nil
}
2023-01-25 14:59:28 +08:00
}
}
// SetOnDblClick 鼠标双击事件
2023-01-24 18:55:02 +08:00
func (m *SysTray) SetOnDblClick(fn TrayICONClick) {
2023-01-25 14:59:28 +08:00
m.dClick = fn
2023-01-24 18:55:02 +08:00
}
// SetOnClick 鼠标单击事件
2023-01-24 18:55:02 +08:00
func (m *SysTray) SetOnClick(fn TrayICONClick) {
2023-01-25 14:59:28 +08:00
m.click = fn
2023-01-24 19:46:20 +08:00
}
2023-01-24 18:55:02 +08:00
// SetOnRClick 鼠标右键
2023-01-24 19:46:20 +08:00
func (m *SysTray) SetOnRClick(fn func(menu systray.IMenu)) {
2023-01-25 14:59:28 +08:00
m.rClick = fn
2023-01-24 18:55:02 +08:00
}
// SetHint 设置托盘提示
2023-01-24 18:55:02 +08:00
func (m *SysTray) SetHint(value string) {
2023-01-25 14:59:28 +08:00
m.tooltip = value
if m.start != nil {
systray.SetTooltip(value)
}
2023-01-24 18:55:02 +08:00
}
// SetTitle 设置托盘标题
2023-01-24 18:55:02 +08:00
func (m *SysTray) SetTitle(title string) {
2023-01-25 14:59:28 +08:00
m.title = title
if m.start != nil {
systray.SetTitle(title)
}
2023-01-24 18:55:02 +08:00
}
2023-02-20 14:42:17 +08:00
// Notice
// 显示系统通知
2023-01-27 21:34:56 +08:00
//
// title 标题
2023-01-27 21:34:56 +08:00
//
// content 内容
2023-01-27 21:34:56 +08:00
//
// timeout 显示时间(毫秒)
2023-01-27 21:34:56 +08:00
func (m *SysTray) Notice(title, content string, timeout int32) {
notification(nil, title, content, timeout)
}
// SetIconFS 设置托盘图标
2023-01-24 18:55:02 +08:00
func (m *SysTray) SetIconFS(iconResourcePath string) {
2023-01-24 19:46:20 +08:00
if emfs.IsExist(iconResourcePath) {
2023-01-25 14:59:28 +08:00
data, err := emfs.GetResources(iconResourcePath)
2023-01-24 19:46:20 +08:00
if err == nil {
2023-01-25 14:59:28 +08:00
m.icon = data
if m.start != nil {
systray.SetIcon(m.icon)
}
2023-01-24 19:46:20 +08:00
}
}
2023-01-24 18:55:02 +08:00
}
// SetIcon 设置托盘图标
2023-01-24 18:55:02 +08:00
func (m *SysTray) SetIcon(iconResourcePath string) {
2023-01-24 19:46:20 +08:00
if tools.IsExist(iconResourcePath) {
2023-01-25 14:59:28 +08:00
data, err := ioutil.ReadFile(iconResourcePath)
2023-01-24 19:46:20 +08:00
if err == nil {
2023-01-25 14:59:28 +08:00
m.icon = data
if m.start != nil {
systray.SetIcon(m.icon)
}
2023-01-24 19:46:20 +08:00
}
}
2023-01-24 18:55:02 +08:00
}
2023-01-25 14:59:28 +08:00
2023-01-25 17:47:55 +08:00
// CreateMenu 创建托盘菜单, 如果托盘菜单是空, 把菜单项添加到托盘
// 该方法主动调用后 如果托盘菜单已创建则添加进去, 之后鼠标事件失效
//
// 仅MacOSX平台
func (m *SysTray) CreateMenu() {
if m.start == nil {
systray.CreateMenu()
}
2023-01-25 14:59:28 +08:00
}
2023-01-25 17:47:55 +08:00
// SetMenuNil 托盘菜单设置为nil, 如果托盘菜单不是空, 把菜单项设置为nil
// 该方法主动调用后 将移除托盘菜单, 之后鼠标事件生效
//
// 仅MacOSX平台
func (m *SysTray) SetMenuNil() {
if m.start == nil {
systray.SetMenuNil()
}
2023-01-25 14:59:28 +08:00
}
// ResetMenu 重置托盘菜单
2023-01-25 17:47:55 +08:00
func (m *SysTray) ResetMenu() {
if m.start == nil {
systray.ResetMenu()
}
}
// Add 添加一个菜单项
2023-01-25 17:47:55 +08:00
func (m *SysTray) Add(menuItem *SysMenuItem) {
m.menu.Add(menuItem)
2023-01-25 14:59:28 +08:00
}
// AddMenuItem 添加一个菜单项
2023-01-25 18:35:51 +08:00
func (m *SysTray) AddMenuItem(label string, click ...MenuItemClick) *SysMenuItem {
if len(click) > 0 {
return m.menu.AddMenuItem(label, click[0])
}
return m.menu.AddMenuItem(label, nil)
2023-01-25 17:47:55 +08:00
}
// AddSeparator 添加一个分隔线
2023-01-25 18:35:51 +08:00
func (m *SysTray) AddSeparator() {
m.menu.AddSeparator()
2023-01-25 17:47:55 +08:00
}
// NewMenuItem 创建一个新菜单项
2023-01-25 18:35:51 +08:00
func (m *SysTray) NewMenuItem(label string, click ...MenuItemClick) *SysMenuItem {
if len(click) > 0 {
return &SysMenuItem{label: label, click: click[0]}
}
return &SysMenuItem{label: label}
2023-01-25 14:59:28 +08:00
}