diff --git a/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/ZoomBarComponents.kt b/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/ZoomBarComponents.kt index 42590e4793..8d1aecf062 100644 --- a/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/ZoomBarComponents.kt +++ b/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/ZoomBarComponents.kt @@ -31,6 +31,8 @@ import androidx.compose.foundation.layout.wrapContentSize import androidx.compose.foundation.selection.selectable import androidx.compose.foundation.selection.selectableGroup import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.CornerBasedShape +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicText import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi @@ -44,7 +46,10 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.drawOutline +import androidx.compose.ui.graphics.drawscope.translate import androidx.compose.ui.graphics.lerp import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.res.stringResource @@ -61,8 +66,10 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.IntOffset +import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.times +import androidx.compose.ui.util.lerp import com.google.jetpackcamera.model.LensFacing import com.google.jetpackcamera.ui.uistate.capture.ZoomControlUiState import java.text.DecimalFormatSymbols @@ -283,6 +290,7 @@ fun ZoomBar( { currentZoomState.animatingToValue ?: currentZoomState.displayZoomRatio() } } + val indicatorShape = MaterialTheme.shapes.medium val totalAlongTrack = ZoomBarDefaults.SlotSize * zoomLevels.size + ZoomBarDefaults.Inset * 2 val totalAcrossTrack = ZoomBarDefaults.SlotSize @@ -312,7 +320,16 @@ fun ZoomBar( ) } .size(ZoomBarDefaults.SlotSize, ZoomBarDefaults.IndicatorHeight) - .background(colors.selectedIndicatorColor, MaterialTheme.shapes.medium) + .drawZoomIndicator( + color = colors.selectedIndicatorColor, + shape = indicatorShape, + orientation = orientation, + lastIndex = zoomLevels.lastIndex, + // The indicator is inset equally from the track's ends and sides, so + // its rounded ends are already concentric with the track's end caps. + endCapExtension = 0.dp, + positionProvider = { indicatorPosition } + ) ) Row( @@ -348,7 +365,16 @@ fun ZoomBar( ) } .size(ZoomBarDefaults.SlotSize, ZoomBarDefaults.IndicatorHeight) - .background(colors.selectedIndicatorColor, MaterialTheme.shapes.medium) + .drawZoomIndicator( + color = colors.selectedIndicatorColor, + shape = indicatorShape, + orientation = orientation, + lastIndex = zoomLevels.lastIndex, + // The indicator is flush with the track's sides, so on the first and + // last stops it extends to the ends of the track to fill the end caps. + endCapExtension = ZoomBarDefaults.Inset * 2, + positionProvider = { indicatorPosition } + ) ) Column( @@ -375,6 +401,62 @@ fun ZoomBar( } } +/** + * How far the indicator has moved onto an end stop that is [distance] stops away, from 0 (one or + * more stops away) to 1 (on or past the end stop). + */ +internal fun endCapFraction(distance: Float): Float = (1f - distance).coerceIn(0f, 1f) + +/** + * Draws the indicator pill behind the selected stop. + * + * As the indicator moves onto the first or last stop, the side facing the track's end cap morphs + * from the corner radius of [shape] to a fully rounded end so that it follows the curve of the end + * cap. That side also extends toward the end of the track by up to [endCapExtension], which keeps + * the gap around the end cap uniform when the indicator is closer to the track's sides than to its + * ends. With a single stop, both sides are rounded. + */ +private fun Modifier.drawZoomIndicator( + color: Color, + shape: CornerBasedShape, + orientation: Orientation, + lastIndex: Int, + endCapExtension: Dp, + positionProvider: () -> Float +): Modifier = drawBehind { + val position = positionProvider() + val startFraction = endCapFraction(distance = position) + val endFraction = endCapFraction(distance = lastIndex - position) + val isHorizontal = orientation == Orientation.Horizontal + val cornerRadius = shape.topStart.toPx(size, this) + val capRadius = (if (isHorizontal) size.height else size.width) / 2f + val startRadius = lerp(cornerRadius, capRadius, startFraction) + val endRadius = lerp(cornerRadius, capRadius, endFraction) + val startExtension = endCapExtension.toPx() * startFraction + val endExtension = endCapExtension.toPx() * endFraction + // In a right-to-left row, the first stop is on the right. + val leadingExtension = + if (isHorizontal && layoutDirection == LayoutDirection.Rtl) endExtension else startExtension + // Corner radii are in the order topStart, topEnd, bottomEnd, bottomStart. + val pillShape = if (isHorizontal) { + RoundedCornerShape(startRadius, endRadius, endRadius, startRadius) + } else { + RoundedCornerShape(startRadius, startRadius, endRadius, endRadius) + } + val extension = startExtension + endExtension + val pillSize = if (isHorizontal) { + size.copy(width = size.width + extension) + } else { + size.copy(height = size.height + extension) + } + translate( + left = if (isHorizontal) -leadingExtension else 0f, + top = if (isHorizontal) 0f else -leadingExtension + ) { + drawOutline(pillShape.createOutline(pillSize, layoutDirection, this), color) + } +} + @Composable private fun ZoomBarItem( value: Float, @@ -518,9 +600,17 @@ private fun getZoomButtonTestTag(buttonValue: Float): String { // Previews // --------------------------------------------------------------------------- -@Preview(name = "Horizontal 60% Black (Overlapping Viewfinder)", showBackground = true) +/** + * Renders a [ZoomBar] on a dark background, at [zoomRatio] with the given [zoomLevels], for + * previews and screenshot tests. + */ @Composable -private fun ZoomBarHorizontalDarkPreview() { +internal fun ZoomBarPreview( + zoomRatio: Float, + orientation: Orientation = Orientation.Horizontal, + zoomLevels: List = listOf(0.5f, 1.0f, 2.0f, 5.0f), + colors: ZoomBarColors = ZoomBarDefaults.black60Colors() +) { Box( modifier = Modifier .background(Color(0xFF2B2B2B)) @@ -529,123 +619,73 @@ private fun ZoomBarHorizontalDarkPreview() { ) { ZoomBar( zoomControlUiState = ZoomControlUiState.Enabled( - zoomLevels = listOf(0.5f, 1.0f, 2.0f, 5.0f), + zoomLevels = zoomLevels, primaryLensFacing = LensFacing.BACK, - primaryZoomRatio = 1.0f + primaryZoomRatio = zoomRatio ), onChangeZoom = {}, - orientation = Orientation.Horizontal, - colors = ZoomBarDefaults.black60Colors() + orientation = orientation, + colors = colors ) } } +@Preview(name = "Horizontal 60% Black (Overlapping Viewfinder)", showBackground = true) +@Composable +private fun ZoomBarHorizontalDarkPreview() { + ZoomBarPreview(zoomRatio = 1.0f) +} + @Preview(name = "Horizontal 20% White (Not Overlapping Viewfinder)", showBackground = true) @Composable private fun ZoomBarHorizontalTranslucentPreview() { - Box( - modifier = Modifier - .background(Color(0xFF2B2B2B)) - .padding(16.dp), - contentAlignment = Alignment.Center - ) { - ZoomBar( - zoomControlUiState = ZoomControlUiState.Enabled( - zoomLevels = listOf(0.5f, 1.0f, 2.0f, 5.0f), - primaryLensFacing = LensFacing.BACK, - primaryZoomRatio = 1.0f - ), - onChangeZoom = {}, - orientation = Orientation.Horizontal, - colors = ZoomBarDefaults.white20Colors() - ) - } + ZoomBarPreview(zoomRatio = 1.0f, colors = ZoomBarDefaults.white20Colors()) } @Preview(name = "Horizontal - Fraction (.5x Selected)", showBackground = true) @Composable private fun ZoomBarHorizontalFractionPreview() { - Box( - modifier = Modifier - .background(Color(0xFF2B2B2B)) - .padding(16.dp), - contentAlignment = Alignment.Center - ) { - ZoomBar( - zoomControlUiState = ZoomControlUiState.Enabled( - zoomLevels = listOf(0.5f, 1.0f, 2.0f, 5.0f), - primaryLensFacing = LensFacing.BACK, - primaryZoomRatio = 0.5f - ), - onChangeZoom = {}, - orientation = Orientation.Horizontal, - colors = ZoomBarDefaults.black60Colors() - ) - } + ZoomBarPreview(zoomRatio = 0.5f) +} + +@Preview(name = "Horizontal - Last Stop (5x Selected)", showBackground = true) +@Composable +private fun ZoomBarHorizontalLastStopPreview() { + ZoomBarPreview(zoomRatio = 5.0f) } @Preview(name = "Vertical 60% Black - Flush Sides", showBackground = true) @Composable private fun ZoomBarVerticalDarkPreview() { - Box( - modifier = Modifier - .background(Color(0xFF2B2B2B)) - .padding(16.dp), - contentAlignment = Alignment.Center - ) { - ZoomBar( - zoomControlUiState = ZoomControlUiState.Enabled( - zoomLevels = listOf(0.5f, 1.0f, 2.0f, 5.0f), - primaryLensFacing = LensFacing.BACK, - primaryZoomRatio = 1.0f - ), - onChangeZoom = {}, - orientation = Orientation.Vertical, - colors = ZoomBarDefaults.black60Colors() - ) - } + ZoomBarPreview(zoomRatio = 1.0f, orientation = Orientation.Vertical) +} + +@Preview(name = "Vertical - First Stop (.5x Selected)", showBackground = true) +@Composable +private fun ZoomBarVerticalFirstStopPreview() { + ZoomBarPreview(zoomRatio = 0.5f, orientation = Orientation.Vertical) +} + +@Preview(name = "Vertical - Last Stop (5x Selected)", showBackground = true) +@Composable +private fun ZoomBarVerticalLastStopPreview() { + ZoomBarPreview(zoomRatio = 5.0f, orientation = Orientation.Vertical) } @Preview(name = "Single Stop (1x Only)", showBackground = true) @Composable private fun ZoomBarSingleStopPreview() { - Box( - modifier = Modifier - .background(Color(0xFF2B2B2B)) - .padding(16.dp), - contentAlignment = Alignment.Center - ) { - ZoomBar( - zoomControlUiState = ZoomControlUiState.Enabled( - zoomLevels = listOf(1.0f), - primaryLensFacing = LensFacing.BACK, - primaryZoomRatio = 1.0f - ), - onChangeZoom = {}, - orientation = Orientation.Horizontal - ) - } + ZoomBarPreview(zoomRatio = 1.0f, zoomLevels = listOf(1.0f), colors = ZoomBarDefaults.colors()) } @Preview(name = "Extended Telephoto Range (2x Selected)", showBackground = true) @Composable private fun ZoomBarExtendedRangePreview() { - Box( - modifier = Modifier - .background(Color(0xFF2B2B2B)) - .padding(16.dp), - contentAlignment = Alignment.Center - ) { - ZoomBar( - zoomControlUiState = ZoomControlUiState.Enabled( - zoomLevels = listOf(0.5f, 1.0f, 2.0f, 5.0f, 10.0f), - primaryLensFacing = LensFacing.BACK, - primaryZoomRatio = 2.0f - ), - onChangeZoom = {}, - orientation = Orientation.Horizontal - ) - } + ZoomBarPreview( + zoomRatio = 2.0f, + zoomLevels = listOf(0.5f, 1.0f, 2.0f, 5.0f, 10.0f), + colors = ZoomBarDefaults.colors() + ) } @Preview(name = "Legacy Delegate - One Selected", showBackground = true) diff --git a/ui/components/capture/src/screenshotTest/kotlin/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTest.kt b/ui/components/capture/src/screenshotTest/kotlin/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTest.kt new file mode 100644 index 0000000000..0fa3b1bf7b --- /dev/null +++ b/ui/components/capture/src/screenshotTest/kotlin/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTest.kt @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.jetpackcamera.ui.components.capture + +import androidx.compose.foundation.gestures.Orientation +import androidx.compose.runtime.Composable +import androidx.compose.ui.tooling.preview.Preview +import com.android.tools.screenshot.PreviewTest + +/** Horizontal zoom bar with the first stop selected. */ +@PreviewTest +@Preview +@Composable +internal fun ZoomBarHorizontalFirstStopScreenshotPreview() { + ZoomBarPreview(zoomRatio = 0.5f) +} + +/** Horizontal zoom bar with a middle stop selected. */ +@PreviewTest +@Preview +@Composable +internal fun ZoomBarHorizontalMiddleStopScreenshotPreview() { + ZoomBarPreview(zoomRatio = 1.0f) +} + +/** Horizontal zoom bar with the last stop selected. */ +@PreviewTest +@Preview +@Composable +internal fun ZoomBarHorizontalLastStopScreenshotPreview() { + ZoomBarPreview(zoomRatio = 5.0f) +} + +/** Vertical zoom bar with the first stop selected. */ +@PreviewTest +@Preview +@Composable +internal fun ZoomBarVerticalFirstStopScreenshotPreview() { + ZoomBarPreview(zoomRatio = 0.5f, orientation = Orientation.Vertical) +} + +/** Vertical zoom bar with a middle stop selected. */ +@PreviewTest +@Preview +@Composable +internal fun ZoomBarVerticalMiddleStopScreenshotPreview() { + ZoomBarPreview(zoomRatio = 1.0f, orientation = Orientation.Vertical) +} + +/** Vertical zoom bar with the last stop selected. */ +@PreviewTest +@Preview +@Composable +internal fun ZoomBarVerticalLastStopScreenshotPreview() { + ZoomBarPreview(zoomRatio = 5.0f, orientation = Orientation.Vertical) +} + +/** Zoom bar with a single stop. */ +@PreviewTest +@Preview +@Composable +internal fun ZoomBarSingleStopScreenshotPreview() { + ZoomBarPreview(zoomRatio = 1.0f, zoomLevels = listOf(1.0f), colors = ZoomBarDefaults.colors()) +} diff --git a/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarHorizontalFirstStopScreenshotPreview_0.png b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarHorizontalFirstStopScreenshotPreview_0.png new file mode 100644 index 0000000000..2ce6df76f8 Binary files /dev/null and b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarHorizontalFirstStopScreenshotPreview_0.png differ diff --git a/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarHorizontalLastStopScreenshotPreview_0.png b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarHorizontalLastStopScreenshotPreview_0.png new file mode 100644 index 0000000000..271c994294 Binary files /dev/null and b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarHorizontalLastStopScreenshotPreview_0.png differ diff --git a/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarHorizontalMiddleStopScreenshotPreview_0.png b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarHorizontalMiddleStopScreenshotPreview_0.png new file mode 100644 index 0000000000..7111ff0c73 Binary files /dev/null and b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarHorizontalMiddleStopScreenshotPreview_0.png differ diff --git a/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarSingleStopScreenshotPreview_0.png b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarSingleStopScreenshotPreview_0.png new file mode 100644 index 0000000000..be7a68f813 Binary files /dev/null and b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarSingleStopScreenshotPreview_0.png differ diff --git a/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarVerticalFirstStopScreenshotPreview_0.png b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarVerticalFirstStopScreenshotPreview_0.png new file mode 100644 index 0000000000..b91c5fc157 Binary files /dev/null and b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarVerticalFirstStopScreenshotPreview_0.png differ diff --git a/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarVerticalLastStopScreenshotPreview_0.png b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarVerticalLastStopScreenshotPreview_0.png new file mode 100644 index 0000000000..c77929ab71 Binary files /dev/null and b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarVerticalLastStopScreenshotPreview_0.png differ diff --git a/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarVerticalMiddleStopScreenshotPreview_0.png b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarVerticalMiddleStopScreenshotPreview_0.png new file mode 100644 index 0000000000..97c07d80d2 Binary files /dev/null and b/ui/components/capture/src/screenshotTestStableDebug/reference/com/google/jetpackcamera/ui/components/capture/ZoomBarScreenshotTestKt/ZoomBarVerticalMiddleStopScreenshotPreview_0.png differ diff --git a/ui/components/capture/src/test/java/com/google/jetpackcamera/ui/components/capture/ZoomBarComponentsTest.kt b/ui/components/capture/src/test/java/com/google/jetpackcamera/ui/components/capture/ZoomBarComponentsTest.kt index 2f876d37b1..e19dcb8755 100644 --- a/ui/components/capture/src/test/java/com/google/jetpackcamera/ui/components/capture/ZoomBarComponentsTest.kt +++ b/ui/components/capture/src/test/java/com/google/jetpackcamera/ui/components/capture/ZoomBarComponentsTest.kt @@ -209,4 +209,17 @@ class ZoomBarComponentsTest { // Respects locale decimal separator assertThat(formatAccessibleZoomLabel(0.50783783f, Locale.GERMANY)).isEqualTo("0,5") } + + @Test + fun endCapFraction_scalesWithDistanceToEndStop() { + assertThat(endCapFraction(distance = 0f)).isEqualTo(1f) + assertThat(endCapFraction(distance = 0.25f)).isEqualTo(0.75f) + assertThat(endCapFraction(distance = 1f)).isEqualTo(0f) + assertThat(endCapFraction(distance = 3f)).isEqualTo(0f) + } + + @Test + fun endCapFraction_pastEndStop_staysFullyRounded() { + assertThat(endCapFraction(distance = -0.2f)).isEqualTo(1f) + } }