Skip to content

Commit

Permalink
Remove Modifier.handleDPadKeyEvents from pillarbox-ui (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 authored Dec 21, 2023
1 parent 0d7849f commit 5c812aa
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ import androidx.compose.ui.focus.focusRestorer
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalFocusManager
Expand All @@ -59,6 +54,7 @@ import androidx.tv.material3.Text
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
import ch.srgssr.pillarbox.demo.shared.data.Playlist
import ch.srgssr.pillarbox.demo.shared.ui.NavigationRoutes
import ch.srgssr.pillarbox.demo.tv.extension.onDpadEvent
import ch.srgssr.pillarbox.demo.tv.ui.theme.PillarboxTheme
import ch.srgssr.pillarbox.demo.tv.ui.theme.paddings
import coil.compose.AsyncImage
Expand Down Expand Up @@ -212,11 +208,16 @@ private fun <T> ExamplesSection(
columns = TvGridCells.Fixed(columnCount),
modifier = Modifier
.focusRestorer()
.onPreviewKeyEvent {
if (it.key == Key.DirectionUp && it.type == KeyEventType.KeyDown && isOnFirstRow) {
focusedIndex = -1
focusManager.moveFocus(FocusDirection.Up)
} else if (it.key == Key.Back && it.type == KeyEventType.KeyDown) {
.onDpadEvent(
onUp = {
if (isOnFirstRow) {
focusedIndex = -1
focusManager.moveFocus(FocusDirection.Up)
} else {
false
}
},
onBack = {
if (!isOnFirstRow) {
focusedIndex = 0

Expand All @@ -232,10 +233,8 @@ private fun <T> ExamplesSection(
} else {
false
}
} else {
false
}
},
),
state = scrollState,
contentPadding = PaddingValues(vertical = MaterialTheme.paddings.baseline),
verticalArrangement = Arrangement.spacedBy(MaterialTheme.paddings.baseline),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.demo.tv.extension

import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type

/**
* This [Modifier] allows you to define actions to perform when a button of the D-pad or the back button is press. Each action returns a [Boolean]
* to indicate if the event was handled or not.
*
* @param onLeft The action to perform when the left button is press.
* @param onUp The action to perform when the up button is press.
* @param onRight The action to perform when the right button is press.
* @param onDown The action to perform when the down button is press.
* @param onEnter The action to perform when the enter button is press.
* @param onBack The action to perform when the back button is press.
*/
fun Modifier.onDpadEvent(
onLeft: () -> Boolean = { false },
onUp: () -> Boolean = { false },
onRight: () -> Boolean = { false },
onDown: () -> Boolean = { false },
onEnter: () -> Boolean = { false },
onBack: () -> Boolean = { false }
): Modifier {
return onPreviewKeyEvent {
if (it.type == KeyEventType.KeyDown) {
when (it.key) {
Key.DirectionLeft,
Key.SystemNavigationLeft,
-> onLeft()

Key.DirectionUp,
Key.SystemNavigationUp,
-> onUp()

Key.DirectionRight,
Key.SystemNavigationRight,
-> onRight()

Key.DirectionDown,
Key.SystemNavigationDown,
-> onDown()

Key.Enter,
Key.DirectionCenter,
Key.NumPadEnter,
-> onEnter()

Key.Back -> onBack()

else -> false
}
} else {
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import androidx.tv.material3.IconButton
import androidx.tv.material3.MaterialTheme
import androidx.tv.material3.rememberDrawerState
import ch.srgssr.pillarbox.demo.shared.R
import ch.srgssr.pillarbox.demo.tv.extension.onDpadEvent
import ch.srgssr.pillarbox.demo.tv.ui.theme.paddings
import ch.srgssr.pillarbox.ui.extension.handleDPadKeyEvents
import ch.srgssr.pillarbox.ui.extension.playerErrorAsState
import ch.srgssr.pillarbox.ui.widget.maintainVisibleOnFocus
import ch.srgssr.pillarbox.ui.widget.player.PlayerSurface
Expand Down Expand Up @@ -70,9 +70,12 @@ fun TvPlayerView(
player = player,
modifier = Modifier
.fillMaxSize()
.handleDPadKeyEvents(onEnter = {
visibilityState.show()
})
.onDpadEvent(
onEnter = {
visibilityState.show()
true
}
)
.focusable(true)
)
AnimatedVisibility(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.focusRestorer
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -34,6 +29,7 @@ import androidx.tv.material3.Tab
import androidx.tv.material3.TabRow
import androidx.tv.material3.Text
import ch.srgssr.pillarbox.demo.shared.ui.HomeDestination
import ch.srgssr.pillarbox.demo.tv.extension.onDpadEvent
import ch.srgssr.pillarbox.demo.tv.ui.theme.PillarboxTheme
import ch.srgssr.pillarbox.demo.tv.ui.theme.paddings

Expand Down Expand Up @@ -68,16 +64,14 @@ fun TVDemoTopBar(
selectedTabIndex = focusedTabIndex,
modifier = modifier
.focusRestorer()
.onPreviewKeyEvent {
if (it.key == Key.DirectionRight && it.type == KeyEventType.KeyDown) {
.onDpadEvent(
onRight = {
if (focusedTabIndex < destinations.lastIndex) {
focusManager.moveFocus(FocusDirection.Right)
}
true
} else {
false
}
}
)
) {
destinations.forEachIndexed { index, destination ->
key(index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -93,6 +88,7 @@ import ch.srgssr.pillarbox.demo.shared.ui.integrationLayer.data.ContentListSecti
import ch.srgssr.pillarbox.demo.shared.ui.integrationLayer.data.contentListFactories
import ch.srgssr.pillarbox.demo.shared.ui.integrationLayer.data.contentListSections
import ch.srgssr.pillarbox.demo.tv.R
import ch.srgssr.pillarbox.demo.tv.extension.onDpadEvent
import ch.srgssr.pillarbox.demo.tv.player.PlayerActivity
import ch.srgssr.pillarbox.demo.tv.ui.theme.PillarboxTheme
import ch.srgssr.pillarbox.demo.tv.ui.theme.paddings
Expand Down Expand Up @@ -285,16 +281,16 @@ private fun <T> ListsSection(
columns = TvGridCells.Fixed(columnCount),
modifier = Modifier
.focusRestorer()
.onKeyEvent(
onUpPress = {
.onDpadEvent(
onUp = {
if (isOnFirstRow) {
focusedIndex = -1
focusManager.moveFocus(FocusDirection.Up)
} else {
false
}
},
onBackPress = {
onBack = {
if (!isOnFirstRow) {
focusedIndex = 0

Expand Down Expand Up @@ -451,16 +447,16 @@ private fun <T : Content> ListsSectionContent(
columns = TvGridCells.Fixed(columnCount),
modifier = modifier
.focusRestorer()
.onKeyEvent(
onUpPress = {
.onDpadEvent(
onUp = {
if (isOnFirstRow) {
focusedIndex = -1
focusManager.moveFocus(FocusDirection.Up)
} else {
false
}
},
onBackPress = {
onBack = {
if (!isOnFirstRow) {
focusedIndex = 0

Expand Down Expand Up @@ -703,23 +699,6 @@ private fun ListsSectionError(
}
}

private fun Modifier.onKeyEvent(
onUpPress: () -> Boolean,
onBackPress: () -> Boolean
): Modifier {
return this then Modifier.onPreviewKeyEvent { keyEvent ->
if (keyEvent.type == KeyEventType.KeyDown) {
when (keyEvent.key) {
Key.DirectionUp -> onUpPress()
Key.Back -> onBackPress()
else -> false
}
} else {
false
}
}
}

@Preview
@Composable
private fun ContentListsViewPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
Expand All @@ -52,6 +47,7 @@ import ch.srgssr.pillarbox.demo.shared.R
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
import ch.srgssr.pillarbox.demo.shared.ui.integrationLayer.SearchViewModel
import ch.srgssr.pillarbox.demo.shared.ui.integrationLayer.data.bus
import ch.srgssr.pillarbox.demo.tv.extension.onDpadEvent
import ch.srgssr.pillarbox.demo.tv.player.PlayerActivity
import ch.srgssr.pillarbox.demo.tv.ui.theme.PillarboxTheme
import ch.srgssr.pillarbox.demo.tv.ui.theme.paddings
Expand Down Expand Up @@ -132,14 +128,12 @@ private fun SearchRow(
query = query,
modifier = Modifier
.fillMaxWidth()
.onPreviewKeyEvent {
if (it.key == Key.Back && it.type == KeyEventType.KeyDown) {
.onDpadEvent(
onBack = {
focusManager.moveFocus(FocusDirection.Up)
true
} else {
false
}
},
),
onQueryChange = onQueryChange
)

Expand Down

This file was deleted.

Loading

0 comments on commit 5c812aa

Please sign in to comment.