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 (
|
2022-11-02 12:47:47 +08:00
|
|
|
. "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托盘
|
2023-01-06 21:42:29 +08:00
|
|
|
type tLCLCefTrayForm struct {
|
2022-10-04 13:21:05 +08:00
|
|
|
*lcl.TForm
|
2023-01-06 19:27:12 +08:00
|
|
|
owner lcl.IComponent
|
2022-10-04 13:21:05 +08:00
|
|
|
trayIcon *lcl.TTrayIcon
|
2022-12-01 15:24:22 +08:00
|
|
|
chromium IChromium
|
2023-01-04 18:41:48 +08:00
|
|
|
windowParent ITCefWindowParent
|
2022-10-04 13:21:05 +08:00
|
|
|
x, y, w, h int32
|
|
|
|
mouseUp TMouseEvent
|
|
|
|
isClosing bool
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func newLCLCefTray(owner lcl.IComponent, width, height int32, url string) *tLCLCefTrayForm {
|
|
|
|
var trayForm *tLCLCefTrayForm
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) OnFormCreate(sender lcl.IObject) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.SetShowInTaskBar(types.StNever)
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) Tray() *Tray {
|
2022-10-04 13:21:05 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) Show() {
|
2023-01-06 13:50:44 +08:00
|
|
|
if BrowserWindow.mainBrowserWindow.Chromium() == nil || !BrowserWindow.mainBrowserWindow.Chromium().Initialized() {
|
2022-10-04 13:21:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
m.TForm.Show()
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) Hide() {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.TForm.Hide()
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) 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()
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) SetOnDblClick(fn lcl.TNotifyEvent) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.trayIcon.SetOnDblClick(fn)
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) SetOnClick(fn lcl.TNotifyEvent) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.trayIcon.SetOnClick(fn)
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) SetOnMouseUp(fn TMouseEvent) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.mouseUp = fn
|
|
|
|
}
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) SetOnMouseDown(fn lcl.TMouseEvent) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.trayIcon.SetOnMouseDown(fn)
|
|
|
|
}
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) SetOnMouseMove(fn lcl.TMouseMoveEvent) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.trayIcon.SetOnMouseMove(fn)
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) Visible() bool {
|
2022-10-04 13:21:05 +08:00
|
|
|
return m.TForm.Visible()
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) SetVisible(v bool) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.trayIcon.SetVisible(v)
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) SetHint(value string) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.trayIcon.SetHint(value)
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) SetTitle(title string) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.TForm.SetCaption(title)
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) 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 显示时间(毫秒)
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
//显示托盘气泡
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) ShowBalloon() {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.trayIcon.ShowBalloonHint()
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) 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) {
|
2023-01-04 22:47:30 +08:00
|
|
|
if m.isClosing {
|
|
|
|
return
|
|
|
|
}
|
2022-10-04 13:21:05 +08:00
|
|
|
m.TForm.Hide()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
m.TForm.SetOnDeactivate(func(sender lcl.IObject) {
|
2023-01-04 22:47:30 +08:00
|
|
|
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()
|
|
|
|
} else {
|
2022-12-15 23:13:27 +08:00
|
|
|
//QueueAsyncCall(func(id int) { //主进程执行
|
|
|
|
//m.windowParent.Free()
|
|
|
|
//logger.Debug("tray.chromium.onClose => windowParent.Free")
|
|
|
|
//})
|
2022-10-04 13:21:05 +08:00
|
|
|
}
|
2022-12-15 23:13:27 +08:00
|
|
|
*aAction = CbaClose
|
|
|
|
//*aAction = CbaDelay
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
//设置托盘图标
|
2023-01-06 21:42:29 +08:00
|
|
|
func (m *tLCLCefTrayForm) SetIcon(iconResourcePath string) {
|
2022-10-04 13:21:05 +08:00
|
|
|
m.trayIcon.Icon().LoadFromFSFile(iconResourcePath)
|
|
|
|
}
|