diff --git a/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/CaptureScaffold.kt b/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/CaptureScaffold.kt index 577837dd2..7549f04e0 100644 --- a/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/CaptureScaffold.kt +++ b/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/CaptureScaffold.kt @@ -25,6 +25,7 @@ import androidx.compose.animation.ExitTransition import androidx.compose.animation.core.tween import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut +import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.material3.MaterialTheme @@ -170,12 +171,25 @@ fun rememberCaptureScaffoldState(isInitiallyExpanded: Boolean = false): CaptureS /** * Receiver scope for [CaptureScaffold] slots. * - * Extends [PreviewOverlaySlotScope] with capture session state, Quick Settings controls, and zoom - * actions coordinated with the scaffold's [ZoomStateManager]. Properties are backed by - * `derivedStateOf` so that slots only recompose when a property they actually read changes. + * Extends [PreviewOverlaySlotScope] with container axis orientation, compact-sizing hints, capture + * session state, Quick Settings controls, and zoom actions coordinated with the scaffold's + * [ZoomStateManager]. Properties are backed by `derivedStateOf` so that slots only recompose when a + * property they actually read changes. */ @Stable sealed interface CaptureSlotScope : PreviewOverlaySlotScope { + /** + * Primary axis along which the hosting container arranges its controls (for example, + * [Orientation.Vertical] in a side rail or [Orientation.Horizontal] in a top or bottom bar). + */ + override val orientation: Orientation + + /** + * Whether the hosting container is height- or space-constrained and requests compact control + * dimensions. + */ + override val isCompact: Boolean + /** * Whether a video recording is currently active (recording or paused). */ @@ -219,6 +233,8 @@ sealed interface CaptureSlotScope : PreviewOverlaySlotScope { * Creates a [CaptureSlotScope] with static values for Compose `@Preview`s and unit tests. */ fun CaptureSlotScope( + orientation: Orientation = Orientation.Horizontal, + isCompact: Boolean = false, isOverViewfinder: Boolean = true, isRecording: Boolean = false, isQuickSettingsOpen: Boolean = false, @@ -228,6 +244,8 @@ fun CaptureSlotScope( onAnimateZoomTo: (Float, LensToZoom) -> Unit = { _, _ -> }, onSetAbsoluteZoom: (Float, LensToZoom) -> Unit = { _, _ -> } ): CaptureSlotScope = StaticCaptureSlotScope( + orientation = orientation, + isCompact = isCompact, isOverViewfinder = isOverViewfinder, isRecording = isRecording, isQuickSettingsOpen = isQuickSettingsOpen, @@ -239,6 +257,8 @@ fun CaptureSlotScope( ) private class StaticCaptureSlotScope( + override val orientation: Orientation, + override val isCompact: Boolean, override val isOverViewfinder: Boolean, override val isRecording: Boolean, override val isQuickSettingsOpen: Boolean, @@ -282,6 +302,12 @@ private class CaptureSlotScopeImpl( private val onAnimateZoomTo: (Float, LensToZoom) -> Unit, private val onSetAbsoluteZoom: (Float, LensToZoom) -> Unit ) : CaptureSlotScope { + override val orientation: Orientation + get() = overlayScope.orientation + + override val isCompact: Boolean + get() = overlayScope.isCompact + override val isOverViewfinder: Boolean get() = overlayScope.isOverViewfinder @@ -1095,22 +1121,16 @@ fun CaptureScaffold( onAnimateZoomTo, onSetAbsoluteZoom ) { - object : PreviewOverlaySlotScope { - override val isOverViewfinder: Boolean = false - override val cameraControlContainerColor: Color = - backgroundStyleFor(false).containerColor - }.let { overlayScope -> - CaptureSlotScopeImpl( - overlayScope = overlayScope, - isRecordingState = isVideoRecordingActive, - scaffoldState = state, - onToggleQuickSettings = onToggleQuickSettings, - onDismissQuickSettings = onDismissQuickSettings, - onIncrementZoom = onIncrementZoom, - onAnimateZoomTo = onAnimateZoomTo, - onSetAbsoluteZoom = onSetAbsoluteZoom - ) - } + CaptureSlotScopeImpl( + overlayScope = PreviewOverlaySlotScope(isOverViewfinder = false), + isRecordingState = isVideoRecordingActive, + scaffoldState = state, + onToggleQuickSettings = onToggleQuickSettings, + onDismissQuickSettings = onDismissQuickSettings, + onIncrementZoom = onIncrementZoom, + onAnimateZoomTo = onAnimateZoomTo, + onSetAbsoluteZoom = onSetAbsoluteZoom + ) } CaptureScaffoldLayout( diff --git a/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/CaptureSlots.kt b/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/CaptureSlots.kt new file mode 100644 index 000000000..1516d46d6 --- /dev/null +++ b/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/CaptureSlots.kt @@ -0,0 +1,524 @@ +/* + * 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.background +import androidx.compose.foundation.gestures.Orientation +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.text.BasicText +import androidx.compose.material3.SnackbarHostState +import androidx.compose.runtime.Composable +import androidx.compose.runtime.Immutable +import androidx.compose.runtime.ProvidableCompositionLocal +import androidx.compose.runtime.staticCompositionLocalOf +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Shape +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp + +/** + * Shared dimension, typography, and color tokens for [CaptureSlots] placeholder scaffolding. + */ +internal object CaptureSlotTokens { + val PillCornerRadius = 24.dp + val ImageWellCornerRadius = 12.dp + val OverlayCornerRadius = 24.dp + + val IndicatorCrossAxisSize = 48.dp + val IndicatorStandardMainAxisSize = 96.dp + val IndicatorCompactMainAxisSize = 64.dp + val HorizontalTopBarHeight = 36.dp + val HorizontalTopBarStandardWidth = 120.dp + val HorizontalTopBarCompactWidth = 96.dp + + val ZoomCrossAxisSize = 48.dp + val ZoomStandardMainAxisSize = 176.dp + val ZoomCompactMainAxisSize = 148.dp + + val IconTouchTargetSize = 48.dp + val SecondaryActionStandardSize = 52.dp + val SecondaryActionCompactSize = 48.dp + + val ShutterStandardDiameter = 86.dp + val ShutterCompactDiameter = 72.dp + + val ModeToggleStandardWidth = 96.dp + val ModeToggleCompactWidth = 72.dp + val ModeToggleStandardHeight = 56.dp + val ModeToggleCompactHeight = 48.dp + + val CarouselStandardWidth = 180.dp + val CarouselCompactWidth = 140.dp + val CarouselHeight = 32.dp + + val ElapsedTimeWidth = 72.dp + val ElapsedTimeStandardHeight = 32.dp + val ElapsedTimeCompactHeight = 28.dp + + val QuickSettingsStandardWidth = 280.dp + val QuickSettingsCompactWidth = 240.dp + val QuickSettingsStandardHeight = 220.dp + val QuickSettingsCompactHeight = 160.dp + + val ErrorDialogWidth = 280.dp + val ErrorDialogHeight = 160.dp + + val PlaceholderLabelFontSize = 11.sp + val ViewfinderLabelFontSize = 14.sp + + val DefaultControlColor = Color(0xFF3949AB) + val IndicatorColor = Color(0xFF00897B) + val ZoomColor = Color(0xFF5E35B1) + val QuickSettingsButtonColor = Color(0xFF039BE5) + val ShutterColor = Color(0xFFE53935) + val FlipCameraColor = Color(0xFF43A047) + val ImageWellColor = Color(0xFFFB8C00) + val ModeToggleColor = Color(0xFF8E24AA) + val CarouselColor = Color(0xFF546E7A) + val ElapsedTimeColor = Color(0xFFC62828) + val OverlayPlaceholderColor = Color(0xFF263238) + val ErrorDialogColor = Color(0xFFB71C1C) + val ViewfinderColor = Color(0xFF1E272C) + val PlaceholderLabelColor = Color.White +} + +/** + * Policy controlling whether spatial camera chrome containers mirror horizontally under + * right-to-left locales. + * + * Hardware-ergonomic camera chrome defaults to preserving physical left/right placement so + * controls stay anchored to the user's grip while inner text and directional icons follow the + * active locale's layout direction. + * + * @property mirrorSideRails Whether start and end side rails swap horizontal sides in RTL. + * @property mirrorCompactBottomBar Whether compact bottom-bar slots mirror horizontally in RTL. + */ +@Immutable +data class CaptureRtlPolicy( + val mirrorSideRails: Boolean = false, + val mirrorCompactBottomBar: Boolean = false +) + +/** + * CompositionLocal providing the active [CaptureRtlPolicy] for adaptive capture screen layouts. + */ +val LocalCaptureRtlPolicy: ProvidableCompositionLocal = + staticCompositionLocalOf { CaptureRtlPolicy() } + +/** + * Hosts a [CaptureSlots] composable inside an [OverlapAwareStyleProvider], binding dynamic + * overlap styling and container layout metadata ([orientation], [isCompact]) into a + * [PreviewOverlaySlotScope]. + */ +@Composable +internal fun OverlapAwareCaptureSlot( + modifier: Modifier = Modifier, + orientation: Orientation = Orientation.Horizontal, + isCompact: Boolean = false, + content: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit +) { + OverlapAwareStyleProvider( + modifier = modifier, + orientation = orientation, + isCompact = isCompact + ) { + content(Modifier) + } +} + +/** + * Slot table bundling the UI control composables placed by adaptive capture screen layouts. + * + * Each slot defaults to an empty composable so layouts and tests can populate only the controls + * relevant to a scenario while omitted controls collapse cleanly. Slots receive + * [PreviewOverlaySlotScope], which exposes live viewfinder overlap styling together with + * [PreviewOverlaySlotScope.orientation] and [PreviewOverlaySlotScope.isCompact] from the hosting + * layout container (and propagates directly into [CaptureSlotScope] when hosted by + * [CaptureScaffold]). + * + * @property topBar Status indicator bar or column content adapting to + * [PreviewOverlaySlotScope.orientation]. + * @property topBarTrailingContent Optional trailing content aligned to the end of [topBar]. + * @property zoomControls Zoom control bar or button cluster, adapting to + * [PreviewOverlaySlotScope.orientation]. + * @property quickSettingsButton Trigger button that toggles the Quick Settings surface. + * @property captureButton Primary photo/video shutter button. + * @property flipCameraButton Lens-facing toggle button. + * @property imageWell Thumbnail button opening the most recently captured media item. + * @property captureModeToggle Photo/video capture mode toggle switch. + * @property captureModeCarousel Secondary sub-mode selector carousel. + * @property elapsedTimeDisplay Active video recording duration readout. + * @property topStartContent Overlay content anchored to the top-start of the capture region. + * @property viewfinderTopEndContent Overlay content anchored to the top-end of the viewfinder. + * @property quickSettingsOverlay Expanded Quick Settings panel or sheet content. + * @property snackBar Transient message host paired with a [SnackbarHostState]. + * @property errorDialog In-hierarchy modal error dialog drawn above capture chrome. + */ +@Immutable +internal data class CaptureSlots( + val topBar: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val topBarTrailingContent: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val zoomControls: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val quickSettingsButton: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val captureButton: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val flipCameraButton: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val imageWell: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val captureModeToggle: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val captureModeCarousel: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val elapsedTimeDisplay: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val topStartContent: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val viewfinderTopEndContent: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val quickSettingsOverlay: @Composable PreviewOverlaySlotScope.(Modifier) -> Unit = {}, + val snackBar: @Composable (Modifier, SnackbarHostState) -> Unit = { _, _ -> }, + val errorDialog: @Composable (Modifier) -> Unit = {} +) + +/** + * Minimal colored placeholder box representing a single camera control in Compose previews and + * layout geometry tests. + * + * Applies [modifier] before `.size(width, height)` so hosting layouts can prepend `Modifier.sizeIn` + * constraints to clamp dimensions in condensed windows. + * + * @param label Short debug label centered inside the control box. + * @param width Unclamped target width of the placeholder control. + * @param height Unclamped target height of the placeholder control. + * @param modifier [Modifier] applied ahead of the default size modifier. + * @param color Fill color of the placeholder shape. + * @param shape Clip shape of the placeholder box. + * @param testTag Optional semantics test tag attached to the outer box. + */ +@Composable +internal fun PlaceholderControlBox( + label: String, + width: Dp, + height: Dp, + modifier: Modifier = Modifier, + color: Color = CaptureSlotTokens.DefaultControlColor, + shape: Shape = RoundedCornerShape(CaptureSlotTokens.PillCornerRadius), + testTag: String? = null +) { + val taggedModifier = if (testTag != null) { + modifier.testTag(testTag) + } else { + modifier + } + Box( + modifier = taggedModifier + .size(width = width, height = height) + .clip(shape) + .background(color), + contentAlignment = Alignment.Center + ) { + BasicText( + text = label, + style = TextStyle( + color = CaptureSlotTokens.PlaceholderLabelColor, + fontSize = CaptureSlotTokens.PlaceholderLabelFontSize, + fontWeight = FontWeight.Medium, + textAlign = TextAlign.Center + ) + ) + } +} + +/** + * Minimal tinted placeholder surface representing the camera viewfinder in layout previews and + * structural tests, tagged with [PREVIEW_DISPLAY]. + * + * @param modifier [Modifier] applied to the viewfinder box before filling its allotted bounds. + * @param label Debug label centered inside the placeholder frame. + */ +@Composable +internal fun PlaceholderViewfinder(modifier: Modifier = Modifier, label: String = "Viewfinder") { + Box( + modifier = modifier + .testTag(PREVIEW_DISPLAY) + .fillMaxSize() + .background(CaptureSlotTokens.ViewfinderColor), + contentAlignment = Alignment.Center + ) { + BasicText( + text = label, + style = TextStyle( + color = CaptureSlotTokens.PlaceholderLabelColor, + fontSize = CaptureSlotTokens.ViewfinderLabelFontSize, + fontWeight = FontWeight.Medium, + textAlign = TextAlign.Center + ) + ) + } +} + +/** + * Factory of colored [PlaceholderControlBox]-backed [CaptureSlots] configurations for Compose + * previews and structural layout tests. + */ +internal object PlaceholderCaptureSlots { + /** + * Returns the default placeholder [CaptureSlots] set, delegating to [sideRail]. + * + * @param isCompactHeight Whether placeholder controls default to compact-height dimensions. + */ + fun default(isCompactHeight: Boolean = false): CaptureSlots = + sideRail(isCompactHeight = isCompactHeight) + + /** + * Builds placeholder [CaptureSlots] recycling existing capture test tags for side-rail and + * adaptive layout verification. + * + * @param isCompactHeight Whether controls default to compact-height dimensions even when the + * hosting [PreviewOverlaySlotScope.isCompact] flag is `false`. + * @param includeImageWell Whether to populate [CaptureSlots.imageWell] or leave it empty. + * @param includeElapsedTime Whether to populate [CaptureSlots.elapsedTimeDisplay] or leave it + * empty. + * @param tagQuickSettingsOverlay Whether the inner [CaptureSlots.quickSettingsOverlay] carries + * [QUICK_SETTINGS_BOTTOM_SHEET]; defaults to `false` so outer Quick Settings card containers + * can own that tag without duplicate semantics nodes. + * @param includeErrorDialog Whether to populate [CaptureSlots.errorDialog] with a placeholder + * tagged with [CAMERA_ERROR_DIALOG_TAG]. + */ + fun sideRail( + isCompactHeight: Boolean = false, + includeImageWell: Boolean = true, + includeElapsedTime: Boolean = false, + tagQuickSettingsOverlay: Boolean = false, + includeErrorDialog: Boolean = false + ): CaptureSlots = CaptureSlots( + topBar = { slotModifier -> + val compact = isCompactHeight || isCompact + if (orientation == Orientation.Vertical) { + PlaceholderControlBox( + label = "Ind", + width = CaptureSlotTokens.IndicatorCrossAxisSize, + height = if (compact) { + CaptureSlotTokens.IndicatorCompactMainAxisSize + } else { + CaptureSlotTokens.IndicatorStandardMainAxisSize + }, + modifier = slotModifier, + color = CaptureSlotTokens.IndicatorColor, + testTag = VIDEO_QUALITY_TAG + ) + } else { + PlaceholderControlBox( + label = "Ind", + width = if (compact) { + CaptureSlotTokens.HorizontalTopBarCompactWidth + } else { + CaptureSlotTokens.HorizontalTopBarStandardWidth + }, + height = CaptureSlotTokens.HorizontalTopBarHeight, + modifier = slotModifier, + color = CaptureSlotTokens.IndicatorColor, + testTag = VIDEO_QUALITY_TAG + ) + } + }, + zoomControls = { slotModifier -> + val compact = isCompactHeight || isCompact + val mainAxisSize = if (compact) { + CaptureSlotTokens.ZoomCompactMainAxisSize + } else { + CaptureSlotTokens.ZoomStandardMainAxisSize + } + val width = if (orientation == Orientation.Vertical) { + CaptureSlotTokens.ZoomCrossAxisSize + } else { + mainAxisSize + } + val height = if (orientation == Orientation.Vertical) { + mainAxisSize + } else { + CaptureSlotTokens.ZoomCrossAxisSize + } + PlaceholderControlBox( + label = "Zoom", + width = width, + height = height, + modifier = slotModifier, + color = CaptureSlotTokens.ZoomColor, + testTag = ZOOM_BUTTON_ROW_TAG + ) + }, + quickSettingsButton = { slotModifier -> + PlaceholderControlBox( + label = "QS", + width = CaptureSlotTokens.IconTouchTargetSize, + height = CaptureSlotTokens.IconTouchTargetSize, + modifier = slotModifier, + color = CaptureSlotTokens.QuickSettingsButtonColor, + shape = CircleShape, + testTag = QUICK_SETTINGS_DROP_DOWN + ) + }, + captureButton = { slotModifier -> + val compact = isCompactHeight || isCompact + val diameter = if (compact) { + CaptureSlotTokens.ShutterCompactDiameter + } else { + CaptureSlotTokens.ShutterStandardDiameter + } + PlaceholderControlBox( + label = "Cap", + width = diameter, + height = diameter, + modifier = slotModifier, + color = CaptureSlotTokens.ShutterColor, + shape = CircleShape, + testTag = CAPTURE_BUTTON + ) + }, + flipCameraButton = { slotModifier -> + val compact = isCompactHeight || isCompact + val size = if (compact) { + CaptureSlotTokens.SecondaryActionCompactSize + } else { + CaptureSlotTokens.SecondaryActionStandardSize + } + PlaceholderControlBox( + label = "Flip", + width = size, + height = size, + modifier = slotModifier, + color = CaptureSlotTokens.FlipCameraColor, + shape = CircleShape, + testTag = FLIP_CAMERA_BUTTON + ) + }, + imageWell = if (includeImageWell) { + { slotModifier -> + val compact = isCompactHeight || isCompact + val size = if (compact) { + CaptureSlotTokens.SecondaryActionCompactSize + } else { + CaptureSlotTokens.SecondaryActionStandardSize + } + PlaceholderControlBox( + label = "Well", + width = size, + height = size, + modifier = slotModifier, + color = CaptureSlotTokens.ImageWellColor, + shape = RoundedCornerShape(CaptureSlotTokens.ImageWellCornerRadius), + testTag = IMAGE_WELL_TAG + ) + } + } else { + {} + }, + captureModeToggle = { slotModifier -> + val compact = isCompactHeight || isCompact + PlaceholderControlBox( + label = "Mode", + width = if (compact) { + CaptureSlotTokens.ModeToggleCompactWidth + } else { + CaptureSlotTokens.ModeToggleStandardWidth + }, + height = if (compact) { + CaptureSlotTokens.ModeToggleCompactHeight + } else { + CaptureSlotTokens.ModeToggleStandardHeight + }, + modifier = slotModifier, + color = CaptureSlotTokens.ModeToggleColor, + testTag = CAPTURE_MODE_TOGGLE_BUTTON + ) + }, + captureModeCarousel = { slotModifier -> + val compact = isCompactHeight || isCompact + PlaceholderControlBox( + label = "Modes", + width = if (compact) { + CaptureSlotTokens.CarouselCompactWidth + } else { + CaptureSlotTokens.CarouselStandardWidth + }, + height = CaptureSlotTokens.CarouselHeight, + modifier = slotModifier, + color = CaptureSlotTokens.CarouselColor, + testTag = CAPTURE_MODE_CAROUSEL + ) + }, + elapsedTimeDisplay = if (includeElapsedTime) { + { slotModifier -> + val compact = isCompactHeight || isCompact + PlaceholderControlBox( + label = "00:12", + width = CaptureSlotTokens.ElapsedTimeWidth, + height = if (compact) { + CaptureSlotTokens.ElapsedTimeCompactHeight + } else { + CaptureSlotTokens.ElapsedTimeStandardHeight + }, + modifier = slotModifier, + color = CaptureSlotTokens.ElapsedTimeColor, + testTag = ELAPSED_TIME_TAG + ) + } + } else { + {} + }, + quickSettingsOverlay = { slotModifier -> + val compact = isCompactHeight || isCompact + PlaceholderControlBox( + label = "Quick Settings", + width = if (compact) { + CaptureSlotTokens.QuickSettingsCompactWidth + } else { + CaptureSlotTokens.QuickSettingsStandardWidth + }, + height = if (compact) { + CaptureSlotTokens.QuickSettingsCompactHeight + } else { + CaptureSlotTokens.QuickSettingsStandardHeight + }, + modifier = slotModifier, + color = CaptureSlotTokens.OverlayPlaceholderColor, + shape = RoundedCornerShape(CaptureSlotTokens.OverlayCornerRadius), + testTag = if (tagQuickSettingsOverlay) QUICK_SETTINGS_BOTTOM_SHEET else null + ) + }, + errorDialog = if (includeErrorDialog) { + { dialogModifier -> + PlaceholderControlBox( + label = "Camera Error", + width = CaptureSlotTokens.ErrorDialogWidth, + height = CaptureSlotTokens.ErrorDialogHeight, + modifier = dialogModifier, + color = CaptureSlotTokens.ErrorDialogColor, + shape = RoundedCornerShape(CaptureSlotTokens.OverlayCornerRadius), + testTag = CAMERA_ERROR_DIALOG_TAG + ) + } + } else { + {} + } + ) +} diff --git a/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/OverlapAwareStyleProvider.kt b/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/OverlapAwareStyleProvider.kt index 0a4cab812..22559ebe1 100644 --- a/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/OverlapAwareStyleProvider.kt +++ b/ui/components/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/OverlapAwareStyleProvider.kt @@ -15,6 +15,7 @@ */ package com.google.jetpackcamera.ui.components.capture +import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.layout.Box import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider @@ -36,7 +37,9 @@ import androidx.compose.ui.layout.onGloballyPositioned * viewfinder or over the background around it. * * Content can use [isOverViewfinder] to choose its own colors, or use - * [cameraControlContainerColor] to match the built-in camera controls. + * [cameraControlContainerColor] to match the built-in camera controls. Adaptive containers such as + * side rails and condensed panes also expose [orientation] and [isCompact] so controls can adapt + * their axis and dimensions to the hosting slot. */ @Stable interface PreviewOverlaySlotScope { @@ -53,10 +56,49 @@ interface PreviewOverlaySlotScope { * it. Using this color is optional. */ val cameraControlContainerColor: Color + + /** + * Primary axis along which the hosting container arranges its controls (for example, + * [Orientation.Vertical] in a side rail or [Orientation.Horizontal] in a top or bottom bar). + */ + val orientation: Orientation + get() = Orientation.Horizontal + + /** + * Whether the hosting container is height- or space-constrained and requests compact control + * dimensions. + */ + val isCompact: Boolean + get() = false +} + +/** + * Creates a standalone [PreviewOverlaySlotScope] backed by fixed values for non-overlapping + * surfaces, Compose previews, and unit tests. + */ +internal fun PreviewOverlaySlotScope( + isOverViewfinder: Boolean = true, + orientation: Orientation = Orientation.Horizontal, + isCompact: Boolean = false +): PreviewOverlaySlotScope = StaticPreviewOverlaySlotScope( + isOverViewfinder = isOverViewfinder, + orientation = orientation, + isCompact = isCompact +) + +private class StaticPreviewOverlaySlotScope( + override val isOverViewfinder: Boolean, + override val orientation: Orientation, + override val isCompact: Boolean +) : PreviewOverlaySlotScope { + override val cameraControlContainerColor: Color + get() = backgroundStyleFor(isOverViewfinder).containerColor } private class PreviewOverlaySlotScopeImpl( - private val isOverViewfinderState: State + private val isOverViewfinderState: State, + override val orientation: Orientation = Orientation.Horizontal, + override val isCompact: Boolean = false ) : PreviewOverlaySlotScope { override val isOverViewfinder: Boolean get() = isOverViewfinderState.value @@ -76,17 +118,23 @@ internal fun backgroundStyleFor(isOverlapping: Boolean): CameraControlBackground * Applies [LocalCameraControlBackgroundStyle] based on whether this * component's spatial bounds overlap with the [LocalOverlapTargetBounds]. * - * The [content] also receives the overlap result through [PreviewOverlaySlotScope], so that - * content supplied by callers outside this module can adapt its own styling. + * The [content] also receives the overlap result and container layout hints through + * [PreviewOverlaySlotScope], so that content supplied by callers outside this module can adapt its + * own styling and axis orientation. * * @param modifier The [Modifier] to be applied to the outer layout. * @param overlapThreshold The ratio of overlap (between 0.0 and 1.0) required to switch the style. - * @param content The composable content that will receive the computed [LocalCameraControlBackgroundStyle]. + * @param orientation Primary axis along which the hosting container arranges its controls. + * @param isCompact Whether compact control sizing is requested by the hosting container. + * @param content The composable content that will receive the computed + * [LocalCameraControlBackgroundStyle]. */ @Composable internal fun OverlapAwareStyleProvider( modifier: Modifier = Modifier, overlapThreshold: Float = 0.5f, + orientation: Orientation = Orientation.Horizontal, + isCompact: Boolean = false, content: @Composable PreviewOverlaySlotScope.() -> Unit ) { val targetBoundsState = LocalOverlapTargetBounds.current @@ -100,7 +148,13 @@ internal fun OverlapAwareStyleProvider( myArea > 0f && (intersectArea / myArea) >= overlapThreshold } } - val scope = remember(isOverlappingState) { PreviewOverlaySlotScopeImpl(isOverlappingState) } + val scope = remember(isOverlappingState, orientation, isCompact) { + PreviewOverlaySlotScopeImpl( + isOverViewfinderState = isOverlappingState, + orientation = orientation, + isCompact = isCompact + ) + } val style = backgroundStyleFor(isOverlappingState.value) Box( diff --git a/ui/components/capture/src/test/java/com/google/jetpackcamera/ui/components/capture/CaptureSlotsTest.kt b/ui/components/capture/src/test/java/com/google/jetpackcamera/ui/components/capture/CaptureSlotsTest.kt new file mode 100644 index 000000000..0dce9b346 --- /dev/null +++ b/ui/components/capture/src/test/java/com/google/jetpackcamera/ui/components/capture/CaptureSlotsTest.kt @@ -0,0 +1,154 @@ +/* + * 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.foundation.layout.Box +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.sizeIn +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.getBoundsInRoot +import androidx.compose.ui.test.junit4.v2.createComposeRule +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.height +import com.google.common.truth.Truth.assertThat +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class CaptureSlotsTest { + + @get:Rule + val composeTestRule = createComposeRule() + + @Test + fun captureRtlPolicy_defaultsToUnmirrored_andPropagatesThroughCompositionLocal() { + var defaultPolicy: CaptureRtlPolicy? = null + var overriddenPolicy: CaptureRtlPolicy? = null + + composeTestRule.setContent { + defaultPolicy = LocalCaptureRtlPolicy.current + CompositionLocalProvider( + LocalCaptureRtlPolicy provides CaptureRtlPolicy( + mirrorSideRails = true, + mirrorCompactBottomBar = true + ) + ) { + overriddenPolicy = LocalCaptureRtlPolicy.current + } + } + + assertThat(defaultPolicy).isEqualTo( + CaptureRtlPolicy(mirrorSideRails = false, mirrorCompactBottomBar = false) + ) + assertThat(overriddenPolicy).isEqualTo( + CaptureRtlPolicy(mirrorSideRails = true, mirrorCompactBottomBar = true) + ) + } + + @Test + fun captureSlotScope_exposesMetadata_overlapContainerColors_andDismissCallback() { + var dismissedCount = 0 + val overViewfinderScope = CaptureSlotScope( + orientation = Orientation.Vertical, + isCompact = true, + isOverViewfinder = true, + isRecording = true, + onDismissQuickSettings = { dismissedCount++ } + ) + val offViewfinderScope = PreviewOverlaySlotScope( + isOverViewfinder = false, + orientation = Orientation.Horizontal, + isCompact = false + ) + + assertThat(overViewfinderScope.orientation).isEqualTo(Orientation.Vertical) + assertThat(overViewfinderScope.isCompact).isTrue() + assertThat(overViewfinderScope.isOverViewfinder).isTrue() + assertThat(overViewfinderScope.isRecording).isTrue() + assertThat(overViewfinderScope.cameraControlContainerColor).isEqualTo( + CameraControlBackgroundStyle.BLACK_60.containerColor + ) + + overViewfinderScope.dismissQuickSettings() + assertThat(dismissedCount).isEqualTo(1) + + assertThat(offViewfinderScope.orientation).isEqualTo(Orientation.Horizontal) + assertThat(offViewfinderScope.isCompact).isFalse() + assertThat(offViewfinderScope.isOverViewfinder).isFalse() + assertThat(offViewfinderScope.cameraControlContainerColor).isEqualTo( + CameraControlBackgroundStyle.WHITE_20.containerColor + ) + } + + @Test + fun captureSlots_andPlaceholders_adaptAxisAndRecycleExistingTags() { + val customSlots = CaptureSlots( + topBar = { modifier -> + Box(modifier.size(48.dp).testTag(VIDEO_QUALITY_TAG)) + }, + topStartContent = { modifier -> + Box(modifier.size(24.dp).testTag(FLIP_CAMERA_BUTTON)) + } + ) + val defaultPlaceholders = PlaceholderCaptureSlots.default() + val taggedOverlayPlaceholders = PlaceholderCaptureSlots.sideRail( + tagQuickSettingsOverlay = true + ) + val slotScope = CaptureSlotScope(orientation = Orientation.Vertical) + var useTaggedOverlay by mutableStateOf(false) + + composeTestRule.setContent { + Box(modifier = Modifier.size(400.dp)) { + PlaceholderViewfinder() + with(slotScope) { + customSlots.topBar(this, Modifier) + customSlots.topStartContent(this, Modifier) + defaultPlaceholders.zoomControls( + this, + Modifier.sizeIn(maxHeight = 40.dp) + ) + if (useTaggedOverlay) { + taggedOverlayPlaceholders.quickSettingsOverlay(this, Modifier) + } else { + defaultPlaceholders.quickSettingsOverlay(this, Modifier) + } + } + } + } + + composeTestRule.onNodeWithTag(PREVIEW_DISPLAY).assertIsDisplayed() + composeTestRule.onNodeWithTag(VIDEO_QUALITY_TAG).assertIsDisplayed() + composeTestRule.onNodeWithTag(FLIP_CAMERA_BUTTON).assertIsDisplayed() + composeTestRule.onNodeWithTag(QUICK_SETTINGS_BOTTOM_SHEET).assertDoesNotExist() + + val clampedZoomBounds = composeTestRule.onNodeWithTag(ZOOM_BUTTON_ROW_TAG).getBoundsInRoot() + assertThat(clampedZoomBounds.height).isEqualTo(40.dp) + + useTaggedOverlay = true + composeTestRule.waitForIdle() + composeTestRule.onNodeWithTag(QUICK_SETTINGS_BOTTOM_SHEET).assertIsDisplayed() + } +}