From 9e75e45056f8713c0f3f105a0e0e0071b55fae3d Mon Sep 17 00:00:00 2001 From: Aleksandras Kostarevas Date: Mon, 2 Sep 2024 20:20:01 +0300 Subject: [PATCH] Fix keyboard height setting and add a test --- .../keyboard/KeyboardSwitcher.java | 2 +- .../latin/uix/theme/selector/ThemePicker.kt | 5 -- .../v2keyboard/KeyboardLayoutSet.kt | 11 ++- .../keyboard/KeyboardLayoutSetTestsBase.java | 2 +- .../internal/KeyboardLayoutSetV2Tests.kt | 68 +++++++++++++++++++ 5 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 tests/src/org/futo/inputmethod/keyboard/internal/KeyboardLayoutSetV2Tests.kt diff --git a/java/src/org/futo/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/org/futo/inputmethod/keyboard/KeyboardSwitcher.java index d0f1de82a..8d3bd54ad 100644 --- a/java/src/org/futo/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/org/futo/inputmethod/keyboard/KeyboardSwitcher.java @@ -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; } diff --git a/java/src/org/futo/inputmethod/latin/uix/theme/selector/ThemePicker.kt b/java/src/org/futo/inputmethod/latin/uix/theme/selector/ThemePicker.kt index 1634fd55d..017866547 100644 --- a/java/src/org/futo/inputmethod/latin/uix/theme/selector/ThemePicker.kt +++ b/java/src/org/futo/inputmethod/latin/uix/theme/selector/ThemePicker.kt @@ -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 = { }) { diff --git a/java/src/org/futo/inputmethod/v2keyboard/KeyboardLayoutSet.kt b/java/src/org/futo/inputmethod/v2keyboard/KeyboardLayoutSet.kt index 681cc285a..67387e086 100644 --- a/java/src/org/futo/inputmethod/v2keyboard/KeyboardLayoutSet.kt +++ b/java/src/org/futo/inputmethod/v2keyboard/KeyboardLayoutSet.kt @@ -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 } } } diff --git a/tests/src/org/futo/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java b/tests/src/org/futo/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java index 59779bcb8..13c7e8be1 100644 --- a/tests/src/org/futo/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java +++ b/tests/src/org/futo/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java @@ -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); diff --git a/tests/src/org/futo/inputmethod/keyboard/internal/KeyboardLayoutSetV2Tests.kt b/tests/src/org/futo/inputmethod/keyboard/internal/KeyboardLayoutSetV2Tests.kt new file mode 100644 index 000000000..00326c169 --- /dev/null +++ b/tests/src/org/futo/inputmethod/keyboard/internal/KeyboardLayoutSetV2Tests.kt @@ -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() + } + } +} \ No newline at end of file