From bfb1b56afa5c543a61da34628bcb95ea7cbe0c7e Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Fri, 13 Dec 2024 00:57:14 +0800 Subject: [PATCH] refactor: correct preedit view behavior and polish its appearance - 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 --- .../data/theme/mapper/GeneralStyleMapper.kt | 2 +- .../data/theme/mapper/LayoutStyleMapper.kt | 2 +- .../trime/data/theme/model/GeneralStyle.kt | 2 +- .../osfans/trime/data/theme/model/Layout.kt | 2 +- .../ime/composition/ComposingPopupWindow.kt | 3 +- .../trime/ime/composition/PreeditModule.kt | 35 +++++++++++++++++-- .../osfans/trime/ime/composition/PreeditUi.kt | 5 ++- .../com/osfans/trime/ime/core/InputView.kt | 1 + .../keyboard/CommonKeyboardActionListener.kt | 12 ++++--- .../ui/main/settings/ColorPickerDialog.kt | 2 ++ .../ui/main/settings/ThemePickerDialog.kt | 2 ++ 11 files changed, 56 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/osfans/trime/data/theme/mapper/GeneralStyleMapper.kt b/app/src/main/java/com/osfans/trime/data/theme/mapper/GeneralStyleMapper.kt index ea57842602..1c2acbc02c 100644 --- a/app/src/main/java/com/osfans/trime/data/theme/mapper/GeneralStyleMapper.kt +++ b/app/src/main/java/com/osfans/trime/data/theme/mapper/GeneralStyleMapper.kt @@ -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") diff --git a/app/src/main/java/com/osfans/trime/data/theme/mapper/LayoutStyleMapper.kt b/app/src/main/java/com/osfans/trime/data/theme/mapper/LayoutStyleMapper.kt index b9dc9fab09..8b5b486eba 100644 --- a/app/src/main/java/com/osfans/trime/data/theme/mapper/LayoutStyleMapper.kt +++ b/app/src/main/java/com/osfans/trime/data/theme/mapper/LayoutStyleMapper.kt @@ -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") diff --git a/app/src/main/java/com/osfans/trime/data/theme/model/GeneralStyle.kt b/app/src/main/java/com/osfans/trime/data/theme/model/GeneralStyle.kt index 3f57cd8af5..c4b42a1929 100644 --- a/app/src/main/java/com/osfans/trime/data/theme/model/GeneralStyle.kt +++ b/app/src/main/java/com/osfans/trime/data/theme/model/GeneralStyle.kt @@ -69,7 +69,7 @@ data class GeneralStyle( val symbolFont: List, val symbolTextSize: Float, val textFont: List, - val textSize: Int, + val textSize: Float, val verticalCorrection: Int, val verticalGap: Int, val longTextFont: List, diff --git a/app/src/main/java/com/osfans/trime/data/theme/model/Layout.kt b/app/src/main/java/com/osfans/trime/data/theme/model/Layout.kt index 73e832493a..31c9e99614 100644 --- a/app/src/main/java/com/osfans/trime/data/theme/model/Layout.kt +++ b/app/src/main/java/com/osfans/trime/data/theme/model/Layout.kt @@ -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, ) diff --git a/app/src/main/java/com/osfans/trime/ime/composition/ComposingPopupWindow.kt b/app/src/main/java/com/osfans/trime/ime/composition/ComposingPopupWindow.kt index 5c6e475692..472672f35a 100644 --- a/app/src/main/java/com/osfans/trime/ime/composition/ComposingPopupWindow.kt +++ b/app/src/main/java/com/osfans/trime/ime/composition/ComposingPopupWindow.kt @@ -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 diff --git a/app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt b/app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt index 815453f68b..3155f5f4cf 100644 --- a/app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt +++ b/app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt @@ -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 @@ -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) } } @@ -42,8 +69,8 @@ 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) } @@ -51,4 +78,8 @@ class PreeditModule( window.dismiss() } } + + fun onDetached() { + window.dismiss() + } } diff --git a/app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt b/app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt index 40e3f8f540..ff4812a2bb 100644 --- a/app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt +++ b/app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt @@ -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 @@ -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") @@ -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 = diff --git a/app/src/main/java/com/osfans/trime/ime/core/InputView.kt b/app/src/main/java/com/osfans/trime/ime/core/InputView.kt index 7c6471be6d..150d30b10e 100644 --- a/app/src/main/java/com/osfans/trime/ime/core/InputView.kt +++ b/app/src/main/java/com/osfans/trime/ime/core/InputView.kt @@ -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() diff --git a/app/src/main/java/com/osfans/trime/ime/keyboard/CommonKeyboardActionListener.kt b/app/src/main/java/com/osfans/trime/ime/keyboard/CommonKeyboardActionListener.kt index 57617b704b..aa11791455 100644 --- a/app/src/main/java/com/osfans/trime/ime/keyboard/CommonKeyboardActionListener.kt +++ b/app/src/main/java/com/osfans/trime/ime/keyboard/CommonKeyboardActionListener.kt @@ -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() + } } } diff --git a/app/src/main/java/com/osfans/trime/ui/main/settings/ColorPickerDialog.kt b/app/src/main/java/com/osfans/trime/ui/main/settings/ColorPickerDialog.kt index ba033e86c4..b61a26cc78 100644 --- a/app/src/main/java/com/osfans/trime/ui/main/settings/ColorPickerDialog.kt +++ b/app/src/main/java/com/osfans/trime/ui/main/settings/ColorPickerDialog.kt @@ -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 @@ -35,6 +36,7 @@ object ColorPickerDialog { currentIndex, ) { dialog, which -> scope.launch { + afterConfirm?.invoke() if (which != currentIndex) { ColorManager.setColorScheme(allIds.elementAt(which)) } diff --git a/app/src/main/java/com/osfans/trime/ui/main/settings/ThemePickerDialog.kt b/app/src/main/java/com/osfans/trime/ui/main/settings/ThemePickerDialog.kt index f992322c17..3606486ceb 100644 --- a/app/src/main/java/com/osfans/trime/ui/main/settings/ThemePickerDialog.kt +++ b/app/src/main/java/com/osfans/trime/ui/main/settings/ThemePickerDialog.kt @@ -18,6 +18,7 @@ object ThemePickerDialog { suspend fun build( scope: LifecycleCoroutineScope, context: Context, + afterConfirm: (suspend () -> Unit)? = null, ): AlertDialog { val all = withContext(Dispatchers.IO) { @@ -46,6 +47,7 @@ object ThemePickerDialog { currentIndex, ) { dialog, which -> scope.launch { + afterConfirm?.invoke() ThemeManager.setNormalTheme(all[which]) dialog.dismiss() }