Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix 20241018 #1495

Merged
merged 4 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class SwitchesUi(
layoutParams = ViewGroup.LayoutParams(matchParent, matchParent)
layoutManager = horizontalLayoutManager()
adapter = switchesAdapter
isHorizontalScrollBarEnabled = false
isVerticalScrollBarEnabled = false
addItemDecoration(SpacesItemDecoration(dp(theme.generalStyle.candidateSpacing).toInt()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CompositionPopupWindow(
// 悬浮窗口彈出位置
private var popupWindowPos = PopupPosition.fromString(theme.generalStyle.layout.position)

private val mPopupWindow =
private val mPopupWindow by lazy {
PopupWindow(root).apply {
isClippingEnabled = false
inputMethodMode = PopupWindow.INPUT_METHOD_NOT_NEEDED
Expand All @@ -113,6 +113,7 @@ class CompositionPopupWindow(
.toFloat(),
)
}
}

var isCursorUpdated = false // 光標是否移動

Expand All @@ -129,11 +130,14 @@ class CompositionPopupWindow(
intArrayOf(0, 0).also {
anchor.getLocationInWindow(it)
}
root.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
val selfWidth = root.measuredWidth
val selfHeight = root.measuredHeight

val minX = anchor.dp(popupMarginH)
val minY = anchor.dp(popupMargin)
val maxX = anchor.width - root.width - minX
val maxY = anchorY - root.height - minY
val maxX = anchor.width - selfWidth - minX
val maxY = anchorY - selfHeight - minY
if (isWinFixed() || !isCursorUpdated) {
// setCandidatesViewShown(true);
when (popupWindowPos) {
Expand Down Expand Up @@ -174,7 +178,7 @@ class CompositionPopupWindow(
PopupPosition.LEFT, PopupPosition.RIGHT ->
y = mPopupRectF.bottom.toInt() + popupMargin
PopupPosition.LEFT_UP, PopupPosition.RIGHT_UP ->
y = mPopupRectF.top.toInt() - mPopupWindow.height - popupMargin
y = mPopupRectF.top.toInt() - selfHeight - popupMargin
else -> Timber.wtf("UNREACHABLE BRANCH")
}
y = MathUtils.clamp(y, minY, maxY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,9 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
)
} else if (hookKeyboard(keyEventCode, metaState)) {
Timber.d("\t<TrimeInput>\thandleKey()\thookKeyboard, keycode=%d", keyEventCode)
} else if (performEnter(keyEventCode) || handleBack(keyEventCode)) {
// 处理返回键(隐藏软键盘)和回车键(换行)
// todo 确认是否有必要单独处理回车键?是否需要把back和escape全部占用?
Timber.d("\t<TrimeInput>\thandleKey()\tEnterOrHide, keycode=%d", keyEventCode)
} else if (handleBack(keyEventCode)) {
// 处理返回键(隐藏软键盘)
Timber.d("handleKey(): Back, keycode=$keyEventCode")
} else if (openCategory(keyEventCode)) {
// 打开系统默认应用
Timber.d("\t<TrimeInput>\thandleKey()\topenCategory keycode=%d", keyEventCode)
Expand Down
116 changes: 57 additions & 59 deletions app/src/main/java/com/osfans/trime/ime/symbol/LiquidTabsUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package com.osfans.trime.ime.symbol
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.PaintDrawable
import android.widget.HorizontalScrollView
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.FontManager
import com.osfans.trime.data.theme.Theme
Expand All @@ -16,14 +18,14 @@ import splitties.dimensions.dp
import splitties.views.dsl.core.Ui
import splitties.views.dsl.core.add
import splitties.views.dsl.core.frameLayout
import splitties.views.dsl.core.horizontalLayout
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.textView
import splitties.views.dsl.core.wrapContent
import splitties.views.dsl.recyclerview.recyclerView
import splitties.views.gravityCenter
import splitties.views.gravityCenterVertical
import splitties.views.horizontalPadding
import splitties.views.recyclerview.horizontalLayoutManager

class LiquidTabsUi(
override val ctx: Context,
Expand All @@ -32,8 +34,6 @@ class LiquidTabsUi(
inner class TabUi : Ui {
override val ctx = this@LiquidTabsUi.ctx

var position: Int = -1

val text =
textView {
textSize = theme.generalStyle.candidateTextSize.toFloat()
Expand All @@ -51,9 +51,6 @@ class LiquidTabsUi(
},
)
background = rippleDrawable(ColorManager.getColor("hilited_candidate_back_color")!!)
setOnClickListener {
onTabClick(this@TabUi)
}
}

fun setText(str: String) {
Expand Down Expand Up @@ -81,71 +78,72 @@ class LiquidTabsUi(
}
}

private var tabs: Array<TabUi> = arrayOf()
private var selected = -1
private var onTabClick: ((Int) -> Unit)? = null

private class TabUiHolder(
val ui: LiquidTabsUi.TabUi,
) : RecyclerView.ViewHolder(ui.root)

private val adapter by lazy {
object : BaseQuickAdapter<TabTag, TabUiHolder>() {
private var selected = -1

override fun onCreateViewHolder(
context: Context,
parent: ViewGroup,
viewType: Int,
) = TabUiHolder(TabUi())

override fun onBindViewHolder(
holder: TabUiHolder,
position: Int,
item: TabTag?,
) {
holder.ui.apply {
setText(item!!.text)
setActive(position == selected)
root.run {
layoutParams = ViewGroup.LayoutParams(wrapContent, matchParent)
}
}
}

private var onTabClick: (TabUi.(Int) -> Unit)? = null
override fun submitList(list: List<TabTag>?) {
selected = -1
super.submitList(list)
}

private val horizontal = horizontalLayout()
fun activateTab(position: Int) {
if (position == selected) return
notifyItemChanged(selected)
selected = position
notifyItemChanged(position)
}
}.apply {
setOnItemClickListener { _, _, position ->
onTabClick?.invoke(position)
}
}
}

override val root =
HorizontalScrollView(ctx).apply {
recyclerView {
layoutManager = horizontalLayoutManager()
adapter = this@LiquidTabsUi.adapter
isVerticalScrollBarEnabled = false
isHorizontalScrollBarEnabled = false
add(
horizontal,
lParams(wrapContent, matchParent) {
gravity = gravityCenterVertical
},
)
post {
scrollX = tabs[selected].root.left
}
}

fun setTabs(tags: List<TabTag>) {
tabs.forEach { root.removeView(it.root) }
selected = -1
tabs =
Array(tags.size) {
val tag = tags[it]
TabUi().apply {
position = it
setText(tag.text)
setActive(false)
}
}
tabs.forEach { tabUi ->
horizontal.apply {
add(
tabUi.root,
lParams(wrapContent, matchParent) {
gravity = gravityCenter
},
)
}
}
adapter.submitList(tags)
}

fun activateTab(index: Int) {
if (index == selected) return
if (selected >= 0) {
tabs[selected].setActive(false)
}
tabs[index].also { tabUi ->
tabUi.setActive(true)
if (tabUi.root.left !in root.scrollX..root.scrollX + root.width) {
root.run { post { smoothScrollTo(tabUi.root.left, scrollY) } }
}
}
selected = index
}

private fun onTabClick(tabUi: TabUi) {
onTabClick?.invoke(tabUi, tabUi.position)
adapter.activateTab(index)
root.post { root.scrollToPosition(index) }
}

fun setOnTabClickListener(listener: (TabUi.(Int) -> Unit)? = null) {
fun setOnTabClickListener(listener: ((Int) -> Unit)? = null) {
onTabClick = listener
}
}
Loading