energy/cef/types-window.go

472 lines
12 KiB
Go
Raw Normal View History

//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
2022-12-30 18:22:21 +08:00
package cef
import (
2023-07-04 15:11:45 +08:00
"errors"
"github.com/energye/energy/v2/cef/internal/def"
2023-05-31 18:00:34 +08:00
"github.com/energye/energy/v2/common/imports"
"github.com/energye/energy/v2/consts"
"github.com/energye/energy/v2/types"
2022-12-30 18:22:21 +08:00
"github.com/energye/golcl/energy/emfs"
"github.com/energye/golcl/lcl/api"
2023-02-14 15:25:02 +08:00
t "github.com/energye/golcl/lcl/types"
2022-12-30 18:22:21 +08:00
"io/ioutil"
"unsafe"
)
2023-07-04 11:59:45 +08:00
// CefWindowRef -> ICefWindow
var CefWindowRef cefWindow
type cefWindow uintptr
func (*cefWindow) New(windowComponent *TCEFWindowComponent) *ICefWindow {
if !windowComponent.IsValid() {
return nil
}
var result uintptr
imports.Proc(def.ICEFWindowRef_CreateTopLevel).Call(windowComponent.Instance(), uintptr(unsafe.Pointer(&result)))
if result != 0 {
return &ICefWindow{instance: getInstance(result)}
}
return nil
}
2023-03-14 22:20:08 +08:00
func (m *ICefWindow) Instance() uintptr {
2023-07-04 15:11:45 +08:00
if m == nil {
return 0
}
2023-03-14 22:20:08 +08:00
return uintptr(m.instance)
}
func (m *ICefWindow) Free() {
if m.instance != nil {
m.base.Free(m.Instance())
m.instance = nil
}
}
2023-07-04 15:11:45 +08:00
func (m *ICefWindow) IsValid() bool {
if m == nil || m.instance == nil {
return false
}
return true
}
2023-02-20 14:42:17 +08:00
// Show 显示窗口
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) Show() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_Show).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// Hide 显示窗口
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) Hide() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_Hide).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// CenterWindow 根据大小窗口居中
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) CenterWindow(size *TCefSize) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_CenterWindow).Call(m.Instance(), uintptr(unsafe.Pointer(size)))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// Close 关闭窗口, 主窗口调用
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) Close() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_Close).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// IsClosed 是否关闭
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) IsClosed() bool {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return false
}
r1, _, _ := imports.Proc(def.ICEFWindow_IsClosed).Call(m.Instance())
2022-12-30 18:22:21 +08:00
return api.GoBool(r1)
}
2023-02-20 14:42:17 +08:00
// Activate 激活窗口
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) Activate() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_Activate).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// Deactivate 停止激活窗口
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) Deactivate() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_Deactivate).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// IsActive 是否激活
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) IsActive() bool {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return false
}
r1, _, _ := imports.Proc(def.ICEFWindow_IsActive).Call(m.Instance())
2022-12-30 18:22:21 +08:00
return api.GoBool(r1)
}
2023-02-20 14:42:17 +08:00
// BringToTop 将窗口移至最上层
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) BringToTop() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_BringToTop).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// SetAlwaysOnTop 设置窗口是否置顶
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SetAlwaysOnTop(onTop bool) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SetAlwaysOnTop).Call(m.Instance(), api.PascalBool(onTop))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// IsAlwaysOnTop 窗口是否置顶
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) IsAlwaysOnTop() bool {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return false
}
r1, _, _ := imports.Proc(def.ICEFWindow_IsAlwaysOnTop).Call(m.Instance())
2022-12-30 18:22:21 +08:00
return api.GoBool(r1)
}
2023-02-20 14:42:17 +08:00
// WindowState 返回窗口最小化、最大化、全屏状态
2023-02-14 15:25:02 +08:00
func (m *ICefWindow) WindowState() t.TWindowState {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return -1
}
2023-02-14 15:25:02 +08:00
if m.IsMinimized() {
return t.WsMinimized
} else if m.IsMaximized() {
return t.WsMaximized
} else if m.IsFullscreen() {
return t.WsFullScreen
}
return t.WsNormal
}
2023-02-20 14:42:17 +08:00
// Maximize 最大化窗口
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) Maximize() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_Maximize).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// Minimize 最小化窗口
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) Minimize() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_Minimize).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// Restore 窗口还原
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) Restore() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_Restore).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// SetFullscreen 设置窗口全屏
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SetFullscreen(fullscreen bool) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SetFullscreen).Call(m.Instance(), api.PascalBool(fullscreen))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// SetBackgroundColor 设置背景色
2023-07-04 09:08:55 +08:00
func (m *ICefWindow) SetBackgroundColor(rect types.TCefColor) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SetBackgroundColor).Call(m.Instance(), rect.ToPtr())
}
2023-02-20 14:42:17 +08:00
// SetBounds 设置窗口边界
func (m *ICefWindow) SetBounds(rect *TCefRect) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SetBounds).Call(m.Instance(), uintptr(unsafe.Pointer(rect)))
}
2023-02-20 14:42:17 +08:00
// SetSize 设置窗口宽高
func (m *ICefWindow) SetSize(size *TCefSize) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SetSize).Call(m.Instance(), uintptr(unsafe.Pointer(size)))
}
2023-02-20 14:42:17 +08:00
// SetPosition 设置窗口位置
func (m *ICefWindow) SetPosition(point *TCefPoint) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SetPosition).Call(m.Instance(), uintptr(unsafe.Pointer(point)))
}
2023-02-20 14:42:17 +08:00
// IsMaximized 是否最大化
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) IsMaximized() bool {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return false
}
r1, _, _ := imports.Proc(def.ICEFWindow_IsMaximized).Call(m.Instance())
2022-12-30 18:22:21 +08:00
return api.GoBool(r1)
}
2023-02-20 14:42:17 +08:00
// IsMinimized 是否最小化
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) IsMinimized() bool {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return false
}
r1, _, _ := imports.Proc(def.ICEFWindow_IsMinimized).Call(m.Instance())
2022-12-30 18:22:21 +08:00
return api.GoBool(r1)
}
2023-02-20 14:42:17 +08:00
// IsFullscreen 是否全屏
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) IsFullscreen() bool {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return false
}
r1, _, _ := imports.Proc(def.ICEFWindow_IsFullscreen).Call(m.Instance())
2022-12-30 18:22:21 +08:00
return api.GoBool(r1)
}
2023-02-20 14:42:17 +08:00
// SetTitle 设置窗口标题
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SetTitle(title string) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SetTitle).Call(m.Instance(), api.PascalStr(title))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// Title 获取窗口标题
func (m *ICefWindow) Title() string {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return ""
}
r1, _, _ := imports.Proc(def.ICEFWindow_GetTitle).Call(m.Instance())
2022-12-30 18:22:21 +08:00
return api.GoStr(r1)
}
2023-02-20 14:42:17 +08:00
// SetWindowIcon 设置窗口图标
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SetWindowIcon(scaleFactor float32, filename string) error {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return errors.New("invalid")
}
2022-12-30 18:22:21 +08:00
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
2023-07-04 15:11:45 +08:00
imports.Proc(def.ICEFWindow_SetWindowIcon).Call(m.Instance(), uintptr(unsafe.Pointer(&scaleFactor)), uintptr(unsafe.Pointer(&bytes[0])), uintptr(int32(len(bytes))))
2022-12-30 18:22:21 +08:00
return nil
}
2023-02-20 14:42:17 +08:00
// SetWindowIconFS 设置窗口图标
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SetWindowIconFS(scaleFactor float32, filename string) error {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return errors.New("invalid")
}
2022-12-30 18:22:21 +08:00
bytes, err := emfs.GetResources(filename)
if err != nil {
return err
}
2023-07-04 15:11:45 +08:00
imports.Proc(def.ICEFWindow_SetWindowIcon).Call(m.Instance(), uintptr(unsafe.Pointer(&scaleFactor)), uintptr(unsafe.Pointer(&bytes[0])), uintptr(int32(len(bytes))))
2022-12-30 18:22:21 +08:00
return nil
}
2023-02-20 14:42:17 +08:00
// WindowIcon 获取窗口图标
func (m *ICefWindow) WindowIcon() *ICefImage {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return nil
}
2023-03-01 13:38:43 +08:00
var result uintptr
2023-07-04 15:11:45 +08:00
imports.Proc(def.ICEFWindow_GetWindowIcon).Call(m.Instance(), uintptr(unsafe.Pointer(&result)))
2022-12-30 18:22:21 +08:00
return &ICefImage{
2023-03-01 13:38:43 +08:00
instance: unsafe.Pointer(result),
2022-12-30 18:22:21 +08:00
}
}
2023-02-20 14:42:17 +08:00
// WindowAppIcon 获取窗口应用图标
func (m *ICefWindow) WindowAppIcon() *ICefImage {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return nil
}
2023-03-01 13:38:43 +08:00
var result uintptr
2023-07-04 15:11:45 +08:00
imports.Proc(def.ICEFWindow_GetWindowAppIcon).Call(m.Instance(), uintptr(unsafe.Pointer(&result)))
2023-02-20 14:42:17 +08:00
return &ICefImage{
2023-03-01 13:38:43 +08:00
instance: unsafe.Pointer(result),
2023-02-20 14:42:17 +08:00
}
}
// SetWindowAppIcon 设置窗口应用图标
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SetWindowAppIcon(scaleFactor float32, filename string) error {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return errors.New("invalid")
}
2022-12-30 18:22:21 +08:00
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
2023-07-04 15:11:45 +08:00
imports.Proc(def.ICEFWindow_SetWindowAppIcon).Call(m.Instance(), uintptr(unsafe.Pointer(&scaleFactor)), uintptr(unsafe.Pointer(&bytes[0])), uintptr(int32(len(bytes))))
2022-12-30 18:22:21 +08:00
return nil
}
2023-02-20 14:42:17 +08:00
// SetWindowAppIconFS 设置窗口应用图标
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SetWindowAppIconFS(scaleFactor float32, filename string) error {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return errors.New("invalid")
}
2022-12-30 18:22:21 +08:00
bytes, err := emfs.GetResources(filename)
if err != nil {
return err
}
2023-07-04 15:11:45 +08:00
imports.Proc(def.ICEFWindow_SetWindowAppIcon).Call(m.Instance(), uintptr(unsafe.Pointer(&scaleFactor)), uintptr(unsafe.Pointer(&bytes[0])), uintptr(int32(len(bytes))))
2022-12-30 18:22:21 +08:00
return nil
}
2023-07-04 15:11:45 +08:00
// AddOverlayView
func (m *ICefWindow) AddOverlayView(view *ICefView, dockingMode consts.TCefDockingMode) *ICefOverlayController {
if !m.IsValid() {
return nil
}
var result uintptr
imports.Proc(def.ICEFWindow_AddOverlayView).Call(m.Instance(), view.Instance(), uintptr(dockingMode), uintptr(unsafe.Pointer(&result)))
if result != 0 {
return &ICefOverlayController{instance: getInstance(result)}
}
return nil
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// ShowMenu 显示菜单
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) ShowMenu(menuModel *ICefMenuModel, point TCefPoint, anchorPosition consts.TCefMenuAnchorPosition) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_ShowMenu).Call(m.Instance(), menuModel.Instance(), uintptr(unsafe.Pointer(&point)), uintptr(anchorPosition))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// CancelMenu 取消菜单
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) CancelMenu() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_CancelMenu).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// Display
func (m *ICefWindow) Display() *ICefDisplay {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return nil
}
2022-12-30 18:22:21 +08:00
var ret uintptr
2023-07-04 15:11:45 +08:00
imports.Proc(def.ICEFWindow_GetDisplay).Call(m.Instance(), uintptr(unsafe.Pointer(&ret)))
2022-12-30 18:22:21 +08:00
return &ICefDisplay{
instance: unsafe.Pointer(ret),
}
}
2023-02-20 14:42:17 +08:00
// ClientAreaBoundsInScreen 获取客户端所在指定屏幕位置
func (m *ICefWindow) ClientAreaBoundsInScreen() (result TCefRect) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_GetClientAreaBoundsInScreen).Call(m.Instance(), uintptr(unsafe.Pointer(&result)))
2022-12-30 18:22:21 +08:00
return
}
2023-02-20 14:42:17 +08:00
// SetDraggableRegions 设置拖拽区域
func (m *ICefWindow) SetDraggableRegions(regions []TCefDraggableRegion) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SetDraggableRegions).Call(m.Instance(), uintptr(int32(len(regions))), uintptr(unsafe.Pointer(&regions[0])), uintptr(int32(len(regions))))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// WindowHandle 获取窗口句柄
func (m *ICefWindow) WindowHandle() consts.TCefWindowHandle {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return 0
}
r1, _, _ := imports.Proc(def.ICEFWindow_GetWindowHandle).Call(m.Instance())
2022-12-30 18:22:21 +08:00
return consts.TCefWindowHandle(r1)
}
2023-02-20 14:42:17 +08:00
// SendKeyPress 发送键盘事件
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SendKeyPress(keyCode int32, eventFlags uint32) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SendKeyPress).Call(m.Instance(), uintptr(keyCode), uintptr(eventFlags))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// SendMouseMove 发送鼠标移动事件
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SendMouseMove(screenX, screenY int32) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SendMouseMove).Call(m.Instance(), uintptr(screenX), uintptr(screenY))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// SendMouseEvents 发送鼠标事件
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SendMouseEvents(button consts.TCefMouseButtonType, mouseDown, mouseUp bool) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SendMouseEvents).Call(m.Instance(), uintptr(button), api.PascalBool(mouseDown), api.PascalBool(mouseUp))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// SetAccelerator 设置快捷键
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) SetAccelerator(commandId, keyCode int32, shiftPressed, ctrlPressed, altPressed bool) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_SetAccelerator).Call(m.Instance(), uintptr(commandId), uintptr(keyCode), api.PascalBool(shiftPressed), api.PascalBool(ctrlPressed), api.PascalBool(altPressed))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// RemoveAccelerator 移除指定快捷键
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) RemoveAccelerator(commandId int32) {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_RemoveAccelerator).Call(m.Instance(), uintptr(commandId))
2022-12-30 18:22:21 +08:00
}
2023-02-20 14:42:17 +08:00
// RemoveAllAccelerators 移除所有快捷键
2022-12-30 18:22:21 +08:00
func (m *ICefWindow) RemoveAllAccelerators() {
2023-07-04 15:11:45 +08:00
if !m.IsValid() {
return
}
imports.Proc(def.ICEFWindow_RemoveAllAccelerators).Call(m.Instance())
2022-12-30 18:22:21 +08:00
}
func (m *ICefWindow) SetWindow(window *ICefWindow) {
m.instance = window.instance
}