Skip to content

Commit

Permalink
feat: custom liquid keyboard fixed key bar
Browse files Browse the repository at this point in the history
  • Loading branch information
shitlime authored and WhiredPlanck committed May 15, 2024
1 parent 5d0bb1f commit d92686b
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 63 deletions.
12 changes: 12 additions & 0 deletions app/src/main/assets/rime/tongwenfeng.trime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,15 @@ liquid_keyboard:
single_width: 60 #single类型的按键宽度
vertical_gap: 8 #纵向按键间隙
margin_x: 2 #左右按键间隙的1/2
fixed_key_bar: # 固定按键条
position: bottom # 摆放位置(在滚动区域的……) top|bottom|left|right
keys: # 按键(显示名称为对应的label,不能放太多)
- liquid_keyboard_exit
- space1
- BackSpace
- Return2
- liquid_keyboard_clipboard
- liquid_keyboard_tabs
keyboards: [emoji, math, ascii, cn, history, clipboard, collection, draft, symbollist, list, ids, symbol, tabs, pinyin, script_symbols, jp, grease, rusa, korea, lation, yinbiao, unit, yanwenzi, combing, emoji_full, candidate] #tab列表
tabs:
name: 更多
Expand Down Expand Up @@ -3920,6 +3929,7 @@ preset_keys:
Shift_L: {label: Shift, preview: '⇪', functional: false, send: Shift_L}
Return: { label: enter_labels, preview: '↩', functional: false, send: Return }
Return1: {label: Enter, preview: '↩', functional: false, send: Return}
Return2: {label: 回车, send: Return }
Hide: {label: 隐藏, send: BACK}
BackSpace: {label: 退格, preview: '⇦', repeatable: true, functional: false, send: BackSpace}
space: {repeatable: false, preview: ' ', functional: false, label: '_______', send: space}
Expand Down Expand Up @@ -3969,8 +3979,10 @@ preset_keys:
Keyboard_next: { label: 后退, functional: false, send: Eisu_toggle, select: .next }
Keyboard_last: { label: 后退, functional: false, send: Eisu_toggle, select: .last }
Keyboard_last_lock: { label: 返回, send: Eisu_toggle, select: .last_lock } #直接切換到下一键盘
liquid_keyboard_exit: {label: 返回, send: function, command: liquid_keyboard, option: "-1"} #退出liquidkeyboard
liquid_keyboard_switch: { label: 更多, send: function, command: liquid_keyboard, option: "特殊" }
liquid_keyboard_switch2: { toggle: _liquid_keyboard, send: Mode_switch, states: [ 更多, 更多 ] }
liquid_keyboard_tabs: { label: 更多, send: function, command: liquid_keyboard, option: "更多" }
liquid_keyboard_emoji: { label: 🙂, send: function, command: liquid_keyboard, option: "emoji" }
liquid_keyboard_clipboard: { label: 剪贴, send: function, command: liquid_keyboard, option: "剪贴" }
# rime状态
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/assets/rime/trime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,15 @@ liquid_keyboard:
single_width: 60 #single类型的按键宽度
vertical_gap: 1 #纵向按键间隙
margin_x: 0.5 #左右按键间隙的1/2
fixed_key_bar: # 固定按键条
position: bottom # 摆放位置(在滚动区域的……) top|bottom|left|right
keys: # 按键(显示名称为对应的label,不能放太多)
- liquid_keyboard_exit
- space1
- BackSpace
- Return2
- liquid_keyboard_clipboard
- liquid_keyboard_switch
keyboards: [emoji, math, ascii, cn, history, clipboard, collection, draft, symbollist, list, ids, symbol, tabs, pinyin, script_symbols, jp, grease, rusa, korea, lation, yinbiao, unit, yanwenzi, combing, emoji_full, candidate] #tab列表
tabs:
name: 更多
Expand Down Expand Up @@ -887,9 +896,11 @@ preset_keys:
Shift_L: {label: Shift, send: Shift_L, shift_lock: ascii_long}
Return: {label: enter_labels, send: Return}
Return1: {label: Enter, send: Return }
Return2: {label: 回车, send: Return }
Hide: {label: 隱藏, send: BACK}
BackSpace: {label: 退格, repeatable: true, send: BackSpace}
space: {repeatable: false, functional: false, send: space}
space1: {label: 空格, repeatable: false, functional: false, send: space}
Escape: {label: Esc, send: Escape}
Home: {label: 行首, send: Home}
Insert: {label: 插入, send: Insert}
Expand Down Expand Up @@ -932,7 +943,7 @@ preset_keys:
Keyboard_letter: {label: 字母, send: Eisu_toggle, select: default}
Keyboard_default: {label: 返回, send: Eisu_toggle, select: .default}
Keyboard_switch: {label: 鍵盤, send: Eisu_toggle, select: .next}
liquid_keyboard_switch: { label: 更多, send: function, command: liquid_keyboard, option: "符号表" }
liquid_keyboard_switch: { label: 更多, send: function, command: liquid_keyboard, option: "更多" }
liquid_keyboard_exit: {label: 返回, send: function, command: liquid_keyboard, option: "-1"} #退出liquidkeyboard
liquid_keyboard_emoji: { label: 🙂, send: function, command: liquid_keyboard, option: "emoji" }
liquid_keyboard_clipboard: { label: 剪贴, send: function, command: liquid_keyboard, option: "剪贴" }
Expand Down
213 changes: 160 additions & 53 deletions app/src/main/java/com/osfans/trime/ime/symbol/LiquidLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import com.osfans.trime.ime.core.TrimeInputMethodService
import splitties.dimensions.dp
import splitties.views.backgroundColor
import splitties.views.dsl.constraintlayout.above
import splitties.views.dsl.constraintlayout.after
import splitties.views.dsl.constraintlayout.before
import splitties.views.dsl.constraintlayout.below
import splitties.views.dsl.constraintlayout.bottomOfParent
import splitties.views.dsl.constraintlayout.bottomToTopOf
import splitties.views.dsl.constraintlayout.centerHorizontally
import splitties.views.dsl.constraintlayout.centerVertically
import splitties.views.dsl.constraintlayout.constraintLayout
import splitties.views.dsl.constraintlayout.endOfParent
Expand All @@ -27,6 +31,7 @@ import splitties.views.dsl.constraintlayout.matchConstraints
import splitties.views.dsl.constraintlayout.startOfParent
import splitties.views.dsl.constraintlayout.startToEndOf
import splitties.views.dsl.constraintlayout.topOfParent
import splitties.views.dsl.constraintlayout.topToBottomOf
import splitties.views.dsl.core.add
import splitties.views.dsl.core.frameLayout
import splitties.views.dsl.core.lParams
Expand All @@ -38,53 +43,76 @@ import splitties.views.gravityCenter
import splitties.views.padding

