Skip to content

Commit

Permalink
refactor: manage bar ui with QuickBar
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 24, 2024
1 parent ae38589 commit 07a5b8d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 54 deletions.
52 changes: 52 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.osfans.trime.ime.bar

import android.content.Context
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.ViewAnimator
import com.osfans.trime.core.Rime
import com.osfans.trime.databinding.CandidateBarBinding
import com.osfans.trime.databinding.TabBarBinding
import com.osfans.trime.ime.core.Trime
import com.osfans.trime.ime.enums.SymbolKeyboardType
import splitties.views.dsl.core.add
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.matchParent

class QuickBar(private val context: Context, private val service: Trime) {
val oldCandidateBar by lazy {
CandidateBarBinding.inflate(LayoutInflater.from(context)).apply {
with(root) {
setPageStr(
{ service.handleKey(KeyEvent.KEYCODE_PAGE_DOWN, 0) },
{ service.handleKey(KeyEvent.KEYCODE_PAGE_UP, 0) },
{ service.selectLiquidKeyboard(SymbolKeyboardType.CANDIDATE) },
)
visibility = if (Rime.getOption("_hide_candidate")) View.GONE else View.VISIBLE
}
with(candidates) {
setCandidateListener(service.textInputManager)
setShowComment(!Rime.getOption("_hide_comment"))
reset()
}
}
}

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

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

val view by lazy {
ViewAnimator(context).apply {
background = oldCandidateBar.root.background
add(oldCandidateBar.root, lParams(matchParent, matchParent))
add(oldTabBar.root, lParams(matchParent, matchParent))
}
}
}
14 changes: 13 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.constraintlayout.widget.ConstraintLayout
import com.osfans.trime.R
import com.osfans.trime.databinding.MainInputLayoutBinding
import com.osfans.trime.databinding.SymbolInputLayoutBinding
import com.osfans.trime.ime.bar.QuickBar
import com.osfans.trime.util.styledFloat
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.lParams
import splitties.views.dsl.constraintlayout.topOfParent
import splitties.views.dsl.core.add
import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.withTheme
Expand All @@ -27,6 +29,7 @@ class InputView(
val service: Trime,
) : 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))
Expand All @@ -37,16 +40,25 @@ class InputView(
keyboardView =
constraintLayout {
isMotionEventSplittingEnabled = true
add(
quickBar.view,
lParams(matchParent, wrapContent) {
topOfParent()
centerHorizontally()
},
)
add(
oldMainInputView.root,
lParams(matchParent, wrapContent) {
below(quickBar.view)
centerHorizontally()
bottomOfParent()
},
)
add(
oldSymbolInputView.root,
lParams(matchParent, wrapContent) {
below(quickBar.view)
centerHorizontally()
bottomOfParent()
},
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ public void selectLiquidKeyboard(final int tabIndex) {
if (tabIndex >= 0) {
symbolInput.getLayoutParams().height = mainInput.getHeight();
symbolInput.setVisibility(View.VISIBLE);
mainInput.setVisibility(View.GONE);
inputView.getQuickBar().switchUiByIndex(1);

symbolKeyboardType = liquidKeyboard.select(tabIndex);
tabView.updateTabWidth();
Expand All @@ -333,9 +335,10 @@ public void selectLiquidKeyboard(final int tabIndex) {
// 设置液体键盘处于隐藏状态
TabManager.get().setTabExited();
symbolInput.setVisibility(View.GONE);
mainInput.setVisibility(View.VISIBLE);
inputView.getQuickBar().switchUiByIndex(0);
updateComposing();
}
mainInput.setVisibility(tabIndex >= 0 ? View.GONE : View.VISIBLE);
}

// 按键需要通过tab name来打开liquidKeyboard的指定tab
Expand Down Expand Up @@ -612,21 +615,21 @@ public View onCreateInputView() {

mainKeyboardView = inputView.getOldMainInputView().mainKeyboardView;
// 初始化候选栏
mCandidateRoot = inputView.getOldMainInputView().candidateView.getRoot();
mCandidate = inputView.getOldMainInputView().candidateView.candidates;
mCandidateRoot = inputView.getQuickBar().getOldCandidateBar().getRoot();
mCandidate = inputView.getQuickBar().getOldCandidateBar().candidates;

// 候选词悬浮窗的容器
compositionRootBinding = CompositionRootBinding.inflate(LayoutInflater.from(this));
mComposition = compositionRootBinding.compositions;
mCompositionPopupWindow.init(compositionRootBinding.compositionRoot, mCandidateRoot);
mTabRoot = inputView.getOldSymbolInputView().tabView.getRoot();
mTabRoot = inputView.getQuickBar().getOldTabBar().getRoot();

updateDarkMode();
Theme.get(darkMode).initCurrentColors(darkMode);

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

for (EventListener listener : eventListeners) {
listener.onInitializeInputUi(inputView);
Expand Down
13 changes: 5 additions & 8 deletions app/src/main/java/com/osfans/trime/ime/symbol/TabView.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public int getHightlightRight() {
}

@Override
protected void onDraw(Canvas canvas) {
if (canvas == null) return;
protected void onDraw(@NonNull Canvas canvas) {
if (tabTags == null) return;
super.onDraw(canvas);

Expand Down Expand Up @@ -165,22 +164,20 @@ public void updateTabWidth() {
computedTab.geometry = new Rect(x, 0, (int) (x + getTabWidth(i)), getHeight());
x += getTabWidth(i) + candidateSpacing;
}
LayoutParams params = getLayoutParams();
Timber.i("update, from Height=" + params.height + " width=" + params.width);
final LayoutParams params = getLayoutParams();
Timber.d("update, from Height=" + params.height + " width=" + params.width);
params.width = x;
params.height = isCommentOnTop ? candidateViewHeight + commentHeight : candidateViewHeight;
Timber.i("update, to Height=" + candidateViewHeight + " width=" + x);
Timber.d("update, reload Height=" + params.height + " width=" + params.width);
setLayoutParams(params);
params = getLayoutParams();
Timber.i("update, reload Height=" + params.height + " width=" + params.width);
invalidate();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
updateTabWidth();
Timber.i("onSizeChanged() w=" + w + ", Height=" + oldh + "=>" + h);
Timber.d("onSizeChanged() w=" + w + ", Height=" + oldh + "=>" + h);
}

@Override
Expand Down
24 changes: 1 addition & 23 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.osfans.trime.ui.main.themePicker
import com.osfans.trime.util.ShortcutUtils
import com.osfans.trime.util.startsWithAsciiChar
import kotlinx.coroutines.Job
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -66,7 +65,6 @@ class TextInputManager private constructor(private val isDarkMode: Boolean) :

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

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

Expand Down Expand Up @@ -144,23 +142,6 @@ class TextInputManager private constructor(private val isDarkMode: Boolean) :
it.setShowSymbol(!Rime.getOption("_hide_key_symbol"))
it.reset()
}
// Initialize candidate bar
candidateRoot =
inputView.oldMainInputView.candidateView.root.also {
it.setPageStr(
Runnable { trime.handleKey(KeyEvent.KEYCODE_PAGE_DOWN, 0) },
Runnable { trime.handleKey(KeyEvent.KEYCODE_PAGE_UP, 0) },
Runnable { trime.selectLiquidKeyboard(SymbolKeyboardType.CANDIDATE) },
)
it.visibility = if (Rime.getOption("_hide_candidate")) View.GONE else View.VISIBLE
}

candidateView =
inputView.oldMainInputView.candidateView.candidates.also {
it.setCandidateListener(this)
it.setShowComment(!Rime.getOption("_hide_comment"))
it.reset()
}
}

/**
Expand All @@ -170,9 +151,6 @@ class TextInputManager private constructor(private val isDarkMode: Boolean) :
intentReceiver?.unregisterReceiver(trime)
intentReceiver = null

candidateView?.setCandidateListener(null)
candidateView = null

candidateRoot = null

mainKeyboardView?.setOnKeyboardActionListener(null)
Expand Down Expand Up @@ -557,7 +535,7 @@ class TextInputManager private constructor(private val isDarkMode: Boolean) :
when (arrow) {
Candidate.PAGE_UP_BUTTON -> onKey(KeyEvent.KEYCODE_PAGE_UP, 0)
Candidate.PAGE_DOWN_BUTTON -> onKey(KeyEvent.KEYCODE_PAGE_DOWN, 0)
Candidate.PAGE_EX_BUTTON -> Trime.getService().selectLiquidKeyboard(SymbolKeyboardType.CANDIDATE)
Candidate.PAGE_EX_BUTTON -> trime.selectLiquidKeyboard(SymbolKeyboardType.CANDIDATE)
}
}

Expand Down
8 changes: 0 additions & 8 deletions app/src/main/res/layout/main_input_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include
android:id="@+id/candidate_view"
layout="@layout/candidate_bar"
app:layout_constraintBottom_toTopOf="@id/main_keyboard_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<com.osfans.trime.ime.keyboard.KeyboardView
android:id="@+id/main_keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/candidate_view"
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
9 changes: 0 additions & 9 deletions app/src/main/res/layout/symbol_input_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
android:visibility="gone"
>

<include
android:id="@+id/tab_view"
layout="@layout/tab_bar"
app:layout_constraintBottom_toTopOf="@id/liquid_keyboard_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/liquid_keyboard_view"
android:layout_width="match_parent"
Expand All @@ -24,7 +16,6 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tab_view"
/>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 07a5b8d

Please sign in to comment.