2021-08-01 22:14:33 +10:00
|
|
|
/*
|
|
|
|
|
_ __ _ __
|
|
|
|
|
| | / /___ _(_) /____
|
|
|
|
|
| | /| / / __ `/ / / ___/
|
|
|
|
|
| |/ |/ / /_/ / / (__ )
|
|
|
|
|
|__/|__/\__,_/_/_/____/
|
2021-08-27 21:11:03 +10:00
|
|
|
The electron alternative for Go
|
2021-08-01 22:14:33 +10:00
|
|
|
(c) Lea Anthony 2019-present
|
|
|
|
|
*/
|
|
|
|
|
/* jshint esversion: 9 */
|
|
|
|
|
import * as Log from './log';
|
2021-09-14 20:24:14 +10:00
|
|
|
import {eventListeners, EventsEmit, EventsNotify, EventsOff, EventsOn, EventsOnce, EventsOnMultiple} from './events';
|
2022-04-20 20:28:41 +10:00
|
|
|
import {Call, Callback, callbacks} from './calls';
|
2021-09-14 20:24:14 +10:00
|
|
|
import {SetBindings} from "./bindings";
|
2021-09-09 20:10:18 +10:00
|
|
|
import * as Window from "./window";
|
2022-07-16 05:33:37 +03:00
|
|
|
import * as Screen from "./screen";
|
2021-09-13 12:58:27 +08:00
|
|
|
import * as Browser from "./browser";
|
2023-01-05 06:41:07 +01:00
|
|
|
import * as Clipboard from "./clipboard";
|
2023-06-27 23:35:02 +03:00
|
|
|
import * as ContextMenu from "./contextmenu";
|
2021-08-01 22:14:33 +10:00
|
|
|
|
2021-09-13 19:35:14 +10:00
|
|
|
|
|
|
|
|
export function Quit() {
|
|
|
|
|
window.WailsInvoke('Q');
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 20:59:49 +10:00
|
|
|
export function Show() {
|
|
|
|
|
window.WailsInvoke('S');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function Hide() {
|
|
|
|
|
window.WailsInvoke('H');
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 20:28:41 +10:00
|
|
|
export function Environment() {
|
|
|
|
|
return Call(":wails:Environment");
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 20:10:18 +10:00
|
|
|
// The JS runtime
|
2021-08-01 22:14:33 +10:00
|
|
|
window.runtime = {
|
|
|
|
|
...Log,
|
2021-09-09 20:10:18 +10:00
|
|
|
...Window,
|
2021-09-13 12:58:27 +08:00
|
|
|
...Browser,
|
2022-07-16 05:33:37 +03:00
|
|
|
...Screen,
|
2023-01-05 06:41:07 +01:00
|
|
|
...Clipboard,
|
2021-08-01 22:14:33 +10:00
|
|
|
EventsOn,
|
|
|
|
|
EventsOnce,
|
|
|
|
|
EventsOnMultiple,
|
|
|
|
|
EventsEmit,
|
|
|
|
|
EventsOff,
|
2022-04-20 20:28:41 +10:00
|
|
|
Environment,
|
2022-07-20 20:59:49 +10:00
|
|
|
Show,
|
|
|
|
|
Hide,
|
2021-09-13 19:35:14 +10:00
|
|
|
Quit
|
2021-08-01 22:14:33 +10:00
|
|
|
};
|
|
|
|
|
|
2021-09-09 20:10:18 +10:00
|
|
|
// Internal wails endpoints
|
2021-08-01 22:14:33 +10:00
|
|
|
window.wails = {
|
2021-08-15 20:16:14 +10:00
|
|
|
Callback,
|
|
|
|
|
EventsNotify,
|
|
|
|
|
SetBindings,
|
2021-08-22 23:05:22 +10:00
|
|
|
eventListeners,
|
2021-10-12 20:45:53 +11:00
|
|
|
callbacks,
|
|
|
|
|
flags: {
|
|
|
|
|
disableScrollbarDrag: false,
|
2023-06-27 23:35:02 +03:00
|
|
|
disableDefaultContextMenu: false,
|
2022-01-12 20:34:37 +11:00
|
|
|
enableResize: false,
|
2022-02-01 20:18:46 +11:00
|
|
|
defaultCursor: null,
|
2022-04-09 10:27:54 +10:00
|
|
|
borderThickness: 6,
|
2022-09-10 14:33:35 +10:00
|
|
|
shouldDrag: false,
|
2023-03-20 08:45:21 +01:00
|
|
|
deferDragToMouseMove: true,
|
2022-09-10 14:33:35 +10:00
|
|
|
cssDragProperty: "--wails-draggable",
|
|
|
|
|
cssDragValue: "drag",
|
2022-01-12 20:34:37 +11:00
|
|
|
}
|
2021-08-01 22:14:33 +10:00
|
|
|
};
|
|
|
|
|
|
2021-09-09 20:10:18 +10:00
|
|
|
// Set the bindings
|
2022-09-13 10:05:37 +10:00
|
|
|
if (window.wailsbindings) {
|
|
|
|
|
window.wails.SetBindings(window.wailsbindings);
|
|
|
|
|
delete window.wails.SetBindings;
|
|
|
|
|
}
|
2021-08-28 14:51:12 +10:00
|
|
|
|
2023-06-27 14:31:08 +03:00
|
|
|
// (bool) This is evaluated at build time in package.json
|
|
|
|
|
if (!DEBUG) {
|
2021-08-28 14:51:12 +10:00
|
|
|
delete window.wailsbindings;
|
|
|
|
|
}
|
2021-08-15 20:16:14 +10:00
|
|
|
|
2022-09-10 14:33:35 +10:00
|
|
|
let dragTest = function (e) {
|
2022-10-18 10:28:59 +02:00
|
|
|
var val = window.getComputedStyle(e.target).getPropertyValue(window.wails.flags.cssDragProperty);
|
|
|
|
|
if (val) {
|
|
|
|
|
val = val.trim();
|
|
|
|
|
}
|
2023-01-16 10:56:13 +01:00
|
|
|
|
|
|
|
|
if (val !== window.wails.flags.cssDragValue) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.buttons !== 1) {
|
|
|
|
|
// Do not start dragging if not the primary button has been clicked.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.detail !== 1) {
|
|
|
|
|
// Do not start dragging if more than once has been clicked, e.g. when double clicking
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2022-08-18 20:39:05 +10:00
|
|
|
};
|
|
|
|
|
|
2022-09-12 21:56:43 +10:00
|
|
|
window.wails.setCSSDragProperties = function (property, value) {
|
2022-09-10 14:33:35 +10:00
|
|
|
window.wails.flags.cssDragProperty = property;
|
|
|
|
|
window.wails.flags.cssDragValue = value;
|
|
|
|
|
}
|
2022-08-18 20:39:05 +10:00
|
|
|
|
2021-08-15 20:16:14 +10:00
|
|
|
window.addEventListener('mousedown', (e) => {
|
2021-11-22 06:47:09 +11:00
|
|
|
|
|
|
|
|
// Check for resizing
|
2022-01-12 20:34:37 +11:00
|
|
|
if (window.wails.flags.resizeEdge) {
|
2021-11-22 06:47:09 +11:00
|
|
|
window.WailsInvoke("resize:" + window.wails.flags.resizeEdge);
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 20:39:05 +10:00
|
|
|
if (dragTest(e)) {
|
|
|
|
|
if (window.wails.flags.disableScrollbarDrag) {
|
|
|
|
|
// This checks for clicks on the scroll bar
|
|
|
|
|
if (e.offsetX > e.target.clientWidth || e.offsetY > e.target.clientHeight) {
|
|
|
|
|
return;
|
2021-10-12 20:45:53 +11:00
|
|
|
}
|
2021-08-15 20:16:14 +10:00
|
|
|
}
|
2023-01-16 10:56:13 +01:00
|
|
|
if (window.wails.flags.deferDragToMouseMove) {
|
2023-03-20 08:45:21 +01:00
|
|
|
window.wails.flags.shouldDrag = true;
|
2023-01-16 10:56:13 +01:00
|
|
|
} else {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
window.WailsInvoke("drag");
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
window.wails.flags.shouldDrag = false;
|
2021-08-15 20:16:14 +10:00
|
|
|
}
|
2023-01-16 10:56:13 +01:00
|
|
|
});
|
2022-08-18 20:39:05 +10:00
|
|
|
|
2023-01-16 10:56:13 +01:00
|
|
|
window.addEventListener('mouseup', () => {
|
|
|
|
|
window.wails.flags.shouldDrag = false;
|
2021-08-15 20:16:14 +10:00
|
|
|
});
|
2021-10-19 20:06:18 +11:00
|
|
|
|
2021-11-22 06:47:09 +11:00
|
|
|
function setResize(cursor) {
|
2023-01-10 13:19:09 +01:00
|
|
|
document.documentElement.style.cursor = cursor || window.wails.flags.defaultCursor;
|
2021-11-22 06:47:09 +11:00
|
|
|
window.wails.flags.resizeEdge = cursor;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 20:34:37 +11:00
|
|
|
window.addEventListener('mousemove', function (e) {
|
2022-08-08 17:27:23 +10:00
|
|
|
if (window.wails.flags.shouldDrag) {
|
2023-03-20 08:45:21 +01:00
|
|
|
window.wails.flags.shouldDrag = false;
|
2023-01-16 10:56:13 +01:00
|
|
|
let mousePressed = e.buttons !== undefined ? e.buttons : e.which;
|
2023-03-20 08:45:21 +01:00
|
|
|
if (mousePressed > 0) {
|
2023-01-16 10:56:13 +01:00
|
|
|
window.WailsInvoke("drag");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-08-08 17:27:23 +10:00
|
|
|
}
|
2021-11-22 06:47:09 +11:00
|
|
|
if (!window.wails.flags.enableResize) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (window.wails.flags.defaultCursor == null) {
|
2023-01-10 13:19:09 +01:00
|
|
|
window.wails.flags.defaultCursor = document.documentElement.style.cursor;
|
2021-11-22 06:47:09 +11:00
|
|
|
}
|
2022-02-01 20:18:46 +11:00
|
|
|
if (window.outerWidth - e.clientX < window.wails.flags.borderThickness && window.outerHeight - e.clientY < window.wails.flags.borderThickness) {
|
2023-01-10 13:19:09 +01:00
|
|
|
document.documentElement.style.cursor = "se-resize";
|
2021-11-22 06:47:09 +11:00
|
|
|
}
|
2022-02-01 20:18:46 +11:00
|
|
|
let rightBorder = window.outerWidth - e.clientX < window.wails.flags.borderThickness;
|
|
|
|
|
let leftBorder = e.clientX < window.wails.flags.borderThickness;
|
|
|
|
|
let topBorder = e.clientY < window.wails.flags.borderThickness;
|
|
|
|
|
let bottomBorder = window.outerHeight - e.clientY < window.wails.flags.borderThickness;
|
2021-11-22 06:47:09 +11:00
|
|
|
|
|
|
|
|
// If we aren't on an edge, but were, reset the cursor to default
|
|
|
|
|
if (!leftBorder && !rightBorder && !topBorder && !bottomBorder && window.wails.flags.resizeEdge !== undefined) {
|
|
|
|
|
setResize();
|
|
|
|
|
} else if (rightBorder && bottomBorder) setResize("se-resize");
|
|
|
|
|
else if (leftBorder && bottomBorder) setResize("sw-resize");
|
|
|
|
|
else if (leftBorder && topBorder) setResize("nw-resize");
|
|
|
|
|
else if (topBorder && rightBorder) setResize("ne-resize");
|
|
|
|
|
else if (leftBorder) setResize("w-resize");
|
|
|
|
|
else if (topBorder) setResize("n-resize");
|
|
|
|
|
else if (bottomBorder) setResize("s-resize");
|
|
|
|
|
else if (rightBorder) setResize("e-resize");
|
|
|
|
|
|
2022-01-12 20:34:37 +11:00
|
|
|
});
|
2021-11-22 06:47:09 +11:00
|
|
|
|
2021-10-19 20:06:18 +11:00
|
|
|
// Setup context menu hook
|
|
|
|
|
window.addEventListener('contextmenu', function (e) {
|
2023-06-27 23:35:02 +03:00
|
|
|
// always show the contextmenu in debug & dev
|
|
|
|
|
if (DEBUG) return;
|
|
|
|
|
|
|
|
|
|
if (window.wails.flags.disableDefaultContextMenu) {
|
2021-10-19 20:06:18 +11:00
|
|
|
e.preventDefault();
|
2023-06-27 23:35:02 +03:00
|
|
|
} else {
|
|
|
|
|
ContextMenu.processDefaultContextMenu(e);
|
2021-10-19 20:06:18 +11:00
|
|
|
}
|
2022-08-18 20:39:05 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.WailsInvoke("runtime:ready");
|