|
| 1 | +package io.github.aecsocket.wordbase |
| 2 | + |
| 3 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 4 | +import androidx.compose.foundation.interaction.collectIsFocusedAsState |
| 5 | +import androidx.compose.foundation.layout.Box |
| 6 | +import androidx.compose.foundation.layout.offset |
| 7 | +import androidx.compose.foundation.layout.sizeIn |
| 8 | +import androidx.compose.foundation.text.BasicTextField |
| 9 | +import androidx.compose.foundation.text.KeyboardActions |
| 10 | +import androidx.compose.foundation.text.KeyboardOptions |
| 11 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 12 | +import androidx.compose.material3.LocalTextStyle |
| 13 | +import androidx.compose.material3.SearchBarDefaults |
| 14 | +import androidx.compose.material3.SearchBarDefaults.InputFieldHeight |
| 15 | +import androidx.compose.material3.SearchBarDefaults.inputFieldColors |
| 16 | +import androidx.compose.material3.TextFieldColors |
| 17 | +import androidx.compose.material3.TextFieldDefaults |
| 18 | +import androidx.compose.runtime.Composable |
| 19 | +import androidx.compose.runtime.LaunchedEffect |
| 20 | +import androidx.compose.runtime.Stable |
| 21 | +import androidx.compose.runtime.remember |
| 22 | +import androidx.compose.ui.Modifier |
| 23 | +import androidx.compose.ui.focus.FocusRequester |
| 24 | +import androidx.compose.ui.focus.focusRequester |
| 25 | +import androidx.compose.ui.focus.onFocusChanged |
| 26 | +import androidx.compose.ui.graphics.Color |
| 27 | +import androidx.compose.ui.graphics.SolidColor |
| 28 | +import androidx.compose.ui.graphics.takeOrElse |
| 29 | +import androidx.compose.ui.platform.LocalFocusManager |
| 30 | +import androidx.compose.ui.semantics.onClick |
| 31 | +import androidx.compose.ui.semantics.semantics |
| 32 | +import androidx.compose.ui.text.TextStyle |
| 33 | +import androidx.compose.ui.text.input.VisualTransformation |
| 34 | +import androidx.compose.ui.unit.Dp |
| 35 | +import androidx.compose.ui.unit.dp |
| 36 | +import kotlinx.coroutines.delay |
| 37 | + |
| 38 | +@ExperimentalMaterial3Api |
| 39 | +@Composable |
| 40 | +fun SearchBarInputField( |
| 41 | + query: String, |
| 42 | + onQueryChange: (String) -> Unit, |
| 43 | + onSearch: (String) -> Unit, |
| 44 | + expanded: Boolean, |
| 45 | + onExpandedChange: (Boolean) -> Unit, |
| 46 | + modifier: Modifier = Modifier, |
| 47 | + enabled: Boolean = true, |
| 48 | + placeholder: @Composable (() -> Unit)? = null, |
| 49 | + leadingIcon: @Composable (() -> Unit)? = null, |
| 50 | + trailingIcon: @Composable (() -> Unit)? = null, |
| 51 | + colors: TextFieldColors = inputFieldColors(), |
| 52 | + interactionSource: MutableInteractionSource? = null, |
| 53 | + // new |
| 54 | + keyboardOptions: KeyboardOptions = KeyboardOptions(), |
| 55 | + focusRequester: FocusRequester = remember { FocusRequester() }, |
| 56 | +) { |
| 57 | + @Suppress("NAME_SHADOWING") |
| 58 | + val interactionSource = interactionSource ?: remember { MutableInteractionSource() } |
| 59 | + |
| 60 | + val focused = interactionSource.collectIsFocusedAsState().value |
| 61 | +// val focusRequester = remember { FocusRequester() } |
| 62 | + val focusManager = LocalFocusManager.current |
| 63 | + |
| 64 | +// val searchSemantics = getString(Strings.SearchBarSearch) |
| 65 | +// val suggestionsAvailableSemantics = getString(Strings.SuggestionsAvailable) |
| 66 | + |
| 67 | + val textColor = |
| 68 | + LocalTextStyle.current.color.takeOrElse { |
| 69 | + colors.textColor(enabled, isError = false, focused = focused) |
| 70 | + } |
| 71 | + |
| 72 | + BasicTextField( |
| 73 | + value = query, |
| 74 | + onValueChange = onQueryChange, |
| 75 | + modifier = |
| 76 | + modifier |
| 77 | + .sizeIn( |
| 78 | + minWidth = SearchBarMinWidth, |
| 79 | + maxWidth = SearchBarMaxWidth, |
| 80 | + minHeight = InputFieldHeight, |
| 81 | + ) |
| 82 | + .focusRequester(focusRequester) |
| 83 | + .onFocusChanged { if (it.isFocused) onExpandedChange(true) } |
| 84 | + .semantics { |
| 85 | +// contentDescription = searchSemantics |
| 86 | +// if (expanded) { |
| 87 | +// stateDescription = suggestionsAvailableSemantics |
| 88 | +// } |
| 89 | + onClick { |
| 90 | + focusRequester.requestFocus() |
| 91 | + true |
| 92 | + } |
| 93 | + }, |
| 94 | + enabled = enabled, |
| 95 | + singleLine = true, |
| 96 | + textStyle = LocalTextStyle.current.merge(TextStyle(color = textColor)), |
| 97 | + cursorBrush = SolidColor(colors.cursorColor(isError = false)), |
| 98 | + keyboardOptions = keyboardOptions, // patched |
| 99 | + keyboardActions = KeyboardActions(onSearch = { onSearch(query) }), |
| 100 | + interactionSource = interactionSource, |
| 101 | + decorationBox = |
| 102 | + @Composable { innerTextField -> |
| 103 | + TextFieldDefaults.DecorationBox( |
| 104 | + value = query, |
| 105 | + innerTextField = innerTextField, |
| 106 | + enabled = enabled, |
| 107 | + singleLine = true, |
| 108 | + visualTransformation = VisualTransformation.None, |
| 109 | + interactionSource = interactionSource, |
| 110 | + placeholder = placeholder, |
| 111 | + leadingIcon = |
| 112 | + leadingIcon?.let { leading -> |
| 113 | + { Box(Modifier.offset(x = SearchBarIconOffsetX)) { leading() } } |
| 114 | + }, |
| 115 | + trailingIcon = |
| 116 | + trailingIcon?.let { trailing -> |
| 117 | + { Box(Modifier.offset(x = -SearchBarIconOffsetX)) { trailing() } } |
| 118 | + }, |
| 119 | + shape = SearchBarDefaults.inputFieldShape, |
| 120 | + colors = colors, |
| 121 | + contentPadding = TextFieldDefaults.contentPaddingWithoutLabel(), |
| 122 | + container = {}, |
| 123 | + ) |
| 124 | + } |
| 125 | + ) |
| 126 | + |
| 127 | + val shouldClearFocus = !expanded && focused |
| 128 | + LaunchedEffect(expanded) { |
| 129 | + if (shouldClearFocus) { |
| 130 | + // Not strictly needed according to the motion spec, but since the animation |
| 131 | + // already has a delay, this works around b/261632544. |
| 132 | + delay(AnimationDelayMillis.toLong()) |
| 133 | + focusManager.clearFocus() |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +private val SearchBarMinWidth: Dp = 360.dp |
| 139 | +private val SearchBarMaxWidth: Dp = 720.dp |
| 140 | +private val SearchBarIconOffsetX: Dp = 4.dp |
| 141 | + |
| 142 | +private const val DurationShort2 = 100.0 |
| 143 | +private const val AnimationDelayMillis: Int = DurationShort2.toInt() |
| 144 | + |
| 145 | +@Stable |
| 146 | +private fun TextFieldColors.textColor( |
| 147 | + enabled: Boolean, |
| 148 | + isError: Boolean, |
| 149 | + focused: Boolean, |
| 150 | +): Color = |
| 151 | + when { |
| 152 | + !enabled -> disabledTextColor |
| 153 | + isError -> errorTextColor |
| 154 | + focused -> focusedTextColor |
| 155 | + else -> unfocusedTextColor |
| 156 | + } |
| 157 | + |
| 158 | +@Stable |
| 159 | +private fun TextFieldColors.cursorColor(isError: Boolean): Color = |
| 160 | + if (isError) errorCursorColor else cursorColor |
0 commit comments