Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,38 @@ package me.lvca.alba.app.ui.designsystem

import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.rememberTransition
import androidx.compose.animation.core.snap
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.FloatingToolbarDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.HorizontalFloatingToolbar
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.semantics.isTraversalGroup
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupPositionProvider
import androidx.compose.ui.window.PopupProperties
import me.lvca.alba.app.ui.designsystem.theme.AlbaIconSize
import me.lvca.alba.app.ui.designsystem.theme.AlbaTheme
import me.lvca.alba.app.ui.designsystem.theme.floating
import me.lvca.alba.app.ui.viewer.reduceGalleryMotion

/**
* Measures of Alba's floating surfaces: the M3 Expressive floating toolbar and the floating menu, matched to
Expand All @@ -73,15 +50,11 @@ internal object AlbaFloatingDefaults {

val MenuMinWidth = 227.dp
val MenuMaxWidth = 280.dp
val MenuItemHeight = 48.dp
val MenuItemPadding = 16.dp
val MenuIconSize = AlbaIconSize.standard
val MenuGroupPadding = 8.dp
val MenuShadow = 3.dp

/** The anchor's gap to the window edge the menu keeps. */
val MenuWindowMargin = 8.dp

/** Floating bars rise in on the theme's fast spatial spring and fade on its fast effects spring; reduced motion snaps. */
@Composable
fun enter(reducedMotion: Boolean): EnterTransition = if (reducedMotion) EnterTransition.None else
Expand All @@ -93,28 +66,33 @@ internal object AlbaFloatingDefaults {
}

