Skip to content

Commit

Permalink
refactor: replace TabView with new LiquidTabsUi
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Mar 31, 2024
1 parent b27b1b6 commit e360c23
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 243 deletions.
14 changes: 9 additions & 5 deletions app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import android.widget.ViewAnimator
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeNotification.OptionNotification
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.databinding.CandidateBarBinding
import com.osfans.trime.databinding.TabBarBinding
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ime.dependency.InputScope
import com.osfans.trime.ime.enums.SymbolKeyboardType
import com.osfans.trime.ime.symbol.LiquidTabsUi
import com.osfans.trime.ime.symbol.TabManager
import me.tatarka.inject.annotations.Inject
import splitties.views.dsl.core.add
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.matchParent

@InputScope
@Inject
class QuickBar(context: Context, service: TrimeInputMethodService) : InputBroadcastReceiver {
class QuickBar(context: Context, service: TrimeInputMethodService, theme: Theme) : InputBroadcastReceiver {
val oldCandidateBar by lazy {
CandidateBarBinding.inflate(LayoutInflater.from(context)).apply {
with(root) {
Expand All @@ -38,8 +40,10 @@ class QuickBar(context: Context, service: TrimeInputMethodService) : InputBroadc
}
}

val oldTabBar by lazy {
TabBarBinding.inflate(LayoutInflater.from(context))
val tabsUi by lazy {
LiquidTabsUi(context, theme).apply {
setTabs(TabManager.tabCandidates)
}
}

enum class State {
Expand Down Expand Up @@ -71,7 +75,7 @@ class QuickBar(context: Context, service: TrimeInputMethodService) : InputBroadc
"candidate_border_round",
)
add(oldCandidateBar.root, lParams(matchParent, matchParent))
add(oldTabBar.root, lParams(matchParent, matchParent))
add(tabsUi.root, lParams(matchParent, matchParent))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.broadcast.IntentReceiver
import com.osfans.trime.ime.enums.FullscreenMode
import com.osfans.trime.ime.enums.InlinePreeditMode
import com.osfans.trime.ime.enums.KeyCommandType
import com.osfans.trime.ime.enums.Keycode
import com.osfans.trime.ime.enums.SymbolKeyboardType
import com.osfans.trime.ime.keyboard.Event
Expand All @@ -63,11 +64,10 @@ import com.osfans.trime.ime.keyboard.InputFeedbackManager
import com.osfans.trime.ime.keyboard.Key
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.ime.keyboard.KeyboardView
import com.osfans.trime.ime.symbol.LiquidTabsUi
import com.osfans.trime.ime.symbol.TabManager
import com.osfans.trime.ime.symbol.TabView
import com.osfans.trime.ime.text.Candidate
import com.osfans.trime.ime.text.CompositionPopupWindow
import com.osfans.trime.ime.text.ScrollView
import com.osfans.trime.ime.text.TextInputManager
import com.osfans.trime.util.ShortcutUtils
import com.osfans.trime.util.StringUtils
Expand All @@ -89,8 +89,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
get() = AppPrefs.defaultInstance()
private var mainKeyboardView: KeyboardView? = null // 主軟鍵盤
private var mCandidate: Candidate? = null // 候選
private var mTabRoot: ScrollView? = null
private var tabView: TabView? = null
private var liquidTabsUi: LiquidTabsUi? = null
var inputView: InputView? = null
private var initializationUi: InitializationUi? = null
private var eventListeners = WeakHashSet<EventListener>()
Expand Down Expand Up @@ -272,8 +271,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
if (tabIndex >= 0) {
inputView!!.switchBoard(InputView.Board.Symbol)
symbolKeyboardType = inputView!!.liquidKeyboard.select(tabIndex)
tabView!!.updateTabWidth()
mTabRoot!!.move(tabView!!.highlightLeft, tabView!!.highlightRight)
liquidTabsUi?.activateTab(TabManager.selectedOrZero)
mCompositionPopupWindow?.composition?.compositionView?.changeToLiquidKeyboardToolbar()
showCompositionView(false)
} else {
Expand Down Expand Up @@ -321,8 +319,17 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
mainKeyboardView = inputView!!.keyboardWindow.oldMainInputView.mainKeyboardView
// 初始化候选栏
mCandidate = inputView!!.quickBar.oldCandidateBar.candidates
mTabRoot = inputView!!.quickBar.oldTabBar.root
tabView = inputView!!.quickBar.oldTabBar.tabs
liquidTabsUi =
inputView!!.quickBar.tabsUi.apply {
setOnTabClickListener {
val tag = TabManager.tabTags[it]
if (tag.type == SymbolKeyboardType.NO_KEY && tag.command == KeyCommandType.EXIT) {
selectLiquidKeyboard(-1)
} else {
selectLiquidKeyboard(it)
}
}
}

mCompositionPopupWindow =
CompositionPopupWindow(this, ThemeManager.activeTheme).apply {
Expand Down Expand Up @@ -1128,7 +1135,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
mCandidate!!.setText(0)
}
// 刷新候选词后,如果候选词超出屏幕宽度,滚动候选栏
mTabRoot?.move(mCandidate!!.highlightLeft, mCandidate!!.highlightRight)
// mTabRoot?.move(mCandidate!!.highlightLeft, mCandidate!!.highlightRight)
}
mainKeyboardView?.invalidateComposingKeys()
if (!onEvaluateInputViewShown()) setCandidatesViewShown(textInputManager!!.isComposable) // 實體鍵盤打字時顯示候選欄
Expand Down
129 changes: 129 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/symbol/LiquidTabsUi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.osfans.trime.ime.symbol

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.PaintDrawable
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.FontManager
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.ime.text.ScrollView
import com.osfans.trime.util.rippleDrawable
import splitties.dimensions.dp
import splitties.views.dsl.core.Ui
import splitties.views.dsl.core.add
import splitties.views.dsl.core.frameLayout
import splitties.views.dsl.core.horizontalLayout
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.textView
import splitties.views.dsl.core.wrapContent
import splitties.views.gravityCenter
import splitties.views.gravityCenterVertical
import splitties.views.horizontalPadding

class LiquidTabsUi(override val ctx: Context, val theme: Theme) : Ui {
inner class TabUi : Ui {
override val ctx = this@LiquidTabsUi.ctx

var position: Int = -1

val text =
textView {
textSize = theme.style.getFloat("candidate_text_size")
typeface = FontManager.getTypeface("candidate_font")
ColorManager.getColor("candidate_text_color")?.let { setTextColor(it) }
}

override val root =
frameLayout {
add(
text,
lParams {
gravity = gravityCenter
horizontalPadding = dp(theme.style.getFloat("candidate_padding")).toInt()
},
)
background = rippleDrawable(ColorManager.getColor("hilited_candidate_back_color")!!)
setOnClickListener {
onTabClick(this@TabUi)
}
}

fun setText(str: String) {
text.text = str
}

fun setActive(active: Boolean) {
val color =
if (active) {
ColorManager.getColor(
"hilited_candidate_text_color",
)!!
} else {
ColorManager.getColor("candidate_text_color")!!
}
val background = if (active) ColorManager.getColor("hilited_candidate_back_color")!! else Color.TRANSPARENT
text.setTextColor(color)
root.background = PaintDrawable(background).apply { setCornerRadius(theme.style.getFloat("layout/round_corner")) }
}
}

private var tabs: Array<TabUi> = arrayOf()
private var selected = -1

private var onTabClick: (TabUi.(Int) -> Unit)? = null

private val horizontal = horizontalLayout()

override val root =
ScrollView(ctx, null).apply {
isHorizontalScrollBarEnabled = false
add(
horizontal,
lParams(wrapContent, matchParent) {
gravity = gravityCenterVertical
},
)
}

fun setTabs(tags: List<TabTag>) {
tabs.forEach { root.removeView(it.root) }
selected = -1
tabs =
Array(tags.size) {
val tag = tags[it]
TabUi().apply {
position = it
setText(tag.text)
setActive(false)
}
}
tabs.forEach { tabUi ->
horizontal.apply {
add(
tabUi.root,
lParams(wrapContent, matchParent) {
gravity = gravityCenter
},
)
}
}
}

fun activateTab(index: Int) {
if (index == selected) return
if (selected >= 0) {
tabs[selected].setActive(false)
}
tabs[index].setActive(true)
selected = index
}

private fun onTabClick(tabUi: TabUi) {
onTabClick?.invoke(tabUi, tabUi.position)
}

fun setOnTabClickListener(listener: (TabUi.(Int) -> Unit)? = null) {
onTabClick = listener
}
}
8 changes: 3 additions & 5 deletions app/src/main/java/com/osfans/trime/ime/symbol/TabTag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ data class TabTag(
val text: String,
/** additional comment of the tab */
val comment: String,
/** position and size info of the tab */
val geometry: Rect,
/** data type of the tab (unused) */
val type: SymbolKeyboardType,
/** action type of the tab */
val command: KeyCommandType,
) {
constructor(text: String, type: SymbolKeyboardType) :
this(text, "", Rect(), type, KeyCommandType.NULL)
this(text, "", type, KeyCommandType.NULL)
constructor(text: String, comment: String, type: SymbolKeyboardType) :
this(text, comment, Rect(), type, KeyCommandType.NULL)
this(text, comment, type, KeyCommandType.NULL)
constructor(text: String, type: SymbolKeyboardType, command: KeyCommandType) :
this(text, "", Rect(), type, command)
this(text, "", type, command)
}
Loading

0 comments on commit e360c23

Please sign in to comment.