5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-06 23:00:36 +08:00
wails/v3/internal/runtime/desktop/drag.js
2023-05-17 21:09:27 +10:00

69 lines
1.5 KiB
JavaScript

/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The electron alternative for Go
(c) Lea Anthony 2019-present
*/
/* jshint esversion: 9 */
import {invoke} from "./invoke";
let shouldDrag = false;
export function dragTest(e) {
let val = window.getComputedStyle(e.target).getPropertyValue("--wails-draggable");
if (val) {
val = val.trim();
}
if (val !== "drag") {
return false;
}
// Only process the primary button
if (e.buttons !== 1) {
return false;
}
return e.detail === 1;
}
export function setupDrag() {
window.addEventListener('mousedown', onMouseDown);
window.addEventListener('mousemove', onMouseMove);
window.addEventListener('mouseup', onMouseUp);
}
function onMouseDown(e) {
if (dragTest(e)) {
// Ignore drag on scrollbars
if (e.offsetX > e.target.clientWidth || e.offsetY > e.target.clientHeight) {
return;
}
shouldDrag = true;
} else {
shouldDrag = false;
}
}
function onMouseUp(e) {
document.body.style.cursor = window.wails.previousCursor || 'auto';
shouldDrag = false;
}
function onMouseMove(e) {
if (shouldDrag) {
shouldDrag = false;
let mousePressed = e.buttons !== undefined ? e.buttons : e.which;
if (mousePressed > 0) {
window.wails.previousCursor = document.body.style.cursor;
document.body.style.cursor = 'grab';
invoke("drag");
return;
}
}
}