Skip to content

Commit

Permalink
feat: reveal the keyboard background when navigation bar is visible
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Feb 9, 2024
1 parent 130ed31 commit 3f13fa8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/osfans/trime/data/theme/Theme.kt
Expand Up @@ -19,6 +19,7 @@ package com.osfans.trime.data.theme

import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import androidx.annotation.ColorInt
import androidx.core.math.MathUtils
import com.osfans.trime.core.Rime
import com.osfans.trime.data.AppPrefs
Expand Down Expand Up @@ -171,11 +172,13 @@ class Theme(private var isDarkMode: Boolean) {
}

// API 2.0
@ColorInt
fun getColor(key: String?): Int? {
val o = theme.currentColors[key]
return if (o is Int) o else null
}

@ColorInt
fun getColor(
m: Map<String, Any?>,
key: String?,
Expand Down
36 changes: 35 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Expand Up @@ -3,11 +3,16 @@ package com.osfans.trime.ime.core
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.res.Configuration
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.View.OnClickListener
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import com.osfans.trime.core.Rime
Expand All @@ -16,6 +21,7 @@ import com.osfans.trime.data.theme.Theme
import com.osfans.trime.ime.bar.QuickBar
import com.osfans.trime.ime.keyboard.KeyboardWindow
import com.osfans.trime.ime.symbol.LiquidKeyboard
import com.osfans.trime.util.ColorUtils
import com.osfans.trime.util.styledFloat
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -122,6 +128,24 @@ 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) {
// 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
}
}
WindowInsetsCompat.CONSUMED
}
}

liquidKeyboard.setKeyboardView(keyboardWindow.oldSymbolInputView.liquidKeyboardView)

keyboardView =
Expand Down Expand Up @@ -214,7 +238,17 @@ class InputView(
quickBar.view.setPadding(sidePadding, 0, sidePadding, 0)
}

fun startInput(info: EditorInfo) {
fun startInput(
info: EditorInfo,
restarting: Boolean = false,
) {
if (!restarting) {
service.window.window!!.also {
WindowCompat.getInsetsController(it, it.decorView)
.isAppearanceLightNavigationBars =
ColorUtils.isDark(theme.colors.getColor("key_text_color")!!)
}
}
keyboardWindow.oldMainInputView.mainKeyboardView.updateEnterLabelOnEditorInfo(info)
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/ime/core/Trime.kt
Expand Up @@ -492,7 +492,7 @@ open class Trime : LifecycleInputMethodService() {
bindKeyboardToInputView()
// if (!restarting) setNavBarColor();
setCandidatesViewShown(!Rime.isEmpty) // 軟鍵盤出現時顯示候選欄
inputView?.startInput(attribute)
inputView?.startInput(attribute, restarting)
when (attribute.inputType and InputType.TYPE_MASK_VARIATION) {
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
InputType.TYPE_TEXT_VARIATION_PASSWORD,
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/osfans/trime/util/ColorUtils.kt
Expand Up @@ -34,4 +34,11 @@ object ColorUtils {
null
}
}

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 comments on commit 3f13fa8

Please sign in to comment.