mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 00:35:26 +02:00
Fix rounding errors in keypad
This commit is contained in:
parent
ba43fe82da
commit
a12916e7b6
@ -39,14 +39,16 @@ import androidx.compose.ui.Modifier
|
||||
* @param content Content, usually some buttons. Use `width` and `height` in [fillMaxWidth]
|
||||
* and [fillMaxHeight] to build a [Modifier]. Passed [Composable]s are equally distributed, occupy
|
||||
* entire height and width.
|
||||
*
|
||||
* _Note_: Do not use provided `width` if [horizontalPadding] is 0. Use `weight` modifier.
|
||||
*/
|
||||
@Composable
|
||||
fun KeypadFlow(
|
||||
modifier: Modifier,
|
||||
rows: Int,
|
||||
columns: Int,
|
||||
@IntRange(0, 100) verticalPadding: Int = 10,
|
||||
@IntRange(0, 100) horizontalPadding: Int = 10,
|
||||
@IntRange(0, 100) verticalPadding: Int = 10,
|
||||
content: @Composable FlowRowScope.(width: Float, height: Float) -> Unit
|
||||
) {
|
||||
val height: Float = remember { (1f - verticalPadding / 100f) / rows }
|
||||
|
@ -116,8 +116,8 @@ private fun RPNCalculatorKeyboardPortrait(
|
||||
columns = 4
|
||||
) { width, height ->
|
||||
val aModifier = Modifier
|
||||
.fillMaxHeight(height)
|
||||
.fillMaxWidth(width)
|
||||
.fillMaxHeight(height)
|
||||
|
||||
KeyboardButtonAdditional(aModifier, UnittoIcons.Swap, allowVibration, KeyboardButtonContentHeightWide) { onCalculationClick(RPNCalculation.Swap) }
|
||||
KeyboardButtonAdditional(aModifier, UnittoIcons.Up, allowVibration, KeyboardButtonContentHeightWide) { onCalculationClick(RPNCalculation.RotateUp) }
|
||||
@ -131,8 +131,8 @@ private fun RPNCalculatorKeyboardPortrait(
|
||||
columns = 4
|
||||
) { width, height ->
|
||||
val bModifier = Modifier
|
||||
.fillMaxHeight(height)
|
||||
.fillMaxWidth(width)
|
||||
.fillMaxHeight(height)
|
||||
|
||||
KeyboardButtonTertiary(bModifier, UnittoIcons.Clear, allowVibration, KeyboardButtonContentHeightTall) { onCalculationClick(RPNCalculation.Clear) }
|
||||
KeyboardButtonFilled(bModifier, UnittoIcons.Unary, allowVibration, KeyboardButtonContentHeightTall) { onCalculationClick(RPNCalculation.Negate) }
|
||||
|
@ -210,6 +210,7 @@ private fun PortraitKeyboard(
|
||||
) { inverse ->
|
||||
if (inverse) {
|
||||
AdditionalPortrait(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
showAdditional = showAdditional,
|
||||
buttonHeight = additionalButtonHeight,
|
||||
content1 = { buttonModifier ->
|
||||
@ -232,6 +233,7 @@ private fun PortraitKeyboard(
|
||||
)
|
||||
} else {
|
||||
AdditionalPortrait(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
showAdditional = showAdditional,
|
||||
buttonHeight = additionalButtonHeight,
|
||||
content1 = { buttonModifier ->
|
||||
@ -335,6 +337,7 @@ private fun PortraitKeyboard(
|
||||
*/
|
||||
@Composable
|
||||
private fun AdditionalPortrait(
|
||||
modifier: Modifier,
|
||||
showAdditional: Boolean,
|
||||
buttonHeight: Dp,
|
||||
content1: @Composable (buttonModifier: Modifier) -> Unit,
|
||||
@ -342,7 +345,7 @@ private fun AdditionalPortrait(
|
||||
) {
|
||||
AnimatedContent(
|
||||
targetState = showAdditional,
|
||||
modifier = Modifier,
|
||||
modifier = modifier,
|
||||
transitionSpec = {
|
||||
expandVertically(expandFrom = Alignment.Top) togetherWith
|
||||
shrinkVertically(shrinkTowards = Alignment.Top) using (SizeTransform())
|
||||
@ -355,12 +358,14 @@ private fun AdditionalPortrait(
|
||||
.fillMaxWidth(),
|
||||
rows = 3,
|
||||
columns = 4,
|
||||
verticalPadding = 0,
|
||||
horizontalPadding = 0,
|
||||
) { width, height ->
|
||||
verticalPadding = 0,
|
||||
) { _, _ ->
|
||||
// Can't use provided width and height due to rounding errors. It's ok since weight
|
||||
// achieves same result (no padding and equally distributed).
|
||||
val buttonModifier = Modifier
|
||||
.fillMaxWidth(width)
|
||||
.fillMaxHeight(height)
|
||||
.weight(1f)
|
||||
.height(buttonHeight)
|
||||
|
||||
content1(buttonModifier)
|
||||
content2(buttonModifier)
|
||||
@ -372,12 +377,12 @@ private fun AdditionalPortrait(
|
||||
.fillMaxWidth(),
|
||||
rows = 1,
|
||||
columns = 4,
|
||||
verticalPadding = 0,
|
||||
horizontalPadding = 0,
|
||||
) { width, height ->
|
||||
verticalPadding = 0,
|
||||
) { _, _ ->
|
||||
val buttonModifier = Modifier
|
||||
.fillMaxWidth(width)
|
||||
.fillMaxHeight(height)
|
||||
.weight(1f)
|
||||
.height(buttonHeight)
|
||||
|
||||
content1(buttonModifier)
|
||||
}
|
||||
@ -527,7 +532,7 @@ private fun LandscapeKeyboard(
|
||||
@Composable
|
||||
private fun PreviewCalculatorKeyboard() {
|
||||
CalculatorKeyboard(
|
||||
modifier = Modifier,
|
||||
modifier = Modifier.fillMaxHeight(0.75f),
|
||||
radianMode = true,
|
||||
fractional = ".",
|
||||
addSymbol = {},
|
||||
|
@ -71,26 +71,26 @@ internal fun AddSubtractKeyboard(
|
||||
.weight(3f),
|
||||
rows = 4,
|
||||
columns = 3
|
||||
) { width, height ->
|
||||
val mainButtonModifier = Modifier
|
||||
) { width, height ->
|
||||
val buttonModifier = Modifier
|
||||
.fillMaxWidth(width)
|
||||
.fillMaxHeight(height)
|
||||
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key7, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._7) }
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key8, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._8) }
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key9, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._9) }
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key7, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._7) }
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key8, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._8) }
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key9, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._9) }
|
||||
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key4, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._4) }
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key5, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._5) }
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key6, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._6) }
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key4, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._4) }
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key5, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._5) }
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key6, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._6) }
|
||||
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key1, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._1) }
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key2, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._2) }
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key3, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._3) }
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key1, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._1) }
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key2, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._2) }
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key3, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._3) }
|
||||
|
||||
Spacer(mainButtonModifier)
|
||||
KeyboardButtonLight(mainButtonModifier, UnittoIcons.Key0, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._0) }
|
||||
Spacer(mainButtonModifier)
|
||||
Spacer(buttonModifier)
|
||||
KeyboardButtonLight(buttonModifier, UnittoIcons.Key0, allowVibration, KeyboardButtonContentHeightTall) { addSymbol(Token.Digit._0) }
|
||||
Spacer(buttonModifier)
|
||||
}
|
||||
|
||||
KeypadFlow(
|
||||
|
Loading…
x
Reference in New Issue
Block a user