Skip to content

Commit

Permalink
fix: make sure the window view height can always follow current keybo…
Browse files Browse the repository at this point in the history
…ard height
  • Loading branch information
WhiredPlanck committed Nov 25, 2024
1 parent 51424b2 commit 9e1969b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.content.Context
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RectShape
import android.view.View
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.lifecycle.lifecycleScope
Expand All @@ -28,7 +27,6 @@ import com.osfans.trime.ime.candidates.unrolled.CandidatesPagingSource
import com.osfans.trime.ime.candidates.unrolled.PagingCandidateViewAdapter
import com.osfans.trime.ime.candidates.unrolled.UnrolledCandidateLayout
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.ime.keyboard.KeyboardWindow
import com.osfans.trime.ime.window.BoardWindow
import com.osfans.trime.ime.window.BoardWindowManager
Expand Down Expand Up @@ -91,9 +89,6 @@ abstract class BaseUnrolledCandidateWindow(
private var candidatesSubmitJob: Job? = null

override fun onAttached() {
candidateLayout.updateLayoutParams {
height = KeyboardSwitcher.currentKeyboard.keyboardHeight
}
lifecycleCoroutineScope = candidateLayout.findViewTreeLifecycleOwner()!!.lifecycleScope
bar.unrollButtonStateMachine.push(UnrollButtonStateMachine.TransitionEvent.UnrolledCandidatesAttached)
offsetJob =
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ 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.matchConstraints
import splitties.views.dsl.constraintlayout.startOfParent
import splitties.views.dsl.constraintlayout.startToEndOf
import splitties.views.dsl.constraintlayout.topOfParent
Expand Down Expand Up @@ -98,6 +97,7 @@ class InputView(
}

private val callbackHandlerJob: Job
private val updateWindowViewHeightJob: Job

private val themedContext = context.withTheme(android.R.style.Theme_DeviceDefault_Settings)
private val inputComponent = InputComponent::class.create(this, themedContext, theme, service, rime)
Expand Down Expand Up @@ -230,7 +230,7 @@ class InputView(
)
add(
windowManager.view,
lParams(matchConstraints, wrapContent) {
lParams {
below(quickBar.view)
above(bottomPaddingSpace)
},
Expand All @@ -245,6 +245,15 @@ class InputView(
)
}

updateWindowViewHeightJob =
service.lifecycleScope.launch {
keyboardWindow.currentKeyboardHeight.collect {
windowManager.view.updateLayoutParams {
height = it
}
}
}

updateKeyboardSize()

add(
Expand Down Expand Up @@ -400,6 +409,7 @@ class InputView(
// cancel the notification job and clear all broadcast receivers,
// implies that InputView should not be attached again after detached.
callbackHandlerJob.cancel()
updateWindowViewHeightJob.cancel()
broadcaster.clear()
super.onDetachedFromWindow()
}
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import com.osfans.trime.ime.keyboard.KeyboardPrefs.isLandscapeMode
import com.osfans.trime.ime.window.BoardWindow
import com.osfans.trime.ime.window.BoardWindowManager
import com.osfans.trime.ime.window.ResidentWindow
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.runBlocking
import me.tatarka.inject.annotations.Inject
import splitties.views.dsl.core.add
import splitties.views.dsl.core.frameLayout
Expand Down Expand Up @@ -59,6 +63,14 @@ class KeyboardWindow(

val mainKeyboardView by lazy { KeyboardView(context, theme) }

private val _currentKeyboardHeight =
MutableSharedFlow<Int>(
replay = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)

val currentKeyboardHeight = _currentKeyboardHeight.asSharedFlow()

private lateinit var keyboardView: FrameLayout

companion object : ResidentWindow.Key
Expand Down Expand Up @@ -89,6 +101,9 @@ class KeyboardWindow(
currentKeyboardId = target
lastKeyboardId = target
currentKeyboard?.let {
runBlocking {
_currentKeyboardHeight.emit(it.keyboardHeight)
}
if (it.isLock) lastLockKeyboardId = target
dispatchCapsState(it::setShifted)
if (Rime.isAsciiMode != it.currentAsciiMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.content.Context
import android.view.KeyEvent
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
Expand All @@ -27,7 +26,6 @@ import com.osfans.trime.data.theme.Theme
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ime.dependency.InputScope
import com.osfans.trime.ime.keyboard.CommonKeyboardActionListener
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.ime.keyboard.KeyboardWindow
import com.osfans.trime.ime.window.BoardWindow
import com.osfans.trime.ime.window.BoardWindowManager
Expand Down Expand Up @@ -120,9 +118,6 @@ class LiquidKeyboard(
override fun onCreateBarView() = liquidLayout.tabsUi.root

override fun onAttached() {
liquidLayout.updateLayoutParams {
height = KeyboardSwitcher.currentKeyboard.keyboardHeight
}
// 注册剪贴板更新监听器
ClipboardHelper.addOnUpdateListener(this)
}
Expand Down

0 comments on commit 9e1969b

Please sign in to comment.