Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions app/src/main/java/com/osfans/trime/TrimeApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import com.osfans.trime.data.db.ClipboardHelper
import com.osfans.trime.data.db.CollectionHelper
import com.osfans.trime.data.db.DraftHelper
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.receiver.RimeIntentReceiver
import com.osfans.trime.ui.main.LogActivity
import com.osfans.trime.util.isStorageAvailable
import com.osfans.trime.worker.BackgroundSyncWork
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.MainScope
Expand Down Expand Up @@ -142,9 +140,6 @@ class TrimeApplication : Application() {
ClipboardHelper.init(applicationContext)
CollectionHelper.init(applicationContext)
DraftHelper.init(applicationContext)
if (applicationContext.isStorageAvailable()) {
ThemeManager.init(resources.configuration)
}
registerBroadcastReceiver()
startWorkManager()
} catch (e: Exception) {
Expand Down
140 changes: 140 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* SPDX-FileCopyrightText: 2015 - 2025 Rime community
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package com.osfans.trime.core

class RimeConfig private constructor(
private val ptr: Long,
) : AutoCloseable {
fun getString(
key: String,
defaultValue: String = "",
): String {
check(ptr != 0L)
return getRimeConfigString(ptr, key) ?: defaultValue
}

fun getBool(
key: String,
defaultValue: Boolean = false,
): Boolean {
check(ptr != 0L)
return getRimeConfigBool(ptr, key) ?: defaultValue
}

fun getDouble(
key: String,
defaultValue: Double = 0.0,
): Double {
check(ptr != 0L)
return getRimeConfigDouble(ptr, key) ?: defaultValue
}

fun getInt(
key: String,
defaultValue: Int = 0,
): Int {
check(ptr != 0L)
return getRimeConfigInt(ptr, key) ?: defaultValue
}

fun getList(key: String): List<RimeConfig> {
check(ptr != 0L)
val array = getRimeConfigList(ptr, key) ?: return emptyList()
return array.map { RimeConfig(it) }
}

fun getStringList(key: String): List<String> {
check(ptr != 0L)
return getRimeConfigList(ptr, key)?.map {
getRimeConfigString(it, "") ?: ""
} ?: emptyList()
}

fun getMap(key: String): Map<String, RimeConfig> {
check(ptr != 0L)
val map = getRimeConfigMap(ptr, key) ?: return emptyMap()
return map.mapValues { RimeConfig(it.value) }
}

fun getStringValueMap(key: String): Map<String, String> {
check(ptr != 0L)
val map = getRimeConfigMap(ptr, key) ?: return emptyMap()
return map.mapValues { getRimeConfigString(it.value, "") ?: "" }
}

fun getItem(key: String): RimeConfig {
check(ptr != 0L)
return RimeConfig(getRimeConfigItem(ptr, key))
}

override fun close() {
if (ptr != 0L) {
closeRimeConfig(ptr)
}
}

companion object {
@JvmStatic
private external fun openRimeSchema(schemaId: String): Long

@JvmStatic
private external fun openRimeConfig(configId: String): Long

@JvmStatic
private external fun openRimeUserConfig(configId: String): Long

@JvmStatic
private external fun getRimeConfigString(
ptr: Long,
key: String,
): String?

@JvmStatic
private external fun getRimeConfigBool(
ptr: Long,
key: String,
): Boolean?

@JvmStatic
private external fun getRimeConfigDouble(
ptr: Long,
key: String,
): Double?

@JvmStatic
private external fun getRimeConfigInt(
ptr: Long,
key: String,
): Int?

@JvmStatic
private external fun getRimeConfigList(
ptr: Long,
key: String,
): LongArray?

@JvmStatic
private external fun getRimeConfigMap(
ptr: Long,
key: String,
): Map<String, Long>?

@JvmStatic
private external fun getRimeConfigItem(
ptr: Long,
key: String,
): Long

@JvmStatic
private external fun closeRimeConfig(ptr: Long)

fun openSchema(schemaId: String) = RimeConfig(openRimeSchema(schemaId))

fun openConfig(configId: String) = RimeConfig(openRimeConfig(configId))

fun openUserConfig(configId: String) = RimeConfig(openRimeUserConfig(configId))
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/com/osfans/trime/data/base/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ object DataManager {
val userDataDir
get() = File(prefs.profile.userDataDir).also { it.mkdirs() }

val prebuiltDataDir = File(sharedDataDir, "build")
val stagingDir get() = File(userDataDir, "build")

/**
* Return the absolute path of the compiled config file
* based on given resource id.
Expand All @@ -62,9 +65,6 @@ object DataManager {
*/
@JvmStatic
fun resolveDeployedResourcePath(resourceId: String): String {
val stagingDir = File(userDataDir, "build")
val prebuiltDataDir = File(sharedDataDir, "build")

val defaultPath = File(stagingDir, "$resourceId.yaml")
if (!defaultPath.exists()) {
val fallbackPath = File(prebuiltDataDir, "$resourceId.yaml")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/data/prefs/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class AppPrefs(
}

val landscapeModeOption by enum(LANDSCAPE_MODE, LandscapeModeOption.NEVER)
val splitSpacePercent by int(SPLIT_SPACE_PERCENT, 100)
val splitSpacePercent = int(SPLIT_SPACE_PERCENT, 100)

val hookCtrlA by bool(HOOK_CTRL_A, false)
val hookCtrlCV by bool(HOOK_CTRL_CV, false)
Expand Down
44 changes: 27 additions & 17 deletions app/src/main/java/com/osfans/trime/data/schema/Schema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,44 @@

package com.osfans.trime.data.schema

import com.osfans.trime.util.config.Config
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import com.osfans.trime.core.RimeConfig
import timber.log.Timber

class Schema(
schemaId: String = ".default",
schemaId: String,
) {
private val config =
if (schemaId == ".default") {
Config.create("default")
} else {
Config.create("$schemaId.schema")
}
val switches: List<Switch>

val switches get() =
config?.getList("switches")
val symbols: Map<String, RimeConfig>

val symbols get() =
config?.getMap("punctuator/symbols")
val alphabet: String

val alphabet get() = config?.getString("speller/alphabet")
init {
RimeConfig.openSchema(schemaId).use { c ->
switches =
c.getList("switches").mapNotNull decode@{ e ->
try {
Switch(
name = e.getString("name"),
options = e.getStringList("options"),
reset = e.getInt("reset", -1),
states = e.getStringList("states"),
)
} catch (e: Exception) {
Timber.e(e, "Failed to decode switches of schema '$schemaId'")
null
}
}
symbols = c.getMap("punctuator/symbols")
alphabet = c.getString("speller/alphabet")
}
}

@Serializable
data class Switch(
val name: String = "",
val options: List<String> = listOf(),
val reset: Int = -1,
val states: List<String> = listOf(),
@Transient var enabled: Int = 0,
var enabled: Int = 0,
)
}
13 changes: 5 additions & 8 deletions app/src/main/java/com/osfans/trime/data/schema/SchemaManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@
package com.osfans.trime.data.schema

import com.osfans.trime.core.Rime
import kotlinx.serialization.builtins.ListSerializer

object SchemaManager {
private lateinit var currentSchema: Schema
var visibleSwitches: List<Schema.Switch> = listOf()

private val defaultSchema = Schema()

@JvmStatic
fun init(schemaId: String) {
currentSchema = runCatching { Schema(schemaId) }.getOrDefault(defaultSchema)
visibleSwitches = currentSchema.switches
?.decode(ListSerializer(Schema.Switch.serializer()))
?.filter { it.states.isNotEmpty() } ?: listOf() // 剔除没有 states 条目项的值,它们不作为开关使用
currentSchema = Schema(schemaId)
visibleSwitches =
currentSchema.switches
.filter { it.states.isNotEmpty() } // 剔除没有 states 条目项的值,它们不作为开关使用
updateSwitchOptions()
}

val activeSchema: Schema
get() = runCatching { currentSchema }.getOrDefault(defaultSchema)
get() = currentSchema

@JvmStatic
fun updateSwitchOptions() {
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/osfans/trime/data/theme/ColorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object ColorManager {
private var isNightMode = false

val presetColorSchemes: List<ColorScheme>
get() = theme.presetColorSchemes
get() = theme.colorSchemes

private val customFallbackRules: Map<String, String>
get() = theme.fallbackColors
Expand All @@ -46,11 +46,11 @@ object ColorManager {
_activeColorScheme = value
lightModeColorScheme =
runCatching {
_activeColorScheme.values["light_scheme"]?.let { colorScheme(it) }
_activeColorScheme.colors["light_scheme"]?.let { colorScheme(it) }
}.getOrNull()
darkModeColorScheme =
runCatching {
_activeColorScheme.values["dark_scheme"]?.let { colorScheme(it) }
_activeColorScheme.colors["dark_scheme"]?.let { colorScheme(it) }
}.getOrNull()
fireChange()
}
Expand Down Expand Up @@ -209,8 +209,8 @@ object ColorManager {

while (true) {
when {
activeColorScheme.values.containsKey(currentKey) -> {
return parser(activeColorScheme.values[currentKey]!!)
activeColorScheme.colors.containsKey(currentKey) -> {
return parser(activeColorScheme.colors[currentKey]!!)
}
fullFallbackRules.containsKey(currentKey) -> {
currentKey =
Expand Down
Loading
Loading