Skip to content

Commit

Permalink
refactor: correct preedit view behavior and polish its appearance
Browse files Browse the repository at this point in the history
- Fix the view wouldn't disappear after changing theme or color on site
- Allow to custom its background color
- Add top left corner radius
- Improve its visual effect of showup
- Get alpha value as float
  • Loading branch information
WhiredPlanck committed Dec 12, 2024
1 parent 5f5912f commit bfb1b56
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class GeneralStyleMapper(

val textFont = getStringList("text_font")

val textSize = getInt("text_size")
val textSize = getFloat("text_size")

val verticalCorrection = getInt("vertical_correction")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LayoutStyleMapper(

val roundCorner = getFloat("round_corner")

val alpha = getInt("alpha")
val alpha = getFloat("alpha", 0.8f)
val elevation = getInt("elevation")
val movable = getString("movable")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ data class GeneralStyle(
val symbolFont: List<String>,
val symbolTextSize: Float,
val textFont: List<String>,
val textSize: Int,
val textSize: Float,
val verticalCorrection: Int,
val verticalGap: Int,
val longTextFont: List<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ data class Layout(
val realMargin: Int,
val spacing: Int,
val roundCorner: Float,
val alpha: Int,
val alpha: Float,
val elevation: Int,
val movable: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class ComposingPopupWindow(
theme.generalStyle.layout.border,
"border_color",
theme.generalStyle.layout.roundCorner,
theme.generalStyle.layout.alpha,
theme.generalStyle.layout.alpha
.toInt(),
),
)
width = WindowManager.LayoutParams.WRAP_CONTENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
package com.osfans.trime.ime.composition

import android.content.Context
import android.graphics.Outline
import android.graphics.Rect
import android.view.Gravity
import android.view.View
import android.view.ViewOutlineProvider
import android.view.WindowManager
import android.widget.PopupWindow
import com.osfans.trime.core.RimeProto
import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.daemon.launchOnReady
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.ime.bar.QuickBar
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
import com.osfans.trime.ime.dependency.InputScope
import me.tatarka.inject.annotations.Inject
import splitties.views.backgroundColor

@InputScope
@Inject
Expand All @@ -26,8 +32,29 @@ class PreeditModule(
rime: RimeSession,
private val bar: QuickBar,
) : InputBroadcastReceiver {
private val textBackColor = ColorManager.getColor("text_back_color")

private val topLeftCornerRadiusOutlineProvider =
object : ViewOutlineProvider() {
override fun getOutline(
view: View,
outline: Outline,
) {
val radius = theme.generalStyle.layout.roundCorner
val width = view.width
val height = view.height
val rect = Rect(-radius.toInt(), 0, width, (height + radius).toInt())
outline.setRoundRect(rect, radius)
}
}

val ui =
PreeditUi(context, theme).apply {
PreeditUi(context, theme, setupPreeditView = {
textBackColor?.let { backgroundColor = it }
}).apply {
root.alpha = theme.generalStyle.layout.alpha
root.outlineProvider = topLeftCornerRadiusOutlineProvider
root.clipToOutline = true
preedit.setOnCursorMoveListener { position ->
rime.launchOnReady { it.moveCursorPos(position) }
}
Expand All @@ -42,13 +69,17 @@ class PreeditModule(
override fun onInputContextUpdate(ctx: RimeProto.Context) {
ui.update(ctx.composition)
if (ctx.composition.length > 0) {
window.showAtLocation(bar.view, Gravity.START or Gravity.TOP, 0, 0)
val (x, y) = intArrayOf(0, 0).also { bar.view.getLocationInWindow(it) }
window.showAtLocation(bar.view, Gravity.START or Gravity.TOP, x, y)
ui.root.post {
window.update(x, y - ui.root.height, -1, -1)
}
} else {
window.dismiss()
}
}

fun onDetached() {
window.dismiss()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import android.view.MotionEvent
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.text.buildSpannedString
import com.osfans.trime.core.RimeProto
import com.osfans.trime.data.theme.ColorManager
Expand All @@ -25,6 +26,7 @@ import splitties.views.setPaddingDp
open class PreeditUi(
final override val ctx: Context,
private val theme: Theme,
private val setupPreeditView: (TextView.() -> Unit)? = null,
) : Ui {
private val textColor = ColorManager.getColor("text_color")
private val highlightTextColor = ColorManager.getColor("hilited_text_color")
Expand All @@ -34,8 +36,9 @@ open class PreeditUi(
view(::PreeditTextView) {
setPaddingDp(3, 1, 3, 1)
textColor?.let { setTextColor(it) }
textSize = theme.generalStyle.textSize.toFloat()
textSize = theme.generalStyle.textSize
typeface = FontManager.getTypeface("text_font")
setupPreeditView?.invoke(this)
}

override val root =
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ class InputView(
// implies that InputView should not be attached again after detached.
baseCallbackHandler.cancelJob()
updateWindowViewHeightJob.cancel()
preedit.onDetached()
preview.root.removeAllViews()
broadcaster.clear()
super.onDetachedFromWindow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ class CommonKeyboardActionListener(
}

private fun showThemePicker() {
showDialog {
ThemePickerDialog.build(service.lifecycleScope, context)
showDialog { api ->
ThemePickerDialog.build(service.lifecycleScope, context) {
api.commitComposition()
}
}
}

private fun showColorPicker() {
showDialog {
ColorPickerDialog.build(service.lifecycleScope, context)
showDialog { api ->
ColorPickerDialog.build(service.lifecycleScope, context) {
api.commitComposition()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object ColorPickerDialog {
suspend fun build(
scope: LifecycleCoroutineScope,
context: Context,
afterConfirm: (suspend () -> Unit)? = null,
): AlertDialog {
val all = withContext(Dispatchers.Default) { ColorManager.presetColorSchemes }
val allIds = all.keys
Expand All @@ -35,6 +36,7 @@ object ColorPickerDialog {
currentIndex,
) { dialog, which ->
scope.launch {
afterConfirm?.invoke()
if (which != currentIndex) {
ColorManager.setColorScheme(allIds.elementAt(which))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object ThemePickerDialog {
suspend fun build(
scope: LifecycleCoroutineScope,
context: Context,
afterConfirm: (suspend () -> Unit)? = null,
): AlertDialog {
val all =
withContext(Dispatchers.IO) {
Expand Down Expand Up @@ -46,6 +47,7 @@ object ThemePickerDialog {
currentIndex,
) { dialog, which ->
scope.launch {
afterConfirm?.invoke()
ThemeManager.setNormalTheme(all[which])
dialog.dismiss()
}
Expand Down

0 comments on commit bfb1b56

Please sign in to comment.