Skip to content

Commit e3997b7

Browse files
committed
refactor: enhance processing of nullable theme parameters
- Use native (de)serializer for `style/enter_label`, `style/layout` and `preset_keys` - Remove parameters: `background_dim_amount`, `color_scheme`, `keyboards`, `locale` and `latin_lacale` in `style` - Set default values for data classes
1 parent 301be9a commit e3997b7

12 files changed

Lines changed: 117 additions & 336 deletions

File tree

app/src/main/assets/shared/tongwenfeng.trime.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ round_corner:
2828
#界面风格、功能开关
2929
style:
3030
auto_caps: false #自動句首大寫:true|false|ascii
31-
background_dim_amount: 0.5
32-
root_background: keyboard_back_color # 整个键盘区+候选栏的背景图/色
3331
candidate_font: han.ttf #候选字体
3432
candidate_padding: 5 #候选项内边距
3533
candidate_spacing: 0.5 #候选间距
3634
candidate_text_size: 18 #候选字号
3735
candidate_use_cursor: true #高亮候选项
3836
candidate_view_height: 28 #候选区高度
39-
color_scheme: default #配色方案
4037
comment_font: comment.ttf #编码提示字体
4138
comment_height: 14 #编码提示区高度
4239
comment_on_top: true #编码提示在上方或右侧
@@ -63,12 +60,9 @@ style:
6360
key_long_text_size: 16 #长标签字号
6461
key_text_size: 24 #键字号
6562
key_width: 10.0 #键宽,占屏幕宽的百分比
66-
keyboards: [.default, letter, default, number, symbols, edit, move, bqrw1, bqrw2, bqrw3, bqrw4, bqrw5, bqrw6, bqqt1, bqqt2, bqqt3, bqqt4, bqqz1, bqqz2, bqqz3, bqqz4, bqqz5, bqqz6, bqpt1, bqpt2, bqhd1, bqhd2, bqhc1, bqhc2, bqhc3, bqlx1, bqlx2, bqlx3, bqlx4, bqlx5, bqlx6, bqkj1, bqkj2, bqkj3, bqkj4, bqkj5, bqfh1, bqfh2, bqfh3, bqfh4, kao_fond, kao_sad, kao_mad, kao_astonished, kao_impotent, kao_happy, numberb] #键盘配置
6763
label_text_size: 22 #标签字号
6864
label_font: label.ttf #编标签字体
6965
latin_font: latin.ttf #西文本体
70-
latin_locale: en_US #西文语言
71-
locale: zh_CN #缺省语言 zh_TW,zh_CN,zh_HK,""
7266
keyboard_height: 250 #锁定键盘高度,避免切换时键盘高度变化而造成闪烁
7367
keyboard_height_land: 200 #锁定横屏下键盘高度,避免切换时键盘高度变化而造成闪烁
7468
preview_font: latin.ttf #按键提示字体

app/src/main/assets/shared/trime.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ author: osfans #作者資訊
1111

1212
style:
1313
auto_caps: false #自動句首大寫:true|false|ascii
14-
background_dim_amount: 0.5
1514
candidate_font: han.ttf #候選字型
1615
candidate_padding: 5 #候選項內邊距
1716
candidate_spacing: 0.5 #候選間距
1817
candidate_text_size: 22 #候選字號
1918
candidate_use_cursor: true #高亮候選項
2019
candidate_view_height: 28 #候選區高度
21-
color_scheme: default #配色方案
2220
comment_font: comment.ttf #編碼提示字型
2321
comment_height: 12 #編碼提示區高度
2422
comment_on_top: true #編碼提示在上方或右側
@@ -51,12 +49,9 @@ style:
5149
key_long_text_size: 14 #長標籤字號
5250
key_text_size: 22 #鍵字號
5351
key_width: 10.0 #鍵寬,佔螢幕寬的百分比
54-
keyboards: [.default, letter, number, symbols, mini] #鍵盤配置:自動鍵盤、字母、數字、符號、精简(供插入实体键盘使用)
5552
label_text_size: 22 #標籤字號
5653
label_font: label.ttf #編標籤字型
5754
latin_font: latin.ttf #西文字型
58-
latin_locale: en_US #西文語言
59-
locale: zh_TW #預設語言 zh_TW,zh_CN,zh_HK,""
6055
keyboard_height: 250 #锁定键盘高度,避免切换时键盘高度变化而造成闪烁
6156
keyboard_height_land: 200 #锁定横屏下键盘高度,避免切换时键盘高度变化而造成闪烁
6257
preview_font: latin.ttf #按鍵提示字型

