Skip to content

Commit

Permalink
Fixed dialogs on macOS. Update WML example to use compiled runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Jan 25, 2024
1 parent 773bdf8 commit 7674f8e
Show file tree
Hide file tree
Showing 43 changed files with 394 additions and 1,171 deletions.
2 changes: 1 addition & 1 deletion v3/examples/window/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
Description: "A demo of the WebviewWindow API",
Assets: application.AlphaAssets,
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
ApplicationShouldTerminateAfterLastWindowClosed: false,
},
})
app.On(events.Mac.ApplicationDidFinishLaunching, func(event *application.Event) {
Expand Down
1 change: 1 addition & 0 deletions v3/examples/wml/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
font-size: large;
}
</style>
<script src="runtime.js"></script>
</head>
<body style="--webkit-app-region: drag;">
<img class="logo"
Expand Down
1 change: 1 addition & 0 deletions v3/examples/wml/assets/runtime.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions v3/internal/commands/build_assets/runtime/compiled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package runtime

import _ "embed"

//go:embed runtime.js
var runtimeJS []byte

//go:embed runtime.debug.js
var runtimeDebugJS []byte
4 changes: 2 additions & 2 deletions v3/internal/runtime/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ tasks:
build:debug:
internal: true
cmds:
- npx esbuild@latest desktop/main.js --bundle --tree-shaking=true --sourcemap=inline --outfile=runtime_debug.js --define:DEBUG=true
- npx esbuild@latest desktop/compiled/main.js --bundle --tree-shaking=true --sourcemap=inline --outfile=../commands/build_assets/runtime/runtime.debug.js --define:DEBUG=true

build:production:
internal: true
cmds:
- npx esbuild@latest desktop/main.js --bundle --tree-shaking=true --minify --outfile=runtime.js --drop:console
- npx esbuild@latest desktop/compiled/main.js --bundle --tree-shaking=true --minify --outfile=../commands/build_assets/runtime/runtime.js --define:DEBUG=false --drop:console

build:all:
internal: true
Expand Down
28 changes: 0 additions & 28 deletions v3/internal/runtime/assets.go

This file was deleted.

28 changes: 0 additions & 28 deletions v3/internal/runtime/assets_dev.go

This file was deleted.

4 changes: 3 additions & 1 deletion v3/internal/runtime/desktop/@wailsio/runtime/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
events.test.js
node_modules
node_modules
types/drag.d.ts
types/contextmenu.d.ts
10 changes: 6 additions & 4 deletions v3/internal/runtime/desktop/@wailsio/runtime/src/calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ The electron alternative for Go
import { newRuntimeCallerWithID, objectNames } from "./runtime";
import { nanoid } from 'nanoid/non-secure';

const CallBinding = 0;
const call = newRuntimeCallerWithID(objectNames.Call, '');
let callResponses = new Map();

// Setup
window._wails = window._wails || {};
window._wails.callResultHandler = resultHandler;
window._wails.callErrorHandler = errorHandler;


const CallBinding = 0;
const call = newRuntimeCallerWithID(objectNames.Call, '');
let callResponses = new Map();

/**
* Generates a unique ID using the nanoid library.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ The electron alternative for Go
import {newRuntimeCallerWithID, objectNames} from "./runtime";
import {IsDebug} from "./system";

// setup
window.addEventListener('contextmenu', contextMenuHandler);

const call = newRuntimeCallerWithID(objectNames.ContextMenu, '');
const ContextMenuOpen = 0;

function openContextMenu(id, x, y, data) {
void call(ContextMenuOpen, {id, x, y, data});
}

export function setupContextMenus() {
window.addEventListener('contextmenu', contextMenuHandler);
}

function contextMenuHandler(event) {
// Check for custom context menu
let element = event.target;
Expand Down
9 changes: 5 additions & 4 deletions v3/internal/runtime/desktop/@wailsio/runtime/src/dialogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ The electron alternative for Go
* @property {string} [Pattern] - Pattern to match for the filter, e.g. "*.txt;*.md" for text markdown files.
*/

// setup
window._wails = window._wails || {};
window._wails.dialogErrorCallback = dialogErrorCallback;
window._wails.dialogResultCallback = dialogResultCallback;

import {newRuntimeCallerWithID, objectNames} from "./runtime";

import { nanoid } from 'nanoid/non-secure';
Expand Down Expand Up @@ -118,10 +123,6 @@ function dialog(type, options = {}) {
});
}

window._wails = window._wails || {};
window._wails.dialogErrorCallback = dialogErrorCallback;
window._wails.dialogResultCallback = dialogResultCallback;

