energy/cef/cef-tray-sys-commponent.go
2023-01-26 12:33:19 +08:00

226 lines
4.5 KiB
Go

//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under GNU General Public License v3.0
//
//----------------------------------------
package cef
import (
"energye/systray"
"github.com/energye/energy/common"
"github.com/energye/golcl/energy/emfs"
"github.com/energye/golcl/energy/tools"
"io/ioutil"
)
//创建系统托盘
func newSysTray() *SysTray {
tray := &SysTray{
menu: &SysMenu{
items: make([]*SysMenuItem, 0, 0),
},
}
return tray
}
func (m *SysTray) onReady() {
if len(m.icon) > 0 {
systray.SetIcon(m.icon)
}
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)
}
//重置菜单
systray.ResetMenu()
//刷新并生成菜单
m.refreshSystray(m.menu.items, nil)
}
//refreshSystray 刷新并生成菜单
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 退出回调事件
func (m *SysTray) onExit() {
if !common.IsDarwin() {
systray.Quit()
}
}
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
}
//Show 显示/启动 托盘
func (m *SysTray) Show() {
m.once.Do(func() {
if m.start == nil {
var runLoop = func() {
m.start, m.stop = systray.RunWithExternalLoop(m.onReady, m.onExit)
m.start()
}
if common.IsDarwin() {
runLoop()
} else {
go runLoop()
}
}
})
}
func (m *SysTray) close() {
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 创建托盘菜单, 如果托盘菜单是空, 把菜单项添加到托盘
// 该方法主动调用后 如果托盘菜单已创建则添加进去, 之后鼠标事件失效
//
// 仅MacOSX平台
func (m *SysTray) CreateMenu() {
if m.start == nil {
systray.CreateMenu()
}
}
// SetMenuNil 托盘菜单设置为nil, 如果托盘菜单不是空, 把菜单项设置为nil
// 该方法主动调用后 将移除托盘菜单, 之后鼠标事件生效
//
// 仅MacOSX平台
func (m *SysTray) SetMenuNil() {
if m.start == nil {
systray.SetMenuNil()
}
}
//ResetMenu 重置托盘菜单
func (m *SysTray) ResetMenu() {
if m.start == nil {
systray.ResetMenu()
}
}
//Add 添加一个菜单项
func (m *SysTray) Add(menuItem *SysMenuItem) {
m.menu.Add(menuItem)
}
//AddMenuItem 添加一个菜单项
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)
}
//AddSeparator 添加一个分隔线
func (m *SysTray) AddSeparator() {
m.menu.AddSeparator()
}
//NewMenuItem 创建一个新菜单项
func (m *SysTray) NewMenuItem(label string, click ...MenuItemClick) *SysMenuItem {
if len(click) > 0 {
return &SysMenuItem{label: label, click: click[0]}
}
return &SysMenuItem{label: label}
}