app/src/main/java/com/osfans/trime/data/theme/Theme.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import com.osfans.trime.core.Rime
1212
import com.osfans.trime.data.base.DataManager
1313
import com.osfans.trime.data.theme.mapper.GeneralStyleMapper
1414
import com.osfans.trime.data.theme.mapper.LiquidKeyboardMapper
15-
import com.osfans.trime.data.theme.mapper.PresetKeyMapper
1615
import com.osfans.trime.data.theme.mapper.TextKeyboardMapper
1716
import com.osfans.trime.data.theme.model.ColorScheme
1817
import com.osfans.trime.data.theme.model.GeneralStyle
@@ -43,8 +42,9 @@ data class Theme(
4342
if (!Rime.deployRimeConfigFile(configId, CONFIG_VERSION_KEY)) {
4443
Timber.w("Failed to deploy theme config file '$configId.yaml'")
4544
}
45+
val yaml = ThemeFilesManager.yaml
4646
val file = File(DataManager.resolveDeployedResourcePath(configId))
47-
val root = ThemeFilesManager.yaml.parseToYamlNode(file.readText()).yamlMap
47+
val root = yaml.parseToYamlNode(file.readText()).yamlMap
4848
return Theme(
4949
configId = configId,
5050
name = root.getString("name"),
@@ -55,9 +55,10 @@ data class Theme(
5555
else -> LiquidKeyboardMapper(node).map()
5656
},
5757
presetKeys =
58-
root.get<YamlMap>("preset_keys")?.entries?.entries?.associate {
59-
it.key.content to PresetKeyMapper(it.value.yamlMap).map()
60-
} ?: emptyMap(),
58+
when (val map = root.get<YamlMap>("preset_keys")) {
59+
null -> emptyMap()
60+
else -> yaml.decodeFromYamlNode(map)
61+
},
6162
presetKeyboards =
6263
root.get<YamlMap>("preset_keyboards")?.entries?.entries?.associate {
6364
it.key.content to TextKeyboardMapper(it.value.yamlMap).map()
@@ -72,9 +73,10 @@ data class Theme(
7273
)
7374
} ?: emptyList(),
7475
fallbackColors =
75-
root.get<YamlMap>("fallback_colors")?.entries?.entries?.associate {
76-
it.key.content to it.value.yamlScalar.content
77-
} ?: emptyMap(),
76+
when (val map = root.get<YamlMap>("fallback_colors")) {
77+
null -> emptyMap()
78+
else -> yaml.decodeFromYamlNode(map)
79+
},
7880
)
7981
}
8082
}

