Skip to content

Commit

Permalink
refactor: rename KeyEventType to KeyBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Nov 9, 2024
1 parent ded0823 commit 2137a2d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 82 deletions.
75 changes: 37 additions & 38 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Key.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.osfans.trime.core.Rime.Companion.isComposing
import com.osfans.trime.core.Rime.Companion.showAsciiPunch
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.KeyActionManager
import com.osfans.trime.ime.enums.KeyEventType
import com.osfans.trime.util.CollectionUtils.obtainBoolean
import com.osfans.trime.util.CollectionUtils.obtainFloat
import com.osfans.trime.util.CollectionUtils.obtainString
Expand Down Expand Up @@ -105,13 +104,13 @@ class Key(
run {
keyConfig = keyDefs
var hasComposingKey = false
for (type in KeyEventType.entries) {
for (type in KeyBehavior.entries) {
val typeStr = type.toString().lowercase()
s = obtainString(keyDefs, typeStr)
if (s.isNotEmpty()) {
keyActions[type.ordinal] = KeyActionManager.getAction(s)
if (type.ordinal < KeyEventType.COMBO.ordinal) hasComposingKey = true
} else if (type == KeyEventType.CLICK) {
if (type.ordinal < KeyBehavior.COMBO.ordinal) hasComposingKey = true
} else if (type == KeyBehavior.CLICK) {
keyActions[type.ordinal] = KeyActionManager.getAction("")
}
}
Expand Down Expand Up @@ -324,62 +323,62 @@ class Key(
}

/**
* @param type 同文按键模式(点击/长按/滑动)
* @param behavior 同文按键模式(点击/长按/滑动)
* @return
*/
fun sendBindings(type: Int): Boolean {
fun sendBindings(behavior: KeyBehavior): Boolean {
var e: KeyAction? = null
if (type != KeyEventType.CLICK.ordinal && type >= 0 && type <= EVENT_NUM) e = keyActions[type]
if (behavior != KeyBehavior.CLICK) e = keyActions[behavior.ordinal]
if (e != null) return true
if (keyActions[KeyEventType.ASCII.ordinal] != null && isAsciiMode) return false
if (keyActions[KeyBehavior.ASCII.ordinal] != null && isAsciiMode) return false
if (sendBindings) {
if (keyActions[KeyEventType.PAGING.ordinal] != null && hasLeft()) return true
if (keyActions[KeyEventType.HAS_MENU.ordinal] != null && hasMenu()) return true
if (keyActions[KeyEventType.COMPOSING.ordinal] != null && isComposing) return true
if (keyActions[KeyBehavior.PAGING.ordinal] != null && hasLeft()) return true
if (keyActions[KeyBehavior.HAS_MENU.ordinal] != null && hasMenu()) return true
if (keyActions[KeyBehavior.COMPOSING.ordinal] != null && isComposing) return true
}
return false
}

private val keyAction: KeyAction?
get() {
if (keyActions[KeyEventType.ASCII.ordinal] != null && isAsciiMode) {
return keyActions[KeyEventType.ASCII.ordinal]
if (keyActions[KeyBehavior.ASCII.ordinal] != null && isAsciiMode) {
return keyActions[KeyBehavior.ASCII.ordinal]
}
if (keyActions[KeyEventType.PAGING.ordinal] != null && hasLeft()) {
return keyActions[KeyEventType.PAGING.ordinal]
if (keyActions[KeyBehavior.PAGING.ordinal] != null && hasLeft()) {
return keyActions[KeyBehavior.PAGING.ordinal]
}
if (keyActions[KeyEventType.HAS_MENU.ordinal] != null && hasMenu()) {
return keyActions[KeyEventType.HAS_MENU.ordinal]
if (keyActions[KeyBehavior.HAS_MENU.ordinal] != null && hasMenu()) {
return keyActions[KeyBehavior.HAS_MENU.ordinal]
}
return if (keyActions[KeyEventType.COMPOSING.ordinal] != null && isComposing) {
keyActions[KeyEventType.COMPOSING.ordinal]
return if (keyActions[KeyBehavior.COMPOSING.ordinal] != null && isComposing) {
keyActions[KeyBehavior.COMPOSING.ordinal]
} else {
click
}
}
val click: KeyAction?
get() = keyActions[KeyEventType.CLICK.ordinal]
get() = keyActions[KeyBehavior.CLICK.ordinal]
val longClick: KeyAction?
get() = keyActions[KeyEventType.LONG_CLICK.ordinal]
get() = keyActions[KeyBehavior.LONG_CLICK.ordinal]

fun hasEvent(i: Int): Boolean = keyActions[i] != null
fun hasAction(behavior: KeyBehavior): Boolean = keyActions[behavior.ordinal] != null

fun getAction(i: Int): KeyAction? {
fun getAction(behavior: KeyBehavior): KeyAction? {
var e: KeyAction? = null
if (i != KeyEventType.CLICK.ordinal && i >= 0 && i <= EVENT_NUM) e = keyActions[i]
if (behavior != KeyBehavior.CLICK) e = keyActions[behavior.ordinal]
if (e != null) return e
if (keyActions[KeyEventType.ASCII.ordinal] != null && isAsciiMode) {
return keyActions[KeyEventType.ASCII.ordinal]
if (keyActions[KeyBehavior.ASCII.ordinal] != null && isAsciiMode) {
return keyActions[KeyBehavior.ASCII.ordinal]
}
if (sendBindings) {
if (keyActions[KeyEventType.PAGING.ordinal] != null && hasLeft()) {
return keyActions[KeyEventType.PAGING.ordinal]
if (keyActions[KeyBehavior.PAGING.ordinal] != null && hasLeft()) {
return keyActions[KeyBehavior.PAGING.ordinal]
}
if (keyActions[KeyEventType.HAS_MENU.ordinal] != null && hasMenu()) {
return keyActions[KeyEventType.HAS_MENU.ordinal]
if (keyActions[KeyBehavior.HAS_MENU.ordinal] != null && hasMenu()) {
return keyActions[KeyBehavior.HAS_MENU.ordinal]
}
if (keyActions[KeyEventType.COMPOSING.ordinal] != null && isComposing) {
return keyActions[KeyEventType.COMPOSING.ordinal]
if (keyActions[KeyBehavior.COMPOSING.ordinal] != null && isComposing) {
return keyActions[KeyBehavior.COMPOSING.ordinal]
}
}
return click
Expand All @@ -388,13 +387,13 @@ class Key(
val code: Int
get() = click!!.code

fun getCode(type: Int): Int = getAction(type)!!.code
fun getCode(behavior: KeyBehavior): Int = getAction(behavior)!!.code

fun getLabel(): String? {
val event = keyAction
return if (!TextUtils.isEmpty(label) &&
event === click &&
keyActions[KeyEventType.ASCII.ordinal] == null &&
keyActions[KeyBehavior.ASCII.ordinal] == null &&
!showAsciiPunch()
) {
label
Expand All @@ -403,11 +402,11 @@ class Key(
}
}

fun getPreviewText(type: Int): String =
if (type == KeyEventType.CLICK.ordinal) {
fun getPreviewText(behavior: KeyBehavior): String =
if (behavior == KeyBehavior.CLICK) {
keyAction!!.getPreview(mKeyboard)
} else {
getAction(type)!!.getPreview(mKeyboard)
getAction(behavior)!!.getPreview(mKeyboard)
}

val symbolLabel: String?
Expand Down Expand Up @@ -455,7 +454,7 @@ class Key(
KEY_STATE_NORMAL, // 5 "key_back_color" 按键背景
)

private val EVENT_NUM = KeyEventType.entries.size
private val EVENT_NUM = KeyBehavior.entries.size

@JvmStatic
fun isTrimeModifierKey(keycode: Int): Boolean = if (keycode == KeyEvent.KEYCODE_FUNCTION) false else KeyEvent.isModifierKey(keycode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

package com.osfans.trime.ime.enums
package com.osfans.trime.ime.keyboard

/** 按键事件枚举 */
enum class KeyEventType {
/** 按键行为枚举 */
enum class KeyBehavior {
// 长按按键展开列表时,正上方为长按对应按键,排序如上,不展示combo及之前的按键,展示extra
COMPOSING,
HAS_MENU,
Expand Down
Loading

0 comments on commit 2137a2d

Please sign in to comment.