mirror of
https://gitee.com/energye/energy.git
synced 2024-12-14 09:21:14 +08:00
180 lines
5.6 KiB
Go
180 lines
5.6 KiB
Go
|
//----------------------------------------
|
||
|
//
|
||
|
// Copyright © yanghy. All Rights Reserved.
|
||
|
//
|
||
|
// Licensed under Apache License Version 2.0, January 2004
|
||
|
//
|
||
|
// https://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
//----------------------------------------
|
||
|
|
||
|
//go:build darwin
|
||
|
|
||
|
package cef
|
||
|
|
||
|
import (
|
||
|
"github.com/energye/energy/v2/cef/internal/ipc"
|
||
|
ipcArgument "github.com/energye/energy/v2/cef/ipc/argument"
|
||
|
)
|
||
|
|
||
|
func dragExtensionJS(browser *ICefBrowser, frame *ICefFrame) {
|
||
|
// MacOS只在LCL窗口中使用自定义窗口拖拽, VF窗口默认已实现
|
||
|
// 在MacOS中LCL窗口没有有效的消息事件
|
||
|
var executeJS = `
|
||
|
energyExtension.drag.setEnableDrag(true);
|
||
|
energyExtension.drag.setup();`
|
||
|
frame.ExecuteJavaScript(executeJS, "", 0)
|
||
|
}
|
||
|
|
||
|
func dragExtensionHandler() {
|
||
|
energyExtensionHandler := V8HandlerRef.New()
|
||
|
energyExtensionHandler.Execute(func(name string, object *ICefV8Value, arguments *TCefV8ValueArray, retVal *ResultV8Value, exception *ResultString) bool {
|
||
|
//fmt.Println("Execute", name, application.IsMessageLoop(), application.SingleProcess())
|
||
|
if name == mouseUp {
|
||
|
message := &ipcArgument.List{
|
||
|
Id: -1,
|
||
|
BId: ipc.RenderChan().BrowserId(),
|
||
|
Name: internalIPCDRAG,
|
||
|
Data: &drag{T: dragUp},
|
||
|
}
|
||
|
//fmt.Println("up")
|
||
|
ipc.RenderChan().IPC().Send(message.Bytes())
|
||
|
} else if name == mouseDown {
|
||
|
var dx, dy int32
|
||
|
if arguments.Size() > 0 {
|
||
|
point := arguments.Get(0)
|
||
|
v8ValX := point.getValueByKey("x")
|
||
|
v8ValY := point.getValueByKey("y")
|
||
|
dx = v8ValX.GetIntValue()
|
||
|
dy = v8ValY.GetIntValue()
|
||
|
v8ValX.Free()
|
||
|
v8ValY.Free()
|
||
|
point.Free()
|
||
|
}
|
||
|
//fmt.Println("down xy:", dx, dy)
|
||
|
message := &ipcArgument.List{
|
||
|
Id: -1,
|
||
|
BId: ipc.RenderChan().BrowserId(),
|
||
|
Name: internalIPCDRAG,
|
||
|
Data: &drag{T: dragDown, X: dx, Y: dy},
|
||
|
}
|
||
|
ipc.RenderChan().IPC().Send(message.Bytes())
|
||
|
} else if name == mouseMove {
|
||
|
var mx, my int32
|
||
|
if arguments.Size() > 0 {
|
||
|
point := arguments.Get(0)
|
||
|
v8ValX := point.getValueByKey("x")
|
||
|
v8ValY := point.getValueByKey("y")
|
||
|
mx = v8ValX.GetIntValue()
|
||
|
my = v8ValY.GetIntValue()
|
||
|
v8ValX.Free()
|
||
|
v8ValY.Free()
|
||
|
point.Free()
|
||
|
}
|
||
|
//fmt.Println("move xy:", mx, my)
|
||
|
message := &ipcArgument.List{
|
||
|
Id: -1,
|
||
|
BId: ipc.RenderChan().BrowserId(),
|
||
|
Name: internalIPCDRAG,
|
||
|
Data: &drag{T: dragMove, X: mx, Y: my},
|
||
|
}
|
||
|
ipc.RenderChan().IPC().Send(message.Bytes())
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
})
|
||
|
var code = `
|
||
|
let energyExtension;
|
||
|
if (!energyExtension) {
|
||
|
energyExtension = {
|
||
|
drag: {
|
||
|
enableDrag: false,
|
||
|
shouldDrag: false,
|
||
|
cssDragProperty: "--webkit-app-region",
|
||
|
cssDragValue: "drag",
|
||
|
defaultCursor: null
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
(function () {
|
||
|
energyExtension.drag.war = function (e) {
|
||
|
let v = window.getComputedStyle(e.target).getPropertyValue(energyExtension.drag.cssDragProperty);
|
||
|
if (v) {
|
||
|
v = v.trim();
|
||
|
if (v !== energyExtension.drag.cssDragValue || e.buttons !== 1) {
|
||
|
return false;
|
||
|
}
|
||
|
return e.detail === 1;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
energyExtension.drag.mouseMove = function (e) {
|
||
|
if (!energyExtension.drag.enableDrag || !energyExtension.drag.shouldDrag) {
|
||
|
return
|
||
|
}
|
||
|
native function mouseMove();
|
||
|
mouseMove({x: e.screenX, y: e.screenY});
|
||
|
}
|
||
|
energyExtension.drag.mouseUp = function (e) {
|
||
|
if (!energyExtension.drag.enableDrag || (energyExtension.drag.goos === "darwin" && !energyExtension.drag.shouldDrag)) {
|
||
|
return
|
||
|
}
|
||
|
energyExtension.drag.shouldDrag = false;
|
||
|
//document.body.style.cursor = "default";
|
||
|
native function mouseUp();
|
||
|
mouseUp();
|
||
|
}
|
||
|
energyExtension.drag.mouseDown = function (e) {
|
||
|
if (!energyExtension.drag.enableDrag || ((e.offsetX > e.target.clientWidth || e.offsetY > e.target.clientHeight))) {
|
||
|
return
|
||
|
}
|
||
|
if (energyExtension.drag.war(e)) {
|
||
|
e.preventDefault();
|
||
|
energyExtension.drag.shouldDrag = true;
|
||
|
native function mouseDown();
|
||
|
mouseDown({x: e.screenX, y: e.screenY});
|
||
|
} else {
|
||
|
energyExtension.drag.shouldDrag = false;
|
||
|
}
|
||
|
}
|
||
|
energyExtension.drag.setEnableDrag = function (v) {
|
||
|
energyExtension.drag.enableDrag = v;
|
||
|
}
|
||
|
energyExtension.drag.setup = function () {
|
||
|
if (!energyExtension.drag.enableDrag) {
|
||
|
return;
|
||
|
}
|
||
|
window.addEventListener("mousemove", energyExtension.drag.mouseMove);
|
||
|
window.addEventListener("mousedown", energyExtension.drag.mouseDown);
|
||
|
window.addEventListener("mouseup", energyExtension.drag.mouseUp);
|
||
|
}
|
||
|
})();
|
||
|
`
|
||
|
// 注册 EnergyExtension JS
|
||
|
RegisterExtension("energyExtension", code, energyExtensionHandler)
|
||
|
}
|
||
|
|
||
|
func (m *drag) drag() {
|
||
|
if m.T == dragUp {
|
||
|
if m.window.IsLCL() {
|
||
|
bw := m.window.AsLCLBrowserWindow().BrowserWindow()
|
||
|
bw.drag = nil
|
||
|
m.window = nil
|
||
|
}
|
||
|
} else if m.T == dragDown {
|
||
|
m.dx = m.X
|
||
|
m.dy = m.Y
|
||
|
point := m.window.Point()
|
||
|
m.wx = point.X
|
||
|
m.wy = point.Y
|
||
|
} else if m.T == dragMove {
|
||
|
m.mx = m.X
|
||
|
m.my = m.Y
|
||
|
if m.window.IsLCL() {
|
||
|
x := m.wx + (m.mx - m.dx)
|
||
|
y := m.wy + (m.my - m.dy)
|
||
|
m.window.SetPoint(x, y)
|
||
|
}
|
||
|
}
|
||
|
}
|