Skip to content

Commit

Permalink
refactor: complete the migration to InputView of which included compo…
Browse files Browse the repository at this point in the history
…nents
  • Loading branch information
WhiredPlanck committed Feb 2, 2024
1 parent ed4a77b commit e56142d
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 108 deletions.
54 changes: 0 additions & 54 deletions app/src/main/java/com/osfans/trime/data/theme/Theme.kt
Expand Up @@ -283,60 +283,6 @@ class Theme(private val isDarkMode: Boolean) {
}
}

lateinit var keyboardPadding: IntArray
private set

fun getKeyboardPadding(landMode: Boolean): IntArray {
Timber.i("update KeyboardPadding: getKeyboardPadding(boolean land_mode) ")
return getKeyboardPadding(oneHandMode, landMode)
}

private var oneHandMode = 0

fun getKeyboardPadding(
oneHandMode1: Int,
landMode: Boolean,
): IntArray {
keyboardPadding = IntArray(3)
this.oneHandMode = oneHandMode1
if (landMode) {
keyboardPadding[0] = dp2px(style.getFloat("keyboard_padding_land")).toInt()
keyboardPadding[1] = keyboardPadding[0]
keyboardPadding[2] = dp2px(style.getFloat("keyboard_padding_land_bottom")).toInt()
} else {
when (oneHandMode1) {
0 -> {
// 普通键盘 预留,目前未实装
keyboardPadding[0] = dp2px(style.getFloat("keyboard_padding")).toInt()
keyboardPadding[1] = keyboardPadding[0]
keyboardPadding[2] = dp2px(style.getFloat("keyboard_padding_bottom")).toInt()
}

1 -> {
// 左手键盘
keyboardPadding[0] = dp2px(style.getFloat("keyboard_padding_left")).toInt()
keyboardPadding[1] = dp2px(style.getFloat("keyboard_padding_right")).toInt()
keyboardPadding[2] = dp2px(style.getFloat("keyboard_padding_bottom")).toInt()
}

2 -> {
// 右手键盘
keyboardPadding[1] = dp2px(style.getFloat("keyboard_padding_left")).toInt()
keyboardPadding[0] = dp2px(style.getFloat("keyboard_padding_right")).toInt()
keyboardPadding[2] = dp2px(style.getFloat("keyboard_padding_bottom")).toInt()
}
}
}
Timber.d(
"update KeyboardPadding: %s %s %s one_hand_mode=%s",
keyboardPadding[0],
keyboardPadding[1],
keyboardPadding[2],
oneHandMode1,
)
return keyboardPadding
}

