@bun-native/core 0.0.0-bootstrap.0 → 0.33.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.
package/src/types.ts ADDED
@@ -0,0 +1,375 @@
1
+ import type {
2
+ Capability,
3
+ DismissReason,
4
+ ResizeSource,
5
+ Element,
6
+ KeyModifiers,
7
+ Limits,
8
+ PointerButton,
9
+ Style,
10
+ } from '@bun-native/protocol'
11
+ import type { HostOptions } from '@bun-native/runtime'
12
+ import type {
13
+ DisplayChangeEvent,
14
+ NativeDisplaySnapshot,
15
+ NativeClipboardText,
16
+ ServiceCapabilities,
17
+ ServiceRequestOptions,
18
+ } from './services.ts'
19
+ import type {
20
+ GlobalShortcutEvent,
21
+ GlobalShortcutOptions,
22
+ NativeGlobalShortcut,
23
+ } from './shortcuts.ts'
24
+ import type { NativeNotification, NotificationEvent, NotificationOptions } from './notifications.ts'
25
+ import type { AppMenuItemEvent, MenuEntryOptions, MenuOptions, NativeMenuBar } from './app-menu.ts'
26
+ import type { ContextMenuPosition } from './context-menus.ts'
27
+ import type { FileGrantOptions, NativeFileGrant } from './files.ts'
28
+ import type { NativeProcess, ProcessEvent, ProcessOptions } from './processes.ts'
29
+ import type {
30
+ OpenDialogOptions,
31
+ OpenDialogResult,
32
+ SaveDialogOptions,
33
+ SaveDialogResult,
34
+ } from './dialogs.ts'
35
+ import type {
36
+ ConfirmDialogOptions,
37
+ MessageDialogOptions,
38
+ MessageDialogResult,
39
+ } from './message-dialogs.ts'
40
+ import type { DropTargetEvent, NativeDropTarget } from './drops.ts'
41
+
42
+ export type { Element, KeyModifiers, PointerButton, Style }
43
+ export type ApplicationOptions = HostOptions
44
+ export type ElementKind = Element['kind']
45
+ /** Element kinds that own ordered children. */
46
+ export type ContainerKind = 'box' | 'split' | 'overlay'
47
+ export type NativeCapability = Capability
48
+ declare const nodeType: unique symbol
49
+
50
+ /** Opaque identity. Copying its properties does not create a usable handle. */
51
+ export interface NativeNode<K extends ElementKind = ElementKind> {
52
+ readonly kind: K
53
+ readonly [nodeType]: K
54
+ }
55
+
56
+ export interface EventContext<K extends ElementKind = ElementKind> {
57
+ /** The native target. It does not change during propagation. */
58
+ readonly node: NativeNode<K>
59
+ readonly currentTarget: NativeNode
60
+ /** Native committed ancestry captured at input, root first and target last. */
61
+ readonly path: readonly NativeNode[]
62
+ readonly phase: 'capture' | 'target' | 'bubble'
63
+ readonly treeRevision: number
64
+ readonly sequence: number
65
+ /** Revoked when this particular registration is removed or replaced. */
66
+ readonly signal: AbortSignal
67
+ readonly propagationStopped: boolean
68
+ /** Affects subsequent JavaScript listeners, not an already-performed native action. */
69
+ stopPropagation(): void
70
+ stopImmediatePropagation(): void
71
+ }
72
+
73
+ export interface ClickEvent extends EventContext<'button'> {
74
+ readonly type: 'click'
75
+ }
76
+
77
+ export interface InputState {
78
+ readonly value: string
79
+ readonly revision: number
80
+ }
81
+
82
+ export interface InputEvent extends InputState, EventContext<'input'> {
83
+ readonly type: 'input'
84
+ }
85
+
86
+ export interface KeyEvent<
87
+ T extends 'key_down' | 'key_up' = 'key_down' | 'key_up',
88
+ > extends EventContext {
89
+ readonly type: T
90
+ readonly key: string
91
+ /** GPUI logical text, when available; not a committed input/IME notification. */
92
+ readonly keyChar?: string
93
+ readonly modifiers: Readonly<KeyModifiers>
94
+ }
95
+
96
+ export interface PointerEvent<
97
+ T extends 'pointer_down' | 'pointer_up' = 'pointer_down' | 'pointer_up',
98
+ > extends EventContext {
99
+ readonly type: T
100
+ readonly button: PointerButton
101
+ /** Coordinates in logical pixels relative to the native window. */
102
+ readonly x: number
103
+ readonly y: number
104
+ readonly modifiers: Readonly<KeyModifiers>
105
+ }
106
+
107
+ export interface FocusEvent<T extends 'focus' | 'blur' = 'focus' | 'blur'> extends EventContext {
108
+ readonly type: T
109
+ }
110
+
111
+ /**
112
+ * The host asks the application to close a popup window overlay, for example
113
+ * because its parent window stopped being active. Nothing is removed natively
114
+ * until a listener commits a batch.
115
+ */
116
+ export interface DismissRequestEvent extends EventContext {
117
+ readonly type: 'dismiss'
118
+ readonly node: NativeNode<'overlay'>
119
+ readonly reason: DismissReason
120
+ }
121
+
122
+ /**
123
+ * Requires `split_resize_v1`. The user moved a split's divider: once after a
124
+ * pointer drag that changed the ratio ends, and once per keyboard step that
125
+ * changed it. `ratio` is the host-owned leading-pane share the window now
126
+ * shows; declaring a different `ratio` later replaces it.
127
+ */
128
+ export interface SplitResizeEvent extends EventContext<'split'> {
129
+ readonly type: 'split_resize'
130
+ readonly ratio: number
131
+ readonly source: ResizeSource
132
+ }
133
+
134
+ export interface NativeEventMap {
135
+ click: ClickEvent
136
+ input: InputEvent
137
+ key_down: KeyEvent<'key_down'>
138
+ key_up: KeyEvent<'key_up'>
139
+ pointer_down: PointerEvent<'pointer_down'>
140
+ pointer_up: PointerEvent<'pointer_up'>
141
+ focus: FocusEvent<'focus'>
142
+ blur: FocusEvent<'blur'>
143
+ dismiss: DismissRequestEvent
144
+ split_resize: SplitResizeEvent
145
+ }
146
+
147
+ export type NativeEventType = keyof NativeEventMap
148
+ export type NativeUIEvent = NativeEventMap[NativeEventType]
149
+ export type Listener<T> = (event: T) => void | Promise<void>
150
+ export interface ListenerOptions {
151
+ readonly capture?: boolean
152
+ }
153
+
154
+ export interface NativeBatch {
155
+ create<E extends Element>(element: E): NativeNode<E['kind']>
156
+ set(
157
+ node: NativeNode<'input'>,
158
+ element: Extract<Element, { kind: 'input' }>,
159
+ expectedInputRevision: number
160
+ ): void
161
+ set<K extends Exclude<ElementKind, 'input'>>(
162
+ node: NativeNode<K>,
163
+ element: Extract<Element, { kind: NoInfer<K> }>
164
+ ): void
165
+ /** Index is evaluated after detaching the child from its previous parent. */
166
+ insert(parent: NativeNode<ContainerKind>, child: NativeNode, index: number): void
167
+ append(parent: NativeNode<ContainerKind>, child: NativeNode): void
168
+ /** Deletes the complete subtree; deleting a revoked node is an error. */
169
+ remove(node: NativeNode): void
170
+ /**
171
+ * Requires `focus_v1`. Focus an enabled button or input after this batch's
172
+ * tree changes commit, and scroll it into view. The target must still be
173
+ * focusable in the committed tree and lie inside the active modal overlay.
174
+ */
175
+ focus(node: NativeNode<'button' | 'input'>): void
176
+ /**
177
+ * Requires `scroll_control_v1`. Set a scrolling container's offset from its
178
+ * content start in logical pixels; an omitted axis keeps its offset. The
179
+ * host clamps the offset to the laid-out maximum.
180
+ */
181
+ scrollTo(node: NativeNode<ContainerKind>, offset: ScrollOffset): void
182
+ /**
183
+ * Requires `scroll_control_v1`. Scroll every scrolling ancestor inside the
184
+ * node's own layer so the ancestor's child on the node's route is visible.
185
+ */
186
+ scrollIntoView(node: NativeNode): void
187
+ }
188
+
189
+ export interface ScrollOffset {
190
+ readonly x?: number
191
+ readonly y?: number
192
+ }
193
+
194
+ export interface WindowOptions {
195
+ title: string
196
+ width?: number
197
+ height?: number
198
+ }
199
+
200
+ export interface NativeWindow {
201
+ readonly root: NativeNode<'box'>
202
+ readonly closed: Promise<void>
203
+ readonly revision: number
204
+ /** The callback is synchronous. Its changes commit atomically after acknowledgement. */
205
+ batch<T>(
206
+ update: (batch: NativeBatch) => T & (T extends PromiseLike<unknown> ? never : unknown)
207
+ ): Promise<T>
208
+ /** One registration per node/type/phase. Replacement revokes its predecessor. */
209
+ on<T extends NativeEventType>(
210
+ node: NativeNode,
211
+ type: T,
212
+ listener: Listener<NativeEventMap[T]>,
213
+ options?: ListenerOptions
214
+ ): () => void
215
+ onClick(node: NativeNode<'button'>, listener: Listener<ClickEvent>): () => void
216
+ onInput(node: NativeNode<'input'>, listener: Listener<InputEvent>): () => void
217
+ input(node: NativeNode<'input'>): InputState
218
+ /** Requires an immutable launch grant; cancellation never publishes a late snapshot. */
219
+ displaySnapshot(options?: ServiceRequestOptions): Promise<NativeDisplaySnapshot>
220
+ /**
221
+ * Reports when the window moves to another display or the OS reconfigures its
222
+ * display, with the new snapshot. Requires the windowDisplay launch grant and
223
+ * `serviceCapabilities.windowDisplay.changes` (macOS, Windows and Linux Wayland). Returns an
224
+ * idempotent remover; window close and shutdown remove every listener.
225
+ */
226
+ onDisplayChange(listener: (event: DisplayChangeEvent) => void | Promise<void>): () => void
227
+ /** Fixed native deadline; cancellation after dispatch drains but withholds the result. */
228
+ readClipboardText(options?: ServiceRequestOptions): Promise<NativeClipboardText>
229
+ /** A submitted write can change the OS clipboard even if ownership is subsequently revoked. */
230
+ writeClipboardText(text: string): Promise<void>
231
+ /**
232
+ * OS-wide activation, delivered even while another window or application is
233
+ * focused. Requires the immutable globalShortcuts launch grant. A combination
234
+ * already held by another client fails with `conflict`; nothing is approximated.
235
+ */
236
+ registerGlobalShortcut(
237
+ options: GlobalShortcutOptions,
238
+ listener: (event: GlobalShortcutEvent) => void | Promise<void>
239
+ ): Promise<NativeGlobalShortcut>
240
+ /**
241
+ * Post a plain-text desktop notification owned by this window. Requires the
242
+ * immutable notifications launch grant. Withdrawal, window close and
243
+ * shutdown close it on the server; the listener receives activations and a
244
+ * final dismissal when the server removes it first.
245
+ */
246
+ postNotification(
247
+ options: NotificationOptions,
248
+ listener: (event: NotificationEvent) => void | Promise<void>
249
+ ): Promise<NativeNotification>
250
+ /**
251
+ * Open a scoped grant on a directory the application named in its
252
+ * immutable `filesystemRoots` launch permission. No run-time path, dropped
253
+ * pathname or model text can create one. Closing the window revokes it.
254
+ */
255
+ openFileGrant(root: string, options?: FileGrantOptions): Promise<NativeFileGrant>
256
+ /**
257
+ * Start an owned child process from an executable the application named in
258
+ * its immutable `processes` launch permission, by ID. Arguments and the
259
+ * whole environment are explicit; a working directory comes only from a live
260
+ * file grant of this window. Closing the window stops its process group.
261
+ */
262
+ spawnProcess(
263
+ executable: string,
264
+ options: ProcessOptions,
265
+ listener: (event: ProcessEvent) => void | Promise<void>
266
+ ): Promise<NativeProcess>
267
+ /**
268
+ * Show an OS file-open or folder dialog owned by this window. Requires the
269
+ * immutable fileDialogs launch grant; at most one file dialog per window.
270
+ * The returned paths are data, never a filesystem grant. Aborting the signal,
271
+ * closing the window or shutting down closes the dialog.
272
+ */
273
+ showOpenDialog(
274
+ options: OpenDialogOptions,
275
+ request?: ServiceRequestOptions
276
+ ): Promise<OpenDialogResult>
277
+ /** Show an OS save dialog owned by this window. Nothing is written or created. */
278
+ showSaveDialog(
279
+ options: SaveDialogOptions,
280
+ request?: ServiceRequestOptions
281
+ ): Promise<SaveDialogResult>
282
+ /**
283
+ * Show a message dialog drawn by core as a window-anchored modal overlay
284
+ * (not an OS alert), with focus on its default button and Tab trapped
285
+ * inside it. No launch grant is needed; requires
286
+ * `serviceCapabilities.messageDialogs`. Dialogs of one window are shown one
287
+ * at a time in call order. The result is delivered after the layer is
288
+ * removed. Aborting the signal rejects with `cancelled`; closing the window
289
+ * or shutting down rejects with `invalid_handle`.
290
+ */
291
+ showMessageDialog(
292
+ options: MessageDialogOptions,
293
+ request?: ServiceRequestOptions
294
+ ): Promise<MessageDialogResult>
295
+ /** An OK/Cancel message dialog: true for OK; Cancel and Escape give false. */
296
+ showConfirmDialog(
297
+ options: ConfirmDialogOptions,
298
+ request?: ServiceRequestOptions
299
+ ): Promise<boolean>
300
+ /**
301
+ * Show an OS context menu at a point of this window's content (logical
302
+ * pixels, as pointer events report them) and resolve with the chosen item's
303
+ * `id`, or `null` when the user dismisses it. Items use the menu-bar entry
304
+ * shape. Requires the optional `context_menus_v1` capability (macOS and
305
+ * Windows); otherwise it throws `UnsupportedFeatureError`. One menu is open
306
+ * per application at a time (`limit_exceeded`). Aborting the signal rejects
307
+ * with `cancelled`; closing the window or shutting down rejects with
308
+ * `invalid_handle`.
309
+ */
310
+ showContextMenu(
311
+ items: readonly MenuEntryOptions[],
312
+ position: ContextMenuPosition,
313
+ request?: ServiceRequestOptions
314
+ ): Promise<string | null>
315
+ /**
316
+ * Make a committed node an owned drop target for external file drags.
317
+ * Requires the immutable dragDrop launch grant. Dropped items arrive as
318
+ * untrusted display names; only `item.resolve(root)` maps one, through the
319
+ * host, beneath a launch root. Removing the node, closing the target or
320
+ * its window revokes it, and no later event is delivered.
321
+ */
322
+ registerDropTarget(
323
+ node: NativeNode,
324
+ listener: (event: DropTargetEvent) => void | Promise<void>
325
+ ): Promise<NativeDropTarget>
326
+ /** Idempotent. Revokes listeners before requesting native window removal. */
327
+ close(): Promise<void>
328
+ }
329
+
330
+ export interface NativeApplication {
331
+ readonly closed: Promise<void>
332
+ readonly limits: Readonly<Limits>
333
+ readonly capabilities: readonly NativeCapability[]
334
+ /** Implemented service features and their effective grants, not an OS sandbox. */
335
+ readonly serviceCapabilities: Readonly<ServiceCapabilities>
336
+ readonly diagnostics: Readonly<{
337
+ windows: number
338
+ nodes: number
339
+ callbacks: number
340
+ queuedTransactions: number
341
+ queuedCallbacks: number
342
+ activeCallbacks: number
343
+ serviceRequests: number
344
+ }>
345
+ openWindow(options: WindowOptions): Promise<NativeWindow>
346
+ /**
347
+ * Installs the application menu bar, replacing (and revoking) the previous
348
+ * one. Requires `serviceCapabilities.appMenu` and the `appMenu` grant.
349
+ */
350
+ setMenuBar(
351
+ menus: readonly MenuOptions[],
352
+ listener: (event: AppMenuItemEvent) => void | Promise<void>
353
+ ): Promise<NativeMenuBar>
354
+ /** Removes the application menu bar and revokes its items. */
355
+ clearMenuBar(): Promise<void>
356
+ /** May be called from a listener. Await closed outside that listener to join dispatch. */
357
+ shutdown(): Promise<void>
358
+ }
359
+
360
+ export class NativeStateError extends Error {
361
+ override readonly name = 'NativeStateError'
362
+ constructor(
363
+ readonly code: 'invalid_handle' | 'invalid_state' | 'stale_revision' | 'limit_exceeded',
364
+ message: string
365
+ ) {
366
+ super(message)
367
+ }
368
+ }
369
+
370
+ export class UnsupportedFeatureError extends Error {
371
+ override readonly name = 'UnsupportedFeatureError'
372
+ constructor(readonly feature: string) {
373
+ super('Unsupported native feature: ' + feature)
374
+ }
375
+ }