Add missing haptic/sound to compose elements

This commit is contained in:
Aleksandras Kostarevas 2024-03-19 12:12:16 -05:00
parent 2c94f8a1cd
commit 25229530f0
5 changed files with 39 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package org.futo.inputmethod.latin.uix
import android.content.Context
import android.view.View
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
@ -35,6 +36,8 @@ interface KeyboardManagerForAction {
fun cursorLeft(steps: Int, stepOverWords: Boolean, select: Boolean)
fun cursorRight(steps: Int, stepOverWords: Boolean, select: Boolean)
fun performHapticAndAudioFeedback(code: Int, view: View)
}
interface ActionWindow {

View File

@ -50,6 +50,7 @@ import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.drawscope.scale
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
@ -70,6 +71,7 @@ import org.futo.inputmethod.latin.SuggestedWords
import org.futo.inputmethod.latin.SuggestedWords.SuggestedWordInfo
import org.futo.inputmethod.latin.SuggestedWords.SuggestedWordInfo.KIND_EMOJI_SUGGESTION
import org.futo.inputmethod.latin.SuggestedWords.SuggestedWordInfo.KIND_TYPED
import org.futo.inputmethod.latin.common.Constants
import org.futo.inputmethod.latin.suggestions.SuggestionStripView
import org.futo.inputmethod.latin.uix.actions.ClipboardAction
import org.futo.inputmethod.latin.uix.actions.EmojiAction
@ -471,8 +473,10 @@ fun ActionBar(
onActionActivated: (Action) -> Unit,
inlineSuggestions: List<MutableState<View?>>,
forceOpenActionsInitially: Boolean = false,
importantNotice: ImportantNotice? = null
importantNotice: ImportantNotice? = null,
keyboardManagerForAction: KeyboardManagerForAction? = null
) {
val view = LocalView.current
val context = LocalContext.current
val isActionsOpen = remember { mutableStateOf(forceOpenActionsInitially) }
val systemVoiceInput = useDataStore(key = USE_SYSTEM_VOICE_INPUT.key, default = USE_SYSTEM_VOICE_INPUT.default)
@ -487,6 +491,7 @@ fun ActionBar(
if(isActionsOpen.value && importantNotice != null) {
importantNotice.onDismiss(context)
}
keyboardManagerForAction?.performHapticAndAudioFeedback(Constants.CODE_TAB, view)
}
if(importantNotice != null && !isActionsOpen.value) {
@ -496,7 +501,10 @@ fun ActionBar(
if (isActionsOpen.value) {
LazyRow {
item {
ActionItems(onActionActivated)
ActionItems {
keyboardManagerForAction?.performHapticAndAudioFeedback(Constants.CODE_TAB, view)
onActionActivated(it)
}
}
}
} else if (inlineSuggestions.isNotEmpty() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
@ -506,6 +514,7 @@ fun ActionBar(
suggestionStripListener.pickSuggestionManually(
words.getInfo(it)
)
keyboardManagerForAction?.performHapticAndAudioFeedback(Constants.CODE_TAB, view)
}, onLongClick = { suggestionStripListener.requestForgetWord(words.getInfo(it)) })
} else {
Spacer(modifier = Modifier.weight(1.0f))

View File

@ -43,6 +43,7 @@ import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.futo.inputmethod.latin.AudioAndHapticFeedbackManager
import org.futo.inputmethod.latin.LatinIME
import org.futo.inputmethod.latin.R
import org.futo.inputmethod.latin.SuggestedWords
@ -156,6 +157,10 @@ class UixActionKeyboardManager(val uixManager: UixManager, val latinIME: LatinIM
override fun cursorRight(steps: Int, stepOverWords: Boolean, select: Boolean) {
latinIME.inputLogic.cursorRight(steps, stepOverWords, select)
}
override fun performHapticAndAudioFeedback(code: Int, view: View) {
AudioAndHapticFeedbackManager.getInstance().performHapticAndAudioFeedback(code, view)
}
}
class UixManager(private val latinIME: LatinIME) {
@ -206,7 +211,8 @@ class UixManager(private val latinIME: LatinIME) {
latinIME.latinIMELegacy as SuggestionStripView.Listener,
inlineSuggestions = inlineSuggestions,
onActionActivated = { onActionActivated(it) },
importantNotice = currentNotice.value
importantNotice = currentNotice.value,
keyboardManagerForAction = keyboardManagerForAction
)
}
}

View File

@ -49,6 +49,7 @@ import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.IntOffset
@ -529,18 +530,23 @@ val EmojiAction = Action(
@Composable
override fun WindowContents(keyboardShown: Boolean) {
val view = LocalView.current
state.emojis.value?.let { emojis ->
EmojiGrid(onClick = {
manager.typeText(it.emoji)
manager.getLifecycleScope().launch {
manager.getContext().useEmoji(it.emoji)
}
manager.performHapticAndAudioFeedback(Constants.CODE_EMOJI, view)
}, onExit = {
manager.closeActionWindow()
manager.performHapticAndAudioFeedback(Constants.CODE_SWITCH_ALPHA_SYMBOL, view)
}, onSpace = {
manager.sendCodePointEvent(Constants.CODE_SPACE)
manager.performHapticAndAudioFeedback(Constants.CODE_SPACE, view)
}, onBackspace = {
manager.sendCodePointEvent(Constants.CODE_DELETE)
manager.performHapticAndAudioFeedback(Constants.CODE_DELETE, view)
}, emojis = emojis, keyboardShown = keyboardShown, emojiMap = state.emojiMap)
}
}

View File

@ -27,6 +27,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
@ -369,9 +370,16 @@ val TextEditAction = Action(
@Composable
override fun WindowContents(keyboardShown: Boolean) {
val view = LocalView.current
TextEditScreen(
onCodePoint = { a -> manager.sendCodePointEvent(a)},
onEvent = { a, b -> manager.sendKeyEvent(a, b) },
onCodePoint = { a ->
manager.sendCodePointEvent(a)
manager.performHapticAndAudioFeedback(a, view)
},
onEvent = { a, b ->
manager.sendKeyEvent(a, b)
manager.performHapticAndAudioFeedback(Constants.CODE_TAB, view)
},
moveCursor = { direction, ctrl, shift ->
val keyEventMetaState = 0 or
(if(shift) { KeyEvent.META_SHIFT_ON } else { 0 }) or
@ -383,6 +391,8 @@ val TextEditAction = Action(
Direction.Up -> manager.sendKeyEvent(KeyEvent.KEYCODE_DPAD_UP, keyEventMetaState)
Direction.Down -> manager.sendKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, keyEventMetaState)
}
manager.performHapticAndAudioFeedback(Constants.CODE_TAB, view)
},
keyboardShown = keyboardShown
)