-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNavigationSuiteScaffold2.kt
584 lines (568 loc) · 26.9 KB
/
NavigationSuiteScaffold2.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package dev.dimension.flare.ui.component
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.material3.BadgedBox
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.FilledTonalIconButton
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.NavigationDrawerItemDefaults
import androidx.compose.material3.NavigationRail
import androidx.compose.material3.NavigationRailItem
import androidx.compose.material3.NavigationRailItemDefaults
import androidx.compose.material3.PermanentDrawerSheet
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
import androidx.compose.material3.adaptive.navigationsuite.ExperimentalMaterial3AdaptiveNavigationSuiteApi
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuite
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteColors
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteDefaults
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteItemColors
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffoldDefaults
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScope
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType
import androidx.compose.material3.contentColorFor
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.State
import androidx.compose.runtime.collection.MutableVector
import androidx.compose.runtime.collection.mutableVectorOf
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.movableContentOf
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.xr.compose.material3.EnableXrComponentOverrides
import androidx.xr.compose.material3.ExperimentalMaterial3XrApi
import androidx.xr.compose.platform.LocalSession
import androidx.xr.compose.platform.LocalSpatialCapabilities
import androidx.xr.compose.spatial.EdgeOffset
import androidx.xr.compose.spatial.Orbiter
import androidx.xr.compose.spatial.OrbiterEdge
import androidx.xr.compose.subspace.SpatialPanel
import androidx.xr.compose.subspace.layout.SpatialRoundedCornerShape
import androidx.xr.compose.subspace.layout.SubspaceModifier
import androidx.xr.compose.subspace.layout.height
import androidx.xr.compose.subspace.layout.movable
import androidx.xr.compose.subspace.layout.resizable
import androidx.xr.compose.subspace.layout.width
import compose.icons.FontAwesomeIcons
import compose.icons.fontawesomeicons.Solid
import compose.icons.fontawesomeicons.solid.DownLeftAndUpRightToCenter
import dev.dimension.flare.ui.component.status.FlareDividerDefaults
import kotlinx.coroutines.launch
import kotlin.math.roundToInt
private val bottomBarHeight = 56.dp
val LocalBottomBarHeight = androidx.compose.runtime.staticCompositionLocalOf<Dp> { bottomBarHeight }
internal data class BottomBarSettings(
val autoHide: Boolean = true,
val withDivider: Boolean = true,
)
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3XrApi::class)
@ExperimentalMaterial3AdaptiveNavigationSuiteApi
@Composable
fun NavigationSuiteScaffold2(
navigationSuiteItems: NavigationSuiteScope2.() -> Unit,
secondaryItems: NavigationSuiteScope2.() -> Unit,
modifier: Modifier = Modifier,
drawerState: DrawerState = rememberDrawerState(DrawerValue.Closed),
drawerGesturesEnabled: Boolean = true,
bottomBarAutoHideEnabled: Boolean = true,
bottomBarDividerEnabled: Boolean = true,
layoutType: NavigationSuiteType =
NavigationSuiteScaffoldDefaults.calculateFromAdaptiveInfo(currentWindowAdaptiveInfo()),
navigationSuiteColors: NavigationSuiteColors = NavigationSuiteDefaults.colors(),
containerColor: Color = MaterialTheme.colorScheme.background,
contentColor: Color = contentColorFor(containerColor),
railHeader: @Composable (ColumnScope.() -> Unit)? = null,
drawerHeader: @Composable (ColumnScope.() -> Unit)? = null,
footerItems: NavigationSuiteScope2.() -> Unit = {},
content: @Composable () -> Unit = {},
) {
val coroutineScope = rememberCoroutineScope()
val bottomBarHeightPx =
with(LocalDensity.current) {
val navigationBar = WindowInsets.navigationBars
remember(navigationBar) {
bottomBarHeight.roundToPx().toFloat() + navigationBar.getBottom(this)
}
}
var bottomBarOffsetHeightPx by remember { mutableFloatStateOf(0f) }
val nestedScrollConnection =
remember(bottomBarHeightPx) {
object : NestedScrollConnection {
override fun onPreScroll(
available: Offset,
source: NestedScrollSource,
): Offset {
val delta = available.y
val newOffset = bottomBarOffsetHeightPx + delta
bottomBarOffsetHeightPx = newOffset.coerceIn(-bottomBarHeightPx, 0f)
return Offset.Zero
}
}
}
Surface(
modifier =
modifier
.let {
if (bottomBarAutoHideEnabled) {
it.nestedScroll(nestedScrollConnection)
} else {
it
}
},
color = containerColor,
contentColor = contentColor,
) {
OptionalSubspace {
EnableXrComponentOverrides {
val scope by rememberStateOfItems(navigationSuiteItems)
val footerScope by rememberStateOfItems(footerItems)
val secondaryScope by rememberStateOfItems(secondaryItems)
ModalNavigationDrawer(
drawerContent = {
ModalDrawerSheet(
drawerContainerColor = navigationSuiteColors.navigationDrawerContainerColor,
drawerContentColor = navigationSuiteColors.navigationDrawerContentColor,
) {
DrawerContent(
drawerHeader = drawerHeader,
showPrimaryItems = layoutType != NavigationSuiteType.NavigationBar,
scope = scope,
secondaryScope = secondaryScope,
footerScope = footerScope,
onItemClicked = {
coroutineScope.launch {
drawerState.close()
}
},
)
}
},
gesturesEnabled = layoutType != NavigationSuiteType.NavigationDrawer && drawerGesturesEnabled,
drawerState = drawerState,
) {
Row {
AnimatedVisibility(layoutType == NavigationSuiteType.NavigationRail) {
NavigationRail(
header = railHeader,
containerColor = navigationSuiteColors.navigationRailContainerColor,
contentColor = navigationSuiteColors.navigationRailContentColor,
) {
scope.itemList.forEach {
NavigationRailItem(
modifier = it.modifier,
selected = it.selected,
onClick = it.onClick,
icon = { NavigationItemIcon(icon = it.icon, badge = it.badge) },
enabled = it.enabled,
label = it.label,
alwaysShowLabel = it.alwaysShowLabel,
colors =
it.colors?.navigationRailItemColors
?: NavigationRailItemDefaults.colors(),
interactionSource = it.interactionSource,
)
}
if (!LocalSpatialCapabilities.current.isSpatialUiEnabled) {
Spacer(modifier = Modifier.weight(1f))
}
footerScope.itemList.forEach {
NavigationRailItem(
modifier = it.modifier,
selected = it.selected,
onClick = it.onClick,
icon = { NavigationItemIcon(icon = it.icon, badge = it.badge) },
enabled = it.enabled,
label = it.label,
alwaysShowLabel = it.alwaysShowLabel,
colors =
it.colors?.navigationRailItemColors
?: NavigationRailItemDefaults.colors(),
interactionSource = it.interactionSource,
)
}
}
}
AnimatedVisibility(layoutType == NavigationSuiteType.NavigationDrawer) {
PermanentDrawerSheet(
modifier =
Modifier
.width(240.dp)
.padding(horizontal = 12.dp),
drawerContainerColor = navigationSuiteColors.navigationDrawerContainerColor,
drawerContentColor = navigationSuiteColors.navigationDrawerContentColor,
) {
DrawerContent(
drawerHeader = drawerHeader,
showPrimaryItems = true,
scope = scope,
secondaryScope = secondaryScope,
footerScope = footerScope,
onItemClicked = {},
)
}
}
Box {
CompositionLocalProvider(
LocalBottomBarHeight provides
if (layoutType == NavigationSuiteType.NavigationBar) {
bottomBarHeight
} else {
0.dp
},
) {
content.invoke()
}
androidx.compose.animation.AnimatedVisibility(
layoutType == NavigationSuiteType.NavigationBar,
enter = slideInVertically { it },
exit = slideOutVertically { it },
modifier =
Modifier
.align(Alignment.BottomCenter)
.offset { IntOffset(x = 0, y = -bottomBarOffsetHeightPx.roundToInt()) },
) {
Surface(
// color = navigationSuiteColors.navigationBarContainerColor,
contentColor = navigationSuiteColors.navigationBarContentColor,
) {
Box {
if (bottomBarDividerEnabled) {
HorizontalDivider(
modifier =
Modifier
.align(Alignment.TopCenter)
.fillMaxWidth(),
color = FlareDividerDefaults.color,
thickness = FlareDividerDefaults.thickness,
)
}
Row(
modifier =
Modifier
.fillMaxWidth()
.windowInsetsPadding(
WindowInsets.systemBars.only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom,
),
),
verticalAlignment = Alignment.CenterVertically,
) {
scope.itemList.forEach {
Box(
modifier =
it.modifier
.weight(1f)
.combinedClickable(
interactionSource = it.interactionSource,
onClick = it.onClick,
indication = LocalIndication.current,
onLongClick = it.onLongClick,
).height(bottomBarHeight),
contentAlignment = Alignment.Center,
) {
val colors =
it.colors?.navigationBarItemColors ?: NavigationBarItemDefaults.colors()
val color =
with(colors) {
when {
!it.enabled -> disabledIconColor
it.selected -> MaterialTheme.colorScheme.primary
else -> unselectedIconColor
}
}
val iconColor by animateColorAsState(
targetValue = color,
animationSpec = tween(100),
)
CompositionLocalProvider(LocalContentColor provides iconColor) {
NavigationItemIcon(icon = it.icon, badge = it.badge)
}
}
}
}
}
}
}
}
}
}
}
}
}
}
@Composable
private fun OptionalSubspace(content: @Composable () -> Unit) {
val session = LocalSession.current
if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
androidx.xr.compose.spatial.Subspace {
SpatialPanel(
SubspaceModifier
.width(1280.dp)
.height(800.dp)
.resizable()
.movable(),
) {
content.invoke()
Orbiter(
position = OrbiterEdge.Top,
offset = EdgeOffset.inner(offset = 20.dp),
alignment = Alignment.End,
shape = SpatialRoundedCornerShape(CornerSize(28.dp)),
) {
FilledTonalIconButton(
onClick = {
session?.requestHomeSpaceMode()
},
modifier = Modifier.size(56.dp),
) {
FAIcon(
FontAwesomeIcons.Solid.DownLeftAndUpRightToCenter,
contentDescription = null,
)
}
}
}
}
} else {
content.invoke()
}
}
@Composable
private fun ColumnScope.DrawerContent(
drawerHeader: @Composable (ColumnScope.() -> Unit)?,
onItemClicked: () -> Unit,
showPrimaryItems: Boolean,
scope: NavigationSuiteItemProvider,
secondaryScope: NavigationSuiteItemProvider,
footerScope: NavigationSuiteItemProvider,
) {
drawerHeader?.invoke(this)
if (showPrimaryItems) {
scope.itemList.forEach {
NavigationDrawerItem(
modifier = it.modifier,
selected = it.selected,
onClick = {
it.onClick()
onItemClicked()
},
icon = it.icon,
badge = it.badge,
label = { it.label?.invoke() ?: Text("") },
colors =
it.colors?.navigationDrawerItemColors
?: NavigationDrawerItemDefaults.colors(),
interactionSource = it.interactionSource,
)
}
}
if (secondaryScope.itemList.isNotEmpty()) {
HorizontalDivider()
}
secondaryScope.itemList.forEach {
NavigationDrawerItem(
modifier = it.modifier,
selected = it.selected,
onClick = {
it.onClick()
onItemClicked()
},
icon = it.icon,
badge = it.badge,
label = { it.label?.invoke() ?: Text("") },
colors =
it.colors?.navigationDrawerItemColors
?: NavigationDrawerItemDefaults.colors(),
interactionSource = it.interactionSource,
)
}
Spacer(modifier = Modifier.weight(1f))
footerScope.itemList.forEach {
NavigationDrawerItem(
modifier = it.modifier,
selected = it.selected,
onClick = {
it.onClick()
onItemClicked()
},
icon = it.icon,
badge = it.badge,
label = { it.label?.invoke() ?: Text("") },
colors =
it.colors?.navigationDrawerItemColors
?: NavigationDrawerItemDefaults.colors(),
interactionSource = it.interactionSource,
)
}
}
private interface NavigationSuiteItemProvider {
val itemsCount: Int
val itemList: MutableVector<NavigationSuiteItem>
}
@OptIn(ExperimentalMaterial3AdaptiveNavigationSuiteApi::class)
private class NavigationSuiteItem(
val selected: Boolean,
val onClick: () -> Unit,
val icon: @Composable () -> Unit,
val modifier: Modifier,
val enabled: Boolean,
val label: @Composable (() -> Unit)?,
val alwaysShowLabel: Boolean,
val badge: (@Composable () -> Unit)?,
val colors: NavigationSuiteItemColors?,
val interactionSource: MutableInteractionSource?,
val onLongClick: (() -> Unit)? = null,
)
/** The scope associated with the [NavigationSuiteScope]. */
@ExperimentalMaterial3AdaptiveNavigationSuiteApi
interface NavigationSuiteScope2 {
/**
* This function sets the parameters of the default Material navigation item to be used with the
* Navigation Suite Scaffold. The item is called in [NavigationSuite], according to the
* current [NavigationSuiteType].
*
* For specifics about each item component, see [NavigationBarItem], [NavigationRailItem], and
* [NavigationDrawerItem].
*
* @param selected whether this item is selected
* @param onClick called when this item is clicked
* @param icon icon for this item, typically an [Icon]
* @param modifier the [Modifier] to be applied to this item
* @param enabled controls the enabled state of this item. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services. Note: as of now, for [NavigationDrawerItem], this is always `true`.
* @param label the text label for this item
* @param alwaysShowLabel whether to always show the label for this item. If `false`, the label will
* only be shown when this item is selected. Note: for [NavigationDrawerItem] this is always `true`
* @param badge optional badge to show on this item
* @param colors [NavigationSuiteItemColors] that will be used to resolve the colors used for this
* item in different states.
* @param interactionSource an optional hoisted [MutableInteractionSource] for observing and
* emitting [Interaction]s for this item. You can use this to change the item's appearance
* or preview the item in different states. Note that if `null` is provided, interactions will
* still happen internally.
*/
fun item(
selected: Boolean,
onClick: () -> Unit,
icon: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
label: @Composable (() -> Unit)? = null,
alwaysShowLabel: Boolean = true,
badge: (@Composable () -> Unit)? = null,
colors: NavigationSuiteItemColors? = null,
interactionSource: MutableInteractionSource? = null,
onLongClick: (() -> Unit)? = null,
)
}
@OptIn(ExperimentalMaterial3AdaptiveNavigationSuiteApi::class)
private class NavigationSuiteScopeImpl :
NavigationSuiteScope2,
NavigationSuiteItemProvider {
override fun item(
selected: Boolean,
onClick: () -> Unit,
icon: @Composable () -> Unit,
modifier: Modifier,
enabled: Boolean,
label: @Composable (() -> Unit)?,
alwaysShowLabel: Boolean,
badge: (@Composable () -> Unit)?,
colors: NavigationSuiteItemColors?,
interactionSource: MutableInteractionSource?,
onLongClick: (() -> Unit)?,
) {
itemList.add(
NavigationSuiteItem(
selected = selected,
onClick = onClick,
icon = icon,
modifier = modifier,
enabled = enabled,
label = label,
alwaysShowLabel = alwaysShowLabel,
badge = badge,
colors = colors,
interactionSource = interactionSource,
onLongClick = onLongClick,
),
)
}
override val itemList: MutableVector<NavigationSuiteItem> = mutableVectorOf()
override val itemsCount: Int
get() = itemList.size
}
@OptIn(ExperimentalMaterial3AdaptiveNavigationSuiteApi::class)
@Composable
private fun rememberStateOfItems(content: NavigationSuiteScope2.() -> Unit): State<NavigationSuiteItemProvider> {
val latestContent = rememberUpdatedState(content)
return remember {
derivedStateOf { NavigationSuiteScopeImpl().apply(latestContent.value) }
}
}
@Composable
private fun NavigationItemIcon(
icon: @Composable () -> Unit,
badge: (@Composable () -> Unit)? = null,
) {
val iconContent = remember(icon) { movableContentOf(icon) }
if (badge != null) {
BadgedBox(badge = { badge.invoke() }) {
iconContent.invoke()
}
} else {
iconContent.invoke()
}
}