Skip to content

Commit

Permalink
refactor/perf(keyboard): improve KeyboardSwitch(er)
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Sep 21, 2021
1 parent 3c2da03 commit 65e4038
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 120 deletions.
30 changes: 17 additions & 13 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BarUtils;
import com.osfans.trime.R;
import com.osfans.trime.Rime;
Expand All @@ -68,11 +70,11 @@
import com.osfans.trime.ime.enums.InlineModeType;
import com.osfans.trime.ime.enums.WindowsPositionType;
import com.osfans.trime.ime.keyboard.Event;
import com.osfans.trime.ime.keyboard.InputFeedbackManager;
import com.osfans.trime.ime.keyboard.Key;
import com.osfans.trime.ime.keyboard.Keyboard;
import com.osfans.trime.ime.keyboard.KeyboardSwitch;
import com.osfans.trime.ime.keyboard.KeyboardSwitcher;
import com.osfans.trime.ime.keyboard.KeyboardView;
import com.osfans.trime.ime.keyboard.InputFeedbackManager;
import com.osfans.trime.ime.symbol.LiquidKeyboard;
import com.osfans.trime.ime.symbol.TabView;
import com.osfans.trime.ime.text.Candidate;
Expand All @@ -87,9 +89,11 @@
import com.osfans.trime.util.LocaleUtils;
import com.osfans.trime.util.ShortcutUtils;
import com.osfans.trime.util.StringUtils;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import timber.log.Timber;

/** {@link InputMethodService 輸入法}主程序 */
Expand All @@ -104,7 +108,7 @@ private Preferences getPrefs() {
}

private KeyboardView mKeyboardView; // 軟鍵盤
private KeyboardSwitch mKeyboardSwitch;
private KeyboardSwitcher keyboardSwitcher;
private Config mConfig; // 配置
@Nullable private InputFeedbackManager inputFeedbackManager = null; // 效果管理器
private Candidate mCandidate; // 候選
Expand Down Expand Up @@ -326,7 +330,7 @@ public void onCreate() {
mConfig = Config.get(this);
mNeedUpdateRimeOption = true;
loadConfig();
mKeyboardSwitch = new KeyboardSwitch(this);
keyboardSwitcher = new KeyboardSwitcher();

@Nullable String s;
s = mConfig.getString("locale");
Expand Down Expand Up @@ -386,10 +390,10 @@ public void onOptionChanged(@NonNull String option, boolean value) {
if (option.startsWith("_keyboard_")
&& option.length() > 10
&& value
&& (mKeyboardSwitch != null)) {
&& (keyboardSwitcher != null)) {
final String keyboard = option.substring(10);
mKeyboardSwitch.setKeyboard(keyboard);
mTempAsciiMode = mKeyboardSwitch.getAsciiMode();
keyboardSwitcher.switchToKeyboard(keyboard);
mTempAsciiMode = keyboardSwitcher.getAsciiMode();
bindKeyboardToInputView();
} else if (option.startsWith("_key_") && option.length() > 5 && value) {
boolean bNeedUpdate = mNeedUpdateRimeOption;
Expand Down Expand Up @@ -508,7 +512,7 @@ private void reset() {
mConfig.reset();
loadConfig();
mConfig.initCurrentColors();
if (mKeyboardSwitch != null) mKeyboardSwitch.reset(this);
if (keyboardSwitcher != null) keyboardSwitcher.newOrReset();
resetCandidate();
hideComposition();
resetKeyboard();
Expand Down Expand Up @@ -705,10 +709,10 @@ public void onStartInput(EditorInfo attribute, boolean restarting) {
Rime.get(this);
if (reset_ascii_mode) mAsciiMode = false;

mKeyboardSwitch.init(getMaxWidth()); // 橫豎屏切換時重置鍵盤
keyboardSwitcher.resize(getMaxWidth()); // 橫豎屏切換時重置鍵盤

// Select a keyboard based on the input type of the editing field.
mKeyboardSwitch.setKeyboard(keyboard);
keyboardSwitcher.switchToKeyboard(keyboard);
updateAsciiMode();
canCompose = canCompose && !Rime.isEmpty();
if (!onEvaluateInputViewShown()) setCandidatesViewShown(canCompose); // 實體鍵盤進入文本框時顯示候選欄
Expand Down Expand Up @@ -745,7 +749,7 @@ public void onFinishInputView(boolean finishingInput) {
private void bindKeyboardToInputView() {
if (mKeyboardView != null) {
// Bind the selected keyboard to the input view.
Keyboard sk = mKeyboardSwitch.getCurrentKeyboard();
Keyboard sk = keyboardSwitcher.getCurrentKeyboard();
mKeyboardView.setKeyboard(sk);
updateCursorCapsToInputView();
}
Expand Down Expand Up @@ -1017,9 +1021,9 @@ public void onEvent(@NonNull Event event) {
Rime.toggleOption(event.getToggle());
commitText();
} else if (code == KeyEvent.KEYCODE_EISU) { // 切換鍵盤
mKeyboardSwitch.setKeyboard(event.getSelect());
keyboardSwitcher.switchToKeyboard(event.getSelect());
// 根據鍵盤設定中英文狀態,不能放在 Rime.onMessage 中做
mTempAsciiMode = mKeyboardSwitch.getAsciiMode(); // 切換到西文鍵盤時不保存狀態
mTempAsciiMode = keyboardSwitcher.getAsciiMode(); // 切換到西文鍵盤時不保存狀態
updateAsciiMode();
bindKeyboardToInputView();
updateComposing();
Expand Down
107 changes: 0 additions & 107 deletions app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardSwitch.java

This file was deleted.

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
}

0 comments on commit 65e4038

Please sign in to comment.