@@ -8,6 +8,7 @@ import android.content.res.Configuration
88import android.view.ViewGroup
99import android.view.ViewGroup.LayoutParams
1010import androidx.compose.foundation.clickable
11+ import androidx.compose.foundation.focusable
1112import androidx.compose.foundation.isSystemInDarkTheme
1213import androidx.compose.foundation.layout.Arrangement
1314import androidx.compose.foundation.layout.Box
@@ -50,6 +51,7 @@ import androidx.compose.runtime.setValue
5051import androidx.compose.ui.Alignment
5152import androidx.compose.ui.Modifier
5253import androidx.compose.ui.draw.clip
54+ import androidx.compose.ui.focus.onFocusChanged
5355import androidx.compose.ui.graphics.Color
5456import androidx.compose.ui.graphics.vector.ImageVector
5557import 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
255257fun 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