Fix keyboard height setting and add a test

This commit is contained in:
Aleksandras Kostarevas 2024-09-02 20:20:01 +03:00
parent 778c973c60
commit 9e75e45056
5 changed files with 75 additions and 13 deletions

View File

@ -112,7 +112,7 @@ public final class KeyboardSwitcher implements SwitchActions {
|| !mThemeContext.getResources().equals(context.getResources())) {
mKeyboardTheme = keyboardTheme;
mThemeContext = new ContextThemeWrapper(context, keyboardTheme.mStyleId);
KeyboardLayoutSetV2.onKeyboardThemeChanged();
KeyboardLayoutSetV2.onKeyboardThemeChanged(context);
themeSwitchPending = false;
return true;
}

View File

@ -41,11 +41,7 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import org.futo.inputmethod.latin.uix.KeyBordersSetting
import org.futo.inputmethod.latin.uix.KeyHintsSetting
import org.futo.inputmethod.latin.uix.KeyboardBottomOffsetSetting
import org.futo.inputmethod.latin.uix.KeyboardHeightMultiplierSetting
import org.futo.inputmethod.latin.uix.THEME_KEY
import org.futo.inputmethod.latin.uix.settings.SettingSlider
import org.futo.inputmethod.latin.uix.settings.SettingToggleDataStore
import org.futo.inputmethod.latin.uix.settings.useDataStore
import org.futo.inputmethod.latin.uix.theme.ThemeOption
@ -59,7 +55,6 @@ import org.futo.inputmethod.latin.uix.theme.presets.DynamicDarkTheme
import org.futo.inputmethod.latin.uix.theme.presets.DynamicLightTheme
import org.futo.inputmethod.latin.uix.theme.presets.DynamicSystemTheme
import org.futo.inputmethod.latin.uix.theme.presets.VoiceInputTheme
import kotlin.math.roundToInt
@Composable
fun ThemePreview(theme: ThemeOption, isSelected: Boolean = false, overrideName: String? = null, modifier: Modifier = Modifier, onClick: () -> Unit = { }) {

View File

@ -180,17 +180,12 @@ class KeyboardLayoutSetV2 internal constructor(
NumberRowMode.AlwaysDisabled -> false
}
private val keyboardHeightMultiplier = context.getSettingBlocking(KeyboardHeightMultiplierSetting)
private val singularRowHeight: Double
get() = params.height?.let { it / 4.0 } ?: run {
(ResourceUtils.getDefaultKeyboardHeight(context.resources) / 4.0) *
keyboardHeightMultiplier
}
// params.height?.let { it / 4.0 } ?: (50.0 * context.resources.displayMetrics.density * keyboardHeightMultiplier)
private fun getRecommendedKeyboardHeight(): Int {
val numRows = 4.0 +
((mainLayout.effectiveRows.size - 5) / 2.0).coerceAtLeast(0.0) +
@ -263,14 +258,18 @@ Stack trace: ${e.stackTrace.map { it.toString() }}
}
companion object {
var keyboardHeightMultiplier: Float = 1.0f
@JvmStatic
fun onSystemLocaleChanged() {
}
@JvmStatic
fun onKeyboardThemeChanged() {
fun onKeyboardThemeChanged(context: Context) {
keyboardHeightMultiplier = context.getSettingBlocking(KeyboardHeightMultiplierSetting)
// This is where we would clear all caches if we had any
}
}
}

View File

@ -86,7 +86,7 @@ public abstract class KeyboardLayoutSetTestsBase extends AndroidTestCase {
final KeyboardTheme keyboardTheme = KeyboardTheme.getKeyboardTheme(context);
setContext(new ContextThemeWrapper(getContext(), keyboardTheme.mStyleId));
KeyboardLayoutSetV2.onKeyboardThemeChanged();
KeyboardLayoutSetV2.onKeyboardThemeChanged(getContext());
mScreenMetrics = Settings.readScreenMetrics(res);

View File

@ -0,0 +1,68 @@
package org.futo.inputmethod.keyboard.internal
import android.test.AndroidTestCase
import android.view.inputmethod.EditorInfo
import androidx.datastore.preferences.core.edit
import kotlinx.coroutines.runBlocking
import org.futo.inputmethod.latin.uix.KeyboardHeightMultiplierSetting
import org.futo.inputmethod.latin.uix.dataStore
import org.futo.inputmethod.v2keyboard.KeyboardLayoutSetV2
import org.futo.inputmethod.v2keyboard.KeyboardLayoutSetV2Params
import java.util.Locale
import kotlin.math.absoluteValue
class KeyboardLayoutSetV2Tests : AndroidTestCase() {
private val layoutParams = KeyboardLayoutSetV2Params(
width = 1024,
height = null,
keyboardLayoutSet = "qwerty",
locale = Locale.ENGLISH,
editorInfo = EditorInfo(),
numberRow = false,
useSplitLayout = false,
bottomActionKey = null
)
private fun setHeight(to: Float) {
runBlocking {
context.dataStore.edit { it[KeyboardHeightMultiplierSetting.key] = to }
}
KeyboardLayoutSetV2.onKeyboardThemeChanged(context)
}
private fun resetHeight() {
runBlocking {
context.dataStore.edit { it.remove(KeyboardHeightMultiplierSetting.key) }
}
KeyboardLayoutSetV2.onKeyboardThemeChanged(context)
}
private fun getActualHeight(layoutSet: KeyboardLayoutSetV2): Int {
return layoutSet.getKeyboard(
KeyboardLayoutElement(
kind = KeyboardLayoutKind.Alphabet,
page = KeyboardLayoutPage.Base
)
).mBaseHeight
}
fun testKeyboardHeightSettingAffectsHeight() {
try {
val layoutSet = KeyboardLayoutSetV2(context, layoutParams)
// Allow for 1px rounding error
val eps = 1.0f
setHeight(1.0f)
val baseHeight = getActualHeight(layoutSet)
setHeight(2.0f)
assert((2.0f * baseHeight - getActualHeight(layoutSet)).absoluteValue < eps)
setHeight(0.5f)
assert((0.5f * baseHeight - getActualHeight(layoutSet)).absoluteValue < eps)
} finally {
resetHeight()
}
}
}