5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-21 19:39:29 +08:00
wails/v2/internal/frontend/runtime/runtime_dev_desktop.js

357 lines
9.5 KiB
JavaScript

(() => {
var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// desktop/log.js
var log_exports = {};
__export(log_exports, {
LogDebug: () => LogDebug,
LogError: () => LogError,
LogFatal: () => LogFatal,
LogInfo: () => LogInfo,
LogLevel: () => LogLevel,
LogPrint: () => LogPrint,
LogTrace: () => LogTrace,
LogWarning: () => LogWarning,
SetLogLevel: () => SetLogLevel
});
function sendLogMessage(level, message) {
window.WailsInvoke("L" + level + message);
}
function LogTrace(message) {
sendLogMessage("T", message);
}
function LogPrint(message) {
sendLogMessage("P", message);
}
function LogDebug(message) {
sendLogMessage("D", message);
}
function LogInfo(message) {
sendLogMessage("I", message);
}
function LogWarning(message) {
sendLogMessage("W", message);
}
function LogError(message) {
sendLogMessage("E", message);
}
function LogFatal(message) {
sendLogMessage("F", message);
}
function SetLogLevel(loglevel) {
sendLogMessage("S", loglevel);
}
var LogLevel = {
TRACE: 1,
DEBUG: 2,
INFO: 3,
WARNING: 4,
ERROR: 5
};
// desktop/events.js
var Listener = class {
constructor(callback, maxCallbacks) {
maxCallbacks = maxCallbacks || -1;
this.Callback = (data) => {
callback.apply(null, data);
if (maxCallbacks === -1) {
return false;
}
maxCallbacks -= 1;
return maxCallbacks === 0;
};
}
};
var eventListeners = {};
function EventsOnMultiple(eventName, callback, maxCallbacks) {
eventListeners[eventName] = eventListeners[eventName] || [];
const thisListener = new Listener(callback, maxCallbacks);
eventListeners[eventName].push(thisListener);
}
function EventsOn(eventName, callback) {
EventsOnMultiple(eventName, callback, -1);
}
function EventsOnce(eventName, callback) {
EventsOnMultiple(eventName, callback, 1);
}
function notifyListeners(eventData) {
let eventName = eventData.name;
if (eventListeners[eventName]) {
const newEventListenerList = eventListeners[eventName].slice();
for (let count = 0; count < eventListeners[eventName].length; count += 1) {
const listener = eventListeners[eventName][count];
let data = eventData.data;
const destroy = listener.Callback(data);
if (destroy) {
newEventListenerList.splice(count, 1);
}
}
eventListeners[eventName] = newEventListenerList;
}
}
function EventsNotify(notifyMessage) {
let message;
try {
message = JSON.parse(notifyMessage);
} catch (e) {
const error = "Invalid JSON passed to Notify: " + notifyMessage;
throw new Error(error);
}
notifyListeners(message);
}
function EventsEmit(eventName) {
const payload = {
name: eventName,
data: [].slice.apply(arguments).slice(1)
};
notifyListeners(payload);
window.WailsInvoke("EE" + JSON.stringify(payload));
}
function EventsOff(eventName) {
eventListeners.delete(eventName);
window.WailsInvoke("EX" + eventName);
}
// desktop/calls.js
var callbacks = {};
function cryptoRandom() {
var array = new Uint32Array(1);
return window.crypto.getRandomValues(array)[0];
}
function basicRandom() {
return Math.random() * 9007199254740991;
}
var randomFunc;
if (window.crypto) {
randomFunc = cryptoRandom;
} else {
randomFunc = basicRandom;
}
function Call(name, args, timeout) {
if (timeout == null) {
timeout = 0;
}
return new Promise(function(resolve, reject) {
var callbackID;
do {
callbackID = name + "-" + randomFunc();
} while (callbacks[callbackID]);
var timeoutHandle;
if (timeout > 0) {
timeoutHandle = setTimeout(function() {
reject(Error("Call to " + name + " timed out. Request ID: " + callbackID));
}, timeout);
}
callbacks[callbackID] = {
timeoutHandle,
reject,
resolve
};
try {
const payload = {
name,
args,
callbackID
};
window.WailsInvoke("C" + JSON.stringify(payload));
} catch (e) {
console.error(e);
}
});
}
function Callback(incomingMessage) {
var message;
try {
message = JSON.parse(incomingMessage);
} catch (e) {
const error = `Invalid JSON passed to callback: ${e.message}. Message: ${incomingMessage}`;
wails.LogDebug(error);
throw new Error(error);
}
var callbackID = message.callbackid;
var callbackData = callbacks[callbackID];
if (!callbackData) {
const error = `Callback '${callbackID}' not registered!!!`;
console.error(error);
throw new Error(error);
}
clearTimeout(callbackData.timeoutHandle);
delete callbacks[callbackID];
if (message.error) {
callbackData.reject(message.error);
} else {
callbackData.resolve(message.result);
}
}
// desktop/bindings.js
window.go = {};
function SetBindings(bindingsMap) {
try {
bindingsMap = JSON.parse(bindingsMap);
} catch (e) {
console.error(e);
}
window.go = window.go || {};
Object.keys(bindingsMap).forEach((packageName) => {
window.go[packageName] = window.go[packageName] || {};
Object.keys(bindingsMap[packageName]).forEach((structName) => {
window.go[packageName][structName] = window.go[packageName][structName] || {};
Object.keys(bindingsMap[packageName][structName]).forEach((methodName) => {
window.go[packageName][structName][methodName] = function() {
let timeout = 0;
function dynamic() {
const args = [].slice.call(arguments);
return Call([packageName, structName, methodName].join("."), args, timeout);
}
dynamic.setTimeout = function(newTimeout) {
timeout = newTimeout;
};
dynamic.getTimeout = function() {
return timeout;
};
return dynamic;
}();
});
});
});
}
// desktop/window.js
var window_exports = {};
__export(window_exports, {
WindowCenter: () => WindowCenter,
WindowFullscreen: () => WindowFullscreen,
WindowGetPosition: () => WindowGetPosition,
WindowGetSize: () => WindowGetSize,
WindowHide: () => WindowHide,
WindowMaximise: () => WindowMaximise,
WindowMinimise: () => WindowMinimise,
WindowReload: () => WindowReload,
WindowSetMaxSize: () => WindowSetMaxSize,
WindowSetMinSize: () => WindowSetMinSize,
WindowSetPosition: () => WindowSetPosition,
WindowSetRGBA: () => WindowSetRGBA,
WindowSetSize: () => WindowSetSize,
WindowSetTitle: () => WindowSetTitle,
WindowShow: () => WindowShow,
WindowUnFullscreen: () => WindowUnFullscreen,
WindowUnmaximise: () => WindowUnmaximise,
WindowUnminimise: () => WindowUnminimise
});
function WindowReload() {
window.location.reload();
}
function WindowCenter() {
window.WailsInvoke("Wc");
}
function WindowSetTitle(title) {
window.WailsInvoke("WT" + title);
}
function WindowFullscreen() {
window.WailsInvoke("WF");
}
function WindowUnFullscreen() {
window.WailsInvoke("Wf");
}
function WindowSetSize(width, height) {
window.WailsInvoke("Ws:" + width + ":" + height);
}
function WindowGetSize() {
return Call(":wails:WindowGetSize");
}
function WindowSetMaxSize(width, height) {
window.WailsInvoke("WZ:" + width + ":" + height);
}
function WindowSetMinSize(width, height) {
window.WailsInvoke("Wz:" + width + ":" + height);
}
function WindowSetPosition(x, y) {
window.WailsInvoke("Wp:" + x + ":" + y);
}
function WindowGetPosition() {
return Call(":wails:WindowGetPos");
}
function WindowHide() {
window.WailsInvoke("WH");
}
function WindowShow() {
window.WailsInvoke("WS");
}
function WindowMaximise() {
window.WailsInvoke("WM");
}
function WindowUnmaximise() {
window.WailsInvoke("WU");
}
function WindowMinimise() {
window.WailsInvoke("Wm");
}
function WindowUnminimise() {
window.WailsInvoke("Wu");
}
function WindowSetRGBA(RGBA) {
let rgba = JSON.stringify(RGBA);
window.WailsInvoke("Wr:" + rgba);
}
// desktop/browser.js
var browser_exports = {};
__export(browser_exports, {
BrowserOpenURL: () => BrowserOpenURL
});
function BrowserOpenURL(url) {
window.WailsInvoke("BO:" + url);
}
// desktop/main.js
function Quit() {
window.WailsInvoke("Q");
}
window.runtime = {
...log_exports,
...window_exports,
...browser_exports,
EventsOn,
EventsOnce,
EventsOnMultiple,
EventsEmit,
EventsOff,
Quit
};
window.wails = {
Callback,
EventsNotify,
SetBindings,
eventListeners,
callbacks
};
window.wails.SetBindings(window.wailsbindings);
delete window.wails.SetBindings;
if (false) {
delete window.wailsbindings;
}
window.addEventListener("mousedown", (e) => {
let currentElement = e.target;
while (currentElement != null) {
if (currentElement.hasAttribute("data-wails-no-drag")) {
break;
} else if (currentElement.hasAttribute("data-wails-drag")) {
window.WailsInvoke("drag");
break;
}
currentElement = currentElement.parentElement;
}
});
})();