var hasDarkLight = false
private set

Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt
Expand Up @@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.widget.ViewAnimator
import com.osfans.trime.core.Rime
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.databinding.CandidateBarBinding
import com.osfans.trime.databinding.TabBarBinding
import com.osfans.trime.ime.core.Trime
Expand All @@ -19,6 +20,7 @@ import splitties.views.dsl.core.matchParent
class QuickBar : KoinComponent {
private val context: Context by inject()
private val service: Trime by inject()
private val theme: Theme by inject()

val oldCandidateBar by lazy {
CandidateBarBinding.inflate(LayoutInflater.from(context)).apply {
Expand All @@ -39,7 +41,9 @@ class QuickBar : KoinComponent {
}

val oldTabBar by lazy {
TabBarBinding.inflate(LayoutInflater.from(context))
TabBarBinding.inflate(LayoutInflater.from(context)).apply {
tabs.reset()
}
}

enum class State {
Expand All @@ -54,7 +58,14 @@ class QuickBar : KoinComponent {

val view by lazy {
ViewAnimator(context).apply {
background = oldCandidateBar.root.background
background =
theme.colors.getDrawable(
"candidate_background",
"candidate_border",
"candidate_border_color",
"candidate_border_round",
null,
)
add(oldCandidateBar.root, lParams(matchParent, matchParent))
add(oldTabBar.root, lParams(matchParent, matchParent))
}
Expand Down
117 changes: 116 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Expand Up @@ -2,24 +2,34 @@ package com.osfans.trime.ime.core

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.res.Configuration
import android.view.View
import android.view.WindowManager
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.updateLayoutParams
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.util.styledFloat
import org.koin.core.context.loadKoinModules
import org.koin.core.context.unloadKoinModules
import org.koin.dsl.module
import splitties.dimensions.dp
import splitties.views.dsl.constraintlayout.above
import splitties.views.dsl.constraintlayout.below
import splitties.views.dsl.constraintlayout.bottomOfParent
import splitties.views.dsl.constraintlayout.centerHorizontally
import splitties.views.dsl.constraintlayout.constraintLayout
import splitties.views.dsl.constraintlayout.endOfParent
import splitties.views.dsl.constraintlayout.endToStartOf
import splitties.views.dsl.constraintlayout.lParams
import splitties.views.dsl.constraintlayout.startOfParent
import splitties.views.dsl.constraintlayout.startToEndOf
import splitties.views.dsl.constraintlayout.topOfParent
import splitties.views.dsl.core.add
import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.view
import splitties.views.dsl.core.withTheme
import splitties.views.dsl.core.wrapContent

Expand All @@ -29,20 +39,64 @@ import splitties.views.dsl.core.wrapContent
@SuppressLint("ViewConstructor")
class InputView(
val service: Trime,
val theme: Theme,
) : ConstraintLayout(service) {
private val placeholderListener = OnClickListener { }

private val leftPaddingSpace =
view(::View) {
setOnClickListener { placeholderListener }
}

private val rightPaddingSpace =
view(::View) {
setOnClickListener { placeholderListener }
}

private val bottomPaddingSpace =
view(::View) {
setOnClickListener { placeholderListener }
}

private val themedContext = context.withTheme(android.R.style.Theme_DeviceDefault_Settings)
val quickBar = QuickBar()
val keyboardWindow = KeyboardWindow()

private val module =
module {
single { this@InputView }
single { theme }
single { themedContext }
single { service }
single { keyboardWindow }
single { quickBar }
}

private val keyboardSidePadding = theme.style.getInt("keyboard_padding")
private val keyboardSidePaddingLandscape = theme.style.getInt("keyboard_padding_land")
private val keyboardBottomPadding = theme.style.getInt("keyboard_padding_bottom")
private val keyboardBottomPaddingLandscape = theme.style.getInt("keyboard_padding_land_bottom")

private val keyboardSidePaddingPx: Int
get() {
val value =
when (resources.configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> keyboardSidePaddingLandscape
else -> keyboardSidePadding
}
return dp(value)
}

private val keyboardBottomPaddingPx: Int
get() {
val value =
when (resources.configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> keyboardBottomPaddingLandscape
else -> keyboardBottomPadding
}
return dp(value)
}

val keyboardView: View

init {
Expand All @@ -52,23 +106,49 @@ class InputView(
keyboardView =
constraintLayout {
isMotionEventSplittingEnabled = true
background = theme.colors.getDrawable("root_background")
add(
quickBar.view,
lParams(matchParent, wrapContent) {
topOfParent()
centerHorizontally()
},
)
add(
leftPaddingSpace,
lParams {
below(quickBar.view)
startOfParent()
bottomOfParent()
},
)
add(
rightPaddingSpace,
lParams {
below(quickBar.view)
endOfParent()
bottomOfParent()
},
)
add(
keyboardWindow.view,
lParams(matchParent, wrapContent) {
below(quickBar.view)
centerHorizontally()
above(bottomPaddingSpace)
},
)
add(
bottomPaddingSpace,
lParams {
startToEndOf(leftPaddingSpace)
endToStartOf(rightPaddingSpace)
bottomOfParent()
},
)
}

updateKeyboardSize()

add(
keyboardView,
lParams(matchParent, wrapContent) {
Expand All @@ -78,6 +158,41 @@ class InputView(
)
}

private fun updateKeyboardSize() {
bottomPaddingSpace.updateLayoutParams {
height = keyboardBottomPaddingPx
}
val sidePadding = keyboardSidePaddingPx
val unset = LayoutParams.UNSET
if (sidePadding == 0) {
// hide side padding space views when unnecessary
leftPaddingSpace.visibility = View.GONE
rightPaddingSpace.visibility = View.GONE
keyboardWindow.view.updateLayoutParams<LayoutParams> {
startToEnd = unset
endToStart = unset
startOfParent()
endOfParent()
}
} else {
leftPaddingSpace.visibility = View.VISIBLE
rightPaddingSpace.visibility = View.VISIBLE
leftPaddingSpace.updateLayoutParams {
width = sidePadding
}
rightPaddingSpace.updateLayoutParams {
width = sidePadding
}
keyboardWindow.view.updateLayoutParams<LayoutParams> {
startToStart = unset
endToEnd = unset
startToEndOf(leftPaddingSpace)
endToStartOf(rightPaddingSpace)
}
}
quickBar.view.setPadding(sidePadding, 0, sidePadding, 0)
}

fun switchUiByState(state: KeyboardWindow.State) {
keyboardWindow.switchUiByState(state)
quickBar.switchUiByState(QuickBar.State.entries[state.ordinal])
Expand Down

0 comments on commit e56142d

Please sign in to comment.