@wailsio/runtime 3.0.0-alpha.96 → 3.0.0-alpha2.117

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,187 @@
1
+ /*
2
+ _ __ _ __
3
+ | | / /___(_) /____
4
+ | | /| / / __ `/ / / ___/
5
+ | |/ |/ / /_/ / / (__ )
6
+ |__/|__/\__,_/_/_/____/
7
+ The electron alternative for Go
8
+ (c) Lea Anthony 2019-present
9
+ */
10
+ import { invoke } from "./system.js";
11
+ import { whenReady } from "./utils.js";
12
+ import { hasDOM } from "./environment.js";
13
+ /*
14
+ --wails-non-client-region: caption; marks an area that can drag the window
15
+ --wails-non-client-region: minimize; marks a custom minimize button
16
+ --wails-non-client-region: maximize; marks a custom maximize button
17
+ --wails-non-client-region: close; marks a custom close button
18
+ */
19
+ const regionProperty = "--wails-non-client-region";
20
+ const runtimeConfigReadyEvent = "wails:runtime-config-ready";
21
+ const validRegions = new Set(["caption", "minimize", "maximize", "close"]);
22
+ // Setup
23
+ if (hasDOM) {
24
+ window._wails = window._wails || {};
25
+ }
26
+ let updatePending = false;
27
+ let lastPayload = "";
28
+ let observedElements = new Set();
29
+ let resizeObserver;
30
+ let trackingStarted = false;
31
+ function normaliseRegionKind(value) {
32
+ const region = value.trim().toLowerCase();
33
+ if (validRegions.has(region)) {
34
+ return region;
35
+ }
36
+ return undefined;
37
+ }
38
+ function nonClientRegionForElement(element) {
39
+ if (!(element instanceof HTMLElement)) {
40
+ return undefined;
41
+ }
42
+ const style = window.getComputedStyle(element);
43
+ const region = normaliseRegionKind(style.getPropertyValue(regionProperty));
44
+ if (!region) {
45
+ return undefined;
46
+ }
47
+ const parent = element.parentElement;
48
+ if (parent) {
49
+ const parentStyle = window.getComputedStyle(parent);
50
+ // The CSS property is inherited. Only report the outermost element for
51
+ // each contiguous region so native hit testing sees stable rectangles.
52
+ if (normaliseRegionKind(parentStyle.getPropertyValue(regionProperty)) === region) {
53
+ return undefined;
54
+ }
55
+ }
56
+ return region;
57
+ }
58
+ function isVisible(element) {
59
+ const style = window.getComputedStyle(element);
60
+ return style.display !== "none" &&
61
+ style.visibility !== "hidden" &&
62
+ style.contentVisibility !== "hidden";
63
+ }
64
+ function elementRegion(element) {
65
+ if (!(element instanceof HTMLElement)) {
66
+ return undefined;
67
+ }
68
+ const kind = nonClientRegionForElement(element);
69
+ if (!kind || !isVisible(element)) {
70
+ return undefined;
71
+ }
72
+ const rect = element.getBoundingClientRect();
73
+ if (rect.width <= 0 || rect.height <= 0) {
74
+ return undefined;
75
+ }
76
+ // Native hit testing runs in physical pixels, while DOM geometry is in CSS pixels.
77
+ const scale = window.devicePixelRatio || 1;
78
+ const left = Math.floor(rect.left * scale);
79
+ const top = Math.floor(rect.top * scale);
80
+ const right = Math.ceil(rect.right * scale);
81
+ const bottom = Math.ceil(rect.bottom * scale);
82
+ if (right <= left || bottom <= top) {
83
+ return undefined;
84
+ }
85
+ return { kind, left, top, right, bottom };
86
+ }
87
+ function regionElements() {
88
+ const elements = [];
89
+ if (document.documentElement) {
90
+ elements.push(document.documentElement);
91
+ }
92
+ if (document.body) {
93
+ elements.push(document.body);
94
+ // Append via a loop: spreading a huge NodeList into push() overflows
95
+ // the engine's argument limit on very large documents.
96
+ for (const element of document.body.querySelectorAll("*")) {
97
+ elements.push(element);
98
+ }
99
+ }
100
+ return elements;
101
+ }
102
+ function observeRegionElements(elements) {
103
+ if (typeof ResizeObserver === "undefined") {
104
+ return;
105
+ }
106
+ // Track size changes only for active region elements. DOM structure and style
107
+ // changes are covered by MutationObserver in startNonClientRegionTracking().
108
+ resizeObserver !== null && resizeObserver !== void 0 ? resizeObserver : (resizeObserver = new ResizeObserver(scheduleUpdate));
109
+ const nextElements = new Set(elements);
110
+ for (const element of observedElements) {
111
+ if (!nextElements.has(element)) {
112
+ resizeObserver.unobserve(element);
113
+ }
114
+ }
115
+ for (const element of nextElements) {
116
+ if (!observedElements.has(element)) {
117
+ resizeObserver.observe(element);
118
+ }
119
+ }
120
+ observedElements = nextElements;
121
+ }
122
+ function updateNonClientRegions() {
123
+ updatePending = false;
124
+ const elements = regionElements();
125
+ const regions = [];
126
+ const activeElements = [];
127
+ for (const element of elements) {
128
+ const region = elementRegion(element);
129
+ if (region) {
130
+ regions.push(region);
131
+ activeElements.push(element);
132
+ }
133
+ }
134
+ observeRegionElements(activeElements);
135
+ const payload = JSON.stringify({ version: 1, regions });
136
+ if (payload === lastPayload) {
137
+ // Avoid sending duplicate native messages during resize or style churn.
138
+ return;
139
+ }
140
+ lastPayload = payload;
141
+ invoke("wails:non-client-region:" + payload);
142
+ }
143
+ function scheduleUpdate() {
144
+ if (updatePending) {
145
+ return;
146
+ }
147
+ // Batch region updates to animation frames so layout is measured once per frame.
148
+ updatePending = true;
149
+ window.requestAnimationFrame(updateNonClientRegions);
150
+ }
151
+ function startNonClientRegionTracking() {
152
+ var _a, _b;
153
+ if (trackingStarted) {
154
+ return;
155
+ }
156
+ trackingStarted = true;
157
+ // Send an initial empty or populated region list once the DOM is ready.
158
+ scheduleUpdate();
159
+ const mutationObserver = new MutationObserver(scheduleUpdate);
160
+ mutationObserver.observe(document.documentElement, {
161
+ attributes: true,
162
+ childList: true,
163
+ subtree: true,
164
+ });
165
+ window.addEventListener("resize", scheduleUpdate);
166
+ window.addEventListener("scroll", scheduleUpdate, true);
167
+ (_a = window.visualViewport) === null || _a === void 0 ? void 0 : _a.addEventListener("resize", scheduleUpdate);
168
+ (_b = window.visualViewport) === null || _b === void 0 ? void 0 : _b.addEventListener("scroll", scheduleUpdate);
169
+ }
170
+ function tryStartNonClientRegionTracking() {
171
+ var _a, _b;
172
+ const os = (_a = window._wails.environment) === null || _a === void 0 ? void 0 : _a.OS;
173
+ if (os === undefined) {
174
+ return false;
175
+ }
176
+ const enabled = (_b = window._wails.flags) === null || _b === void 0 ? void 0 : _b.nonClientRegionTracking;
177
+ if (os === "windows") {
178
+ if (enabled === true) {
179
+ whenReady(startNonClientRegionTracking);
180
+ }
181
+ return true;
182
+ }
183
+ return true;
184
+ }
185
+ if (hasDOM && !tryStartNonClientRegionTracking()) {
186
+ window.addEventListener(runtimeConfigReadyEvent, tryStartNonClientRegionTracking, { once: true });
187
+ }
package/dist/calls.js CHANGED
@@ -20,21 +20,10 @@ const cancelCall = newRuntimeCaller(objectNames.CancelCall);
20
20
  const callResponses = new Map();
