mirror of
https://github.com/mihonapp/mihon.git
synced 2025-06-29 20:47:50 +02:00
Adjust SearchToolbar soft keyboard behavior (#9282)
* Show soft keyboard when the text field is composed (a redo) * Clear focus on text field when soft keyboard is hidden * Request focus on text field and show soft keyboard when clear button is clicked
This commit is contained in:
@ -4,14 +4,25 @@ import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.combinedClickable
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.foundation.layout.WindowInsets
|
||||
import androidx.compose.foundation.layout.isImeVisible
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.composed
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.input.key.Key
|
||||
import androidx.compose.ui.input.key.key
|
||||
import androidx.compose.ui.input.key.onPreviewKeyEvent
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
import tachiyomi.presentation.core.components.material.SecondaryItemAlpha
|
||||
|
||||
fun Modifier.selectedBackground(isSelected: Boolean): Modifier = composed {
|
||||
@ -52,3 +63,53 @@ fun Modifier.runOnEnterKeyPressed(action: () -> Unit): Modifier = this.onPreview
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For TextField on AppBar, this modifier will request focus
|
||||
* to the element the first time it's composed.
|
||||
*/
|
||||
fun Modifier.showSoftKeyboard(show: Boolean): Modifier = if (show) {
|
||||
composed {
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
var openKeyboard by rememberSaveable { mutableStateOf(show) }
|
||||
LaunchedEffect(focusRequester) {
|
||||
if (openKeyboard) {
|
||||
focusRequester.requestFocus()
|
||||
openKeyboard = false
|
||||
}
|
||||
}
|
||||
|
||||
Modifier.focusRequester(focusRequester)
|
||||
}
|
||||
} else {
|
||||
this
|
||||
}
|
||||
|
||||
/**
|
||||
* For TextField, this modifier will clear focus when soft
|
||||
* keyboard is hidden.
|
||||
*/
|
||||
fun Modifier.clearFocusOnSoftKeyboardHide(): Modifier = composed {
|
||||
var isFocused by remember { mutableStateOf(false) }
|
||||
var keyboardShowedSinceFocused by remember { mutableStateOf(false) }
|
||||
if (isFocused) {
|
||||
val imeVisible = WindowInsets.isImeVisible
|
||||
val focusManager = LocalFocusManager.current
|
||||
LaunchedEffect(imeVisible) {
|
||||
if (imeVisible) {
|
||||
keyboardShowedSinceFocused = true
|
||||
} else if (keyboardShowedSinceFocused) {
|
||||
focusManager.clearFocus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Modifier.onFocusChanged {
|
||||
if (isFocused != it.isFocused) {
|
||||
if (isFocused) {
|
||||
keyboardShowedSinceFocused = false
|
||||
}
|
||||
isFocused = it.isFocused
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user