mirror of
https://github.com/fcitx5-android/fcitx5-android.git
synced 2026-08-02 20:16:06 +08:00
feat: load Popup from json config config/PopupPreset.json
This commit is contained in:
parent
df4e1349f5
commit
6e23aebe39
@ -27,6 +27,9 @@ import splitties.views.dsl.core.add
|
||||
import splitties.views.dsl.core.frameLayout
|
||||
import splitties.views.dsl.core.lParams
|
||||
import java.util.LinkedList
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.fcitx.fcitx5.android.utils.appContext
|
||||
import java.io.File
|
||||
|
||||
class PopupComponent :
|
||||
UniqueComponent<PopupComponent>(), Dependent, ManagedHandler by managedHandler() {
|
||||
@ -62,6 +65,32 @@ class PopupComponent :
|
||||
private val rootLocation = intArrayOf(0, 0)
|
||||
private val rootBounds: Rect = Rect()
|
||||
|
||||
companion object {
|
||||
private var lastModified = 0L
|
||||
private var cachedPopupPreset: Map<String, Array<String>>? = null
|
||||
val popupPresetJson: Map<String, Array<String>>?
|
||||
@Synchronized
|
||||
get() {
|
||||
var file = File(appContext.getExternalFilesDir(null), "config/PopupPreset.json")
|
||||
if (!file.exists()) {
|
||||
cachedPopupPreset = null
|
||||
return null
|
||||
}
|
||||
if (cachedPopupPreset == null || file.lastModified() != lastModified) {
|
||||
try {
|
||||
lastModified = file.lastModified()
|
||||
val json = file.readText()
|
||||
cachedPopupPreset = Json.decodeFromString<Map<String, List<String>>>(json)
|
||||
.mapValues { it.value.toTypedArray() }
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
cachedPopupPreset = null
|
||||
}
|
||||
}
|
||||
return cachedPopupPreset
|
||||
}
|
||||
}
|
||||
|
||||
val root by lazy {
|
||||
context.frameLayout {
|
||||
// we want (0, 0) at top left
|
||||
@ -107,7 +136,7 @@ class PopupComponent :
|
||||
}
|
||||
|
||||
private fun showKeyboard(viewId: Int, keyboard: KeyDef.Popup.Keyboard, bounds: Rect) {
|
||||
val keys = PopupPreset[keyboard.label] ?: return
|
||||
val keys = Companion.popupPresetJson?.get(keyboard.label) ?: PopupPreset[keyboard.label] ?: return
|
||||
// clear popup preview text OR create empty popup preview
|
||||
showingEntryUi[viewId]?.setText("") ?: showPopup(viewId, "", bounds)
|
||||
reallyShowKeyboard(viewId, keys, bounds)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user