/**
* The M3 Expressive floating toolbar: a stadium in the neutral floating colours with a soft shadow, 64dp tall, holding icon
* buttons. Content defaults to [contentColor]; callers lay out their own items in the row.
* Material's [HorizontalFloatingToolbar] in Alba's neutral floating colours: a 64dp stadium with a soft shadow, holding
* icon buttons [ToolbarItemSpacing][AlbaFloatingDefaults.ToolbarItemSpacing] apart. Content defaults to [contentColor].
* Like a surface, the bar takes the touches that land between its buttons rather than passing them to the grid below.
*/
@Composable
internal fun AlbaFloatingToolbar(
modifier: Modifier = Modifier,
contentColor: androidx.compose.ui.graphics.Color = AlbaTheme.floating.onToolbar,
contentColor: Color = AlbaTheme.floating.onToolbar,
content: @Composable RowScope.() -> Unit,
) {
Surface(modifier, shape = CircleShape, color = AlbaTheme.floating.toolbar, contentColor = contentColor,
shadowElevation = AlbaFloatingDefaults.ToolbarShadow) {
Row(Modifier.height(AlbaFloatingDefaults.ToolbarHeight).padding(horizontal = AlbaFloatingDefaults.ToolbarPadding),
horizontalArrangement = Arrangement.spacedBy(AlbaFloatingDefaults.ToolbarItemSpacing),
HorizontalFloatingToolbar(expanded = true,
modifier = modifier.semantics { isTraversalGroup = true }.pointerInput(Unit) {},
colors = FloatingToolbarDefaults.standardFloatingToolbarColors(toolbarContainerColor = AlbaTheme.floating.toolbar,
toolbarContentColor = contentColor),
contentPadding = PaddingValues(horizontal = AlbaFloatingDefaults.ToolbarPadding),
expandedShadowElevation = AlbaFloatingDefaults.ToolbarShadow,
collapsedShadowElevation = AlbaFloatingDefaults.ToolbarShadow) {
Row(horizontalArrangement = Arrangement.spacedBy(AlbaFloatingDefaults.ToolbarItemSpacing),
verticalAlignment = Alignment.CenterVertically, content = content)
}
}

/**
* Alba's one floating menu, used for every overflow. It opens from its anchor (the layout it sits in, as with
* Material's DropdownMenu): below it, or above when there is no room, aligned to the anchor's end. It grows from the
* anchor on a fast spatial spring, so reopening mid-close picks up where it is, and snaps when motion is reduced.
* Group items with [AlbaMenuDivider].
* Alba's one floating menu, used for every overflow: Material's [DropdownMenu] in Alba's menu shape and container
* colour, at least as wide as Photos' menu. It opens from its anchor (the layout it sits in), below it or above when
* there is no room, dismisses on an outside tap or back, and grows on the theme's fast spatial spring, which the
* system's animation scale turns off when motion is reduced. Group items with [AlbaMenuDivider].
*/
@Composable
internal fun AlbaMenu(
Expand All @@ -123,36 +101,15 @@ internal fun AlbaMenu(
modifier: Modifier = Modifier,
content: @Composable ColumnScope.() -> Unit,
) {
val visibility = remember { MutableTransitionState(false) }
visibility.targetState = expanded
if (!visibility.currentState && !visibility.targetState) return
val margin = with(LocalDensity.current) { AlbaFloatingDefaults.MenuWindowMargin.roundToPx() }
var origin by remember { mutableStateOf(TransformOrigin.Center) }
val position = remember(margin) { AnchoredMenuPosition(margin) { origin = it } }
Popup(popupPositionProvider = position, onDismissRequest = onDismissRequest, properties = PopupProperties(focusable = true)) {
val reduce = reduceGalleryMotion()
val transition = rememberTransition(visibility, label = "alba-menu")
val motion = MaterialTheme.motionScheme
val scale by transition.animateFloat({ if (reduce) snap() else motion.fastSpatialSpec() }, label = "menu-scale") {
if (it) 1f else MENU_CLOSED_SCALE
}
val alpha by transition.animateFloat({ if (reduce) snap() else motion.fastEffectsSpec() }, label = "menu-alpha") {
if (it) 1f else 0f
}
Surface(Modifier.padding(AlbaFloatingDefaults.MenuShadow).graphicsLayer {
scaleX = scale; scaleY = scale; this.alpha = alpha; transformOrigin = origin
}, shape = MaterialTheme.shapes.medium, color = MaterialTheme.colorScheme.surfaceContainer,
contentColor = MaterialTheme.colorScheme.onSurface, shadowElevation = AlbaFloatingDefaults.MenuShadow) {
Column(modifier.width(IntrinsicSize.Max)
.widthIn(min = AlbaFloatingDefaults.MenuMinWidth, max = AlbaFloatingDefaults.MenuMaxWidth)
.verticalScroll(rememberScrollState()).padding(vertical = AlbaFloatingDefaults.MenuGroupPadding), content = content)
}
}
DropdownMenu(expanded = expanded, onDismissRequest = onDismissRequest,
modifier = modifier.widthIn(min = AlbaFloatingDefaults.MenuMinWidth, max = AlbaFloatingDefaults.MenuMaxWidth),
shape = MaterialTheme.shapes.medium, containerColor = MaterialTheme.colorScheme.surfaceContainer,
tonalElevation = 0.dp, shadowElevation = AlbaFloatingDefaults.MenuShadow, content = content)
}

/**
* One menu row: a 24dp [leadingIcon], the [text] in body large and an optional [trailingIcon], 48dp tall.
* Disabled rows keep their place at Material's disabled opacity.
* One menu row: Material's [DropdownMenuItem] with a 24dp [leadingIcon], the [text] in body large and an optional
* [trailingIcon]. Disabled rows keep their place in Material's disabled colours.
*/
@Composable
internal fun AlbaMenuItem(
Expand All @@ -163,50 +120,20 @@ internal fun AlbaMenuItem(
leadingIcon: (@Composable () -> Unit)? = null,
trailingIcon: (@Composable () -> Unit)? = null,
) {
val colors = MaterialTheme.colorScheme
val disabledAlpha = .38f
Row(modifier.fillMaxWidth().heightIn(min = AlbaFloatingDefaults.MenuItemHeight).clickable(enabled = enabled, onClick = onClick)
.padding(horizontal = AlbaFloatingDefaults.MenuItemPadding),
horizontalArrangement = Arrangement.spacedBy(AlbaFloatingDefaults.MenuItemPadding), verticalAlignment = Alignment.CenterVertically) {
val icons = if (enabled) colors.onSurfaceVariant else colors.onSurface.copy(alpha = disabledAlpha)
if (leadingIcon != null) CompositionLocalProvider(LocalContentColor provides icons) {
Box(Modifier.size(AlbaFloatingDefaults.MenuIconSize), contentAlignment = Alignment.Center) { leadingIcon() }
}
Text(text, Modifier.weight(1f), color = if (enabled) colors.onSurface else colors.onSurface.copy(alpha = disabledAlpha),
style = MaterialTheme.typography.bodyLarge, maxLines = 2, overflow = TextOverflow.Ellipsis)
if (trailingIcon != null) CompositionLocalProvider(LocalContentColor provides icons) {
Box(Modifier.size(AlbaFloatingDefaults.MenuIconSize), contentAlignment = Alignment.Center) { trailingIcon() }
}
}
DropdownMenuItem(text = { Text(text, style = MaterialTheme.typography.bodyLarge, maxLines = 2, overflow = TextOverflow.Ellipsis) },
onClick = onClick, modifier = modifier, enabled = enabled,
leadingIcon = leadingIcon?.let { icon -> { MenuIcon(icon) } },
trailingIcon = trailingIcon?.let { icon -> { MenuIcon(icon) } },
contentPadding = PaddingValues(horizontal = AlbaFloatingDefaults.MenuItemPadding))
}

@Composable
private fun MenuIcon(icon: @Composable () -> Unit) {
Box(Modifier.size(AlbaFloatingDefaults.MenuIconSize), contentAlignment = Alignment.Center) { icon() }
}

/** Separates groups of related items, as Photos' menus do. */
@Composable
internal fun AlbaMenuDivider() {
HorizontalDivider(Modifier.padding(vertical = AlbaFloatingDefaults.MenuGroupPadding), color = MaterialTheme.colorScheme.outlineVariant)
}

/** A closed menu is this much smaller, growing from its anchor. */
private const val MENU_CLOSED_SCALE = .8f

/** Below the anchor, else above it, aligned to its end and kept [margin] inside the window. */
private class AnchoredMenuPosition(private val margin: Int, private val onOrigin: (TransformOrigin) -> Unit) : PopupPositionProvider {
override fun calculatePosition(anchorBounds: IntRect, windowSize: IntSize, layoutDirection: LayoutDirection,
popupContentSize: IntSize): IntOffset {
val width = popupContentSize.width
val height = popupContentSize.height
val endAligned = if (layoutDirection == LayoutDirection.Ltr) anchorBounds.right - width else anchorBounds.left
val x = endAligned.coerceIn(margin, (windowSize.width - width - margin).coerceAtLeast(margin))
val below = anchorBounds.bottom
val above = anchorBounds.top - height
val y = when {
below + height <= windowSize.height - margin -> below
above >= margin -> above
else -> (windowSize.height - height - margin).coerceAtLeast(margin)
}
val pivotX = if (width > 0) ((anchorBounds.center.x - x).toFloat() / width).coerceIn(0f, 1f) else TransformOrigin.Center.pivotFractionX
val pivotY = if (y >= anchorBounds.top) 0f else 1f
onOrigin(TransformOrigin(pivotX, pivotY))
return IntOffset(x, y)
}
}
22 changes: 20 additions & 2 deletions app/src/test/java/me/lvca/alba/app/screenshots/AppScreenshots.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ abstract class AppScreenshots(
capture("gallery-selection")
}

@Test fun gallerySelectionMore() {
state("gallery-selection-more")
val gallery = ScreenshotFixtures.gallery()
show(gallery.copy(selection = gallery.items.take(3).map { it.id }.toSet()))
compose.onNodeWithTag("selection-more").performClick()
compose.onNodeWithTag("selection-more-menu").assertExists()
capture("gallery-selection-more")
}

@Test fun galleryOffline() {
state("gallery-offline")
show(session = ScreenshotFixtures.offline)
Expand Down Expand Up @@ -123,6 +132,15 @@ abstract class AppScreenshots(
capture("viewer")
}

@Test fun viewerMore() {
state("viewer-more")
val gallery = ScreenshotFixtures.gallery()
show(gallery.copy(selectedMedia = gallery.items[0]))
compose.onNodeWithTag("viewer-more").performClick()
compose.onNodeWithTag("viewer-more-menu").assertExists()
capture("viewer-more")
}

@Test fun viewerDetails() {
state("viewer-details")
val gallery = ScreenshotFixtures.gallery()
Expand Down Expand Up @@ -221,8 +239,8 @@ class WideScreenshots : AppScreenshots("wide",

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

/** The largest common accessibility text size short of the system maximum. */
@Config(qualifiers = STANDARD)
Expand Down
Binary file modified app/src/test/screenshots/compact/gallery-selection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/test/screenshots/dark/gallery-selection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/test/screenshots/dark/viewer-more.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/test/screenshots/standard/gallery-selection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/test/screenshots/standard/viewer-more.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.