calculator: landscape keeps display on top with wide keypad
- Commit
bce1e8b33df44506b05e739e19ebf898bb4cb5ab- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-25 20:26 UTC (1 month ago)
- Parents
a5ad79f
Diff
calculator/src/main/java/com/vayunmathur/calculator/ui/CalculatorLandscape.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/ui/CalculatorLandscape.kt b/calculator/src/main/java/com/vayunmathur/calculator/ui/CalculatorLandscape.ktnew file mode 100644index 0000000000..33252b807f--- /dev/null+++ b/calculator/src/main/java/com/vayunmathur/calculator/ui/CalculatorLandscape.kt@@ -0,0 +1,141 @@+package com.vayunmathur.calculator.ui++import androidx.compose.foundation.horizontalScroll+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.PaddingValues+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.RowScope+import androidx.compose.foundation.layout.fillMaxHeight+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.rememberScrollState+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.sp+import com.vayunmathur.calculator.util.CalculatorActions+import com.vayunmathur.calculator.util.CalculatorUiState+import com.vayunmathur.calculator.util.renderInputForDisplay+import com.vayunmathur.library.ui.Button+import com.vayunmathur.library.ui.ButtonDefaults+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.rememberHaptics++/**+ * Compact readout for short (landscape-phone) windows: the equation with its live+ * preview beneath, stacked above the keypad. The full [DisplayColumn] — history+ * stack, unit chips, large type — assumes portrait height and would eat the keypad's rows here.+ */+@Composable+internal fun LandscapeDisplay(state: CalculatorUiState, modifier: Modifier = Modifier) {+ val equation = renderInputForDisplay(state.input)+ .ifEmpty { "0" }+ .replace("*", "×").replace("/", "÷").replace("-", "−")+ Column(modifier.padding(horizontal = 16.dp, vertical = 4.dp), horizontalAlignment = Alignment.End) {+ Text(+ equation,+ modifier = Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()),+ textAlign = TextAlign.End,+ fontSize = 26.sp,+ maxLines = 1,+ softWrap = false,+ color = MaterialTheme.colorScheme.onSurface,+ )+ Text(+ state.preview,+ modifier = Modifier.fillMaxWidth(),+ textAlign = TextAlign.End,+ fontSize = 18.sp,+ maxLines = 1,+ softWrap = false,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }+}++/**+ * Keypad for short (landscape-phone) windows. The 8 portrait rows are re-flowed+ * into two side-by-side 4-row halves — digits/operators on the right where thumbs+ * expect them, scientific functions on the left — so 4 weight-distributed rows fit+ * the ~300dp of content height where 8 fixed-height rows overflowed. Keys take+ * their share of the available height (`weight`, `fillMaxHeight`) instead of a+ * fixed 50dp, which is what left the bottom row half-cut under the nav inset.+ * No scrolling: every key is on screen with no gesture needed.+ */+@Composable+internal fun LandscapeKeypad(+ rows: List<List<Key>>,+ actions: CalculatorActions,+ second: Boolean,+ onToggleSecond: () -> Unit,+ modifier: Modifier = Modifier,+) {+ // Portrait order is [2nd/pi row, trig row, powers/parens row, memory row,+ // 7-8-9 row, 4-5-6 row, 1-2-3 row, ans-0 row]. Re-flowed as scientific+ // (first four rows) | basic (last four rows).+ val scientific = rows.take(4)+ val basic = rows.drop(4)+ Row(modifier.padding(horizontal = 4.dp)) {+ Column(Modifier.weight(1f).fillMaxHeight()) {+ scientific.forEach { row ->+ Row(Modifier.fillMaxWidth().weight(1f)) {+ row.forEach { key ->+ LandscapeKeyButton(key, actions, second) { onToggleSecond() }+ }+ }+ }+ }+ Column(Modifier.weight(1f).fillMaxHeight()) {+ basic.forEach { row ->+ Row(Modifier.fillMaxWidth().weight(1f)) {+ row.forEach { key ->+ LandscapeKeyButton(key, actions, second) { onToggleSecond() }+ }+ }+ }+ }+ }+}++@Composable+private fun RowScope.LandscapeKeyButton(+ key: Key,+ actions: CalculatorActions,+ second: Boolean,+ onToggleSecond: () -> Unit,+) {+ val showSecond = second && key.second != null+ val label = if (showSecond) key.second else key.label+ val secondActive = key.emphasis == KeyEmphasis.Toggle && second+ // A light key tick on every press, matching KeyButton.+ val haptics = rememberHaptics()+ val colors = when (key.emphasis) {+ KeyEmphasis.Digit -> ButtonDefaults.filledTonalButtonColors()+ KeyEmphasis.Operator -> ButtonDefaults.buttonColors(+ containerColor = MaterialTheme.colorScheme.secondaryContainer,+ contentColor = MaterialTheme.colorScheme.onSecondaryContainer,+ )+ KeyEmphasis.Primary -> ButtonDefaults.buttonColors()+ KeyEmphasis.Function -> ButtonDefaults.textButtonColors()+ KeyEmphasis.Toggle -> if (secondActive) ButtonDefaults.buttonColors() else ButtonDefaults.textButtonColors()+ }+ Button(+ onClick = {+ haptics.keyPress()+ when {+ key.emphasis == KeyEmphasis.Toggle -> onToggleSecond()+ showSecond -> key.secondPress?.invoke(actions)+ else -> key.onPress(actions)+ }+ },+ modifier = Modifier.weight(key.weight).fillMaxHeight().padding(2.dp),+ shape = MaterialTheme.shapes.largeIncreased,+ colors = colors,+ contentPadding = PaddingValues(0.dp),+ ) {+ Text(label, fontSize = if (label.length >= 4) 13.sp else 18.sp, maxLines = 1)+ }+}
calculator/src/main/java/com/vayunmathur/calculator/ui/CalculatorPage.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/ui/CalculatorPage.kt b/calculator/src/main/java/com/vayunmathur/calculator/ui/CalculatorPage.ktindex da12a56701..191c31e5ef 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/ui/CalculatorPage.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/ui/CalculatorPage.kt@@ -23,6 +23,7 @@ import com.vayunmathur.library.ui.IconButton import com.vayunmathur.library.ui.IconRuler import com.vayunmathur.library.ui.VerticalDivider import com.vayunmathur.library.ui.appBarScrollBehavior+import com.vayunmathur.library.ui.isCompactHeight import com.vayunmathur.library.ui.isExpandedWidth /** Binds [CalculatorViewModel] to the stateless [CalculatorScreen]. */@@ -60,20 +61,48 @@ fun CalculatorScreen( }, scrollBehavior = appBarScrollBehavior(), ) { padding ->- // Sibling tools, never list-detail. On expanded windows the display sits- // beside the keypad (fractional split, keypad capped so keys stay usable)- // instead of stretching full-width.- if (isExpandedWidth()) {- Row(Modifier.fillMaxSize().padding(padding).padding(16.dp)) {- DisplayColumn(state, actions, Modifier.weight(1f).fillMaxSize())- VerticalDivider()- KeypadColumn(rows, actions, second, { second = !second }, Modifier.weight(1f).widthIn(max = 560.dp).fillMaxSize())+ // Three genuinely different windows, not two:+ //+ // - Compact height (landscape phone, <480dp tall): vertical space is the+ // scarce axis. The portrait stack (display over an 8-row keypad) needs+ // ~500dp+ of height and clips; the old side-by-side branch squeezed all+ // 8 rows into ~300dp of content height, so the nav inset (~24dp) plus+ // the fixed 50dp keys overflowed and the bottom row rendered half-cut.+ // Instead the keypad goes full-bleed edge to edge with weight-distributed+ // keys (no fixed height, no outer 16dp padding), and the readout+ // collapses to a single compact line above the keys. No scrolling, no+ // clipping, no lost row.+ // - Expanded width (tablet/desktop): room for the sibling-tools split —+ // display beside a capped keypad so keys stay usable.+ // - Everything else (portrait phone): the classic stacked layout.+ val compactHeight = isCompactHeight()+ val expandedWidth = isExpandedWidth()+ when {+ compactHeight -> {+ Column(Modifier.fillMaxSize().padding(padding)) {+ LandscapeDisplay(state, Modifier.fillMaxWidth())+ LandscapeKeypad(+ rows,+ actions,+ second,+ { second = !second },+ Modifier.fillMaxWidth().weight(1f),+ )+ } }- } else {- Column(Modifier.fillMaxSize().padding(padding)) {- DisplayColumn(state, actions, Modifier.fillMaxWidth().weight(1f))- HorizontalDivider()- KeypadColumn(rows, actions, second, { second = !second }, Modifier.fillMaxWidth())+ expandedWidth -> {+ Row(Modifier.fillMaxSize().padding(padding).padding(16.dp)) {+ DisplayColumn(state, actions, Modifier.weight(1f).fillMaxSize())+ VerticalDivider()+ KeypadColumn(rows, actions, second, { second = !second }, Modifier.weight(1f).widthIn(max = 560.dp).fillMaxSize())+ }+ }+ else -> {+ Column(Modifier.fillMaxSize().padding(padding)) {+ DisplayColumn(state, actions, Modifier.fillMaxWidth().weight(1f))+ HorizontalDivider()+ KeypadColumn(rows, actions, second, { second = !second }, Modifier.fillMaxWidth())+ } } } }