Skip to content

Commit

Permalink
feat: improve switch display
Browse files Browse the repository at this point in the history
- Improve the display just like do with candidate
- Cleanup the code tree of SwitchUi
  • Loading branch information
WhiredPlanck committed Dec 29, 2024
1 parent 1b0857a commit adbf06c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 66 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/data/prefs/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ class AppPrefs(

val softCursorEnabled by bool(SOFT_CURSOR_ENABLED, true)
val popupKeyPressEnabled = bool(POPUP_KEY_PRESS_ENABLED, false)
val switchesEnabled by bool(SWITCHES_ENABLED, true)
val switchArrowEnabled by bool(SWITCH_ARROW_ENABLED, true)
val switchesEnabled = bool(SWITCHES_ENABLED, true)
val switchArrowEnabled = bool(SWITCH_ARROW_ENABLED, true)

enum class LandscapeModeOption {
NEVER,
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/osfans/trime/data/schema/Schema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class Schema(

@Serializable
data class Switch(
val name: String? = null,
val options: List<String>? = listOf(),
val reset: Int? = null,
val states: List<String>? = listOf(),
val name: String = "",
val options: List<String> = listOf(),
val reset: Int = -1,
val states: List<String> = listOf(),
@Transient var enabled: Int = 0,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object SchemaManager {
currentSchema = runCatching { Schema(schemaId) }.getOrDefault(defaultSchema)
visibleSwitches = currentSchema.switches
?.decode(ListSerializer(Schema.Switch.serializer()))
?.filter { !it.states.isNullOrEmpty() } ?: listOf() // 剔除没有 states 条目项的值,它们不作为开关使用
?.filter { it.states.isNotEmpty() } ?: listOf() // 剔除没有 states 条目项的值,它们不作为开关使用
updateSwitchOptions()
}

Expand All @@ -33,8 +33,8 @@ object SchemaManager {
if (visibleSwitches.isEmpty()) return // 無方案
visibleSwitches.forEach { s ->
s.enabled =
if (s.options.isNullOrEmpty()) { // 只有单 Rime 运行时选项的开关,开关名即选项名,标记其启用状态
Rime.getRimeOption(s.name!!).compareTo(false)
if (s.options.isEmpty()) { // 只有单 Rime 运行时选项的开关,开关名即选项名,标记其启用状态
Rime.getRimeOption(s.name).compareTo(false)
} else { // 带有一系列 Rime 运行时选项的开关,找到启用的选项并标记
// 将启用状态标记为此选项的索引值,方便切换时直接从选项列表中获取
// 注意:有可能每个 option 的状态都为 false(未启用), 因此 indexOfFirst 可能会返回 -1,
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class QuickBar(

private val prefs = AppPrefs.defaultInstance()

private val showSwitchers get() = prefs.keyboard.switchesEnabled
private val showSwitchers by prefs.keyboard.switchesEnabled

val themedHeight =
theme.generalStyle.candidateViewHeight + theme.generalStyle.commentHeight
Expand All @@ -72,10 +72,10 @@ class QuickBar(
setOnSwitchClick { switch ->
val prevEnabled = switch.enabled
switch.enabled =
if (switch.options.isNullOrEmpty()) {
if (switch.options.isEmpty()) {
(1 - prevEnabled).also { newValue ->
rime.launchOnReady {
it.setRuntimeOption(switch.name!!, newValue == 1)
it.setRuntimeOption(switch.name, newValue == 1)
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,17 @@ 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
import splitties.views.dsl.constraintlayout.before
import splitties.views.dsl.constraintlayout.below
import splitties.views.dsl.constraintlayout.bottomOfParent
import splitties.views.dsl.constraintlayout.centerHorizontally
import splitties.views.dsl.constraintlayout.centerVertically
import splitties.views.dsl.constraintlayout.constraintLayout
import splitties.views.dsl.constraintlayout.endOfParent
import splitties.views.dsl.constraintlayout.lParams
import splitties.views.dsl.constraintlayout.startOfParent
import splitties.views.dsl.constraintlayout.topOfParent
import splitties.views.dsl.constraintlayout.horizontalChain
import splitties.views.dsl.constraintlayout.packed
import splitties.views.dsl.constraintlayout.verticalChain
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.gravityCenter
import splitties.views.horizontalPadding

class SwitchUi(
Expand All @@ -37,17 +31,20 @@ class SwitchUi(
) : Ui {
var enabled: Int = -1

private val label =
private val firstText =
textView {
textSize = theme.generalStyle.candidateTextSize.toFloat()
textSize = theme.generalStyle.candidateTextSize
typeface = FontManager.getTypeface("candidate_font")
isSingleLine = true
gravity = gravityCenter
ColorManager.getColor("candidate_text_color")?.let { setTextColor(it) }
}

private val altLabel =
private val lastText =
textView {
textSize = theme.generalStyle.commentTextSize.toFloat()
textSize = theme.generalStyle.commentTextSize
typeface = FontManager.getTypeface("comment_font")
isSingleLine = true
ColorManager.getColor("comment_text_color")?.let { setTextColor(it) }
visibility = View.GONE
}
Expand All @@ -58,50 +55,29 @@ class SwitchUi(
layoutParams = ViewGroup.LayoutParams(wrapContent, matchParent)
background = rippleDrawable(ColorManager.getColor("hilited_candidate_back_color")!!)
if (theme.generalStyle.commentOnTop) {
add(
altLabel,
lParams(wrapContent, wrapContent) {
bottomMargin = dp(-3)
topOfParent()
above(label)
centerHorizontally()
},
)
add(
label,
lParams(wrapContent, wrapContent) {
topMargin = dp(-3)
below(altLabel)
centerHorizontally()
bottomOfParent()
},
verticalChain(
listOf(lastText, firstText),
style = packed,
defaultHeight = wrapContent,
defaultWidth = wrapContent,
initParams = { centerHorizontally() },
)
} else {
add(
label,
lParams(wrapContent, wrapContent) {
startOfParent()
before(altLabel)
centerVertically()
},
)
add(
altLabel,
lParams(wrapContent, wrapContent) {
after(label)
centerVertically()
endOfParent()
},
horizontalChain(
listOf(firstText, lastText),
style = packed,
defaultWidth = wrapContent,
initParams = { centerVertically() },
)
}
}

fun setLabel(str: String) {
label.text = str
fun setFirstText(str: String) {
firstText.text = str
}

fun setAltLabel(str: String) {
altLabel.run {
fun setLastText(str: String) {
lastText.run {
if (str.isNotEmpty()) {
text = str
if (visibility == View.GONE) visibility = View.VISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.osfans.trime.data.theme.Theme
class SwitchesAdapter(
private val theme: Theme,
) : BaseQuickAdapter<Schema.Switch, SwitchesAdapter.Holder>() {
private val showArrow = AppPrefs.defaultInstance().keyboard.switchArrowEnabled
private val showArrow by AppPrefs.defaultInstance().keyboard.switchArrowEnabled

inner class Holder(
val ui: SwitchUi,
Expand All @@ -34,9 +34,9 @@ class SwitchesAdapter(
) {
holder.ui.apply {
val enabled = item!!.enabled
setLabel(item.states!![enabled])
setFirstText(item.states[enabled])
val altText =
if (item.options.isNullOrEmpty()) {
if (item.options.isEmpty()) {
item.states[1 - enabled].let {
if (showArrow) {
"$it"
Expand All @@ -47,7 +47,7 @@ class SwitchesAdapter(
} else {
""
}
setAltLabel(altText)
setLastText(altText)
}
}
}

0 comments on commit adbf06c

Please sign in to comment.