Skip to content

Commit

Permalink
refactor: manage keyboard ui with KeyboardWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 24, 2024
1 parent 07a5b8d commit 879c7f5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 40 deletions.
23 changes: 8 additions & 15 deletions app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package com.osfans.trime.ime.core

import android.annotation.SuppressLint
import android.app.Dialog
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.constraintlayout.widget.ConstraintLayout
import com.osfans.trime.databinding.MainInputLayoutBinding
import com.osfans.trime.databinding.SymbolInputLayoutBinding
import com.osfans.trime.ime.bar.QuickBar
import com.osfans.trime.ime.keyboard.KeyboardWindow
import com.osfans.trime.util.styledFloat
import splitties.views.dsl.constraintlayout.below
import splitties.views.dsl.constraintlayout.bottomOfParent
Expand All @@ -30,9 +28,7 @@ class InputView(
) : ConstraintLayout(service) {
private val themedContext = context.withTheme(android.R.style.Theme_DeviceDefault_Settings)
val quickBar = QuickBar(context, service)

val oldMainInputView = MainInputLayoutBinding.inflate(LayoutInflater.from(context))
val oldSymbolInputView = SymbolInputLayoutBinding.inflate(LayoutInflater.from(context))
val keyboardWindow = KeyboardWindow(context, service)

val keyboardView: View

Expand All @@ -48,15 +44,7 @@ class InputView(
},
)
add(
oldMainInputView.root,
lParams(matchParent, wrapContent) {
below(quickBar.view)
centerHorizontally()
bottomOfParent()
},
)
add(
oldSymbolInputView.root,
keyboardWindow.view,
lParams(matchParent, wrapContent) {
below(quickBar.view)
centerHorizontally()
Expand All @@ -74,6 +62,11 @@ class InputView(
)
}

fun switchUiByIndex(index: Int) {
keyboardWindow.switchUiByIndex(index)
quickBar.switchUiByIndex(index)
}

private var showingDialog: Dialog? = null

fun showDialog(dialog: Dialog) {
Expand Down
19 changes: 5 additions & 14 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.PathUtils;
import com.osfans.trime.BuildConfig;
import com.osfans.trime.R;
Expand Down Expand Up @@ -316,13 +315,8 @@ public void inputSymbol(final String text) {

public void selectLiquidKeyboard(final int tabIndex) {
if (inputView == null) return;
final View symbolInput = inputView.getOldSymbolInputView().getRoot();
final View mainInput = inputView.getOldMainInputView().getRoot();
if (tabIndex >= 0) {
symbolInput.getLayoutParams().height = mainInput.getHeight();
symbolInput.setVisibility(View.VISIBLE);
mainInput.setVisibility(View.GONE);
inputView.getQuickBar().switchUiByIndex(1);
inputView.switchUiByIndex(1);

symbolKeyboardType = liquidKeyboard.select(tabIndex);
tabView.updateTabWidth();
Expand All @@ -334,9 +328,7 @@ public void selectLiquidKeyboard(final int tabIndex) {
symbolKeyboardType = SymbolKeyboardType.NO_KEY;
// 设置液体键盘处于隐藏状态
TabManager.get().setTabExited();
symbolInput.setVisibility(View.GONE);
mainInput.setVisibility(View.VISIBLE);
inputView.getQuickBar().switchUiByIndex(0);
inputView.switchUiByIndex(0);
updateComposing();
}
}
Expand Down Expand Up @@ -448,8 +440,7 @@ public void resetCandidate() {
/** 重置鍵盤、候選條、狀態欄等 !!注意,如果其中調用Rime.setOption,切換方案會卡住 */
private void reset() {
if (inputView == null) return;
inputView.getOldSymbolInputView().getRoot().setVisibility(View.GONE);
inputView.getOldMainInputView().getRoot().setVisibility(View.VISIBLE);
inputView.switchUiByIndex(0);
loadConfig();
updateDarkMode();
final Theme theme = Theme.get(darkMode);
Expand Down Expand Up @@ -613,7 +604,7 @@ public View onCreateInputView() {
() -> {
inputView = new InputView(this);

mainKeyboardView = inputView.getOldMainInputView().mainKeyboardView;
mainKeyboardView = inputView.getKeyboardWindow().getOldMainInputView().mainKeyboardView;
// 初始化候选栏
mCandidateRoot = inputView.getQuickBar().getOldCandidateBar().getRoot();
mCandidate = inputView.getQuickBar().getOldCandidateBar().candidates;
Expand All @@ -628,7 +619,7 @@ public View onCreateInputView() {
Theme.get(darkMode).initCurrentColors(darkMode);

liquidKeyboard.setKeyboardView(
(RecyclerView) inputView.getOldSymbolInputView().liquidKeyboardView);
inputView.getKeyboardWindow().getOldSymbolInputView().liquidKeyboardView);
tabView = inputView.getQuickBar().getOldTabBar().tabs;

for (EventListener listener : eventListeners) {
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardWindow.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.osfans.trime.ime.keyboard

import android.content.Context
import android.view.LayoutInflater
import android.widget.ViewAnimator
import com.osfans.trime.core.Rime
import com.osfans.trime.databinding.MainInputLayoutBinding
import com.osfans.trime.databinding.SymbolInputLayoutBinding
import com.osfans.trime.ime.core.Trime
import splitties.views.dsl.core.add
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.matchParent

class KeyboardWindow(private val context: Context, private val service: Trime) {
val oldMainInputView by lazy {
MainInputLayoutBinding.inflate(LayoutInflater.from(context)).apply {
with(mainKeyboardView) {
onKeyboardActionListener = service.textInputManager
setShowHint(!Rime.getOption("_hide_key_hint"))
setShowSymbol(!Rime.getOption("_hide_key_symbol"))
reset()
}
}
}

val oldSymbolInputView by lazy {
SymbolInputLayoutBinding.inflate(LayoutInflater.from(context))
}

fun switchUiByIndex(index: Int) {
if (view.displayedChild == index) return
view.displayedChild = index
}

val view by lazy {
ViewAnimator(context).apply {
add(oldMainInputView.root, lParams(matchParent, matchParent))
add(oldSymbolInputView.root, lParams(matchParent, matchParent))
}
}
}
10 changes: 2 additions & 8 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class TextInputManager private constructor(private val isDarkMode: Boolean) :
private var rimeNotiHandlerJob: Job? = null

private var mainKeyboardView: KeyboardView? = null
var candidateRoot: ScrollView? = null
private var candidateRoot: ScrollView? = null

val locales = Array(2) { Locale.getDefault() }

Expand Down Expand Up @@ -135,13 +135,7 @@ class TextInputManager private constructor(private val isDarkMode: Boolean) :
override fun onInitializeInputUi(inputView: InputView) {
super.onInitializeInputUi(inputView)
// Initialize main keyboard view
mainKeyboardView =
inputView.oldMainInputView.mainKeyboardView.also {
it.setOnKeyboardActionListener(this)
it.setShowHint(!Rime.getOption("_hide_key_hint"))
it.setShowSymbol(!Rime.getOption("_hide_key_symbol"))
it.reset()
}
mainKeyboardView = inputView.keyboardWindow.oldMainInputView.mainKeyboardView
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/main_input_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
android:id="@+id/main_keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/layout/symbol_input_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
android:id="@+id/symbol_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
>
android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/liquid_keyboard_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:backgroundTint="#FF00FF"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down

0 comments on commit 879c7f5

Please sign in to comment.