@SuppressLint("ViewConstructor")
class LiquidLayout(context: Context, service: TrimeInputMethodService, theme: Theme) : ConstraintLayout(context) {
class LiquidLayout(context: Context, service: TrimeInputMethodService, theme: Theme) :
ConstraintLayout(context) {
// TODO: 继承一个键盘视图嵌入到这里,而不是自定义一个视图
val operations =
private val fixedKeyBar =
constraintLayout {
val btns =
Array(SimpleKeyDao.operations.size) {
val operation = SimpleKeyDao.operations[it]
val text =
textView {
text = operation.second
textSize = theme.generalStyle.labelTextSize.toFloat()
typeface = FontManager.getTypeface("key_font")
ColorManager.getColor("key_text_color")?.let { color -> setTextColor(color) }
}
val root =
frameLayout {
add(
text,
lParams(matchParent, wrapContent) {
gravity = gravityCenter
padding = dp(5)
},
)
ColorManager.getColor("key_back_color")?.let { bg -> backgroundColor = bg }
setOnClickListener {
// TODO: 这个方式不太优雅,还需打磨
if (operation.first == "liquid_keyboard_exit") {
service.selectLiquidKeyboard(-1)
} else {
val event = EventManager.getEvent(operation.first)
val operations = theme.liquid.getMap("fixed_key_bar")?.get("keys")?.configList
operations?.let {
val btns =
Array(it.size) {
val operation = operations.get(it)
val text =
textView {
text =
theme.presetKeys?.get(operation.toString())?.configMap?.get("label")
.toString()
textSize = theme.generalStyle.labelTextSize.toFloat()
typeface = FontManager.getTypeface("key_font")
ColorManager.getColor("key_text_color")
?.let { color -> setTextColor(color) }
}
val root =
frameLayout {
add(
text,
lParams(matchParent, wrapContent) {
gravity = gravityCenter
padding = dp(5)
},
)
ColorManager.getColor("key_back_color")
?.let { bg -> backgroundColor = bg }
// todo 想办法实现退格键、空格键等 repeatable: true 长按连续触发
setOnClickListener {
val event = EventManager.getEvent(operation.toString())
service.textInputManager?.run {
onPress(event.code)
onEvent(event)
}
}
}
return@Array root
}
when (
theme.liquid.getMap("fixed_key_bar")
?.get("position")?.configValue.toString()
) {
LEFT, RIGHT -> {
btns.forEachIndexed { i, btn ->
add(
btn,
lParams(wrapContent, matchConstraints) {
if (i == 0) topOfParent() else below(btns[i - 1])
if (i == btns.size - 1) bottomOfParent() else above(btns[i + 1])
},
)
}
return@Array root
}

TOP, BOTTOM -> {
btns.forEachIndexed { i, btn ->
add(
btn,
lParams(wrapContent, matchConstraints) {
if (i == 0) startOfParent() else after(btns[i - 1])
if (i == btns.size - 1) endOfParent() else before(btns[i + 1])
},
)
}
}
}
btns.forEachIndexed { i, btn ->
add(
btn,
lParams(wrapContent, matchConstraints) {
if (i == 0) topOfParent() else below(btns[i - 1])
if (i == btns.size - 1) bottomOfParent() else above(btns[i + 1])
},
)
}
}

Expand All @@ -98,21 +126,100 @@ class LiquidLayout(context: Context, service: TrimeInputMethodService, theme: Th
val tabsUi = LiquidTabsUi(context, theme)

init {
add(
boardView,
lParams {
centerVertically()
startOfParent()
endToStartOf(operations)
},
)
add(
operations,
lParams(wrapContent, matchConstraints) {
centerVertically()
startToEndOf(boardView)
endOfParent()
},
)
when (theme.liquid.getMap("fixed_key_bar")?.get("position")?.configValue.toString() ?: "") {
TOP -> {
add(
boardView,
lParams {
centerHorizontally()
topToBottomOf(fixedKeyBar)
bottomOfParent()
},
)
add(
fixedKeyBar,
lParams(wrapContent, wrapContent) {
centerHorizontally()
topOfParent()
bottomToTopOf(boardView)
},
)
}

BOTTOM -> {
add(
boardView,
lParams {
centerHorizontally()
topOfParent()
bottomToTopOf(fixedKeyBar)
},
)
add(
fixedKeyBar,
lParams(wrapContent, wrapContent) {
centerHorizontally()
topToBottomOf(boardView)
bottomOfParent()
},
)
}

LEFT -> {
add(
boardView,
lParams {
centerVertically()
startToEndOf(fixedKeyBar)
endOfParent()
},
)
add(
fixedKeyBar,
lParams(wrapContent, matchConstraints) {
centerVertically()
startOfParent()
endToStartOf(boardView)
},
)
}

RIGHT -> {
add(
boardView,
lParams {
centerVertically()
startOfParent()
endToStartOf(fixedKeyBar)
},
)
add(
fixedKeyBar,
lParams(wrapContent, matchConstraints) {
centerVertically()
startToEndOf(boardView)
endOfParent()
},
)
}

else -> {
add(
boardView,
lParams {
centerVertically()
startOfParent()
endOfParent()
},
)
}
}
}

companion object {
private const val TOP = "top"
private const val BOTTOM = "bottom"
private const val LEFT = "left"
private const val RIGHT = "right"
}
}
8 changes: 0 additions & 8 deletions app/src/main/java/com/osfans/trime/ime/symbol/SimpleKeyDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,4 @@ object SimpleKeyDao {
}
return list
}

val operations =
arrayOf(
"liquid_keyboard_exit" to "返回",
"space" to "空格",
"BackSpace" to "退格",
"Return" to "回车",
)
}
18 changes: 17 additions & 1 deletion doc/Keyboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ SPDX-License-Identifier: GPL-3.0-or-later

- `single_width` : 在 `SINGLE` 類別中,每項的闊度 (和高度相等)。

新增一些参数 `fixed_key_bar:` 用于自定义液态键盘中的固定按键条:

例:
```yaml
liquid_keyboard:
fixed_key_bar: # 固定按键条
position: bottom # 摆放位置(在滚动区域的……) top|bottom|left|right (上/下/左/右)
keys: # 按键(显示名称为对应的label,不能放太多)
- liquid_keyboard_exit # 返回键(在preset_keys内定义,同下)
- space1 # 空格键
- BackSpace # 退格键
- Return2 # 回车键
- liquid_keyboard_clipboard # “剪贴板”跳转按键
- liquid_keyboard_switch # “更多”跳转按键
```
# 藍芽鍵盤問題
https://github.com/osfans/trime/issues/1058#issuecomment-1672635413
Expand Down Expand Up @@ -42,4 +58,4 @@ patch:
```
以上例子代表:
直屏時使用 `my_keyboard`
橫屏時使用 `my_landscape_keyboard`,並且不做自動分割。若 `landscape_split_percent` > 0,則會分割顯示。
橫屏時使用 `my_landscape_keyboard`,並且不做自動分割。若 `landscape_split_percent` > 0,則會分割顯示。
7 changes: 7 additions & 0 deletions doc/trime-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,13 @@
},
"vertical_gap": { "type": "integer", "description": "纵向按键间隙" },
"margin_x": { "type": "number", "description": "左右按键间隙的1/2" },
"fixed_key_bar": {
"type": "object",
"properties": {
"position": {"type": "string", "description": "固定按键条摆放位置(相对滚动区域的方位) top|bottom|left|right (上/下/左/右)"},
"keys": {"type": "array", "description": "固定按键条的按键(显示名称为对应的label,不能放太多)"}
}
},
"keyboards": { "type": "array", "items": { "type": "string" } }
}
},
Expand Down

0 comments on commit d92686b

Please sign in to comment.