camera: fix landscape preview aspect ratio (#827)
- Commit
a5b150262d3e3d38171e6a581a192965820f6d08- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-18 19:38 UTC (1 month ago)
- Parents
40ac218
Diff
camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.ktindex a9735863c4..4df8ca7937 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt@@ -60,6 +60,17 @@ private fun currentLensFacing(selectorInt: Int): com.vayunmathur.camera.domain.L com.vayunmathur.camera.domain.LensFacing.FRONT else com.vayunmathur.camera.domain.LensFacing.BACK +/**+ * The still-crop ratios in [CameraScreenState.previewAspectRatio] are portrait (3:4, 9:16,+ * ...). In a landscape window the sensor frame rotates with the device, so the preview+ * box uses the reciprocal — otherwise a portrait box in a wide window collapses to a+ * narrow strip down the middle (#827). Pure so it stays unit-testable.+ */+internal fun previewRatioForWindow(baseRatio: Float, maxWidth: Float, maxHeight: Float): Float {+ val isLandscape = maxWidth > maxHeight+ return if (isLandscape && baseRatio > 0f) 1f / baseRatio else baseRatio+}+ /** * The letterboxed viewfinder plus its gesture handling, bokeh/color RenderEffects, * grid/level/night overlays, settings column and panorama overlay. Extracted verbatim@@ -77,11 +88,15 @@ internal fun BoxWithConstraintsScope.CameraPreviewBox( // Fit the preview inside the available area (letterboxed) so a tall ratio never // overflows onto the top/bottom bars and steals their touch events.+ // The still-crop ratios in state are portrait (3:4, 9:16, ...). In a landscape+ // window the sensor frame rotates with the device, so use the reciprocal —+ // otherwise a portrait box in a wide window collapses to a narrow strip (#827).+ val previewRatio = previewRatioForWindow(state.previewAspectRatio, maxWidth.value, maxHeight.value) val boxAspect = if (maxHeight.value > 0f) maxWidth.value / maxHeight.value else 1f- val previewSize = if (state.previewAspectRatio > boxAspect) {- Modifier.fillMaxWidth().aspectRatio(state.previewAspectRatio)+ val previewSize = if (previewRatio > boxAspect) {+ Modifier.fillMaxWidth().aspectRatio(previewRatio) } else {- Modifier.fillMaxHeight().aspectRatio(state.previewAspectRatio)+ Modifier.fillMaxHeight().aspectRatio(previewRatio) } // RuntimeShader and the runtime-shader RenderEffect are API 33+. On older // releases the portrait preview renders without the background blur.
camera/src/test/java/com/vayunmathur/camera/ui/PreviewRatioForWindowTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/ui/PreviewRatioForWindowTest.kt b/camera/src/test/java/com/vayunmathur/camera/ui/PreviewRatioForWindowTest.ktnew file mode 100644index 0000000000..681b712a7b--- /dev/null+++ b/camera/src/test/java/com/vayunmathur/camera/ui/PreviewRatioForWindowTest.kt@@ -0,0 +1,23 @@+package com.vayunmathur.camera.ui++import kotlin.test.Test+import kotlin.test.assertEquals++/** Regression tests for #827: portrait crop ratios must flip in landscape windows. */+class PreviewRatioForWindowTest {+ @Test+ fun portraitWindowKeepsPortraitRatio() {+ assertEquals(3f / 4f, previewRatioForWindow(3f / 4f, 1080f, 2400f), 0.0001f)+ }++ @Test+ fun landscapeWindowUsesReciprocal() {+ // 4:3 still crop in a landscape phone window (2400x1080) fills wide, not a strip.+ assertEquals(4f / 3f, previewRatioForWindow(3f / 4f, 2400f, 1080f), 0.0001f)+ }++ @Test+ fun squareRatioUnaffectedByOrientation() {+ assertEquals(1f, previewRatioForWindow(1f, 2400f, 1080f), 0.0001f)+ }+}