@octane-xplat/context-menu 0.19.0 → 0.20.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/README.md +2 -163
- package/package.json +9 -9
- package/src/AppKitContextMenu.macos.tsrx +34 -32
- package/src/AppKitContextMenu.tsrx +2 -0
- package/src/ContextMenu.tsrx +2 -0
- package/src/ContextMenu.web.tsrx +165 -159
- package/src/MaterialContextMenu.android.tsrx +117 -113
- package/src/MaterialContextMenu.tsrx +2 -0
- package/src/SwiftUIContextMenu.ios.tsrx +93 -90
- package/src/SwiftUIContextMenu.tsrx +2 -0
- package/src/android/index.ts +3 -3
- package/src/android/types.ts +27 -27
- package/src/context-menu-owner.macos.ts +194 -171
- package/src/ios/types.ts +27 -27
- package/src/macos/types.ts +26 -26
- package/src/web/types.ts +26 -26
|
@@ -1,190 +1,213 @@
|
|
|
1
1
|
import type { AppKitContextMenuItem, AppKitContextMenuProps } from './macos/types'
|
|
2
2
|
|
|
3
3
|
export type ContextMenuAttach = (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
view: object,
|
|
5
|
+
options: { items: readonly AppKitContextMenuItem[] },
|
|
6
|
+
onSelect: (id: string | undefined) => void,
|
|
7
7
|
) => (() => void) | null
|
|
8
8
|
|
|
9
9
|
export type ContextMenuStatus = 'ready' | 'empty' | 'invalid' | 'unavailable'
|
|
10
10
|
|
|
11
11
|
/** Copy primitive content so in-place item changes cannot rewrite an old menu. */
|
|
12
12
|
function menuSnapshot(
|
|
13
|
-
|
|
13
|
+
items: readonly AppKitContextMenuItem[] = [],
|
|
14
14
|
): AppKitContextMenuItem[] | null {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
15
|
+
const ids = new Set<string>()
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
return items.map((item) => {
|
|
19
|
+
if (typeof item.id !== 'string' || typeof item.title !== 'string' || ids.has(item.id)) {
|
|
20
|
+
throw Error('invalid menu')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
ids.add(item.id)
|
|
24
|
+
return {
|
|
25
|
+
id: item.id,
|
|
26
|
+
title: item.title,
|
|
27
|
+
disabled: !!item.disabled,
|
|
28
|
+
divider: !!item.divider,
|
|
29
|
+
destructive: !!item.destructive,
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
} catch {
|
|
33
|
+
return null
|
|
34
|
+
}
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
const sameMenuItems = (a: readonly AppKitContextMenuItem[], b: readonly AppKitContextMenuItem[]) =>
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
38
|
+
a.length === b.length &&
|
|
39
|
+
a.every((item, i) => {
|
|
40
|
+
const other = b[i]
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
item.id === other.id &&
|
|
44
|
+
item.title === other.title &&
|
|
45
|
+
item.disabled === other.disabled &&
|
|
46
|
+
item.divider === other.divider &&
|
|
47
|
+
item.destructive === other.destructive
|
|
48
|
+
)
|
|
49
|
+
})
|
|
47
50
|
|
|
48
51
|
/** Current native menu session; callback-only renders preserve its tracking menu. */
|
|
49
52
|
export function createContextMenuOwner(deps: {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
props(): AppKitContextMenuProps
|
|
54
|
+
attach(): ContextMenuAttach | undefined
|
|
55
|
+
status?(status: ContextMenuStatus): void
|
|
53
56
|
}): {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
update: () => void
|
|
58
|
+
bind(next: object | null): void
|
|
59
|
+
dispose(): void
|
|
60
|
+
getStatus: () => ContextMenuStatus
|
|
58
61
|
} {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
62
|
+
type Session = {
|
|
63
|
+
view: object
|
|
64
|
+
scope: string | undefined
|
|
65
|
+
items: AppKitContextMenuItem[]
|
|
66
|
+
ready: boolean
|
|
67
|
+
selecting: boolean
|
|
68
|
+
detach?: () => void
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let view: object | null = null,
|
|
72
|
+
session: Session | null = null,
|
|
73
|
+
disposed = false
|
|
74
|
+
|
|
75
|
+
let status: ContextMenuStatus = 'empty'
|
|
76
|
+
const report = (next: ContextMenuStatus) => {
|
|
77
|
+
if (!disposed && next !== status) {
|
|
78
|
+
status = next
|
|
79
|
+
deps.status?.(next)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const detach = () => {
|
|
84
|
+
const old = session
|
|
85
|
+
|
|
86
|
+
session = null
|
|
87
|
+
try {
|
|
88
|
+
old?.detach?.()
|
|
89
|
+
} catch {}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const update = () => {
|
|
93
|
+
if (disposed || !view) {
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const props = deps.props(),
|
|
98
|
+
items = menuSnapshot(props.items)
|
|
99
|
+
|
|
100
|
+
if (!items) {
|
|
101
|
+
detach()
|
|
102
|
+
report('invalid')
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!items.length) {
|
|
107
|
+
detach()
|
|
108
|
+
report('empty')
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (
|
|
113
|
+
session &&
|
|
114
|
+
session.view === view &&
|
|
115
|
+
session.scope === props.id &&
|
|
116
|
+
sameMenuItems(session.items, items)
|
|
117
|
+
) {
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
detach()
|
|
122
|
+
|
|
123
|
+
const attach = deps.attach()
|
|
124
|
+
|
|
125
|
+
if (!attach) {
|
|
126
|
+
report('unavailable')
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const owner: Session = { view, scope: props.id, items, ready: false, selecting: false }
|
|
131
|
+
|
|
132
|
+
session = owner
|
|
133
|
+
try {
|
|
134
|
+
const cleanup = attach(view, { items }, (id) => {
|
|
135
|
+
if (
|
|
136
|
+
disposed ||
|
|
137
|
+
session !== owner ||
|
|
138
|
+
!owner.ready ||
|
|
139
|
+
owner.selecting ||
|
|
140
|
+
typeof id !== 'string'
|
|
141
|
+
) {
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const current = deps.props(),
|
|
146
|
+
currentItems = menuSnapshot(current.items)
|
|
147
|
+
|
|
148
|
+
if (
|
|
149
|
+
!currentItems ||
|
|
150
|
+
current.id !== owner.scope ||
|
|
151
|
+
!sameMenuItems(owner.items, currentItems)
|
|
152
|
+
) {
|
|
153
|
+
update()
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const item = currentItems.find((item) => item.id === id)
|
|
158
|
+
|
|
159
|
+
if (!item || item.disabled) {
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
owner.selecting = true
|
|
164
|
+
try {
|
|
165
|
+
current.onItemSelected?.(id)
|
|
166
|
+
} finally {
|
|
167
|
+
owner.selecting = false
|
|
168
|
+
}
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
if (disposed || session !== owner) {
|
|
172
|
+
cleanup?.()
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (typeof cleanup !== 'function') {
|
|
177
|
+
session = null
|
|
178
|
+
report('unavailable')
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
owner.detach = cleanup
|
|
183
|
+
owner.ready = true
|
|
184
|
+
report('ready')
|
|
185
|
+
} catch {
|
|
186
|
+
if (session === owner) {
|
|
187
|
+
detach()
|
|
188
|
+
report('unavailable')
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
update,
|
|
195
|
+
bind(next: object | null) {
|
|
196
|
+
if (!disposed && next !== view) {
|
|
197
|
+
detach()
|
|
198
|
+
view = next
|
|
199
|
+
if (view) {
|
|
200
|
+
update()
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
dispose() {
|
|
205
|
+
if (!disposed) {
|
|
206
|
+
disposed = true
|
|
207
|
+
detach()
|
|
208
|
+
view = null
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
getStatus: () => status,
|
|
212
|
+
}
|
|
190
213
|
}
|
package/src/ios/types.ts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
1
|
/** One row of serialized menu content carried over the bridge. */
|
|
2
2
|
export interface SwiftUIContextMenuItem {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
/** Stable id reported to `onItemSelected`. */
|
|
4
|
+
id: string
|
|
5
|
+
/** Item display text. */
|
|
6
|
+
title: string
|
|
7
|
+
/** Renders the item with .destructive role. */
|
|
8
|
+
destructive?: boolean
|
|
9
|
+
/** Greys the item; not selectable. */
|
|
10
|
+
disabled?: boolean
|
|
11
|
+
/** Render a divider before this item. */
|
|
12
|
+
divider?: boolean
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/** iOS context-menu props (SwiftUI contextMenu). */
|
|
16
16
|
export interface SwiftUIContextMenuProps {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
17
|
+
/** Element id passed through to the host element. */
|
|
18
|
+
id?: string
|
|
19
|
+
/** Class list applied to the element. */
|
|
20
|
+
className?: any
|
|
21
|
+
/** Inline style (string CSS or object). */
|
|
22
|
+
style?: any
|
|
23
|
+
/** Menu entries in display order. */
|
|
24
|
+
items?: readonly SwiftUIContextMenuItem[]
|
|
25
|
+
/** Render fn producing the activation element — hosted inside the
|
|
26
|
+
* SwiftUI tree via the plugin's NativeScriptView-by-id factory. */
|
|
27
|
+
trigger: () => any
|
|
28
|
+
/** Optional preview shown while the menu opens (iOS 16+). */
|
|
29
|
+
preview?: () => any
|
|
30
|
+
/** Fires with the selected item id. */
|
|
31
|
+
onItemSelected?: (id: string) => void
|
|
32
|
+
/** Spoken name for assistive tech. */
|
|
33
|
+
accessibilityLabel?: string
|
|
34
34
|
}
|
package/src/macos/types.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
/** One row of serialized menu content carried over the bridge. */
|
|
2
2
|
export interface AppKitContextMenuItem {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
/** Stable id reported to `onItemSelected`; unique within this menu. */
|
|
4
|
+
id: string
|
|
5
|
+
/** Item display text. */
|
|
6
|
+
title: string
|
|
7
|
+
/** No AppKit destructive role — kept in the contract for parity; the
|
|
8
|
+
* item renders as a normal menu row. */
|
|
9
|
+
destructive?: boolean
|
|
10
|
+
/** Greys the item; not selectable. */
|
|
11
|
+
disabled?: boolean
|
|
12
|
+
/** Render a divider before this item. */
|
|
13
|
+
divider?: boolean
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/** macOS context-menu props (NSMenu). */
|
|
17
17
|
export interface AppKitContextMenuProps {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
18
|
+
/** Native view identifier. Changing it retires this record's menu session. */
|
|
19
|
+
id?: string
|
|
20
|
+
/** Class list applied to the element. */
|
|
21
|
+
className?: any
|
|
22
|
+
/** Inline style (string CSS or object). */
|
|
23
|
+
style?: any
|
|
24
|
+
/** Menu entries in display order. */
|
|
25
|
+
items?: readonly AppKitContextMenuItem[]
|
|
26
|
+
/** Render fn producing the trigger element — rendered inline; the NSMenu
|
|
27
|
+
* attaches to the host's backing NSView. */
|
|
28
|
+
trigger: () => any
|
|
29
|
+
/** Current enabled item selections; retired/replaced menu actions are ignored. */
|
|
30
|
+
onItemSelected?: (id: string) => void
|
|
31
|
+
/** Spoken name for assistive tech. */
|
|
32
|
+
accessibilityLabel?: string
|
|
33
33
|
}
|
package/src/web/types.ts
CHANGED
|
@@ -3,34 +3,34 @@ export type ContextMenuActivation = 'longPress' | 'singlePress'
|
|
|
3
3
|
|
|
4
4
|
/** One menu entry or separator. */
|
|
5
5
|
export interface ContextMenuItem {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
/** Element id passed through to the host element. */
|
|
7
|
+
id: string
|
|
8
|
+
/** Item display text. */
|
|
9
|
+
title: string
|
|
10
|
+
/** Marks the action destructive (red styling where supported). */
|
|
11
|
+
destructive?: boolean
|
|
12
|
+
/** Greys the item; not selectable. */
|
|
13
|
+
disabled?: boolean
|
|
14
|
+
/** Render as a separator instead of an item. */
|
|
15
|
+
divider?: boolean
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/** Web context-menu props (composed web menu). */
|
|
19
19
|
export interface ContextMenuProps {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
20
|
+
/** Element id passed through to the host element. */
|
|
21
|
+
id?: string
|
|
22
|
+
/** Class list applied to the element. */
|
|
23
|
+
className?: any
|
|
24
|
+
/** Inline style (string CSS or object). */
|
|
25
|
+
style?: any
|
|
26
|
+
/** 'longPress' maps to right-click (contextmenu); 'singlePress' to click. */
|
|
27
|
+
activation?: ContextMenuActivation
|
|
28
|
+
/** Menu entries in display order. */
|
|
29
|
+
items?: readonly ContextMenuItem[]
|
|
30
|
+
/** Content that hosts the context menu (long-press/right-click). */
|
|
31
|
+
trigger: () => any
|
|
32
|
+
/** Fires with the selected item id. */
|
|
33
|
+
onItemSelected?: (id: string) => void
|
|
34
|
+
/** Spoken name for assistive tech. */
|
|
35
|
+
accessibilityLabel?: string
|
|
36
36
|
}
|