Skip to content

Commit

Permalink
fix: calculate scaled vertical gap to fit all keyboard height
Browse files Browse the repository at this point in the history
Use the scaled vertical gap in arranging `Key` in `Keyboard`, so that the
keyboard's height will remain the same for all keyboard.

Fix #1250
  • Loading branch information
goofyz committed Apr 19, 2024
1 parent b51b48a commit df6d73e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
19 changes: 10 additions & 9 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Keyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,8 @@ class Keyboard() {
keyboardHeight = getKeyboardHeight(theme, keyboardConfig)
val keyboardKeyWidth = obtainFloat(keyboardConfig, "width", 0f)
val maxColumns = if (columns == -1) Int.MAX_VALUE else columns
var x = this.horizontalGap / 2
var y = this.verticalGap
var row = 0
var column = 0
minWidth = 0
val isSplit = KeyboardPrefs().isLandscapeMode() && isLandscapeSplit
val (rowWidthTotalWeight, oneWeightWidthPx, multiplier, height1) =
val (rowWidthTotalWeight, oneWeightWidthPx, multiplier, scaledHeight, scaledVerticalGap) =
KeyboardSizeCalculator(
name,
isSplit,
Expand All @@ -252,6 +247,12 @@ class Keyboard() {
autoHeightIndex,
).calc(lm)

var x = this.horizontalGap / 2
var y = scaledVerticalGap
var row = 0
var column = 0
minWidth = 0

try {
var rowWidthWeight = 0f
for (mk in lm) {
Expand All @@ -266,7 +267,7 @@ class Keyboard() {
// new row
rowWidthWeight = 0f
x = gap / 2
y += this.verticalGap + rowHeight
y += scaledVerticalGap + rowHeight
column = 0
row++
if (mKeys.size > 0) mKeys[mKeys.size - 1].edgeFlags = mKeys[mKeys.size - 1].edgeFlags or EDGE_RIGHT
Expand Down Expand Up @@ -296,7 +297,7 @@ class Keyboard() {
if (column == 0) {
rowHeight =
if (keyboardHeight > 0) {
height1[row]
scaledHeight[row]
} else {
val heightK = appContext.sp(obtainFloat(mk, "height", 0f)).toInt()
if (heightK > 0) heightK else defaultHeight
Expand Down Expand Up @@ -418,7 +419,7 @@ class Keyboard() {
}
}
if (mKeys.size > 0) mKeys[mKeys.size - 1].edgeFlags = mKeys[mKeys.size - 1].edgeFlags or EDGE_RIGHT
this.height = y + rowHeight + this.verticalGap
this.height = y + rowHeight + scaledVerticalGap
for (key in mKeys) {
if (key.column == 0) key.edgeFlags = key.edgeFlags or EDGE_LEFT
if (key.row == 0) key.edgeFlags = key.edgeFlags or EDGE_TOP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ data class KeyboardSize(
val rowWidthTotalWeight: Map<Int, Float>,
val defaultWidth: Float,
val multiplier: Float,
val height: List<Int>,
val scaledHeight: List<Int>,
val scaledVerticalGap: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.osfans.trime.util.CollectionUtils.obtainFloat
import com.osfans.trime.util.appContext
import com.osfans.trime.util.sp
import kotlin.math.abs
import kotlin.math.ceil

class KeyboardSizeCalculator(
val name: String,
Expand Down Expand Up @@ -83,31 +84,38 @@ class KeyboardSizeCalculator(
rawSumHeight += rowHeight
rawHeight.add(rowHeight)

val scaledVerticalGap = calculateScaledVerticalGap(rawSumHeight, rawHeight)
return KeyboardSize(
rowTotalWeight,
calculateOneWeightWidthPx(),
splitSpaceRatio,
calculateAdjustedHeight(rawSumHeight, rawHeight, autoHeightIndex),
calculateAdjustedHeight(rawSumHeight, rawHeight, scaledVerticalGap),
scaledVerticalGap.toInt(),
)
}

private fun calculateOneWeightWidthPx(): Float {
return (mAllowedWidth / (MAX_TOTAL_WEIGHT * (1 + splitSpaceRatio)))
}

private fun calculateAdjustedHeight(
private fun calculateScaledVerticalGap(
rawSumHeight: Int,
rawHeight: List<Int>,
autoHeightIndex: Int,
): List<Int> {
var scale: Float =
keyboardHeight.toFloat() / (rawSumHeight + mDefaultVerticalGap * (rawHeight.size + 1))
): Double {
val scale: Double =
keyboardHeight.toDouble() / (rawSumHeight + mDefaultVerticalGap * (rawHeight.size + 1))

val verticalGap = Math.ceil((mDefaultVerticalGap * scale).toDouble()).toInt()
return ceil((mDefaultVerticalGap * scale))
}

var autoHeight: Int = keyboardHeight - verticalGap * (rawHeight.size + 1)
private fun calculateAdjustedHeight(
rawSumHeight: Int,
rawHeight: List<Int>,
scaledVerticalGap: Double,
): List<Int> {
var remainHeight = keyboardHeight - scaledVerticalGap * (rawHeight.size + 1)

scale = (autoHeight.toFloat()) / rawSumHeight
val scale = remainHeight / rawSumHeight

var finalAutoHeightIndex = -100
if (autoHeightIndex < 0) {
Expand All @@ -122,13 +130,13 @@ class KeyboardSizeCalculator(
if (i != finalAutoHeightIndex) {
val h: Int = (rawHeight[i] * scale).toInt()
newHeight[i] = h
autoHeight -= h
remainHeight -= h
}
}
if (autoHeight < 1) {
if (rawHeight[autoHeight] > 0) autoHeight = 1
if (remainHeight < 1) {
if (rawHeight[finalAutoHeightIndex] > 0) remainHeight = 1.0
}
newHeight[finalAutoHeightIndex] = autoHeight
newHeight[finalAutoHeightIndex] = remainHeight.toInt()

return newHeight
}
Expand Down

0 comments on commit df6d73e

Please sign in to comment.