mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
fix: keep ModeHeader search from stealing focus when recomposed (#1141)
Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
This commit is contained in:
1 parent
583c6805fd
commit
64b966af49
2 files changed
+101
-3
No files matched your search
@@ -996,6 +996,7 @@ internal fun ModeHeader(
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
var searching by remember { mutableStateOf(query.isNotEmpty()) }
|
||||
var focusOnOpen by remember { mutableStateOf(false) }
|
||||
val open by animateFloatAsState(if (searching) 1f else 0f, Motion.sp(0.75f, 420f), label = "searchOpen")
|
||||
Column(Modifier.fillMaxWidth()) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.fillMaxWidth().heightIn(min = 52.dp).padding(vertical = 4.dp)) {
|
||||
@@ -1010,11 +1011,11 @@ internal fun ModeHeader(
|
||||
Box(Modifier.width(w).graphicsLayer { alpha = (open * 1.6f).coerceAtMost(1f) }) {
|
||||
AdbTextField(
|
||||
value = query, onValueChange = onQuery, label = "", keyboardType = KeyboardType.Text, imeAction = ImeAction.Search,
|
||||
placeholder = placeholder, compact = true, focusOnShow = true,
|
||||
onFocusChange = { focused -> if (!focused && query.isEmpty()) searching = false },
|
||||
placeholder = placeholder, compact = true, focusOnShow = focusOnOpen,
|
||||
onFocusChange = { focused -> if (!focused && query.isEmpty()) { searching = false; focusOnOpen = false } },
|
||||
)
|
||||
}
|
||||
} else SearchIcon(stringResource(R.string.stores_search_open)) { searching = true }
|
||||
} else SearchIcon(stringResource(R.string.stores_search_open)) { searching = true; focusOnOpen = true }
|
||||
}
|
||||
trailing()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.EditText
|
||||
import androidx.compose.runtime.SideEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.compose.ui.test.onNodeWithContentDescription
|
||||
import androidx.compose.ui.test.performClick
|
||||
import com.droiddeck.launcher.R
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], qualifiers = "w960dp-h540dp-land", application = android.app.Application::class)
|
||||
class ModeHeaderFocusTest {
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
|
||||
@Test fun reenteringCompositionWithNonEmptyQueryDoesNotStealFocus() {
|
||||
lateinit var root: View
|
||||
var show by mutableStateOf(true)
|
||||
var query by mutableStateOf("celeste")
|
||||
compose.setContent {
|
||||
DroidDeckTheme {
|
||||
val view = LocalView.current
|
||||
SideEffect { root = view.rootView }
|
||||
if (show) {
|
||||
ModeHeader(
|
||||
title = "Library",
|
||||
query = query,
|
||||
onQuery = { query = it },
|
||||
placeholder = "Search",
|
||||
trailing = {},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
compose.waitForIdle()
|
||||
val initialField = editText(root)!!
|
||||
compose.runOnIdle {
|
||||
assertEquals("celeste", initialField.text.toString())
|
||||
assertFalse("Pre-populated search field must not auto-focus on mount", initialField.isFocused)
|
||||
show = false
|
||||
}
|
||||
compose.waitForIdle()
|
||||
compose.runOnIdle { show = true }
|
||||
compose.waitForIdle()
|
||||
val remountedField = editText(root)!!
|
||||
compose.runOnIdle {
|
||||
assertEquals("celeste", remountedField.text.toString())
|
||||
assertFalse("Remounted search field must not steal focus when scrolled back into view", remountedField.isFocused)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun clickingSearchIconFocusesField() {
|
||||
lateinit var root: View
|
||||
var query by mutableStateOf("")
|
||||
compose.setContent {
|
||||
DroidDeckTheme {
|
||||
val view = LocalView.current
|
||||
SideEffect { root = view.rootView }
|
||||
ModeHeader(
|
||||
title = "Library",
|
||||
query = query,
|
||||
onQuery = { query = it },
|
||||
placeholder = "Search",
|
||||
trailing = {},
|
||||
)
|
||||
}
|
||||
}
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
compose.onNodeWithContentDescription(context.getString(R.string.stores_search_open)).performClick()
|
||||
compose.waitForIdle()
|
||||
val field = editText(root)!!
|
||||
compose.runOnIdle {
|
||||
assertTrue("Opening search via icon must focus the text field", field.isFocused)
|
||||
}
|
||||
}
|
||||
|
||||
private fun editText(view: View): EditText? {
|
||||
if (view is EditText) return view
|
||||
if (view is ViewGroup) for (i in 0 until view.childCount) {
|
||||
editText(view.getChildAt(i))?.let { return it }
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user