Skip to content

Commit 069a12f

Browse files
if-canWhiredPlanck
authored andcommitted
feat: page navigation using prevIcon and nextIcon components
1 parent 7a3232d commit 069a12f

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

app/src/main/java/com/osfans/trime/ime/candidates/popup/PagedCandidatesUi.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ class PagedCandidatesUi(
3939
) : UiHolder(ui)
4040
}
4141

42+
enum class ClickType {
43+
CANDIDATE,
44+
PREV_PAGE,
45+
NEXT_PAGE,
46+
}
47+
48+
private var clickListener: ((type: ClickType, position: Int) -> Unit)? = null
49+
50+
fun setOnClickListener(listener: (type: ClickType, position: Int) -> Unit) {
51+
clickListener = listener
52+
}
53+
4254
val candidatesAdapter =
4355
object : BaseQuickAdapter<RimeProto.Candidate, UiHolder>() {
4456
override fun getItemCount(items: List<RimeProto.Candidate>) =
@@ -75,13 +87,22 @@ class PagedCandidatesUi(
7587
is UiHolder.Candidate -> {
7688
val candidate = item ?: return
7789
holder.ui.update(candidate, position == menu.highlightedCandidateIndex)
90+
holder.ui.root.setOnClickListener {
91+
clickListener?.invoke(ClickType.CANDIDATE, position)
92+
}
7893
}
7994
is UiHolder.Pagination -> {
8095
holder.ui.update(menu)
8196
holder.ui.root.updateLayoutParams<FlexboxLayoutManager.LayoutParams> {
8297
width = if (isHorizontal) ViewGroup.LayoutParams.WRAP_CONTENT else ViewGroup.LayoutParams.MATCH_PARENT
8398
alignSelf = if (isHorizontal) AlignItems.CENTER else AlignItems.STRETCH
8499
}
100+
holder.ui.prevIcon.setOnClickListener {
101+
clickListener?.invoke(ClickType.PREV_PAGE, menu.pageNumber)
102+
}
103+
holder.ui.nextIcon.setOnClickListener {
104+
clickListener?.invoke(ClickType.NEXT_PAGE, menu.pageNumber)
105+
}
85106
}
86107
}
87108
}

app/src/main/java/com/osfans/trime/ime/candidates/popup/PaginationUi.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class PaginationUi(
3838
scaleType = ImageView.ScaleType.CENTER_CROP
3939
}
4040

41-
private val prevIcon = createIcon(R.drawable.ic_baseline_arrow_left_24)
42-
private val nextIcon = createIcon(R.drawable.ic_baseline_arrow_right_24)
41+
val prevIcon = createIcon(R.drawable.ic_baseline_arrow_left_24)
42+
val nextIcon = createIcon(R.drawable.ic_baseline_arrow_right_24)
4343

4444
private val disabledAlpha = ctx.styledFloat(android.R.attr.disabledAlpha)
4545

app/src/main/java/com/osfans/trime/ime/composition/CandidatesView.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
package com.osfans.trime.ime.composition
77

88
import android.annotation.SuppressLint
9+
import android.view.KeyEvent
910
import android.view.ViewGroup
1011
import androidx.constraintlayout.widget.ConstraintLayout
12+
import com.osfans.trime.core.RimeKeyMapping
1113
import com.osfans.trime.core.RimeProto
1214
import com.osfans.trime.daemon.RimeSession
1315
import com.osfans.trime.daemon.launchOnReady
@@ -49,8 +51,20 @@ class CandidatesView(
4951

5052
private val candidatesUi =
5153
PagedCandidatesUi(ctx, theme).apply {
52-
candidatesAdapter.setOnItemClickListener { _, _, position ->
53-
rime.launchOnReady { it.selectPagedCandidate(position) }
54+
setOnClickListener { type, position ->
55+
when (type) {
56+
PagedCandidatesUi.ClickType.CANDIDATE -> {
57+
rime.launchOnReady { it.selectPagedCandidate(position) }
58+
}
59+
PagedCandidatesUi.ClickType.PREV_PAGE -> {
60+
val value = RimeKeyMapping.keyCodeToVal(KeyEvent.KEYCODE_PAGE_UP)
61+
rime.launchOnReady { it.processKey(value, 0u) }
62+
}
63+
PagedCandidatesUi.ClickType.NEXT_PAGE -> {
64+
val value = RimeKeyMapping.keyCodeToVal(KeyEvent.KEYCODE_PAGE_DOWN)
65+
rime.launchOnReady { it.processKey(value, 0u) }
66+
}
67+
}
5468
}
5569
}
5670

0 commit comments

Comments
 (0)