diff --git a/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt b/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt index 8f1357df..3971c5f6 100644 --- a/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt +++ b/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt @@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.width import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.Audiotrack import androidx.compose.material.icons.rounded.CameraAlt +import androidx.compose.material.icons.rounded.Videocam import androidx.compose.material.icons.rounded.Image import androidx.compose.material.icons.rounded.LocationOn import androidx.compose.material.icons.rounded.Person @@ -679,6 +680,7 @@ private fun AttachTrayPreview() { cameraIcon = Icons.Rounded.CameraAlt, actions = listOf( AttachAction("Gallery", Icons.Rounded.Image) {}, + AttachAction("Video", Icons.Rounded.Videocam) {}, AttachAction("Audio", Icons.Rounded.Audiotrack) {}, AttachAction("Location", Icons.Rounded.LocationOn) {}, AttachAction("Contact", Icons.Rounded.Person) {} diff --git a/app/src/main/java/com/anindra/messages/ui/AttachTray.kt b/app/src/main/java/com/anindra/messages/ui/AttachTray.kt index c9e00464..3992dce8 100644 --- a/app/src/main/java/com/anindra/messages/ui/AttachTray.kt +++ b/app/src/main/java/com/anindra/messages/ui/AttachTray.kt @@ -25,15 +25,15 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import com.anindra.messages.ui.theme.LocalReduceMotion -import com.anindra.messages.ui.theme.inputPill import com.anindra.messages.ui.theme.motionSpring -/** One action the tray offers. */ +/** One action the tray offers, beside the camera tile. */ internal data class AttachAction( val label: String, val icon: ImageVector, @@ -42,8 +42,8 @@ internal data class AttachAction( /** * A 3x2 grid of rounded tiles, each with its icon above its label, in a panel - * that takes the keyboard's place. Six actions fill the grid exactly — no - * oversized camera column and no tile left alone on a final row. + * that takes the keyboard's place. Camera, Gallery, Video, Audio, Location and + * Contact fill the six slots exactly. */ internal data class AttachTraySizes( val tile: Dp, @@ -58,37 +58,45 @@ internal data class AttachTraySizes( internal object AttachTrayMetrics { const val COLUMNS = 3 - const val GAP_DP = 8f - const val PAGE_PAD_DP = 16f - const val VERTICAL_PAD_DP = 12f const val ROWS = 2 + const val GAP_DP = 8f + const val PAGE_PAD_DP = 8f + const val VERTICAL_PAD_DP = 8f - /** A tile is a little wider than it is tall, keeping the tray short. */ + /** A tile is slightly wider than it is tall, keeping the tray short. */ const val TILE_ASPECT = 0.9f - const val ICON_LIFT = 0.07f - const val LABEL_INSET_FRACTION = 0.08f - // Material 3: a 24dp icon in a large (16dp) cornered card. - const val ICON_DP = 24f - const val CORNER_DP = 16f /** Tiles stop growing past this, so a tablet keyboard is not a wall of cards. */ const val MAX_TILE_ASPECT = 1.35f + /** Fraction of a tile's height the icon sits above its centre. */ + const val ICON_LIFT = 0.07f + + /** The label's distance from the tile's bottom edge, as a fraction of it. */ + const val LABEL_INSET_FRACTION = 0.08f + + /** Icon size and corner radius, as fractions of the tile they sit in. */ + const val ICON_FRACTION = 0.24f + const val RADIUS_FRACTION = 0.107f + fun sizes( contentWidthDp: Float, targetHeightDp: Float? = null, rows: Int = ROWS ): AttachTraySizes { + val rowCount = rows.coerceAtLeast(1) val column = (contentWidthDp - GAP_DP * (COLUMNS - 1)) / COLUMNS val tile = column.coerceAtLeast(1f) var tileHeight = tile * TILE_ASPECT - val natural = VERTICAL_PAD_DP * 2 + tileHeight * rows + GAP_DP * (rows - 1) + val natural = VERTICAL_PAD_DP * 2 + + tileHeight * rowCount + GAP_DP * (rowCount - 1) // The tray replaces the keyboard, so a taller slot grows the tiles to // fill it. The cap keeps a tablet keyboard from turning them into // portraits; the panel still fills the slot below them. val target = targetHeightDp ?: natural if (target > natural) { - val roomForRow = (target - VERTICAL_PAD_DP * 2 - GAP_DP * (rows - 1)) / rows + val roomForRow = + (target - VERTICAL_PAD_DP * 2 - GAP_DP * (rowCount - 1)) / rowCount tileHeight = roomForRow.coerceAtMost(tile * MAX_TILE_ASPECT) } return AttachTraySizes( @@ -98,14 +106,21 @@ internal object AttachTrayMetrics { pagePad = PAGE_PAD_DP.dp, verticalPad = VERTICAL_PAD_DP.dp, rowHeight = tileHeight.dp, - height = target.coerceAtLeast( - VERTICAL_PAD_DP * 2 + tileHeight * rows + GAP_DP * (rows - 1) + height = maxOf( + target, + VERTICAL_PAD_DP * 2 + tileHeight * rowCount + GAP_DP * (rowCount - 1) ).dp ) } } -/** A grid of tiles in the chat's own column, with the conversation still on screen above. */ +/** + * The attachment tray that takes the keyboard's place. + * + * It is not a sheet: it sits at the bottom of the chat's own column, so the + * conversation above it stays on screen and undimmed while it is open. The + * tiles carry the approved design's one accent each, cycled from the scheme. + */ @Composable internal fun AttachTray( onCamera: () -> Unit, @@ -113,16 +128,22 @@ internal fun AttachTray( cameraIcon: ImageVector, actions: List, modifier: Modifier = Modifier, + /** The height the keyboard last had, so the tray takes exactly its slot. */ keyboardHeight: Dp? = null ) { val colors = MaterialTheme.colorScheme + // One accent per tile, cycled from the scheme's three so a dynamic palette + // keeps them related instead of turning the tray into a paint chart. + val accents = listOf(colors.primary, colors.tertiary, colors.secondary) val tiles = listOf(AttachAction(cameraLabel, cameraIcon, onCamera)) + actions + val rows = (tiles.size + AttachTrayMetrics.COLUMNS - 1) / AttachTrayMetrics.COLUMNS Column( modifier .fillMaxWidth() - // The page's own colour: a container role made the tray a visible - // band under the chat in the dark scheme. - .background(colors.inputPill) + // The page's own colour, which is what the composer already sits + // on: painting a container role here made the tray a visible band + // under the chat in the dark scheme. + .background(colors.background) ) { BoxWithConstraints( Modifier @@ -132,7 +153,12 @@ internal fun AttachTray( val sizes = AttachTrayMetrics.sizes( maxWidth.value, keyboardHeight?.value, - rows = (tiles.size + AttachTrayMetrics.COLUMNS - 1) / AttachTrayMetrics.COLUMNS + rows = rows + ) + android.util.Log.d( + "AttachTrayHeight", + "content=${maxWidth.value} target=${keyboardHeight?.value} " + + "tray=${sizes.height.value} tile=${sizes.tileHeight.value} rows=$rows" ) Column(Modifier.height(sizes.height)) { tiles.chunked(AttachTrayMetrics.COLUMNS) @@ -141,17 +167,25 @@ internal fun AttachTray( Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(sizes.gap) ) { - row.forEach { action -> + row.forEachIndexed { column, action -> + val index = + rowIndex * AttachTrayMetrics.COLUMNS + column AttachTile( label = action.label, icon = action.icon, width = sizes.tile, height = sizes.tileHeight, - onClick = action.onClick + onClick = action.onClick, + iconTint = accents[index % accents.size] ) } + // A final row with fewer tiles keeps their width + // rather than stretching them across the panel. + repeat(AttachTrayMetrics.COLUMNS - row.size) { + Spacer(Modifier.size(width = sizes.tile, height = sizes.tileHeight)) + } } - if (rowIndex < tiles.size / AttachTrayMetrics.COLUMNS - 1) { + if (rowIndex < rows - 1) { Spacer(Modifier.height(sizes.gap)) } } @@ -160,7 +194,7 @@ internal fun AttachTray( } } -/** One tile: a rounded surface with its icon above its label. */ +/** One tile: a Material surface with its icon above the label. */ @Composable internal fun AttachTile( label: String, @@ -168,10 +202,13 @@ internal fun AttachTile( width: Dp, height: Dp, onClick: () -> Unit, - modifier: Modifier = Modifier + modifier: Modifier = Modifier, + iconTint: Color = Color.Unspecified ) { val colors = MaterialTheme.colorScheme val reduceMotion = LocalReduceMotion.current + // A pressed tile gives a little: the ripple alone is easy to miss on a + // surface this size, and this is what makes a tap feel answered. val interaction = remember { MutableInteractionSource() } val pressed by interaction.collectIsPressedAsState() val scale by animateFloatAsState( @@ -182,6 +219,7 @@ internal fun AttachTile( ), label = "attachTilePress" ) + Surface( onClick = onClick, modifier = modifier @@ -190,8 +228,8 @@ internal fun AttachTile( scaleX = scale scaleY = scale }, - shape = RoundedCornerShape(AttachTrayMetrics.CORNER_DP.dp), - color = colors.inputPill, + shape = RoundedCornerShape(width * AttachTrayMetrics.RADIUS_FRACTION), + color = colors.surfaceContainerHighest, contentColor = colors.onSurface, interactionSource = interaction ) { @@ -199,9 +237,9 @@ internal fun AttachTile( Icon( icon, contentDescription = null, - tint = colors.onSurfaceVariant, + tint = if (iconTint == Color.Unspecified) colors.onSurface else iconTint, modifier = Modifier - .size(AttachTrayMetrics.ICON_DP.dp) + .size(minOf(width, height) * AttachTrayMetrics.ICON_FRACTION) .offset(y = -(height * AttachTrayMetrics.ICON_LIFT)) ) Text( diff --git a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt index e756826f..c38b2d09 100644 --- a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt +++ b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt @@ -1698,8 +1698,11 @@ fun ChatScreen( // without this it drops to the bottom of the window for a frame and climbs // back with the keyboard, which is the flicker this is here to stop. var keyboardSwap by remember { mutableStateOf(false) } - LaunchedEffect(imeBottom) { - if (imeBottom > 0.dp) keyboardSwap = false + LaunchedEffect(imeBottom, keyboardSlot) { + // Hold the vacated slot until the keyboard has actually arrived. Any + // positive inset is only the first frame of its animation, and clearing + // on it dropped the composer to a partial height on a tall tray. + if (imeBottom > 0.dp && imeBottom.value + 1f >= keyboardSlot) keyboardSwap = false } LaunchedEffect(keyboardSwap) { if (!keyboardSwap) return@LaunchedEffect diff --git a/app/src/test/java/com/anindra/messages/ui/AttachTrayMaterialTest.kt b/app/src/test/java/com/anindra/messages/ui/AttachTrayMaterialTest.kt index f4b29aca..42945894 100644 --- a/app/src/test/java/com/anindra/messages/ui/AttachTrayMaterialTest.kt +++ b/app/src/test/java/com/anindra/messages/ui/AttachTrayMaterialTest.kt @@ -6,9 +6,9 @@ import org.junit.Test import java.io.File /** - * The attach tray is built from Material 3 roles rather than picked colours: - * a surfaceContainer panel, cards one tonal step above it, onSurfaceVariant - * icons and labels, and the M3 large corner / 24dp icon sizes. + * The tray is built from Material 3 roles rather than picked colours: the page + * colour behind it, surfaceContainerHighest cards, onSurfaceVariant labels and + * one accent per tile from the scheme's primary/tertiary/secondary. */ class AttachTrayMaterialTest { @@ -30,18 +30,23 @@ class AttachTrayMaterialTest { @Test fun theTonalLadderIsTheMaterialOne() { - // Panel and cards both use the composer pill's colour. - assertTrue(".background(colors.inputPill)" in source) - assertTrue("color = colors.inputPill" in source) - assertTrue("tint = colors.onSurfaceVariant" in source) + assertTrue(".background(colors.background)" in source) + assertTrue("color = colors.surfaceContainerHighest" in source) assertTrue("color = colors.onSurfaceVariant" in source) + assertTrue("colors.primary" in source) + assertTrue("colors.tertiary" in source) + assertTrue("colors.secondary" in source) } @Test - fun theShapesAreTheMaterialOnes() { - assertTrue("RoundedCornerShape(AttachTrayMetrics.CORNER_DP.dp)" in source) - assertTrue(".size(AttachTrayMetrics.ICON_DP.dp)" in source) - assertTrue("const val ICON_DP = 24f" in source) - assertTrue("const val CORNER_DP = 16f" in source) + fun theShapesAndIconsScaleWithTheirTile() { + assertTrue( + "RoundedCornerShape(width * AttachTrayMetrics.RADIUS_FRACTION)" in source + ) + assertTrue( + ".size(minOf(width, height) * AttachTrayMetrics.ICON_FRACTION)" in source + ) + assertTrue("const val ICON_FRACTION = 0.24f" in source) + assertTrue("const val RADIUS_FRACTION = 0.107f" in source) } } diff --git a/app/src/test/java/com/anindra/messages/ui/AttachTrayMetricsTest.kt b/app/src/test/java/com/anindra/messages/ui/AttachTrayMetricsTest.kt index a576b997..5d43cc5c 100644 --- a/app/src/test/java/com/anindra/messages/ui/AttachTrayMetricsTest.kt +++ b/app/src/test/java/com/anindra/messages/ui/AttachTrayMetricsTest.kt @@ -6,7 +6,7 @@ import org.junit.Test /** * The attach tray is a 3x2 grid of rounded tiles, so all six actions — camera, - * gallery, video, audio, location and contact — fit without an orphan row. + * gallery, video, audio, location and contact — fill it without an orphan row. */ class AttachTrayMetricsTest { @@ -23,24 +23,25 @@ class AttachTrayMetricsTest { val sizes = AttachTrayMetrics.sizes(396f) assertEquals(8f, sizes.gap.value, epsilon) - assertEquals(16f, sizes.pagePad.value, epsilon) - assertEquals(12f, sizes.verticalPad.value, epsilon) + assertEquals(8f, sizes.pagePad.value, epsilon) + assertEquals(8f, sizes.verticalPad.value, epsilon) val tile = (396f - 8f * 2) / 3 assertEquals(tile, sizes.tile.value, epsilon) assertEquals(tile * 0.9f, sizes.tileHeight.value, epsilon) assertEquals(tile * 0.9f, sizes.rowHeight.value, epsilon) // Two rows, one gap between them and the panel padding. - assertEquals(12f * 2 + tile * 0.9f * 2 + 8f, sizes.height.value, epsilon) + assertEquals(8f * 2 + tile * 0.9f * 2 + 8f, sizes.height.value, epsilon) } @Test fun theTrayTakesTheKeyboardsSlot() { - val sizes = AttachTrayMetrics.sizes(396f, targetHeightDp = 261f) - assertEquals(261f, sizes.height.value, epsilon) + val sizes = AttachTrayMetrics.sizes(396f, targetHeightDp = 283f) + assertEquals(283f, sizes.height.value, epsilon) assertTrue("the panel is taller than one row of tiles", sizes.rowHeight < sizes.height) assertTrue( "a taller slot grows the tiles", - AttachTrayMetrics.sizes(396f, targetHeightDp = 320f).tileHeight.value > sizes.tileHeight.value + AttachTrayMetrics.sizes(396f, targetHeightDp = 320f).tileHeight.value > + sizes.tileHeight.value ) } @@ -63,13 +64,13 @@ class AttachTrayMetricsTest { } @Test - fun aNarrowScreenShrinksTheCirclesButKeepsThemTappable() { - val sizes = AttachTrayMetrics.sizes(120f) - val column = (120f - 8f * (AttachTrayMetrics.COLUMNS - 1)) / AttachTrayMetrics.COLUMNS + fun aNarrowScreenShrinksTheTilesButKeepsThemTappable() { + val sizes = AttachTrayMetrics.sizes(280f) + val column = (280f - 8f * (AttachTrayMetrics.COLUMNS - 1)) / AttachTrayMetrics.COLUMNS assertEquals(column, sizes.tile.value, epsilon) assertTrue( "even a narrow phone keeps a tappable tile", - AttachTrayMetrics.sizes(280f).tile.value >= 80f + sizes.tile.value >= 80f ) } diff --git a/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt b/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt index 6256b74d..6bc403a8 100644 --- a/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt +++ b/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt @@ -53,8 +53,8 @@ class AttachTrayWiringTest { fun theTrayOffersEveryAction() { val tray = bottomBar.substringAfter("AttachTray(") for (resource in listOf( - "chat_media_camera", "chat_media_gallery", "chat_media_audio", - "chat_media_location", "chat_media_contact" + "chat_media_camera", "chat_media_gallery", "chat_media_video", + "chat_media_audio", "chat_media_location", "chat_media_contact" )) { assertTrue("$resource must have a tile", tray.contains("R.string.$resource")) } diff --git a/scripts b/scripts index a8eb2e37..58fe845a 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit a8eb2e37fb5a82afce3b646abc861062b508e5af +Subproject commit 58fe845aeb903eef6555f883cbc0a23436d87084