@@ -2,61 +2,38 @@ package me.lvca.alba.app.ui.designsystem
22
33import androidx.compose.animation.EnterTransition
44import androidx.compose.animation.ExitTransition
5- import androidx.compose.animation.core.MutableTransitionState
6- import androidx.compose.animation.core.animateFloat
7- import androidx.compose.animation.core.rememberTransition
8- import androidx.compose.animation.core.snap
95import androidx.compose.animation.fadeIn
106import androidx.compose.animation.fadeOut
117import androidx.compose.animation.slideInVertically
128import androidx.compose.animation.slideOutVertically
13- import androidx.compose.foundation.clickable
149import androidx.compose.foundation.layout.Arrangement
1510import androidx.compose.foundation.layout.Box
16- import androidx.compose.foundation.layout.Column
1711import androidx.compose.foundation.layout.ColumnScope
18- import androidx.compose.foundation.layout.IntrinsicSize
12+ import androidx.compose.foundation.layout.PaddingValues
1913import androidx.compose.foundation.layout.Row
2014import androidx.compose.foundation.layout.RowScope
21- import androidx.compose.foundation.layout.fillMaxWidth
22- import androidx.compose.foundation.layout.height
23- import androidx.compose.foundation.layout.heightIn
2415import androidx.compose.foundation.layout.padding
2516import androidx.compose.foundation.layout.size
26- import androidx.compose.foundation.layout.width
2717import androidx.compose.foundation.layout.widthIn
28- import androidx.compose.foundation.rememberScrollState
29- import androidx.compose.foundation.shape.CircleShape
30- import androidx.compose.foundation.verticalScroll
18+ import androidx.compose.material3.DropdownMenu
19+ import androidx.compose.material3.DropdownMenuItem
20+ import androidx.compose.material3.FloatingToolbarDefaults
3121import androidx.compose.material3.HorizontalDivider
32- import androidx.compose.material3.LocalContentColor
22+ import androidx.compose.material3.HorizontalFloatingToolbar
3323import androidx.compose.material3.MaterialTheme
34- import androidx.compose.material3.Surface
3524import androidx.compose.material3.Text
3625import androidx.compose.runtime.Composable
37- import androidx.compose.runtime.CompositionLocalProvider
38- import androidx.compose.runtime.getValue
39- import androidx.compose.runtime.mutableStateOf
40- import androidx.compose.runtime.remember
41- import androidx.compose.runtime.setValue
4226import androidx.compose.ui.Alignment
4327import androidx.compose.ui.Modifier
44- import androidx.compose.ui.graphics.TransformOrigin
45- import androidx.compose.ui.graphics.graphicsLayer
46- import androidx.compose.ui.platform.LocalDensity
28+ import androidx.compose.ui.graphics.Color
29+ import androidx.compose.ui.input.pointer.pointerInput
30+ import androidx.compose.ui.semantics.isTraversalGroup
31+ import androidx.compose.ui.semantics.semantics
4732import androidx.compose.ui.text.style.TextOverflow
48- import androidx.compose.ui.unit.IntOffset
49- import androidx.compose.ui.unit.IntRect
50- import androidx.compose.ui.unit.IntSize
51- import androidx.compose.ui.unit.LayoutDirection
5233import androidx.compose.ui.unit.dp
53- import androidx.compose.ui.window.Popup
54- import androidx.compose.ui.window.PopupPositionProvider
55- import androidx.compose.ui.window.PopupProperties
5634import me.lvca.alba.app.ui.designsystem.theme.AlbaIconSize
5735import me.lvca.alba.app.ui.designsystem.theme.AlbaTheme
5836import me.lvca.alba.app.ui.designsystem.theme.floating
59- import me.lvca.alba.app.ui.viewer.reduceGalleryMotion
6037
6138/* *
6239 * Measures of Alba's floating surfaces: the M3 Expressive floating toolbar and the floating menu, matched to
@@ -73,15 +50,11 @@ internal object AlbaFloatingDefaults {
7350
7451 val MenuMinWidth = 227 .dp
7552 val MenuMaxWidth = 280 .dp
76- val MenuItemHeight = 48 .dp
7753 val MenuItemPadding = 16 .dp
7854 val MenuIconSize = AlbaIconSize .standard
7955 val MenuGroupPadding = 8 .dp
8056 val MenuShadow = 3 .dp
8157
82- /* * The anchor's gap to the window edge the menu keeps. */
83- val MenuWindowMargin = 8 .dp
84-
8558 /* * Floating bars rise in on the theme's fast spatial spring and fade on its fast effects spring; reduced motion snaps. */
8659 @Composable
8760 fun enter (reducedMotion : Boolean ): EnterTransition = if (reducedMotion) EnterTransition .None else
@@ -93,28 +66,33 @@ internal object AlbaFloatingDefaults {
9366}
9467
9568/* *
96- * The M3 Expressive floating toolbar: a stadium in the neutral floating colours with a soft shadow, 64dp tall, holding icon
97- * buttons. Content defaults to [contentColor]; callers lay out their own items in the row.
69+ * Material's [HorizontalFloatingToolbar] in Alba's neutral floating colours: a 64dp stadium with a soft shadow, holding
70+ * icon buttons [ToolbarItemSpacing][AlbaFloatingDefaults.ToolbarItemSpacing] apart. Content defaults to [contentColor].
71+ * Like a surface, the bar takes the touches that land between its buttons rather than passing them to the grid below.
9872 */
9973@Composable
10074internal fun AlbaFloatingToolbar (
10175 modifier : Modifier = Modifier ,
102- contentColor : androidx.compose.ui.graphics. Color = AlbaTheme .floating.onToolbar,
76+ contentColor : Color = AlbaTheme .floating.onToolbar,
10377 content : @Composable RowScope .() -> Unit ,
10478) {
105- Surface (modifier, shape = CircleShape , color = AlbaTheme .floating.toolbar, contentColor = contentColor,
106- shadowElevation = AlbaFloatingDefaults .ToolbarShadow ) {
107- Row (Modifier .height(AlbaFloatingDefaults .ToolbarHeight ).padding(horizontal = AlbaFloatingDefaults .ToolbarPadding ),
108- horizontalArrangement = Arrangement .spacedBy(AlbaFloatingDefaults .ToolbarItemSpacing ),
79+ HorizontalFloatingToolbar (expanded = true ,
80+ modifier = modifier.semantics { isTraversalGroup = true }.pointerInput(Unit ) {},
81+ colors = FloatingToolbarDefaults .standardFloatingToolbarColors(toolbarContainerColor = AlbaTheme .floating.toolbar,
82+ toolbarContentColor = contentColor),
83+ contentPadding = PaddingValues (horizontal = AlbaFloatingDefaults .ToolbarPadding ),
84+ expandedShadowElevation = AlbaFloatingDefaults .ToolbarShadow ,
85+ collapsedShadowElevation = AlbaFloatingDefaults .ToolbarShadow ) {
86+ Row (horizontalArrangement = Arrangement .spacedBy(AlbaFloatingDefaults .ToolbarItemSpacing ),
10987 verticalAlignment = Alignment .CenterVertically , content = content)
11088 }
11189}
11290
11391/* *
114- * Alba's one floating menu, used for every overflow. It opens from its anchor (the layout it sits in, as with
115- * Material's DropdownMenu): below it, or above when there is no room, aligned to the anchor's end. It grows from the
116- * anchor on a fast spatial spring, so reopening mid-close picks up where it is , and snaps when motion is reduced.
117- * Group items with [AlbaMenuDivider].
92+ * Alba's one floating menu, used for every overflow: Material's [DropdownMenu] in Alba's menu shape and container
93+ * colour, at least as wide as Photos' menu. It opens from its anchor ( the layout it sits in), below it or above when
94+ * there is no room, dismisses on an outside tap or back , and grows on the theme's fast spatial spring, which the
95+ * system's animation scale turns off when motion is reduced. Group items with [AlbaMenuDivider].
11896 */
11997@Composable
12098internal fun AlbaMenu (
@@ -123,36 +101,15 @@ internal fun AlbaMenu(
123101 modifier : Modifier = Modifier ,
124102 content : @Composable ColumnScope .() -> Unit ,
125103) {
126- val visibility = remember { MutableTransitionState (false ) }
127- visibility.targetState = expanded
128- if (! visibility.currentState && ! visibility.targetState) return
129- val margin = with (LocalDensity .current) { AlbaFloatingDefaults .MenuWindowMargin .roundToPx() }
130- var origin by remember { mutableStateOf(TransformOrigin .Center ) }
131- val position = remember(margin) { AnchoredMenuPosition (margin) { origin = it } }
132- Popup (popupPositionProvider = position, onDismissRequest = onDismissRequest, properties = PopupProperties (focusable = true )) {
133- val reduce = reduceGalleryMotion()
134- val transition = rememberTransition(visibility, label = " alba-menu" )
135- val motion = MaterialTheme .motionScheme
136- val scale by transition.animateFloat({ if (reduce) snap() else motion.fastSpatialSpec() }, label = " menu-scale" ) {
137- if (it) 1f else MENU_CLOSED_SCALE
138- }
139- val alpha by transition.animateFloat({ if (reduce) snap() else motion.fastEffectsSpec() }, label = " menu-alpha" ) {
140- if (it) 1f else 0f
141- }
142- Surface (Modifier .padding(AlbaFloatingDefaults .MenuShadow ).graphicsLayer {
143- scaleX = scale; scaleY = scale; this .alpha = alpha; transformOrigin = origin
144- }, shape = MaterialTheme .shapes.medium, color = MaterialTheme .colorScheme.surfaceContainer,
145- contentColor = MaterialTheme .colorScheme.onSurface, shadowElevation = AlbaFloatingDefaults .MenuShadow ) {
146- Column (modifier.width(IntrinsicSize .Max )
147- .widthIn(min = AlbaFloatingDefaults .MenuMinWidth , max = AlbaFloatingDefaults .MenuMaxWidth )
148- .verticalScroll(rememberScrollState()).padding(vertical = AlbaFloatingDefaults .MenuGroupPadding ), content = content)
149- }
150- }
104+ DropdownMenu (expanded = expanded, onDismissRequest = onDismissRequest,
105+ modifier = modifier.widthIn(min = AlbaFloatingDefaults .MenuMinWidth , max = AlbaFloatingDefaults .MenuMaxWidth ),
106+ shape = MaterialTheme .shapes.medium, containerColor = MaterialTheme .colorScheme.surfaceContainer,
107+ tonalElevation = 0 .dp, shadowElevation = AlbaFloatingDefaults .MenuShadow , content = content)
151108}
152109
153110/* *
154- * One menu row: a 24dp [leadingIcon], the [text] in body large and an optional [trailingIcon], 48dp tall.
155- * Disabled rows keep their place at Material's disabled opacity .
111+ * One menu row: Material's [DropdownMenuItem] with a 24dp [leadingIcon], the [text] in body large and an optional
112+ * [trailingIcon]. Disabled rows keep their place in Material's disabled colours .
156113 */
157114@Composable
158115internal fun AlbaMenuItem (
@@ -163,50 +120,20 @@ internal fun AlbaMenuItem(
163120 leadingIcon : (@Composable () -> Unit )? = null,
164121 trailingIcon : (@Composable () -> Unit )? = null,
165122) {
166- val colors = MaterialTheme .colorScheme
167- val disabledAlpha = .38f
168- Row (modifier.fillMaxWidth().heightIn(min = AlbaFloatingDefaults .MenuItemHeight ).clickable(enabled = enabled, onClick = onClick)
169- .padding(horizontal = AlbaFloatingDefaults .MenuItemPadding ),
170- horizontalArrangement = Arrangement .spacedBy(AlbaFloatingDefaults .MenuItemPadding ), verticalAlignment = Alignment .CenterVertically ) {
171- val icons = if (enabled) colors.onSurfaceVariant else colors.onSurface.copy(alpha = disabledAlpha)
172- if (leadingIcon != null ) CompositionLocalProvider (LocalContentColor provides icons) {
173- Box (Modifier .size(AlbaFloatingDefaults .MenuIconSize ), contentAlignment = Alignment .Center ) { leadingIcon() }
174- }
175- Text (text, Modifier .weight(1f ), color = if (enabled) colors.onSurface else colors.onSurface.copy(alpha = disabledAlpha),
176- style = MaterialTheme .typography.bodyLarge, maxLines = 2 , overflow = TextOverflow .Ellipsis )
177- if (trailingIcon != null ) CompositionLocalProvider (LocalContentColor provides icons) {
178- Box (Modifier .size(AlbaFloatingDefaults .MenuIconSize ), contentAlignment = Alignment .Center ) { trailingIcon() }
179- }
180- }
123+ DropdownMenuItem (text = { Text (text, style = MaterialTheme .typography.bodyLarge, maxLines = 2 , overflow = TextOverflow .Ellipsis ) },
124+ onClick = onClick, modifier = modifier, enabled = enabled,
125+ leadingIcon = leadingIcon?.let { icon -> { MenuIcon (icon) } },
126+ trailingIcon = trailingIcon?.let { icon -> { MenuIcon (icon) } },
127+ contentPadding = PaddingValues (horizontal = AlbaFloatingDefaults .MenuItemPadding ))
128+ }
129+
130+ @Composable
131+ private fun MenuIcon (icon : @Composable () -> Unit ) {
132+ Box (Modifier .size(AlbaFloatingDefaults .MenuIconSize ), contentAlignment = Alignment .Center ) { icon() }
181133}
182134
183135/* * Separates groups of related items, as Photos' menus do. */
184136@Composable
185137internal fun AlbaMenuDivider () {
186138 HorizontalDivider (Modifier .padding(vertical = AlbaFloatingDefaults .MenuGroupPadding ), color = MaterialTheme .colorScheme.outlineVariant)
187139}
188-
189- /* * A closed menu is this much smaller, growing from its anchor. */
190- private const val MENU_CLOSED_SCALE = .8f
191-
192- /* * Below the anchor, else above it, aligned to its end and kept [margin] inside the window. */
193- private class AnchoredMenuPosition (private val margin : Int , private val onOrigin : (TransformOrigin ) -> Unit ) : PopupPositionProvider {
194- override fun calculatePosition (anchorBounds : IntRect , windowSize : IntSize , layoutDirection : LayoutDirection ,
195- popupContentSize : IntSize ): IntOffset {
196- val width = popupContentSize.width
197- val height = popupContentSize.height
198- val endAligned = if (layoutDirection == LayoutDirection .Ltr ) anchorBounds.right - width else anchorBounds.left
199- val x = endAligned.coerceIn(margin, (windowSize.width - width - margin).coerceAtLeast(margin))
200- val below = anchorBounds.bottom
201- val above = anchorBounds.top - height
202- val y = when {
203- below + height <= windowSize.height - margin -> below
204- above >= margin -> above
205- else -> (windowSize.height - height - margin).coerceAtLeast(margin)
206- }
207- val pivotX = if (width > 0 ) ((anchorBounds.center.x - x).toFloat() / width).coerceIn(0f , 1f ) else TransformOrigin .Center .pivotFractionX
208- val pivotY = if (y >= anchorBounds.top) 0f else 1f
209- onOrigin(TransformOrigin (pivotX, pivotY))
210- return IntOffset (x, y)
211- }
212- }
0 commit comments