Skip to content

Commit

Permalink
fix: the keyboard overlapping with the navigation bar
Browse files Browse the repository at this point in the history
Only allow to draw keyboard view behind the navigation bar on Android 11+, otherwise just set navigation bar color.
  • Loading branch information
WhiredPlanck committed Feb 12, 2024
1 parent 7a38f5f commit a5d5119
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/com/osfans/trime/data/theme/ThemeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ object ThemeManager {
activeTheme = evalActiveTheme()
}

fun setColorScheme(name: String) {
_activeTheme.setColorScheme(name)
fireChange()
}

private fun evalActiveTheme(): Theme {
return if (prefs.autoDark) {
Theme(isNightMode)
Expand Down
48 changes: 34 additions & 14 deletions app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class InputView(
val rime: Rime,
val theme: Theme,
) : ConstraintLayout(service) {
private var shouldUpdateNavbarForeground = false
private var shouldUpdateNavbarBackground = false

private val keyboardBackground =
imageView {
scaleType = ImageView.ScaleType.CENTER_CROP
Expand Down Expand Up @@ -137,20 +140,30 @@ class InputView(
}

service.window.window!!.also { it ->
// allow draw behind navigation bar
WindowCompat.setDecorFitsSystemWindows(it, false)
it.navigationBarColor = Color.TRANSPARENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
shouldUpdateNavbarForeground = true
// allow draw behind navigation bar
WindowCompat.setDecorFitsSystemWindows(it, false)
it.navigationBarColor = Color.TRANSPARENT
// don't apply scrim to transparent navigation bar
it.isNavigationBarContrastEnforced = false
}
ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
insets.getInsets(WindowInsetsCompat.Type.navigationBars()).let {
bottomPaddingSpace.updateLayoutParams<LayoutParams> {
bottomMargin = it.bottom
ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
insets.getInsets(WindowInsetsCompat.Type.navigationBars()).let {
bottomPaddingSpace.updateLayoutParams<LayoutParams> {
bottomMargin = it.bottom
}
}
WindowInsetsCompat.CONSUMED
}
} else {
shouldUpdateNavbarForeground = true
shouldUpdateNavbarBackground = true
// don't draw behind navigation bar
WindowCompat.setDecorFitsSystemWindows(it, true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// don't apply scrim to transparent navigation bar
it.isNavigationBarContrastEnforced = false
}
WindowInsetsCompat.CONSUMED
}
}

Expand Down Expand Up @@ -259,10 +272,17 @@ class InputView(
restarting: Boolean = false,
) {
if (!restarting) {
service.window.window!!.also {
WindowCompat.getInsetsController(it, it.decorView)
.isAppearanceLightNavigationBars =
ColorUtils.isDark(theme.colors.getColor("key_text_color")!!)
if (shouldUpdateNavbarForeground || shouldUpdateNavbarBackground) {
service.window.window!!.also {
val backColor = theme.colors.getColor("back_color") ?: Color.BLACK
if (shouldUpdateNavbarForeground) {
WindowCompat.getInsetsController(it, it.decorView)
.isAppearanceLightNavigationBars = ColorUtils.isContrastedDark(backColor)
}
if (shouldUpdateNavbarBackground) {
it.navigationBarColor = backColor
}
}
}
}
keyboardWindow.oldMainInputView.mainKeyboardView.updateEnterLabelOnEditorInfo(info)
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ open class Trime : LifecycleInputMethodService() {
ThemeManager.OnThemeChangeListener {
lifecycleScope.launch(Dispatchers.Main) {
recreateInputView(it)
inputView?.startInput(currentInputEditorInfo)
}
}

Expand Down Expand Up @@ -430,10 +431,9 @@ open class Trime : LifecycleInputMethodService() {
}

override fun onCreateInputView(): View {
Timber.d("onCreateInputView()")
Timber.d("onCreateInputView")
RimeWrapper.runAfterStarted {
recreateInputView(ThemeManager.activeTheme)
Timber.d("onCreateInputView - completely ended")
}
Timber.d("onCreateInputView() finish")
return InitializationUi(this).root
Expand Down Expand Up @@ -484,7 +484,6 @@ open class Trime : LifecycleInputMethodService() {
showStatusIcon(R.drawable.ic_trime_status) // 狀態欄圖標
}
bindKeyboardToInputView()
// if (!restarting) setNavBarColor();
setCandidatesViewShown(!Rime.isEmpty) // 軟鍵盤出現時顯示候選欄
inputView?.startInput(attribute, restarting)
when (attribute.inputType and InputType.TYPE_MASK_VARIATION) {
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/com/osfans/trime/ui/main/Pickers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.osfans.trime.data.sound.SoundTheme
import com.osfans.trime.data.sound.SoundThemeManager
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.core.RimeWrapper
import com.osfans.trime.ime.core.Trime
import com.osfans.trime.ime.symbol.TabManager
import com.osfans.trime.ui.components.CoroutineChoiceDialog
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -62,10 +61,7 @@ suspend fun Context.colorPicker(
postiveDispatcher = Dispatchers.Default
onOKButton {
val schemeIds = all.map { it.first }
ThemeManager.activeTheme.setColorScheme(schemeIds[checkedItem])
launch {
Trime.getServiceOrNull()?.recreateInputView(ThemeManager.activeTheme) // 立刻重初始化生效
}
ThemeManager.setColorScheme(schemeIds[checkedItem])
}
}.create()
}
Expand Down
19 changes: 14 additions & 5 deletions app/src/main/java/com/osfans/trime/util/ColorUtils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.osfans.trime.util

import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.core.graphics.blue
import androidx.core.graphics.green
import androidx.core.graphics.red
import timber.log.Timber

object ColorUtils {
Expand Down Expand Up @@ -35,10 +39,15 @@ object ColorUtils {
}
}

fun isDark(color: Int): Boolean {
val r = Color.red(color)
val g = Color.green(color)
val b = Color.blue(color)
return (r * 0.299 + g * 0.587 + b * 0.114) < 128
/**
* 计算颜色相对亮度,如果超出 0.73,认为是亮色,否则认为是暗色
*/
fun isContrastedDark(
@ColorInt color: Int,
): Boolean {
val r = color.red / 255f
val g = color.green / 255f
val b = color.blue / 255f
return (r * 0.2126f + g * 0.7152f + b * 0.0722f) > 0.73f
}
}

0 comments on commit a5d5119

Please sign in to comment.