@ng-native/components 0.27.0 → 0.29.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/android/src/main/java/dev/ngnative/components/ColorSchemeModule.kt +116 -11
- package/android/src/main/java/dev/ngnative/components/ContextMenuPopup.kt +61 -19
- package/android/src/main/java/dev/ngnative/components/MenusModule.kt +22 -0
- package/android/src/main/java/dev/ngnative/components/Shortcut.kt +13 -4
- package/dist/context-menu.d.ts +14 -6
- package/dist/context-menu.js +30 -28
- package/dist/context-menu.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/menu.d.ts +17 -8
- package/dist/menu.js +22 -36
- package/dist/menu.js.map +1 -1
- package/dist/modal.d.ts +6 -2
- package/dist/modal.js.map +1 -1
- package/dist/native-button.d.ts +31 -2
- package/dist/native-button.js +104 -6
- package/dist/native-button.js.map +1 -1
- package/dist/press-menu.d.ts +60 -0
- package/dist/press-menu.js +116 -0
- package/dist/press-menu.js.map +1 -0
- package/dist/pressable.d.ts +3 -0
- package/dist/pressable.js +7 -1
- package/dist/pressable.js.map +1 -1
- package/dist/reanimated.js +27 -3
- package/dist/reanimated.js.map +1 -1
- package/dist/scroll-view.d.ts +3 -1
- package/dist/scroll-view.js +7 -2
- package/dist/scroll-view.js.map +1 -1
- package/dist/settled-updates.d.ts +55 -0
- package/dist/settled-updates.js +77 -0
- package/dist/settled-updates.js.map +1 -0
- package/dist/sidebar-item.d.ts +18 -3
- package/dist/sidebar-item.js +29 -3
- package/dist/sidebar-item.js.map +1 -1
- package/dist/sidebar.js +4 -3
- package/dist/sidebar.js.map +1 -1
- package/dist/split-view.d.ts +23 -2
- package/dist/split-view.js +32 -5
- package/dist/split-view.js.map +1 -1
- package/dist/stand-ins/reanimated.d.ts +5 -0
- package/dist/stand-ins/reanimated.js +7 -0
- package/dist/stand-ins/reanimated.js.map +1 -1
- package/dist/sticky-headers.d.ts +19 -0
- package/dist/sticky-headers.js +67 -10
- package/dist/sticky-headers.js.map +1 -1
- package/dist/sticky-rows.d.ts +123 -26
- package/dist/sticky-rows.js +285 -71
- package/dist/sticky-rows.js.map +1 -1
- package/dist/svg-icon.d.ts +23 -0
- package/dist/svg-icon.js +53 -0
- package/dist/svg-icon.js.map +1 -0
- package/dist/tray-icon.d.ts +7 -1
- package/dist/tray-icon.js +15 -3
- package/dist/tray-icon.js.map +1 -1
- package/dist/virtual-list-sticky-level.d.ts +33 -0
- package/dist/virtual-list-sticky-level.js +58 -0
- package/dist/virtual-list-sticky-level.js.map +1 -0
- package/dist/virtual-list-sticky.d.ts +19 -0
- package/dist/virtual-list-sticky.js +13 -0
- package/dist/virtual-list-sticky.js.map +1 -0
- package/dist/virtual-list.d.ts +29 -7
- package/dist/virtual-list.js +127 -59
- package/dist/virtual-list.js.map +1 -1
- package/dist/worklet-style.d.ts +11 -0
- package/dist/worklet-style.js +15 -1
- package/dist/worklet-style.js.map +1 -1
- package/dist/worklets.d.ts +5 -1
- package/dist/worklets.js.map +1 -1
- package/dist/written-props.js +3 -0
- package/dist/written-props.js.map +1 -1
- package/ios/NgNativeButtonComponentView.mm +8 -4
- package/ios/NgNativeButtonMetrics.h +9 -2
- package/ios/NgNativeButtonMetrics.mm +33 -4
- package/ios/NgNativeButtonShadowNode.h +2 -0
- package/ios/NgNativeButtonShadowNode.mm +5 -1
- package/ios/NgNativeMenus.mm +42 -2
- package/ios/NgNativePressMenu.h +28 -0
- package/ios/NgNativePressMenu.mm +45 -0
- package/package.json +3 -3
|
@@ -4,11 +4,18 @@ import android.app.UiModeManager
|
|
|
4
4
|
import android.content.Context
|
|
5
5
|
import android.content.res.ColorStateList
|
|
6
6
|
import android.content.res.Configuration
|
|
7
|
+
import android.graphics.drawable.BitmapDrawable
|
|
8
|
+
import android.graphics.drawable.Drawable
|
|
9
|
+
import android.graphics.drawable.DrawableContainer
|
|
10
|
+
import android.graphics.drawable.LayerDrawable
|
|
11
|
+
import android.graphics.drawable.NinePatchDrawable
|
|
12
|
+
import android.graphics.drawable.RippleDrawable
|
|
7
13
|
import android.os.Build
|
|
8
14
|
import android.os.Handler
|
|
9
15
|
import android.os.Looper
|
|
10
16
|
import android.view.View
|
|
11
17
|
import android.view.ViewGroup
|
|
18
|
+
import android.widget.EditText
|
|
12
19
|
import androidx.activity.ComponentActivity
|
|
13
20
|
import androidx.appcompat.R
|
|
14
21
|
import androidx.appcompat.content.res.AppCompatResources
|
|
@@ -28,14 +35,20 @@ import java.lang.ref.WeakReference
|
|
|
28
35
|
* (Android 12 and later) is part of the configuration the system applies, so it holds, and the
|
|
29
36
|
* system keeps it across launches. Earlier Android has no such setting and this does nothing there.
|
|
30
37
|
*
|
|
31
|
-
* It also retints the activity's switches after each configuration change the
|
|
32
|
-
* place, whatever made it, as React Native's
|
|
38
|
+
* It also retints the activity's switches and text inputs after each configuration change the
|
|
39
|
+
* activity takes in place, whatever made it, as React Native's keep the colours they were created
|
|
40
|
+
* with.
|
|
33
41
|
*/
|
|
34
42
|
class ColorSchemeModule : Module() {
|
|
35
43
|
private val main = Handler(Looper.getMainLooper())
|
|
36
44
|
private var watched: WeakReference<ComponentActivity>? = null
|
|
45
|
+
/** The theme's selection colour before the change, which a field showing it took by default. */
|
|
46
|
+
private var highlight = 0
|
|
37
47
|
private val onConfigurationChanged = Consumer<Configuration> {
|
|
38
|
-
watched?.get()
|
|
48
|
+
val activity = watched?.get() ?: return@Consumer
|
|
49
|
+
val was = highlight
|
|
50
|
+
highlight = themeHighlight(activity)
|
|
51
|
+
activity.window?.decorView?.let { refreshControls(it, was, highlight) }
|
|
39
52
|
}
|
|
40
53
|
|
|
41
54
|
override fun definition() = ModuleDefinition {
|
|
@@ -63,6 +76,7 @@ class ColorSchemeModule : Module() {
|
|
|
63
76
|
unwatch()
|
|
64
77
|
activity.addOnConfigurationChangedListener(onConfigurationChanged)
|
|
65
78
|
watched = WeakReference(activity)
|
|
79
|
+
highlight = themeHighlight(activity)
|
|
66
80
|
}
|
|
67
81
|
}
|
|
68
82
|
|
|
@@ -73,20 +87,81 @@ class ColorSchemeModule : Module() {
|
|
|
73
87
|
}
|
|
74
88
|
|
|
75
89
|
/**
|
|
76
|
-
* Retint every switch under [view] from its theme as it is now. `SwitchCompat`,
|
|
77
|
-
* Native's `AndroidSwitch` is, takes its thumb and track tints
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
90
|
+
* Retint every switch and text input under [view] from its theme as it is now. `SwitchCompat`,
|
|
91
|
+
* which React Native's `AndroidSwitch` is, takes its thumb and track tints and its press ripple
|
|
92
|
+
* from the theme once, when it is created, and an `EditText` its selection colour, cursor and
|
|
93
|
+
* selection handles, so after a scheme change that an activity handling `uiMode` takes in place,
|
|
94
|
+
* by the system or by `setColorScheme`, those on screen keep the old scheme's. Colours React
|
|
95
|
+
* Native set from JavaScript stay: its `trackColor`, `thumbColor`, `cursorColor` and
|
|
96
|
+
* `selectionHandleColor` are colour filters, which draw in place of a tint and are left alone, and
|
|
97
|
+
* a `selectionColor` is kept unless it is the old theme's own, [was]. A modal's controls are
|
|
98
|
+
* reached too: its host hands out its dialog's views as its children.
|
|
82
99
|
*/
|
|
83
|
-
internal fun
|
|
100
|
+
internal fun refreshControls(view: View, was: Int, highlight: Int) {
|
|
84
101
|
if (view is SwitchCompat) refreshSwitch(view)
|
|
85
|
-
if (view is
|
|
102
|
+
if (view is EditText) refreshField(view, was, highlight)
|
|
103
|
+
if (view is ViewGroup) {
|
|
104
|
+
for (i in 0 until view.childCount) refreshControls(view.getChildAt(i), was, highlight)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The theme's `textColorHighlight`, which React Native gives a field with no `selectionColor`. */
|
|
109
|
+
private fun themeHighlight(context: Context): Int {
|
|
110
|
+
val styled = context.obtainStyledAttributes(intArrayOf(android.R.attr.textColorHighlight))
|
|
111
|
+
try {
|
|
112
|
+
return styled.getColorStateList(0)?.defaultColor ?: 0
|
|
113
|
+
} finally {
|
|
114
|
+
styled.recycle()
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A field's selection colour after a change from [was] to [now]: the new theme's when it showed
|
|
120
|
+
* the old theme's, and its own otherwise.
|
|
121
|
+
*
|
|
122
|
+
* ponytail: a `selectionColor` set to exactly the old theme's colour follows the theme too.
|
|
123
|
+
*/
|
|
124
|
+
internal fun followedHighlight(current: Int, was: Int, now: Int): Int =
|
|
125
|
+
if (current == was) now else current
|
|
126
|
+
|
|
127
|
+
private fun refreshField(field: EditText, was: Int, highlight: Int) {
|
|
128
|
+
field.highlightColor = followedHighlight(field.highlightColor, was, highlight)
|
|
129
|
+
// Android 10 added the getters and setters; earlier, React Native reaches these by reflection.
|
|
130
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return
|
|
131
|
+
// The platform's cursor and handles are tinted `colorControlActivated`. Loading them again does
|
|
132
|
+
// not do: the drawable cache keeps the old scheme's tint, as a theme attribute is not a `uiMode`
|
|
133
|
+
// dependency it knows of. So they are tinted again in place.
|
|
134
|
+
val styled = field.context.obtainStyledAttributes(intArrayOf(android.R.attr.colorControlActivated))
|
|
135
|
+
val tint =
|
|
136
|
+
try {
|
|
137
|
+
styled.getColorStateList(0)
|
|
138
|
+
} finally {
|
|
139
|
+
styled.recycle()
|
|
140
|
+
} ?: return
|
|
141
|
+
field.textCursorDrawable?.takeIf { it.colorFilter == null }?.let {
|
|
142
|
+
it.mutate().setTintList(tint)
|
|
143
|
+
field.textCursorDrawable = it
|
|
144
|
+
}
|
|
145
|
+
// React Native filters all three handles or none.
|
|
146
|
+
if (field.textSelectHandle?.colorFilter != null) return
|
|
147
|
+
field.textSelectHandle?.let { field.setTextSelectHandle(it.mutate().apply { setTintList(tint) }) }
|
|
148
|
+
field.textSelectHandleLeft?.let {
|
|
149
|
+
field.setTextSelectHandleLeft(it.mutate().apply { setTintList(tint) })
|
|
150
|
+
}
|
|
151
|
+
field.textSelectHandleRight?.let {
|
|
152
|
+
field.setTextSelectHandleRight(it.mutate().apply { setTintList(tint) })
|
|
153
|
+
}
|
|
86
154
|
}
|
|
87
155
|
|
|
88
156
|
/** The tints AppCompat gives its own switch drawables, read again; others it leaves alone. */
|
|
89
157
|
private fun refreshSwitch(switch: SwitchCompat) {
|
|
158
|
+
// React Native's `thumbColor` filters the thumb (a state list, whose nine-patch states keep the
|
|
159
|
+
// filter on their paint and report none) and colours the ripple too, which a wrapper for an
|
|
160
|
+
// outline or a border added later keeps.
|
|
161
|
+
// ponytail: a ripple has no colour to read back, so with the thumb filtered it is taken as the
|
|
162
|
+
// app's. One wrapped before `thumbColor` came, which React Native left the theme's, then keeps
|
|
163
|
+
// the old scheme's ripple; an app's own colour is never painted over.
|
|
164
|
+
val ownRipple = switch.thumbDrawable?.let(::hasFilter) == true
|
|
90
165
|
val context = switch.context
|
|
91
166
|
val styled =
|
|
92
167
|
context.obtainStyledAttributes(null, R.styleable.SwitchCompat, R.attr.switchStyle, 0)
|
|
@@ -100,8 +175,38 @@ private fun refreshSwitch(switch: SwitchCompat) {
|
|
|
100
175
|
switch.trackTintList =
|
|
101
176
|
AppCompatResources.getColorStateList(context, R.color.abc_tint_switch_track)
|
|
102
177
|
}
|
|
178
|
+
if (ownRipple) return
|
|
179
|
+
val ripple = themeRipple(switch.background) ?: return
|
|
180
|
+
val highlight = context.obtainStyledAttributes(intArrayOf(R.attr.colorControlHighlight))
|
|
181
|
+
try {
|
|
182
|
+
highlight.getColorStateList(0)?.let(ripple::setColor)
|
|
183
|
+
} finally {
|
|
184
|
+
highlight.recycle()
|
|
185
|
+
}
|
|
103
186
|
}
|
|
104
187
|
|
|
188
|
+
/** Whether a colour filter is on [drawable], or on the state a state list shows. */
|
|
189
|
+
private fun hasFilter(drawable: Drawable): Boolean {
|
|
190
|
+
val shown = if (drawable is DrawableContainer) drawable.current ?: drawable else drawable
|
|
191
|
+
return drawable.colorFilter != null ||
|
|
192
|
+
shown.colorFilter != null ||
|
|
193
|
+
(shown as? NinePatchDrawable)?.paint?.colorFilter != null ||
|
|
194
|
+
(shown as? BitmapDrawable)?.paint?.colorFilter != null
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The theme's borderless item background, whose colour is `colorControlHighlight`: a ripple with
|
|
199
|
+
* no layers, where React Native's `backgroundColor` makes one around a colour. Found inside the
|
|
200
|
+
* layers React Native wraps it in for an outline, a border or a shadow.
|
|
201
|
+
*/
|
|
202
|
+
private fun themeRipple(drawable: Drawable?): RippleDrawable? =
|
|
203
|
+
when (drawable) {
|
|
204
|
+
is RippleDrawable -> drawable.takeIf { it.numberOfLayers == 0 }
|
|
205
|
+
is LayerDrawable ->
|
|
206
|
+
(0 until drawable.numberOfLayers).firstNotNullOfOrNull { themeRipple(drawable.getDrawable(it)) }
|
|
207
|
+
else -> null
|
|
208
|
+
}
|
|
209
|
+
|
|
105
210
|
/**
|
|
106
211
|
* AppCompat's thumb tint, which it builds in code: `colorSwitchThumbNormal`, and
|
|
107
212
|
* `colorControlActivated` when checked.
|
|
@@ -17,11 +17,13 @@ import android.widget.FrameLayout
|
|
|
17
17
|
import android.widget.PopupMenu
|
|
18
18
|
import com.facebook.react.bridge.Arguments
|
|
19
19
|
import com.facebook.react.bridge.WritableMap
|
|
20
|
+
import com.facebook.react.uimanager.IllegalViewOperationException
|
|
20
21
|
import com.facebook.react.uimanager.UIManagerHelper
|
|
21
22
|
import com.facebook.react.uimanager.events.Event
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
|
-
* A `[contextMenu]` drawn as a PopupMenu at a point in the window,
|
|
25
|
+
* A `[contextMenu]` drawn as a PopupMenu at a point in the window, or a `<pressable>`'s `[menu]`
|
|
26
|
+
* drawn below its view (`showUnder`), from the entries
|
|
25
27
|
* `serializeMenu` makes: labels, separators (dividers between groups, Android 9 and later),
|
|
26
28
|
* submenus, inline sections, checked, disabled and destructive items, and `icon` as a drawable
|
|
27
29
|
* resource's name. A PopupMenu row has no second line or tooltip, so `subtitle` is not shown.
|
|
@@ -77,13 +79,65 @@ internal object ContextMenuPopup {
|
|
|
77
79
|
topMargin = (y - at[1]).toInt()
|
|
78
80
|
},
|
|
79
81
|
)
|
|
80
|
-
|
|
81
|
-
val
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
// Later, not while the view tree may be detaching its children.
|
|
83
|
+
val (popup, done) = popup(activity, anchor, Gravity.NO_GRAVITY, view, entries) {
|
|
84
|
+
main.post { content.removeView(anchor) }
|
|
85
|
+
} ?: run {
|
|
84
86
|
content.removeView(anchor)
|
|
85
87
|
return false
|
|
86
88
|
}
|
|
89
|
+
// The anchor has no place until it is laid out, so the menu waits for that.
|
|
90
|
+
anchor.addOnLayoutChangeListener(
|
|
91
|
+
object : View.OnLayoutChangeListener {
|
|
92
|
+
override fun onLayoutChange(
|
|
93
|
+
v: View, l: Int, t: Int, r: Int, b: Int, ol: Int, ot: Int, or: Int, ob: Int,
|
|
94
|
+
) {
|
|
95
|
+
anchor.removeOnLayoutChangeListener(this)
|
|
96
|
+
if (anchor.isAttachedToWindow && view.isAttachedToWindow) popup.show() else done()
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
)
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The view `resolve` finds for `tag`, or none where it has none, as for a view gone since the
|
|
105
|
+
* press: React Native's `resolveView` throws for a tag it has no view for.
|
|
106
|
+
*/
|
|
107
|
+
fun viewFor(tag: Int, resolve: (Int) -> View?): View? =
|
|
108
|
+
try {
|
|
109
|
+
resolve(tag)
|
|
110
|
+
} catch (_: IllegalViewOperationException) {
|
|
111
|
+
null
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Show `entries` below `view`, from its leading edge, as a `<pressable>`'s `[menu]` opens on a
|
|
116
|
+
* press. Whether there was a menu: none for a view not attached to `activity`'s window.
|
|
117
|
+
*/
|
|
118
|
+
fun showUnder(activity: Activity, view: View, entries: List<Map<String, Any?>>): Boolean {
|
|
119
|
+
if (!view.isAttachedToWindow) return false
|
|
120
|
+
val (popup, _) = popup(activity, view, Gravity.START, view, entries) {} ?: return false
|
|
121
|
+
popup.show()
|
|
122
|
+
return true
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A PopupMenu of `entries` at `anchor`, whose choice goes to `view`, and the function that lets
|
|
127
|
+
* go of it, which runs `release` too and runs as it is dismissed. Null where it shows no item.
|
|
128
|
+
*/
|
|
129
|
+
private fun popup(
|
|
130
|
+
activity: Activity,
|
|
131
|
+
anchor: View,
|
|
132
|
+
gravity: Int,
|
|
133
|
+
view: View,
|
|
134
|
+
entries: List<Map<String, Any?>>,
|
|
135
|
+
release: () -> Unit,
|
|
136
|
+
): Pair<PopupMenu, () -> Unit>? {
|
|
137
|
+
val popup = PopupMenu(activity, anchor, gravity)
|
|
138
|
+
val ids = HashMap<Int, String>()
|
|
139
|
+
fill(activity, popup.menu, entries, ids, destructiveColour(isLight(activity)))
|
|
140
|
+
if (popup.menu.size() == 0) return null
|
|
87
141
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) popup.setForceShowIcon(true)
|
|
88
142
|
popup.setOnMenuItemClickListener { item ->
|
|
89
143
|
val id = ids[item.itemId] ?: return@setOnMenuItemClickListener false
|
|
@@ -94,24 +148,12 @@ internal object ContextMenuPopup {
|
|
|
94
148
|
// A submenu open in its own popup goes too.
|
|
95
149
|
val gone = closingOnDetach(popup.menu, popup::dismiss)
|
|
96
150
|
view.addOnAttachStateChangeListener(gone)
|
|
97
|
-
// Later, not while the view tree may be detaching its children.
|
|
98
151
|
val done = {
|
|
99
152
|
view.removeOnAttachStateChangeListener(gone)
|
|
100
|
-
|
|
153
|
+
release()
|
|
101
154
|
}
|
|
102
155
|
popup.setOnDismissListener { done() }
|
|
103
|
-
|
|
104
|
-
anchor.addOnLayoutChangeListener(
|
|
105
|
-
object : View.OnLayoutChangeListener {
|
|
106
|
-
override fun onLayoutChange(
|
|
107
|
-
v: View, l: Int, t: Int, r: Int, b: Int, ol: Int, ot: Int, or: Int, ob: Int,
|
|
108
|
-
) {
|
|
109
|
-
anchor.removeOnLayoutChangeListener(this)
|
|
110
|
-
if (anchor.isAttachedToWindow && view.isAttachedToWindow) popup.show() else done()
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
)
|
|
114
|
-
return true
|
|
156
|
+
return popup to done
|
|
115
157
|
}
|
|
116
158
|
|
|
117
159
|
/**
|
|
@@ -2,6 +2,8 @@ package dev.ngnative.components
|
|
|
2
2
|
|
|
3
3
|
import android.os.Handler
|
|
4
4
|
import android.os.Looper
|
|
5
|
+
import com.facebook.react.bridge.ReactContext
|
|
6
|
+
import com.facebook.react.uimanager.UIManagerHelper
|
|
5
7
|
import expo.modules.kotlin.modules.Module
|
|
6
8
|
import expo.modules.kotlin.modules.ModuleDefinition
|
|
7
9
|
|
|
@@ -18,6 +20,10 @@ import expo.modules.kotlin.modules.ModuleDefinition
|
|
|
18
20
|
* as a PopupMenu at the press point, and so does the view's long-press accessibility action.
|
|
19
21
|
* `setTakesLongPress` names views inside one that take a long press themselves, where a long press
|
|
20
22
|
* opens no menu.
|
|
23
|
+
*
|
|
24
|
+
* `popUpMenu` draws a `<pressable>`'s `[menu]` below the view with that tag now, on a press, in
|
|
25
|
+
* whichever window the view is in: the activity's, or a `<modal>`'s dialog. Either menu sends the
|
|
26
|
+
* item chosen to its view as `topContextMenuSelect`.
|
|
21
27
|
*/
|
|
22
28
|
class MenusModule : Module() {
|
|
23
29
|
private val main = Handler(Looper.getMainLooper())
|
|
@@ -59,6 +65,10 @@ class MenusModule : Module() {
|
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
67
|
|
|
68
|
+
Function("popUpMenu") { tag: Int, entries: List<Map<String, Any?>> ->
|
|
69
|
+
main.post { popUpMenu(tag, entries) }
|
|
70
|
+
}
|
|
71
|
+
|
|
62
72
|
// On the main thread, in order with the old module's `OnDestroy` and every call after.
|
|
63
73
|
OnCreate {
|
|
64
74
|
main.post { MenuHost.attach(sender) }
|
|
@@ -78,6 +88,18 @@ class MenusModule : Module() {
|
|
|
78
88
|
}
|
|
79
89
|
}
|
|
80
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Show `entries` below the view with `tag`. React Native's UIManager finds it by its tag in any
|
|
93
|
+
* window, a `<modal>`'s dialog among them, where a search of the activity's window would not.
|
|
94
|
+
*/
|
|
95
|
+
private fun popUpMenu(tag: Int, entries: List<Map<String, Any?>>) {
|
|
96
|
+
val activity = appContext.currentActivity ?: return
|
|
97
|
+
val context = appContext.reactContext as? ReactContext ?: return
|
|
98
|
+
val view = ContextMenuPopup.viewFor(tag) { UIManagerHelper.getUIManagerForReactTag(context, it)?.resolveView(it) } ?: return
|
|
99
|
+
ContextMenuPopup.showUnder(activity, view, entries)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
81
103
|
private fun watchActivity() {
|
|
82
104
|
val activity = appContext.currentActivity ?: return
|
|
83
105
|
main.post { MenuWindowCallback.install(activity) }
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
package dev.ngnative.components
|
|
2
2
|
|
|
3
|
+
import android.os.Build
|
|
3
4
|
import android.view.KeyEvent
|
|
4
5
|
import android.view.KeyboardShortcutGroup
|
|
5
6
|
import android.view.KeyboardShortcutInfo
|
|
@@ -64,8 +65,15 @@ internal class Shortcut(
|
|
|
64
65
|
return KeyboardShortcutInfo(label, helperChar() ?: return null, modifiers)
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
/**
|
|
68
|
-
|
|
68
|
+
/**
|
|
69
|
+
* The key code of a key `KeyboardEvent.key` names with a word, or null. Null for F13 to F20
|
|
70
|
+
* before Android 16, whose helper rejects a key code past the last one the OS knows.
|
|
71
|
+
*/
|
|
72
|
+
fun keyCode(sdk: Int = Build.VERSION.SDK_INT): Int? {
|
|
73
|
+
val code = NAMED.entries.firstOrNull { it.value.equals(normalKey, true) }?.key ?: return null
|
|
74
|
+
val unknown = code in KeyEvent.KEYCODE_F13..KeyEvent.KEYCODE_F20 && sdk < Build.VERSION_CODES.BAKLAVA
|
|
75
|
+
return if (unknown) null else code
|
|
76
|
+
}
|
|
69
77
|
|
|
70
78
|
/** The character the helper shows for a key with no key code, or null for a word it lacks. */
|
|
71
79
|
fun helperChar(): Char? = normalKey.singleOrNull()?.uppercaseChar()
|
|
@@ -89,8 +97,8 @@ internal class Shortcut(
|
|
|
89
97
|
}
|
|
90
98
|
|
|
91
99
|
/**
|
|
92
|
-
* The keys `KeyboardEvent.key` names with a word, by their key code.
|
|
93
|
-
*
|
|
100
|
+
* The keys `KeyboardEvent.key` names with a word, by their key code. F13 to F20 have key codes
|
|
101
|
+
* from Android 16 (API 36); an older Android sends none for them.
|
|
94
102
|
*/
|
|
95
103
|
private val NAMED: Map<Int, String> = buildMap {
|
|
96
104
|
put(KeyEvent.KEYCODE_DPAD_LEFT, "ArrowLeft")
|
|
@@ -113,6 +121,7 @@ internal class Shortcut(
|
|
|
113
121
|
put(KeyEvent.KEYCODE_SCROLL_LOCK, "ScrollLock")
|
|
114
122
|
put(KeyEvent.KEYCODE_MENU, "ContextMenu")
|
|
115
123
|
for (n in 1..12) put(KeyEvent.KEYCODE_F1 + n - 1, "F$n")
|
|
124
|
+
for (n in 13..20) put(KeyEvent.KEYCODE_F13 + n - 13, "F$n")
|
|
116
125
|
}
|
|
117
126
|
|
|
118
127
|
/** The key `event` is, as `KeyboardEvent.key` names it, ignoring Shift: `k`, `,`, `Enter`. */
|
package/dist/context-menu.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { InjectionToken, type TemplateRef } from '@angular/core';
|
|
1
|
+
import { InjectionToken, Injector, type Signal, type TemplateRef } from '@angular/core';
|
|
2
|
+
import { type NativeMenuEntry, type ShortcutOs } from '@ng-native/device';
|
|
2
3
|
import * as i0 from "@angular/core";
|
|
3
4
|
/**
|
|
4
5
|
* The `[contextMenu]` on an element, for `<pressable>`: where a long press opens the menu,
|
|
@@ -32,9 +33,7 @@ export declare class ContextMenu {
|
|
|
32
33
|
/** Android's and iOS's hosts, which take the menu by `nativeID` and draw it on a long press. */
|
|
33
34
|
private readonly byNativeId;
|
|
34
35
|
private readonly sender;
|
|
35
|
-
private readonly built;
|
|
36
36
|
private readonly menu;
|
|
37
|
-
/** The entries to send, or null where they show no item: only separators and empty sections. */
|
|
38
37
|
private readonly sent;
|
|
39
38
|
constructor();
|
|
40
39
|
/** Whether a long press opens this menu now, so `(longPress)` does not also fire. */
|
|
@@ -44,9 +43,18 @@ export declare class ContextMenu {
|
|
|
44
43
|
* name, and runs as the view's own `topContextMenuSelect` would.
|
|
45
44
|
*/
|
|
46
45
|
private sendByNativeId;
|
|
47
|
-
private choose;
|
|
48
|
-
/** Make the template's items, which register with the menu this element sends. */
|
|
49
|
-
private build;
|
|
50
46
|
static ɵfac: i0.ɵɵFactoryDeclaration<ContextMenu, never>;
|
|
51
47
|
static ɵdir: i0.ɵɵDirectiveDeclaration<ContextMenu, "[contextMenu]", never, { "contextMenu": { "alias": "contextMenu"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
|
|
52
48
|
}
|
|
49
|
+
/** A menu written as a template of `<menu-item>`s, as `templateMenu` makes it. */
|
|
50
|
+
export interface TemplateMenu {
|
|
51
|
+
/** The entries to send, or null where they show no item: only separators and empty sections. */
|
|
52
|
+
readonly sent: Signal<NativeMenuEntry[] | null>;
|
|
53
|
+
/** Run the item with `id`, as the menu is now: nothing for an id it no longer has. */
|
|
54
|
+
choose(id: string): void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The menu `template` holds, its items made in `injector` and remade as the template changes, and
|
|
58
|
+
* destroyed with it. Called in an injection context.
|
|
59
|
+
*/
|
|
60
|
+
export declare function templateMenu(template: () => TemplateRef<unknown> | null, injector: Injector, os: ShortcutOs): TemplateMenu;
|
package/dist/context-menu.js
CHANGED
|
@@ -36,18 +36,9 @@ export class ContextMenu {
|
|
|
36
36
|
sender = this.byNativeId
|
|
37
37
|
? byNativeId(this.engine, this.node, (nativeID) => this.sendByNativeId(nativeID), (nativeID) => this.menus.setContextMenu(nativeID, null))
|
|
38
38
|
: null;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
menu = computed(() => serializeMenu(this.built()?.model.entries() ?? [], this.os), /* @ts-ignore */
|
|
42
|
-
...(ngDevMode ? [{ debugName: "menu" }] : /* istanbul ignore next */ []));
|
|
43
|
-
/** The entries to send, or null where they show no item: only separators and empty sections. */
|
|
44
|
-
sent = computed(() => {
|
|
45
|
-
const { entries } = this.menu();
|
|
46
|
-
return showsAnItem(entries) ? entries : null;
|
|
47
|
-
}, /* @ts-ignore */
|
|
48
|
-
...(ngDevMode ? [{ debugName: "sent" }] : /* istanbul ignore next */ []));
|
|
39
|
+
menu = templateMenu(() => this.contextMenu() ?? null, this.injector, this.os);
|
|
40
|
+
sent = this.menu.sent;
|
|
49
41
|
constructor() {
|
|
50
|
-
effect(() => this.build(this.contextMenu() ?? null));
|
|
51
42
|
effect(() => {
|
|
52
43
|
this.engine.setProp(this.node, 'contextMenu', this.sent());
|
|
53
44
|
this.sender?.();
|
|
@@ -55,12 +46,9 @@ export class ContextMenu {
|
|
|
55
46
|
const stop = this.engine.setEventListener(this.node, 'topContextMenuSelect', (event) => {
|
|
56
47
|
const id = event.nativeEvent?.id;
|
|
57
48
|
if (id !== undefined)
|
|
58
|
-
this.choose(id);
|
|
59
|
-
});
|
|
60
|
-
inject(DestroyRef).onDestroy(() => {
|
|
61
|
-
stop();
|
|
62
|
-
this.build(null);
|
|
49
|
+
this.menu.choose(id);
|
|
63
50
|
});
|
|
51
|
+
inject(DestroyRef).onDestroy(stop);
|
|
64
52
|
}
|
|
65
53
|
/** Whether a long press opens this menu now, so `(longPress)` does not also fire. */
|
|
66
54
|
takesLongPress() {
|
|
@@ -71,18 +59,7 @@ export class ContextMenu {
|
|
|
71
59
|
* name, and runs as the view's own `topContextMenuSelect` would.
|
|
72
60
|
*/
|
|
73
61
|
sendByNativeId(nativeID) {
|
|
74
|
-
this.menus.setContextMenu(nativeID, untracked(this.sent), (id) => this.choose(id));
|
|
75
|
-
}
|
|
76
|
-
choose(id) {
|
|
77
|
-
this.menu().actions.get(id)?.();
|
|
78
|
-
}
|
|
79
|
-
/** Make the template's items, which register with the menu this element sends. */
|
|
80
|
-
build(template) {
|
|
81
|
-
// Read and written outside the effect that calls this, which would otherwise run again.
|
|
82
|
-
untracked(() => {
|
|
83
|
-
this.built()?.destroy();
|
|
84
|
-
this.built.set(template ? buildMenuTemplate(template, this.injector) : null);
|
|
85
|
-
});
|
|
62
|
+
this.menus.setContextMenu(nativeID, untracked(this.sent), (id) => this.menu.choose(id));
|
|
86
63
|
}
|
|
87
64
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.0", ngImport: i0, type: ContextMenu, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
88
65
|
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.2.0", type: ContextMenu, isStandalone: true, selector: "[contextMenu]", inputs: { contextMenu: { classPropertyName: "contextMenu", publicName: "contextMenu", isSignal: true, isRequired: false, transformFunction: null } }, providers: [{ provide: CONTEXT_MENU, useExisting: forwardRef(() => ContextMenu) }], ngImport: i0 });
|
|
@@ -94,6 +71,31 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.0", ngImpor
|
|
|
94
71
|
providers: [{ provide: CONTEXT_MENU, useExisting: forwardRef(() => ContextMenu) }],
|
|
95
72
|
}]
|
|
96
73
|
}], ctorParameters: () => [], propDecorators: { contextMenu: [{ type: i0.Input, args: [{ isSignal: true, alias: "contextMenu", required: false }] }] } });
|
|
74
|
+
/**
|
|
75
|
+
* The menu `template` holds, its items made in `injector` and remade as the template changes, and
|
|
76
|
+
* destroyed with it. Called in an injection context.
|
|
77
|
+
*/
|
|
78
|
+
export function templateMenu(template, injector, os) {
|
|
79
|
+
const built = signal(null, /* @ts-ignore */
|
|
80
|
+
...(ngDevMode ? [{ debugName: "built" }] : /* istanbul ignore next */ []));
|
|
81
|
+
const build = (next) =>
|
|
82
|
+
// Read and written outside the effect that calls this, which would otherwise run again.
|
|
83
|
+
untracked(() => {
|
|
84
|
+
built()?.destroy();
|
|
85
|
+
built.set(next ? buildMenuTemplate(next, injector) : null);
|
|
86
|
+
});
|
|
87
|
+
effect(() => build(template()));
|
|
88
|
+
inject(DestroyRef).onDestroy(() => build(null));
|
|
89
|
+
const menu = computed(() => serializeMenu(built()?.model.entries() ?? [], os), /* @ts-ignore */
|
|
90
|
+
...(ngDevMode ? [{ debugName: "menu" }] : /* istanbul ignore next */ []));
|
|
91
|
+
return {
|
|
92
|
+
sent: computed(() => {
|
|
93
|
+
const { entries } = menu();
|
|
94
|
+
return showsAnItem(entries) ? entries : null;
|
|
95
|
+
}),
|
|
96
|
+
choose: (id) => untracked(menu).actions.get(id)?.(),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
97
99
|
/** Whether `entries` show an item: one that is not a separator, nor a section with none. */
|
|
98
100
|
function showsAnItem(entries) {
|
|
99
101
|
return entries.some((entry) => !('separator' in entry) && (!entry.inline || showsAnItem(entry.items ?? [])));
|
package/dist/context-menu.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-menu.js","sourceRoot":"","sources":["../src/context-menu.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,SAAS,EACT,UAAU,EACV,cAAc,EACd,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,UAAU,EACV,MAAM,EACN,KAAK,EACL,MAAM,EACN,SAAS,
|
|
1
|
+
{"version":3,"file":"context-menu.js","sourceRoot":"","sources":["../src/context-menu.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,SAAS,EACT,UAAU,EACV,cAAc,EACd,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,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE;KACpD,CAAC;AACJ,CAAC;AAED,4FAA4F;AAC5F,SAAS,WAAW,CAAC,OAAmC;IACtD,OAAO,OAAO,CAAC,IAAI,CACjB,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,MAAM,IAAI,WAAW,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,CACxF,CAAC;AACJ,CAAC","sourcesContent":["import {\n DestroyRef,\n Directive,\n ElementRef,\n InjectionToken,\n Injector,\n computed,\n effect,\n forwardRef,\n inject,\n input,\n signal,\n untracked,\n type Signal,\n type TemplateRef,\n} from '@angular/core';\nimport {\n Menus,\n OS,\n buildMenuTemplate,\n serializeMenu,\n type MenuTemplate,\n type NativeMenuEntry,\n type ShortcutOs,\n} from '@ng-native/device';\nimport { HostEngine, type HostNode } from '@ng-native/fabric';\nimport { byNativeId } from './native-id.ts';\n\n/**\n * The `[contextMenu]` on an element, for `<pressable>`: where a long press opens the menu,\n * `(longPress)` does not also fire.\n */\nexport const CONTEXT_MENU = new InjectionToken<{ takesLongPress(): boolean }>(\n 'angular-native.contextMenu',\n);\n\n/**\n * A context menu on an element: `[contextMenu]=\"menu\"`, with `<ng-template #menu>` holding\n * `<menu-item>`s. A right-click opens it on macOS. On Android a long press or a right-click opens\n * it as a PopupMenu at the press point, and on iOS and iPadOS a long press, or a secondary click\n * from an iPad pointer, opens it through UIContextMenuInteraction. On Android and iOS that takes\n * the `NgNativeMenus` module, which Expo Go lacks, and where it is there `(longPress)` does not\n * also fire. On Android a `(longPress)` on a view inside the element keeps its long press, which\n * then opens no menu: the innermost handler wins.\n *\n * The host contract: the view takes a `contextMenu` prop, the menu as `serializeMenu` makes it\n * (null to take it away), and sends `topContextMenuSelect` with the chosen item's `id`. React\n * Native's Android and iOS views drop a prop they do not know, so there the menu goes through\n * `Menus.setContextMenu` by the view's `nativeID`, and an element without one is given one. iOS's\n * module sends the choice, which reaches the element as `topContextMenuSelect`.\n */\n@Directive({\n selector: '[contextMenu]',\n providers: [{ provide: CONTEXT_MENU, useExisting: forwardRef(() => ContextMenu) }],\n})\nexport class ContextMenu {\n readonly contextMenu = input<TemplateRef<unknown> | null>();\n\n private readonly node = inject(ElementRef).nativeElement as HostNode;\n private readonly engine = inject(HostEngine);\n private readonly injector = inject(Injector);\n private readonly os = inject(OS);\n private readonly menus = inject(Menus);\n /** Android's and iOS's hosts, which take the menu by `nativeID` and draw it on a long press. */\n private readonly byNativeId =\n (this.os === 'android' || this.os === 'ios') && this.menus.contextMenuByNativeId;\n private readonly sender = this.byNativeId\n ? byNativeId(\n this.engine,\n this.node,\n (nativeID) => this.sendByNativeId(nativeID),\n (nativeID) => this.menus.setContextMenu(nativeID, null),\n )\n : null;\n private readonly menu = templateMenu(() => this.contextMenu() ?? null, this.injector, this.os);\n private readonly sent = this.menu.sent;\n\n constructor() {\n effect(() => {\n this.engine.setProp(this.node, 'contextMenu', this.sent());\n this.sender?.();\n });\n const stop = this.engine.setEventListener(this.node, 'topContextMenuSelect', (event) => {\n const id = (event as { nativeEvent?: { id?: string } }).nativeEvent?.id;\n if (id !== undefined) this.menu.choose(id);\n });\n inject(DestroyRef).onDestroy(stop);\n }\n\n /** Whether a long press opens this menu now, so `(longPress)` does not also fire. */\n takesLongPress(): boolean {\n return this.byNativeId && this.sent() !== null;\n }\n\n /**\n * Send the menu to the host under the view's `nativeID`. On iOS the choice comes back by that\n * name, and runs as the view's own `topContextMenuSelect` would.\n */\n private sendByNativeId(nativeID: string): void {\n this.menus.setContextMenu(nativeID, untracked(this.sent), (id) => this.menu.choose(id));\n }\n}\n\n/** A menu written as a template of `<menu-item>`s, as `templateMenu` makes it. */\nexport interface TemplateMenu {\n /** The entries to send, or null where they show no item: only separators and empty sections. */\n readonly sent: Signal<NativeMenuEntry[] | null>;\n /** Run the item with `id`, as the menu is now: nothing for an id it no longer has. */\n choose(id: string): void;\n}\n\n/**\n * The menu `template` holds, its items made in `injector` and remade as the template changes, and\n * destroyed with it. Called in an injection context.\n */\nexport function templateMenu(\n template: () => TemplateRef<unknown> | null,\n injector: Injector,\n os: ShortcutOs,\n): TemplateMenu {\n const built = signal<MenuTemplate | null>(null);\n const build = (next: TemplateRef<unknown> | null) =>\n // Read and written outside the effect that calls this, which would otherwise run again.\n untracked(() => {\n built()?.destroy();\n built.set(next ? buildMenuTemplate(next, injector) : null);\n });\n effect(() => build(template()));\n inject(DestroyRef).onDestroy(() => build(null));\n const menu = computed(() => serializeMenu(built()?.model.entries() ?? [], os));\n return {\n sent: computed(() => {\n const { entries } = menu();\n return showsAnItem(entries) ? entries : null;\n }),\n choose: (id) => untracked(menu).actions.get(id)?.(),\n };\n}\n\n/** Whether `entries` show an item: one that is not a separator, nor a section with none. */\nfunction showsAnItem(entries: readonly NativeMenuEntry[]): boolean {\n return entries.some(\n (entry) => !('separator' in entry) && (!entry.inline || showsAnItem(entry.items ?? [])),\n );\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -19,10 +19,11 @@ export { KeyboardAvoidingView, type KeyboardAvoidingBehavior } from './keyboard-
|
|
|
19
19
|
export { Modal, type ModalAnchor, type ModalOrientation, type ModalPresentationStyle, } from './modal.ts';
|
|
20
20
|
export { MenuBar, MenuGroup, MenuItem, MenuSeparator, treeOrder } from './menu.ts';
|
|
21
21
|
export { CONTEXT_MENU, ContextMenu } from './context-menu.ts';
|
|
22
|
+
export { PRESS_MENU, PressMenu } from './press-menu.ts';
|
|
22
23
|
export { Shortcut } from './shortcut.ts';
|
|
23
24
|
export { DragItem, FileDrop, type DragItemValue, type DroppedFile, type FileDropEvent, } from './drag-and-drop.ts';
|
|
24
25
|
export { TrayIcon } from './tray-icon.ts';
|
|
25
|
-
export { SplitView, SplitViewContent, type SplitViewShowing } from './split-view.ts';
|
|
26
|
+
export { SplitView, SplitViewContent, type SplitViewContentStyle, type SplitViewShowing, } from './split-view.ts';
|
|
26
27
|
export { Sidebar, type SidebarRow } from './sidebar.ts';
|
|
27
28
|
export { SidebarSection } from './sidebar-section.ts';
|
|
28
29
|
export { SidebarItem, type SidebarItemIcon } from './sidebar-item.ts';
|
|
@@ -48,6 +49,8 @@ export { ViewBase, ViewBehavior, contributeAccessibility, type AccessibilityActi
|
|
|
48
49
|
export { VirtualList, VirtualListRow, VirtualListSeparator, type VirtualItemHeight, type VirtualListSeparatorContext, type VirtualListPadding, type VirtualListVisiblePosition, type VirtualRow, } from './virtual-list.ts';
|
|
49
50
|
export { VIRTUAL_LIST_LAYOUT, type VirtualListLayout } from './virtual-list-layout.ts';
|
|
50
51
|
export { VirtualListNumColumns } from './virtual-list-num-columns.ts';
|
|
52
|
+
export { VIRTUAL_LIST_STICKY, type VirtualListSticky } from './virtual-list-sticky.ts';
|
|
53
|
+
export { VirtualListStickyLevel } from './virtual-list-sticky-level.ts';
|
|
51
54
|
export { type GestureBackend, type GestureSpec, type GestureTarget } from './gesture-backend.ts';
|
|
52
55
|
export { workletScroll, workletStyle, type SharedValue, type WorkletBackend, type WorkletScrollSpec, type WorkletStyleSpec, type WorkletTarget, } from './worklets.ts';
|
|
53
56
|
export { releaseOnDestroy } from './release-on-destroy.ts';
|
package/dist/index.js
CHANGED
|
@@ -19,10 +19,11 @@ export { KeyboardAvoidingView } from "./keyboard-avoiding-view.js";
|
|
|
19
19
|
export { Modal, } from "./modal.js";
|
|
20
20
|
export { MenuBar, MenuGroup, MenuItem, MenuSeparator, treeOrder } from "./menu.js";
|
|
21
21
|
export { CONTEXT_MENU, ContextMenu } from "./context-menu.js";
|
|
22
|
+
export { PRESS_MENU, PressMenu } from "./press-menu.js";
|
|
22
23
|
export { Shortcut } from "./shortcut.js";
|
|
23
24
|
export { DragItem, FileDrop, } from "./drag-and-drop.js";
|
|
24
25
|
export { TrayIcon } from "./tray-icon.js";
|
|
25
|
-
export { SplitView, SplitViewContent } from "./split-view.js";
|
|
26
|
+
export { SplitView, SplitViewContent, } from "./split-view.js";
|
|
26
27
|
export { Sidebar } from "./sidebar.js";
|
|
27
28
|
export { SidebarSection } from "./sidebar-section.js";
|
|
28
29
|
export { SidebarItem } from "./sidebar-item.js";
|
|
@@ -48,6 +49,8 @@ export { ViewBase, ViewBehavior, contributeAccessibility, } from "./view-base.js
|
|
|
48
49
|
export { VirtualList, VirtualListRow, VirtualListSeparator, } from "./virtual-list.js";
|
|
49
50
|
export { VIRTUAL_LIST_LAYOUT } from "./virtual-list-layout.js";
|
|
50
51
|
export { VirtualListNumColumns } from "./virtual-list-num-columns.js";
|
|
52
|
+
export { VIRTUAL_LIST_STICKY } from "./virtual-list-sticky.js";
|
|
53
|
+
export { VirtualListStickyLevel } from "./virtual-list-sticky-level.js";
|
|
51
54
|
export {} from "./gesture-backend.js";
|
|
52
55
|
export { workletScroll, workletStyle, } from "./worklets.js";
|
|
53
56
|
export { releaseOnDestroy } from "./release-on-destroy.js";
|