Skip to content

Commit

Permalink
feat: add runtime option setter and getter to new api interface
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Aug 17, 2024
1 parent bfe66a4 commit 9e38094
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
13 changes: 13 additions & 0 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ class Rime : RimeApi, RimeLifecycleOwner {
updateContext()
}

override suspend fun setRuntimeOption(
option: String,
value: Boolean,
): Unit =
withRimeContext {
setRimeOption(option, value)
}

override suspend fun getRuntimeOption(option: String): Boolean =
withRimeContext {
getRimeOption(option)
}

private fun handleRimeNotification(notif: RimeNotification<*>) {
when (notif) {
is RimeNotification.SchemaNotification -> schemaItemCached = notif.value
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ interface RimeApi {
suspend fun commitComposition(): Boolean

suspend fun clearComposition()

suspend fun setRuntimeOption(
option: String,
value: Boolean,
)

suspend fun getRuntimeOption(option: String): Boolean
}
39 changes: 24 additions & 15 deletions app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.ViewAnimator
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeNotification.OptionNotification
import com.osfans.trime.core.SchemaItem
import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.daemon.launchOnReady
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.schema.SchemaManager
import com.osfans.trime.data.theme.ColorManager
Expand All @@ -32,7 +33,7 @@ import splitties.views.dsl.core.matchParent

@InputScope
@Inject
class QuickBar(context: Context, service: TrimeInputMethodService, theme: Theme) : InputBroadcastReceiver {
class QuickBar(context: Context, service: TrimeInputMethodService, rime: RimeSession, theme: Theme) : InputBroadcastReceiver {
private val prefs = AppPrefs.defaultInstance()

private val showSwitchers get() = prefs.keyboard.switchesEnabled
Expand All @@ -55,12 +56,18 @@ class QuickBar(context: Context, service: TrimeInputMethodService, theme: Theme)
val prevEnabled = switch.enabled
switch.enabled =
if (switch.options.isNullOrEmpty()) {
(1 - prevEnabled).also { Rime.setOption(switch.name!!, it == 1) }
(1 - prevEnabled).also { newValue ->
rime.launchOnReady {
it.setRuntimeOption(switch.name!!, newValue == 1)
}
}
} else {
val options = switch.options
((prevEnabled + 1) % options.size).also {
Rime.setOption(options[prevEnabled], false)
Rime.setOption(options[it], true)
((prevEnabled + 1) % options.size).also { newValue ->
rime.launchOnReady {
it.setRuntimeOption(options[prevEnabled], false)
it.setRuntimeOption(options[newValue], true)
}
}
}
}
Expand All @@ -79,7 +86,7 @@ class QuickBar(context: Context, service: TrimeInputMethodService, theme: Theme)
}
with(candidates) {
setCandidateListener(service.textInputManager)
shouldShowComment = !Rime.getOption("_hide_comment")
rime.launchOnReady { shouldShowComment = !it.getRuntimeOption("_hide_comment") }
}
}
}
Expand All @@ -106,14 +113,16 @@ class QuickBar(context: Context, service: TrimeInputMethodService, theme: Theme)

val view by lazy {
ViewAnimator(context).apply {
visibility =
if (Rime.getOption("_hide_candidate") ||
Rime.getOption("_hide_bar")
) {
View.GONE
} else {
View.VISIBLE
}
rime.launchOnReady {
visibility =
if (it.getRuntimeOption("_hide_candidate") ||
it.getRuntimeOption("_hide_bar")
) {
View.GONE
} else {
View.VISIBLE
}
}
background =
ColorManager.getDrawable(
context,
Expand Down

0 comments on commit 9e38094

Please sign in to comment.