From 8278de948402ec9943037436b2370b4722bdc72f Mon Sep 17 00:00:00 2001 From: Aleksandras Kostarevas Date: Sat, 26 Aug 2023 20:39:55 +0300 Subject: [PATCH] Inflate inline suggestions only once --- .../org/futo/inputmethod/latin/LatinIME.kt | 9 ++- .../futo/inputmethod/latin/uix/ActionBar.kt | 5 +- .../latin/uix/InlineSuggestionView.kt | 64 +++++++++---------- .../org/futo/inputmethod/latin/uix/Utils.kt | 6 ++ 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/java/src/org/futo/inputmethod/latin/LatinIME.kt b/java/src/org/futo/inputmethod/latin/LatinIME.kt index 2b01aa3c7..b5b12ba2b 100644 --- a/java/src/org/futo/inputmethod/latin/LatinIME.kt +++ b/java/src/org/futo/inputmethod/latin/LatinIME.kt @@ -26,6 +26,7 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableState import androidx.compose.runtime.key import androidx.compose.ui.Alignment.Companion.CenterVertically import androidx.compose.ui.Modifier @@ -51,6 +52,7 @@ import androidx.savedstate.SavedStateRegistryOwner import androidx.savedstate.findViewTreeSavedStateRegistryOwner import androidx.savedstate.setViewTreeSavedStateRegistryOwner import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.runBlocking import org.futo.inputmethod.latin.common.Constants import org.futo.inputmethod.latin.uix.Action @@ -64,6 +66,7 @@ import org.futo.inputmethod.latin.uix.createInlineSuggestionsRequest import org.futo.inputmethod.latin.uix.deferGetSetting import org.futo.inputmethod.latin.uix.deferSetSetting import org.futo.inputmethod.latin.uix.differsFrom +import org.futo.inputmethod.latin.uix.inflateInlineSuggestion import org.futo.inputmethod.latin.uix.theme.DarkColorScheme import org.futo.inputmethod.latin.uix.theme.ThemeOption import org.futo.inputmethod.latin.uix.theme.ThemeOptions @@ -123,7 +126,7 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save return currWindowAction != null } - private var inlineSuggestions: List = listOf() + private var inlineSuggestions: List> = listOf() private fun recreateKeyboard() { legacyInputView = latinIMELegacy.onCreateInputView() @@ -546,7 +549,9 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save @RequiresApi(Build.VERSION_CODES.R) override fun onInlineSuggestionsResponse(response: InlineSuggestionsResponse): Boolean { - inlineSuggestions = response.inlineSuggestions + inlineSuggestions = response.inlineSuggestions.map { + inflateInlineSuggestion(it) + } setContent() return true diff --git a/java/src/org/futo/inputmethod/latin/uix/ActionBar.kt b/java/src/org/futo/inputmethod/latin/uix/ActionBar.kt index ce8ce70d4..e7ea69710 100644 --- a/java/src/org/futo/inputmethod/latin/uix/ActionBar.kt +++ b/java/src/org/futo/inputmethod/latin/uix/ActionBar.kt @@ -1,6 +1,7 @@ package org.futo.inputmethod.latin.uix import android.os.Build +import android.view.View import android.view.inputmethod.InlineSuggestion import androidx.annotation.RequiresApi import androidx.compose.foundation.Canvas @@ -27,6 +28,7 @@ import androidx.compose.material3.TextButton import androidx.compose.material3.dynamicDarkColorScheme import androidx.compose.material3.dynamicLightColorScheme import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment.Companion.CenterVertically @@ -55,6 +57,7 @@ import androidx.compose.ui.unit.Constraints import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import kotlinx.coroutines.flow.Flow import org.futo.inputmethod.latin.R import org.futo.inputmethod.latin.SuggestedWords import org.futo.inputmethod.latin.SuggestedWords.SuggestedWordInfo @@ -353,7 +356,7 @@ fun ActionBar( words: SuggestedWords?, suggestionStripListener: SuggestionStripView.Listener, onActionActivated: (Action) -> Unit, - inlineSuggestions: List, + inlineSuggestions: List>, forceOpenActionsInitially: Boolean = false, ) { val isActionsOpen = remember { mutableStateOf(forceOpenActionsInitially) } diff --git a/java/src/org/futo/inputmethod/latin/uix/InlineSuggestionView.kt b/java/src/org/futo/inputmethod/latin/uix/InlineSuggestionView.kt index 88121fb8a..154d1aec5 100644 --- a/java/src/org/futo/inputmethod/latin/uix/InlineSuggestionView.kt +++ b/java/src/org/futo/inputmethod/latin/uix/InlineSuggestionView.kt @@ -4,11 +4,10 @@ import android.annotation.SuppressLint import android.content.Context import android.os.Build import android.util.Size -import android.util.TypedValue +import android.view.View import android.view.ViewGroup import android.view.inputmethod.InlineSuggestion import android.view.inputmethod.InlineSuggestionsRequest -import android.widget.inline.InlineContentView import android.widget.inline.InlinePresentationSpec import androidx.annotation.RequiresApi import androidx.autofill.inline.UiVersions @@ -21,19 +20,15 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyRow import androidx.compose.material3.ColorScheme import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue +import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.toArgb -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidView import kotlin.math.roundToInt + @SuppressLint("RestrictedApi") @RequiresApi(Build.VERSION_CODES.R) fun createInlineSuggestionsRequest( @@ -41,11 +36,7 @@ fun createInlineSuggestionsRequest( activeColorScheme: ColorScheme ): InlineSuggestionsRequest { val fromDp = { v: Float -> - TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - v, - context.resources.displayMetrics - ).roundToInt() + context.fromDp(v).roundToInt() } val stylesBuilder = UiVersions.newStylesBuilder() @@ -113,30 +104,31 @@ fun createInlineSuggestionsRequest( } } +@RequiresApi(Build.VERSION_CODES.R) +fun Context.inflateInlineSuggestion(inlineSuggestion: InlineSuggestion): MutableState { + val mutableState: MutableState = mutableStateOf(null) + + val size = Size(ViewGroup.LayoutParams.WRAP_CONTENT, fromDp(32f).roundToInt()) + + try { + inlineSuggestion.inflate(this, size, mainExecutor) { inflatedView -> + if (inflatedView != null) { + mutableState.value = inflatedView + } + } + } catch (e: Exception) { + println(e.toString()) + } + + return mutableState +} @RequiresApi(Build.VERSION_CODES.R) @Composable -fun InlineSuggestionView(inlineSuggestion: InlineSuggestion) = with(LocalDensity.current) { - val context = LocalContext.current - - val size = Size(ViewGroup.LayoutParams.WRAP_CONTENT, 32.dp.toPx().toInt()) - var inlineContentView by remember { mutableStateOf(null) } - - LaunchedEffect(Unit) { - try { - inlineSuggestion.inflate(context, size, context.mainExecutor) { inflatedView -> - if (inflatedView != null) { - inlineContentView = inflatedView - } - } - } catch (e: Exception) { - println(e.toString()) - } - } - - if (inlineContentView != null) { +fun InlineSuggestionView(inlineSuggestion: MutableState) { + if (inlineSuggestion.value != null) { AndroidView( - factory = { inlineContentView!! }, + factory = { inlineSuggestion.value!! }, modifier = Modifier.padding(4.dp, 0.dp) ) } @@ -144,8 +136,10 @@ fun InlineSuggestionView(inlineSuggestion: InlineSuggestion) = with(LocalDensity @RequiresApi(Build.VERSION_CODES.R) @Composable -fun RowScope.InlineSuggestions(suggestions: List) { - LazyRow(modifier = Modifier.weight(1.0f).padding(0.dp, 4.dp)) { +fun RowScope.InlineSuggestions(suggestions: List>) { + LazyRow(modifier = Modifier + .weight(1.0f) + .padding(0.dp, 4.dp)) { items(suggestions.size) { InlineSuggestionView(suggestions[it]) } diff --git a/java/src/org/futo/inputmethod/latin/uix/Utils.kt b/java/src/org/futo/inputmethod/latin/uix/Utils.kt index c22dd5ed6..07849c5ff 100644 --- a/java/src/org/futo/inputmethod/latin/uix/Utils.kt +++ b/java/src/org/futo/inputmethod/latin/uix/Utils.kt @@ -1,5 +1,7 @@ package org.futo.inputmethod.latin.uix +import android.content.Context +import android.util.TypedValue import androidx.compose.material3.ColorScheme // Not exhaustive @@ -13,4 +15,8 @@ fun ColorScheme.differsFrom(other: ColorScheme): Boolean { || this.onSurface != other.onSurface || this.onBackground != other.onBackground || this.onPrimary != other.onPrimary +} + +fun Context.fromDp(v: Float): Float { + return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, v, resources.displayMetrics) } \ No newline at end of file