@nstudio/nativescript-effect-ui 0.1.0 → 0.1.1
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/README.md +24 -18
- package/dist/desktop/html-services.d.ts +33 -0
- package/dist/desktop/html-services.d.ts.map +1 -0
- package/dist/desktop/html-services.js +113 -0
- package/dist/desktop/html-services.js.map +1 -0
- package/dist/linux/LinuxHost.d.ts +108 -0
- package/dist/linux/LinuxHost.d.ts.map +1 -0
- package/dist/linux/LinuxHost.js +601 -0
- package/dist/linux/LinuxHost.js.map +1 -0
- package/dist/linux/index.d.ts +3 -0
- package/dist/linux/index.d.ts.map +1 -0
- package/dist/linux/index.js +3 -0
- package/dist/linux/index.js.map +1 -0
- package/dist/linux/platform.d.ts +17 -0
- package/dist/linux/platform.d.ts.map +1 -0
- package/dist/linux/platform.js +119 -0
- package/dist/linux/platform.js.map +1 -0
- package/dist/platform/index.d.ts +1 -1
- package/dist/platform/index.d.ts.map +1 -1
- package/dist/platform/native.d.ts +1 -1
- package/dist/platform/native.d.ts.map +1 -1
- package/dist/platform/native.js +89 -14
- package/dist/platform/native.js.map +1 -1
- package/dist/windows/WindowsHost.d.ts +107 -0
- package/dist/windows/WindowsHost.d.ts.map +1 -0
- package/dist/windows/WindowsHost.js +651 -0
- package/dist/windows/WindowsHost.js.map +1 -0
- package/dist/windows/index.d.ts +3 -0
- package/dist/windows/index.d.ts.map +1 -0
- package/dist/windows/index.js +3 -0
- package/dist/windows/index.js.map +1 -0
- package/dist/windows/platform.d.ts +12 -0
- package/dist/windows/platform.d.ts.map +1 -0
- package/dist/windows/platform.js +105 -0
- package/dist/windows/platform.js.map +1 -0
- package/package.json +31 -2
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
import { Html } from '@effect/ui/html';
|
|
2
|
+
import * as Renderer from '@effect/ui/Renderer';
|
|
3
|
+
import * as Context from 'effect/Context';
|
|
4
|
+
import * as Effect from 'effect/Effect';
|
|
5
|
+
import * as Layer from 'effect/Layer';
|
|
6
|
+
import { darkPalette, estimateDesktopTextSize, layoutTree, lightPalette, makeDesktopHtmlServices, measureNode, resolveDesktopStyle, toByteArgb, } from '../desktop/html-services.js';
|
|
7
|
+
function collectElementChildren(node) {
|
|
8
|
+
const out = [];
|
|
9
|
+
for (const child of node.children) {
|
|
10
|
+
if (child.kind === 'container') {
|
|
11
|
+
out.push(...collectElementChildren(child));
|
|
12
|
+
}
|
|
13
|
+
else if (child.kind === 'element') {
|
|
14
|
+
out.push(child);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
function collectText(node) {
|
|
20
|
+
if (node.kind === 'text')
|
|
21
|
+
return node.text;
|
|
22
|
+
let out = '';
|
|
23
|
+
for (const child of node.children) {
|
|
24
|
+
if (child.kind === 'text') {
|
|
25
|
+
out += child.text;
|
|
26
|
+
}
|
|
27
|
+
else if (child.kind === 'container') {
|
|
28
|
+
out += collectText(child);
|
|
29
|
+
}
|
|
30
|
+
else if (child.kind === 'element' && child.tag === 'span') {
|
|
31
|
+
out += collectText(child);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
function hasDirectBlockElements(node) {
|
|
37
|
+
for (const child of collectElementChildren(node)) {
|
|
38
|
+
if (child.tag !== 'span')
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
function isInteractiveNode(node) {
|
|
44
|
+
if (node.tag === 'button' || node.tag === 'a')
|
|
45
|
+
return true;
|
|
46
|
+
return Boolean(node.handlers['click']);
|
|
47
|
+
}
|
|
48
|
+
export function fireWindowsEvent(start, eventName, eventPayload) {
|
|
49
|
+
let stopped = false;
|
|
50
|
+
let cur = start;
|
|
51
|
+
const ev = {
|
|
52
|
+
type: eventName,
|
|
53
|
+
target: start,
|
|
54
|
+
currentTarget: start,
|
|
55
|
+
preventDefault() { },
|
|
56
|
+
stopPropagation() {
|
|
57
|
+
stopped = true;
|
|
58
|
+
},
|
|
59
|
+
...eventPayload,
|
|
60
|
+
};
|
|
61
|
+
while (cur && !stopped) {
|
|
62
|
+
const handler = cur.handlers[eventName];
|
|
63
|
+
if (handler && cur.dispatch) {
|
|
64
|
+
ev.currentTarget = cur;
|
|
65
|
+
cur.dispatch(eventName, handler, ev);
|
|
66
|
+
}
|
|
67
|
+
cur = cur.parent;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export function hitTestWindowsItems(items, x, y) {
|
|
71
|
+
for (let i = items.length - 1; i >= 0; i--) {
|
|
72
|
+
const item = items[i];
|
|
73
|
+
if (!item.isInteractive && !item.isInput)
|
|
74
|
+
continue;
|
|
75
|
+
const f = item.frame;
|
|
76
|
+
if (x >= f.x && x <= f.x + f.width && y >= f.y && y <= f.y + f.height) {
|
|
77
|
+
return item;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Creates a `Renderer.Host<WindowsNode>` bound to any `WindowsHostBackend`
|
|
84
|
+
* (WinUI 3, XAML Islands, WinRT Composition, or test backend).
|
|
85
|
+
*/
|
|
86
|
+
export function makeWindowsHost(options) {
|
|
87
|
+
const activeDialogs = new Map();
|
|
88
|
+
let latestItems = [];
|
|
89
|
+
let lastTitle;
|
|
90
|
+
const createNode = (kind, tag, text = '') => ({
|
|
91
|
+
kind,
|
|
92
|
+
tag,
|
|
93
|
+
parent: undefined,
|
|
94
|
+
children: [],
|
|
95
|
+
text,
|
|
96
|
+
className: '',
|
|
97
|
+
id: '',
|
|
98
|
+
attrs: {},
|
|
99
|
+
props: {},
|
|
100
|
+
handlers: {},
|
|
101
|
+
dispatch: undefined,
|
|
102
|
+
nativeHandle: undefined,
|
|
103
|
+
dialogHandle: undefined,
|
|
104
|
+
layoutNode: undefined,
|
|
105
|
+
});
|
|
106
|
+
const rootContainer = createNode('container', '#root');
|
|
107
|
+
const buildLayoutNode = (node, inheritedColor, dark, isDialogRoot) => {
|
|
108
|
+
const style = resolveDesktopStyle(node.tag, node.className, node.attrs, inheritedColor, dark);
|
|
109
|
+
if (isDialogRoot) {
|
|
110
|
+
style.display = 'flex';
|
|
111
|
+
style.flexDirection = 'column';
|
|
112
|
+
style.padTop = 20;
|
|
113
|
+
style.padRight = 20;
|
|
114
|
+
style.padBottom = 20;
|
|
115
|
+
style.padLeft = 20;
|
|
116
|
+
style.gap = 12;
|
|
117
|
+
style.fullWidth = true;
|
|
118
|
+
}
|
|
119
|
+
const isPortal = !isDialogRoot && node.tag === 'dialog';
|
|
120
|
+
const hasBlocks = hasDirectBlockElements(node);
|
|
121
|
+
const leafText = !hasBlocks && node.tag !== 'input' ? collectText(node).trim() : undefined;
|
|
122
|
+
const childLayouts = [];
|
|
123
|
+
if (hasBlocks && !isPortal) {
|
|
124
|
+
for (const child of collectElementChildren(node)) {
|
|
125
|
+
childLayouts.push(buildLayoutNode(child, style.color, dark, false));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const ln = {
|
|
129
|
+
tag: node.tag,
|
|
130
|
+
isPortal,
|
|
131
|
+
style,
|
|
132
|
+
text: leafText && leafText.length > 0 ? leafText : undefined,
|
|
133
|
+
children: childLayouts,
|
|
134
|
+
frame: { x: 0, y: 0, width: 0, height: 0 },
|
|
135
|
+
};
|
|
136
|
+
node.layoutNode = ln;
|
|
137
|
+
return ln;
|
|
138
|
+
};
|
|
139
|
+
const flattenVisualItems = (node, offsetX, offsetY, out, isDialogRoot) => {
|
|
140
|
+
const ln = node.layoutNode;
|
|
141
|
+
if (!ln || ln.style.display === 'none' || (ln.isPortal && !isDialogRoot))
|
|
142
|
+
return;
|
|
143
|
+
const absFrame = {
|
|
144
|
+
x: offsetX + ln.frame.x,
|
|
145
|
+
y: offsetY + ln.frame.y,
|
|
146
|
+
width: ln.frame.width,
|
|
147
|
+
height: ln.frame.height,
|
|
148
|
+
};
|
|
149
|
+
const isInput = node.tag === 'input';
|
|
150
|
+
const rawVal = node.props['value'] ?? node.attrs['value'] ?? '';
|
|
151
|
+
out.push({
|
|
152
|
+
node,
|
|
153
|
+
frame: absFrame,
|
|
154
|
+
style: ln.style,
|
|
155
|
+
text: ln.text,
|
|
156
|
+
isInteractive: isInteractiveNode(node),
|
|
157
|
+
isInput,
|
|
158
|
+
inputValue: typeof rawVal === 'string' ? rawVal : String(rawVal),
|
|
159
|
+
placeholder: node.attrs['placeholder'] ?? '',
|
|
160
|
+
});
|
|
161
|
+
if (hasDirectBlockElements(node)) {
|
|
162
|
+
for (const child of collectElementChildren(node)) {
|
|
163
|
+
flattenVisualItems(child, absFrame.x, absFrame.y, out, false);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
const syncDialogs = (dark) => {
|
|
168
|
+
const found = [];
|
|
169
|
+
const walk = (n) => {
|
|
170
|
+
if (n.kind === 'element' && n.tag === 'dialog')
|
|
171
|
+
found.push(n);
|
|
172
|
+
for (const c of n.children)
|
|
173
|
+
walk(c);
|
|
174
|
+
};
|
|
175
|
+
walk(rootContainer);
|
|
176
|
+
const pal = dark ? darkPalette : lightPalette;
|
|
177
|
+
for (const dlg of found) {
|
|
178
|
+
const openVal = dlg.props['open'];
|
|
179
|
+
const wantOpen = openVal === 'modal' || openVal === true;
|
|
180
|
+
const existing = activeDialogs.get(dlg);
|
|
181
|
+
if (wantOpen) {
|
|
182
|
+
const dlgW = 420;
|
|
183
|
+
const ln = buildLayoutNode(dlg, pal.foreground, dark, true);
|
|
184
|
+
const m = measureNode(ln, dlgW, estimateDesktopTextSize);
|
|
185
|
+
const dlgH = Math.max(240, m.height + 16);
|
|
186
|
+
layoutTree(ln, 0, 0, dlgW, dlgH, estimateDesktopTextSize);
|
|
187
|
+
const items = [];
|
|
188
|
+
flattenVisualItems(dlg, 0, 0, items, true);
|
|
189
|
+
if (!existing) {
|
|
190
|
+
const handle = options.backend.openDialog?.(dlg, items, dlgW, dlgH, dark, () => {
|
|
191
|
+
activeDialogs.delete(dlg);
|
|
192
|
+
dlg.dialogHandle = undefined;
|
|
193
|
+
fireWindowsEvent(dlg, 'cancel', { cancelable: true });
|
|
194
|
+
});
|
|
195
|
+
dlg.dialogHandle = handle;
|
|
196
|
+
activeDialogs.set(dlg, { handle, items });
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
existing.items = items;
|
|
200
|
+
options.backend.updateDialog?.(existing.handle, items, dlgW, dlgH, dark);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
else if (existing) {
|
|
204
|
+
activeDialogs.delete(dlg);
|
|
205
|
+
dlg.dialogHandle = undefined;
|
|
206
|
+
options.backend.closeDialog?.(existing.handle);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
const relayout = () => {
|
|
211
|
+
const dark = options.getDarkMode();
|
|
212
|
+
const pal = dark ? darkPalette : lightPalette;
|
|
213
|
+
const viewportW = Math.max(320, options.getViewportWidth());
|
|
214
|
+
const topElements = collectElementChildren(rootContainer);
|
|
215
|
+
const primary = topElements[0];
|
|
216
|
+
if (!primary)
|
|
217
|
+
return;
|
|
218
|
+
const ln = buildLayoutNode(primary, pal.foreground, dark, false);
|
|
219
|
+
const m = measureNode(ln, viewportW, estimateDesktopTextSize);
|
|
220
|
+
const totalH = Math.max(m.height, 100);
|
|
221
|
+
layoutTree(ln, 0, 0, viewportW, totalH, estimateDesktopTextSize);
|
|
222
|
+
const items = [];
|
|
223
|
+
flattenVisualItems(primary, 0, 0, items, false);
|
|
224
|
+
latestItems = items;
|
|
225
|
+
options.backend.renderFrame(items, totalH, viewportW, dark);
|
|
226
|
+
syncDialogs(dark);
|
|
227
|
+
};
|
|
228
|
+
const detach = (node) => {
|
|
229
|
+
const oldParent = node.parent;
|
|
230
|
+
if (!oldParent)
|
|
231
|
+
return;
|
|
232
|
+
const idx = oldParent.children.indexOf(node);
|
|
233
|
+
if (idx >= 0)
|
|
234
|
+
oldParent.children.splice(idx, 1);
|
|
235
|
+
node.parent = undefined;
|
|
236
|
+
};
|
|
237
|
+
const host = {
|
|
238
|
+
createElement(tag) {
|
|
239
|
+
return createNode('element', tag.toLowerCase());
|
|
240
|
+
},
|
|
241
|
+
createText(text) {
|
|
242
|
+
return createNode('text', '#text', text);
|
|
243
|
+
},
|
|
244
|
+
createContainer() {
|
|
245
|
+
return createNode('container', '#container');
|
|
246
|
+
},
|
|
247
|
+
setText(node, text) {
|
|
248
|
+
node.text = text;
|
|
249
|
+
},
|
|
250
|
+
setAttr(node, name, value) {
|
|
251
|
+
if (name === 'class')
|
|
252
|
+
node.className = value ?? '';
|
|
253
|
+
else if (name === 'id')
|
|
254
|
+
node.id = value ?? '';
|
|
255
|
+
node.attrs[name] = value;
|
|
256
|
+
},
|
|
257
|
+
setProp(node, name, value) {
|
|
258
|
+
node.props[name] = value;
|
|
259
|
+
},
|
|
260
|
+
insertBefore(parent, node, ref) {
|
|
261
|
+
if (node.parent)
|
|
262
|
+
detach(node);
|
|
263
|
+
const refIdx = ref === undefined ? -1 : parent.children.indexOf(ref);
|
|
264
|
+
if (refIdx < 0)
|
|
265
|
+
parent.children.push(node);
|
|
266
|
+
else
|
|
267
|
+
parent.children.splice(refIdx, 0, node);
|
|
268
|
+
node.parent = parent;
|
|
269
|
+
},
|
|
270
|
+
remove(node) {
|
|
271
|
+
detach(node);
|
|
272
|
+
},
|
|
273
|
+
removeChildren(_parent, nodes) {
|
|
274
|
+
for (const n of nodes)
|
|
275
|
+
detach(n);
|
|
276
|
+
},
|
|
277
|
+
parentOf(node) {
|
|
278
|
+
return node.parent;
|
|
279
|
+
},
|
|
280
|
+
nextOf(node) {
|
|
281
|
+
const p = node.parent;
|
|
282
|
+
if (!p)
|
|
283
|
+
return undefined;
|
|
284
|
+
const idx = p.children.indexOf(node);
|
|
285
|
+
return idx >= 0 ? p.children[idx + 1] : undefined;
|
|
286
|
+
},
|
|
287
|
+
setEvents(node, handlers, dispatch) {
|
|
288
|
+
node.handlers = handlers;
|
|
289
|
+
node.dispatch = dispatch;
|
|
290
|
+
},
|
|
291
|
+
elements: [Html.name],
|
|
292
|
+
controlled: new Set(['value', 'checked', 'open']),
|
|
293
|
+
runsMounts: true,
|
|
294
|
+
commit() {
|
|
295
|
+
relayout();
|
|
296
|
+
},
|
|
297
|
+
head(head) {
|
|
298
|
+
const title = head?.title;
|
|
299
|
+
if (title !== undefined && title !== lastTitle) {
|
|
300
|
+
lastTitle = title;
|
|
301
|
+
options.backend.setTitle?.(title);
|
|
302
|
+
options.onTitle?.(title);
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
const triggerClick = (node) => {
|
|
307
|
+
fireWindowsEvent(node, 'click', { button: 0 });
|
|
308
|
+
};
|
|
309
|
+
const triggerInput = (node, value) => {
|
|
310
|
+
node.props['value'] = value;
|
|
311
|
+
fireWindowsEvent(node, 'input', { target: { value } });
|
|
312
|
+
fireWindowsEvent(node, 'change', { target: { value } });
|
|
313
|
+
};
|
|
314
|
+
return {
|
|
315
|
+
host,
|
|
316
|
+
rootContainer,
|
|
317
|
+
relayout,
|
|
318
|
+
getItems: () => latestItems,
|
|
319
|
+
getDialogItems: (dialog) => activeDialogs.get(dialog)?.items ?? [],
|
|
320
|
+
clickAt: (x, y) => {
|
|
321
|
+
const hit = hitTestWindowsItems(latestItems, x, y);
|
|
322
|
+
if (!hit)
|
|
323
|
+
return false;
|
|
324
|
+
triggerClick(hit.node);
|
|
325
|
+
return true;
|
|
326
|
+
},
|
|
327
|
+
triggerClick,
|
|
328
|
+
triggerInput,
|
|
329
|
+
dispose: () => {
|
|
330
|
+
for (const [, entry] of activeDialogs) {
|
|
331
|
+
options.backend.closeDialog?.(entry.handle);
|
|
332
|
+
}
|
|
333
|
+
activeDialogs.clear();
|
|
334
|
+
options.backend.dispose?.();
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Builds a live WinUI 3 / XAML Islands backend when `Microsoft.UI.Xaml`
|
|
340
|
+
* (`@nativescript/windows`) or `Windows.UI.Xaml` (`@nativescript/windows-napi`
|
|
341
|
+
* `XamlWindow`) is available on the host thread.
|
|
342
|
+
*/
|
|
343
|
+
function createXamlBackend(xamlNs, rootScrollViewer, onClickNode, onInputNode, setWindowTitle, xamlWindowCtor) {
|
|
344
|
+
const Controls = xamlNs.Controls;
|
|
345
|
+
const Media = xamlNs.Media;
|
|
346
|
+
const NSWinRT = globalThis.NSWinRT;
|
|
347
|
+
const makeBrush = (c) => {
|
|
348
|
+
const brush = new Media.SolidColorBrush();
|
|
349
|
+
brush.Color = toByteArgb(c);
|
|
350
|
+
return brush;
|
|
351
|
+
};
|
|
352
|
+
const renderIntoCanvas = (canvas, items, width, height, dark) => {
|
|
353
|
+
const pal = dark ? darkPalette : lightPalette;
|
|
354
|
+
canvas.Width = width;
|
|
355
|
+
canvas.Height = height;
|
|
356
|
+
canvas.Background = makeBrush(pal.background);
|
|
357
|
+
canvas.Children.Clear();
|
|
358
|
+
for (const item of items) {
|
|
359
|
+
const { frame, style, node } = item;
|
|
360
|
+
let element;
|
|
361
|
+
if (item.isInput) {
|
|
362
|
+
const box = new Controls.TextBox();
|
|
363
|
+
box.Text = item.inputValue;
|
|
364
|
+
box.PlaceholderText = item.placeholder;
|
|
365
|
+
box.FontSize = style.fontSize;
|
|
366
|
+
box.Foreground = makeBrush(style.color);
|
|
367
|
+
if (style.backgroundColor)
|
|
368
|
+
box.Background = makeBrush(style.backgroundColor);
|
|
369
|
+
const onChanged = () => onInputNode(node, String(box.Text ?? ''));
|
|
370
|
+
box.TextChanged = NSWinRT?.asDelegate
|
|
371
|
+
? NSWinRT.asDelegate(`${xamlNs.__typeName__ ?? 'Microsoft.UI.Xaml'}.Controls.TextChangedEventHandler`, onChanged)
|
|
372
|
+
: onChanged;
|
|
373
|
+
element = box;
|
|
374
|
+
}
|
|
375
|
+
else if (item.isInteractive) {
|
|
376
|
+
const btn = new Controls.Button();
|
|
377
|
+
btn.Content = item.text ?? '';
|
|
378
|
+
btn.FontSize = style.fontSize;
|
|
379
|
+
btn.Foreground = makeBrush(style.color);
|
|
380
|
+
if (style.backgroundColor)
|
|
381
|
+
btn.Background = makeBrush(style.backgroundColor);
|
|
382
|
+
if (style.borderColor && style.borderWidth > 0) {
|
|
383
|
+
btn.BorderBrush = makeBrush(style.borderColor);
|
|
384
|
+
btn.BorderThickness = {
|
|
385
|
+
Left: style.borderWidth,
|
|
386
|
+
Top: style.borderWidth,
|
|
387
|
+
Right: style.borderWidth,
|
|
388
|
+
Bottom: style.borderWidth,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
if (style.borderRadius > 0) {
|
|
392
|
+
btn.CornerRadius = {
|
|
393
|
+
TopLeft: style.borderRadius,
|
|
394
|
+
TopRight: style.borderRadius,
|
|
395
|
+
BottomRight: style.borderRadius,
|
|
396
|
+
BottomLeft: style.borderRadius,
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
const onClick = () => onClickNode(node);
|
|
400
|
+
btn.Click = NSWinRT?.asDelegate
|
|
401
|
+
? NSWinRT.asDelegate(`${xamlNs.__typeName__ ?? 'Microsoft.UI.Xaml'}.RoutedEventHandler`, onClick)
|
|
402
|
+
: onClick;
|
|
403
|
+
element = btn;
|
|
404
|
+
}
|
|
405
|
+
else if (item.text && item.text.length > 0 && !style.backgroundColor && style.borderWidth === 0) {
|
|
406
|
+
const tb = new Controls.TextBlock();
|
|
407
|
+
tb.Text = item.text;
|
|
408
|
+
tb.FontSize = style.fontSize;
|
|
409
|
+
tb.Foreground = makeBrush(style.color);
|
|
410
|
+
tb.TextWrapping = 2; // TextWrapping.Wrap
|
|
411
|
+
element = tb;
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
const border = new Controls.Border();
|
|
415
|
+
if (style.backgroundColor)
|
|
416
|
+
border.Background = makeBrush(style.backgroundColor);
|
|
417
|
+
if (style.borderColor && style.borderWidth > 0) {
|
|
418
|
+
border.BorderBrush = makeBrush(style.borderColor);
|
|
419
|
+
border.BorderThickness = {
|
|
420
|
+
Left: style.borderWidth,
|
|
421
|
+
Top: style.borderWidth,
|
|
422
|
+
Right: style.borderWidth,
|
|
423
|
+
Bottom: style.borderWidth,
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
if (style.borderRadius > 0) {
|
|
427
|
+
border.CornerRadius = {
|
|
428
|
+
TopLeft: style.borderRadius,
|
|
429
|
+
TopRight: style.borderRadius,
|
|
430
|
+
BottomRight: style.borderRadius,
|
|
431
|
+
BottomLeft: style.borderRadius,
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
if (item.text && item.text.length > 0) {
|
|
435
|
+
const tb = new Controls.TextBlock();
|
|
436
|
+
tb.Text = item.text;
|
|
437
|
+
tb.FontSize = style.fontSize;
|
|
438
|
+
tb.Foreground = makeBrush(style.color);
|
|
439
|
+
tb.TextWrapping = 2;
|
|
440
|
+
tb.Margin = {
|
|
441
|
+
Left: style.padLeft,
|
|
442
|
+
Top: style.padTop,
|
|
443
|
+
Right: style.padRight,
|
|
444
|
+
Bottom: style.padBottom,
|
|
445
|
+
};
|
|
446
|
+
border.Child = tb;
|
|
447
|
+
}
|
|
448
|
+
element = border;
|
|
449
|
+
}
|
|
450
|
+
element.Width = Math.max(1, frame.width);
|
|
451
|
+
element.Height = Math.max(1, frame.height);
|
|
452
|
+
Controls.Canvas.SetLeft(element, frame.x);
|
|
453
|
+
Controls.Canvas.SetTop(element, frame.y);
|
|
454
|
+
canvas.Children.Append(element);
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
const rootCanvas = new Controls.Canvas();
|
|
458
|
+
rootScrollViewer.Content = rootCanvas;
|
|
459
|
+
return {
|
|
460
|
+
mode: xamlWindowCtor ? 'xaml-islands' : 'winui',
|
|
461
|
+
renderFrame: (items, contentHeight, viewportWidth, dark) => {
|
|
462
|
+
renderIntoCanvas(rootCanvas, items, viewportWidth, contentHeight, dark);
|
|
463
|
+
},
|
|
464
|
+
openDialog: (_dlg, items, width, height, dark, onClose) => {
|
|
465
|
+
if (!xamlWindowCtor)
|
|
466
|
+
return undefined;
|
|
467
|
+
const win = new xamlWindowCtor({ style: 'dialog', title: 'Dialog', theme: dark ? 'dark' : 'light' });
|
|
468
|
+
const canvas = new Controls.Canvas();
|
|
469
|
+
renderIntoCanvas(canvas, items, width, height, dark);
|
|
470
|
+
win.content = canvas;
|
|
471
|
+
win.on?.('closed', onClose);
|
|
472
|
+
win.place({ width, height });
|
|
473
|
+
win.show();
|
|
474
|
+
return { win, canvas };
|
|
475
|
+
},
|
|
476
|
+
updateDialog: (handle, items, width, height, dark) => {
|
|
477
|
+
if (handle?.canvas) {
|
|
478
|
+
renderIntoCanvas(handle.canvas, items, width, height, dark);
|
|
479
|
+
}
|
|
480
|
+
},
|
|
481
|
+
closeDialog: (handle) => {
|
|
482
|
+
try {
|
|
483
|
+
handle?.win?.close?.();
|
|
484
|
+
}
|
|
485
|
+
catch {
|
|
486
|
+
// Ignore
|
|
487
|
+
}
|
|
488
|
+
},
|
|
489
|
+
setTitle: setWindowTitle,
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Builds a WinRT Composition backend using `@nativescript/windows-napi`
|
|
494
|
+
* `createWindow` + `attachCompositorToWindow` (`Windows.UI.Composition`).
|
|
495
|
+
* Works in plain Node.js, Bun, and Deno on Windows without a XAML manifest.
|
|
496
|
+
*/
|
|
497
|
+
function createCompositionBackend(Windows, native, hwnd) {
|
|
498
|
+
const compositor = new Windows.UI.Composition.Compositor();
|
|
499
|
+
const target = native.attachCompositorToWindow(compositor, hwnd);
|
|
500
|
+
const rootVisual = compositor.CreateSpriteVisual();
|
|
501
|
+
target.Root = rootVisual;
|
|
502
|
+
return {
|
|
503
|
+
mode: 'composition',
|
|
504
|
+
renderFrame: (items, contentHeight, viewportWidth, dark) => {
|
|
505
|
+
const pal = dark ? darkPalette : lightPalette;
|
|
506
|
+
rootVisual.Size = { X: viewportWidth, Y: Math.max(contentHeight, 100) };
|
|
507
|
+
rootVisual.Brush = compositor.CreateColorBrush(toByteArgb(pal.background));
|
|
508
|
+
rootVisual.Children.RemoveAll();
|
|
509
|
+
for (const item of items) {
|
|
510
|
+
if (!item.style.backgroundColor && item.style.borderWidth === 0)
|
|
511
|
+
continue;
|
|
512
|
+
const visual = compositor.CreateSpriteVisual();
|
|
513
|
+
visual.Offset = { X: item.frame.x, Y: item.frame.y, Z: 0 };
|
|
514
|
+
visual.Size = { X: Math.max(1, item.frame.width), Y: Math.max(1, item.frame.height) };
|
|
515
|
+
const fill = item.style.backgroundColor ?? item.style.borderColor ?? pal.card;
|
|
516
|
+
visual.Brush = compositor.CreateColorBrush(toByteArgb(fill));
|
|
517
|
+
rootVisual.Children.InsertAtTop(visual);
|
|
518
|
+
}
|
|
519
|
+
},
|
|
520
|
+
setTitle: (title) => {
|
|
521
|
+
try {
|
|
522
|
+
native.setWindowTitle(hwnd, title);
|
|
523
|
+
}
|
|
524
|
+
catch {
|
|
525
|
+
// Ignore
|
|
526
|
+
}
|
|
527
|
+
},
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Launches a native Windows desktop application rendering any shared
|
|
532
|
+
* `@effect/ui` HTML component (`PulseApp`, `@effect/ui-kit`, etc.) via
|
|
533
|
+
* `@nativescript/windows` (WinUI 3) or `@nativescript/windows-napi`
|
|
534
|
+
* (XAML Islands / WinRT Composition).
|
|
535
|
+
*/
|
|
536
|
+
export async function run(component, props, options) {
|
|
537
|
+
const width = options?.width ?? 460;
|
|
538
|
+
const height = options?.height ?? 840;
|
|
539
|
+
const title = options?.title ?? 'Effect Pulse';
|
|
540
|
+
const isDark = () => options?.appearance === 'dark';
|
|
541
|
+
const gWin = globalThis;
|
|
542
|
+
let api;
|
|
543
|
+
let winuiMode = false;
|
|
544
|
+
if (gWin.Microsoft?.UI?.Xaml?.Controls?.Canvas) {
|
|
545
|
+
// Running inside `@nativescript/windows` V8 WinUI 3 app runtime.
|
|
546
|
+
winuiMode = true;
|
|
547
|
+
api = { Microsoft: gWin.Microsoft, Windows: gWin.Windows };
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
// Running inside Node.js, Bun, or Deno via `@nativescript/windows-napi`.
|
|
551
|
+
const modName = '@nativescript/windows-napi';
|
|
552
|
+
api = await import(modName);
|
|
553
|
+
if (api.Windows && !gWin.Windows)
|
|
554
|
+
gWin.Windows = api.Windows;
|
|
555
|
+
}
|
|
556
|
+
let currentViewportW = width;
|
|
557
|
+
let hostHandle;
|
|
558
|
+
let hwnd;
|
|
559
|
+
let nativeMod;
|
|
560
|
+
let backend;
|
|
561
|
+
if (winuiMode) {
|
|
562
|
+
const win = new api.Microsoft.UI.Xaml.Window();
|
|
563
|
+
win.Title = title;
|
|
564
|
+
const scroller = new api.Microsoft.UI.Xaml.Controls.ScrollViewer();
|
|
565
|
+
win.Content = scroller;
|
|
566
|
+
backend = createXamlBackend(api.Microsoft.UI.Xaml, scroller, (node) => hostHandle?.triggerClick(node), (node, val) => hostHandle?.triggerInput(node, val), (t) => {
|
|
567
|
+
win.Title = t;
|
|
568
|
+
});
|
|
569
|
+
win.Activate();
|
|
570
|
+
}
|
|
571
|
+
else {
|
|
572
|
+
const Windows = api.Windows ?? api.default?.Windows;
|
|
573
|
+
const XamlWindow = api.XamlWindow ?? api.default?.XamlWindow;
|
|
574
|
+
nativeMod = api.native ?? api.default?.native ?? api;
|
|
575
|
+
let xamlWindow;
|
|
576
|
+
if (XamlWindow && Windows?.UI?.Xaml?.Controls?.ScrollViewer) {
|
|
577
|
+
try {
|
|
578
|
+
xamlWindow = new XamlWindow({
|
|
579
|
+
style: 'dialog',
|
|
580
|
+
title,
|
|
581
|
+
theme: options?.appearance ?? 'system',
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
catch {
|
|
585
|
+
// Plain node.exe/bun.exe without XAML Islands manifest falls back to Composition.
|
|
586
|
+
xamlWindow = undefined;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
if (xamlWindow) {
|
|
590
|
+
const scroller = new Windows.UI.Xaml.Controls.ScrollViewer();
|
|
591
|
+
xamlWindow.content = scroller;
|
|
592
|
+
backend = createXamlBackend(Windows.UI.Xaml, scroller, (node) => hostHandle?.triggerClick(node), (node, val) => hostHandle?.triggerInput(node, val), () => { }, XamlWindow);
|
|
593
|
+
xamlWindow.place({ width, height });
|
|
594
|
+
xamlWindow.show();
|
|
595
|
+
}
|
|
596
|
+
else {
|
|
597
|
+
hwnd = nativeMod.createWindow(title, width, height);
|
|
598
|
+
const size = nativeMod.getWindowSize?.(hwnd);
|
|
599
|
+
if (size?.width && size.width > 0)
|
|
600
|
+
currentViewportW = size.width;
|
|
601
|
+
backend = createCompositionBackend(Windows, nativeMod, hwnd);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
hostHandle = makeWindowsHost({
|
|
605
|
+
getViewportWidth: () => currentViewportW,
|
|
606
|
+
getDarkMode: isDark,
|
|
607
|
+
backend,
|
|
608
|
+
});
|
|
609
|
+
const mountLayer = Layer.effectDiscard(Effect.provideContext(Renderer.mount(component, props, hostHandle.rootContainer, {
|
|
610
|
+
fallback: options?.fallback,
|
|
611
|
+
onEmit: options?.onEmit,
|
|
612
|
+
}), Context.make(Renderer.Renderer, { host: hostHandle.host }).pipe(Context.merge(makeDesktopHtmlServices(isDark)))));
|
|
613
|
+
const provided = options?.layer ? Layer.provide(mountLayer, options.layer) : mountLayer;
|
|
614
|
+
Effect.runFork(Layer.launch(provided));
|
|
615
|
+
if (!winuiMode && nativeMod?.pumpMessages) {
|
|
616
|
+
const start = Date.now();
|
|
617
|
+
await new Promise((resolve) => {
|
|
618
|
+
const timer = setInterval(() => {
|
|
619
|
+
nativeMod.pumpMessages();
|
|
620
|
+
if (hwnd && nativeMod.pollWindowEvents) {
|
|
621
|
+
const events = nativeMod.pollWindowEvents(hwnd);
|
|
622
|
+
for (const ev of events) {
|
|
623
|
+
if (ev.type === 'pointerdown') {
|
|
624
|
+
hostHandle?.clickAt(ev.x, ev.y);
|
|
625
|
+
}
|
|
626
|
+
else if (ev.type === 'resize' && ev.width > 0) {
|
|
627
|
+
currentViewportW = ev.width;
|
|
628
|
+
hostHandle?.relayout();
|
|
629
|
+
}
|
|
630
|
+
else if (ev.type === 'close') {
|
|
631
|
+
clearInterval(timer);
|
|
632
|
+
hostHandle?.dispose();
|
|
633
|
+
resolve();
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
if (options?.autoCloseMs && Date.now() - start >= options.autoCloseMs) {
|
|
639
|
+
clearInterval(timer);
|
|
640
|
+
hostHandle?.dispose();
|
|
641
|
+
resolve();
|
|
642
|
+
}
|
|
643
|
+
}, 16);
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
export const WindowsHost = {
|
|
648
|
+
makeWindowsHost,
|
|
649
|
+
run,
|
|
650
|
+
};
|
|
651
|
+
//# sourceMappingURL=WindowsHost.js.map
|