mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 22:36:00 +00:00
fix(ui): keep the Agent composer above the keyboard and release search fields on tap (#2852)
* fix(agent): keep the composer above the keyboard outside fullscreen * fix(ui): release a search field when another control is tapped * fix(ui): release the add-on search field in its dialog, and let a reopened search take focus * fix(ui): a drag that returns to where it began does not release the search field
This commit is contained in:
1 parent
71b7b6efad
commit
c3809c0a75
7 files changed
+146
-9
No files matched your search
@@ -17,8 +17,11 @@ import android.os.Looper
|
||||
import android.util.Log
|
||||
import android.view.Display
|
||||
import android.view.KeyEvent
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewConfiguration
|
||||
import android.view.WindowManager
|
||||
import android.widget.EditText
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
@@ -2042,6 +2045,36 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
}
|
||||
}
|
||||
|
||||
private var touchDownX = 0f
|
||||
private var touchDownY = 0f
|
||||
private var touchDragged = false
|
||||
|
||||
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
com.droiddeck.launcher.ui.ScreenTaps.count++
|
||||
touchDownX = event.rawX
|
||||
touchDownY = event.rawY
|
||||
touchDragged = false
|
||||
}
|
||||
// A drag that wanders back to where it began is still a drag, not a tap.
|
||||
MotionEvent.ACTION_MOVE -> if (!touchDragged &&
|
||||
kotlin.math.hypot(event.rawX - touchDownX, event.rawY - touchDownY) > ViewConfiguration.get(this).scaledTouchSlop
|
||||
) touchDragged = true
|
||||
MotionEvent.ACTION_CANCEL -> touchDragged = true
|
||||
}
|
||||
val handled = super.dispatchTouchEvent(event)
|
||||
if (event.actionMasked == MotionEvent.ACTION_UP) leaveTextFieldOnOutsideTap(event)
|
||||
return handled
|
||||
}
|
||||
|
||||
private fun leaveTextFieldOnOutsideTap(event: MotionEvent) {
|
||||
val field = currentFocus as? EditText ?: return
|
||||
val slop = ViewConfiguration.get(this).scaledTouchSlop
|
||||
if (touchDragged || kotlin.math.hypot(event.rawX - touchDownX, event.rawY - touchDownY) > slop) return
|
||||
com.droiddeck.launcher.ui.releaseTextField(field, IntArray(2).also(field::getLocationOnScreen), event.rawX, event.rawY)
|
||||
}
|
||||
|
||||
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
|
||||
if (com.droiddeck.launcher.ui.AgentPet.handleBack(this, event)) return true
|
||||
if (compatLaunch != null) {
|
||||
|
||||
@@ -162,7 +162,7 @@ internal fun AddonBrowserDialog(targets: List<WowAddons.Target>, initial: WowAdd
|
||||
LaunchedEffect(results, results.items.isNotEmpty()) {
|
||||
if (results.items.isNotEmpty() && !searching && inputMode.inputMode == InputMode.Keyboard) focusSoon { firstAction }
|
||||
}
|
||||
AppDialog(shown, close, "addonBrowser", wide = true, maxWidth = 980.dp, scrollable = false, fillHeight = true) {
|
||||
AppDialog(shown, close, "addonBrowser", wide = true, maxWidth = 980.dp, scrollable = false, fillHeight = true, modifier = Modifier.releaseTextFieldOnTap()) {
|
||||
val sortChip: @Composable () -> Unit = {
|
||||
BrowseDropdown(sortLabel(sort)) { first, done ->
|
||||
WowAddonCatalog.Sort.entries.forEachIndexed { index, option ->
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.droiddeck.launcher.ui
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ContextWrapper
|
||||
import android.os.Build
|
||||
import android.view.KeyEvent
|
||||
import android.view.WindowManager
|
||||
import androidx.activity.compose.LocalActivityResultRegistryOwner
|
||||
@@ -184,7 +185,11 @@ private fun AgentPageContent(
|
||||
?: generateSequence(view.context) { (it as? ContextWrapper)?.baseContext }
|
||||
.filterIsInstance<Activity>().firstOrNull()?.window
|
||||
val previousMode = window?.attributes?.softInputMode
|
||||
// A window fitted to the system bars with ADJUST_NOTHING reports no keyboard height to Compose.
|
||||
// It stays unfitted after the page leaves: fitting it again insets the other pages twice.
|
||||
val edgeToEdge = visible && hidesPet && !drawer && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
|
||||
if (visible) window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
|
||||
if (edgeToEdge) window?.setDecorFitsSystemWindows(false)
|
||||
onDispose {
|
||||
if (visible && previousMode != null) window?.setSoftInputMode(previousMode)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.widget.EditText
|
||||
import androidx.compose.foundation.gestures.awaitEachGesture
|
||||
import androidx.compose.foundation.gestures.awaitFirstDown
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberUpdatedState
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.snapshotFlow
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.composed
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.input.pointer.PointerEventPass
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.layout.LayoutCoordinates
|
||||
import androidx.compose.ui.layout.onGloballyPositioned
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
internal object ScreenTaps {
|
||||
var count by mutableStateOf(0)
|
||||
}
|
||||
|
||||
/** A field that opens on a heavy page can take focus after the next tap has already finished. */
|
||||
@Composable
|
||||
internal fun CancelPendingFocus(pending: Boolean, askedAtTap: Int, focused: Boolean, cancel: () -> Unit) {
|
||||
val focusedNow by rememberUpdatedState(focused)
|
||||
val cancelNow by rememberUpdatedState(cancel)
|
||||
LaunchedEffect(pending, askedAtTap) {
|
||||
if (!pending) return@LaunchedEffect
|
||||
snapshotFlow { ScreenTaps.count }.first { it != askedAtTap }
|
||||
if (!focusedNow) cancelNow()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tapped controls do not take focus in touch mode, so a text field would otherwise keep it, and its highlight.
|
||||
* [x] and [y] share coordinates with [origin], the field's own position.
|
||||
*/
|
||||
internal fun releaseTextField(field: EditText, origin: IntArray, x: Float, y: Float) {
|
||||
val margin = 12 * field.resources.displayMetrics.density
|
||||
val insideX = x >= origin[0] - margin && x <= origin[0] + field.width + margin
|
||||
val insideY = y >= origin[1] - margin && y <= origin[1] + field.height + margin
|
||||
if (insideX && insideY) return
|
||||
field.clearFocus()
|
||||
(field.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager)?.hideSoftInputFromWindow(field.windowToken, 0)
|
||||
}
|
||||
|
||||
/** A dialog's touches never reach the activity, so a dialog holding a search field counts its taps and releases the field itself. */
|
||||
internal fun Modifier.releaseTextFieldOnTap(): Modifier = composed {
|
||||
val view = LocalView.current
|
||||
val placed = remember { arrayOfNulls<LayoutCoordinates>(1) }
|
||||
onGloballyPositioned { placed[0] = it }.pointerInput(view) {
|
||||
awaitEachGesture {
|
||||
val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Initial)
|
||||
ScreenTaps.count++
|
||||
var up: Offset? = null
|
||||
while (true) {
|
||||
val change = awaitPointerEvent(PointerEventPass.Final).changes.firstOrNull { it.id == down.id } ?: break
|
||||
if ((change.position - down.position).getDistance() > viewConfiguration.touchSlop) break
|
||||
if (!change.pressed) { up = change.position; break }
|
||||
}
|
||||
val at = up?.let { placed[0]?.takeIf { c -> c.isAttached }?.localToWindow(it) } ?: return@awaitEachGesture
|
||||
view.post {
|
||||
val field = view.findFocus() as? EditText ?: return@post
|
||||
releaseTextField(field, IntArray(2).also(field::getLocationInWindow), at.x, at.y)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,12 @@ internal fun SettingsPillSearch(
|
||||
val icon = remember { FocusRequester() }
|
||||
var toIcon by remember { mutableStateOf(false) }
|
||||
var focusOnOpen by remember { mutableStateOf(false) }
|
||||
var focusTap by remember { mutableStateOf(0) }
|
||||
var fieldFocused by remember { mutableStateOf(false) }
|
||||
CancelPendingFocus(focusOnOpen, focusTap, fieldFocused) {
|
||||
focusOnOpen = false
|
||||
if (query.isEmpty()) onClose()
|
||||
}
|
||||
val close = { toIcon = true; focusOnOpen = false; onClose() }
|
||||
BackHandler(enabled = searching) { close() }
|
||||
LaunchedEffect(toIcon, searching, open <= 0.01f) {
|
||||
@@ -73,15 +79,18 @@ internal fun SettingsPillSearch(
|
||||
Box(Modifier.width(w).graphicsLayer { alpha = (open * 1.6f).coerceAtMost(1f) }) {
|
||||
AdbTextField(
|
||||
value = query, onValueChange = onQuery, label = "", keyboardType = KeyboardType.Text, imeAction = ImeAction.Search,
|
||||
placeholder = stringResource(R.string.settings_search), compact = true, showLabel = false, focusOnShow = focusOnOpen,
|
||||
placeholder = stringResource(R.string.settings_search), compact = true, showLabel = false, focusOnShow = focusOnOpen, focusAtTap = focusTap,
|
||||
clearFocus = !searching,
|
||||
onFocusChange = { focused -> if (!focused && query.isEmpty() && searching) { focusOnOpen = false; onClose() } },
|
||||
onFocusChange = { focused ->
|
||||
fieldFocused = focused
|
||||
if (!focused && query.isEmpty() && searching) { focusOnOpen = false; onClose() }
|
||||
},
|
||||
)
|
||||
}
|
||||
SmallIconButton(Icons.Filled.Close, stringResource(R.string.settings_search_clear), tint = colors.onSurfaceVariant) { close() }
|
||||
}
|
||||
} else {
|
||||
Box(Modifier.focusRequester(icon)) { SearchIcon(stringResource(R.string.settings_search)) { focusOnOpen = true; onOpen() } }
|
||||
Box(Modifier.focusRequester(icon)) { SearchIcon(stringResource(R.string.settings_search)) { focusTap = ScreenTaps.count; focusOnOpen = true; onOpen() } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -925,6 +925,12 @@ internal fun ModeHeader(
|
||||
val pal = LocalPalette.current
|
||||
var searching by remember { mutableStateOf(query.isNotEmpty()) }
|
||||
var focusOnOpen by remember { mutableStateOf(false) }
|
||||
var focusTap by remember { mutableStateOf(0) }
|
||||
var fieldFocused by remember { mutableStateOf(false) }
|
||||
CancelPendingFocus(focusOnOpen, focusTap, fieldFocused) {
|
||||
focusOnOpen = false
|
||||
if (query.isEmpty()) searching = false
|
||||
}
|
||||
val open by animateFloatAsState(if (searching) 1f else 0f, Motion.sp(0.75f, 420f), label = "searchOpen")
|
||||
// Closing takes the field away with focus still in it; the search button gets it once it is back on screen.
|
||||
val searchIcon = remember { FocusRequester() }
|
||||
@@ -955,9 +961,12 @@ 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, showLabel = false, focusOnShow = focusOnOpen,
|
||||
placeholder = placeholder, compact = true, showLabel = false, focusOnShow = focusOnOpen, focusAtTap = focusTap,
|
||||
clearFocus = clearSearchFocus || !searching,
|
||||
onFocusChange = { focused -> if (!focused && query.isEmpty()) { searching = false; focusOnOpen = false } },
|
||||
onFocusChange = { focused ->
|
||||
fieldFocused = focused
|
||||
if (!focused && query.isEmpty()) { searching = false; focusOnOpen = false }
|
||||
},
|
||||
)
|
||||
}
|
||||
if (searching && closable) SmallIconButton(Icons.Filled.Close, stringResource(R.string.common_close), tint = colors.onSurfaceVariant, onClick = close)
|
||||
@@ -965,7 +974,7 @@ internal fun ModeHeader(
|
||||
}
|
||||
}
|
||||
val searchButton: @Composable () -> Unit = {
|
||||
Box(Modifier.focusRequester(searchIcon)) { SearchIcon(stringResource(R.string.stores_search_open)) { searching = true; focusOnOpen = true } }
|
||||
Box(Modifier.focusRequester(searchIcon)) { SearchIcon(stringResource(R.string.stores_search_open)) { focusTap = ScreenTaps.count; searching = true; focusOnOpen = true } }
|
||||
}
|
||||
BoxWithConstraints(Modifier.fillMaxWidth()) {
|
||||
val stacked = maxWidth < NarrowPaneWidth
|
||||
|
||||
@@ -365,11 +365,13 @@ internal fun AdbTextField(
|
||||
onDone: (() -> Unit)? = null,
|
||||
focusOnShow: Boolean = false,
|
||||
showLabel: Boolean = true,
|
||||
focusAtTap: Int? = null,
|
||||
onBack: (() -> Unit)? = null,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val onBackLatest = rememberUpdatedState(onBack)
|
||||
val focusAsked = remember { booleanArrayOf(false) }
|
||||
val focusTapAsked = remember { arrayOfNulls<Int>(1) }
|
||||
val palette = LocalPalette.current
|
||||
val onValueChangeLatest = rememberUpdatedState(onValueChange)
|
||||
val onNextLatest = rememberUpdatedState(onNext)
|
||||
@@ -502,9 +504,12 @@ internal fun AdbTextField(
|
||||
},
|
||||
update = { editText ->
|
||||
editText.showSoftInputOnFocus = !bottomTyping
|
||||
if (focusOnShow && !focusAsked[0]) {
|
||||
focusAsked[0] = true
|
||||
// A new tap may ask again: a field still on screen while it closes can be opened once more.
|
||||
val fresh = if (focusAtTap != null) focusTapAsked[0] != focusAtTap else !focusAsked[0]
|
||||
if (focusOnShow && fresh) {
|
||||
if (focusAtTap != null) focusTapAsked[0] = focusAtTap else focusAsked[0] = true
|
||||
editText.post {
|
||||
if (focusAtTap != null && focusAtTap != ScreenTaps.count) return@post
|
||||
editText.requestFocus()
|
||||
if (!bottomTypingLatest.value) (editText.context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as? android.view.inputmethod.InputMethodManager)
|
||||
?.showSoftInput(editText, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT)
|
||||
|
||||
Reference in new issue
Block a user