Skip to content

Commit

Permalink
refactors: enhance the process of theme switching
Browse files Browse the repository at this point in the history
refactors: add id for some view for the convenience of debugging
  • Loading branch information
WhiredPlanck committed Oct 7, 2024
1 parent e5acfb2 commit 6817f1e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 25 deletions.
3 changes: 0 additions & 3 deletions app/src/main/java/com/osfans/trime/data/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package com.osfans.trime.data.theme

import com.osfans.trime.core.Rime
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.theme.mapper.GeneralStyleMapper
import com.osfans.trime.data.theme.model.GeneralStyle
import com.osfans.trime.util.config.Config
Expand All @@ -32,7 +31,6 @@ class Theme(
private set

companion object {
private val prefs = AppPrefs.defaultInstance().theme
private const val VERSION_KEY = "config_version"
private const val DEFAULT_THEME_NAME = "trime"

Expand Down Expand Up @@ -70,7 +68,6 @@ class Theme(
presetColorSchemes = config.getMap("preset_color_schemes")
presetKeyboards = config.getMap("preset_keyboards")
Timber.i("The theme is initialized")
prefs.selectedTheme = name
}

private fun mapToGeneralStyle(config: Config): GeneralStyle {
Expand Down
40 changes: 34 additions & 6 deletions app/src/main/java/com/osfans/trime/data/theme/ThemeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.annotation.Keep
import com.osfans.trime.data.base.DataManager
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.ime.symbol.TabManager
import com.osfans.trime.util.WeakHashSet
import java.io.File

object ThemeManager {
Expand Down Expand Up @@ -50,23 +51,50 @@ object ThemeManager {
setNormalTheme(prefs.selectedTheme)
}

lateinit var activeTheme: Theme
private set
private lateinit var _activeTheme: Theme

var activeTheme: Theme
get() = _activeTheme
set(value) {
if (::_activeTheme.isInitialized && _activeTheme == value) return
_activeTheme = value
fireChange()
}

private val onChangeListeners = WeakHashSet<OnThemeChangeListener>()

fun addOnChangedListener(listener: OnThemeChangeListener) {
onChangeListeners.add(listener)
}

fun removeOnChangedListener(listener: OnThemeChangeListener) {
onChangeListeners.remove(listener)
}

private fun fireChange() {
onChangeListeners.forEach { it.onThemeChange(_activeTheme) }
}

private val prefs = AppPrefs.defaultInstance().theme

fun init() = setNormalTheme(prefs.selectedTheme)
fun init() {
Theme(prefs.selectedTheme).let {
EventManager.resetCache()
FontManager.resetCache(it)
ColorManager.resetCache(it)
TabManager.resetCache(it)
_activeTheme = it
}
}

fun setNormalTheme(name: String) {
Theme(name).let {
if (::activeTheme.isInitialized) {
if (activeTheme == it) return
}
EventManager.resetCache()
FontManager.resetCache(it)
ColorManager.resetCache(it)
TabManager.resetCache(it)
activeTheme = it
}
prefs.selectedTheme = name
}
}
3 changes: 2 additions & 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 @@ -51,6 +51,7 @@ import splitties.views.dsl.constraintlayout.constraintLayout
import splitties.views.dsl.constraintlayout.endOfParent
import splitties.views.dsl.constraintlayout.endToStartOf
import splitties.views.dsl.constraintlayout.lParams
import splitties.views.dsl.constraintlayout.matchConstraints
import splitties.views.dsl.constraintlayout.startOfParent
import splitties.views.dsl.constraintlayout.startToEndOf
import splitties.views.dsl.constraintlayout.topOfParent
Expand Down Expand Up @@ -236,7 +237,7 @@ class InputView(
)
add(
windowManager.view,
lParams(matchParent, wrapContent) {
lParams(matchConstraints, wrapContent) {
below(quickBar.view)
above(bottomPaddingSpace)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import android.view.inputmethod.ExtractedTextRequest
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.annotation.Keep
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -76,8 +77,6 @@ import splitties.systemservices.inputMethodManager
import splitties.views.gravityBottom
import timber.log.Timber
import java.util.Locale
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

/** [輸入法][InputMethodService]主程序 */

Expand All @@ -102,6 +101,12 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
var lastCommittedText: CharSequence = ""
private set

@Keep
private val onThemeChangeListener =
ThemeManager.OnThemeChangeListener {
recreateInputView(it)
}

@Keep
private val onColorChangeListener =
ColorManager.OnColorChangeListener {
Expand All @@ -111,11 +116,10 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}

private fun postJob(
ctx: CoroutineContext,
scope: CoroutineScope,
block: suspend () -> Unit,
): Job {
val job = scope.launch(ctx, CoroutineStart.LAZY) { block() }
val job = scope.launch(start = CoroutineStart.LAZY) { block() }
jobs.trySend(job)
return job
}
Expand All @@ -127,10 +131,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
* subsequent operations can start if the prior operation is not finished (suspended),
* [postRimeJob] ensures that operations are executed sequentially.
*/
fun postRimeJob(
ctx: CoroutineContext = EmptyCoroutineContext,
block: suspend RimeApi.() -> Unit,
) = postJob(ctx, rime.lifecycleScope) { rime.runOnReady(block) }
fun postRimeJob(block: suspend RimeApi.() -> Unit) = postJob(rime.lifecycleScope) { rime.runOnReady(block) }

override fun onWindowShown() {
super.onWindowShown()
Expand Down Expand Up @@ -222,6 +223,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
handleRimeResponse(it)
}
}
ThemeManager.addOnChangedListener(onThemeChangeListener)
ColorManager.addOnChangedListener(onColorChangeListener)
super.onCreate()
instance = this
Expand Down Expand Up @@ -324,6 +326,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
mIntentReceiver = null
InputFeedbackManager.destroy()
inputView = null
ThemeManager.removeOnChangedListener(onThemeChangeListener)
ColorManager.removeOnChangedListener(onColorChangeListener)
super.onDestroy()
RimeDaemon.destroySession(javaClass.name)
Expand Down Expand Up @@ -413,8 +416,10 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {

override fun onCreateInputView(): View {
Timber.d("onCreateInputView")
postRimeJob(Dispatchers.Main) {
recreateInputView(ThemeManager.activeTheme)
postRimeJob {
ContextCompat.getMainExecutor(this@TrimeInputMethodService).execute {
recreateInputView(ThemeManager.activeTheme)
}
}
initializationUi = InitializationUi(this)
return initializationUi!!.root
Expand Down Expand Up @@ -460,7 +465,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
restarting: Boolean,
) {
Timber.d("onStartInputView: restarting=%s", restarting)
postRimeJob(Dispatchers.Main) {
postRimeJob {
InputFeedbackManager.loadSoundEffects(this@TrimeInputMethodService)
InputFeedbackManager.resetPlayProgress()
isComposable =
Expand All @@ -477,8 +482,9 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
if (prefs.other.showStatusBarIcon) {
showStatusIcon(R.drawable.ic_trime_status) // 狀態欄圖標
}
setCandidatesViewShown(!rime.run { isEmpty() }) // 軟鍵盤出現時顯示候選欄
inputView?.startInput(attribute, restarting)
ContextCompat.getMainExecutor(this@TrimeInputMethodService).execute {
inputView?.startInput(attribute, restarting)
}
when (attribute.inputType and InputType.TYPE_MASK_VARIATION) {
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
InputType.TYPE_TEXT_VARIATION_PASSWORD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.osfans.trime.R
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeNotification.OptionNotification
import com.osfans.trime.daemon.RimeSession
Expand Down Expand Up @@ -76,7 +77,7 @@ class KeyboardWindow(
private val currentKeyboard: Keyboard? get() = keyboardsCached[currentKeyboardId]

override fun onCreateView(): View {
keyboardView = context.frameLayout()
keyboardView = context.frameLayout(R.id.keyboard_view)
attachKeyboard(evalKeyboard(".default"))
keyboardView.apply { add(mainKeyboardView, lParams(matchParent, matchParent)) }
return keyboardView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.widget.FrameLayout
import androidx.transition.Transition
import androidx.transition.TransitionManager
import androidx.transition.TransitionSet
import com.osfans.trime.R
import com.osfans.trime.ime.broadcast.InputBroadcaster
import com.osfans.trime.ime.dependency.InputScope
import me.tatarka.inject.annotations.Inject
Expand Down Expand Up @@ -60,6 +61,7 @@ class BoardWindowManager(
throw IllegalStateException("${window.key} is already occupied")
}
}
broadcaster.addReceiver(window)
val view = if (createView) window.onCreateView() else null
cachedResidentWindows[window.key] = window to view
}
Expand Down Expand Up @@ -111,7 +113,7 @@ class BoardWindowManager(
broadcaster.onWindowAttached(window)
}

val view: FrameLayout by lazy { context.frameLayout() }
val view: FrameLayout by lazy { context.frameLayout(R.id.input_window) }

fun isAttached(window: BoardWindow) = currentWindow === window
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/input_view_ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ SPDX-License-Identifier: GPL-3.0-or-later
<resources>
<item name="candidate_view" type="id" />
<item name="unrolled_candidate_view" type="id" />
<item name="input_window" type="id" />
<item name="keyboard_view" type="id" />
</resources>

0 comments on commit 6817f1e

Please sign in to comment.