Skip to content

Commit

Permalink
refactor: slightly improve the switches view
Browse files Browse the repository at this point in the history
fix: avoid potential crash when update visible switch
fix: incorrect switch state text
feat: add ripple background as visual feedback
feat: apply more theme setting to custom its appearance
  • Loading branch information
WhiredPlanck committed Aug 19, 2024
1 parent a03ec12 commit db08b6d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
21 changes: 16 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 @@ -10,6 +10,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.ViewAnimator
import androidx.lifecycle.lifecycleScope
import com.osfans.trime.core.RimeNotification.OptionNotification
import com.osfans.trime.core.SchemaItem
import com.osfans.trime.daemon.RimeSession
Expand All @@ -26,14 +27,20 @@ import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ime.dependency.InputScope
import com.osfans.trime.ime.symbol.SymbolBoardType
import com.osfans.trime.ime.window.BoardWindow
import kotlinx.coroutines.launch
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, rime: RimeSession, theme: Theme) : InputBroadcastReceiver {
class QuickBar(
private val context: Context,
private val service: TrimeInputMethodService,
private val rime: RimeSession,
private val theme: Theme,
) : InputBroadcastReceiver {
private val prefs = AppPrefs.defaultInstance()

private val showSwitchers get() = prefs.keyboard.switchesEnabled
Expand Down Expand Up @@ -146,8 +153,10 @@ class QuickBar(context: Context, service: TrimeInputMethodService, rime: RimeSes

override fun onRimeSchemaUpdated(schema: SchemaItem) {
if (alwaysUi.currentState == AlwaysUi.State.Switchers) {
SchemaManager.init(schema.id)
alwaysUi.switchesUi.setSwitches(SchemaManager.visibleSwitches)
service.lifecycleScope.launch {
SchemaManager.init(schema.id)
alwaysUi.switchesUi.setSwitches(SchemaManager.visibleSwitches)
}
}
}

Expand All @@ -161,8 +170,10 @@ class QuickBar(context: Context, service: TrimeInputMethodService, rime: RimeSes
}
}
if (alwaysUi.currentState == AlwaysUi.State.Switchers) {
SchemaManager.updateSwitchOptions()
alwaysUi.switchesUi.setSwitches(SchemaManager.visibleSwitches)
service.lifecycleScope.launch {
SchemaManager.updateSwitchOptions()
alwaysUi.switchesUi.setSwitches(SchemaManager.visibleSwitches)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package com.osfans.trime.ime.bar.ui.always.switches

import android.content.Context
import android.view.View
import android.view.ViewGroup
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.util.rippleDrawable
import splitties.dimensions.dp
import splitties.views.dsl.constraintlayout.above
import splitties.views.dsl.constraintlayout.after
Expand All @@ -24,8 +26,10 @@ import splitties.views.dsl.constraintlayout.startOfParent
import splitties.views.dsl.constraintlayout.topOfParent
import splitties.views.dsl.core.Ui
import splitties.views.dsl.core.add
import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.textView
import splitties.views.dsl.core.wrapContent
import splitties.views.horizontalPadding

class SwitchUi(override val ctx: Context, private val theme: Theme) : Ui {
var enabled: Int = -1
Expand All @@ -47,7 +51,9 @@ class SwitchUi(override val ctx: Context, private val theme: Theme) : Ui {

override val root =
constraintLayout {
layoutParams = lParams(wrapContent, wrapContent)
horizontalPadding = dp(theme.generalStyle.candidatePadding)
layoutParams = ViewGroup.LayoutParams(wrapContent, matchParent)
background = rippleDrawable(ColorManager.getColor("hilited_candidate_back_color")!!)
if (theme.generalStyle.commentOnTop) {
add(
altLabel,
Expand Down Expand Up @@ -92,9 +98,13 @@ class SwitchUi(override val ctx: Context, private val theme: Theme) : Ui {
}

fun setAltLabel(str: String) {
altLabel.text = str
if (altLabel.visibility == View.GONE) {
altLabel.visibility = View.VISIBLE
altLabel.run {
if (str.isNotEmpty()) {
text = str
if (visibility == View.GONE) visibility = View.VISIBLE
} else if (visibility != View.GONE) {
visibility = View.GONE
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ class SwitchesAdapter(private val theme: Theme) :
holder.ui.apply {
val enabled = item!!.enabled
setLabel(item.states!![enabled])
if (item.options.isNullOrEmpty()) {
val text =
val altText =
if (item.options.isNullOrEmpty()) {
item.states[1 - enabled].let {
if (showArrow) {
"$it"
} else {
it
}
}
setAltLabel(text)
}
} else {
""
}
setAltLabel(altText)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SwitchesUi(override val ctx: Context, val theme: Theme) : Ui {
layoutParams = ViewGroup.LayoutParams(matchParent, matchParent)
layoutManager = horizontalLayoutManager()
adapter = switchesAdapter
addItemDecoration(SpacesItemDecoration(dp(3)))
addItemDecoration(SpacesItemDecoration(dp(theme.generalStyle.candidateSpacing).toInt()))
}

fun setSwitches(list: List<Schema.Switch>) {
Expand Down

0 comments on commit db08b6d

Please sign in to comment.