Skip to content

Commit

Permalink
refactor: move SymbolKeyboardType to symbol package and rename to Sym…
Browse files Browse the repository at this point in the history
…bolBoardType
  • Loading branch information
WhiredPlanck committed Apr 26, 2024
1 parent 2bb3070 commit 73d6929
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 71 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.osfans.trime.databinding.CandidateBarBinding
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ime.dependency.InputScope
import com.osfans.trime.ime.enums.SymbolKeyboardType
import com.osfans.trime.ime.symbol.SymbolBoardType
import com.osfans.trime.ime.window.BoardWindow
import me.tatarka.inject.annotations.Inject
import splitties.views.dsl.core.add
Expand All @@ -33,7 +33,7 @@ class QuickBar(context: Context, service: TrimeInputMethodService, theme: Theme)
setPageStr(
{ service.handleKey(KeyEvent.KEYCODE_PAGE_DOWN, 0) },
{ service.handleKey(KeyEvent.KEYCODE_PAGE_UP, 0) },
{ service.selectLiquidKeyboard(SymbolKeyboardType.CANDIDATE) },
{ service.selectLiquidKeyboard(SymbolBoardType.CANDIDATE) },
)
}
with(candidates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ import com.osfans.trime.ime.broadcast.IntentReceiver
import com.osfans.trime.ime.enums.FullscreenMode
import com.osfans.trime.ime.enums.InlinePreeditMode
import com.osfans.trime.ime.enums.Keycode
import com.osfans.trime.ime.enums.SymbolKeyboardType
import com.osfans.trime.ime.keyboard.Event
import com.osfans.trime.ime.keyboard.InitializationUi
import com.osfans.trime.ime.keyboard.InputFeedbackManager
import com.osfans.trime.ime.keyboard.Key
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.ime.keyboard.KeyboardView
import com.osfans.trime.ime.symbol.SymbolBoardType
import com.osfans.trime.ime.symbol.TabManager
import com.osfans.trime.ime.text.Candidate
import com.osfans.trime.ime.text.CompositionPopupWindow
Expand Down Expand Up @@ -277,13 +277,13 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
if (name.matches("-?\\d+".toRegex())) {
selectLiquidKeyboard(name.toInt())
} else if (name.matches("[A-Z]+".toRegex())) {
selectLiquidKeyboard(SymbolKeyboardType.valueOf(name))
selectLiquidKeyboard(SymbolBoardType.valueOf(name))
} else {
selectLiquidKeyboard(TabManager.tabTags.indexOfFirst { it.text == name })
}
}

