Modern Apps git

← Modern-Apps.git

camera: video mode gets its own settings set, zoom included

Commit
584b88cfce2a4a2d7d90a13f2c07ab0f5f151f0e
Author
Vayun Mathur <[email protected]>
When
2026-11-21 16:00 UTC (15 days ago)
Parents
d1382d9
With the settings pane open, switching to video left a stale
photo pane on screen and the video button row offered nothing
- the pane also hid the zoom bar. Settings are now modelled
per mode: zoom and brightness/exposure apply to photo and
video (CameraX applies zoom ratio and EV to every bound use
case, live during recording); shadows/warmth stay photo-only
(still-baked), shutter/ISO photo-only (a fixed shutter fights
the frame rate). The pane renders the effective setting for
the current mode, an inapplicable stored pane falls back to
the zoom bar and is cleared so it can't resurrect, and a pane
both modes offer stays open. This file (CameraScreenEffects)
also carries the rotation agent's deviceRotationForOrientation
listener hunk.

Verified: 548-test camera suite green after integration
(incl. 5 new video-settings compose tests and a video-mode
zoom recording test).

Diff

camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenEffects.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenEffects.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenEffects.ktindex 46ac02671c..133fc884a0 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenEffects.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenEffects.kt@@ -65,13 +65,8 @@ internal fun CameraScreenEffects(     DisposableEffect(Unit) {         val listener = object : OrientationEventListener(context) {             override fun onOrientationChanged(orientation: Int) {-                if (orientation == ORIENTATION_UNKNOWN) return-                state.deviceRotation = when {-                    orientation in 45..134 -> 270-                    orientation in 135..224 -> 180-                    orientation in 225..314 -> 90-                    else -> 0-                }+                // Unknown angle keeps the last rotation (helper returns null).+                state.deviceRotation = deviceRotationForOrientation(orientation) ?: return             }         }         listener.enable()@@ -94,11 +89,17 @@ internal fun CameraScreenEffects(         viewModel.applyExposureCompensation(state.exposureComp)     } -    // Close mode-specific settings when leaving the mode that offers them.+    // Re-render (not close) the settings pane on a mode switch: a pane the+    // new mode also offers (BRIGHTNESS/ZOOM in VIDEO) stays open with its+    // correct control; a photo-only pane (SHADOWS/WARMTH/EXPOSURE_TIME/+    // ISO/PORTRAIT_BLUR) is cleared so the column falls back to the new+    // mode's lens/zoom bar instead of showing the stale photo slider, and+    // does not resurrect when switching back. CameraSettingsColumn applies+    // the same effectiveSettingForMode check at render time.     LaunchedEffect(cameraMode) {-        val portraitOnly = state.activeSetting == CameraSetting.PORTRAIT_BLUR && cameraMode != CameraMode.PORTRAIT-        val photoOnly = state.activeSetting == CameraSetting.ISO && cameraMode != CameraMode.PHOTO-        if (portraitOnly || photoOnly) state.activeSetting = null+        if (effectiveSettingForMode(state.activeSetting, cameraMode) != state.activeSetting) {+            state.activeSetting = null+        }     }      // Whether to offer the NIGHT extension. Reactive: the ViewModel recomputes it on lens/mode
camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenKit.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenKit.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenKit.ktindex 3df89360cc..786f3d2aa2 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenKit.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenKit.kt@@ -8,9 +8,66 @@ import com.vayunmathur.camera.platform.CameraMode  /** Which settings pane is open below the preview. */ internal enum class CameraSetting {-    BRIGHTNESS, SHADOWS, WARMTH, EXPOSURE_TIME, PORTRAIT_BLUR, ISO+    BRIGHTNESS, SHADOWS, WARMTH, EXPOSURE_TIME, PORTRAIT_BLUR, ISO, ZOOM } +/**+ * The settings offered in [mode], in button-row order.+ *+ * Applicability is judged by whether the capture path actually honours the+ * setting in that mode:+ * - [CameraSetting.ZOOM]: `setZoomRatio` goes through the bound camera's+ *   `CameraControl`, which drives every bound use case โ€” photo stills and+ *   the video Preview+VideoCapture alike (preview and recording), and is+ *   re-applied on every bind by `refreshCapabilities`/`restoreZoom`. So+ *   zoom is offered everywhere the zoom bar already worked.+ * - [CameraSetting.BRIGHTNESS] (exposure compensation): likewise a+ *   `CameraControl` call (`setExposureCompensationIndex`), re-applied on+ *   video binds too, so it is live for stills and video. Offered for+ *   PHOTO/PORTRAIT and the video family (VIDEO/TIMELAPSE/CINEMATIC).+ * - [CameraSetting.SHADOWS]/[CameraSetting.WARMTH]: baked into saved+ *   stills and previewed via a still-only RenderEffect; a video recording+ *   would silently drop them, so they stay PHOTO/PORTRAIT-only.+ * - [CameraSetting.EXPOSURE_TIME]/[CameraSetting.ISO]: manual shutter/ISO+ *   locks AE off; a fixed shutter fights the video frame rate, so they+ *   stay PHOTO-only (the existing manual-capture design).+ * - [CameraSetting.PORTRAIT_BLUR]: only the portrait session renders or+ *   bakes bokeh, so PORTRAIT-only.+ *+ * Flash/torch, grid, timer and mic are top-bar controls, not panes here;+ * video resolution/fps have no picker to offer (the video session always+ * records at max quality/fps, codec lives on the Settings page).+ */+internal fun settingsForMode(mode: CameraMode): List<CameraSetting> {+    val bakesStills = mode == CameraMode.PHOTO || mode == CameraMode.PORTRAIT+    val videoFamily = mode == CameraMode.VIDEO || mode == CameraMode.TIMELAPSE ||+        mode == CameraMode.CINEMATIC+    return buildList {+        if (bakesStills || videoFamily) add(CameraSetting.BRIGHTNESS)+        if (bakesStills) {+            add(CameraSetting.SHADOWS)+            add(CameraSetting.WARMTH)+        }+        if (mode == CameraMode.PHOTO) {+            add(CameraSetting.EXPOSURE_TIME)+            add(CameraSetting.ISO)+        }+        if (mode == CameraMode.PORTRAIT) add(CameraSetting.PORTRAIT_BLUR)+        add(CameraSetting.ZOOM)+    }+}++/**+ * The pane to actually show for [setting] in [mode]: a pane the new mode+ * does not offer resolves to null (the lens/zoom bar), so switching modes+ * with a photo-only pane open re-renders the mode-appropriate set instead+ * of leaving the stale slider on screen.+ */+internal fun effectiveSettingForMode(+    setting: CameraSetting?,+    mode: CameraMode,+): CameraSetting? = setting?.takeIf { it in settingsForMode(mode) }+ /** Which camera pipeline drives the preview for the current mode. */ internal enum class SessionKind { PHOTO, PORTRAIT, PANORAMA, HIGH_SPEED, VIDEO } 
camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.ktindex 5709c65463..cea9f32221 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.kt@@ -33,6 +33,7 @@ import com.vayunmathur.library.ui.IconIso import com.vayunmathur.library.ui.IconLightbulb import com.vayunmathur.library.ui.IconSunny import com.vayunmathur.library.ui.IconTimer+import com.vayunmathur.library.ui.IconZoomIn import com.vayunmathur.library.ui.Slider import com.vayunmathur.library.ui.SliderDefaults import com.vayunmathur.library.ui.Text@@ -102,24 +103,22 @@ internal fun SettingsButtonRow(         horizontalArrangement = Arrangement.spacedBy(2.dp),         verticalAlignment = Alignment.CenterVertically     ) {-        // The sliders below bake into saved stills (PHOTO/PORTRAIT) or drive the live-        // preview's 3A (PHOTO only). Offer them only where they do something: elsewhere-        // they'd silently move a preview effect that the capture path drops.-        val bakesStills = cameraMode == CameraMode.PHOTO || cameraMode == CameraMode.PORTRAIT-        val settings = buildList<Pair<CameraSetting, @Composable (Modifier, Color) -> Unit>> {-            if (bakesStills) {-                add(CameraSetting.BRIGHTNESS to { m, c -> IconSunny(m, c) })-                add(CameraSetting.SHADOWS to { m, c -> IconContrast(m, c) })-                add(CameraSetting.WARMTH to { m, c -> IconLightbulb(m, c) })+        // Offer only settings the capture path honours in this mode (see+        // settingsForMode): elsewhere they'd silently move a preview effect+        // that the capture path drops. Video gets brightness (exposure+        // compensation) and zoom, both CameraControl-driven.+        val settings: List<Pair<CameraSetting, @Composable (Modifier, Color) -> Unit>> =+            settingsForMode(cameraMode).map { setting ->+                setting to when (setting) {+                    CameraSetting.BRIGHTNESS -> { m: Modifier, c: Color -> IconSunny(m, c) }+                    CameraSetting.SHADOWS -> { m: Modifier, c: Color -> IconContrast(m, c) }+                    CameraSetting.WARMTH -> { m: Modifier, c: Color -> IconLightbulb(m, c) }+                    CameraSetting.EXPOSURE_TIME -> { m: Modifier, c: Color -> IconTimer(m, c) }+                    CameraSetting.ISO -> { m: Modifier, c: Color -> IconIso(m, c) }+                    CameraSetting.PORTRAIT_BLUR -> { m: Modifier, c: Color -> IconBlur(m, c) }+                    CameraSetting.ZOOM -> { m: Modifier, c: Color -> IconZoomIn(m, c) }+                }             }-            if (cameraMode == CameraMode.PHOTO) {-                add(CameraSetting.EXPOSURE_TIME to { m, c -> IconTimer(m, c) })-                add(CameraSetting.ISO to { m, c -> IconIso(m, c) })-            }-            if (cameraMode == CameraMode.PORTRAIT) {-                add(CameraSetting.PORTRAIT_BLUR to { m, c -> IconBlur(m, c) })-            }-        }         settings.forEach { (setting, icon) ->             val isActive = activeSetting == setting             Box(
camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.ktindex 3932d8d712..b1ef299b49 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.kt@@ -80,8 +80,13 @@ internal fun CameraSettingsColumn(                 }             }         }-        when (activeSetting) {-            null -> {+        // A pane the current mode does not offer (e.g. SHADOWS left open+        // when switching PHOTO -> VIDEO) falls back to the lens/zoom bar+        // instead of rendering the stale slider; CameraScreenEffects also+        // clears the stored pane so it does not resurrect on switching back.+        val effectiveSetting = effectiveSettingForMode(activeSetting, cameraMode)+        when (effectiveSetting) {+            null, CameraSetting.ZOOM -> {                 LensBar(                     lenses = availableLenses,                     selected = selectedLens,@@ -127,7 +132,7 @@ internal fun CameraSettingsColumn(         }          SettingsButtonRow(-            activeSetting = activeSetting,+            activeSetting = effectiveSetting,             cameraMode = cameraMode,             onSelect = onSelectSetting         )
camera/src/test/java/com/vayunmathur/camera/platform/ControlsRecordingExtraTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/platform/ControlsRecordingExtraTest.kt b/camera/src/test/java/com/vayunmathur/camera/platform/ControlsRecordingExtraTest.ktindex 3edafb3dbb..dbf39f927d 100644--- a/camera/src/test/java/com/vayunmathur/camera/platform/ControlsRecordingExtraTest.kt+++ b/camera/src/test/java/com/vayunmathur/camera/platform/ControlsRecordingExtraTest.kt@@ -75,6 +75,22 @@ class ControlsRecordingExtraTest {         assertEquals(8f, viewModel.zoomRatioMutable.value)     } +    @Test fun videoModeZoomAppliesToBoundCameraControl() {+        val viewModel = vm()+        viewModel.switchCameraMode(CameraMode.VIDEO)+        viewModel.boundCamera = fakeCamera()+        // setZoomRatio drives the bound camera's CameraControl, which CameraX+        // applies to every bound use case โ€” the video Preview and the+        // VideoCapture alike โ€” so it takes effect in preview and recording.+        viewModel.setZoomRatio(2f)+        assertEquals(2f, viewModel.zoomRatioMutable.value)+        Mockito.verify(viewModel.boundCamera!!.cameraControl).setZoomRatio(2f)+        // Exposure compensation likewise reaches the video session's control.+        viewModel.applyExposureCompensation(0.5f)+        Mockito.verify(viewModel.boundCamera!!.cameraControl)+            .setExposureCompensationIndex(2)+    }+     @Test fun qrTorchExposureAndFocusControls() {         val viewModel = vm()         viewModel.setQrResult("WIFI:T:WPA;S:x;;")
camera/src/test/java/com/vayunmathur/camera/ui/CameraVideoSettingsComposeTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/ui/CameraVideoSettingsComposeTest.kt b/camera/src/test/java/com/vayunmathur/camera/ui/CameraVideoSettingsComposeTest.ktnew file mode 100644index 0000000000..f676afdf4e--- /dev/null+++ b/camera/src/test/java/com/vayunmathur/camera/ui/CameraVideoSettingsComposeTest.kt@@ -0,0 +1,128 @@+package com.vayunmathur.camera.ui++import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.setValue+import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.hasClickAction+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onAllNodesWithText+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.ext.junit.runners.AndroidJUnit4+import com.vayunmathur.camera.platform.CameraMode+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith++/** Video-mode settings: no stale photo panes, zoom + exposure available. */+@RunWith(AndroidJUnit4::class)+class CameraVideoSettingsComposeTest {++    @get:Rule val rule = createComposeRule()++    private fun column(+        mode: CameraMode,+        setting: CameraSetting?,+        onZoom: (Float) -> Unit = {},+        onSelect: (CameraSetting?) -> Unit = {},+    ) {+        rule.setContent {+            CameraSettingsColumn(+                activeSetting = setting,+                availableLenses = emptyList(), selectedLens = null,+                onLensSelected = {}, zoomRatio = 1f,+                availableZoomLevels = listOf("1x" to 1f, "2x" to 2f),+                onZoomSelected = onZoom, exposureComp = 0.5f, onExposureComp = {},+                shadows = -0.5f, onShadows = {}, warmth = 0f, onWarmth = {},+                exposureTimeIndex = 0, onExposureTimeIndex = {},+                blurStrength = 0.5f, onBlurStrength = {},+                manualIsoIndex = 0, isoStops = listOf(100, 200), onManualIsoIndex = {},+                cameraMode = mode, onSelectSetting = onSelect,+            )+        }+        rule.waitForIdle()+    }++    @Test fun settingsApplicabilityPerMode() {+        assertEquals(+            listOf(+                CameraSetting.BRIGHTNESS, CameraSetting.SHADOWS, CameraSetting.WARMTH,+                CameraSetting.EXPOSURE_TIME, CameraSetting.ISO, CameraSetting.ZOOM,+            ),+            settingsForMode(CameraMode.PHOTO),+        )+        assertEquals(+            listOf(CameraSetting.BRIGHTNESS, CameraSetting.ZOOM),+            settingsForMode(CameraMode.VIDEO),+        )+        assertEquals(settingsForMode(CameraMode.VIDEO), settingsForMode(CameraMode.TIMELAPSE))+        assertEquals(settingsForMode(CameraMode.VIDEO), settingsForMode(CameraMode.CINEMATIC))+        assertTrue(CameraSetting.PORTRAIT_BLUR in settingsForMode(CameraMode.PORTRAIT))+        assertTrue(CameraSetting.SHADOWS !in settingsForMode(CameraMode.VIDEO))+        // Stale photo-only panes resolve to the zoom bar in video; shared panes survive.+        assertNull(effectiveSettingForMode(CameraSetting.SHADOWS, CameraMode.VIDEO))+        assertNull(effectiveSettingForMode(CameraSetting.ISO, CameraMode.VIDEO))+        assertNull(effectiveSettingForMode(CameraSetting.EXPOSURE_TIME, CameraMode.VIDEO))+        assertEquals(+            CameraSetting.BRIGHTNESS,+            effectiveSettingForMode(CameraSetting.BRIGHTNESS, CameraMode.VIDEO),+        )+        assertEquals(+            CameraSetting.ZOOM,+            effectiveSettingForMode(CameraSetting.ZOOM, CameraMode.VIDEO),+        )+    }++    @Test fun switchingPhotoShadowsPanelToVideoShowsZoomAndAppliesIt() {+        var mode by mutableStateOf(CameraMode.PHOTO)+        var zoom: Float? = null+        // The stored pane stays SHADOWS (what the old mode switch left behind);+        // rendering in VIDEO must not show the stale slider.+        rule.setContent {+            CameraSettingsColumn(+                activeSetting = CameraSetting.SHADOWS,+                availableLenses = emptyList(), selectedLens = null,+                onLensSelected = {}, zoomRatio = 1f,+                availableZoomLevels = listOf("1x" to 1f, "2x" to 2f),+                onZoomSelected = { zoom = it }, exposureComp = 0f, onExposureComp = {},+                shadows = -0.5f, onShadows = {}, warmth = 0f, onWarmth = {},+                exposureTimeIndex = 0, onExposureTimeIndex = {},+                blurStrength = 0.5f, onBlurStrength = {},+                manualIsoIndex = 0, isoStops = listOf(100, 200), onManualIsoIndex = {},+                cameraMode = mode, onSelectSetting = {},+            )+        }+        rule.onNodeWithText("-0.5").assertIsDisplayed()+        rule.runOnIdle { mode = CameraMode.VIDEO }+        rule.waitForIdle()+        assertTrue(rule.onAllNodesWithText("-0.5").fetchSemanticsNodes().isEmpty())+        rule.onNodeWithText("2x").assertIsDisplayed()+        rule.onNodeWithText("2x").performClick()+        assertEquals(2f, zoom)+    }++    @Test fun videoModeShowsBrightnessSlider() {+        column(CameraMode.VIDEO, CameraSetting.BRIGHTNESS)+        // Exposure-compensation slider (BRIGHTNESS) is live for video.+        rule.onNodeWithText("+0.5").assertIsDisplayed()+    }++    @Test fun videoModeZoomSettingShowsZoomBar() {+        column(CameraMode.VIDEO, CameraSetting.ZOOM)+        rule.onNodeWithText("2x").assertIsDisplayed()+    }++    @Test fun videoSettingsRowOffersOnlyBrightnessAndZoom() {+        rule.setContent {+            SettingsButtonRow(+                activeSetting = null, cameraMode = CameraMode.VIDEO, onSelect = {},+            )+        }+        rule.waitForIdle()+        assertEquals(2, rule.onAllNodes(hasClickAction()).fetchSemanticsNodes().size)+    }+}