Add ActionTextEditor

This commit is contained in:
Aleksandras Kostarevas 2024-06-08 17:21:48 +03:00
parent 0c4659004a
commit 412fdd0d0c
6 changed files with 127 additions and 16 deletions

View File

@ -12,6 +12,7 @@ import android.view.inputmethod.CompletionInfo
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InlineSuggestionsRequest
import android.view.inputmethod.InlineSuggestionsResponse
import android.view.inputmethod.InputConnection
import android.view.inputmethod.InputMethodSubtype
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.padding
@ -663,6 +664,11 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save
}
}
var overrideInputConnection: InputConnection? = null
override fun getCurrentInputConnection(): InputConnection? {
return overrideInputConnection ?: super.getCurrentInputConnection()
}
override val lifecycle: Lifecycle
get() = mLifecycleRegistry
override val savedStateRegistry: SavedStateRegistry

View File

@ -142,7 +142,7 @@ public class LatinIMELegacy implements KeyboardActionListener,
private static final String SCHEME_PACKAGE = "package";
public static boolean mPendingDictionaryUpdate = false;
final Settings mSettings;
public final Settings mSettings;
private Locale mLocale;
final DictionaryFacilitator mDictionaryFacilitator =
DictionaryFacilitatorProvider.getDictionaryFacilitator(

View File

@ -2,6 +2,7 @@ package org.futo.inputmethod.latin.uix
import android.content.Context
import android.view.View
import android.view.inputmethod.InputConnection
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
@ -42,6 +43,9 @@ interface KeyboardManagerForAction {
fun performHapticAndAudioFeedback(code: Int, view: View)
fun announce(s: String)
fun getActiveLocale(): Locale
fun overrideInputConnection(inputConnection: InputConnection)
fun unsetInputConnection()
}
interface ActionWindow {

View File

@ -0,0 +1,70 @@
package org.futo.inputmethod.latin.uix
import android.content.Context
import android.text.InputType
import android.util.AttributeSet
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
class ActionEditText(context: Context, val textChanged: (String) -> Unit) :
androidx.appcompat.widget.AppCompatEditText(context) {
var inputConnection: InputConnection? = null
private set
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? {
inputConnection = super.onCreateInputConnection(outAttrs)
return inputConnection
}
override fun onTextChanged(
text: CharSequence?,
start: Int,
lengthBefore: Int,
lengthAfter: Int
) {
super.onTextChanged(text, start, lengthBefore, lengthAfter)
textChanged(text?.toString() ?: "")
}
}
@Composable
fun ActionTextEditor(text: MutableState<String>) {
val context = LocalContext.current
val manager = LocalManager.current
AndroidView(
factory = {
ActionEditText(context) {
text.value = it
}.apply {
onCreateInputConnection(
EditorInfo()
)
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
inputType = InputType.TYPE_CLASS_TEXT
manager.overrideInputConnection(inputConnection!!)
}
},
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
onRelease = {
manager.unsetInputConnection()
}
)
}

View File

@ -9,6 +9,7 @@ import android.os.Vibrator
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InlineSuggestionsResponse
import android.view.inputmethod.InputConnection
import androidx.annotation.RequiresApi
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
@ -28,8 +29,10 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
@ -50,6 +53,7 @@ import org.futo.inputmethod.latin.BuildConfig
import org.futo.inputmethod.latin.LanguageSwitcherDialog
import org.futo.inputmethod.latin.LatinIME
import org.futo.inputmethod.latin.R
import org.futo.inputmethod.latin.RichInputMethodManager
import org.futo.inputmethod.latin.SuggestedWords
import org.futo.inputmethod.latin.SuggestedWords.SuggestedWordInfo
import org.futo.inputmethod.latin.common.Constants
@ -69,6 +73,10 @@ import org.futo.inputmethod.updates.openManualUpdateCheck
import org.futo.inputmethod.updates.retrieveSavedLastUpdateCheckResult
import java.util.Locale
val LocalManager = staticCompositionLocalOf<KeyboardManagerForAction> {
error("No LocalManager provided")
}
private class LatinIMEActionInputTransaction(
private val inputLogic: InputLogic,
shouldApplySpace: Boolean,
@ -189,6 +197,18 @@ class UixActionKeyboardManager(val uixManager: UixManager, val latinIME: LatinIM
return latinIME.latinIMELegacy.locale
}
override fun overrideInputConnection(inputConnection: InputConnection) {
latinIME.overrideInputConnection = inputConnection
latinIME.inputLogic.startInput(RichInputMethodManager.getInstance().combiningRulesExtraValueOfCurrentSubtype,
latinIME.latinIMELegacy.mSettings.current)
}
override fun unsetInputConnection() {
latinIME.overrideInputConnection = null
latinIME.inputLogic.startInput(RichInputMethodManager.getInstance().combiningRulesExtraValueOfCurrentSubtype,
latinIME.latinIMELegacy.mSettings.current)
}
override fun announce(s: String) {
AccessibilityUtils.init(getContext())
if(AccessibilityUtils.getInstance().isAccessibilityEnabled) {
@ -445,25 +465,27 @@ class UixManager(private val latinIME: LatinIME) {
fun setContent() {
composeView?.setContent {
UixThemeWrapper(latinIME.colorScheme) {
Column {
Spacer(modifier = Modifier.weight(1.0f))
Surface(modifier = Modifier.onSizeChanged {
latinIME.updateTouchableHeight(it.height)
}, color = latinIME.keyboardColor) {
Box {
Column {
when {
currWindowActionWindow != null -> ActionViewWithHeader(
currWindowActionWindow!!
)
CompositionLocalProvider(LocalManager provides keyboardManagerForAction) {
Column {
Spacer(modifier = Modifier.weight(1.0f))
Surface(modifier = Modifier.onSizeChanged {
latinIME.updateTouchableHeight(it.height)
}, color = latinIME.keyboardColor) {
Box {
Column {
when {
currWindowActionWindow != null -> ActionViewWithHeader(
currWindowActionWindow!!
)
else -> MainKeyboardViewWithActionBar()
else -> MainKeyboardViewWithActionBar()
}
latinIME.LegacyKeyboardView(hidden = isMainKeyboardHidden)
}
latinIME.LegacyKeyboardView(hidden = isMainKeyboardHidden)
ForgetWordDialog()
}
ForgetWordDialog()
}
}
}

View File

@ -2,12 +2,18 @@ package org.futo.inputmethod.latin.uix.actions
import android.content.Context
import android.graphics.drawable.Drawable
import android.text.InputType
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.accessibility.AccessibilityEvent
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.UiThread
import androidx.appcompat.widget.AppCompatEditText
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
@ -88,6 +94,7 @@ import org.futo.inputmethod.latin.uix.ActionWindow
import org.futo.inputmethod.latin.uix.AutoFitText
import org.futo.inputmethod.latin.uix.EmojiTracker.getRecentEmojis
import org.futo.inputmethod.latin.uix.EmojiTracker.useEmoji
import org.futo.inputmethod.latin.uix.LocalManager
import org.futo.inputmethod.latin.uix.PersistentActionState
import org.futo.inputmethod.latin.uix.actions.emoji.EmojiItem
import org.futo.inputmethod.latin.uix.actions.emoji.EmojiView
@ -97,6 +104,7 @@ import kotlin.math.min
import kotlin.math.roundToInt
import kotlin.streams.toList
data class PopupInfo(val emoji: EmojiItem, val x: Int, val y: Int)
sealed class EmojiViewItem
@ -427,6 +435,7 @@ fun EmojiNavigation(
}
EmojiCategoriesContainer(Modifier.weight(1.0f), categories, goToCategory, activeCategoryItem)
//CustomEditTextView()
if(showKeys) {
BackspaceKey(onBackspace)