fun selectLiquidKeyboard(type: SymbolKeyboardType) {
fun selectLiquidKeyboard(type: SymbolBoardType) {
selectLiquidKeyboard(TabManager.tabTags.indexOfFirst { it.type == type })
}

Expand Down
35 changes: 17 additions & 18 deletions app/src/main/java/com/osfans/trime/ime/symbol/DbAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.osfans.trime.data.db.DatabaseBean
import com.osfans.trime.data.db.DraftHelper
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ime.enums.SymbolKeyboardType
import com.osfans.trime.util.ShortcutUtils
import kotlinx.coroutines.launch

Expand All @@ -23,7 +22,7 @@ class DbAdapter(
private val service: TrimeInputMethodService,
theme: Theme,
) : FlexibleAdapter(theme) {
var type = SymbolKeyboardType.CLIPBOARD
var type = SymbolBoardType.CLIPBOARD

override fun onPaste(bean: DatabaseBean) {
service.commitText(bean.text)
Expand All @@ -32,9 +31,9 @@ class DbAdapter(
override fun onPin(bean: DatabaseBean) {
service.lifecycleScope.launch {
when (type) {
SymbolKeyboardType.CLIPBOARD -> ClipboardHelper.pin(bean.id)
SymbolKeyboardType.COLLECTION -> CollectionHelper.pin(bean.id)
SymbolKeyboardType.DRAFT -> DraftHelper.pin(bean.id)
SymbolBoardType.CLIPBOARD -> ClipboardHelper.pin(bean.id)
SymbolBoardType.COLLECTION -> CollectionHelper.pin(bean.id)
SymbolBoardType.DRAFT -> DraftHelper.pin(bean.id)
else -> return@launch
}
}
Expand All @@ -43,9 +42,9 @@ class DbAdapter(
override fun onUnpin(bean: DatabaseBean) {
service.lifecycleScope.launch {
when (type) {
SymbolKeyboardType.CLIPBOARD -> ClipboardHelper.unpin(bean.id)
SymbolKeyboardType.COLLECTION -> CollectionHelper.unpin(bean.id)
SymbolKeyboardType.DRAFT -> DraftHelper.unpin(bean.id)
SymbolBoardType.CLIPBOARD -> ClipboardHelper.unpin(bean.id)
SymbolBoardType.COLLECTION -> CollectionHelper.unpin(bean.id)
SymbolBoardType.DRAFT -> DraftHelper.unpin(bean.id)
else -> return@launch
}
}
Expand All @@ -54,9 +53,9 @@ class DbAdapter(
override fun onDelete(bean: DatabaseBean) {
service.lifecycleScope.launch {
when (type) {
SymbolKeyboardType.CLIPBOARD -> ClipboardHelper.delete(bean.id)
SymbolKeyboardType.COLLECTION -> CollectionHelper.delete(bean.id)
SymbolKeyboardType.DRAFT -> DraftHelper.delete(bean.id)
SymbolBoardType.CLIPBOARD -> ClipboardHelper.delete(bean.id)
SymbolBoardType.COLLECTION -> CollectionHelper.delete(bean.id)
SymbolBoardType.DRAFT -> DraftHelper.delete(bean.id)
else -> return@launch
}
}
Expand All @@ -77,9 +76,9 @@ class DbAdapter(
// 如果没有未置顶的条目,则删除所有已置顶的条目
service.lifecycleScope.launch {
when (type) {
SymbolKeyboardType.CLIPBOARD -> ClipboardHelper.deleteAll(false)
SymbolKeyboardType.COLLECTION -> CollectionHelper.deleteAll(false)
SymbolKeyboardType.DRAFT -> DraftHelper.deleteAll(false)
SymbolBoardType.CLIPBOARD -> ClipboardHelper.deleteAll(false)
SymbolBoardType.COLLECTION -> CollectionHelper.deleteAll(false)
SymbolBoardType.DRAFT -> DraftHelper.deleteAll(false)
else -> return@launch
}
}
Expand All @@ -88,17 +87,17 @@ class DbAdapter(
// 如果有已置顶的条目,则删除所有未置顶的条目
service.lifecycleScope.launch {
when (type) {
SymbolKeyboardType.CLIPBOARD -> {
SymbolBoardType.CLIPBOARD -> {
ClipboardHelper.deleteAll()
updateBeans(ClipboardHelper.getAll())
}

SymbolKeyboardType.COLLECTION -> {
SymbolBoardType.COLLECTION -> {
CollectionHelper.deleteAll()
updateBeans(CollectionHelper.getAll())
}

SymbolKeyboardType.DRAFT -> {
SymbolBoardType.DRAFT -> {
DraftHelper.deleteAll()
updateBeans(DraftHelper.getAll())
}
Expand All @@ -121,5 +120,5 @@ class DbAdapter(
service.inputView?.showDialog(confirm)
}

override val showCollectButton: Boolean = type != SymbolKeyboardType.COLLECTION
override val showCollectButton: Boolean = type != SymbolBoardType.COLLECTION
}
39 changes: 19 additions & 20 deletions app/src/main/java/com/osfans/trime/ime/symbol/LiquidKeyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.osfans.trime.data.db.DraftHelper
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ime.dependency.InputScope
import com.osfans.trime.ime.enums.SymbolKeyboardType
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.ime.text.TextInputManager
import com.osfans.trime.ime.window.BoardWindow
Expand All @@ -43,7 +42,7 @@ class LiquidKeyboard(
) : BoardWindow.BarBoardWindow(), ResidentWindow, ClipboardHelper.OnClipboardUpdateListener {
private lateinit var liquidLayout: LiquidLayout
private val symbolHistory = SymbolHistory(180)
private lateinit var currentBoardType: SymbolKeyboardType
private lateinit var currentBoardType: SymbolBoardType
private lateinit var currentBoardAdapter: RecyclerView.Adapter<*>

private val simpleAdapter by lazy {
Expand All @@ -55,10 +54,10 @@ class LiquidKeyboard(
if (position in beans.indices) {
val bean = beans[position]
when (currentBoardType) {
SymbolKeyboardType.SYMBOL -> service.inputSymbol(bean.text)
SymbolBoardType.SYMBOL -> service.inputSymbol(bean.text)
else -> {
service.commitText(bean.text)
if (currentBoardType != SymbolKeyboardType.HISTORY) {
if (currentBoardType != SymbolBoardType.HISTORY) {
symbolHistory.insert(bean.text)
symbolHistory.save()
}
Expand Down Expand Up @@ -93,8 +92,8 @@ class LiquidKeyboard(
if (position in data.indices) {
val bean = data[position]
when (currentBoardType) {
SymbolKeyboardType.SYMBOL -> service.inputSymbol(bean.text)
SymbolKeyboardType.TABS -> {
SymbolBoardType.SYMBOL -> service.inputSymbol(bean.text)
SymbolBoardType.TABS -> {
val realPosition = TabManager.tabTags.indexOfFirst { it.text == bean.text }
select(realPosition)
}
Expand Down Expand Up @@ -157,22 +156,22 @@ class LiquidKeyboard(
StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
}

fun select(i: Int): SymbolKeyboardType {
fun select(i: Int): SymbolBoardType {
if (TabManager.currentTabIndex == i) return currentBoardType
val tag = TabManager.tabTags[i]
currentBoardType = tag.type
liquidLayout.tabsUi.activateTab(i)
symbolHistory.load()
val data by lazy { TabManager.selectTabByIndex(i) }
when (tag.type) {
SymbolKeyboardType.CLIPBOARD,
SymbolKeyboardType.COLLECTION,
SymbolKeyboardType.DRAFT,
SymbolBoardType.CLIPBOARD,
SymbolBoardType.COLLECTION,
SymbolBoardType.DRAFT,
-> initDbData()
SymbolKeyboardType.CANDIDATE -> initCandidates()
SymbolKeyboardType.VAR_LENGTH,
SymbolKeyboardType.SYMBOL,
SymbolKeyboardType.TABS,
SymbolBoardType.CANDIDATE -> initCandidates()
SymbolBoardType.VAR_LENGTH,
SymbolBoardType.SYMBOL,
SymbolBoardType.TABS,
-> initVarLengthKeys(data)
else -> initFixData(data)
}
Expand All @@ -194,7 +193,7 @@ class LiquidKeyboard(
}

when (currentBoardType) {
SymbolKeyboardType.HISTORY ->
SymbolBoardType.HISTORY ->
simpleAdapter.updateBeans(symbolHistory.toOrderedList().map(::SimpleKeyBean))
else ->
simpleAdapter.updateBeans(data)
Expand All @@ -218,9 +217,9 @@ class LiquidKeyboard(
service.lifecycleScope.launch {
val all =
when (currentBoardType) {
SymbolKeyboardType.CLIPBOARD -> ClipboardHelper.getAll()
SymbolKeyboardType.COLLECTION -> CollectionHelper.getAll()
SymbolKeyboardType.DRAFT -> DraftHelper.getAll()
SymbolBoardType.CLIPBOARD -> ClipboardHelper.getAll()
SymbolBoardType.COLLECTION -> CollectionHelper.getAll()
SymbolBoardType.DRAFT -> DraftHelper.getAll()
else -> emptyList()
}
dbAdapter.updateBeans(all)
Expand Down Expand Up @@ -258,7 +257,7 @@ class LiquidKeyboard(
}

val candidates =
if (currentBoardType === SymbolKeyboardType.SYMBOL) {
if (currentBoardType === SymbolBoardType.SYMBOL) {
data.map { b -> CandidateListItem("", b.label) }
} else {
data.map { b -> CandidateListItem("", b.text) }
Expand All @@ -275,7 +274,7 @@ class LiquidKeyboard(
// 判断液体键盘视图是否已开启,-1为未开启
if (selected >= 0) {
val tag = TabManager.tabTags[selected]
if (tag.type == SymbolKeyboardType.CLIPBOARD) {
if (tag.type == SymbolBoardType.CLIPBOARD) {
Timber.v("OnClipboardUpdateListener onUpdate: update clipboard view")
service.lifecycleScope.launch {
dbAdapter.updateBeans(ClipboardHelper.getAll())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

package com.osfans.trime.ime.enums
package com.osfans.trime.ime.symbol

enum class SymbolKeyboardType {
enum class SymbolBoardType {
// 不占据tab位,仅当在“更多”面板,即“TABS”中显示时,产生换行效果
NEW_ROW,

Expand Down Expand Up @@ -55,7 +55,7 @@ enum class SymbolKeyboardType {

companion object {
@JvmStatic
fun fromString(code: String?): SymbolKeyboardType {
fun fromString(code: String?): SymbolBoardType {
code ?: return SINGLE
return runCatching {
valueOf(code.uppercase())
Expand All @@ -64,13 +64,13 @@ enum class SymbolKeyboardType {

// 是否在liquidKeyboard键盘区域展示按键
@JvmStatic
fun hasKeys(type: SymbolKeyboardType): Boolean {
fun hasKeys(type: SymbolBoardType): Boolean {
return type > HISTORY
}

// 是否呈现在liquidKeyboard键盘区域的tabs列表中
@JvmStatic
fun hasKey(type: SymbolKeyboardType): Boolean {
fun hasKey(type: SymbolBoardType): Boolean {
return type >= CLIPBOARD
}
}
Expand Down
15 changes: 7 additions & 8 deletions app/src/main/java/com/osfans/trime/ime/symbol/TabManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package com.osfans.trime.ime.symbol

import com.osfans.trime.data.schema.SchemaManager
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.enums.SymbolKeyboardType
import com.osfans.trime.util.config.ConfigItem
import com.osfans.trime.util.config.ConfigList
import com.osfans.trime.util.config.ConfigMap
Expand All @@ -31,7 +30,7 @@ object TabManager {
val keyboard = theme.liquid.getMap(id) ?: continue
if (!keyboard.containsKey("type")) continue
val name = keyboard.getValue("name")?.getString() ?: id
val type = SymbolKeyboardType.fromString(keyboard.getValue("type")?.getString())
val type = SymbolBoardType.fromString(keyboard.getValue("type")?.getString())
val keys = keyboard["keys"]
addTabHasKeys(name, type, keys)
}
Expand All @@ -44,11 +43,11 @@ object TabManager {

private fun addListTab(
name: String,
type: SymbolKeyboardType,
type: SymbolBoardType,
keyBeans: List<SimpleKeyBean>,
) {
if (name.isBlank()) return
if (SymbolKeyboardType.hasKeys(type)) {
if (SymbolBoardType.hasKeys(type)) {
val index = tabTags.indexOfFirst { it.text == name }
if (index >= 0) {
keyboards[index] = keyBeans
Expand All @@ -61,15 +60,15 @@ object TabManager {

private fun addTabHasKeys(
name: String,
type: SymbolKeyboardType,
type: SymbolBoardType,
keys: ConfigItem?,
) {
if (keys is ConfigValue?) {
// 对于没有按键的类型,也要返回一个空的 key 值,否则无法显示在标签栏内
val key = keys?.configValue?.getString() ?: ""
when (type) {
// 处理 SINGLE 类型:把字符串切分为多个按键
SymbolKeyboardType.SINGLE -> addListTab(name, type, SimpleKeyDao.singleData(key))
SymbolBoardType.SINGLE -> addListTab(name, type, SimpleKeyDao.singleData(key))
else -> addListTab(name, type, SimpleKeyDao.simpleKeyboardData(key))
}
}
Expand Down Expand Up @@ -106,8 +105,8 @@ object TabManager {
if (index !in tabTags.indices) return listOf()
currentTabIndex = index
val tag = tabTags[index]
if (tag.type == SymbolKeyboardType.TABS) {
return tabTags.filter { SymbolKeyboardType.hasKey(it.type) }
if (tag.type == SymbolBoardType.TABS) {
return tabTags.filter { SymbolBoardType.hasKey(it.type) }
.map { SimpleKeyBean(it.text) }
}
return keyboards[index]
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/com/osfans/trime/ime/symbol/TabTag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package com.osfans.trime.ime.symbol

import com.osfans.trime.ime.enums.KeyCommandType
import com.osfans.trime.ime.enums.SymbolKeyboardType

// Tab是滑动键盘顶部的标签按钮(包含返回键)。
// 为了公用候选栏的皮肤参数以及外观,保持了和普通键盘布局相似的代码。此类相当于原键盘布局的Rime.RimeCandidate
Expand All @@ -15,14 +14,14 @@ data class TabTag(
/** additional comment of the tab */
val comment: String,
/** data type of the tab (unused) */
val type: SymbolKeyboardType,
val type: SymbolBoardType,
/** action type of the tab */
val command: KeyCommandType,
) {
constructor(text: String, type: SymbolKeyboardType) :
constructor(text: String, type: SymbolBoardType) :
this(text, "", type, KeyCommandType.NULL)
constructor(text: String, comment: String, type: SymbolKeyboardType) :
constructor(text: String, comment: String, type: SymbolBoardType) :
this(text, comment, type, KeyCommandType.NULL)
constructor(text: String, type: SymbolKeyboardType, command: KeyCommandType) :
constructor(text: String, type: SymbolBoardType, command: KeyCommandType) :
this(text, "", type, command)
}
Loading

0 comments on commit 73d6929

Please sign in to comment.