2023-01-06 21:59:59 +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-06 21:59:59 +08:00
|
|
|
|
//
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
|
|
//go:build windows
|
|
|
|
|
// +build windows
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// 基于VF组件系统托盘 - windows 平台
|
|
|
|
|
// Html + CSS + JavaScript实现
|
2023-01-06 21:59:59 +08:00
|
|
|
|
package cef
|
|
|
|
|
|
2023-01-07 11:48:19 +08:00
|
|
|
|
import (
|
2023-01-27 21:34:56 +08:00
|
|
|
|
"github.com/energye/energy/common"
|
2023-01-07 16:12:00 +08:00
|
|
|
|
"github.com/energye/energy/common/assetserve"
|
|
|
|
|
"github.com/energye/energy/consts"
|
|
|
|
|
"github.com/energye/energy/logger"
|
2023-01-07 11:48:19 +08:00
|
|
|
|
"github.com/energye/golcl/lcl"
|
|
|
|
|
"github.com/energye/golcl/lcl/types"
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// 创建系统托盘
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func newViewsFrameTray(owner lcl.IComponent, width, height int32, url string) *ViewsFrameTray {
|
|
|
|
|
var tray = &ViewsFrameTray{}
|
2023-01-07 11:48:19 +08:00
|
|
|
|
cc := NewChromiumConfig()
|
2023-01-07 16:12:00 +08:00
|
|
|
|
cc.SetEnableMenu(false)
|
2023-01-07 11:48:19 +08:00
|
|
|
|
wp := NewWindowProperty()
|
2023-01-07 16:12:00 +08:00
|
|
|
|
wp.Title = ""
|
2023-01-07 11:48:19 +08:00
|
|
|
|
wp.Url = url
|
|
|
|
|
wp.Width = width
|
|
|
|
|
wp.Height = height
|
2023-01-07 16:12:00 +08:00
|
|
|
|
wp.X = -width
|
|
|
|
|
wp.Y = -height
|
2023-01-07 11:48:19 +08:00
|
|
|
|
wp.AlwaysOnTop = true
|
2023-01-23 10:52:40 +08:00
|
|
|
|
wp.EnableMaximize = false
|
|
|
|
|
wp.EnableMinimize = false
|
|
|
|
|
wp.EnableResize = false
|
2023-01-23 19:14:19 +08:00
|
|
|
|
wp.EnableCenterWindow = false
|
2023-01-07 18:42:17 +08:00
|
|
|
|
tray.trayWindow = NewViewsFrameworkBrowserWindow(cc, wp)
|
2023-01-09 12:10:06 +08:00
|
|
|
|
tray.trayWindow.ResetWindowPropertyForEvent()
|
2023-01-07 16:12:00 +08:00
|
|
|
|
tray.trayIcon = lcl.NewTrayIcon(owner)
|
|
|
|
|
tray.trayIcon.SetVisible(true)
|
|
|
|
|
tray.x = wp.X
|
|
|
|
|
tray.y = wp.Y
|
|
|
|
|
tray.w = wp.Width
|
|
|
|
|
tray.h = wp.Height
|
|
|
|
|
tray.registerMouseEvent()
|
|
|
|
|
tray.registerChromiumEvent()
|
|
|
|
|
return tray
|
2023-01-07 11:48:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) registerMouseEvent() {
|
2023-01-07 16:12:00 +08:00
|
|
|
|
m.trayWindow.WindowComponent().SetOnWindowActivationChanged(func(sender lcl.IObject, window *ICefWindow, active bool) {
|
|
|
|
|
if active {
|
|
|
|
|
} else {
|
|
|
|
|
m.trayWindow.Hide()
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-01-09 13:11:42 +08:00
|
|
|
|
var isCreateTopLevelWindow = true
|
2023-01-07 11:48:19 +08:00
|
|
|
|
m.trayIcon.SetOnMouseUp(func(sender lcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
if isCreateTopLevelWindow {
|
|
|
|
|
isCreateTopLevelWindow = false
|
|
|
|
|
//m.trayWindow.windowId = BrowserWindow.GetNextWindowNum()
|
|
|
|
|
//m.trayWindow.putChromiumWindowInfo()
|
|
|
|
|
BrowserWindow.setOrIncNextWindowNum() //明确的生成下一个窗口序号
|
2023-01-07 16:12:00 +08:00
|
|
|
|
m.trayWindow.CreateTopLevelWindow()
|
|
|
|
|
m.trayWindow.HideTitle()
|
|
|
|
|
m.trayWindow.SetNotInTaskBar()
|
|
|
|
|
m.trayWindow.WindowComponent().SetAlwaysOnTop(true)
|
|
|
|
|
}
|
|
|
|
|
display := m.trayWindow.WindowComponent().Display()
|
|
|
|
|
bounds := display.Bounds()
|
|
|
|
|
var monitorWidth = bounds.Width
|
|
|
|
|
width, height := m.w, m.h
|
|
|
|
|
var mx = x + width
|
|
|
|
|
var my = y + height
|
|
|
|
|
if mx < monitorWidth {
|
|
|
|
|
mx = x
|
|
|
|
|
} else {
|
|
|
|
|
mx = x - width
|
|
|
|
|
}
|
|
|
|
|
if my > m.h {
|
|
|
|
|
my = y
|
|
|
|
|
}
|
|
|
|
|
if my > height {
|
|
|
|
|
my = y - height
|
|
|
|
|
}
|
|
|
|
|
var ret bool
|
|
|
|
|
if m.mouseUp != nil {
|
|
|
|
|
ret = m.mouseUp(sender, button, shift, x, y)
|
|
|
|
|
}
|
|
|
|
|
if !ret {
|
|
|
|
|
if button == types.MbRight {
|
|
|
|
|
m.trayWindow.WindowComponent().SetBounds(NewCefRect(mx, my, width, height))
|
|
|
|
|
m.trayWindow.Show()
|
|
|
|
|
m.trayWindow.BrowserViewComponent().RequestFocus()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) registerChromiumEvent() {
|
2023-01-07 16:12:00 +08:00
|
|
|
|
m.trayWindow.Chromium().SetOnBeforeContextMenu(func(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, params *ICefContextMenuParams, model *ICefMenuModel) {
|
|
|
|
|
model.Clear()
|
|
|
|
|
})
|
|
|
|
|
m.trayWindow.Chromium().SetOnBeforeResourceLoad(func(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, request *ICefRequest, callback *ICefCallback, result *consts.TCefReturnValue) {
|
|
|
|
|
if assetserve.AssetsServerHeaderKeyValue != "" {
|
|
|
|
|
request.SetHeaderByName(assetserve.AssetsServerHeaderKeyName, assetserve.AssetsServerHeaderKeyValue, true)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
m.trayWindow.Chromium().SetOnBeforeClose(func(sender lcl.IObject, browser *ICefBrowser) {
|
|
|
|
|
logger.Debug("tray.chromium.onBeforeClose")
|
|
|
|
|
m.close()
|
|
|
|
|
})
|
2023-02-26 21:17:28 +08:00
|
|
|
|
m.trayWindow.Chromium().SetOnProcessMessageReceived(func(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, sourceProcess consts.CefProcessId, message *ICefProcessMessage) bool {
|
2023-01-07 16:12:00 +08:00
|
|
|
|
return false
|
2023-01-07 11:48:19 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// AsSysTray 尝试转换为 SysTray 组件托盘,如果创建的是其它类型托盘返回nil
|
2023-01-24 18:55:02 +08:00
|
|
|
|
func (m *ViewsFrameTray) 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 *ViewsFrameTray) AsViewsFrameTray() *ViewsFrameTray {
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// AsCEFTray 尝试转换为 LCL+CEF 组件托盘, 如果创建的是其它类型托盘返回nil
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) AsCEFTray() *CEFTray {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// AsLCLTray 尝试转换为 LCL 组件托盘, 如果创建的是其它类型托盘返回nil
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) AsLCLTray() *LCLTray {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// Show 显示/启动 托盘
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) Show() {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
m.trayWindow.Show()
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// Hide 隐藏 托盘
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) Hide() {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
m.trayWindow.Hide()
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) close() {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
if m.isClosing {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
m.trayIcon.SetVisible(false)
|
|
|
|
|
m.Hide()
|
2023-01-09 18:32:21 +08:00
|
|
|
|
m.trayIcon.Free()
|
2023-01-09 13:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnDblClick 设置双击事件
|
2023-01-10 19:52:57 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetOnDblClick(fn TrayICONClick) {
|
|
|
|
|
m.trayIcon.SetOnDblClick(func(sender lcl.IObject) {
|
|
|
|
|
fn()
|
|
|
|
|
})
|
2023-01-09 13:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnClick 设置单击事件
|
2023-01-10 19:52:57 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetOnClick(fn TrayICONClick) {
|
|
|
|
|
m.trayIcon.SetOnClick(func(sender lcl.IObject) {
|
|
|
|
|
fn()
|
|
|
|
|
})
|
2023-01-09 13:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnMouseUp 鼠标抬起事件
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetOnMouseUp(fn TMouseEvent) {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
m.mouseUp = fn
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnMouseDown 鼠标按下事件
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetOnMouseDown(fn lcl.TMouseEvent) {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
m.trayIcon.SetOnMouseDown(fn)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetOnMouseMove 鼠标移动事件
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetOnMouseMove(fn lcl.TMouseMoveEvent) {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
m.trayIcon.SetOnMouseMove(fn)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// Visible 显示状态
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) Visible() bool {
|
2023-01-28 13:22:11 +08:00
|
|
|
|
return m.visible
|
2023-01-09 13:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetVisible 设置显示状态
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetVisible(v bool) {
|
2023-01-28 13:22:11 +08:00
|
|
|
|
m.visible = v
|
2023-01-09 13:11:42 +08:00
|
|
|
|
m.trayIcon.SetVisible(v)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetHint 设置提示
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetHint(value string) {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
m.trayIcon.SetHint(value)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:42:17 +08:00
|
|
|
|
// SetTitle 设置标题 - 空函数
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetTitle(title string) {
|
2023-01-09 13:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 显示系统通知
|
2023-01-27 21:34:56 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// title 标题
|
2023-01-27 21:34:56 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// content 内容
|
2023-01-27 21:34:56 +08:00
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// timeout 显示时间(毫秒)
|
2023-01-27 21:34:56 +08:00
|
|
|
|
func (m *ViewsFrameTray) Notice(title, content string, timeout int32) {
|
|
|
|
|
if common.IsWindows() {
|
|
|
|
|
notification(m.trayIcon, title, content, timeout)
|
|
|
|
|
} else {
|
|
|
|
|
notification(nil, title, content, timeout)
|
|
|
|
|
}
|
2023-01-07 11:48:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 设置托盘图标
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetIconFS(iconResourcePath string) {
|
2023-01-07 16:12:00 +08:00
|
|
|
|
m.trayIcon.Icon().LoadFromFSFile(iconResourcePath)
|
2023-01-07 11:48:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 设置托盘图标
|
2023-01-09 18:32:21 +08:00
|
|
|
|
func (m *ViewsFrameTray) SetIcon(iconResourcePath string) {
|
2023-01-07 16:12:00 +08:00
|
|
|
|
m.trayIcon.Icon().LoadFromFile(iconResourcePath)
|
2023-01-07 11:48:19 +08:00
|
|
|
|
}
|