From f795a7228e369a5e34e08a4cdf6177823c281ed2 Mon Sep 17 00:00:00 2001 From: Aleksandras Kostarevas Date: Sat, 21 Sep 2024 23:16:37 +0300 Subject: [PATCH] Fix width too long in landscape mode --- .../config.xml | 0 .../config.xml | 2 +- java/res/values-sw768dp/config.xml | 2 +- .../keyboard/KeyboardSwitcher.java | 7 +- .../futo/inputmethod/latin/uix/UixManager.kt | 66 ++++++++++++------- .../latin/utils/ResourceUtils.java | 15 ++++- .../keyboard/KeyboardLayoutSetTestsBase.java | 2 +- 7 files changed, 64 insertions(+), 30 deletions(-) rename java/res/{values-sw600dp-land => values-sw600dp-long-land}/config.xml (100%) rename java/res/{values-sw768dp-land => values-sw768dp-long-land}/config.xml (98%) diff --git a/java/res/values-sw600dp-land/config.xml b/java/res/values-sw600dp-long-land/config.xml similarity index 100% rename from java/res/values-sw600dp-land/config.xml rename to java/res/values-sw600dp-long-land/config.xml diff --git a/java/res/values-sw768dp-land/config.xml b/java/res/values-sw768dp-long-land/config.xml similarity index 98% rename from java/res/values-sw768dp-land/config.xml rename to java/res/values-sw768dp-long-land/config.xml index fdb95c6cc..be4b6864e 100644 --- a/java/res/values-sw768dp-land/config.xml +++ b/java/res/values-sw768dp-long-land/config.xml @@ -22,7 +22,7 @@ - 365.4dp + 302.4dp 35%p 1.896%p diff --git a/java/res/values-sw768dp/config.xml b/java/res/values-sw768dp/config.xml index 13be6bedf..ac5db4a92 100644 --- a/java/res/values-sw768dp/config.xml +++ b/java/res/values-sw768dp/config.xml @@ -22,7 +22,7 @@ - 302.4dp + 365.4dp 46%p -35.0%p diff --git a/java/src/org/futo/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/org/futo/inputmethod/keyboard/KeyboardSwitcher.java index 5b5185fd4..828312c7c 100644 --- a/java/src/org/futo/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/org/futo/inputmethod/keyboard/KeyboardSwitcher.java @@ -23,6 +23,7 @@ import android.util.Log; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.View; +import android.view.Window; import android.view.inputmethod.EditorInfo; import androidx.annotation.NonNull; @@ -149,13 +150,15 @@ public final class KeyboardSwitcher implements SwitchActions { Rect padding = new Rect(); + Window window = mLatinIMELegacy.getInputMethodService().getWindow().getWindow(); + if(computedSize instanceof SplitKeyboardSize) { - keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res); + keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(window, res); keyboardHeight = ((SplitKeyboardSize) computedSize).getHeight(); splitLayoutWidth = ((SplitKeyboardSize) computedSize).getSplitLayoutWidth(); padding = ((SplitKeyboardSize) computedSize).getPadding(); }else if(computedSize instanceof RegularKeyboardSize) { - keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res); + keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(window, res); keyboardHeight = ((RegularKeyboardSize) computedSize).getHeight(); padding = ((RegularKeyboardSize) computedSize).getPadding(); } diff --git a/java/src/org/futo/inputmethod/latin/uix/UixManager.kt b/java/src/org/futo/inputmethod/latin/uix/UixManager.kt index 709f29111..079f7847b 100644 --- a/java/src/org/futo/inputmethod/latin/uix/UixManager.kt +++ b/java/src/org/futo/inputmethod/latin/uix/UixManager.kt @@ -1,7 +1,6 @@ package org.futo.inputmethod.latin.uix import android.app.Activity -import android.app.KeyguardManager import android.content.ClipDescription import android.content.Context import android.content.Intent @@ -62,11 +61,15 @@ import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.dp import androidx.lifecycle.LifecycleCoroutineScope import androidx.lifecycle.lifecycleScope +import androidx.window.layout.FoldingFeature +import androidx.window.layout.WindowInfoTracker +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import org.futo.inputmethod.accessibility.AccessibilityUtils import org.futo.inputmethod.latin.AudioAndHapticFeedbackManager import org.futo.inputmethod.latin.BuildConfig +import org.futo.inputmethod.latin.FoldingOptions import org.futo.inputmethod.latin.LanguageSwitcherDialog import org.futo.inputmethod.latin.LatinIME import org.futo.inputmethod.latin.R @@ -103,6 +106,12 @@ val LocalThemeProvider = compositionLocalOf { error("No LocalThemeProvider provided") } +val LocalFoldingState = compositionLocalOf { + FoldingOptions(null) +} + + + private class LatinIMEActionInputTransaction( private val inputLogic: InputLogic, shouldApplySpace: Boolean, @@ -323,6 +332,8 @@ class UixManager(private val latinIME: LatinIME) { isShowingActionEditor.value = true } + val foldingOptions = mutableStateOf(FoldingOptions(null)) + var isInputOverridden = mutableStateOf(false) var currWindowActionWindow: ActionWindow? = null @@ -622,35 +633,39 @@ class UixManager(private val latinIME: LatinIME) { CompositionLocalProvider(LocalManager provides keyboardManagerForAction) { CompositionLocalProvider(LocalThemeProvider provides latinIME.getDrawableProvider()) { CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) { - InputDarkener(isInputOverridden.value || isShowingActionEditor.value) { - closeActionWindow() - isShowingActionEditor.value = false - } + CompositionLocalProvider(LocalFoldingState provides foldingOptions.value) { + InputDarkener(isInputOverridden.value || isShowingActionEditor.value) { + closeActionWindow() + isShowingActionEditor.value = false + } - Column { - Spacer(modifier = Modifier.weight(1.0f)) - Surface(modifier = Modifier.onSizeChanged { - latinIME.updateTouchableHeight(it.height) - }, color = latinIME.keyboardColor) { - Box { - Column { - when { - currWindowActionWindow != null -> ActionViewWithHeader( - currWindowActionWindow!! - ) + Column { + Spacer(modifier = Modifier.weight(1.0f)) + Surface(modifier = Modifier.onSizeChanged { + latinIME.updateTouchableHeight(it.height) + }, color = latinIME.keyboardColor) { + Box { + Column { + when { + currWindowActionWindow != null -> ActionViewWithHeader( + currWindowActionWindow!! + ) - else -> MainKeyboardViewWithActionBar() + else -> MainKeyboardViewWithActionBar() + } + + latinIME.LegacyKeyboardView(hidden = isMainKeyboardHidden) } - latinIME.LegacyKeyboardView(hidden = isMainKeyboardHidden) + ForgetWordDialog() } - ForgetWordDialog() } - } - } - ActionEditorHost() + } + + ActionEditorHost() + } } } } @@ -852,6 +867,13 @@ class UixManager(private val latinIME: LatinIME) { } isActionsExpanded.value = latinIME.getSettingBlocking(ActionBarExpanded) + + latinIME.lifecycleScope.launch(Dispatchers.Main) { + WindowInfoTracker.getOrCreate(latinIME).windowLayoutInfo(latinIME).collect { + foldingOptions.value = FoldingOptions(it.displayFeatures.filterIsInstance().firstOrNull()) + latinIME.invalidateKeyboard(true) + } + } } fun onPersistentStatesUnlocked() { diff --git a/java/src/org/futo/inputmethod/latin/utils/ResourceUtils.java b/java/src/org/futo/inputmethod/latin/utils/ResourceUtils.java index 3bce20ae3..cd2aac846 100644 --- a/java/src/org/futo/inputmethod/latin/utils/ResourceUtils.java +++ b/java/src/org/futo/inputmethod/latin/utils/ResourceUtils.java @@ -18,11 +18,15 @@ package org.futo.inputmethod.latin.utils; import android.content.res.Resources; import android.content.res.TypedArray; +import android.graphics.Insets; import android.os.Build; import android.text.TextUtils; import android.util.DisplayMetrics; import android.util.Log; import android.util.TypedValue; +import android.view.Window; +import android.view.WindowInsets; +import android.view.WindowMetrics; import org.futo.inputmethod.annotations.UsedForTesting; import org.futo.inputmethod.latin.R; @@ -182,9 +186,14 @@ public final class ResourceUtils { return matchedAll; } - public static int getDefaultKeyboardWidth(final Resources res) { - final DisplayMetrics dm = res.getDisplayMetrics(); - return dm.widthPixels; + public static int getDefaultKeyboardWidth(final Window window, final Resources res) { + if(window != null && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)) { + WindowMetrics metrics = window.getWindowManager().getCurrentWindowMetrics(); + Insets insets = metrics.getWindowInsets().getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars() | WindowInsets.Type.displayCutout()); + + return metrics.getBounds().width() - (insets.left + insets.right); + } + return (int)(res.getConfiguration().screenWidthDp * res.getDisplayMetrics().density); } public static int getKeyboardHeight(final Resources res, final SettingsValues settingsValues) { diff --git a/tests/src/org/futo/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java b/tests/src/org/futo/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java index a192b6ba0..76e00d571 100644 --- a/tests/src/org/futo/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java +++ b/tests/src/org/futo/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java @@ -143,7 +143,7 @@ public abstract class KeyboardLayoutSetTestsBase extends AndroidTestCase { final boolean languageSwitchKeyEnabled, final boolean splitLayoutEnabled) { final Context context = getContext(); final Resources res = context.getResources(); - final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res); + final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(null, res); final int keyboardHeight = ResourceUtils.getDefaultKeyboardHeight(res); final RichInputMethodSubtype richInputMethodSubtype = RichInputMethodSubtype.getRichInputMethodSubtype(subtype);