-
-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add logics & UIs for displaying inline suggestions
- Loading branch information
Showing
7 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/osfans/trime/ime/bar/ui/SuggestionUi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-FileCopyrightText: 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.ime.bar.ui | ||
|
||
import android.content.Context | ||
import android.view.View | ||
import com.osfans.trime.R | ||
import splitties.dimensions.dp | ||
import splitties.views.dsl.constraintlayout.after | ||
import splitties.views.dsl.constraintlayout.centerVertically | ||
import splitties.views.dsl.constraintlayout.constraintLayout | ||
import splitties.views.dsl.constraintlayout.endOfParent | ||
import splitties.views.dsl.constraintlayout.lParams | ||
import splitties.views.dsl.constraintlayout.startOfParent | ||
import splitties.views.dsl.core.Ui | ||
import splitties.views.dsl.core.add | ||
|
||
class SuggestionUi( | ||
override val ctx: Context, | ||
private val compatView: View, | ||
) : Ui { | ||
val homeButton = | ||
ToolButton(ctx, R.drawable.ic_trime_status) | ||
|
||
override val root = | ||
ctx.constraintLayout { | ||
add( | ||
homeButton, | ||
lParams(dp(40)) { | ||
centerVertically() | ||
startOfParent() | ||
}, | ||
) | ||
add( | ||
compatView, | ||
lParams { | ||
centerVertically() | ||
after(homeButton) | ||
endOfParent() | ||
}, | ||
) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/osfans/trime/ime/candidates/suggestion/SuggestionCandidateModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
package com.osfans.trime.ime.candidates.suggestion | ||
|
||
import android.content.Context | ||
import android.view.View | ||
import com.osfans.trime.R | ||
import com.osfans.trime.daemon.RimeSession | ||
import com.osfans.trime.data.theme.Theme | ||
import com.osfans.trime.ime.bar.QuickBar | ||
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver | ||
import com.osfans.trime.ime.core.TrimeInputMethodService | ||
import splitties.views.dsl.recyclerview.recyclerView | ||
import splitties.views.recyclerview.horizontalLayoutManager | ||
|
||
class SuggestionCandidateModule( | ||
val context: Context, | ||
val service: TrimeInputMethodService, | ||
val rime: RimeSession, | ||
val theme: Theme, | ||
val bar: QuickBar, | ||
) : InputBroadcastReceiver { | ||
val adapter by lazy { | ||
SuggestionCandidateViewAdapter(theme) | ||
} | ||
|
||
val view by lazy { | ||
context.recyclerView(R.id.suggestion_view) { | ||
adapter = this@SuggestionCandidateModule.adapter | ||
layoutManager = horizontalLayoutManager() | ||
isVerticalScrollBarEnabled = false | ||
isHorizontalScrollBarEnabled = false | ||
} | ||
} | ||
|
||
override fun onInlineSuggestion(views: List<View>) { | ||
adapter.updateCandidates( | ||
views.map { | ||
SuggestionViewItem(it) | ||
}, | ||
true, | ||
0, | ||
-1, | ||
) | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...rc/main/java/com/osfans/trime/ime/candidates/suggestion/SuggestionCandidateViewAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-FileCopyrightText: 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.ime.candidates.suggestion | ||
|
||
import android.content.Context | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.chad.library.adapter4.BaseQuickAdapter | ||
import com.osfans.trime.data.theme.Theme | ||
import splitties.dimensions.dp | ||
import splitties.views.dsl.core.matchParent | ||
import splitties.views.dsl.core.wrapContent | ||
import splitties.views.setPaddingDp | ||
|
||
open class SuggestionCandidateViewAdapter( | ||
val theme: Theme, | ||
) : BaseQuickAdapter<SuggestionViewItem, SuggestionViewHolder>() { | ||
var isLastPage: Boolean = false | ||
private set | ||
|
||
var previous: Int = 0 | ||
private set | ||
|
||
var highlightedIdx: Int = -1 | ||
private set | ||
|
||
fun updateCandidates( | ||
list: List<SuggestionViewItem>, | ||
isLastPage: Boolean, | ||
previous: Int, | ||
highlightedIdx: Int, | ||
) { | ||
this.isLastPage = isLastPage | ||
this.previous = previous | ||
this.highlightedIdx = highlightedIdx | ||
super.submitList(list) | ||
} | ||
|
||
override fun onCreateViewHolder( | ||
context: Context, | ||
parent: ViewGroup, | ||
viewType: Int, | ||
): SuggestionViewHolder { | ||
val ui = SuggestionItemUi(context, theme) | ||
ui.root.apply { | ||
minimumWidth = dp(40) | ||
val size = theme.generalStyle.candidatePadding | ||
setPaddingDp(size, 0, size, 0) | ||
layoutParams = RecyclerView.LayoutParams(wrapContent, matchParent) | ||
} | ||
return SuggestionViewHolder(ui) | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: SuggestionViewHolder, | ||
position: Int, | ||
item: SuggestionViewItem?, | ||
) { | ||
item ?: return | ||
val isHighlighted = theme.generalStyle.candidateUseCursor && position == highlightedIdx | ||
holder.ui.update(item, isHighlighted) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/osfans/trime/ime/candidates/suggestion/SuggestionItemUi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-FileCopyrightText: 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.ime.candidates.suggestion | ||
|
||
import android.content.Context | ||
import android.graphics.drawable.ColorDrawable | ||
import com.osfans.trime.data.theme.ColorManager | ||
import com.osfans.trime.data.theme.Theme | ||
import com.osfans.trime.util.pressHighlightDrawable | ||
import splitties.views.dsl.constraintlayout.constraintLayout | ||
import splitties.views.dsl.core.Ui | ||
|
||
class SuggestionItemUi( | ||
override val ctx: Context, | ||
theme: Theme, | ||
) : Ui { | ||
private val firstBackColorH = ColorManager.getColor("hilited_candidate_back_color")!! | ||
|
||
override val root = constraintLayout {} | ||
|
||
fun update( | ||
item: SuggestionViewItem, | ||
isHighlighted: Boolean, | ||
) { | ||
root.removeAllViews() | ||
root.addView(item.view) | ||
|
||
root.background = | ||
if (isHighlighted) { | ||
ColorDrawable(firstBackColorH) | ||
} else { | ||
pressHighlightDrawable(firstBackColorH) | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/osfans/trime/ime/candidates/suggestion/SuggestionViewHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-FileCopyrightText: 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.ime.candidates.suggestion | ||
|
||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class SuggestionViewHolder( | ||
val ui: SuggestionItemUi, | ||
) : RecyclerView.ViewHolder(ui.root) |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/osfans/trime/ime/candidates/suggestion/SuggestionViewItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015 - 2025 Rime community | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
package com.osfans.trime.ime.candidates.suggestion | ||
|
||
import android.view.View | ||
|
||
data class SuggestionViewItem( | ||
val view: View, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters