@nstudio/nativescript-effect-ui 0.0.0-stage → 0.1.0

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.
Files changed (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +80 -2
  3. package/dist/crash/index.d.ts +49 -0
  4. package/dist/crash/index.d.ts.map +1 -0
  5. package/dist/crash/index.js +199 -0
  6. package/dist/crash/index.js.map +1 -0
  7. package/dist/devtools/index.d.ts +77 -0
  8. package/dist/devtools/index.d.ts.map +1 -0
  9. package/dist/devtools/index.js +158 -0
  10. package/dist/devtools/index.js.map +1 -0
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +2 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/keyboard/index.d.ts +97 -0
  16. package/dist/keyboard/index.d.ts.map +1 -0
  17. package/dist/keyboard/index.js +226 -0
  18. package/dist/keyboard/index.js.map +1 -0
  19. package/dist/list/index.d.ts +91 -0
  20. package/dist/list/index.d.ts.map +1 -0
  21. package/dist/list/index.js +192 -0
  22. package/dist/list/index.js.map +1 -0
  23. package/dist/macos/MacosHost.d.ts +77 -0
  24. package/dist/macos/MacosHost.d.ts.map +1 -0
  25. package/dist/macos/MacosHost.js +878 -0
  26. package/dist/macos/MacosHost.js.map +1 -0
  27. package/dist/macos/index.d.ts +5 -0
  28. package/dist/macos/index.d.ts.map +1 -0
  29. package/dist/macos/index.js +5 -0
  30. package/dist/macos/index.js.map +1 -0
  31. package/dist/macos/layout.d.ts +39 -0
  32. package/dist/macos/layout.d.ts.map +1 -0
  33. package/dist/macos/layout.js +226 -0
  34. package/dist/macos/layout.js.map +1 -0
  35. package/dist/macos/platform.d.ts +12 -0
  36. package/dist/macos/platform.d.ts.map +1 -0
  37. package/dist/macos/platform.js +103 -0
  38. package/dist/macos/platform.js.map +1 -0
  39. package/dist/macos/styles.d.ts +66 -0
  40. package/dist/macos/styles.d.ts.map +1 -0
  41. package/dist/macos/styles.js +350 -0
  42. package/dist/macos/styles.js.map +1 -0
  43. package/dist/mason/MasonHost.d.ts +102 -0
  44. package/dist/mason/MasonHost.d.ts.map +1 -0
  45. package/dist/mason/MasonHost.js +413 -0
  46. package/dist/mason/MasonHost.js.map +1 -0
  47. package/dist/mason/dialog.d.ts +23 -0
  48. package/dist/mason/dialog.d.ts.map +1 -0
  49. package/dist/mason/dialog.js +186 -0
  50. package/dist/mason/dialog.js.map +1 -0
  51. package/dist/mason/elements.d.ts +50 -0
  52. package/dist/mason/elements.d.ts.map +1 -0
  53. package/dist/mason/elements.js +194 -0
  54. package/dist/mason/elements.js.map +1 -0
  55. package/dist/mason/events.d.ts +37 -0
  56. package/dist/mason/events.d.ts.map +1 -0
  57. package/dist/mason/events.js +304 -0
  58. package/dist/mason/events.js.map +1 -0
  59. package/dist/mason/index.d.ts +6 -0
  60. package/dist/mason/index.d.ts.map +1 -0
  61. package/dist/mason/index.js +6 -0
  62. package/dist/mason/index.js.map +1 -0
  63. package/dist/mason/services.d.ts +36 -0
  64. package/dist/mason/services.d.ts.map +1 -0
  65. package/dist/mason/services.js +268 -0
  66. package/dist/mason/services.js.map +1 -0
  67. package/dist/mason/styles.d.ts +20 -0
  68. package/dist/mason/styles.d.ts.map +1 -0
  69. package/dist/mason/styles.js +269 -0
  70. package/dist/mason/styles.js.map +1 -0
  71. package/dist/motion/index.d.ts +179 -0
  72. package/dist/motion/index.d.ts.map +1 -0
  73. package/dist/motion/index.js +332 -0
  74. package/dist/motion/index.js.map +1 -0
  75. package/dist/native/Native.d.ts +163 -0
  76. package/dist/native/Native.d.ts.map +1 -0
  77. package/dist/native/Native.js +144 -0
  78. package/dist/native/Native.js.map +1 -0
  79. package/dist/native/NativeCommand.d.ts +36 -0
  80. package/dist/native/NativeCommand.d.ts.map +1 -0
  81. package/dist/native/NativeCommand.js +38 -0
  82. package/dist/native/NativeCommand.js.map +1 -0
  83. package/dist/native/NativeHost.d.ts +46 -0
  84. package/dist/native/NativeHost.d.ts.map +1 -0
  85. package/dist/native/NativeHost.js +346 -0
  86. package/dist/native/NativeHost.js.map +1 -0
  87. package/dist/native/index.d.ts +4 -0
  88. package/dist/native/index.d.ts.map +1 -0
  89. package/dist/native/index.js +4 -0
  90. package/dist/native/index.js.map +1 -0
  91. package/dist/platform/index.d.ts +86 -0
  92. package/dist/platform/index.d.ts.map +1 -0
  93. package/dist/platform/index.js +131 -0
  94. package/dist/platform/index.js.map +1 -0
  95. package/dist/platform/native.d.ts +12 -0
  96. package/dist/platform/native.d.ts.map +1 -0
  97. package/dist/platform/native.js +127 -0
  98. package/dist/platform/native.js.map +1 -0
  99. package/dist/sheet/index.d.ts +113 -0
  100. package/dist/sheet/index.d.ts.map +1 -0
  101. package/dist/sheet/index.js +202 -0
  102. package/dist/sheet/index.js.map +1 -0
  103. package/dist/testing/MockMasonHost.d.ts +48 -0
  104. package/dist/testing/MockMasonHost.d.ts.map +1 -0
  105. package/dist/testing/MockMasonHost.js +142 -0
  106. package/dist/testing/MockMasonHost.js.map +1 -0
  107. package/dist/testing/index.d.ts +2 -0
  108. package/dist/testing/index.d.ts.map +1 -0
  109. package/dist/testing/index.js +2 -0
  110. package/dist/testing/index.js.map +1 -0
  111. package/package.json +116 -3
@@ -0,0 +1,878 @@
1
+ import { Html, HtmlSubscription } 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 { layoutTree, measureNode } from './layout.js';
7
+ import { darkPalette, lightPalette, resolveMacosStyle, } from './styles.js';
8
+ // AppKit classes initialized lazily once `@nativescript/macos-node-api` is loaded.
9
+ let classesReady = false;
10
+ let FlippedNSViewCtor;
11
+ let ClickableNSViewCtor;
12
+ let InputDelegateCtor;
13
+ let WindowDelegateCtor;
14
+ let AppDelegateCtor;
15
+ function g(name) {
16
+ return globalThis[name];
17
+ }
18
+ function ensureAppKitClasses() {
19
+ if (classesReady)
20
+ return;
21
+ classesReady = true;
22
+ const NSViewCls = g('NSView');
23
+ const NSObjectCls = g('NSObject');
24
+ const NativeClassFn = g('NativeClass');
25
+ const interopObj = g('interop');
26
+ class FlippedNSView extends NSViewCls {
27
+ get isFlipped() {
28
+ return true;
29
+ }
30
+ }
31
+ NativeClassFn(FlippedNSView);
32
+ FlippedNSViewCtor = FlippedNSView;
33
+ class ClickableNSView extends NSViewCls {
34
+ _onClick;
35
+ get isFlipped() {
36
+ return true;
37
+ }
38
+ mouseDown(_event) {
39
+ if (this.layer)
40
+ this.layer.opacity = 0.72;
41
+ }
42
+ mouseUp(_event) {
43
+ if (this.layer)
44
+ this.layer.opacity = 1.0;
45
+ this._onClick?.();
46
+ }
47
+ resetCursorRects() {
48
+ try {
49
+ const NSCursorCls = g('NSCursor');
50
+ this.addCursorRectCursor(this.bounds, NSCursorCls.pointingHandCursor);
51
+ }
52
+ catch {
53
+ // Ignore
54
+ }
55
+ }
56
+ }
57
+ NativeClassFn(ClickableNSView);
58
+ ClickableNSViewCtor = ClickableNSView;
59
+ class InputDelegate extends NSObjectCls {
60
+ _onChange;
61
+ static ObjCProtocols = [g('NSTextFieldDelegate')];
62
+ controlTextDidChange(notification) {
63
+ try {
64
+ const field = notification?.object;
65
+ const val = String(field?.stringValue ?? '');
66
+ this._onChange?.(val);
67
+ }
68
+ catch {
69
+ // Ignore
70
+ }
71
+ }
72
+ }
73
+ NativeClassFn(InputDelegate);
74
+ InputDelegateCtor = InputDelegate;
75
+ class WindowDelegate extends NSObjectCls {
76
+ _onResize;
77
+ _onClose;
78
+ static ObjCProtocols = [g('NSWindowDelegate')];
79
+ windowDidResize(notification) {
80
+ try {
81
+ const win = notification?.object;
82
+ const rect = win?.contentView?.bounds;
83
+ if (rect) {
84
+ this._onResize?.(rect.size.width, rect.size.height);
85
+ }
86
+ }
87
+ catch {
88
+ // Ignore
89
+ }
90
+ }
91
+ windowWillClose(_notification) {
92
+ this._onClose?.();
93
+ }
94
+ }
95
+ NativeClassFn(WindowDelegate);
96
+ WindowDelegateCtor = WindowDelegate;
97
+ class EffectAppDelegate extends NSObjectCls {
98
+ running = true;
99
+ static ObjCProtocols = [g('NSApplicationDelegate')];
100
+ static ObjCExposedMethods = {
101
+ quitApp: { returns: interopObj.types.void, params: [interopObj.types.id] },
102
+ };
103
+ applicationDidFinishLaunching(_notification) {
104
+ const app = g('NSApp');
105
+ app.activateIgnoringOtherApps(true);
106
+ app.stop(this);
107
+ }
108
+ applicationWillTerminate(_notification) {
109
+ this.running = false;
110
+ }
111
+ quitApp(_sender) {
112
+ this.running = false;
113
+ g('NSApp').terminate(this);
114
+ }
115
+ }
116
+ NativeClassFn(EffectAppDelegate);
117
+ AppDelegateCtor = EffectAppDelegate;
118
+ }
119
+ function toNSColor(c) {
120
+ return g('NSColor').colorWithSRGBRedGreenBlueAlpha(c.r, c.g, c.b, c.a);
121
+ }
122
+ function toNSFont(size, weight) {
123
+ const NSFontCls = g('NSFont');
124
+ // NSFontWeight values: regular=0, medium=0.23, semibold=0.3, bold=0.4
125
+ const w = weight === 'bold' ? 0.4 : weight === 'semibold' ? 0.3 : weight === 'medium' ? 0.23 : 0.0;
126
+ return NSFontCls.systemFontOfSizeWeight(size, w);
127
+ }
128
+ function makeAttributedText(text, style) {
129
+ const attrs = {
130
+ NSFont: toNSFont(style.fontSize, style.fontWeight),
131
+ NSColor: toNSColor(style.color),
132
+ };
133
+ if (style.strikethrough) {
134
+ attrs['NSStrikethrough'] = 1;
135
+ }
136
+ if (style.textAlign !== 'left') {
137
+ const para = g('NSMutableParagraphStyle').new();
138
+ // NSTextAlignment: Left=0, Right=1, Center=2
139
+ para.alignment = style.textAlign === 'center' ? 2 : 1;
140
+ attrs['NSParagraphStyle'] = para;
141
+ }
142
+ return g('NSAttributedString').alloc().initWithStringAttributes(text, attrs);
143
+ }
144
+ function measureAppKitText(text, style, maxWidth) {
145
+ if (!text)
146
+ return { width: 0, height: style.fontSize * 1.3 };
147
+ try {
148
+ const attrStr = makeAttributedText(text, style);
149
+ const boundedW = maxWidth > 0 ? maxWidth : 10000;
150
+ // NSStringDrawingUsesLineFragmentOrigin (1) | NSStringDrawingUsesFontLeading (2) = 3
151
+ const rect = attrStr.boundingRectWithSizeOptionsContext({ width: boundedW, height: 10000 }, 3, null);
152
+ return {
153
+ width: Math.min(boundedW, Math.ceil(rect.size.width) + 4),
154
+ height: Math.max(Math.ceil(style.fontSize * 1.32), Math.ceil(rect.size.height) + 2),
155
+ };
156
+ }
157
+ catch {
158
+ const charW = style.fontSize * 0.56;
159
+ const rawW = text.length * charW;
160
+ const lines = maxWidth > 0 ? Math.max(1, Math.ceil(rawW / maxWidth)) : 1;
161
+ return {
162
+ width: Math.min(maxWidth || rawW, rawW),
163
+ height: lines * style.fontSize * 1.38,
164
+ };
165
+ }
166
+ }
167
+ function collectDirectText(node) {
168
+ let buf = '';
169
+ let hasElements = false;
170
+ for (const ch of node.children) {
171
+ if (ch.kind === 'text')
172
+ buf += ch.text;
173
+ else if (ch.kind === 'element')
174
+ hasElements = true;
175
+ else if (ch.kind === 'container') {
176
+ const sub = collectDirectText(ch);
177
+ if (sub)
178
+ buf += sub;
179
+ }
180
+ }
181
+ if (hasElements)
182
+ return undefined;
183
+ return buf.length > 0 ? buf : undefined;
184
+ }
185
+ function collectElementChildren(node) {
186
+ const out = [];
187
+ for (const ch of node.children) {
188
+ if (ch.kind === 'element')
189
+ out.push(ch);
190
+ else if (ch.kind === 'container')
191
+ out.push(...collectElementChildren(ch));
192
+ }
193
+ return out;
194
+ }
195
+ function fireBubble(node, eventName, payload) {
196
+ let cur = node;
197
+ while (cur) {
198
+ const handler = cur.handlers[eventName];
199
+ if (handler && cur.dispatch) {
200
+ cur.dispatch(eventName, handler, payload);
201
+ return true;
202
+ }
203
+ cur = cur.parent;
204
+ }
205
+ return false;
206
+ }
207
+ function findAncestorTag(node, tag) {
208
+ let cur = node.parent;
209
+ while (cur) {
210
+ if (cur.kind === 'element' && cur.tag === tag)
211
+ return cur;
212
+ cur = cur.parent;
213
+ }
214
+ return undefined;
215
+ }
216
+ function collectFormValues(formNode) {
217
+ const out = {};
218
+ const walk = (n) => {
219
+ if (n.kind === 'element' && n.tag === 'input') {
220
+ const name = n.attrs['name'];
221
+ if (name) {
222
+ out[name] = String(n.nsInput?.stringValue ?? n.props['value'] ?? '');
223
+ }
224
+ }
225
+ for (const c of n.children)
226
+ walk(c);
227
+ };
228
+ walk(formNode);
229
+ return out;
230
+ }
231
+ function createMacosElementNode(tag) {
232
+ return {
233
+ kind: 'element',
234
+ tag: tag.toLowerCase(),
235
+ parent: undefined,
236
+ children: [],
237
+ text: '',
238
+ className: '',
239
+ id: '',
240
+ attrs: {},
241
+ props: {},
242
+ handlers: {},
243
+ dispatch: undefined,
244
+ nsView: undefined,
245
+ nsLabel: undefined,
246
+ nsInput: undefined,
247
+ dialogWindow: undefined,
248
+ layoutNode: undefined,
249
+ };
250
+ }
251
+ function makeNativeHtmlServices() {
252
+ return Context.make(Html.Host, {
253
+ focus: (el) => {
254
+ try {
255
+ const node = el;
256
+ const win = node?.nsInput?.window ?? node?.nsView?.window;
257
+ if (win && node?.nsInput)
258
+ win.makeFirstResponder(node.nsInput);
259
+ }
260
+ catch {
261
+ // Ignore
262
+ }
263
+ },
264
+ blur: (el) => {
265
+ try {
266
+ const node = el;
267
+ const win = node?.nsInput?.window ?? node?.nsView?.window;
268
+ if (win)
269
+ win.makeFirstResponder(null);
270
+ }
271
+ catch {
272
+ // Ignore
273
+ }
274
+ },
275
+ scrollIntoView: () => { },
276
+ measure: (el) => {
277
+ const f = el?.layoutNode?.frame;
278
+ return {
279
+ x: f?.x ?? 0,
280
+ y: f?.y ?? 0,
281
+ width: f?.width ?? 0,
282
+ height: f?.height ?? 0,
283
+ top: f?.y ?? 0,
284
+ left: f?.x ?? 0,
285
+ right: (f?.x ?? 0) + (f?.width ?? 0),
286
+ bottom: (f?.y ?? 0) + (f?.height ?? 0),
287
+ };
288
+ },
289
+ select: () => { },
290
+ requestSubmit: (el) => {
291
+ const node = el;
292
+ if (!node)
293
+ return false;
294
+ return fireBubble(node, 'submit', { values: collectFormValues(node) });
295
+ },
296
+ showPicker: () => false,
297
+ }).pipe(Context.add(HtmlSubscription.AnimationFrames, {
298
+ now: () => Date.now(),
299
+ listen: (cb) => {
300
+ let stopped = false;
301
+ let id;
302
+ const tick = () => {
303
+ if (stopped)
304
+ return;
305
+ cb(Date.now());
306
+ if (!stopped)
307
+ id = setTimeout(tick, 16);
308
+ };
309
+ id = setTimeout(tick, 16);
310
+ return () => {
311
+ stopped = true;
312
+ if (id !== undefined)
313
+ clearTimeout(id);
314
+ };
315
+ },
316
+ }), Context.add(HtmlSubscription.MediaQueries, {
317
+ matches: () => undefined,
318
+ listen: () => () => { },
319
+ }), Context.add(HtmlSubscription.GlobalEvents, {
320
+ listen: () => () => { },
321
+ }));
322
+ }
323
+ /**
324
+ * Builds the `Renderer.Host<MacosNode>` backed by AppKit views inside a native
325
+ * macOS `NSScrollView` + `NSWindow`.
326
+ */
327
+ export function makeMacosHost(options) {
328
+ ensureAppKitClasses();
329
+ const rootContainer = createMacosElementNode('div');
330
+ const activeDialogs = new Set();
331
+ let lastTitle;
332
+ const ensureNSView = (node, isClickable) => {
333
+ if (node.nsView)
334
+ return node.nsView;
335
+ const view = isClickable ? ClickableNSViewCtor.new() : FlippedNSViewCtor.new();
336
+ view.wantsLayer = true;
337
+ if (isClickable) {
338
+ view._onClick = () => {
339
+ if (node.tag === 'button' && node.attrs['type'] === 'submit') {
340
+ const form = findAncestorTag(node, 'form');
341
+ if (form) {
342
+ fireBubble(form, 'submit', { values: collectFormValues(form) });
343
+ return;
344
+ }
345
+ }
346
+ fireBubble(node, 'click', {});
347
+ };
348
+ }
349
+ node.nsView = view;
350
+ return view;
351
+ };
352
+ const applyViewVisuals = (node, style, rect, text) => {
353
+ const clickable = node.tag === 'button' || Boolean(node.handlers['click']);
354
+ const view = ensureNSView(node, clickable);
355
+ view.frame = {
356
+ origin: { x: rect.x, y: rect.y },
357
+ size: { width: Math.max(0, rect.width), height: Math.max(0, rect.height) },
358
+ };
359
+ const layer = view.layer;
360
+ if (layer) {
361
+ layer.backgroundColor = style.backgroundColor ? toNSColor(style.backgroundColor).CGColor : null;
362
+ layer.borderWidth = style.borderWidth;
363
+ layer.borderColor = style.borderColor ? toNSColor(style.borderColor).CGColor : null;
364
+ const maxR = Math.min(rect.width, rect.height) / 2;
365
+ layer.cornerRadius = style.borderRadius >= 999 ? maxR : Math.min(style.borderRadius, maxR);
366
+ layer.masksToBounds = style.borderRadius > 0;
367
+ }
368
+ // Input element -> real native editable NSTextField
369
+ if (node.tag === 'input') {
370
+ if (!node.nsInput) {
371
+ const tf = g('NSTextField').alloc().initWithFrame({
372
+ origin: { x: 0, y: 0 },
373
+ size: { width: rect.width, height: rect.height },
374
+ });
375
+ tf.isEditable = true;
376
+ tf.isSelectable = true;
377
+ tf.isBordered = false;
378
+ tf.drawsBackground = false;
379
+ tf.focusRingType = 1; // NSFocusRingTypeNone
380
+ const del = InputDelegateCtor.new();
381
+ del._onChange = (val) => {
382
+ node.props['value'] = val;
383
+ fireBubble(node, 'input', { value: val });
384
+ };
385
+ tf.delegate = del;
386
+ node._inputDelegate = del;
387
+ node.nsInput = tf;
388
+ view.addSubview(tf);
389
+ }
390
+ const tf = node.nsInput;
391
+ tf.frame = {
392
+ origin: { x: style.padLeft, y: Math.max(0, (rect.height - 20) / 2) },
393
+ size: { width: Math.max(0, rect.width - style.padLeft - style.padRight), height: 22 },
394
+ };
395
+ tf.font = toNSFont(style.fontSize, style.fontWeight);
396
+ tf.textColor = toNSColor(style.color);
397
+ if (node.attrs['placeholder']) {
398
+ tf.placeholderString = node.attrs['placeholder'];
399
+ }
400
+ const desired = String(node.props['value'] ?? '');
401
+ if (String(tf.stringValue ?? '') !== desired) {
402
+ tf.stringValue = desired;
403
+ }
404
+ return view;
405
+ }
406
+ // Leaf text element -> native non-editable NSTextField
407
+ if (text !== undefined) {
408
+ if (!node.nsLabel) {
409
+ const lbl = g('NSTextField').labelWithString('');
410
+ lbl.isEditable = false;
411
+ lbl.isSelectable = false;
412
+ lbl.drawsBackground = false;
413
+ lbl.isBordered = false;
414
+ lbl.usesSingleLineMode = false;
415
+ lbl.lineBreakMode = 0; // NSLineBreakByWordWrapping
416
+ node.nsLabel = lbl;
417
+ view.addSubview(lbl);
418
+ }
419
+ const lbl = node.nsLabel;
420
+ const innerW = Math.max(0, rect.width - style.padLeft - style.padRight);
421
+ const innerH = Math.max(0, rect.height - style.padTop - style.padBottom);
422
+ const measured = measureAppKitText(text, style, innerW);
423
+ const textY = style.padTop + Math.max(0, (innerH - measured.height) / 2);
424
+ lbl.frame = {
425
+ origin: { x: style.padLeft, y: textY },
426
+ size: { width: innerW, height: Math.max(innerH, measured.height) },
427
+ };
428
+ lbl.attributedStringValue = makeAttributedText(text, style);
429
+ }
430
+ else if (node.nsLabel) {
431
+ node.nsLabel.removeFromSuperview();
432
+ node.nsLabel = undefined;
433
+ }
434
+ return view;
435
+ };
436
+ const buildLayoutNode = (node, inheritedColor, dark, allowDialog = false) => {
437
+ const isPortal = node.tag === 'dialog' && !allowDialog;
438
+ const style = resolveMacosStyle(node.tag, node.className, node.attrs, inheritedColor, dark);
439
+ const text = collectDirectText(node);
440
+ const childElements = text === undefined ? collectElementChildren(node) : [];
441
+ const children = childElements.map((c) => buildLayoutNode(c, style.color, dark, false));
442
+ const ln = {
443
+ tag: node.tag,
444
+ isPortal,
445
+ style,
446
+ text,
447
+ children,
448
+ frame: { x: 0, y: 0, width: 0, height: 0 },
449
+ };
450
+ node.layoutNode = ln;
451
+ return ln;
452
+ };
453
+ const syncNSViewTree = (node, parentNSView, allowDialog = false) => {
454
+ const ln = node.layoutNode;
455
+ if (!ln || ln.style.display === 'none' || (node.tag === 'dialog' && !allowDialog)) {
456
+ if (node.nsView && node.nsView.superview) {
457
+ node.nsView.removeFromSuperview();
458
+ }
459
+ return;
460
+ }
461
+ const view = applyViewVisuals(node, ln.style, ln.frame, ln.text);
462
+ if (parentNSView && view.superview !== parentNSView) {
463
+ view.removeFromSuperview();
464
+ parentNSView.addSubview(view);
465
+ }
466
+ const childElements = ln.text === undefined ? collectElementChildren(node) : [];
467
+ const activeChildViews = new Set();
468
+ for (const ch of childElements) {
469
+ syncNSViewTree(ch, view, false);
470
+ if (ch.nsView && ch.nsView.superview === view) {
471
+ activeChildViews.add(ch.nsView);
472
+ }
473
+ }
474
+ // Prune stale subviews that are no longer in the tree
475
+ try {
476
+ const subviews = view.subviews;
477
+ const count = subviews.count;
478
+ for (let i = count - 1; i >= 0; i--) {
479
+ const sv = subviews.objectAtIndex(i);
480
+ if (sv !== node.nsLabel && sv !== node.nsInput && !activeChildViews.has(sv)) {
481
+ sv.removeFromSuperview();
482
+ }
483
+ }
484
+ }
485
+ catch {
486
+ // Ignore
487
+ }
488
+ };
489
+ const syncDialogs = (root, dark) => {
490
+ const foundDialogs = [];
491
+ const walk = (n) => {
492
+ if (n.kind === 'element' && n.tag === 'dialog')
493
+ foundDialogs.push(n);
494
+ for (const c of n.children)
495
+ walk(c);
496
+ };
497
+ walk(root);
498
+ for (const dlg of foundDialogs) {
499
+ const openVal = dlg.props['open'];
500
+ const wantOpen = openVal === 'modal' || openVal === true;
501
+ const isOpen = activeDialogs.has(dlg);
502
+ if (wantOpen && !isOpen) {
503
+ activeDialogs.add(dlg);
504
+ const pal = dark ? darkPalette : lightPalette;
505
+ const dlgW = 420;
506
+ const ln = buildLayoutNode(dlg, pal.foreground, dark, true);
507
+ const m = measureNode(ln, dlgW, measureAppKitText);
508
+ const dlgH = Math.max(240, m.height + 16);
509
+ layoutTree(ln, 0, 0, dlgW, dlgH, measureAppKitText);
510
+ const panel = g('NSWindow')
511
+ .alloc()
512
+ .initWithContentRectStyleMaskBackingDefer({ origin: { x: 0, y: 0 }, size: { width: dlgW, height: dlgH } }, 1 | 2, // Titled | Closable
513
+ 2, // NSBackingStoreBuffered
514
+ false);
515
+ panel.title = 'New Milestone';
516
+ panel.backgroundColor = toNSColor(pal.card);
517
+ const contentRoot = FlippedNSViewCtor.new();
518
+ contentRoot.wantsLayer = true;
519
+ contentRoot.frame = { origin: { x: 0, y: 0 }, size: { width: dlgW, height: dlgH } };
520
+ panel.contentView = contentRoot;
521
+ const del = WindowDelegateCtor.new();
522
+ del._onClose = () => {
523
+ if (activeDialogs.has(dlg)) {
524
+ activeDialogs.delete(dlg);
525
+ dlg.dialogWindow = undefined;
526
+ fireBubble(dlg, 'cancel', { cancelable: true });
527
+ }
528
+ };
529
+ panel.delegate = del;
530
+ panel._winDel = del;
531
+ syncNSViewTree(dlg, contentRoot, true);
532
+ dlg.dialogWindow = panel;
533
+ const hostWin = options.getHostWindow();
534
+ if (hostWin) {
535
+ hostWin.beginSheetCompletionHandler(panel, null);
536
+ }
537
+ else {
538
+ panel.center();
539
+ panel.makeKeyAndOrderFront(null);
540
+ }
541
+ }
542
+ else if (wantOpen && isOpen && dlg.dialogWindow) {
543
+ const pal = dark ? darkPalette : lightPalette;
544
+ const dlgW = 420;
545
+ const ln = buildLayoutNode(dlg, pal.foreground, dark, true);
546
+ const m = measureNode(ln, dlgW, measureAppKitText);
547
+ const dlgH = Math.max(240, m.height + 16);
548
+ layoutTree(ln, 0, 0, dlgW, dlgH, measureAppKitText);
549
+ syncNSViewTree(dlg, dlg.dialogWindow.contentView, true);
550
+ }
551
+ else if (!wantOpen && isOpen) {
552
+ activeDialogs.delete(dlg);
553
+ const panel = dlg.dialogWindow;
554
+ dlg.dialogWindow = undefined;
555
+ if (panel) {
556
+ const hostWin = options.getHostWindow();
557
+ try {
558
+ if (hostWin)
559
+ hostWin.endSheet(panel);
560
+ panel.orderOut(null);
561
+ }
562
+ catch {
563
+ // Ignore
564
+ }
565
+ }
566
+ }
567
+ }
568
+ };
569
+ const relayout = () => {
570
+ const dark = options.getDarkMode();
571
+ const pal = dark ? darkPalette : lightPalette;
572
+ const viewportW = Math.max(320, options.getViewportWidth());
573
+ const topElements = collectElementChildren(rootContainer);
574
+ const primary = topElements[0];
575
+ if (!primary)
576
+ return;
577
+ const ln = buildLayoutNode(primary, pal.foreground, dark, false);
578
+ const m = measureNode(ln, viewportW, measureAppKitText);
579
+ const totalH = Math.max(m.height, 100);
580
+ layoutTree(ln, 0, 0, viewportW, totalH, measureAppKitText);
581
+ syncNSViewTree(primary, null, false);
582
+ if (primary.nsView) {
583
+ options.attachRootView(primary.nsView, totalH);
584
+ }
585
+ syncDialogs(rootContainer, dark);
586
+ };
587
+ const detach = (node) => {
588
+ const oldParent = node.parent;
589
+ if (!oldParent)
590
+ return;
591
+ const idx = oldParent.children.indexOf(node);
592
+ if (idx >= 0)
593
+ oldParent.children.splice(idx, 1);
594
+ node.parent = undefined;
595
+ if (node.nsView?.superview) {
596
+ node.nsView.removeFromSuperview();
597
+ }
598
+ };
599
+ const host = {
600
+ createElement(tag) {
601
+ return createMacosElementNode(tag);
602
+ },
603
+ createText(text) {
604
+ return {
605
+ kind: 'text',
606
+ tag: '#text',
607
+ parent: undefined,
608
+ children: [],
609
+ text,
610
+ className: '',
611
+ id: '',
612
+ attrs: {},
613
+ props: {},
614
+ handlers: {},
615
+ dispatch: undefined,
616
+ nsView: undefined,
617
+ nsLabel: undefined,
618
+ nsInput: undefined,
619
+ dialogWindow: undefined,
620
+ layoutNode: undefined,
621
+ };
622
+ },
623
+ createContainer() {
624
+ return {
625
+ kind: 'container',
626
+ tag: '#container',
627
+ parent: undefined,
628
+ children: [],
629
+ text: '',
630
+ className: '',
631
+ id: '',
632
+ attrs: {},
633
+ props: {},
634
+ handlers: {},
635
+ dispatch: undefined,
636
+ nsView: undefined,
637
+ nsLabel: undefined,
638
+ nsInput: undefined,
639
+ dialogWindow: undefined,
640
+ layoutNode: undefined,
641
+ };
642
+ },
643
+ setText(node, text) {
644
+ node.text = text;
645
+ },
646
+ setAttr(node, name, value) {
647
+ if (name === 'class')
648
+ node.className = value ?? '';
649
+ else if (name === 'id')
650
+ node.id = value ?? '';
651
+ node.attrs[name] = value;
652
+ },
653
+ setProp(node, name, value) {
654
+ node.props[name] = value;
655
+ },
656
+ insertBefore(parent, node, ref) {
657
+ if (node.parent)
658
+ detach(node);
659
+ const refIdx = ref === undefined ? -1 : parent.children.indexOf(ref);
660
+ if (refIdx < 0)
661
+ parent.children.push(node);
662
+ else
663
+ parent.children.splice(refIdx, 0, node);
664
+ node.parent = parent;
665
+ },
666
+ remove(node) {
667
+ detach(node);
668
+ },
669
+ removeChildren(_parent, nodes) {
670
+ for (const n of nodes)
671
+ detach(n);
672
+ },
673
+ parentOf(node) {
674
+ return node.parent;
675
+ },
676
+ nextOf(node) {
677
+ const p = node.parent;
678
+ if (!p)
679
+ return undefined;
680
+ const idx = p.children.indexOf(node);
681
+ return idx >= 0 ? p.children[idx + 1] : undefined;
682
+ },
683
+ setEvents(node, handlers, dispatch) {
684
+ node.handlers = handlers;
685
+ node.dispatch = dispatch;
686
+ },
687
+ elements: [Html.name],
688
+ controlled: new Set(['value', 'checked', 'open']),
689
+ runsMounts: true,
690
+ commit() {
691
+ relayout();
692
+ },
693
+ head(head) {
694
+ const title = head?.title;
695
+ if (title !== undefined && title !== lastTitle) {
696
+ lastTitle = title;
697
+ options.onTitle?.(title);
698
+ }
699
+ },
700
+ };
701
+ return {
702
+ host,
703
+ rootContainer,
704
+ relayout,
705
+ dispose: () => {
706
+ for (const dlg of activeDialogs) {
707
+ try {
708
+ dlg.dialogWindow?.orderOut?.(null);
709
+ }
710
+ catch {
711
+ // Ignore
712
+ }
713
+ }
714
+ activeDialogs.clear();
715
+ },
716
+ };
717
+ }
718
+ function captureWindowToPng(window, outputPath) {
719
+ try {
720
+ const view = window.contentView;
721
+ const bounds = view.bounds;
722
+ const rep = view.bitmapImageRepForCachingDisplayInRect(bounds);
723
+ if (!rep)
724
+ return;
725
+ view.cacheDisplayInRectToBitmapImageRep(bounds, rep);
726
+ // NSBitmapImageFileTypePNG = 4
727
+ const pngData = rep.representationUsingTypeProperties(4, null);
728
+ if (pngData) {
729
+ pngData.writeToFileAtomically(outputPath, true);
730
+ }
731
+ }
732
+ catch (err) {
733
+ console.error('[MacosHost] capture failed:', err);
734
+ }
735
+ }
736
+ /**
737
+ * Launches a real native macOS AppKit desktop application rendering any shared
738
+ * `@effect/ui` HTML component (`PulseApp`, `@effect/ui-kit`, etc.) via
739
+ * `@nativescript/macos-node-api` with zero Xcode build required.
740
+ */
741
+ export async function run(component, props, options) {
742
+ // Dynamically load `@nativescript/macos-node-api` only on macOS desktop.
743
+ await import('@nativescript/macos-node-api');
744
+ g('objc').import('AppKit');
745
+ g('objc').import('QuartzCore');
746
+ ensureAppKitClasses();
747
+ const NSApp = g('NSApplication').sharedApplication;
748
+ // NSApplicationActivationPolicyRegular = 0
749
+ NSApp.setActivationPolicy(0);
750
+ const appDelegate = AppDelegateCtor.new();
751
+ NSApp.delegate = appDelegate;
752
+ // Build native macOS menu bar (Cmd+Q to quit cleanly)
753
+ const mainMenu = g('NSMenu').new();
754
+ const appMenuItem = g('NSMenuItem').new();
755
+ mainMenu.addItem(appMenuItem);
756
+ const appMenu = g('NSMenu').new();
757
+ const quitItem = g('NSMenuItem')
758
+ .alloc()
759
+ .initWithTitleActionKeyEquivalent(`Quit ${options?.title ?? 'Effect Pulse'}`, 'quitApp:', 'q');
760
+ quitItem.target = appDelegate;
761
+ appMenu.addItem(quitItem);
762
+ appMenuItem.submenu = appMenu;
763
+ NSApp.mainMenu = mainMenu;
764
+ const width = options?.width ?? 460;
765
+ const height = options?.height ?? 840;
766
+ const window = g('NSWindow')
767
+ .alloc()
768
+ .initWithContentRectStyleMaskBackingDefer({ origin: { x: 0, y: 0 }, size: { width, height } }, 1 | 2 | 4 | 8, // Titled | Closable | Miniaturizable | Resizable
769
+ 2, // NSBackingStoreBuffered
770
+ false);
771
+ window.title = options?.title ?? 'Effect Pulse';
772
+ window.minSize = { width: 380, height: 560 };
773
+ // Appearance selection (defaults to clean light monochrome matching PulseApp)
774
+ const NSAppearance = g('NSAppearance');
775
+ if (options?.appearance === 'dark') {
776
+ window.appearance = NSAppearance.appearanceNamed('NSAppearanceNameDarkAqua');
777
+ }
778
+ else if (options?.appearance === 'light') {
779
+ window.appearance = NSAppearance.appearanceNamed('NSAppearanceNameAqua');
780
+ }
781
+ const isDark = () => {
782
+ if (options?.appearance === 'dark')
783
+ return true;
784
+ if (options?.appearance === 'light')
785
+ return false;
786
+ const name = String(window.effectiveAppearance?.name ?? '');
787
+ return name.toLowerCase().includes('dark');
788
+ };
789
+ const pal = isDark() ? darkPalette : lightPalette;
790
+ window.backgroundColor = toNSColor(pal.background);
791
+ window.titlebarAppearsTransparent = true;
792
+ const scrollView = g('NSScrollView').alloc().initWithFrame({
793
+ origin: { x: 0, y: 0 },
794
+ size: { width, height },
795
+ });
796
+ scrollView.hasVerticalScroller = true;
797
+ scrollView.autohidesScrollers = true;
798
+ scrollView.drawsBackground = true;
799
+ scrollView.backgroundColor = toNSColor(pal.background);
800
+ // Autoresize width & height with window
801
+ scrollView.autoresizingMask = 2 | 16; // NSViewWidthSizable | NSViewHeightSizable
802
+ window.contentView = scrollView;
803
+ let currentViewportW = width;
804
+ const macos = makeMacosHost({
805
+ getViewportWidth: () => currentViewportW,
806
+ getDarkMode: isDark,
807
+ getHostWindow: () => window,
808
+ attachRootView: (docView, contentHeight) => {
809
+ const h = Math.max(contentHeight, scrollView.bounds.size.height);
810
+ docView.frame = {
811
+ origin: { x: 0, y: 0 },
812
+ size: { width: currentViewportW, height: h },
813
+ };
814
+ if (scrollView.documentView !== docView) {
815
+ scrollView.documentView = docView;
816
+ }
817
+ },
818
+ onTitle: (title) => {
819
+ window.title = title;
820
+ },
821
+ });
822
+ const winDelegate = WindowDelegateCtor.new();
823
+ winDelegate._onResize = (w) => {
824
+ if (Math.abs(w - currentViewportW) > 1) {
825
+ currentViewportW = w;
826
+ macos.relayout();
827
+ }
828
+ };
829
+ winDelegate._onClose = () => {
830
+ appDelegate.running = false;
831
+ macos.dispose();
832
+ process.exit(0);
833
+ };
834
+ window.delegate = winDelegate;
835
+ // Mount the Effect UI component directly into the AppKit host!
836
+ const mountLayer = Layer.effectDiscard(Effect.provideContext(Renderer.mount(component, props, macos.rootContainer, {
837
+ fallback: options?.fallback,
838
+ onEmit: options?.onEmit,
839
+ }), Context.make(Renderer.Renderer, { host: macos.host }).pipe(Context.merge(makeNativeHtmlServices()))));
840
+ const provided = options?.layer ? Layer.provide(mountLayer, options.layer) : mountLayer;
841
+ Effect.runFork(Layer.launch(provided));
842
+ window.center();
843
+ window.makeKeyAndOrderFront(null);
844
+ // Start AppKit cooperative event pump alongside Node/Bun + Effect event loop!
845
+ NSApp.finishLaunching();
846
+ NSApp.activateIgnoringOtherApps(true);
847
+ if (options?.capturePath) {
848
+ const delay = options.captureDelayMs ?? 900;
849
+ setTimeout(() => {
850
+ macos.relayout();
851
+ window.displayIfNeeded();
852
+ captureWindowToPng(window, options.capturePath);
853
+ appDelegate.running = false;
854
+ process.exit(0);
855
+ }, delay);
856
+ }
857
+ const pump = () => {
858
+ if (!appDelegate.running)
859
+ return;
860
+ let drained = 0;
861
+ while (drained < 16) {
862
+ const ev = NSApp.nextEventMatchingMaskUntilDateInModeDequeue(0xffffffffffffffffn, // NSEventMaskAny
863
+ null, 'kCFRunLoopDefaultMode', true);
864
+ if (!ev)
865
+ break;
866
+ NSApp.sendEvent(ev);
867
+ drained += 1;
868
+ }
869
+ NSApp.updateWindows();
870
+ setTimeout(pump, drained > 0 ? 4 : 16);
871
+ };
872
+ setTimeout(pump, 0);
873
+ }
874
+ export const MacosHost = {
875
+ makeMacosHost,
876
+ run,
877
+ };
878
+ //# sourceMappingURL=MacosHost.js.map