-
-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/perf(keyboard): improve KeyboardSwitch(er)
- Loading branch information
1 parent
3c2da03
commit 65e4038
Showing
3 changed files
with
93 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 0 additions & 107 deletions
107
app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardSwitch.java
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardSwitcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.osfans.trime.ime.keyboard | ||
|
||
import com.osfans.trime.ime.core.Trime | ||
import com.osfans.trime.setup.Config | ||
|
||
/** Manages [Keyboard]s and their status. **/ | ||
class KeyboardSwitcher { | ||
var currentId: Int = -1 | ||
var lastId: Int = 0 | ||
var lastLockId: Int = 0 | ||
|
||
private var currentDisplayWidth: Int = 0 | ||
|
||
lateinit var keyboards: Array<Keyboard> | ||
lateinit var keyboardNames: List<String> | ||
|
||
/** To get current keyboard instance. **/ | ||
val currentKeyboard: Keyboard get() = keyboards[currentId] | ||
/** To get [currentKeyboard]'s ascii mode. **/ | ||
val asciiMode: Boolean get() = currentKeyboard.asciiMode | ||
|
||
init { | ||
newOrReset() | ||
} | ||
|
||
fun newOrReset() { | ||
val ims = Trime.getService() | ||
keyboardNames = Config.get(ims).keyboardNames | ||
keyboards = Array(keyboardNames.size) { i -> | ||
Keyboard(ims, keyboardNames[i]) | ||
} | ||
setKeyboard(0) | ||
} | ||
|
||
/** | ||
* Switch to a certain keyboard by given [name]. | ||
*/ | ||
fun switchToKeyboard(name: String?) { | ||
var i = if (currentId.isValidId()) currentId else 0 | ||
i = when { | ||
name.isNullOrEmpty() -> if (keyboards[i].isLock) i else lastLockId | ||
name.contentEquals(".default") -> 0 | ||
name.contentEquals(".prior") -> currentId - 1 | ||
name.contentEquals(".next") -> currentId + 1 | ||
name.contentEquals(".last") -> lastId | ||
name.contentEquals(".last_lock") -> lastLockId | ||
name.contentEquals(".ascii") -> { | ||
val asciiKeyboard = keyboards[i].asciiKeyboard | ||
if (asciiKeyboard.isEmpty()) { i } | ||
else { keyboardNames.indexOf(asciiKeyboard) } | ||
} | ||
else -> keyboardNames.indexOf(name) | ||
} | ||
setKeyboard(i) | ||
} | ||
|
||
/** | ||
* Change current display width when e.g. rotate the screen. | ||
*/ | ||
fun resize(displayWidth: Int) { | ||
if (currentId >= 0 && (displayWidth == currentDisplayWidth)) return | ||
|
||
currentDisplayWidth = displayWidth | ||
newOrReset() | ||
} | ||
|
||
private fun setKeyboard(id: Int) { | ||
lastId = currentId | ||
if (lastId.isValidId() && keyboards[lastId].isLock) { | ||
lastLockId = lastId | ||
} | ||
currentId = if (id.isValidId()) id else 0 | ||
} | ||
|
||
private fun Int.isValidId() = this in keyboards.indices | ||
} |