app/src/main/java/com/osfans/trime/data/theme/ThemeFilesManager.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package com.osfans.trime.data.theme
88
import com.charleskorn.kaml.AnchorsAndAliases
99
import com.charleskorn.kaml.Yaml
1010
import com.charleskorn.kaml.YamlConfiguration
11+
import com.charleskorn.kaml.YamlNamingStrategy
1112
import com.charleskorn.kaml.yamlMap
1213
import com.osfans.trime.data.base.DataManager
1314
import com.osfans.trime.util.getString
@@ -22,6 +23,8 @@ object ThemeFilesManager {
2223
configuration =
2324
YamlConfiguration(
2425
strictMode = false,
26+
yamlNamingStrategy = YamlNamingStrategy.SnakeCase,
27+
decodeEnumCaseInsensitive = true,
2528
anchorsAndAliases = AnchorsAndAliases.Permitted(null),
2629
codePointLimit = CODE_POINT_LIMIT, // 10 MB
2730
),

app/src/main/java/com/osfans/trime/data/theme/mapper/EnterLabelStyleMapper.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

app/src/main/java/com/osfans/trime/data/theme/mapper/GeneralStyleMapper.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
package com.osfans.trime.data.theme.mapper
66

77
import com.charleskorn.kaml.YamlMap
8+
import com.osfans.trime.data.theme.ThemeFilesManager
89
import com.osfans.trime.data.theme.model.GeneralStyle
910

1011
class GeneralStyleMapper(
1112
node: YamlMap,
1213
) : Mapper<GeneralStyle>(node) {
1314
override fun map() =
1415
GeneralStyle(
15-
autoCaps = getString("auto_caps"),
16-
backgroundDimAmount = getFloat("background_dim_amount"),
16+
autoCaps = getBoolean("auto_caps"),
1717
candidateBorder = getInt("candidate_border"),
1818
candidateBorderRound = getFloat("candidate_border_round"),
1919
candidateFont = getStringList("candidate_font"),
@@ -22,7 +22,6 @@ class GeneralStyleMapper(
2222
candidateTextSize = getFloat("candidate_text_size"),
2323
candidateUseCursor = getBoolean("candidate_use_cursor"),
2424
candidateViewHeight = getInt("candidate_view_height"),
25-
colorScheme = getString("color_scheme"),
2625
commentFont = getStringList("comment_font"),
2726
commentHeight = getInt("comment_height"),
2827
commentOnTop = getBoolean("comment_on_top"),
@@ -37,7 +36,11 @@ class GeneralStyleMapper(
3736
keyboardPaddingBottom = getInt("keyboard_padding_bottom"),
3837
keyboardPaddingLand = getInt("keyboard_padding_land"),
3938
keyboardPaddingLandBottom = getInt("keyboard_padding_land_bottom"),
40-
layout = LayoutStyleMapper(node.get<YamlMap>("layout")!!).map(),
39+
layout =
40+
when (val map = node.get<YamlMap>("layout")) {
41+
null -> GeneralStyle.Layout()
42+
else -> ThemeFilesManager.yaml.decodeFromYamlNode(map)
43+
},
4144
keyFont = getStringList("key_font"),
4245
keyBorder = getInt("key_border"),
4346
keyHeight = getInt("key_height"),
@@ -52,12 +55,9 @@ class GeneralStyleMapper(
5255
keyPressOffsetX = getInt("key_press_offset_x"),
5356
keyPressOffsetY = getInt("key_press_offset_y"),
5457
keyWidth = getFloat("key_width"),
55-
keyboards = getStringList("keyboards"),
5658
labelTextSize = getFloat("label_text_size"),
5759
labelFont = getStringList("label_font"),
5860
latinFont = getStringList("latin_font"),
59-
latinLocale = getString("latin_locale"),
60-
locale = getString("locale"),
6161
keyboardHeight = getInt("keyboard_height"),
6262
keyboardHeightLand = getInt("keyboard_height_land"),
6363
previewFont = getStringList("preview_font"),
@@ -82,7 +82,7 @@ class GeneralStyleMapper(
8282
enterLabel =
8383
when (val map = node.get<YamlMap>("enter_labels")) {
8484
null -> GeneralStyle.EnterLabel()
85-
else -> EnterLabelStyleMapper(map).map()
85+
else -> ThemeFilesManager.yaml.decodeFromYamlNode(map)
8686
},
8787
)
8888
}

app/src/main/java/com/osfans/trime/data/theme/mapper/LayoutStyleMapper.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/src/main/java/com/osfans/trime/data/theme/mapper/PresetKeyMapper.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)