Skip to content

Commit

Permalink
refactor: bind composition view into its popup window
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and Bambooin committed Feb 7, 2024
1 parent f910e46 commit ed7aa14
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 172 deletions.
73 changes: 17 additions & 56 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import android.os.Looper
import android.os.Message
import android.text.InputType
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
Expand All @@ -49,7 +48,6 @@ import com.osfans.trime.data.AppPrefs
import com.osfans.trime.data.db.DraftHelper
import com.osfans.trime.data.sound.SoundThemeManager
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.databinding.CompositionRootBinding
import com.osfans.trime.ime.broadcast.IntentReceiver
import com.osfans.trime.ime.enums.Keycode
import com.osfans.trime.ime.enums.SymbolKeyboardType
Expand All @@ -65,7 +63,6 @@ import com.osfans.trime.ime.lifecycle.LifecycleInputMethodService
import com.osfans.trime.ime.symbol.TabManager
import com.osfans.trime.ime.symbol.TabView
import com.osfans.trime.ime.text.Candidate
import com.osfans.trime.ime.text.Composition
import com.osfans.trime.ime.text.CompositionPopupWindow
import com.osfans.trime.ime.text.ScrollView
import com.osfans.trime.ime.text.TextInputManager
Expand All @@ -76,7 +73,6 @@ import com.osfans.trime.util.isNightMode
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import splitties.bitflags.hasFlag
import splitties.dimensions.dp
import splitties.systemservices.inputMethodManager
import splitties.views.gravityBottom
import timber.log.Timber
Expand All @@ -90,8 +86,6 @@ open class Trime : LifecycleInputMethodService() {
get() = AppPrefs.defaultInstance()
private var mainKeyboardView: KeyboardView? = null // 主軟鍵盤
private var mCandidate: Candidate? = null // 候選
private var mComposition: Composition? = null // 編碼
private var compositionRootBinding: CompositionRootBinding? = null
private var mCandidateRoot: ScrollView? = null
private var mTabRoot: ScrollView? = null
private var tabView: TabView? = null
Expand All @@ -104,8 +98,6 @@ open class Trime : LifecycleInputMethodService() {
private var isAutoCaps = false // 句首自動大寫
var activeEditorInstance: EditorInstance? = null
var textInputManager: TextInputManager? = null // 文字输入管理器
private var minPopupSize = 0 // 上悬浮窗的候选词的最小词长
private var minPopupCheckSize = 0 // 第一屏候选词数量少于设定值,则候选词上悬浮窗。(也就是说,第一屏存在长词)此选项大于1时,min_length等参数失效
private var mCompositionPopupWindow: CompositionPopupWindow? = null
var candidateExPage = false

Expand Down Expand Up @@ -171,12 +163,9 @@ open class Trime : LifecycleInputMethodService() {

fun loadConfig() {
val theme = ThemeManager.activeTheme
minPopupSize = theme.style.getInt("layout/min_length")
minPopupCheckSize = theme.style.getInt("layout/min_check")
textInputManager!!.shouldResetAsciiMode = theme.style.getBoolean("reset_ascii_mode")
isAutoCaps = theme.style.getBoolean("auto_caps")
textInputManager!!.shouldUpdateRimeOption = true
mCompositionPopupWindow!!.loadConfig(theme, prefs)
}

private fun updateRimeOption(): Boolean {
Expand Down Expand Up @@ -236,7 +225,6 @@ open class Trime : LifecycleInputMethodService() {
textInputManager = TextInputManager.getInstance()
activeEditorInstance = EditorInstance(context)
inputFeedbackManager = InputFeedbackManager(context)
mCompositionPopupWindow = CompositionPopupWindow()
restartSystemStartTimingSync()
try {
for (listener in eventListeners) {
Expand Down Expand Up @@ -271,7 +259,8 @@ open class Trime : LifecycleInputMethodService() {
symbolKeyboardType = inputView!!.liquidKeyboard.select(tabIndex)
tabView!!.updateTabWidth()
mTabRoot!!.move(tabView!!.hightlightLeft, tabView!!.hightlightRight)
showLiquidKeyboardToolbar()
mCompositionPopupWindow?.composition?.compositionView?.changeToLiquidKeyboardToolbar()
showCompositionView(false)
} else {
symbolKeyboardType = SymbolKeyboardType.NO_KEY
// 设置液体键盘处于隐藏状态
Expand Down Expand Up @@ -305,31 +294,11 @@ open class Trime : LifecycleInputMethodService() {
mCompositionPopupWindow!!.hideCompositionView()
return
}
compositionRootBinding!!.compositionRoot.measure(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
)
mCompositionPopupWindow!!.updateCompositionView(
compositionRootBinding!!.compositionRoot.measuredWidth,
compositionRootBinding!!.compositionRoot.measuredHeight,
)
mCompositionPopupWindow?.updateCompositionView()
}

private fun loadBackground(recreate: Boolean) {
val theme = ThemeManager.activeTheme
val textBackground =
theme.colors.getDrawable(
"text_back_color",
"layout/border",
"border_color",
"layout/round_corner",
"layout/alpha",
)
mCompositionPopupWindow!!.setThemeStyle(
dp(theme.style.getFloat("layout/elevation")),
textBackground,
)

if (inputView == null || !recreate) return

inputView?.quickBar?.view?.background =
Expand Down Expand Up @@ -365,12 +334,10 @@ open class Trime : LifecycleInputMethodService() {
View.GONE
}
mCandidate!!.reset()
mCompositionPopupWindow!!.isPopupWindowEnabled = (
prefs.keyboard.popupWindowEnabled &&
ThemeManager.activeTheme.style.getObject("window") != null
)
mComposition!!.visibility = if (mCompositionPopupWindow!!.isPopupWindowEnabled) View.VISIBLE else View.GONE
mComposition!!.reset()
mCompositionPopupWindow =
CompositionPopupWindow(this, ThemeManager.activeTheme).apply {
anchorView = inputView?.quickBar?.view
}
}
}

Expand Down Expand Up @@ -410,9 +377,7 @@ open class Trime : LifecycleInputMethodService() {
listener.onDestroy()
}
eventListeners.clear()
if (mCompositionPopupWindow != null) {
mCompositionPopupWindow!!.destroy()
}
mCompositionPopupWindow?.finishInput()
ThemeManager.removeOnChangedListener(onThemeChangeListener)
super.onDestroy()
self = null
Expand Down Expand Up @@ -511,15 +476,15 @@ open class Trime : LifecycleInputMethodService() {
Timber.d("onCreateInputView()")
RimeWrapper.runAfterStarted {
inputView = InputView(this, Rime.getInstance(false), ThemeManager.activeTheme)
mCompositionPopupWindow =
CompositionPopupWindow(this, ThemeManager.activeTheme).apply {
anchorView = inputView!!.quickBar.view
}
mainKeyboardView = inputView!!.keyboardWindow.oldMainInputView.mainKeyboardView
// 初始化候选栏
mCandidateRoot = inputView!!.quickBar.oldCandidateBar.root
mCandidate = inputView!!.quickBar.oldCandidateBar.candidates

// 候选词悬浮窗的容器
compositionRootBinding = CompositionRootBinding.inflate(LayoutInflater.from(this))
mComposition = compositionRootBinding!!.compositions
mCompositionPopupWindow!!.init(compositionRootBinding!!.compositionRoot, mCandidateRoot!!)
mTabRoot = inputView!!.quickBar.oldTabBar.root

tabView = inputView!!.quickBar.oldTabBar.tabs
Expand Down Expand Up @@ -559,7 +524,6 @@ open class Trime : LifecycleInputMethodService() {

fun setShowComment(showComment: Boolean) {
if (mCandidateRoot != null) mCandidate!!.setShowComment(showComment)
mComposition!!.setShowComment(showComment)
}

override fun onStartInput(
Expand Down Expand Up @@ -1013,13 +977,15 @@ open class Trime : LifecycleInputMethodService() {
if (mCandidateRoot != null) {
if (mCompositionPopupWindow!!.isPopupWindowEnabled) {
Timber.d("updateComposing() SymbolKeyboardType=%s", symbolKeyboardType.toString())
val composition = mCompositionPopupWindow?.composition
if (symbolKeyboardType != SymbolKeyboardType.NO_KEY &&
symbolKeyboardType != SymbolKeyboardType.CANDIDATE
) {
showLiquidKeyboardToolbar()
composition?.compositionView?.changeToLiquidKeyboardToolbar()
showCompositionView(false)
} else {
mComposition!!.visibility = View.VISIBLE
startNum = mComposition!!.setWindow(minPopupSize, minPopupCheckSize, Int.MAX_VALUE)
composition?.root?.visibility = View.VISIBLE
startNum = composition?.compositionView?.setWindowContent() ?: 0
mCandidate!!.setText(startNum)
// if isCursorUpdated, showCompositionView will be called in onUpdateCursorAnchorInfo
// otherwise we need to call it here
Expand All @@ -1036,11 +1002,6 @@ open class Trime : LifecycleInputMethodService() {
return startNum
}

private fun showLiquidKeyboardToolbar() {
mComposition!!.changeToLiquidKeyboardToolbar()
showCompositionView(false)
}

/**
* 如果爲回車鍵[KeyEvent.KEYCODE_ENTER],則換行
*
Expand Down
31 changes: 9 additions & 22 deletions app/src/main/java/com/osfans/trime/ime/text/Composition.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import com.osfans.trime.core.CandidateListItem;
import com.osfans.trime.core.Rime;
import com.osfans.trime.core.RimeComposition;
Expand All @@ -56,7 +56,7 @@
import timber.log.Timber;

/** 編碼區,顯示已輸入的按鍵編碼,可使用方向鍵或觸屏移動光標位置 */
public class Composition extends AppCompatTextView {
public class Composition extends TextView {
private int key_text_size, text_size, label_text_size, candidate_text_size, comment_text_size;
private int key_text_color, text_color, label_color, candidate_text_color, comment_text_color;
private int hilited_text_color, hilited_candidate_text_color, hilited_comment_text_color;
Expand Down Expand Up @@ -537,23 +537,12 @@ private void appendMove(Map<String, Object> m) {
/**
* 设置悬浮窗文本
*
* @param charLength 候选词长度大于设定,才会显示到悬浮窗中
* @param minCheck 检查至少多少个候选词。当首选词长度不足时,继续检查后方候选词
* @param maxPopup 最多在悬浮窗显示多少个候选词
* @return 悬浮窗显示的候选词数量
*/
public int setWindow(int charLength, int minCheck, int maxPopup) {
return setWindow(charLength, minCheck);
}

/**
* 设置悬浮窗文本
*
* @param stringMinLength 候选词长度大于设定,才会显示到悬浮窗中
* @param candidateMinCheck 检查至少多少个候选词。当首选词长度不足时,继续检查后方候选词
* @return 悬浮窗显示的候选词数量
*/
public int setWindow(int stringMinLength, int candidateMinCheck) {
public int setWindowContent() {
final Theme theme = ThemeManager.getActiveTheme();
int minLen = theme.style.getInt("layout/min_length"); // 候选词长度大于设定,才会显示到悬浮窗中
int minCheck = theme.style.getInt("layout/min_check"); // 检查至少多少个候选词。当首选词长度不足时,继续检查后方候选词
if (getVisibility() != View.VISIBLE) return 0;
StackTraceElement[] stacks = new Throwable().getStackTrace();
Timber.d(
Expand All @@ -575,11 +564,9 @@ public int setWindow(int stringMinLength, int candidateMinCheck) {
for (Map<String, Object> m : windows_comps) {
if (m.containsKey("composition")) appendComposition(m);
else if (m.containsKey("candidate")) {
start_num = calcStartNum(stringMinLength, candidateMinCheck);
Timber.d(
"start_num = %s, min_length = %s, min_check = %s",
start_num, stringMinLength, candidateMinCheck);
appendCandidates(m, stringMinLength, start_num);
start_num = calcStartNum(minLen, minCheck);
Timber.d("start_num = %s, min_length = %s, min_check = %s", start_num, minLen, minCheck);
appendCandidates(m, minLen, start_num);
} else if (m.containsKey("click")) appendButton(m);
else if (m.containsKey("move")) appendMove(m);
}
Expand Down
Loading

0 comments on commit ed7aa14

Please sign in to comment.