2023-01-10 09:40:03 +08:00
|
|
|
|
//----------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Copyright © yanghy. All Rights Reserved.
|
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// Licensed under Apache License Version 2.0, January 2004
|
|
|
|
|
//
|
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2023-01-10 09:40:03 +08:00
|
|
|
|
//
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// energy 系统托盘组件
|
2023-05-31 17:41:14 +08:00
|
|
|
|
|
2023-01-10 09:40:03 +08:00
|
|
|
|
package cef
|
|
|
|
|
|
|
|
|
|
import (
|
2023-05-31 18:00:34 +08:00
|
|
|
|
"github.com/energye/energy/v2/pkgs/notice"
|
|
|
|
|
"github.com/energye/energy/v2/pkgs/systray"
|
2023-01-10 09:40:03 +08:00
|
|
|
|
"github.com/energye/golcl/lcl"
|
|
|
|
|
"github.com/energye/golcl/lcl/types"
|
2023-01-25 17:47:55 +08:00
|
|
|
|
"sync"
|
2023-01-10 09:40:03 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// TMouseEvent 鼠标事件
|
2023-01-10 09:40:03 +08:00
|
|
|
|
type TMouseEvent func(sender lcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) bool
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// TrayICONClick 托盘图标鼠标事件
|
2023-01-10 19:52:57 +08:00
|
|
|
|
type TrayICONClick func()
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// ITray 托盘定义接口
|
2023-01-24 23:08:39 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 实现4种系统托盘 1: LCLTray LCL组件, 2: CEFTray CEF基于LCL组件+html, 3: ViewsFrameTray VF(views framework)组件+html, 4: SysTray 系统原生
|
2023-01-24 23:08:39 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 1. LCLTray 对Windows、MacOSX支持较好,linux由于gtk2与gtk3原因目前无法正常使用
|
2023-01-24 23:08:39 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 2. CEFTray Windows
|
2023-01-24 23:08:39 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 3. ViewsFrameTray Windows
|
2023-01-24 23:08:39 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 4. SysTray 对Windows、MacOSX和Linux支持较好
|
2023-01-10 09:40:03 +08:00
|
|
|
|
type ITray interface {
|
2023-01-27 21:34:56 +08:00
|
|
|
|
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
|
|
|
|
|
Notice(title, content string, timeout int32) //Notice 托盘系统通知
|
2023-01-10 09:40:03 +08:00
|
|
|
|
}
|
2023-01-10 10:19:20 +08:00
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// LCLTray LCL组件 托盘
|
2023-01-10 10:19:20 +08:00
|
|
|
|
type LCLTray struct {
|
|
|
|
|
owner lcl.IComponent
|
|
|
|
|
trayIcon *lcl.TTrayIcon
|
|
|
|
|
popupMenu *lcl.TPopupMenu
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// ViewsFrameTray VF(views framework)组件+html 托盘
|
2023-01-10 10:19:20 +08:00
|
|
|
|
type ViewsFrameTray struct {
|
|
|
|
|
trayWindow *ViewsFrameworkBrowserWindow
|
|
|
|
|
trayIcon *lcl.TTrayIcon
|
2023-01-28 13:22:11 +08:00
|
|
|
|
visible bool
|
2023-01-10 10:19:20 +08:00
|
|
|
|
x, y, w, h int32
|
|
|
|
|
mouseUp TMouseEvent
|
|
|
|
|
isClosing bool
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// CEFTray CEF基于LCL组件+html 托盘
|
2023-01-10 10:19:20 +08:00
|
|
|
|
type CEFTray struct {
|
|
|
|
|
*lcl.TForm
|
|
|
|
|
owner lcl.IComponent
|
|
|
|
|
trayIcon *lcl.TTrayIcon
|
|
|
|
|
chromium IChromium
|
2023-05-31 17:41:14 +08:00
|
|
|
|
windowParent ICEFWindowParent
|
2023-01-10 10:19:20 +08:00
|
|
|
|
x, y, w, h int32
|
|
|
|
|
mouseUp TMouseEvent
|
|
|
|
|
isClosing bool
|
|
|
|
|
url string
|
|
|
|
|
}
|
2023-01-24 18:21:49 +08:00
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// SysTray 系统原生
|
2023-01-24 18:21:49 +08:00
|
|
|
|
type SysTray struct {
|
2023-02-02 15:12:14 +08:00
|
|
|
|
lock sync.Mutex
|
2023-01-25 14:59:28 +08:00
|
|
|
|
menu *SysMenu
|
|
|
|
|
icon []byte
|
|
|
|
|
title, tooltip string
|
|
|
|
|
click TrayICONClick
|
|
|
|
|
dClick TrayICONClick
|
|
|
|
|
rClick func(menu systray.IMenu)
|
|
|
|
|
start, stop func()
|
2023-01-24 18:21:49 +08:00
|
|
|
|
}
|
2023-01-27 21:34:56 +08:00
|
|
|
|
|
|
|
|
|
func notification(tray lcl.IComponent, title, content string, timeout int32) {
|
|
|
|
|
var lclTrayNotice *lcl.TTrayIcon
|
|
|
|
|
if tray != nil {
|
|
|
|
|
lclTrayNotice = tray.(*lcl.TTrayIcon)
|
|
|
|
|
}
|
|
|
|
|
var lclNotice = func() {
|
|
|
|
|
lclTrayNotice.SetBalloonTitle(title)
|
|
|
|
|
lclTrayNotice.SetBalloonHint(content)
|
|
|
|
|
lclTrayNotice.SetBalloonTimeout(timeout)
|
|
|
|
|
lclTrayNotice.ShowBalloonHint()
|
|
|
|
|
}
|
|
|
|
|
var sysNotice = func() {
|
|
|
|
|
notify := notice.NewNotification(title, content)
|
|
|
|
|
notify.SetTimeout(timeout)
|
|
|
|
|
notice.SendNotification(notify)
|
|
|
|
|
}
|
|
|
|
|
if lclTrayNotice != nil {
|
|
|
|
|
lclNotice()
|
|
|
|
|
} else {
|
|
|
|
|
sysNotice()
|
|
|
|
|
}
|
|
|
|
|
}
|