Skip to content

Commit ef1bbcc

Browse files
committed
Fix long press menu on DPAD clicks onEditActions right after opened
Also see the comment
1 parent b5d3a74 commit ef1bbcc

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenu.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.content.res.Configuration
88
import android.view.ViewGroup
99
import android.view.ViewGroup.LayoutParams
1010
import androidx.compose.foundation.clickable
11+
import androidx.compose.foundation.focusable
1112
import androidx.compose.foundation.isSystemInDarkTheme
1213
import androidx.compose.foundation.layout.Arrangement
1314
import androidx.compose.foundation.layout.Box
@@ -50,6 +51,7 @@ import androidx.compose.runtime.setValue
5051
import androidx.compose.ui.Alignment
5152
import androidx.compose.ui.Modifier
5253
import androidx.compose.ui.draw.clip
54+
import androidx.compose.ui.focus.onFocusChanged
5355
import androidx.compose.ui.graphics.Color
5456
import androidx.compose.ui.graphics.vector.ImageVector
5557
import androidx.compose.ui.platform.ComposeView
@@ -122,7 +124,7 @@ fun LongPressMenu(
122124
longPressActions: List<LongPressAction>,
123125
onDismissRequest: () -> Unit,
124126
) {
125-
var showEditor by rememberSaveable(key = longPressable.url) { mutableStateOf(false) }
127+
var showEditor by rememberSaveable { mutableStateOf(false) }
126128
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
127129

128130
if (showEditor) {
@@ -253,9 +255,22 @@ private fun LongPressMenuContent(
253255

254256
@Composable
255257
fun LongPressMenuDragHandle(onEditActions: () -> Unit) {
258+
var showFocusTrap by remember { mutableStateOf(true) }
259+
256260
Box(
257261
modifier = Modifier.fillMaxWidth()
258262
) {
263+
if (showFocusTrap) {
264+
// Just a focus trap to make sure the button below (onEditActions) is not the button
265+
// that is first focused when opening the view. That would be a problem on Android TVs
266+
// with DPAD, where the long press menu is opened by long pressing on stuff, and the UP
267+
// event of the long press would click the button below if it were the first focused.
268+
// This way we create a focus trap which disappears as soon as it is focused, leaving
269+
// the focus to "nothing focused". Ideally it would be great to focus the first item in
270+
// the long press menu, but then there would need to be a way to ignore the UP from the
271+
// DPAD after an externally-triggered long press.
272+
Box(Modifier.size(1.dp).focusable().onFocusChanged { showFocusTrap = !it.isFocused })
273+
}
259274
BottomSheetDefaults.DragHandle(
260275
modifier = Modifier.align(Alignment.Center)
261276
)

0 commit comments

Comments
 (0)