/**
* Handles the callback from a dialog.
*
Expand Down
23 changes: 11 additions & 12 deletions v3/internal/runtime/desktop/@wailsio/runtime/src/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,32 @@ The electron alternative for Go
import {invoke, IsWindows} from "./system";
import {GetFlag} from "./flags";

// Setup
window._wails = window._wails || {};
window._wails.setResizable = setResizable;
window._wails.endDrag = endDrag;
window.addEventListener('mousedown', onMouseDown);
window.addEventListener('mousemove', onMouseMove);
window.addEventListener('mouseup', onMouseUp);

let shouldDrag = false;
let resizeEdge = null;
let resizable = false;
let defaultCursor = "auto";
window._wails = window._wails || {};
window._wails.setResizable = setResizable;
window._wails.endDrag = endDrag;

export function dragTest(e) {
function dragTest(e) {
let val = window.getComputedStyle(e.target).getPropertyValue("--webkit-app-region");
if (!val || val === "" || val.trim() !== "drag" || e.buttons !== 1) {
return false;
}
return e.detail === 1;
}

export function setupDrag() {
window.addEventListener('mousedown', onMouseDown);
window.addEventListener('mousemove', onMouseMove);
window.addEventListener('mouseup', onMouseUp);
}

export function setResizable(value) {
function setResizable(value) {
resizable = value;
}

export function endDrag() {
function endDrag() {
document.body.style.cursor = 'default';
shouldDrag = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export const EventTypes = {
WindowHide: "common:WindowHide",
WindowDPIChanged: "common:WindowDPIChanged",
WindowFilesDropped: "common:WindowFilesDropped",
WindowRuntimeReady: "common:WindowRuntimeReady",
ThemeChanged: "common:ThemeChanged",
},
};
18 changes: 5 additions & 13 deletions v3/internal/runtime/desktop/@wailsio/runtime/src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {newRuntimeCallerWithID, objectNames} from "./runtime";
import {EventTypes} from "./event_types";
export const Types = EventTypes;

// Setup
window._wails = window._wails || {};
window._wails.dispatchWailsEvent = dispatchWailsEvent;

const call = newRuntimeCallerWithID(objectNames.Events, '');
const EmitMethod = 0;
const eventListeners = new Map();
Expand All @@ -42,19 +46,7 @@ export class WailsEvent {
}
}

/**
* Sets up event callbacks for Wails.
*
* @function setupEventCallbacks
*
* @description This method is responsible for setting up event callbacks for Wails. It checks if the global object `_wails` exists on the `window` object, and if not, initializes it
*. It then assigns the `dispatchWailsEvent` function as a property on the `_wails` object.
*
* @returns {undefined} Returns nothing.
*/
export function setupEventCallbacks() {
window._wails = window._wails || {};
window._wails.dispatchWailsEvent = dispatchWailsEvent;
export function setup() {
}

function dispatchWailsEvent(event) {
Expand Down
23 changes: 1 addition & 22 deletions v3/internal/runtime/desktop/@wailsio/runtime/src/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,7 @@ describe('Once', () => {
cancel();
})
})
//
// describe('EventsNotify', () => {
// it('should inform a listener', () => {
// const cb = vi.fn()
// EventsOn('a', cb)
// EventsNotify(JSON.stringify({name: 'a', data: ["one", "two", "three"]}))
// expect(cb).toBeCalledTimes(1);
// expect(cb).toHaveBeenLastCalledWith("one", "two", "three");
// expect(window.WailsInvoke).toBeCalledTimes(0);
// })
// })
//
// describe('EventsEmit', () => {
// it('should emit an event', () => {
// EventsEmit('a', 'one', 'two', 'three')
// expect(window.WailsInvoke).toBeCalledTimes(1);
// const calledWith = window.WailsInvoke.calls[0][0];
// expect(calledWith.slice(0, 2)).toBe('EE')
// expect(JSON.parse(calledWith.slice(2))).toStrictEqual({data: ["one", "two", "three"], name: "a"})
// })
// })
//

describe('Off', () => {
beforeEach(() => {
On('a', () => {})
Expand Down
48 changes: 5 additions & 43 deletions v3/internal/runtime/desktop/@wailsio/runtime/src/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,16 @@ The electron alternative for Go

/* jshint esversion: 9 */

let flags = new Map();

function convertToMap(obj) {
const map = new Map();

for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'object' && value !== null) {
map.set(key, convertToMap(value)); // Recursively convert nested object
} else {
map.set(key, value);
}
}

return map;
}

fetch("/wails/flags").then((response) => {
response.json().then((data) => {
flags = convertToMap(data);
});
});


function getValueFromMap(keyString) {
const keys = keyString.split('.');
let value = flags;

for (const key of keys) {
if (value instanceof Map) {
value = value.get(key);
} else {
value = value[key];
}

if (value === undefined) {
break;
}
}

return value;
}

/**
* Retrieves the value associated with the specified key from the flag map.
*
* @param {string} keyString - The key to retrieve the value for.
* @return {*} - The value associated with the specified key.
*/
export function GetFlag(keyString) {
return getValueFromMap(keyString);
try {
return window._wails.flags[keyString];
} catch (e) {
throw new Error("Unable to retrieve flag '" + keyString + "': " + e);
}
}
Loading

0 comments on commit 7674f8e

Please sign in to comment.