21
21
  const CallBinding = 0;
22
22
  const CancelMethod = 0;
23
- /**
24
- * Exception class that will be thrown in case the bound method returns an error.
25
- * The value of the {@link RuntimeError#name} property is "RuntimeError".
26
- */
27
- export class RuntimeError extends Error {
28
- /**
29
- * Constructs a new RuntimeError instance.
30
- * @param message - The error message.
31
- * @param options - Options to be forwarded to the Error constructor.
32
- */
33
- constructor(message, options) {
34
- super(message, options);
35
- this.name = "RuntimeError";
36
- }
37
- }
23
+ // runtime.js needs to use RuntimeError internally to properly parse and return
24
+ // errors for binding calls, so it had to move there. Exporting here again to
25
+ // keep from breaking the public Call interface.
26
+ export { RuntimeError } from "./runtime.js";
38
27
  /**
39
28
  * Generates a unique ID using the nanoid library.
40
29
  *
package/dist/drag.js CHANGED
@@ -155,6 +155,12 @@ function primaryDown(event) {
155
155
  }
156
156
  }
157
157
  if (resizeEdge) {
158
+ // Do not arm edge resize from synthesized presses observed on move/up:
159
+ // entering the window with the primary button already held should not
160
+ // steal another gesture into a resize.
161
+ if (event.type !== 'mousedown') {
162
+ return;
163
+ }
158
164
  // Ready to resize if the primary button was pressed for the first time.
159
165
  canResize = true;
160
166
  // Do not start drag operations when on resize edges.
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ if (hasDOM) {
14
14
  }
15
15
  import "./contextmenu.js";
16
16
  import "./drag.js";
17
+ import "./appregion.js";
17
18
  // Re-export public API
18
19
  import * as Application from "./application.js";
19
20
  import * as Browser from "./browser.js";
package/dist/runtime.js CHANGED
@@ -19,6 +19,21 @@ function runtimeURL() {
19
19
  const CHUNK_THRESHOLD = 512 * 1024;
20
20
  // Re-export nanoid for custom transport implementations
21
21
  export { nanoid };
22
+ /**
23
+ * Exception class that will be thrown in case the bound method returns an error.
24
+ * The value of the {@link RuntimeError#name} property is "RuntimeError".
25
+ */
26
+ export class RuntimeError extends Error {
27
+ /**
28
+ * Constructs a new RuntimeError instance.
29
+ * @param message - The error message.
30
+ * @param options - Options to be forwarded to the Error constructor.
31
+ */
32
+ constructor(message, options) {
33
+ super(message, options);
34
+ this.name = "RuntimeError";
35
+ }
36
+ }
22
37
  // Object Names
