Skip to content

Commit

Permalink
refactor: rename Event to KeyAction
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Nov 10, 2024
1 parent 3f91d3d commit 1ed4c7b
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 151 deletions.
24 changes: 0 additions & 24 deletions app/src/main/java/com/osfans/trime/data/theme/EventManager.kt

This file was deleted.

23 changes: 23 additions & 0 deletions app/src/main/java/com/osfans/trime/data/theme/KeyActionManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2015 - 2024 Rime community
//
// SPDX-License-Identifier: GPL-3.0-or-later

package com.osfans.trime.data.theme

import android.view.KeyEvent
import com.osfans.trime.ime.keyboard.KeyAction

object KeyActionManager {
private val actionCache = mutableMapOf<String, KeyAction>()

fun getAction(actionId: String): KeyAction =
actionCache[actionId]
?: KeyAction(actionId).also {
// 空格的 label 需要根据方案动态显示,所以不加入缓存
if (it.code != KeyEvent.KEYCODE_SPACE) {
actionCache[actionId] = it
}
}

fun resetCache() = actionCache.clear()
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/data/theme/ThemeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object ThemeManager {

fun init() {
Theme(prefs.selectedTheme).let {
EventManager.resetCache()
KeyActionManager.resetCache()
FontManager.resetCache(it)
ColorManager.resetCache(it)
TabManager.resetCache(it)
Expand All @@ -66,7 +66,7 @@ object ThemeManager {

fun setNormalTheme(name: String) {
Theme(name).let {
EventManager.resetCache()
KeyActionManager.resetCache()
FontManager.resetCache(it)
ColorManager.resetCache(it)
TabManager.resetCache(it)
Expand Down
22 changes: 0 additions & 22 deletions app/src/main/java/com/osfans/trime/ime/composition/Composition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import com.osfans.trime.data.theme.FontManager
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.data.theme.model.CompositionComponent
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ime.keyboard.Event
import com.osfans.trime.ime.keyboard.KeyboardActionListener
import com.osfans.trime.ime.keyboard.KeyboardPrefs.isLandscapeMode
import com.osfans.trime.util.sp
import splitties.dimensions.dp
Expand Down Expand Up @@ -94,7 +92,6 @@ class Composition(
private var initialY = 0f
private var onActionMove: ((Float, Float) -> Unit)? = null
private var onSelectCandidate: ((Int) -> Unit)? = null
private var keyboardActionListener: KeyboardActionListener? = null

private val stickyLines: Int
get() =
Expand Down Expand Up @@ -160,25 +157,6 @@ class Composition(
onSelectCandidate = listener
}

fun setKeyboardActionListener(listener: KeyboardActionListener?) {
keyboardActionListener = listener
}

private inner class EventSpan(
private val event: Event,
) : ClickableSpan() {
override fun onClick(tv: View) {
keyboardActionListener?.onPress(event.code)
keyboardActionListener?.onEvent(event)
}

override fun updateDrawState(ds: TextPaint) {
ds.isUnderlineText = false
keyTextColor?.let { ds.color = it }
keyBackColor?.let { ds.bgColor = it }
}
}

private fun alignmentSpan(alignment: String): AlignmentSpan {
val align =
when (alignment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.osfans.trime.ime.bar.QuickBar
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
import com.osfans.trime.ime.dependency.InputScope
import com.osfans.trime.ime.enums.PopupPosition
import com.osfans.trime.ime.keyboard.CommonKeyboardActionListener
import me.tatarka.inject.annotations.Inject
import splitties.dimensions.dp
import splitties.views.dsl.core.add
Expand All @@ -44,7 +43,6 @@ class CompositionPopupWindow(
private val ctx: Context,
private val rime: RimeSession,
private val theme: Theme,
private val commonKeyboardActionListener: CommonKeyboardActionListener,
private val bar: QuickBar,
) : InputBroadcastReceiver {
// 顯示懸浮窗口
Expand All @@ -60,7 +58,6 @@ class CompositionPopupWindow(
setOnSelectCandidateListener { idx ->
rime.launchOnReady { it.selectCandidate(idx) }
}
setKeyboardActionListener(commonKeyboardActionListener.listener)
}

private val root =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.data.db.DraftHelper
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.EventManager
import com.osfans.trime.data.theme.KeyActionManager
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.broadcast.IntentReceiver
Expand Down Expand Up @@ -276,7 +276,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
inputView
?.commonKeyboardActionListener
?.listener
?.onEvent(EventManager.getEvent(key))
?.onAction(KeyActionManager.getAction(key))
shouldUpdateRimeOption = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.daemon.launchOnReady
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.EventManager
import com.osfans.trime.data.theme.KeyActionManager
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.core.InputView
import com.osfans.trime.ime.core.Speech
Expand Down Expand Up @@ -133,12 +133,12 @@ class CommonKeyboardActionListener(
}
}

override fun onEvent(event: Event) {
when (event.code) {
override fun onAction(action: KeyAction) {
when (action.code) {
KeyEvent.KEYCODE_SWITCH_CHARSET -> { // Switch status
rime.launchOnReady { api ->
service.lifecycleScope.launch {
val option = event.getToggle()
val option = action.getToggle()
val status = api.getRuntimeOption(option)
api.setRuntimeOption(option, !status)

Expand All @@ -147,9 +147,9 @@ class CommonKeyboardActionListener(
}
}
KeyEvent.KEYCODE_LANGUAGE_SWITCH -> { // Switch IME
if (event.select == ".next") {
if (action.select == ".next") {
service.switchToNextIme()
} else if (event.select.isNotEmpty()) {
} else if (action.select.isNotEmpty()) {
service.switchToPrevIme()
} else {
inputMethodManager.showInputMethodPicker()
Expand All @@ -161,7 +161,7 @@ class CommonKeyboardActionListener(
// %2$s爲當前輸入的編碼
// %3$s爲光標前字符
// %4$s爲光標前所有字符
var arg = event.option
var arg = action.option
val activeTextRegex = Regex(".*%(\\d*)\\$" + "s.*")
if (arg.matches(activeTextRegex)) {
var activeTextMode =
Expand All @@ -180,7 +180,7 @@ class CommonKeyboardActionListener(
)
}

when (event.command) {
when (action.command) {
"liquid_keyboard" -> {
val target =
when {
Expand All @@ -201,7 +201,7 @@ class CommonKeyboardActionListener(
"paste_by_char" -> service.pasteByChar()
"set_color_scheme" -> ColorManager.setColorScheme(arg)
else -> {
ShortcutUtils.call(service, event.command, arg)?.let {
ShortcutUtils.call(service, action.command, arg)?.let {
service.commitText(it)
service.updateComposing()
}
Expand All @@ -210,7 +210,7 @@ class CommonKeyboardActionListener(
}
KeyEvent.KEYCODE_VOICE_ASSIST -> Speech(service).startListening() // Speech Recognition
KeyEvent.KEYCODE_SETTINGS -> { // Settings
when (event.option) {
when (action.option) {
"theme" -> showThemePicker()
"color" -> showColorPicker()
"schema" -> showAvailableSchemaPicker()
Expand All @@ -221,31 +221,31 @@ class CommonKeyboardActionListener(
KeyEvent.KEYCODE_PROG_RED -> showColorPicker()
KeyEvent.KEYCODE_MENU -> showEnabledSchemaPicker()
else -> {
if (event.mask == 0 && KeyboardSwitcher.currentKeyboard.isOnlyShiftOn) {
if (event.code == KeyEvent.KEYCODE_SPACE && prefs.keyboard.hookShiftSpace) {
onKey(event.code, 0)
if (action.mask == 0 && KeyboardSwitcher.currentKeyboard.isOnlyShiftOn) {
if (action.code == KeyEvent.KEYCODE_SPACE && prefs.keyboard.hookShiftSpace) {
onKey(action.code, 0)
return
} else if (event.code >= KeyEvent.KEYCODE_0 &&
event.code <= KeyEvent.KEYCODE_9 &&
} else if (action.code >= KeyEvent.KEYCODE_0 &&
action.code <= KeyEvent.KEYCODE_9 &&
prefs.keyboard.hookShiftNum
) {
onKey(event.code, 0)
onKey(action.code, 0)
return
} else if (prefs.keyboard.hookShiftSymbol) {
if (event.code >= KeyEvent.KEYCODE_GRAVE &&
event.code <= KeyEvent.KEYCODE_SLASH ||
event.code == KeyEvent.KEYCODE_COMMA ||
event.code == KeyEvent.KEYCODE_PERIOD
if (action.code >= KeyEvent.KEYCODE_GRAVE &&
action.code <= KeyEvent.KEYCODE_SLASH ||
action.code == KeyEvent.KEYCODE_COMMA ||
action.code == KeyEvent.KEYCODE_PERIOD
) {
onKey(event.code, 0)
onKey(action.code, 0)
return
}
}
}
if (event.mask == 0) {
onKey(event.code, KeyboardSwitcher.currentKeyboard.modifer)
if (action.mask == 0) {
onKey(action.code, KeyboardSwitcher.currentKeyboard.modifer)
} else {
onKey(event.code, event.mask)
onKey(action.code, action.mask)
}
}
}
Expand Down Expand Up @@ -297,11 +297,11 @@ class CommonKeyboardActionListener(
}
BRACED_KEY_EVENT.matches(sequence) -> {
slice = BRACED_KEY_EVENT.matchEntire(sequence)?.groupValues?.get(1) ?: ""
onEvent(EventManager.getEvent(slice))
onAction(KeyActionManager.getAction(slice))
}
else -> {
slice = sequence.first().toString()
onEvent(EventManager.getEvent(slice))
onAction(KeyActionManager.getAction(slice))
}
}
sequence = sequence.substring(slice.length)
Expand Down
Loading

0 comments on commit 1ed4c7b

Please sign in to comment.