Add option for keyboard height percent base

- resources.displayMetrics.heightPixels is unstable on some devices
- maybe we should change the semantics of "height percent" to physical screen size
This commit is contained in:
Rocka 2026-07-26 13:31:34 +08:00
parent 7a3ea861c8
commit 461b4e4fb2
No known key found for this signature in database
GPG Key ID: 28031158FFDD6853
4 changed files with 48 additions and 1 deletions

View File

@ -16,6 +16,7 @@ import org.fcitx.fcitx5.android.input.candidates.expanded.ExpandedCandidateStyle
import org.fcitx.fcitx5.android.input.candidates.floating.FloatingCandidatesMode
import org.fcitx.fcitx5.android.input.candidates.floating.FloatingCandidatesOrientation
import org.fcitx.fcitx5.android.input.candidates.horizontal.HorizontalCandidateMode
import org.fcitx.fcitx5.android.input.keyboard.KeyboardHeightPercentBase
import org.fcitx.fcitx5.android.input.keyboard.LangSwitchBehavior
import org.fcitx.fcitx5.android.input.keyboard.SpaceLongPressBehavior
import org.fcitx.fcitx5.android.input.keyboard.SwipeSymbolDirection
@ -50,6 +51,11 @@ class AppPrefs(private val sharedPreferences: SharedPreferences) {
val ignoreSystemWindowInsets = switch(
R.string.ignore_system_window_insets, "ignore_system_window_insets", false
)
val keyboardHeightPercentBase = enumList(
R.string.keyboard_height_percent_base,
"keyboard_height_percent_base",
KeyboardHeightPercentBase.DisplayMetrics
)
}
inner class Keyboard : ManagedPreferenceCategory(R.string.virtual_keyboard, sharedPreferences) {

View File

@ -7,6 +7,7 @@ package org.fcitx.fcitx5.android.input
import android.annotation.SuppressLint
import android.content.res.Configuration
import android.graphics.Point
import android.os.Build
import android.view.View
import android.view.WindowInsets
@ -31,6 +32,8 @@ import org.fcitx.fcitx5.android.input.broadcast.PunctuationComponent
import org.fcitx.fcitx5.android.input.broadcast.ReturnKeyDrawableComponent
import org.fcitx.fcitx5.android.input.candidates.horizontal.HorizontalCandidateComponent
import org.fcitx.fcitx5.android.input.keyboard.CommonKeyActionListener
import org.fcitx.fcitx5.android.input.keyboard.KeyboardHeightPercentBase.DisplayMetrics
import org.fcitx.fcitx5.android.input.keyboard.KeyboardHeightPercentBase.RealSize
import org.fcitx.fcitx5.android.input.keyboard.KeyboardWindow
import org.fcitx.fcitx5.android.input.picker.emojiPicker
import org.fcitx.fcitx5.android.input.picker.emoticonPicker
@ -39,6 +42,7 @@ import org.fcitx.fcitx5.android.input.popup.PopupComponent
import org.fcitx.fcitx5.android.input.preedit.PreeditComponent
import org.fcitx.fcitx5.android.input.wm.InputWindowManager
import org.fcitx.fcitx5.android.utils.unset
import org.fcitx.fcitx5.android.utils.windowManager
import org.mechdancer.dependency.DynamicScope
import org.mechdancer.dependency.manager.wrapToUniqueComponent
import org.mechdancer.dependency.plusAssign
@ -61,6 +65,7 @@ import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.view
import splitties.views.dsl.core.wrapContent
import splitties.views.imageDrawable
import timber.log.Timber
@SuppressLint("ViewConstructor")
class InputView(
@ -136,6 +141,9 @@ class InputView(
private val keyboardBottomPadding = keyboardPrefs.keyboardBottomPadding
private val keyboardBottomPaddingLandscape = keyboardPrefs.keyboardBottomPaddingLandscape
private val advancedPrefs = AppPrefs.getInstance().advanced
private val keyboardHeightPercentBase = advancedPrefs.keyboardHeightPercentBase
private val keyboardSizePrefs = listOf(
keyboardHeightPercent,
keyboardHeightPercentLandscape,
@ -143,15 +151,29 @@ class InputView(
keyboardSidePaddingLandscape,
keyboardBottomPadding,
keyboardBottomPaddingLandscape,
keyboardHeightPercentBase,
)
private val keyboardHeightPx: Int
get() {
val baseType = keyboardHeightPercentBase.getValue()
val base = when (baseType) {
DisplayMetrics -> resources.displayMetrics.heightPixels
RealSize -> Point().also {
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.display
} else {
context.windowManager.defaultDisplay
}.getRealSize(it)
}.y
}
val percent = when (resources.configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> keyboardHeightPercentLandscape
else -> keyboardHeightPercent
}.getValue()
return resources.displayMetrics.heightPixels * percent / 100
Timber.d("keyboardHeightPx get(): baseType=${baseType}, base=${base}, percent=${percent}")
return base * percent / 100
}
private val keyboardSidePaddingPx: Int
@ -255,6 +277,7 @@ class InputView(
})
keyboardPrefs.registerOnChangeListener(onKeyboardSizeChangeListener)
advancedPrefs.registerOnChangeListener(onKeyboardSizeChangeListener)
}
private fun updateKeyboardSize() {
@ -360,6 +383,7 @@ class InputView(
}
override fun onDetachedFromWindow() {
advancedPrefs.unregisterOnChangeListener(onKeyboardSizeChangeListener)
keyboardPrefs.unregisterOnChangeListener(onKeyboardSizeChangeListener)
// clear DynamicScope, implies that InputView should not be attached again after detached.
scope.clear()

View File

@ -0,0 +1,14 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* SPDX-FileCopyrightText: Copyright 2026 Fcitx5 for Android Contributors
*/
package org.fcitx.fcitx5.android.input.keyboard
import org.fcitx.fcitx5.android.R
import org.fcitx.fcitx5.android.data.prefs.ManagedPreferenceEnum
enum class KeyboardHeightPercentBase(override val stringRes: Int) : ManagedPreferenceEnum {
DisplayMetrics(R.string.display_metrics),
RealSize(R.string.real_size)
}

View File

@ -318,4 +318,7 @@
<string name="key_border_stroke">Enable stroke for key border</string>
<string name="preferred_voice_input">Preferred voice input</string>
<string name="edit_text_playground">EditText Playground</string>
<string name="keyboard_height_percent_base">Keyboard height percent base</string>
<string name="display_metrics">Display metrics (Resources.getDisplayMetrics)</string>
<string name="real_size">Real size (Display.getRealSize)</string>
</resources>