Skip to content

Commit 5f5440b

Browse files
thisislvcaclaudealba-goldens-bot[bot]
authored
refactor(ui): build the floating toolbar and menus on stock Material components (#110)
* test(screenshots): capture the open selection and viewer menus Records the selection toolbar's More menu and the viewer's overflow menu, standard and dark, before they move to Material's DropdownMenu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vGQ3zr1LSQtH3D545V8mU * refactor(ui): build the floating toolbar and menus on stock Material components AlbaFloatingToolbar now wraps HorizontalFloatingToolbar, and AlbaMenu and AlbaMenuItem wrap DropdownMenu and DropdownMenuItem, instead of redrawing them with a Surface, a hand-positioned Popup and clickable rows (docs/design.md: wrap stock menus and floating toolbars, don't redraw them). Alba's colours, shape, shadow, widths and paddings pass through the components' parameters; the APIs and every testTag stay the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vGQ3zr1LSQtH3D545V8mU * test: update screenshot goldens Golden-New: dark/gallery-selection-more.png Golden-New: dark/viewer-more.png Golden-New: standard/gallery-selection-more.png Golden-New: standard/viewer-more.png * test: update screenshot goldens Golden-Changed: compact/gallery-selection.png Golden-Changed: dark/gallery-selection-more.png Golden-Changed: dark/gallery-selection.png Golden-Changed: dark/viewer-more.png Golden-Changed: standard/gallery-selection-more.png Golden-Changed: standard/gallery-selection.png Golden-Changed: standard/viewer-more.png --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: alba-goldens-bot[bot] <348500388+alba-goldens-bot[bot]@users.noreply.github.com>
1 parent a8f661c commit 5f5440b

9 files changed

Lines changed: 61 additions & 116 deletions

File tree

‎app/src/main/java/me/lvca/alba/app/ui/designsystem/AlbaFloatingDefaults.kt‎

Lines changed: 41 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,38 @@ package me.lvca.alba.app.ui.designsystem
22

33
import androidx.compose.animation.EnterTransition
44
import 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
95
import androidx.compose.animation.fadeIn
106
import androidx.compose.animation.fadeOut
117
import androidx.compose.animation.slideInVertically
128
import androidx.compose.animation.slideOutVertically
13-
import androidx.compose.foundation.clickable
149
import androidx.compose.foundation.layout.Arrangement
1510
import androidx.compose.foundation.layout.Box
16-
import androidx.compose.foundation.layout.Column
1711
import androidx.compose.foundation.layout.ColumnScope
18-
import androidx.compose.foundation.layout.IntrinsicSize
12+
import androidx.compose.foundation.layout.PaddingValues
1913
import androidx.compose.foundation.layout.Row
2014
import 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
2415
import androidx.compose.foundation.layout.padding
2516
import androidx.compose.foundation.layout.size
26-
import androidx.compose.foundation.layout.width
2717
import 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
3121
import androidx.compose.material3.HorizontalDivider
32-
import androidx.compose.material3.LocalContentColor
22+
import androidx.compose.material3.HorizontalFloatingToolbar
3323
import androidx.compose.material3.MaterialTheme
34-
import androidx.compose.material3.Surface
3524
import androidx.compose.material3.Text
3625
import 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
4226
import androidx.compose.ui.Alignment
4327
import 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
4732
import 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
5233
import 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
5634
import me.lvca.alba.app.ui.designsystem.theme.AlbaIconSize
5735
import me.lvca.alba.app.ui.designsystem.theme.AlbaTheme
5836
import 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
10074
internal 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
12098
internal 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
158115
internal 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
185137
internal 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-
}

‎app/src/test/java/me/lvca/alba/app/screenshots/AppScreenshots.kt‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ abstract class AppScreenshots(
8181
capture("gallery-selection")
8282
}
8383

84+
@Test fun gallerySelectionMore() {
85+
state("gallery-selection-more")
86+
val gallery = ScreenshotFixtures.gallery()
87+
show(gallery.copy(selection = gallery.items.take(3).map { it.id }.toSet()))
88+
compose.onNodeWithTag("selection-more").performClick()
89+
compose.onNodeWithTag("selection-more-menu").assertExists()
90+
capture("gallery-selection-more")
91+
}
92+
8493
@Test fun galleryOffline() {
8594
state("gallery-offline")
8695
show(session = ScreenshotFixtures.offline)
@@ -123,6 +132,15 @@ abstract class AppScreenshots(
123132
capture("viewer")
124133
}
125134

135+
@Test fun viewerMore() {
136+
state("viewer-more")
137+
val gallery = ScreenshotFixtures.gallery()
138+
show(gallery.copy(selectedMedia = gallery.items[0]))
139+
compose.onNodeWithTag("viewer-more").performClick()
140+
compose.onNodeWithTag("viewer-more-menu").assertExists()
141+
capture("viewer-more")
142+
}
143+
126144
@Test fun viewerDetails() {
127145
state("viewer-details")
128146
val gallery = ScreenshotFixtures.gallery()
@@ -221,8 +239,8 @@ class WideScreenshots : AppScreenshots("wide",
221239

222240
@Config(qualifiers = STANDARD)
223241
class DarkScreenshots : AppScreenshots("dark", darkTheme = true,
224-
states = setOf("gallery", "gallery-selection", "viewer", "filter-sheet", "profile", "sign-in", "sign-in-wrong-password",
225-
"sign-in-signing-in", "sign-in-code", "sign-in-verifying"))
242+
states = setOf("gallery", "gallery-selection", "gallery-selection-more", "viewer", "viewer-more", "filter-sheet", "profile",
243+
"sign-in", "sign-in-wrong-password", "sign-in-signing-in", "sign-in-code", "sign-in-verifying"))
226244

227245
/** The largest common accessibility text size short of the system maximum. */
228246
@Config(qualifiers = STANDARD)
32 Bytes
Loading
60.5 KB
Loading
27 Bytes
Loading
35.7 KB
Loading
60.8 KB
Loading
26 Bytes
Loading
37.7 KB
Loading

0 commit comments

Comments
 (0)