|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2015 - 2025 Rime community |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +package com.osfans.trime.ime.candidates.suggestion |
| 7 | + |
| 8 | +import android.annotation.SuppressLint |
| 9 | +import android.content.Context |
| 10 | +import android.content.res.ColorStateList |
| 11 | +import android.graphics.Color |
| 12 | +import android.graphics.drawable.Icon |
| 13 | +import android.os.Build |
| 14 | +import android.util.Size |
| 15 | +import android.view.View |
| 16 | +import android.view.ViewGroup |
| 17 | +import android.view.inputmethod.InlineSuggestionsRequest |
| 18 | +import android.view.inputmethod.InlineSuggestionsResponse |
| 19 | +import android.widget.inline.InlinePresentationSpec |
| 20 | +import androidx.annotation.RequiresApi |
| 21 | +import androidx.autofill.inline.UiVersions |
| 22 | +import androidx.autofill.inline.common.ImageViewStyle |
| 23 | +import androidx.autofill.inline.common.TextViewStyle |
| 24 | +import androidx.autofill.inline.common.ViewStyle |
| 25 | +import androidx.autofill.inline.v1.InlineSuggestionUi |
| 26 | +import androidx.core.graphics.ColorUtils |
| 27 | +import com.osfans.trime.R |
| 28 | +import com.osfans.trime.data.theme.ColorManager |
| 29 | +import splitties.dimensions.dp |
| 30 | +import java.util.concurrent.Executor |
| 31 | +import kotlin.coroutines.resume |
| 32 | +import kotlin.coroutines.suspendCoroutine |
| 33 | + |
| 34 | +class InlineSuggestionHandler( |
| 35 | + private val context: Context, |
| 36 | +) { |
| 37 | + @SuppressLint("NewApi", "RestrictedApi") |
| 38 | + fun createRequest(): InlineSuggestionsRequest { |
| 39 | + val firstTextColor = ColorManager.getColor("candidate_text_color")!! |
| 40 | + val backColor = ColorManager.getColor("candidate_background")!! |
| 41 | + |
| 42 | + val style = |
| 43 | + InlineSuggestionUi |
| 44 | + .newStyleBuilder() |
| 45 | + .setSingleIconChipStyle( |
| 46 | + ViewStyle |
| 47 | + .Builder() |
| 48 | + .setBackgroundColor(Color.TRANSPARENT) |
| 49 | + .setPadding(0, 0, 0, 0) |
| 50 | + .build(), |
| 51 | + ).setChipStyle( |
| 52 | + ViewStyle |
| 53 | + .Builder() |
| 54 | + .setBackground( |
| 55 | + Icon.createWithResource(context, R.drawable.bg_inline_suggestion).apply { |
| 56 | + setTint(ColorUtils.blendARGB(backColor, firstTextColor, 0.2f)) |
| 57 | + }, |
| 58 | + ).build(), |
| 59 | + ).setTitleStyle( |
| 60 | + TextViewStyle |
| 61 | + .Builder() |
| 62 | + .setLayoutMargin(context.dp(4), 0, context.dp(4), 0) |
| 63 | + .setTextColor(firstTextColor) |
| 64 | + .setTextSize(14f) |
| 65 | + .build(), |
| 66 | + ).setSubtitleStyle( |
| 67 | + TextViewStyle |
| 68 | + .Builder() |
| 69 | + .setTextColor( |
| 70 | + ColorUtils.blendARGB(firstTextColor, backColor, 0.3f), |
| 71 | + ).setTextSize(12f) |
| 72 | + .build(), |
| 73 | + ).setStartIconStyle( |
| 74 | + ImageViewStyle |
| 75 | + .Builder() |
| 76 | + .setTintList(ColorStateList.valueOf(firstTextColor)) |
| 77 | + .build(), |
| 78 | + ).setEndIconStyle( |
| 79 | + ImageViewStyle |
| 80 | + .Builder() |
| 81 | + .setTintList(ColorStateList.valueOf(firstTextColor)) |
| 82 | + .build(), |
| 83 | + ).build() |
| 84 | + val styleBundle = |
| 85 | + UiVersions |
| 86 | + .newStylesBuilder() |
| 87 | + .addStyle(style) |
| 88 | + .build() |
| 89 | + val spec = |
| 90 | + InlinePresentationSpec |
| 91 | + .Builder(Size(0, 0), Size(context.dp(160), Int.MAX_VALUE)) |
| 92 | + .setStyle(styleBundle) |
| 93 | + .build() |
| 94 | + return InlineSuggestionsRequest |
| 95 | + .Builder(listOf(spec)) |
| 96 | + .setMaxSuggestionCount(InlineSuggestionsRequest.SUGGESTION_COUNT_UNLIMITED) |
| 97 | + .build() |
| 98 | + } |
| 99 | + |
| 100 | + private val suggestionSize by lazy { |
| 101 | + Size(ViewGroup.LayoutParams.WRAP_CONTENT, context.dp(INLINE_SUGGESTION_HEIGHT)) |
| 102 | + } |
| 103 | + |
| 104 | + @RequiresApi(Build.VERSION_CODES.R) |
| 105 | + suspend fun inflateSuggestion(response: InlineSuggestionsResponse): List<View> = |
| 106 | + response.inlineSuggestions.map { |
| 107 | + suspendCoroutine { c -> |
| 108 | + it.inflate(context, suggestionSize, directExecutor) { v -> |
| 109 | + c.resume(v) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + companion object { |
| 115 | + private const val INLINE_SUGGESTION_HEIGHT = 40 |
| 116 | + |
| 117 | + private val directExecutor by lazy { |
| 118 | + Executor { it.run() } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments