@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.
@@ -0,0 +1,129 @@
1
+ import {
2
+ GLOBAL_SHORTCUT_KEYS,
3
+ type GlobalShortcutKey,
4
+ type ShortcutModifiers,
5
+ } from '@bun-native/protocol'
6
+ import type { Listener } from './types.ts'
7
+
8
+ /** Layout-level key names; the native backend grabs the unshifted key that produces them. */
9
+ export type { GlobalShortcutKey }
10
+
11
+ type ShortcutFlags = {
12
+ readonly control?: boolean
13
+ readonly alt?: boolean
14
+ readonly shift?: boolean
15
+ /** Super on Linux, Command on macOS. */
16
+ readonly platform?: boolean
17
+ }
18
+
19
+ /**
20
+ * A key plus modifiers. At least one of `control`, `alt` or `platform` must be
21
+ * `true`, which the type enforces; `shift` alone is not a global shortcut.
22
+ */
23
+ export type GlobalShortcutOptions = { readonly key: GlobalShortcutKey } & (
24
+ | (ShortcutFlags & { readonly control: true })
25
+ | (ShortcutFlags & { readonly alt: true })
26
+ | (ShortcutFlags & { readonly platform: true })
27
+ )
28
+
29
+ export interface GlobalShortcutEvent {
30
+ readonly type: 'global_shortcut'
31
+ readonly sequence: number
32
+ readonly shortcut: NativeGlobalShortcut
33
+ /** Aborted when the registration is removed, its window closes or the app stops. */
34
+ readonly signal: AbortSignal
35
+ }
36
+
37
+ export interface NativeGlobalShortcut {
38
+ readonly key: GlobalShortcutKey
39
+ readonly modifiers: Readonly<ShortcutModifiers>
40
+ /** False once unregistered or revoked; no later activation is delivered. */
41
+ readonly active: boolean
42
+ /** Idempotent. Delivery stops immediately; resolves after the OS grab is released. */
43
+ unregister(): Promise<void>
44
+ }
45
+
46
+ const OPTION_KEYS = ['key', 'control', 'alt', 'shift', 'platform']
47
+
48
+ export function shortcutRequest(options: GlobalShortcutOptions): {
49
+ key: GlobalShortcutKey
50
+ modifiers: ShortcutModifiers
51
+ } {
52
+ if (!options || typeof options !== 'object' || Array.isArray(options))
53
+ throw new TypeError('Global shortcut options must be an object')
54
+ if (Object.keys(options).some((key) => !OPTION_KEYS.includes(key)))
55
+ throw new TypeError('Unknown global shortcut option')
56
+ const key = options.key
57
+ if (typeof key !== 'string' || !GLOBAL_SHORTCUT_KEYS.includes(key))
58
+ throw new TypeError('Global shortcut key must be one of a-z, 0-9 or f1-f12')
59
+ const flag = (name: 'control' | 'alt' | 'shift' | 'platform') => {
60
+ const value = Object.hasOwn(options, name) ? options[name] : false
61
+ if (typeof value !== 'boolean')
62
+ throw new TypeError('Global shortcut modifier ' + name + ' must be a boolean')
63
+ return value
64
+ }
65
+ const modifiers = {
66
+ control: flag('control'),
67
+ alt: flag('alt'),
68
+ shift: flag('shift'),
69
+ platform: flag('platform'),
70
+ }
71
+ if (!modifiers.control && !modifiers.alt && !modifiers.platform)
72
+ throw new TypeError('A global shortcut requires control, alt or platform')
73
+ return { key, modifiers }
74
+ }
75
+
76
+ /** Internal registry entry. Consumers only receive its frozen handle. */
77
+ export class ShortcutRegistration {
78
+ readonly handle: NativeGlobalShortcut
79
+ readonly controller = new AbortController()
80
+ listener: Listener<GlobalShortcutEvent> | undefined
81
+ private released = false
82
+ private releasing: Promise<void> | undefined
83
+
84
+ constructor(
85
+ readonly id: number,
86
+ readonly windowId: number,
87
+ key: GlobalShortcutKey,
88
+ modifiers: ShortcutModifiers,
89
+ listener: Listener<GlobalShortcutEvent>,
90
+ release: (registration: ShortcutRegistration) => Promise<void>
91
+ ) {
92
+ this.listener = listener
93
+ this.handle = publicHandle(this, key, modifiers, release)
94
+ }
95
+
96
+ /** Shared by the public handle; one release per registration. */
97
+ release(release: (registration: ShortcutRegistration) => Promise<void>): Promise<void> {
98
+ this.releasing ??= this.released ? Promise.resolve() : release(this)
99
+ return this.releasing
100
+ }
101
+
102
+ get live(): boolean {
103
+ return !this.released
104
+ }
105
+
106
+ /** Synchronous local revocation; the native grab is released separately. */
107
+ revoke(): void {
108
+ if (this.released) return
109
+ this.released = true
110
+ this.listener = undefined
111
+ this.controller.abort()
112
+ }
113
+ }
114
+
115
+ function publicHandle(
116
+ registration: ShortcutRegistration,
117
+ key: GlobalShortcutKey,
118
+ modifiers: ShortcutModifiers,
119
+ release: (registration: ShortcutRegistration) => Promise<void>
120
+ ): NativeGlobalShortcut {
121
+ return Object.freeze({
122
+ key,
123
+ modifiers: Object.freeze({ ...modifiers }),
124
+ get active() {
125
+ return registration.live
126
+ },
127
+ unregister: () => registration.release(release),
128
+ })
129
+ }
package/src/splits.ts ADDED
@@ -0,0 +1,34 @@
1
+ import {
2
+ NativeStateError,
3
+ type NativeNode,
4
+ type NativeWindow,
5
+ type SplitResizeEvent,
6
+ } from './types.ts'
7
+
8
+ /**
9
+ * Observe the user's divider moves on one split, for example to persist them.
10
+ * Requires the host's `split_resize_v1` capability; without it this throws
11
+ * `UnsupportedFeatureError('event:split_resize')` before registering.
12
+ *
13
+ * A pointer drag reports once, after the button is released, when the drag
14
+ * changed the ratio; each keyboard step that changes it reports once. A
15
+ * declared `ratio` the host adopts is the application's own value and is
16
+ * never reported. The listener occupies the split's bubbling `split_resize`
17
+ * registration; removing the split, closing the window or calling the
18
+ * returned function revokes it.
19
+ */
20
+ export function onSplitResize(
21
+ window: NativeWindow,
22
+ split: NativeNode<'split'>,
23
+ listener: (event: SplitResizeEvent) => void | Promise<void>
24
+ ): () => void {
25
+ if (split.kind !== 'split' || typeof listener !== 'function')
26
+ throw new NativeStateError(
27
+ 'invalid_state',
28
+ 'Split resize listeners require a split and a function'
29
+ )
30
+ return window.on(split, 'split_resize', (event) => {
31
+ if (event.node !== split) return
32
+ return listener(event)
33
+ })
34
+ }
package/src/theme.ts ADDED
@@ -0,0 +1,96 @@
1
+ import type { Style } from '@bun-native/protocol'
2
+
3
+ /**
4
+ * Named design values. Tokens resolve to plain numbers in JavaScript before a
5
+ * batch is staged: the native host never sees a token, so switching themes is
6
+ * an ordinary property update that every adapter commits the same way.
7
+ */
8
+ export interface ThemeTokens {
9
+ /** Unsigned 24-bit RGB. */
10
+ readonly colors: Readonly<Record<string, number>>
11
+ /** Logical pixels for `gap` and `padding`. */
12
+ readonly space: Readonly<Record<string, number>>
13
+ /** Logical pixels for `corner_radius`. */
14
+ readonly radii: Readonly<Record<string, number>>
15
+ /** Logical pixels for `font_size`. */
16
+ readonly fontSizes: Readonly<Record<string, number>>
17
+ }
18
+
19
+ const groups = {
20
+ background: 'colors',
21
+ color: 'colors',
22
+ border_color: 'colors',
23
+ gap: 'space',
24
+ padding: 'space',
25
+ corner_radius: 'radii',
26
+ font_size: 'fontSizes',
27
+ } as const satisfies Partial<Record<keyof Style, keyof ThemeTokens>>
28
+ type TokenField = keyof typeof groups
29
+ type Token<T extends ThemeTokens, F extends TokenField> = Extract<
30
+ keyof T[(typeof groups)[F]],
31
+ string
32
+ >
33
+
34
+ /** A style whose color, spacing, radius and font-size fields may name tokens. */
35
+ export type ThemedStyle<T extends ThemeTokens> = {
36
+ [K in keyof Style]?: K extends TokenField ? number | Token<T, K> : Style[K]
37
+ }
38
+ /** The resolved style keeps every literal non-token field, such as `overflow`. */
39
+ export type ResolvedStyle<S> = { [K in keyof S]: K extends TokenField ? number : S[K] }
40
+
41
+ export interface Theme<T extends ThemeTokens = ThemeTokens> {
42
+ readonly tokens: T
43
+ /** Replace token names with numbers; unknown names throw before staging. */
44
+ style<const S extends ThemedStyle<T>>(style: S): ResolvedStyle<S>
45
+ }
46
+
47
+ function check(group: keyof ThemeTokens, name: string, value: unknown): void {
48
+ const valid =
49
+ typeof value === 'number' &&
50
+ Number.isFinite(value) &&
51
+ value >= 0 &&
52
+ (group !== 'colors' || (Number.isInteger(value) && value <= 0xff_ffff))
53
+ if (!valid) throw new TypeError(`Theme token ${group}.${name} is not a valid value`)
54
+ }
55
+
56
+ export function defineTheme<T extends ThemeTokens>(tokens: T): Theme<T> {
57
+ const frozen = {} as Record<keyof ThemeTokens, Readonly<Record<string, number>>>
58
+ for (const group of ['colors', 'space', 'radii', 'fontSizes'] as const) {
59
+ const values = tokens[group]
60
+ if (!values || typeof values !== 'object')
61
+ throw new TypeError('Theme tokens require a ' + group + ' group')
62
+ for (const [name, value] of Object.entries(values)) check(group, name, value)
63
+ frozen[group] = Object.freeze({ ...values })
64
+ }
65
+ const copy = Object.freeze(frozen) as unknown as T
66
+ return Object.freeze({
67
+ tokens: copy,
68
+ style<const S extends ThemedStyle<T>>(style: S): ResolvedStyle<S> {
69
+ const resolved: Record<string, unknown> = { ...style }
70
+ for (const [field, group] of Object.entries(groups)) {
71
+ const value = resolved[field]
72
+ if (typeof value !== 'string') continue
73
+ const table: Readonly<Record<string, number>> = copy[group]
74
+ if (!Object.hasOwn(table, value))
75
+ throw new TypeError(`Unknown ${group} token for ${field}: ${value}`)
76
+ resolved[field] = table[value]
77
+ }
78
+ return resolved as ResolvedStyle<S>
79
+ },
80
+ })
81
+ }
82
+
83
+ /** The values the native host uses when a primitive's style omits a field. */
84
+ export const defaultTheme = defineTheme({
85
+ colors: {
86
+ surface: 0xffffff,
87
+ text: 0x17212b,
88
+ accent: 0x2255cc,
89
+ on_accent: 0xffffff,
90
+ disabled: 0x7a8396,
91
+ divider: 0xc8ced8,
92
+ },
93
+ space: { none: 0, xs: 4, sm: 8, md: 12, lg: 16, xl: 24 },
94
+ radii: { none: 0, sm: 4, md: 8, lg: 12 },
95
+ fontSizes: { sm: 14, body: 18, lg: 24, xl: 32 },
96
+ })