Skip to content

Commit

Permalink
fix: fix liquid keyboard (#1052)
Browse files Browse the repository at this point in the history
fix: fix two issues in liquid keyboard

1. fix: remove blank keys from TABS on the liquid keyboard.
2. fix: skip beans update with invisible liquid keyboard view.
  • Loading branch information
shitlime committed Jul 29, 2023
1 parent bf04d08 commit e2f9ec1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
14 changes: 12 additions & 2 deletions app/src/main/java/com/osfans/trime/data/db/ClipboardHelper.kt
Expand Up @@ -38,11 +38,21 @@ object ClipboardHelper :
private val onUpdateListeners = WeakHashSet<OnClipboardUpdateListener>()

fun addOnUpdateListener(listener: OnClipboardUpdateListener) {
onUpdateListeners.add(listener)
Timber.d("Add OnUpdateListener: $listener")
val result = onUpdateListeners.add(listener)
Timber.d(
"onUpdateListeners.add: result = $result," +
"onUpdateListeners.size = ${onUpdateListeners.size}",
)
}

fun removeOnUpdateListener(listener: OnClipboardUpdateListener) {
onUpdateListeners.remove(listener)
Timber.d("Remove OnUpdateListener: $listener")
val result = onUpdateListeners.remove(listener)
Timber.d(
"onUpdateListeners.remove: result = $result," +
"onUpdateListeners.size = ${onUpdateListeners.size}",
)
}

private val limit get() = AppPrefs.defaultInstance().clipboard.clipboardLimit
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Expand Up @@ -418,6 +418,8 @@ public void selectLiquidKeyboard(final int tabIndex) {
}
} else {
symbolKeyboardType = SymbolKeyboardType.NO_KEY;
// 设置液体键盘处于隐藏状态
TabManager.get().setTabExited();
symbolInput.setVisibility(View.GONE);
}
updateComposing();
Expand Down
31 changes: 21 additions & 10 deletions app/src/main/java/com/osfans/trime/ime/symbol/LiquidKeyboard.kt
Expand Up @@ -100,18 +100,23 @@ class LiquidKeyboard(private val context: Context) : ClipboardHelper.OnClipboard
}
}
} else {
val tag = TabManager.getTag(position)
val tag = tabManager.getTabSwitchTabTag(position)
val truePosition = tabManager.getTabSwitchPosition(position)
Timber.v(
"TABS click: " +
"position = $position, truePosition = $truePosition, tag.text = ${tag.text}",
)
if (tag.type === SymbolKeyboardType.NO_KEY) {
when (tag.command) {
KeyCommandType.EXIT -> service.selectLiquidKeyboard(-1)
KeyCommandType.DEL_LEFT, KeyCommandType.DEL_RIGHT, KeyCommandType.REDO, KeyCommandType.UNDO -> {}
else -> {}
}
} else if (tabManager.isAfterTabSwitch(position)) {
} else if (tabManager.isAfterTabSwitch(truePosition)) {
// tab的位置在“更多”的右侧,不滚动tab,焦点仍然在”更多“上
select(position)
select(truePosition)
} else {
service.selectLiquidKeyboard(position)
service.selectLiquidKeyboard(truePosition)
}
}
}
Expand All @@ -135,8 +140,10 @@ class LiquidKeyboard(private val context: Context) : ClipboardHelper.OnClipboard
when (tabTag.type) {
SymbolKeyboardType.HISTORY ->
simpleAdapter.updateBeans(symbolHistory.toOrderedList().map(::SimpleKeyBean))
SymbolKeyboardType.TABS ->
SymbolKeyboardType.TABS -> {
simpleAdapter.updateBeans(tabManager.tabSwitchData)
Timber.v("All tags in TABS: tabManager.tabSwitchData = ${tabManager.tabSwitchData}")
}
else ->
simpleAdapter.updateBeans(tabManager.select(i))
}
Expand Down Expand Up @@ -286,11 +293,15 @@ class LiquidKeyboard(private val context: Context) : ClipboardHelper.OnClipboard
* 当剪贴板内容变化且剪贴板视图处于开启状态时,更新视图.
*/
override fun onUpdate(text: String) {
val tag = TabManager.getTag(tabManager.selected)
// FIXME 先判断液体键盘是否打开,否则会在液体键盘未开启状态执行视图数据更新,可能浪费性能
if (tag.type == SymbolKeyboardType.CLIPBOARD) {
service.lifecycleScope.launch {
(keyboardView.adapter as FlexibleAdapter).updateBeans(ClipboardHelper.getAll())
val selected = tabManager.selected
// 判断液体键盘视图是否已开启,-1为未开启
if (selected >= 0) {
val tag = TabManager.getTag(selected)
if (tag.type == SymbolKeyboardType.CLIPBOARD) {
Timber.v("OnClipboardUpdateListener onUpdate: update clipboard view")
service.lifecycleScope.launch {
(keyboardView.adapter as FlexibleAdapter).updateBeans(ClipboardHelper.getAll())
}
}
}
}
Expand Down
38 changes: 37 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/symbol/TabManager.java
Expand Up @@ -31,11 +31,43 @@ public List<SimpleKeyBean> getTabSwitchData() {

for (TabTag tag : tabTags) {
if (SymbolKeyboardType.hasKey(tag.type)) tabSwitchData.add(new SimpleKeyBean(tag.text));
else tabSwitchData.add(new SimpleKeyBean(""));
}
return tabSwitchData;
}

/**
* 得到TABS中对应的TabTag 去除不显示的tagTab(没有keys列表的tagTab)之后按顺序排列tagTab,再从中获取TabTag
*
* @param position 位置(索引)
* @return TabTag
*/
public TabTag getTabSwitchTabTag(int position) {
int i = 0;
for (TabTag tag : tabTags) {
if (SymbolKeyboardType.hasKey(tag.type)) {
if (i++ == position) return tag;
}
}
return null;
}

/**
* 得到TABS中对应的真实索引 真实的索引是去除 没有keys列表的tagTab 之后按顺序排列的tagTab索引
*
* @param position 位置(索引)
* @return int TABS中显示的真实索引
*/
public int getTabSwitchPosition(int position) {
int i = 0;
for (TabTag tag : tabTags) {
if (SymbolKeyboardType.hasKey(tag.type)) {
if (position-- <= 0) break;
}
i++;
}
return i;
}

public static TabTag getTag(int i) {
return self.tabTags.get(i);
}
Expand Down Expand Up @@ -171,6 +203,10 @@ public int getSelected() {
return selected;
}

public void setTabExited() {
this.selected = -1;
}

public boolean isAfterTabSwitch(int position) {
return tabSwitchPosition <= position;
}
Expand Down

0 comments on commit e2f9ec1

Please sign in to comment.