2022-10-04 13:21:05 +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
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// 基于 LCL 系统托盘
|
2022-10-04 13:21:05 +08:00
|
|
|
|
package cef
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/energye/golcl/lcl"
|
|
|
|
|
"github.com/energye/golcl/lcl/types"
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 创建系统托盘
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func newTray(owner lcl.IComponent) *LCLTray {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
trayIcon := lcl.NewTrayIcon(owner)
|
2023-01-09 18:32:21 +08:00
|
|
|
|
return &LCLTray{
|
2023-01-10 19:52:57 +08:00
|
|
|
|
owner: owner,
|
|
|
|
|
trayIcon: trayIcon,
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// AsSysTray 尝试转换为 SysTray 组件托盘,如果创建的是其它类型托盘返回nil
|
2023-01-24 18:55:02 +08:00
|
|
|
|
func (m *LCLTray) AsSysTray() *SysTray {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// AsViewsFrameTray 尝试转换为 views framework 组件托盘, 如果创建的是其它类型托盘返回nil
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) AsViewsFrameTray() *ViewsFrameTray {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// AsCEFTray 尝试转换为 LCL+CEF 组件托盘, 如果创建的是其它类型托盘返回nil
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) AsCEFTray() *CEFTray {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// AsLCLTray 尝试转换为 LCL 组件托盘, 如果创建的是其它类型托盘返回nil
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) AsLCLTray() *LCLTray {
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetVisible 设置显示状态
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) SetVisible(v bool) {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
m.trayIcon.SetVisible(v)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// Visible 显示状态
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) Visible() bool {
|
2023-01-06 21:42:29 +08:00
|
|
|
|
return m.trayIcon.Visible()
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// Show 显示/启动 托盘
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) Show() {
|
2023-01-06 21:42:29 +08:00
|
|
|
|
m.SetVisible(true)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// Hide 隐藏 托盘
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) Hide() {
|
2023-01-06 21:42:29 +08:00
|
|
|
|
m.SetVisible(false)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
2023-01-06 21:42:29 +08:00
|
|
|
|
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) close() {
|
2023-01-06 23:08:10 +08:00
|
|
|
|
m.Hide()
|
|
|
|
|
}
|
2022-10-04 13:21:05 +08:00
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnDblClick 设置双击事件
|
2023-01-10 19:52:57 +08:00
|
|
|
|
func (m *LCLTray) SetOnDblClick(fn TrayICONClick) {
|
|
|
|
|
m.trayIcon.SetOnDblClick(func(sender lcl.IObject) {
|
|
|
|
|
fn()
|
|
|
|
|
})
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnClick 设置单击事件
|
2023-01-10 19:52:57 +08:00
|
|
|
|
func (m *LCLTray) SetOnClick(fn TrayICONClick) {
|
|
|
|
|
m.trayIcon.SetOnClick(func(sender lcl.IObject) {
|
|
|
|
|
fn()
|
|
|
|
|
})
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
2023-01-24 18:21:49 +08:00
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnMouseUp 鼠标抬起事件
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) SetOnMouseUp(fn TMouseEvent) {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
m.trayIcon.SetOnMouseUp(func(sender lcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) {
|
|
|
|
|
fn(sender, button, shift, x, y)
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-01-24 18:21:49 +08:00
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnMouseDown 鼠标按下事件
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) SetOnMouseDown(fn lcl.TMouseEvent) {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
m.trayIcon.SetOnMouseDown(fn)
|
|
|
|
|
}
|
2023-01-24 18:21:49 +08:00
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnMouseMove 鼠标移动事件
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) SetOnMouseMove(fn lcl.TMouseMoveEvent) {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
m.trayIcon.SetOnMouseMove(fn)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// TrayMenu 创建并返回托盘根菜单 PopupMenu
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) TrayMenu() *lcl.TPopupMenu {
|
2023-01-10 19:52:57 +08:00
|
|
|
|
if m.popupMenu == nil {
|
|
|
|
|
m.popupMenu = lcl.NewPopupMenu(m.trayIcon)
|
|
|
|
|
m.trayIcon.SetPopupMenu(m.popupMenu)
|
|
|
|
|
}
|
2022-10-04 13:21:05 +08:00
|
|
|
|
return m.popupMenu
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetIconFS 设置托盘图标
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) SetIconFS(iconResourcePath string) {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
m.trayIcon.Icon().LoadFromFSFile(iconResourcePath)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetIcon 设置托盘图标
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) SetIcon(iconResourcePath string) {
|
2023-01-07 16:12:00 +08:00
|
|
|
|
m.trayIcon.Icon().LoadFromFile(iconResourcePath)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetHint 设置提示
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) SetHint(value string) {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
m.trayIcon.SetHint(value)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// SetTitle 设置标题
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *LCLTray) SetTitle(title string) {
|
2023-01-06 21:59:59 +08:00
|
|
|
|
m.trayIcon.SetHint(title)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// Notice
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 显示系统通知
|
2023-01-06 21:59:59 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// title 标题
|
2023-01-06 21:59:59 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// content 内容
|
2023-01-06 21:59:59 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// timeout 显示时间(毫秒)
|
2023-01-27 21:34:56 +08:00
|
|
|
|
func (m *LCLTray) Notice(title, content string, timeout int32) {
|
|
|
|
|
notification(m.trayIcon, title, content, timeout)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// NewMenuItem
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 创建一个菜单,还未添加到托盘
|
2023-01-24 23:08:39 +08:00
|
|
|
|
func (m *LCLTray) NewMenuItem(caption string, onClick MenuItemClick) *lcl.TMenuItem {
|
2023-01-06 21:42:29 +08:00
|
|
|
|
item := lcl.NewMenuItem(m.trayIcon)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
item.SetCaption(caption)
|
|
|
|
|
if onClick != nil {
|
2023-01-24 23:08:39 +08:00
|
|
|
|
item.SetOnClick(func(sender lcl.IObject) {
|
|
|
|
|
onClick()
|
|
|
|
|
})
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
return item
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// AddMenuItem
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 添加一个托盘菜单
|
2023-01-24 23:08:39 +08:00
|
|
|
|
func (m *LCLTray) AddMenuItem(caption string, onClick MenuItemClick) *lcl.TMenuItem {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
item := m.NewMenuItem(caption, onClick)
|
2023-01-10 19:52:57 +08:00
|
|
|
|
m.TrayMenu().Items().Add(item)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
return item
|
|
|
|
|
}
|