@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
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
import { registerElement } from '@nativescript-community/octane'
|
|
3
3
|
import type { Attributes } from '@octane-xplat/jsx/intrinsics'
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
ComposeDataDriver,
|
|
6
|
+
JetpackCompose,
|
|
7
|
+
registerJetpackCompose,
|
|
8
8
|
} from '@nativescript/jetpack-compose'
|
|
9
9
|
|
|
10
10
|
import { Application, GridLayout } from '@nativescript/core'
|
|
11
11
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
createPortal as createUniversalPortal,
|
|
13
|
+
type UniversalRenderable,
|
|
14
14
|
} from 'octane/universal/native'
|
|
15
15
|
|
|
16
16
|
import { useEffect, useRef, useState } from 'octane'
|
|
@@ -19,23 +19,23 @@ import type { MaterialContextMenuProps } from './android/types'
|
|
|
19
19
|
declare const com: any
|
|
20
20
|
|
|
21
21
|
declare module '@octane-xplat/jsx/intrinsics' {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
interface NativeElements<TElement> {
|
|
23
|
+
xplatandroidcontextmenu: Attributes<
|
|
24
|
+
TElement,
|
|
25
|
+
{
|
|
26
|
+
composeId?: string
|
|
27
|
+
data?: Record<string, unknown>
|
|
28
|
+
onComposeEvent?: (event: { data?: unknown }) => void
|
|
29
|
+
}
|
|
30
|
+
>
|
|
31
|
+
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
registerElement('xplatandroidcontextmenu', JetpackCompose)
|
|
35
35
|
registerJetpackCompose(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
'xplat-material-contextmenu',
|
|
37
|
+
(owner: any) =>
|
|
38
|
+
new ComposeDataDriver(new com.octanexplat.contextmenu.XplatContextMenuProvider(), owner),
|
|
39
39
|
)
|
|
40
40
|
|
|
41
41
|
// Dash-free ids — the host-side representable appends "-<uuid>" and the
|
|
@@ -43,107 +43,111 @@ registerJetpackCompose(
|
|
|
43
43
|
let seq = 0
|
|
44
44
|
|
|
45
45
|
function TriggerRoot(props: { render: () => any }) @{
|
|
46
|
-
|
|
46
|
+
<>{props.render()}</>
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
function createPortal(children: unknown, target: unknown) {
|
|
50
|
-
|
|
50
|
+
return createUniversalPortal(children as UniversalRenderable, target)
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
/** Renders a Material 3 DropdownMenu anchored to a NativeScript-hosted
|
|
54
54
|
* trigger subtree portaled from the declaring Octane tree. */
|
|
55
55
|
export function MaterialContextMenu(props: MaterialContextMenuProps): unknown @{
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
56
|
+
const triggerId = useRef<string>('')
|
|
57
|
+
|
|
58
|
+
if (!triggerId.current) {
|
|
59
|
+
triggerId.current = `ctxmenu${++seq}`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const mounted = useRef<{ host: any } | null>(null)
|
|
63
|
+
const [portalHost, setPortalHost] = useState<GridLayout | null>(null)
|
|
64
|
+
const [expanded, setExpanded] = useState(false)
|
|
65
|
+
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
const host = new GridLayout()
|
|
68
|
+
// Detached subtree — the plugin's generateNativeView shortcut passes
|
|
69
|
+
// `{}` as the native context, which fails to marshal on Android; the
|
|
70
|
+
// view factory needs a real android.content.Context to build its
|
|
71
|
+
// native view.
|
|
72
|
+
|
|
73
|
+
const context =
|
|
74
|
+
(Application.android as any).foregroundActivity ?? (Application.android as any).startActivity
|
|
75
|
+
|
|
76
|
+
const nativeHost = host as any
|
|
77
|
+
|
|
78
|
+
nativeHost._setupAsRootView(context)
|
|
79
|
+
nativeHost.callLoaded()
|
|
80
|
+
com.octanexplat.contextmenu.XplatViewRegistry.register(triggerId.current, host.android)
|
|
81
|
+
mounted.current = { host }
|
|
82
|
+
|
|
83
|
+
// Gate triggerId on registration — the provider only composes the
|
|
84
|
+
// AndroidView host once triggerId is non-empty, so the factory must
|
|
85
|
+
// find the view in the registry on first read.
|
|
86
|
+
setPortalHost(host)
|
|
87
|
+
|
|
88
|
+
return () => {
|
|
89
|
+
com.octanexplat.contextmenu.XplatViewRegistry.unregister(triggerId.current)
|
|
90
|
+
mounted.current = null
|
|
91
|
+
nativeHost.callUnloaded?.()
|
|
92
|
+
nativeHost.removeChildren?.()
|
|
93
|
+
}
|
|
94
|
+
}, [])
|
|
95
|
+
|
|
96
|
+
// Activation is an NS gesture on the trigger host — the hosted subtree
|
|
97
|
+
// consumes pointer input before Compose's gesture detectors could resolve
|
|
98
|
+
// it, so expanded is pushed through data instead. Rebind when the
|
|
99
|
+
// activation prop or the mounted host changes.
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
const host = mounted.current?.host
|
|
102
|
+
|
|
103
|
+
if (!portalHost || !host) {
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const open = () => setExpanded(true)
|
|
108
|
+
const event = (props.activation ?? 'longPress') === 'singlePress' ? 'tap' : 'longPress'
|
|
109
|
+
|
|
110
|
+
host.on(event, open)
|
|
111
|
+
return () => host.off(event, open)
|
|
112
|
+
}, [portalHost, props.activation])
|
|
113
|
+
|
|
114
|
+
const items = props.items ?? []
|
|
115
|
+
|
|
116
|
+
<>
|
|
117
|
+
<xplatandroidcontextmenu
|
|
118
|
+
id={props.id}
|
|
119
|
+
className={props.className}
|
|
120
|
+
style={props.style}
|
|
121
|
+
accessibilityLabel={props.accessibilityLabel}
|
|
122
|
+
composeId="xplat-material-contextmenu"
|
|
123
|
+
data={{
|
|
124
|
+
triggerId: portalHost ? triggerId.current : '',
|
|
125
|
+
expanded: expanded ? 1 : 0,
|
|
126
|
+
items: items.map((item) => ({
|
|
127
|
+
id: item.id,
|
|
128
|
+
title: item.title,
|
|
129
|
+
// The bridge's dataSerialize throws on booleans; send numbers.
|
|
130
|
+
destructive: item.destructive ? 1 : 0,
|
|
131
|
+
disabled: item.disabled ? 1 : 0,
|
|
132
|
+
divider: item.divider ? 1 : 0,
|
|
133
|
+
})),
|
|
134
|
+
}}
|
|
135
|
+
onComposeEvent={(event: any) => {
|
|
136
|
+
if (event.data?.dismissed) {
|
|
137
|
+
setExpanded(false)
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const item = event.data?.item
|
|
142
|
+
|
|
143
|
+
if (typeof item === 'string' && item) {
|
|
144
|
+
setExpanded(false)
|
|
145
|
+
props.onItemSelected?.(item)
|
|
146
|
+
}
|
|
147
|
+
}}
|
|
148
|
+
/>
|
|
149
|
+
{portalHost && props.trigger
|
|
150
|
+
? createPortal(<TriggerRoot render={props.trigger} />, portalHost)
|
|
151
|
+
: null}
|
|
152
|
+
</>
|
|
149
153
|
}
|
|
@@ -4,14 +4,14 @@ import type { Attributes } from '@octane-xplat/jsx/intrinsics'
|
|
|
4
4
|
import { SwiftUI, UIDataDriver, registerSwiftUI, SwiftUIManager } from '@nativescript/swift-ui'
|
|
5
5
|
import { GridLayout } from '@nativescript/core'
|
|
6
6
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
getSwiftUIHostLifecycle,
|
|
8
|
+
registerSwiftUIHost,
|
|
9
|
+
unregisterSwiftUIHost,
|
|
10
10
|
} from '@octane-xplat/lib'
|
|
11
11
|
|
|
12
12
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
createPortal as createUniversalPortal,
|
|
14
|
+
type UniversalRenderable,
|
|
15
15
|
} from 'octane/universal/native'
|
|
16
16
|
|
|
17
17
|
import { useEffect, useRef, useState } from 'octane'
|
|
@@ -21,51 +21,51 @@ declare const XplatContextMenuProvider: any
|
|
|
21
21
|
declare const NativeScriptViewFactory: any
|
|
22
22
|
|
|
23
23
|
declare module '@octane-xplat/jsx/intrinsics' {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
interface NativeElements<TElement> {
|
|
25
|
+
xplatioscontextmenu: Attributes<
|
|
26
|
+
TElement,
|
|
27
|
+
{
|
|
28
|
+
swiftId?: string
|
|
29
|
+
data?: Record<string, unknown>
|
|
30
|
+
onSwiftUIEvent?: (event: { data?: unknown }) => void
|
|
31
|
+
}
|
|
32
|
+
>
|
|
33
|
+
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
registerElement('xplatioscontextmenu', SwiftUI)
|
|
37
37
|
registerSwiftUI(
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
'xplat-swiftui-contextmenu',
|
|
39
|
+
(owner: any) => new UIDataDriver(new XplatContextMenuProvider(), owner),
|
|
40
40
|
)
|
|
41
41
|
|
|
42
42
|
function createPortal(children: unknown, target: unknown) {
|
|
43
|
-
|
|
43
|
+
return createUniversalPortal(children as UniversalRenderable, target)
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
function ensureNativeScriptViewFactory(): void {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
if (!NativeScriptViewFactory.shared) {
|
|
48
|
+
NativeScriptViewFactory.initShared()
|
|
49
|
+
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
function registerHosts(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
triggerId: string,
|
|
54
|
+
trigger: GridLayout,
|
|
55
|
+
previewId: string,
|
|
56
|
+
preview: GridLayout,
|
|
57
57
|
): void {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
ensureNativeScriptViewFactory()
|
|
59
|
+
registerSwiftUIHost(triggerId, { create: () => trigger })
|
|
60
|
+
registerSwiftUIHost(previewId, { create: () => preview })
|
|
61
|
+
SwiftUIManager.registerNativeScriptViews(
|
|
62
|
+
{ [triggerId]: true, [previewId]: true },
|
|
63
|
+
getSwiftUIHostLifecycle(),
|
|
64
|
+
)
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
function TriggerRoot(props: { render: () => any }) @{
|
|
68
|
-
|
|
68
|
+
<>{props.render()}</>
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
let seq = 0
|
|
@@ -74,61 +74,64 @@ let seq = 0
|
|
|
74
74
|
* from the declaring Octane tree into NativeScript hosts. Menu items arrive
|
|
75
75
|
* as serialized data. */
|
|
76
76
|
export function SwiftUIContextMenu(props: SwiftUIContextMenuProps): unknown @{
|
|
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
|
-
|
|
77
|
+
const triggerId = useRef<string>('')
|
|
78
|
+
|
|
79
|
+
if (!triggerId.current) {
|
|
80
|
+
triggerId.current = `ctxmenu${++seq}`
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const previewId = `${triggerId.current}preview`
|
|
84
|
+
const [portalHosts, setPortalHosts] = useState<{
|
|
85
|
+
trigger: GridLayout
|
|
86
|
+
preview: GridLayout
|
|
87
|
+
} | null>(null)
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
const trigger = new GridLayout()
|
|
91
|
+
const preview = new GridLayout()
|
|
92
|
+
|
|
93
|
+
registerHosts(triggerId.current, trigger, previewId, preview)
|
|
94
|
+
setPortalHosts({ trigger, preview })
|
|
95
|
+
|
|
96
|
+
return () => {
|
|
97
|
+
unregisterSwiftUIHost(triggerId.current)
|
|
98
|
+
unregisterSwiftUIHost(previewId)
|
|
99
|
+
}
|
|
100
|
+
}, [])
|
|
101
|
+
|
|
102
|
+
const items = props.items ?? []
|
|
103
|
+
|
|
104
|
+
<>
|
|
105
|
+
<xplatioscontextmenu
|
|
106
|
+
id={props.id}
|
|
107
|
+
className={props.className}
|
|
108
|
+
style={props.style}
|
|
109
|
+
accessibilityLabel={props.accessibilityLabel}
|
|
110
|
+
swiftId="xplat-swiftui-contextmenu"
|
|
111
|
+
data={{
|
|
112
|
+
triggerId: portalHosts ? triggerId.current : '',
|
|
113
|
+
previewId: portalHosts && props.preview ? previewId : '',
|
|
114
|
+
items: items.map((item) => ({
|
|
115
|
+
id: item.id,
|
|
116
|
+
title: item.title,
|
|
117
|
+
destructive: item.destructive ? 1 : 0,
|
|
118
|
+
disabled: item.disabled ? 1 : 0,
|
|
119
|
+
divider: item.divider ? 1 : 0,
|
|
120
|
+
})),
|
|
121
|
+
}}
|
|
122
|
+
onSwiftUIEvent={(event: any) => {
|
|
123
|
+
const item = event.data?.item
|
|
124
|
+
|
|
125
|
+
if (typeof item === 'string' && item) {
|
|
126
|
+
props.onItemSelected?.(item)
|
|
127
|
+
}
|
|
128
|
+
}}
|
|
129
|
+
/>
|
|
130
|
+
{portalHosts && props.trigger
|
|
131
|
+
? createPortal(<TriggerRoot render={props.trigger} />, portalHosts.trigger)
|
|
132
|
+
: null}
|
|
133
|
+
{portalHosts && props.preview
|
|
134
|
+
? createPortal(<TriggerRoot render={props.preview} />, portalHosts.preview)
|
|
135
|
+
: null}
|
|
136
|
+
</>
|
|
134
137
|
}
|
package/src/android/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { MaterialContextMenu } from '../MaterialContextMenu.android.tsrx'
|
|
2
2
|
export type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
MaterialContextMenuActivation,
|
|
4
|
+
MaterialContextMenuItem,
|
|
5
|
+
MaterialContextMenuProps,
|
|
6
6
|
} from './types'
|
package/src/android/types.ts
CHANGED
|
@@ -3,35 +3,35 @@ export type MaterialContextMenuActivation = 'longPress' | 'singlePress'
|
|
|
3
3
|
|
|
4
4
|
/** One row of serialized menu content carried over the bridge. */
|
|
5
5
|
export interface MaterialContextMenuItem {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
/** Stable id reported to `onItemSelected`. */
|
|
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 a divider before this item. */
|
|
15
|
+
divider?: boolean
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/** Android context-menu props. */
|
|
19
19
|
export interface MaterialContextMenuProps {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
/** Activation gesture. @default 'longPress' */
|
|
27
|
+
activation?: MaterialContextMenuActivation
|
|
28
|
+
/** Menu entries in display order. */
|
|
29
|
+
items?: readonly MaterialContextMenuItem[]
|
|
30
|
+
/** Render fn producing the trigger subtree — portaled from the declaring
|
|
31
|
+
* Octane tree into the menu's AndroidView anchor. */
|
|
32
|
+
trigger: () => any
|
|
33
|
+
/** Fires with the selected item id. */
|
|
34
|
+
onItemSelected?: (id: string) => void
|
|
35
|
+
/** Spoken name for assistive tech. */
|
|
36
|
+
accessibilityLabel?: string
|
|
37
37
|
}
|