mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-10 15:11:58 +01:00
ui: restore search focus when returning
Keep the query populated and put the cursor back at the end of the search field when the screen is restored.
This commit is contained in:
@@ -23,7 +23,6 @@ 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.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -56,13 +55,7 @@ fun SearchField(
|
||||
textFieldValue = TextFieldValue(value, selection = TextRange(value.length))
|
||||
}
|
||||
}
|
||||
var focusRequested by rememberSaveable { mutableStateOf(false) }
|
||||
LaunchedEffect(Unit) {
|
||||
if (!focusRequested) {
|
||||
focusRequester.requestFocus()
|
||||
focusRequested = true
|
||||
}
|
||||
}
|
||||
LaunchedEffect(Unit) { focusRequester.requestFocus() }
|
||||
Surface(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
color = MaterialTheme.colorScheme.surfaceContainerHigh,
|
||||
|
||||
@@ -7,7 +7,6 @@ import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.assertIsFocused
|
||||
import androidx.compose.ui.test.assertIsNotFocused
|
||||
import androidx.compose.ui.test.hasSetTextAction
|
||||
import androidx.compose.ui.test.junit4.StateRestorationTester
|
||||
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
|
||||
@@ -124,7 +123,7 @@ class SearchScreenTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun restoringTheScreenDoesNotStealFocus() {
|
||||
fun restoringTheScreenKeepsFocusOnTheQuery() {
|
||||
val restorationTester = StateRestorationTester(composeRule)
|
||||
restorationTester.setContent {
|
||||
SubtracksTheme {
|
||||
@@ -135,7 +134,7 @@ class SearchScreenTest {
|
||||
composeRule.onNode(hasSetTextAction()).assertIsFocused()
|
||||
restorationTester.emulateSavedInstanceStateRestore()
|
||||
|
||||
composeRule.onNode(hasSetTextAction()).assertIsNotFocused()
|
||||
composeRule.onNode(hasSetTextAction()).assertIsFocused()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user