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

222 lines
4.4 KiB
Go
Raw Normal View History

2022-10-04 13:21:05 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
2022-10-04 16:38:43 +08:00
// Licensed under GNU General Public License v3.0
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 (
"energye/systray"
2023-01-25 22:26:08 +08:00
"energye/systray/icon"
2023-01-25 17:47:55 +08:00
"github.com/energye/energy/common"
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
//创建系统托盘
func newSysTray() *SysTray {
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:26:08 +08:00
systray.SetIcon(icon.Data)
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)
}
2023-01-25 17:47:55 +08:00
//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)
}
}
}
2023-01-25 17:47:55 +08:00
//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
}
func (m *SysTray) AsSysTray() *SysTray {
return m
}
func (m *SysTray) AsViewsFrameTray() *ViewsFrameTray {
return nil
}
func (m *SysTray) AsCEFTray() *CEFTray {
return nil
}
func (m *SysTray) AsLCLTray() *LCLTray {
return nil
}
2023-01-25 17:47:55 +08:00
//Show 显示/启动 托盘
2023-01-24 18:55:02 +08:00
func (m *SysTray) Show() {
2023-01-25 17:47:55 +08:00
m.once.Do(func() {
if m.start == nil {
go func() {
m.start, m.stop = systray.RunWithExternalLoop(m.onReady, m.onExit)
m.start()
}()
}
})
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
}
2023-01-25 14:59:28 +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
2023-01-25 14:59:28 +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
}
2023-01-25 17:47:55 +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
}
2023-01-25 17:47:55 +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-01-25 14:59:28 +08:00
//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
}
2023-01-25 14:59:28 +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
}
2023-01-25 17:47:55 +08:00
//ResetMenu 重置托盘菜单
func (m *SysTray) ResetMenu() {
if m.start == nil {
systray.ResetMenu()
}
}
//Add 添加一个菜单项
func (m *SysTray) Add(menuItem *SysMenuItem) {
m.menu.Add(menuItem)
2023-01-25 14:59:28 +08:00
}
2023-01-25 17:47:55 +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
}
2023-01-25 18:35:51 +08:00
//AddSeparator 添加一个分隔线
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
}