@bun-native/core 0.0.0-stage → 0.33.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/LICENSE +21 -0
- package/README.md +4 -2
- package/bun-native-source.json +111 -0
- package/package.json +33 -4
- package/src/app-menu.ts +271 -0
- package/src/application.ts +1858 -0
- package/src/context-menus.ts +76 -0
- package/src/dialogs.ts +153 -0
- package/src/display.ts +25 -0
- package/src/drops.ts +79 -0
- package/src/events.ts +344 -0
- package/src/files.ts +242 -0
- package/src/index.ts +88 -0
- package/src/message-dialogs.ts +489 -0
- package/src/notifications.ts +124 -0
- package/src/order.ts +50 -0
- package/src/overlays.ts +55 -0
- package/src/processes.ts +201 -0
- package/src/registration.ts +39 -0
- package/src/services.ts +428 -0
- package/src/shortcuts.ts +108 -0
- package/src/splits.ts +34 -0
- package/src/theme.ts +96 -0
- package/src/tree.ts +642 -0
- package/src/types.ts +375 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bunly contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bun-native/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The public TypeScript API of the Bun Native desktop framework.
|
|
4
|
+
|
|
5
|
+
Part of [Bun Native](https://github.com/0xPlayerOne/bun-native), an experimental developer preview. Version 0.33.1 was built from [c56fdd675365](https://github.com/0xPlayerOne/bun-native/tree/c56fdd6753653a10144a4167c1d3dfb3c38a0647) and published from GitHub Actions with npm provenance. Use the Bun revision pinned in that commit's toolchain.json. Start with the [getting-started guide](https://github.com/0xPlayerOne/bun-native/blob/c56fdd6753653a10144a4167c1d3dfb3c38a0647/docs/getting-started.md); `bun-native host install` fetches the matching release host. The license notice is in LICENSE.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 1,
|
|
3
|
+
"commit": "c56fdd6753653a10144a4167c1d3dfb3c38a0647",
|
|
4
|
+
"tree": "c6e81b3f9d041e11dd0413b5fdd7a50910383854",
|
|
5
|
+
"files": {
|
|
6
|
+
"package.json": {
|
|
7
|
+
"bytes": 2115,
|
|
8
|
+
"sha256": "caf866d56f3cd7530d5735ecbf62512e9e22526bc44c75a4b48a18c26352c0b8"
|
|
9
|
+
},
|
|
10
|
+
"packages/core/package.json": {
|
|
11
|
+
"bytes": 282,
|
|
12
|
+
"sha256": "ac4e3b505bff3e344fe3a9407394f500bf43107f609f87f5c1bfaf9b34b80f82"
|
|
13
|
+
},
|
|
14
|
+
"packages/core/src/app-menu.ts": {
|
|
15
|
+
"bytes": 7879,
|
|
16
|
+
"sha256": "be41f76ea03059acb892700a172a8df3f07821af79f16332173399aaf6db7731"
|
|
17
|
+
},
|
|
18
|
+
"packages/core/src/application.ts": {
|
|
19
|
+
"bytes": 65604,
|
|
20
|
+
"sha256": "f3319d95d7a8dd2352947ef636073bcd1d6eb8a8f831009c241d02a631a42417"
|
|
21
|
+
},
|
|
22
|
+
"packages/core/src/context-menus.ts": {
|
|
23
|
+
"bytes": 2555,
|
|
24
|
+
"sha256": "19c59698130066d5a09c977a77a03f54450a51c89523aba4d797044270f90ec8"
|
|
25
|
+
},
|
|
26
|
+
"packages/core/src/dialogs.ts": {
|
|
27
|
+
"bytes": 5546,
|
|
28
|
+
"sha256": "e0bfb065e5c63b835a4bf6b69c9d31d7608c5ddeaa5b5962c5cfff77ff5622cb"
|
|
29
|
+
},
|
|
30
|
+
"packages/core/src/display.ts": {
|
|
31
|
+
"bytes": 787,
|
|
32
|
+
"sha256": "e1fabce2f0640b4764c357834e6f600242941534ded6238b15c5e568dd66638e"
|
|
33
|
+
},
|
|
34
|
+
"packages/core/src/drops.ts": {
|
|
35
|
+
"bytes": 2724,
|
|
36
|
+
"sha256": "e93885243ad5e060f9fb8ef3c983ce41d25067935039e74d5ac8c2dec537a509"
|
|
37
|
+
},
|
|
38
|
+
"packages/core/src/events.ts": {
|
|
39
|
+
"bytes": 11229,
|
|
40
|
+
"sha256": "9070622fc0abf243c43a8f636d761466603cc2c965955f039d8631d90a438a90"
|
|
41
|
+
},
|
|
42
|
+
"packages/core/src/files.ts": {
|
|
43
|
+
"bytes": 9003,
|
|
44
|
+
"sha256": "f300701ab41598b93dd26df8d8da6b6475a88df4bbdacd5e7dd2a6ce2f61779d"
|
|
45
|
+
},
|
|
46
|
+
"packages/core/src/index.ts": {
|
|
47
|
+
"bytes": 2509,
|
|
48
|
+
"sha256": "1ab9096fb92e6e0e06f73c07130f5c22e69e088896c59b8f6b3f7e803f425099"
|
|
49
|
+
},
|
|
50
|
+
"packages/core/src/message-dialogs.ts": {
|
|
51
|
+
"bytes": 19467,
|
|
52
|
+
"sha256": "f47672b59bddd460c2994aadabf1abeaf652bfab701a54ec9e3d8e3468edd223"
|
|
53
|
+
},
|
|
54
|
+
"packages/core/src/notifications.ts": {
|
|
55
|
+
"bytes": 3942,
|
|
56
|
+
"sha256": "1afa302c1f60e8643964bcd595856370da7e6a44b623193d3192fc73bd3d9a1a"
|
|
57
|
+
},
|
|
58
|
+
"packages/core/src/order.ts": {
|
|
59
|
+
"bytes": 2018,
|
|
60
|
+
"sha256": "bf3adde6497cb379e3dd047adaccbdc97ad39fe1c251f607798b7f1b4e7532dd"
|
|
61
|
+
},
|
|
62
|
+
"packages/core/src/overlays.ts": {
|
|
63
|
+
"bytes": 2095,
|
|
64
|
+
"sha256": "1634d00f9585e355b633c461c64b702e19ab9b0277028357d75cf239e8f7c5de"
|
|
65
|
+
},
|
|
66
|
+
"packages/core/src/processes.ts": {
|
|
67
|
+
"bytes": 7342,
|
|
68
|
+
"sha256": "39956b419f938ae7fac4071d5381b67115a57b680b537f3b14325d87fe5b55f7"
|
|
69
|
+
},
|
|
70
|
+
"packages/core/src/registration.ts": {
|
|
71
|
+
"bytes": 1182,
|
|
72
|
+
"sha256": "cdaea319fc117d8951bf006de9fa2300b1b1bd7a5e643dcc63bfeb948c401e2d"
|
|
73
|
+
},
|
|
74
|
+
"packages/core/src/services.ts": {
|
|
75
|
+
"bytes": 16678,
|
|
76
|
+
"sha256": "d8f4af83a0c6e1c99bf93ab8fc628c2b62edef3ef804d46ebdd69ab5ae5d53a8"
|
|
77
|
+
},
|
|
78
|
+
"packages/core/src/shortcuts.ts": {
|
|
79
|
+
"bytes": 3696,
|
|
80
|
+
"sha256": "099cb9701c798a73e41c1b209110aa3a3cbbf8e12754dc9b90d9a792b06cef39"
|
|
81
|
+
},
|
|
82
|
+
"packages/core/src/splits.ts": {
|
|
83
|
+
"bytes": 1232,
|
|
84
|
+
"sha256": "1274f9e5b695e32959059e6fad95517369d05e260129c068064c3e10317d1fba"
|
|
85
|
+
},
|
|
86
|
+
"packages/core/src/theme.ts": {
|
|
87
|
+
"bytes": 3670,
|
|
88
|
+
"sha256": "c460c4e1a2e9d337e79294bce333e53896381529af8f7e35b23e207c6e132b85"
|
|
89
|
+
},
|
|
90
|
+
"packages/core/src/tree.ts": {
|
|
91
|
+
"bytes": 23735,
|
|
92
|
+
"sha256": "ea5731e87ff4ac05f87ffca05c6e3eb5207470dd9f5758183097f2c4ea02f52a"
|
|
93
|
+
},
|
|
94
|
+
"packages/core/src/types.ts": {
|
|
95
|
+
"bytes": 14319,
|
|
96
|
+
"sha256": "25400d57bebe1a449ec2c877b0d2cecd58ff172ddc3192eb93bc2532c5ccbf43"
|
|
97
|
+
},
|
|
98
|
+
"LICENSE": {
|
|
99
|
+
"bytes": 1075,
|
|
100
|
+
"sha256": "86673fb485f12fa9aaa3941dfccc9f69a7f8322f8141d0d5c73db4323de10ac5"
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
"toolchain": {
|
|
104
|
+
"bytes": 3234,
|
|
105
|
+
"sha256": "9f4d9362cb4cc38c0f4e705295789a0cbfec46ffdd007e0541b9441a7c3003a7"
|
|
106
|
+
},
|
|
107
|
+
"dependencyLock": {
|
|
108
|
+
"bytes": 28357,
|
|
109
|
+
"sha256": "65e3dbd01be4a1889367690faf1a9c3f08fb31d22dbd273d9bb9f39dd6baa6fb"
|
|
110
|
+
}
|
|
111
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bun-native/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.33.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"files": [
|
|
7
|
+
"src",
|
|
8
|
+
"LICENSE",
|
|
9
|
+
"README.md",
|
|
10
|
+
"bun-native-source.json"
|
|
11
|
+
],
|
|
12
|
+
"engines": {
|
|
13
|
+
"bun": "1.4.4-canary.1"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./src/index.ts",
|
|
17
|
+
"./order": "./src/order.ts"
|
|
18
|
+
},
|
|
19
|
+
"description": "The public TypeScript API of the Bun Native desktop framework.",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@bun-native/protocol": "0.33.1",
|
|
22
|
+
"@bun-native/runtime": "0.33.1"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/0xPlayerOne/bun-native.git",
|
|
27
|
+
"directory": "packages/core"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/0xPlayerOne/bun-native#readme",
|
|
30
|
+
"bugs": "https://github.com/0xPlayerOne/bun-native/issues",
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public",
|
|
33
|
+
"provenance": true
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/app-menu.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import {
|
|
2
|
+
APP_MENU_KEYS,
|
|
3
|
+
MAX_APP_MENU_DEPTH,
|
|
4
|
+
MAX_APP_MENU_ENTRIES,
|
|
5
|
+
parseAppMenus,
|
|
6
|
+
ProtocolError,
|
|
7
|
+
type AppMenu,
|
|
8
|
+
type AppMenuEntry,
|
|
9
|
+
} from '@bun-native/protocol'
|
|
10
|
+
import type { Listener } from './types.ts'
|
|
11
|
+
|
|
12
|
+
type Letter =
|
|
13
|
+
| 'a'
|
|
14
|
+
| 'b'
|
|
15
|
+
| 'c'
|
|
16
|
+
| 'd'
|
|
17
|
+
| 'e'
|
|
18
|
+
| 'f'
|
|
19
|
+
| 'g'
|
|
20
|
+
| 'h'
|
|
21
|
+
| 'i'
|
|
22
|
+
| 'j'
|
|
23
|
+
| 'k'
|
|
24
|
+
| 'l'
|
|
25
|
+
| 'm'
|
|
26
|
+
| 'n'
|
|
27
|
+
| 'o'
|
|
28
|
+
| 'p'
|
|
29
|
+
| 'q'
|
|
30
|
+
| 'r'
|
|
31
|
+
| 's'
|
|
32
|
+
| 't'
|
|
33
|
+
| 'u'
|
|
34
|
+
| 'v'
|
|
35
|
+
| 'w'
|
|
36
|
+
| 'x'
|
|
37
|
+
| 'y'
|
|
38
|
+
| 'z'
|
|
39
|
+
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
|
|
40
|
+
type FunctionKey = `f${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12}`
|
|
41
|
+
type Punctuation = ',' | '.' | '/' | ';' | "'" | '[' | ']' | '-' | '=' | '`' | '\\'
|
|
42
|
+
type NamedKey =
|
|
43
|
+
| 'enter'
|
|
44
|
+
| 'escape'
|
|
45
|
+
| 'tab'
|
|
46
|
+
| 'space'
|
|
47
|
+
| 'backspace'
|
|
48
|
+
| 'delete'
|
|
49
|
+
| 'up'
|
|
50
|
+
| 'down'
|
|
51
|
+
| 'left'
|
|
52
|
+
| 'right'
|
|
53
|
+
| 'home'
|
|
54
|
+
| 'end'
|
|
55
|
+
| 'pageup'
|
|
56
|
+
| 'pagedown'
|
|
57
|
+
|
|
58
|
+
/** The closed key set of a menu shortcut. */
|
|
59
|
+
export type MenuShortcutKey = Letter | Digit | FunctionKey | Punctuation | NamedKey
|
|
60
|
+
|
|
61
|
+
export interface MenuShortcutOptions {
|
|
62
|
+
readonly key: MenuShortcutKey
|
|
63
|
+
readonly control?: boolean
|
|
64
|
+
readonly alt?: boolean
|
|
65
|
+
readonly shift?: boolean
|
|
66
|
+
/** Command on macOS. Every key except f1-f12 needs control, alt or platform. */
|
|
67
|
+
readonly platform?: boolean
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type MenuEntryOptions =
|
|
71
|
+
| Readonly<{
|
|
72
|
+
kind: 'item'
|
|
73
|
+
/** Reported back on selection: 1-64 of `A-Z a-z 0-9 . _ : -`, unique in the bar. */
|
|
74
|
+
id: string
|
|
75
|
+
label: string
|
|
76
|
+
/** Shown beside the item and bound while the bar is installed; unique in the bar. */
|
|
77
|
+
shortcut?: MenuShortcutOptions
|
|
78
|
+
/** Default true. */
|
|
79
|
+
enabled?: boolean
|
|
80
|
+
/** Default false. */
|
|
81
|
+
checked?: boolean
|
|
82
|
+
}>
|
|
83
|
+
| Readonly<{ kind: 'separator' }>
|
|
84
|
+
| Readonly<{
|
|
85
|
+
kind: 'submenu'
|
|
86
|
+
label: string
|
|
87
|
+
/** Default true. */
|
|
88
|
+
enabled?: boolean
|
|
89
|
+
items: readonly MenuEntryOptions[]
|
|
90
|
+
}>
|
|
91
|
+
|
|
92
|
+
export interface MenuOptions {
|
|
93
|
+
/** On macOS the first menu is the application menu, shown under the process name. */
|
|
94
|
+
readonly title: string
|
|
95
|
+
readonly items: readonly MenuEntryOptions[]
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface AppMenuItemEvent {
|
|
99
|
+
readonly type: 'app_menu_item'
|
|
100
|
+
readonly sequence: number
|
|
101
|
+
/** The application's `id` of the selected item. */
|
|
102
|
+
readonly itemId: string
|
|
103
|
+
readonly menuBar: NativeMenuBar
|
|
104
|
+
/** Aborted when the bar is replaced, cleared, or the app stops. */
|
|
105
|
+
readonly signal: AbortSignal
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface NativeMenuBar {
|
|
109
|
+
/** False once replaced, cleared or revoked; no later selection is delivered. */
|
|
110
|
+
readonly active: boolean
|
|
111
|
+
/** Removes this bar if it is still installed. Idempotent; a replaced bar is left alone. */
|
|
112
|
+
clear(): Promise<void>
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function options(value: unknown, field: string, allowed: readonly string[]) {
|
|
116
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
117
|
+
throw new TypeError(field + ' must be an object')
|
|
118
|
+
if (Object.keys(value).some((key) => !allowed.includes(key)))
|
|
119
|
+
throw new TypeError('Unknown ' + field + ' option')
|
|
120
|
+
return value as Record<string, unknown>
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function flag(data: Record<string, unknown>, key: string, fallback: boolean): boolean {
|
|
124
|
+
const value = Object.hasOwn(data, key) ? data[key] : fallback
|
|
125
|
+
if (typeof value !== 'boolean') throw new TypeError('Menu ' + key + ' must be a boolean')
|
|
126
|
+
return value
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function list(value: unknown, field: string): readonly unknown[] {
|
|
130
|
+
if (!Array.isArray(value)) throw new TypeError(field + ' must be an array')
|
|
131
|
+
return value
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
interface Budget {
|
|
135
|
+
entries: number
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function shortcut(
|
|
139
|
+
value: unknown
|
|
140
|
+
): NonNullable<Extract<AppMenuEntry, { kind: 'item' }>['shortcut']> {
|
|
141
|
+
const data = options(value, 'Menu shortcut', ['key', 'control', 'alt', 'shift', 'platform'])
|
|
142
|
+
const key = data['key']
|
|
143
|
+
if (typeof key !== 'string' || !APP_MENU_KEYS.includes(key))
|
|
144
|
+
throw new TypeError('Menu shortcut key is not in the closed key set')
|
|
145
|
+
return {
|
|
146
|
+
key,
|
|
147
|
+
modifiers: {
|
|
148
|
+
control: flag(data, 'control', false),
|
|
149
|
+
alt: flag(data, 'alt', false),
|
|
150
|
+
shift: flag(data, 'shift', false),
|
|
151
|
+
platform: flag(data, 'platform', false),
|
|
152
|
+
},
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function entries(value: unknown, depth: number, budget: Budget): AppMenuEntry[] {
|
|
157
|
+
// Bounded before recursion, so a cyclic or huge consumer object is rejected.
|
|
158
|
+
return list(value, 'Menu items').map((raw): AppMenuEntry => {
|
|
159
|
+
if (++budget.entries > MAX_APP_MENU_ENTRIES)
|
|
160
|
+
throw new TypeError('A menu bar holds at most 128 entries')
|
|
161
|
+
const kind =
|
|
162
|
+
raw && typeof raw === 'object' ? (raw as Record<string, unknown>)['kind'] : undefined
|
|
163
|
+
switch (kind) {
|
|
164
|
+
case 'item': {
|
|
165
|
+
const data = options(raw, 'Menu item', [
|
|
166
|
+
'kind',
|
|
167
|
+
'id',
|
|
168
|
+
'label',
|
|
169
|
+
'shortcut',
|
|
170
|
+
'enabled',
|
|
171
|
+
'checked',
|
|
172
|
+
])
|
|
173
|
+
return {
|
|
174
|
+
kind: 'item',
|
|
175
|
+
id: data['id'] as string,
|
|
176
|
+
label: data['label'] as string,
|
|
177
|
+
...(Object.hasOwn(data, 'shortcut') ? { shortcut: shortcut(data['shortcut']) } : {}),
|
|
178
|
+
enabled: flag(data, 'enabled', true),
|
|
179
|
+
checked: flag(data, 'checked', false),
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
case 'separator':
|
|
183
|
+
options(raw, 'Menu separator', ['kind'])
|
|
184
|
+
return { kind: 'separator' }
|
|
185
|
+
case 'submenu': {
|
|
186
|
+
const data = options(raw, 'Submenu', ['kind', 'label', 'enabled', 'items'])
|
|
187
|
+
if (depth >= MAX_APP_MENU_DEPTH)
|
|
188
|
+
throw new TypeError('Submenus nest at most two levels below a top-level menu')
|
|
189
|
+
return {
|
|
190
|
+
kind: 'submenu',
|
|
191
|
+
label: data['label'] as string,
|
|
192
|
+
enabled: flag(data, 'enabled', true),
|
|
193
|
+
items: entries(data['items'], depth + 1, budget),
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
default:
|
|
197
|
+
throw new TypeError("Menu entry kind must be 'item', 'separator' or 'submenu'")
|
|
198
|
+
}
|
|
199
|
+
})
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** The entries of one menu in wire shape, not yet validated as a whole. */
|
|
203
|
+
export function menuEntriesRequest(items: readonly MenuEntryOptions[]): AppMenuEntry[] {
|
|
204
|
+
return entries(items, 1, { entries: 0 })
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Copies consumer options into the closed wire shape and validates it with the shared parser. */
|
|
208
|
+
export function menuBarRequest(menus: readonly MenuOptions[]): AppMenu[] {
|
|
209
|
+
const budget: Budget = { entries: 0 }
|
|
210
|
+
const wire = list(menus, 'Menu bar').map((raw) => {
|
|
211
|
+
const data = options(raw, 'Menu', ['title', 'items'])
|
|
212
|
+
return {
|
|
213
|
+
title: data['title'] as string,
|
|
214
|
+
items: entries(data['items'], 1, budget),
|
|
215
|
+
}
|
|
216
|
+
})
|
|
217
|
+
try {
|
|
218
|
+
return parseAppMenus(wire)
|
|
219
|
+
} catch (error) {
|
|
220
|
+
if (error instanceof ProtocolError) throw new TypeError(error.message)
|
|
221
|
+
throw error
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Internal registry entry. Consumers only receive its frozen handle. */
|
|
226
|
+
export class MenuBarRegistration {
|
|
227
|
+
readonly handle: NativeMenuBar
|
|
228
|
+
readonly controller = new AbortController()
|
|
229
|
+
listener: Listener<AppMenuItemEvent> | undefined
|
|
230
|
+
private revoked = false
|
|
231
|
+
private clearing: Promise<void> | undefined
|
|
232
|
+
|
|
233
|
+
constructor(
|
|
234
|
+
readonly id: number,
|
|
235
|
+
listener: Listener<AppMenuItemEvent>,
|
|
236
|
+
clear: (registration: MenuBarRegistration) => Promise<void>
|
|
237
|
+
) {
|
|
238
|
+
this.listener = listener
|
|
239
|
+
this.handle = publicHandle(this, clear)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Shared by the public handle; one clear per registration. */
|
|
243
|
+
clear(clear: (registration: MenuBarRegistration) => Promise<void>): Promise<void> {
|
|
244
|
+
this.clearing ??= this.revoked ? Promise.resolve() : clear(this)
|
|
245
|
+
return this.clearing
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
get live(): boolean {
|
|
249
|
+
return !this.revoked
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Synchronous local revocation; the native bar is replaced separately. */
|
|
253
|
+
revoke(): void {
|
|
254
|
+
if (this.revoked) return
|
|
255
|
+
this.revoked = true
|
|
256
|
+
this.listener = undefined
|
|
257
|
+
this.controller.abort()
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function publicHandle(
|
|
262
|
+
registration: MenuBarRegistration,
|
|
263
|
+
clear: (registration: MenuBarRegistration) => Promise<void>
|
|
264
|
+
): NativeMenuBar {
|
|
265
|
+
return Object.freeze({
|
|
266
|
+
get active() {
|
|
267
|
+
return registration.live
|
|
268
|
+
},
|
|
269
|
+
clear: () => registration.clear(clear),
|
|
270
|
+
})
|
|
271
|
+
}
|