package eu.friedcutlet.yokan.ui.components import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material.icons.filled.Clear import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextFieldDefaults import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.input.key.Key import androidx.compose.ui.input.key.KeyEventType import androidx.compose.ui.input.key.key import androidx.compose.ui.input.key.onPreviewKeyEvent import androidx.compose.ui.input.key.type import androidx.compose.ui.unit.dp import eu.friedcutlet.yokan.services.system.isTvDevice import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.action_clear_search import yokan.composeapp.generated.resources.action_close_search /** * The inline search field that replaces an app-bar title: a back arrow that calls [onClose] * (also bound to Escape), a clear button, and transparent chrome. [onSearch] adds an IME * search action and Enter-to-submit; [onClear] defaults to emptying the query. */ @Composable fun AppBarSearchField( query: String, placeholder: String, onQueryChange: (String) -> Unit, onClose: () -> Unit, modifier: Modifier = Modifier, onSearch: (() -> Unit)? = null, onClear: () -> Unit = { onQueryChange("") }, ) { OutlinedTextField( value = query, onValueChange = onQueryChange, modifier = modifier .fillMaxWidth() .autoFocusOnMount() .compactSearchField() .then(if (onSearch != null) Modifier.searchOnEnter(onSearch) else Modifier) .closeSearchOnEscape(onClose), singleLine = true, placeholder = { Text(placeholder) }, leadingIcon = { IconButton(onClick = onClose) { Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = stringResource(Res.string.action_close_search)) } }, trailingIcon = { if (query.isNotEmpty()) { IconButton(onClick = onClear) { Icon(Icons.Filled.Clear, contentDescription = stringResource(Res.string.action_clear_search)) } } }, keyboardOptions = if (onSearch != null) KeyboardOptions(imeAction = ImeAction.Search) else KeyboardOptions.Default, keyboardActions = if (onSearch != null) KeyboardActions(onSearch = { onSearch() }) else KeyboardActions.Default, colors = OutlinedTextFieldDefaults.colors( focusedBorderColor = Color.Transparent, unfocusedBorderColor = Color.Transparent, focusedContainerColor = Color.Transparent, unfocusedContainerColor = Color.Transparent, ), ) } fun Modifier.closeSearchOnEscape(onClose: () -> Unit): Modifier = onPreviewKeyEvent { event -> if (event.type == KeyEventType.KeyDown && event.key == Key.Escape) { onClose() true } else { false } } fun Modifier.searchOnEnter(onSearch: () -> Unit): Modifier = onPreviewKeyEvent { event -> if (event.type == KeyEventType.KeyUp && event.key == Key.Enter) { onSearch() true } else { false } } @Composable fun Modifier.autoFocusOnMount(): Modifier { if (isTvDevice()) return this val focusRequester = remember { FocusRequester() } LaunchedEffect(focusRequester) { runCatching { focusRequester.requestFocus() } } return focusRequester(focusRequester) } fun Modifier.compactSearchField(): Modifier = heightIn(min = 52.dp, max = 56.dp)