5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 17:22:01 +08:00
wails/v2/internal/frontend/runtime/desktop/main.js
Jakub Peleška 022a5ffec4
Fix Drag & Drop JS runtime (#3516)
* Feature/1090 native drag and drop for file and folder (#3203)

* implement basic dnd for linux

* implemented windows

* progress changed linux handling and added coordinates to drop

* progress fix drop coordinates on windows

* progress remove log from windows

* progress move js

* update js after merge

* fix event listener registration

* fix segfault on non file drag

* remove logs, fix coordinates

* minor changes, simplify to drop only

* rename EnableWails -> EnableFileDrop

* add documentation (PR id missing)

* add PR id to changelog

* fix remove casting from malloc

* fix nil check for OnFileDrop's callback

* fix nil check for OnFileDrop skip event when nil

* add error message for nil callback in OnFileDrop

---------

Co-authored-by: lyimmi <lelvente.zambo@gmail.com>
Co-authored-by: Lea Anthony <lea.anthony@gmail.com>

* implement native drag and drop for macOS (#3250)

* implement native drag and drop for macOS

* update docs

* add to changelog

* update docs (macOS is supported)

* Fix windows DragAndDrop options

* Fix class unset on dragleave for full frame elements

* improve class unset (nested elements and borders case)

* Fix runtime drop target detection and CSS class assignment

* Edit changelog

* Fix drag-and-drop options in references

* Update v2/internal/frontend/desktop/darwin/WailsWebView.m

* Update v2/internal/frontend/desktop/darwin/WailsWebView.m

---------

Co-authored-by: Zámbó, Levente <levente.zambo@gmail.com>
Co-authored-by: lyimmi <lelvente.zambo@gmail.com>
Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
Co-authored-by: Andrey Pshenkin <andrey.pshenkin@gmail.com>
Co-authored-by: Pavel Binar <pavel@beamtransfer.io>
2024-06-10 15:15:28 +10:00

209 lines
5.9 KiB
JavaScript

/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The electron alternative for Go
(c) Lea Anthony 2019-present
*/
/* jshint esversion: 9 */
import * as Log from './log';
import {eventListeners, EventsEmit, EventsNotify, EventsOff, EventsOn, EventsOnce, EventsOnMultiple} from './events';
import {Call, Callback, callbacks} from './calls';
import {SetBindings} from "./bindings";
import * as Window from "./window";
import * as Screen from "./screen";
import * as Browser from "./browser";
import * as Clipboard from "./clipboard";
import * as DragAndDrop from "./draganddrop";
import * as ContextMenu from "./contextmenu";
export function Quit() {
window.WailsInvoke('Q');
}
export function Show() {
window.WailsInvoke('S');
}
export function Hide() {
window.WailsInvoke('H');
}
export function Environment() {
return Call(":wails:Environment");
}
// The JS runtime
window.runtime = {
...Log,
...Window,
...Browser,
...Screen,
...Clipboard,
...DragAndDrop,
EventsOn,
EventsOnce,
EventsOnMultiple,
EventsEmit,
EventsOff,
Environment,
Show,
Hide,
Quit
};
// Internal wails endpoints
window.wails = {
Callback,
EventsNotify,
SetBindings,
eventListeners,
callbacks,
flags: {
disableScrollbarDrag: false,
disableDefaultContextMenu: false,
enableResize: false,
defaultCursor: null,
borderThickness: 6,
shouldDrag: false,
deferDragToMouseMove: true,
cssDragProperty: "--wails-draggable",
cssDragValue: "drag",
cssDropProperty: "--wails-drop-target",
cssDropValue: "drop",
enableWailsDragAndDrop: false,
}
};
// Set the bindings
if (window.wailsbindings) {
window.wails.SetBindings(window.wailsbindings);
delete window.wails.SetBindings;
}
// (bool) This is evaluated at build time in package.json
if (!DEBUG) {
delete window.wailsbindings;
}
let dragTest = function (e) {
var val = window.getComputedStyle(e.target).getPropertyValue(window.wails.flags.cssDragProperty);
if (val) {
val = val.trim();
}
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;
};
window.wails.setCSSDragProperties = function (property, value) {
window.wails.flags.cssDragProperty = property;
window.wails.flags.cssDragValue = value;
}
window.wails.setCSSDropProperties = function (property, value) {
window.wails.flags.cssDropProperty = property;
window.wails.flags.cssDropValue = value;
}
window.addEventListener('mousedown', (e) => {
// Check for resizing
if (window.wails.flags.resizeEdge) {
window.WailsInvoke("resize:" + window.wails.flags.resizeEdge);
e.preventDefault();
return;
}
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;
}
}
if (window.wails.flags.deferDragToMouseMove) {
window.wails.flags.shouldDrag = true;
} else {
e.preventDefault()
window.WailsInvoke("drag");
}
return;
} else {
window.wails.flags.shouldDrag = false;
}
});
window.addEventListener('mouseup', () => {
window.wails.flags.shouldDrag = false;
});
function setResize(cursor) {
document.documentElement.style.cursor = cursor || window.wails.flags.defaultCursor;
window.wails.flags.resizeEdge = cursor;
}
window.addEventListener('mousemove', function (e) {
if (window.wails.flags.shouldDrag) {
window.wails.flags.shouldDrag = false;
let mousePressed = e.buttons !== undefined ? e.buttons : e.which;
if (mousePressed > 0) {
window.WailsInvoke("drag");
return;
}
}
if (!window.wails.flags.enableResize) {
return;
}
if (window.wails.flags.defaultCursor == null) {
window.wails.flags.defaultCursor = document.documentElement.style.cursor;
}
if (window.outerWidth - e.clientX < window.wails.flags.borderThickness && window.outerHeight - e.clientY < window.wails.flags.borderThickness) {
document.documentElement.style.cursor = "se-resize";
}
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;
// 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");
});
// Setup context menu hook
window.addEventListener('contextmenu', function (e) {
// always show the contextmenu in debug & dev
if (DEBUG) return;
if (window.wails.flags.disableDefaultContextMenu) {
e.preventDefault();
} else {
ContextMenu.processDefaultContextMenu(e);
}
});
window.WailsInvoke("runtime:ready");