energy/cef/cef-lcl-commponent-tray_windows.go

249 lines
6.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
//
//----------------------------------------
//go:build windows
// +build windows
package cef
import (
. "github.com/energye/energy/common"
2022-11-02 15:56:00 +08:00
"github.com/energye/energy/common/assetserve"
2022-10-04 22:34:57 +08:00
. "github.com/energye/energy/consts"
"github.com/energye/energy/ipc"
2022-12-15 23:13:27 +08:00
"github.com/energye/energy/logger"
2022-10-04 13:21:05 +08:00
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/types"
)
//Cef托盘
type tLCLTrayWindow struct {
2022-10-04 13:21:05 +08:00
*lcl.TForm
owner lcl.IComponent
2022-10-04 13:21:05 +08:00
trayIcon *lcl.TTrayIcon
chromium IChromium
windowParent ITCefWindowParent
2022-10-04 13:21:05 +08:00
x, y, w, h int32
mouseUp TMouseEvent
isClosing bool
url string
}
func newLCLTrayWindow(owner lcl.IComponent, width, height int32, url string) *tLCLTrayWindow {
var trayForm *tLCLTrayWindow
2022-10-04 13:21:05 +08:00
lcl.Application.CreateForm(&trayForm)
trayForm.trayIcon = lcl.NewTrayIcon(owner)
trayForm.trayIcon.SetVisible(true)
trayForm.owner = owner
trayForm.x = -width
trayForm.y = -height
trayForm.w = width
trayForm.h = height
trayForm.url = url
trayForm.onmMouse()
trayForm.createCefTrayWindow()
return trayForm
}
func (m *tLCLTrayWindow) OnFormCreate(sender lcl.IObject) {
2022-10-04 13:21:05 +08:00
m.SetShowInTaskBar(types.StNever)
}
func (m *tLCLTrayWindow) Tray() *Tray {
2022-10-04 13:21:05 +08:00
return nil
}
func (m *tLCLTrayWindow) Show() {
if BrowserWindow.mainBrowserWindow.Chromium() == nil || !BrowserWindow.mainBrowserWindow.Chromium().Initialized() {
2022-10-04 13:21:05 +08:00
return
}
m.TForm.Show()
}
func (m *tLCLTrayWindow) Hide() {
2022-10-04 13:21:05 +08:00
m.TForm.Hide()
}
func (m *tLCLTrayWindow) close() {
2022-11-02 16:18:02 +08:00
if m.isClosing {
return
}
2022-10-04 13:21:05 +08:00
m.Hide()
m.TForm.Close()
}
func (m *tLCLTrayWindow) SetOnDblClick(fn lcl.TNotifyEvent) {
2022-10-04 13:21:05 +08:00
m.trayIcon.SetOnDblClick(fn)
}
func (m *tLCLTrayWindow) SetOnClick(fn lcl.TNotifyEvent) {
2022-10-04 13:21:05 +08:00
m.trayIcon.SetOnClick(fn)
}
func (m *tLCLTrayWindow) SetOnMouseUp(fn TMouseEvent) {
2022-10-04 13:21:05 +08:00
m.mouseUp = fn
}
func (m *tLCLTrayWindow) SetOnMouseDown(fn lcl.TMouseEvent) {
2022-10-04 13:21:05 +08:00
m.trayIcon.SetOnMouseDown(fn)
}
func (m *tLCLTrayWindow) SetOnMouseMove(fn lcl.TMouseMoveEvent) {
2022-10-04 13:21:05 +08:00
m.trayIcon.SetOnMouseMove(fn)
}
func (m *tLCLTrayWindow) Visible() bool {
2022-10-04 13:21:05 +08:00
return m.TForm.Visible()
}
func (m *tLCLTrayWindow) SetVisible(v bool) {
2022-10-04 13:21:05 +08:00
m.trayIcon.SetVisible(v)
}
func (m *tLCLTrayWindow) SetHint(value string) {
2022-10-04 13:21:05 +08:00
m.trayIcon.SetHint(value)
}
func (m *tLCLTrayWindow) SetTitle(title string) {
2022-10-04 13:21:05 +08:00
m.TForm.SetCaption(title)
}
func (m *tLCLTrayWindow) onmMouse() {
2022-10-04 13:21:05 +08:00
QueueAsyncCall(func(id int) {
m.trayIcon.SetOnMouseUp(func(sender lcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) {
var monitor = m.TForm.Monitor()
var monitorWidth = monitor.Width()
width, height := m.TForm.Width(), m.TForm.Height()
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
}
m.TForm.SetBounds(mx, my, width, height)
var ret bool
if m.mouseUp != nil {
ret = m.mouseUp(sender, button, shift, x, y)
}
if !ret {
if button == types.MbRight {
m.Show()
}
}
})
})
}
//设置托盘气泡
//title 气泡标题
//content 气泡内容
//timeout 显示时间(毫秒)
func (m *tLCLTrayWindow) SetBalloon(title, content string, timeout int32) ITray {
2022-10-04 13:21:05 +08:00
m.trayIcon.SetBalloonTitle(title)
m.trayIcon.SetBalloonHint(content)
m.trayIcon.SetBalloonTimeout(timeout)
return m
}
//显示托盘气泡
func (m *tLCLTrayWindow) ShowBalloon() {
2022-10-04 13:21:05 +08:00
m.trayIcon.ShowBalloonHint()
}
func (m *tLCLTrayWindow) createCefTrayWindow() {
2022-10-04 13:21:05 +08:00
m.TForm.SetBorderStyle(types.BsNone)
m.TForm.SetFormStyle(types.FsStayOnTop)
m.TForm.SetBounds(-(m.w * 2), -(m.h * 2), m.w, m.h)
m.TForm.SetOnActivate(func(sender lcl.IObject) {
m.chromium.Initialized()
m.chromium.CreateBrowser(m.windowParent)
})
m.TForm.SetOnWndProc(func(msg *types.TMessage) {
m.TForm.InheritedWndProc(msg)
if msg.Msg == 6 && msg.WParam == 0 && msg.LParam == 0 {
QueueAsyncCall(func(id int) {
if m.isClosing {
return
}
2022-10-04 13:21:05 +08:00
m.TForm.Hide()
})
}
})
m.TForm.SetOnDeactivate(func(sender lcl.IObject) {
if m.isClosing {
return
}
2022-10-04 13:21:05 +08:00
m.TForm.Hide()
})
m.TForm.SetOnCloseQuery(func(sender lcl.IObject, canClose *bool) {
2022-12-15 23:13:27 +08:00
*canClose = true
logger.Debug("tray.window.onCloseQuery canClose:", *canClose)
2022-10-04 13:21:05 +08:00
if m.isClosing {
return
}
m.isClosing = true
m.Hide()
m.chromium.CloseBrowser(true)
2022-11-02 16:18:02 +08:00
m.trayIcon.Free()
2022-10-04 13:21:05 +08:00
})
m.TForm.SetOnClose(func(sender lcl.IObject, action *types.TCloseAction) {
*action = types.CaFree
2022-12-15 23:13:27 +08:00
logger.Debug("tray.window.onClose action:", *action)
2022-10-04 13:21:05 +08:00
})
m.TForm.SetOnShow(func(sender lcl.IObject) {
if m.windowParent != nil {
QueueAsyncCall(func(id int) {
m.windowParent.UpdateSize()
})
}
})
m.windowParent = NewCEFWindow(m.TForm)
m.windowParent.SetParent(m.TForm)
m.windowParent.SetAlign(types.AlClient)
m.windowParent.SetAnchors(types.NewSet(types.AkTop, types.AkLeft, types.AkRight, types.AkBottom))
m.chromium = NewChromium(m.windowParent, nil)
m.chromium.SetOnBeforeContextMenu(func(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, params *ICefContextMenuParams, model *ICefMenuModel) {
model.Clear()
})
m.chromium.SetOnBeforeBrowser(func(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame) bool {
BrowserWindow.setOrIncNextWindowNum(browser.Identifier() + 1)
return false
})
2022-11-02 15:56:00 +08:00
m.chromium.SetOnBeforeResourceLoad(func(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, request *ICefRequest, callback *ICefCallback, result *TCefReturnValue) {
if assetserve.AssetsServerHeaderKeyValue != "" {
request.SetHeaderByName(assetserve.AssetsServerHeaderKeyName, assetserve.AssetsServerHeaderKeyValue, true)
}
})
2022-10-04 13:21:05 +08:00
m.chromium.SetOnClose(func(sender lcl.IObject, browser *ICefBrowser, aAction *TCefCloseBrowsesAction) {
2022-12-15 23:13:27 +08:00
logger.Debug("tray.chromium.onClose")
2022-10-04 13:21:05 +08:00
if IsDarwin() {
m.windowParent.DestroyChildWindow()
}
2022-12-15 23:13:27 +08:00
*aAction = CbaClose
2022-10-04 13:21:05 +08:00
})
m.chromium.SetOnBeforeClose(func(sender lcl.IObject, browser *ICefBrowser) {
2022-12-15 23:13:27 +08:00
logger.Debug("tray.chromium.onBeforeClose")
2022-10-04 13:21:05 +08:00
})
2022-10-04 22:34:57 +08:00
m.chromium.SetOnProcessMessageReceived(func(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, sourceProcess CefProcessId, message *ipc.ICefProcessMessage) bool {
2022-10-04 13:21:05 +08:00
return false
})
m.windowParent.SetChromium(m.chromium, 0)
m.chromium.SetDefaultURL(m.url)
}
//设置托盘图标
func (m *tLCLTrayWindow) SetIcon(iconResourcePath string) {
2022-10-04 13:21:05 +08:00
m.trayIcon.Icon().LoadFromFSFile(iconResourcePath)
}