23
38
  export const objectNames = Object.freeze({
24
39
  Call: 0,
@@ -112,6 +127,25 @@ async function runtimeCallWithID(objectID, method, windowName, args) {
112
127
  response = await fetch(url, { method: 'POST', headers, body: bodyStr });
113
128
  }
114
129
  if (!response.ok) {
130
+ const ct = response.headers.get("Content-Type");
131
+ if (ct === null || ct === void 0 ? void 0 : ct.includes("application/json")) {
132
+ const json = await response.json();
133
+ let err;
134
+ switch (json.kind) {
135
+ case "ReferenceError":
136
+ err = new ReferenceError(json.message);
137
+ break;
138
+ case "TypeError":
139
+ err = new TypeError(json.message);
140
+ break;
141
+ case "RuntimeError":
142
+ err = new RuntimeError(json.message);
143
+ break;
144
+ default: err = new Error(json.message);
145
+ }
146
+ err.cause = json.cause;
147
+ throw err;
148
+ }
115
149
  throw new Error(await response.text());
116
150
  }
117
151
  if (((_b = (_a = response.headers.get("Content-Type")) === null || _a === void 0 ? void 0 : _a.indexOf("application/json")) !== null && _b !== void 0 ? _b : -1) !== -1) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wailsio/runtime",
3
3
  "type": "module",
4
- "version": "3.0.0-alpha.96",
4
+ "version": "3.0.0-alpha2.117",
5
5
  "description": "Wails Runtime",
6
6
  "types": "types/index.d.ts",
7
7
  "exports": {
@@ -31,8 +31,13 @@
31
31
  ],
32
32
  "sideEffects": [
33
33
  "./dist/index.js",
34
+ "./dist/appregion.js",
34
35
  "./dist/contextmenu.js",
35
- "./dist/drag.js"
36
+ "./dist/drag.js",
37
+ "./src/index.ts",
38
+ "./src/appregion.ts",
39
+ "./src/contextmenu.ts",
40
+ "./src/drag.ts"
36
41
  ],
37
42
  "scripts": {
38
43
  "check": "npx tsc --noEmit",
@@ -0,0 +1 @@
1
+ export {};
package/types/calls.d.ts CHANGED
@@ -18,18 +18,7 @@ export type CallOptions = {
18
18
  /** Arguments to be passed into the bound method. */
19
19
  args: any[];
20
20
  };
21
- /**
22
- * Exception class that will be thrown in case the bound method returns an error.
23
- * The value of the {@link RuntimeError#name} property is "RuntimeError".
24
- */
25
- export declare class RuntimeError extends Error {
26
- /**
27
- * Constructs a new RuntimeError instance.
28
- * @param message - The error message.
29
- * @param options - Options to be forwarded to the Error constructor.
30
- */
31
- constructor(message?: string, options?: ErrorOptions);
32
- }
21
+ export { RuntimeError } from "./runtime.js";
33
22
  /**
34
23
  * Call a bound method according to the given call options.
35
24
  *
package/types/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import "./contextmenu.js";
2
2
  import "./drag.js";
3
+ import "./appregion.js";
3
4
  import * as Application from "./application.js";
4
5
  import * as Browser from "./browser.js";
5
6
  import * as Call from "./calls.js";
@@ -1,5 +1,17 @@
1
1
  import { nanoid } from "./nanoid.js";
2
2
  export { nanoid };
3
+ /**
4
+ * Exception class that will be thrown in case the bound method returns an error.
5
+ * The value of the {@link RuntimeError#name} property is "RuntimeError".
6
+ */
7
+ export declare class RuntimeError extends Error {
8
+ /**
9
+ * Constructs a new RuntimeError instance.
10
+ * @param message - The error message.
11
+ * @param options - Options to be forwarded to the Error constructor.
12
+ */
13
+ constructor(message?: string, options?: ErrorOptions);
14
+ }
3
15
  export declare const objectNames: Readonly<{
4
16
  Call: 0;
5
17
  Clipboard: 1;