@nativescript/windows-napi 1.0.0-beta.20 → 1.0.0-beta.22
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.
- package/nswinrt.js +217 -217
- package/package.json +47 -47
- package/windows.win32-arm64-msvc.node +0 -0
- package/windows.win32-x64-msvc.node +0 -0
package/nswinrt.js
CHANGED
|
@@ -1,217 +1,217 @@
|
|
|
1
|
-
// NSWinRT JS helper layer (ships with the package alongside the native addon).
|
|
2
|
-
// Pure JS that operates on the native WinRT proxies — async→Promise, event helpers.
|
|
3
|
-
// Mirrors the runtime's HELPER_SOURCE `toPromise`, adapted for the napi backend where the
|
|
4
|
-
// host owns the loop (the consumer pumps via `pumpMessages`).
|
|
5
|
-
'use strict';
|
|
6
|
-
const native = require('./index.js');
|
|
7
|
-
|
|
8
|
-
const Windows = native.getNamespace('Windows');
|
|
9
|
-
|
|
10
|
-
function statusEnum() {
|
|
11
|
-
try { return Windows.Foundation.AsyncStatus; }
|
|
12
|
-
catch { return { Started: 0, Completed: 1, Canceled: 2, Error: 3 }; }
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function normalizeStatus(status) {
|
|
16
|
-
if (status == null) return NaN;
|
|
17
|
-
if (typeof status === 'number') return status;
|
|
18
|
-
const n = Number(status);
|
|
19
|
-
return Number.isNaN(n) ? NaN : n;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Pump lifecycle: while any WinRT async op is outstanding, the pump timer is ref'd so Node
|
|
23
|
-
// stays alive to deliver Completed; when the last settles, it is unref'd so the process can
|
|
24
|
-
// exit. (An unref'd-only timer would let Node exit before completion fires.)
|
|
25
|
-
let pending = 0;
|
|
26
|
-
function retainPump() {
|
|
27
|
-
pending += 1;
|
|
28
|
-
if (autoPumpTimer && typeof autoPumpTimer.ref === 'function') autoPumpTimer.ref();
|
|
29
|
-
}
|
|
30
|
-
function releasePump() {
|
|
31
|
-
pending = Math.max(0, pending - 1);
|
|
32
|
-
if (pending === 0 && autoPumpTimer && typeof autoPumpTimer.unref === 'function') {
|
|
33
|
-
autoPumpTimer.unref();
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Convert a WinRT IAsyncOperation/IAsyncAction proxy to a JS Promise via its Completed event,
|
|
38
|
-
// Status property, and GetResults() method — all provided by the native proxy layer.
|
|
39
|
-
function toPromise(op) {
|
|
40
|
-
if (op == null || (typeof op !== 'object' && typeof op !== 'function')) {
|
|
41
|
-
return Promise.resolve(op);
|
|
42
|
-
}
|
|
43
|
-
if (typeof op.then === 'function' && !('Completed' in op)) return op;
|
|
44
|
-
|
|
45
|
-
const S = statusEnum();
|
|
46
|
-
retainPump();
|
|
47
|
-
return new Promise((resolve, reject) => {
|
|
48
|
-
let settled = false;
|
|
49
|
-
const done = (fn, arg) => { settled = true; releasePump(); fn(arg); };
|
|
50
|
-
const settle = (override) => {
|
|
51
|
-
if (settled) return;
|
|
52
|
-
try {
|
|
53
|
-
const status = normalizeStatus(override !== undefined ? override : op.Status);
|
|
54
|
-
if (status === S.Completed || status === 1) {
|
|
55
|
-
done(resolve, typeof op.GetResults === 'function' ? op.GetResults() : undefined);
|
|
56
|
-
} else if (status === S.Canceled || status === 2) {
|
|
57
|
-
done(reject, new Error('WinRT async operation was canceled'));
|
|
58
|
-
} else if (status === S.Error || status === 3) {
|
|
59
|
-
done(reject, op.ErrorCode || new Error('WinRT async operation failed'));
|
|
60
|
-
}
|
|
61
|
-
} catch (err) { done(reject, err); }
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const initial = normalizeStatus(op.Status);
|
|
65
|
-
if (!Number.isNaN(initial) && initial !== 0) { settle(initial); return; }
|
|
66
|
-
op.Completed = (asyncInfo, asyncStatus) => settle(asyncStatus);
|
|
67
|
-
// Race guard: it may have completed between the status read and handler assignment.
|
|
68
|
-
const race = normalizeStatus(op.Status);
|
|
69
|
-
if (!Number.isNaN(race) && race !== 0) settle(race);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Drive the Windows message loop until `promise` settles or `timeoutMs` elapses. WinRT STA
|
|
74
|
-
// async completions are delivered as messages, so the loop must be pumped for them to fire.
|
|
75
|
-
async function awaitWithPump(promise, timeoutMs = 5000) {
|
|
76
|
-
let done = false, result, error, ok;
|
|
77
|
-
promise.then((r) => { done = true; ok = true; result = r; },
|
|
78
|
-
(e) => { done = true; ok = false; error = e; });
|
|
79
|
-
const start = Date.now();
|
|
80
|
-
while (!done && Date.now() - start < timeoutMs) {
|
|
81
|
-
native.pumpMessages();
|
|
82
|
-
await new Promise((r) => setImmediate(r));
|
|
83
|
-
}
|
|
84
|
-
if (!done) throw new Error('awaitWithPump timed out');
|
|
85
|
-
if (!ok) throw error;
|
|
86
|
-
return result;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Pump the WinRT message loop automatically on a Node timer so awaiting WinRT Promises is
|
|
90
|
-
// transparent (no manual `awaitWithPump`). The timer is unref'd, so it never keeps the process
|
|
91
|
-
// alive on its own. Returns a stop function. Safe to call once at startup.
|
|
92
|
-
let autoPumpTimer = null;
|
|
93
|
-
function enableAutoPump(intervalMs = 4) {
|
|
94
|
-
if (autoPumpTimer) return () => disableAutoPump();
|
|
95
|
-
autoPumpTimer = setInterval(() => { native.pumpMessages(); }, intervalMs);
|
|
96
|
-
if (typeof autoPumpTimer.unref === 'function') autoPumpTimer.unref();
|
|
97
|
-
return () => disableAutoPump();
|
|
98
|
-
}
|
|
99
|
-
function disableAutoPump() {
|
|
100
|
-
if (autoPumpTimer) { clearInterval(autoPumpTimer); autoPumpTimer = null; }
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// interop.* — the full NSWinRT.interop surface (Pointer/OutParam, reference/typed-value
|
|
104
|
-
// boxing, buffer + DateTime utilities, uuid/winmd registration, zero-copy
|
|
105
|
-
// arrayBufferFromBuffer). Installed by the shared native layer so the surface is identical
|
|
106
|
-
// across Node/Bun/Deno and the standalone engine hosts.
|
|
107
|
-
native.installInterop();
|
|
108
|
-
const interop = globalThis.NSWinRT.interop;
|
|
109
|
-
|
|
110
|
-
// dotnet.* — the .NET/BCL bridge (NSWinRT.dotnet: invoke/get/fromHandle/registerNamespace,
|
|
111
|
-
// taskToPromise/asDelegate, plus NSWinRT.runOnUIThread). A no-op at the JS layer until a
|
|
112
|
-
// dotnet-bridge/publish/DotNetBridge.dll exists next to the app.
|
|
113
|
-
native.installDotnet();
|
|
114
|
-
const dotnet = globalThis.NSWinRT.dotnet;
|
|
115
|
-
|
|
116
|
-
// Windows.UI.Xaml content in a Win32 window (XAML Islands). Needs a thread that pumps messages.
|
|
117
|
-
let xamlManager = null;
|
|
118
|
-
|
|
119
|
-
function systemTheme() {
|
|
120
|
-
const vm = Windows.UI.ViewManagement;
|
|
121
|
-
const bg = new vm.UISettings().GetColorValue(vm.UIColorType.Background);
|
|
122
|
-
return bg.R < 128 ? 'dark' : 'light';
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
class XamlWindow {
|
|
126
|
-
/**
|
|
127
|
-
* @param {object} [options]
|
|
128
|
-
* @param {'dialog'|'popup'} [options.style] dialog: title bar and close box; popup: borderless,
|
|
129
|
-
* not in the taskbar or Alt+Tab.
|
|
130
|
-
* @param {number|bigint|Buffer} [options.owner] owner window handle.
|
|
131
|
-
* @param {string} [options.title]
|
|
132
|
-
* @param {boolean} [options.topmost]
|
|
133
|
-
* @param {'system'|'dark'|'light'} [options.theme] title bar theme; read it back from `theme`.
|
|
134
|
-
*/
|
|
135
|
-
constructor(options = {}) {
|
|
136
|
-
if (!xamlManager) {
|
|
137
|
-
try {
|
|
138
|
-
xamlManager = Windows.UI.Xaml.Hosting.WindowsXamlManager.InitializeForCurrentThread();
|
|
139
|
-
} catch (e) {
|
|
140
|
-
if (!/0x8000FFFF/i.test(String(e && e.message))) throw e;
|
|
141
|
-
throw new Error('XamlWindow: XAML Islands are unavailable in this process. The host ' +
|
|
142
|
-
'executable\'s manifest must declare <maxversiontested Id="10.0.18362.0"/> or later.');
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
const { theme = 'system', ...rest } = options;
|
|
146
|
-
this.theme = theme === 'system' ? systemTheme() : theme;
|
|
147
|
-
this.source = new Windows.UI.Xaml.Hosting.DesktopWindowXamlSource();
|
|
148
|
-
this.hwnd = native.xamlHostCreate(this.source, { ...rest, dark: this.theme === 'dark' });
|
|
149
|
-
this.closed = false;
|
|
150
|
-
this._handlers = { activate: [], deactivate: [], close: [], closed: [] };
|
|
151
|
-
native.xamlHostSetListener(this.hwnd, (type) => this._emit(type));
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
get content() { return this.source.Content; }
|
|
155
|
-
set content(element) { this.source.Content = element; }
|
|
156
|
-
|
|
157
|
-
get visible() { return !this.closed && native.xamlHostIsVisible(this.hwnd); }
|
|
158
|
-
|
|
159
|
-
/** `activate`, `deactivate`, `close` (title-bar X or Alt+F4; closes when unhandled), `closed`. */
|
|
160
|
-
on(type, fn) {
|
|
161
|
-
if (!this._handlers[type]) throw new TypeError(`XamlWindow: unknown event '${type}'`);
|
|
162
|
-
this._handlers[type].push(fn);
|
|
163
|
-
return this;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
off(type, fn) {
|
|
167
|
-
const list = this._handlers[type];
|
|
168
|
-
if (list && list.includes(fn)) list.splice(list.indexOf(fn), 1);
|
|
169
|
-
return this;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** Client size in DIPs; centered over `over`, or on the pointer's monitor at `y` (0..1). */
|
|
173
|
-
place({ width, height, over, y } = {}) {
|
|
174
|
-
native.xamlHostPlace(this.hwnd, { width, height, over, y });
|
|
175
|
-
return this;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** Returns whether the window is in the foreground. */
|
|
179
|
-
show({ activate = true } = {}) { return native.xamlHostShow(this.hwnd, activate); }
|
|
180
|
-
|
|
181
|
-
hide() { if (!this.closed) native.xamlHostHide(this.hwnd); }
|
|
182
|
-
|
|
183
|
-
close() { if (!this.closed) native.xamlHostDestroy(this.hwnd); }
|
|
184
|
-
|
|
185
|
-
_emit(type) {
|
|
186
|
-
if (type === 'closed') {
|
|
187
|
-
this.closed = true;
|
|
188
|
-
try { this.source.Close(); } catch {}
|
|
189
|
-
}
|
|
190
|
-
const handlers = this._handlers[type].slice();
|
|
191
|
-
if (type === 'close' && handlers.length === 0) this.close();
|
|
192
|
-
for (const fn of handlers) {
|
|
193
|
-
// Rethrown outside the native callback so the host reports it as uncaught.
|
|
194
|
-
try { fn(); } catch (e) { setTimeout(() => { throw e; }); }
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
static systemTheme() { return systemTheme(); }
|
|
199
|
-
/** The foreground window of any process, as a number (0 if none). */
|
|
200
|
-
static foregroundWindow() { return native.foregroundWindow(); }
|
|
201
|
-
/** Bring a window to the foreground; `false` if Windows refuses. */
|
|
202
|
-
static activateWindow(hwnd) { return native.activateWindow(hwnd); }
|
|
203
|
-
static enableWindow(hwnd, enabled) { native.enableWindow(hwnd, enabled); }
|
|
204
|
-
static windowProcessId(hwnd) { return native.windowProcessId(hwnd); }
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
module.exports = {
|
|
208
|
-
native,
|
|
209
|
-
Windows,
|
|
210
|
-
toPromise,
|
|
211
|
-
awaitWithPump,
|
|
212
|
-
enableAutoPump,
|
|
213
|
-
disableAutoPump,
|
|
214
|
-
interop,
|
|
215
|
-
dotnet,
|
|
216
|
-
XamlWindow,
|
|
217
|
-
};
|
|
1
|
+
// NSWinRT JS helper layer (ships with the package alongside the native addon).
|
|
2
|
+
// Pure JS that operates on the native WinRT proxies — async→Promise, event helpers.
|
|
3
|
+
// Mirrors the runtime's HELPER_SOURCE `toPromise`, adapted for the napi backend where the
|
|
4
|
+
// host owns the loop (the consumer pumps via `pumpMessages`).
|
|
5
|
+
'use strict';
|
|
6
|
+
const native = require('./index.js');
|
|
7
|
+
|
|
8
|
+
const Windows = native.getNamespace('Windows');
|
|
9
|
+
|
|
10
|
+
function statusEnum() {
|
|
11
|
+
try { return Windows.Foundation.AsyncStatus; }
|
|
12
|
+
catch { return { Started: 0, Completed: 1, Canceled: 2, Error: 3 }; }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function normalizeStatus(status) {
|
|
16
|
+
if (status == null) return NaN;
|
|
17
|
+
if (typeof status === 'number') return status;
|
|
18
|
+
const n = Number(status);
|
|
19
|
+
return Number.isNaN(n) ? NaN : n;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Pump lifecycle: while any WinRT async op is outstanding, the pump timer is ref'd so Node
|
|
23
|
+
// stays alive to deliver Completed; when the last settles, it is unref'd so the process can
|
|
24
|
+
// exit. (An unref'd-only timer would let Node exit before completion fires.)
|
|
25
|
+
let pending = 0;
|
|
26
|
+
function retainPump() {
|
|
27
|
+
pending += 1;
|
|
28
|
+
if (autoPumpTimer && typeof autoPumpTimer.ref === 'function') autoPumpTimer.ref();
|
|
29
|
+
}
|
|
30
|
+
function releasePump() {
|
|
31
|
+
pending = Math.max(0, pending - 1);
|
|
32
|
+
if (pending === 0 && autoPumpTimer && typeof autoPumpTimer.unref === 'function') {
|
|
33
|
+
autoPumpTimer.unref();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Convert a WinRT IAsyncOperation/IAsyncAction proxy to a JS Promise via its Completed event,
|
|
38
|
+
// Status property, and GetResults() method — all provided by the native proxy layer.
|
|
39
|
+
function toPromise(op) {
|
|
40
|
+
if (op == null || (typeof op !== 'object' && typeof op !== 'function')) {
|
|
41
|
+
return Promise.resolve(op);
|
|
42
|
+
}
|
|
43
|
+
if (typeof op.then === 'function' && !('Completed' in op)) return op;
|
|
44
|
+
|
|
45
|
+
const S = statusEnum();
|
|
46
|
+
retainPump();
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
let settled = false;
|
|
49
|
+
const done = (fn, arg) => { settled = true; releasePump(); fn(arg); };
|
|
50
|
+
const settle = (override) => {
|
|
51
|
+
if (settled) return;
|
|
52
|
+
try {
|
|
53
|
+
const status = normalizeStatus(override !== undefined ? override : op.Status);
|
|
54
|
+
if (status === S.Completed || status === 1) {
|
|
55
|
+
done(resolve, typeof op.GetResults === 'function' ? op.GetResults() : undefined);
|
|
56
|
+
} else if (status === S.Canceled || status === 2) {
|
|
57
|
+
done(reject, new Error('WinRT async operation was canceled'));
|
|
58
|
+
} else if (status === S.Error || status === 3) {
|
|
59
|
+
done(reject, op.ErrorCode || new Error('WinRT async operation failed'));
|
|
60
|
+
}
|
|
61
|
+
} catch (err) { done(reject, err); }
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const initial = normalizeStatus(op.Status);
|
|
65
|
+
if (!Number.isNaN(initial) && initial !== 0) { settle(initial); return; }
|
|
66
|
+
op.Completed = (asyncInfo, asyncStatus) => settle(asyncStatus);
|
|
67
|
+
// Race guard: it may have completed between the status read and handler assignment.
|
|
68
|
+
const race = normalizeStatus(op.Status);
|
|
69
|
+
if (!Number.isNaN(race) && race !== 0) settle(race);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Drive the Windows message loop until `promise` settles or `timeoutMs` elapses. WinRT STA
|
|
74
|
+
// async completions are delivered as messages, so the loop must be pumped for them to fire.
|
|
75
|
+
async function awaitWithPump(promise, timeoutMs = 5000) {
|
|
76
|
+
let done = false, result, error, ok;
|
|
77
|
+
promise.then((r) => { done = true; ok = true; result = r; },
|
|
78
|
+
(e) => { done = true; ok = false; error = e; });
|
|
79
|
+
const start = Date.now();
|
|
80
|
+
while (!done && Date.now() - start < timeoutMs) {
|
|
81
|
+
native.pumpMessages();
|
|
82
|
+
await new Promise((r) => setImmediate(r));
|
|
83
|
+
}
|
|
84
|
+
if (!done) throw new Error('awaitWithPump timed out');
|
|
85
|
+
if (!ok) throw error;
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Pump the WinRT message loop automatically on a Node timer so awaiting WinRT Promises is
|
|
90
|
+
// transparent (no manual `awaitWithPump`). The timer is unref'd, so it never keeps the process
|
|
91
|
+
// alive on its own. Returns a stop function. Safe to call once at startup.
|
|
92
|
+
let autoPumpTimer = null;
|
|
93
|
+
function enableAutoPump(intervalMs = 4) {
|
|
94
|
+
if (autoPumpTimer) return () => disableAutoPump();
|
|
95
|
+
autoPumpTimer = setInterval(() => { native.pumpMessages(); }, intervalMs);
|
|
96
|
+
if (typeof autoPumpTimer.unref === 'function') autoPumpTimer.unref();
|
|
97
|
+
return () => disableAutoPump();
|
|
98
|
+
}
|
|
99
|
+
function disableAutoPump() {
|
|
100
|
+
if (autoPumpTimer) { clearInterval(autoPumpTimer); autoPumpTimer = null; }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// interop.* — the full NSWinRT.interop surface (Pointer/OutParam, reference/typed-value
|
|
104
|
+
// boxing, buffer + DateTime utilities, uuid/winmd registration, zero-copy
|
|
105
|
+
// arrayBufferFromBuffer). Installed by the shared native layer so the surface is identical
|
|
106
|
+
// across Node/Bun/Deno and the standalone engine hosts.
|
|
107
|
+
native.installInterop();
|
|
108
|
+
const interop = globalThis.NSWinRT.interop;
|
|
109
|
+
|
|
110
|
+
// dotnet.* — the .NET/BCL bridge (NSWinRT.dotnet: invoke/get/fromHandle/registerNamespace,
|
|
111
|
+
// taskToPromise/asDelegate, plus NSWinRT.runOnUIThread). A no-op at the JS layer until a
|
|
112
|
+
// dotnet-bridge/publish/DotNetBridge.dll exists next to the app.
|
|
113
|
+
native.installDotnet();
|
|
114
|
+
const dotnet = globalThis.NSWinRT.dotnet;
|
|
115
|
+
|
|
116
|
+
// Windows.UI.Xaml content in a Win32 window (XAML Islands). Needs a thread that pumps messages.
|
|
117
|
+
let xamlManager = null;
|
|
118
|
+
|
|
119
|
+
function systemTheme() {
|
|
120
|
+
const vm = Windows.UI.ViewManagement;
|
|
121
|
+
const bg = new vm.UISettings().GetColorValue(vm.UIColorType.Background);
|
|
122
|
+
return bg.R < 128 ? 'dark' : 'light';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
class XamlWindow {
|
|
126
|
+
/**
|
|
127
|
+
* @param {object} [options]
|
|
128
|
+
* @param {'dialog'|'popup'} [options.style] dialog: title bar and close box; popup: borderless,
|
|
129
|
+
* not in the taskbar or Alt+Tab.
|
|
130
|
+
* @param {number|bigint|Buffer} [options.owner] owner window handle.
|
|
131
|
+
* @param {string} [options.title]
|
|
132
|
+
* @param {boolean} [options.topmost]
|
|
133
|
+
* @param {'system'|'dark'|'light'} [options.theme] title bar theme; read it back from `theme`.
|
|
134
|
+
*/
|
|
135
|
+
constructor(options = {}) {
|
|
136
|
+
if (!xamlManager) {
|
|
137
|
+
try {
|
|
138
|
+
xamlManager = Windows.UI.Xaml.Hosting.WindowsXamlManager.InitializeForCurrentThread();
|
|
139
|
+
} catch (e) {
|
|
140
|
+
if (!/0x8000FFFF/i.test(String(e && e.message))) throw e;
|
|
141
|
+
throw new Error('XamlWindow: XAML Islands are unavailable in this process. The host ' +
|
|
142
|
+
'executable\'s manifest must declare <maxversiontested Id="10.0.18362.0"/> or later.');
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const { theme = 'system', ...rest } = options;
|
|
146
|
+
this.theme = theme === 'system' ? systemTheme() : theme;
|
|
147
|
+
this.source = new Windows.UI.Xaml.Hosting.DesktopWindowXamlSource();
|
|
148
|
+
this.hwnd = native.xamlHostCreate(this.source, { ...rest, dark: this.theme === 'dark' });
|
|
149
|
+
this.closed = false;
|
|
150
|
+
this._handlers = { activate: [], deactivate: [], close: [], closed: [] };
|
|
151
|
+
native.xamlHostSetListener(this.hwnd, (type) => this._emit(type));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
get content() { return this.source.Content; }
|
|
155
|
+
set content(element) { this.source.Content = element; }
|
|
156
|
+
|
|
157
|
+
get visible() { return !this.closed && native.xamlHostIsVisible(this.hwnd); }
|
|
158
|
+
|
|
159
|
+
/** `activate`, `deactivate`, `close` (title-bar X or Alt+F4; closes when unhandled), `closed`. */
|
|
160
|
+
on(type, fn) {
|
|
161
|
+
if (!this._handlers[type]) throw new TypeError(`XamlWindow: unknown event '${type}'`);
|
|
162
|
+
this._handlers[type].push(fn);
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
off(type, fn) {
|
|
167
|
+
const list = this._handlers[type];
|
|
168
|
+
if (list && list.includes(fn)) list.splice(list.indexOf(fn), 1);
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Client size in DIPs; centered over `over`, or on the pointer's monitor at `y` (0..1). */
|
|
173
|
+
place({ width, height, over, y } = {}) {
|
|
174
|
+
native.xamlHostPlace(this.hwnd, { width, height, over, y });
|
|
175
|
+
return this;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Returns whether the window is in the foreground. */
|
|
179
|
+
show({ activate = true } = {}) { return native.xamlHostShow(this.hwnd, activate); }
|
|
180
|
+
|
|
181
|
+
hide() { if (!this.closed) native.xamlHostHide(this.hwnd); }
|
|
182
|
+
|
|
183
|
+
close() { if (!this.closed) native.xamlHostDestroy(this.hwnd); }
|
|
184
|
+
|
|
185
|
+
_emit(type) {
|
|
186
|
+
if (type === 'closed') {
|
|
187
|
+
this.closed = true;
|
|
188
|
+
try { this.source.Close(); } catch {}
|
|
189
|
+
}
|
|
190
|
+
const handlers = this._handlers[type].slice();
|
|
191
|
+
if (type === 'close' && handlers.length === 0) this.close();
|
|
192
|
+
for (const fn of handlers) {
|
|
193
|
+
// Rethrown outside the native callback so the host reports it as uncaught.
|
|
194
|
+
try { fn(); } catch (e) { setTimeout(() => { throw e; }); }
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static systemTheme() { return systemTheme(); }
|
|
199
|
+
/** The foreground window of any process, as a number (0 if none). */
|
|
200
|
+
static foregroundWindow() { return native.foregroundWindow(); }
|
|
201
|
+
/** Bring a window to the foreground; `false` if Windows refuses. */
|
|
202
|
+
static activateWindow(hwnd) { return native.activateWindow(hwnd); }
|
|
203
|
+
static enableWindow(hwnd, enabled) { native.enableWindow(hwnd, enabled); }
|
|
204
|
+
static windowProcessId(hwnd) { return native.windowProcessId(hwnd); }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
module.exports = {
|
|
208
|
+
native,
|
|
209
|
+
Windows,
|
|
210
|
+
toPromise,
|
|
211
|
+
awaitWithPump,
|
|
212
|
+
enableAutoPump,
|
|
213
|
+
disableAutoPump,
|
|
214
|
+
interop,
|
|
215
|
+
dotnet,
|
|
216
|
+
XamlWindow,
|
|
217
|
+
};
|
package/package.json
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@nativescript/windows-napi",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
4
|
-
"description": "Node-API WinRT interop for Windows.",
|
|
5
|
-
"main": "nswinrt.js",
|
|
6
|
-
"types": "index.d.ts",
|
|
7
|
-
"exports": {
|
|
8
|
-
".": "./nswinrt.js",
|
|
9
|
-
"./native": "./index.js",
|
|
10
|
-
"./nswinrt.js": "./nswinrt.js"
|
|
11
|
-
},
|
|
12
|
-
"files": [
|
|
13
|
-
"index.js",
|
|
14
|
-
"index.d.ts",
|
|
15
|
-
"nswinrt.js",
|
|
16
|
-
"windows.*.node"
|
|
17
|
-
],
|
|
18
|
-
"napi": {
|
|
19
|
-
"name": "windows",
|
|
20
|
-
"triples": {
|
|
21
|
-
"defaults": false,
|
|
22
|
-
"additional": [
|
|
23
|
-
"x86_64-pc-windows-msvc",
|
|
24
|
-
"aarch64-pc-windows-msvc"
|
|
25
|
-
]
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
"os": [
|
|
29
|
-
"win32"
|
|
30
|
-
],
|
|
31
|
-
"engines": {
|
|
32
|
-
"node": ">= 18"
|
|
33
|
-
},
|
|
34
|
-
"scripts": {
|
|
35
|
-
"build": "napi build --platform --release",
|
|
36
|
-
"build:debug": "napi build --platform",
|
|
37
|
-
"test": "node test/run-tests.js"
|
|
38
|
-
},
|
|
39
|
-
"repository": {
|
|
40
|
-
"type": "git",
|
|
41
|
-
"url": "https://github.com/NativeScript/windows",
|
|
42
|
-
"directory": "windows-napi"
|
|
43
|
-
},
|
|
44
|
-
"devDependencies": {
|
|
45
|
-
"@napi-rs/cli": "^2.18.0"
|
|
46
|
-
}
|
|
47
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@nativescript/windows-napi",
|
|
3
|
+
"version": "1.0.0-beta.22",
|
|
4
|
+
"description": "Node-API WinRT interop for Windows.",
|
|
5
|
+
"main": "nswinrt.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./nswinrt.js",
|
|
9
|
+
"./native": "./index.js",
|
|
10
|
+
"./nswinrt.js": "./nswinrt.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"index.js",
|
|
14
|
+
"index.d.ts",
|
|
15
|
+
"nswinrt.js",
|
|
16
|
+
"windows.*.node"
|
|
17
|
+
],
|
|
18
|
+
"napi": {
|
|
19
|
+
"name": "windows",
|
|
20
|
+
"triples": {
|
|
21
|
+
"defaults": false,
|
|
22
|
+
"additional": [
|
|
23
|
+
"x86_64-pc-windows-msvc",
|
|
24
|
+
"aarch64-pc-windows-msvc"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"os": [
|
|
29
|
+
"win32"
|
|
30
|
+
],
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">= 18"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "napi build --platform --release",
|
|
36
|
+
"build:debug": "napi build --platform",
|
|
37
|
+
"test": "node test/run-tests.js"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/NativeScript/windows",
|
|
42
|
+
"directory": "windows-napi"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@napi-rs/cli": "^2.18.0"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
Binary file
|
|
Binary file
|