Skip to content

Commit

Permalink
refactor: restore the style for SwitchesUi
Browse files Browse the repository at this point in the history
feat: always apply `candidate_view_height` plus `comment_height` as bar view height
  • Loading branch information
WhiredPlanck committed Aug 17, 2024
1 parent 9e38094 commit 1aea201
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 12 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class QuickBar(context: Context, service: TrimeInputMethodService, rime: RimeSes

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

val themedHeight =
theme.generalStyle.candidateViewHeight + theme.generalStyle.commentHeight

private fun evalAlwaysUiState() {
val newState =
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@
package com.osfans.trime.ime.bar.ui.always.switches

import android.content.Context
import android.view.View
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.FontManager
import com.osfans.trime.data.theme.Theme
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.core.Ui
import splitties.views.dsl.core.add
import splitties.views.dsl.core.frameLayout
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.textView
import splitties.views.gravityCenter
import splitties.views.dsl.core.wrapContent

class SwitchUi(override val ctx: Context, private val theme: Theme) : Ui {
var enabled: Int = -1
Expand All @@ -25,12 +37,64 @@ class SwitchUi(override val ctx: Context, private val theme: Theme) : Ui {
ColorManager.getColor("candidate_text_color")?.let { setTextColor(it) }
}

private val altLabel =
textView {
textSize = theme.generalStyle.commentTextSize.toFloat()
typeface = FontManager.getTypeface("comment_font")
ColorManager.getColor("comment_text_color")?.let { setTextColor(it) }
visibility = View.GONE
}

override val root =
frameLayout {
add(label, lParams { gravity = gravityCenter })
constraintLayout {
layoutParams = lParams(wrapContent, wrapContent)
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()
},
)
} else {
add(
label,
lParams(wrapContent, wrapContent) {
startOfParent()
before(altLabel)
centerVertically()
},
)
add(
altLabel,
lParams(wrapContent, wrapContent) {
after(label)
centerVertically()
endOfParent()
},
)
}
}

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

fun setAltLabel(str: String) {
altLabel.text = str
if (altLabel.visibility == View.GONE) {
altLabel.visibility = View.VISIBLE
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.schema.Schema
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

inner class Holder(val ui: SwitchUi) : RecyclerView.ViewHolder(ui.root)

override fun onCreateViewHolder(
Expand All @@ -31,6 +34,17 @@ class SwitchesAdapter(private val theme: Theme) :
holder.ui.apply {
val enabled = item!!.enabled
setLabel(item.states!![enabled])
if (item.options.isNullOrEmpty()) {
val text =
item.states[1 - enabled].let {
if (showArrow) {
"$it"
} else {
it
}
}
setAltLabel(text)
}
}
}
}
2 changes: 1 addition & 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 @@ -197,7 +197,7 @@ class InputView(
)
add(
quickBar.view,
lParams(matchParent, wrapContent) {
lParams(matchParent, dp(quickBar.themedHeight)) {
topOfParent()
centerHorizontally()
},
Expand Down
7 changes: 1 addition & 6 deletions app/src/main/java/com/osfans/trime/ime/text/Candidate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,7 @@ class Candidate(
widthMeasureSpec: Int,
heightMeasureSpec: Int,
) {
val h =
if (shouldShowComment && isCommentOnTop) {
candidateViewHeight + commentHeight
} else {
candidateViewHeight
}
val h = candidateViewHeight + commentHeight
setMeasuredDimension(
MeasureSpec.makeMeasureSpec(widthMeasureSpec, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(h, MeasureSpec.AT_MOST),
Expand Down

0 comments on commit 1aea201

Please sign in to comment.