Skip to content

Commit 390413e

Browse files
committed
Implement LongPressMenuEditor UI (still not persisted)
1 parent 00d8d11 commit 390413e

4 files changed

Lines changed: 462 additions & 2 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.schabi.newpipe.ui
2+
3+
import androidx.compose.foundation.gestures.awaitEachGesture
4+
import androidx.compose.foundation.gestures.awaitFirstDown
5+
import androidx.compose.ui.Modifier
6+
import androidx.compose.ui.geometry.Offset
7+
import androidx.compose.ui.input.pointer.pointerInput
8+
import androidx.compose.ui.unit.IntOffset
9+
10+
/**
11+
* Detects a drag gesture **without** trying to filter out any misclicks. This is useful in menus
12+
* where items are dragged around, where the usual misclick guardrails would cause unexpected lags
13+
* or strange behaviors when dragging stuff around quickly. For other use cases, use
14+
* [androidx.compose.foundation.gestures.detectDragGestures] or
15+
* [androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress].
16+
*
17+
* @param beginDragGesture called when the user first touches the screen (down event) with the
18+
* pointer position, should return `true` if the receiver wants to handle this gesture, `false`
19+
* otherwise.
20+
* @param handleDragGestureChange called with the current pointer position, every time the user
21+
* moves the finger after [beginDragGesture] has returned `true`.
22+
* @param endDragGesture called when the drag gesture finishes after [beginDragGesture] has returned
23+
* `true`.
24+
*/
25+
fun Modifier.detectDragGestures(
26+
beginDragGesture: (IntOffset) -> Boolean,
27+
handleDragGestureChange: (IntOffset) -> Unit,
28+
endDragGesture: () -> Unit
29+
): Modifier {
30+
return this.pointerInput(Unit) {
31+
awaitEachGesture {
32+
val down = awaitFirstDown()
33+
val pointerId = down.id
34+
if (!beginDragGesture(down.position.toIntOffset())) {
35+
return@awaitEachGesture
36+
}
37+
while (true) {
38+
val change = awaitPointerEvent().changes.find { it.id == pointerId }
39+
if (change == null || !change.pressed) {
40+
break
41+
}
42+
handleDragGestureChange(change.position.toIntOffset())
43+
change.consume()
44+
}
45+
endDragGesture()
46+
}
47+
}
48+
}
49+
50+
private fun Offset.toIntOffset() = IntOffset(this.x.toInt(), this.y.toInt())

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ data class LongPressAction(
6363
Enqueue(R.string.enqueue, Icons.Default.AddToQueue),
6464
EnqueueNext(R.string.enqueue_next_stream, Icons.Default.QueuePlayNext),
6565
Background(R.string.controls_background_title, Icons.Default.Headset),
66-
BackgroundFromHere(R.string.background_from_here, Icons.Default.BackgroundFromHere),
6766
Popup(R.string.controls_popup_title, Icons.Default.PictureInPicture),
68-
PopupFromHere(R.string.popup_from_here, Icons.Default.PopupFromHere),
6967
Play(R.string.play, Icons.Default.PlayArrow),
68+
BackgroundFromHere(R.string.background_from_here, Icons.Default.BackgroundFromHere),
69+
PopupFromHere(R.string.popup_from_here, Icons.Default.PopupFromHere),
7070
PlayFromHere(R.string.play_from_here, Icons.Default.PlayFromHere),
7171
PlayWithKodi(R.string.play_with_kodi_title, Icons.Default.Cast),
7272
Download(R.string.download, Icons.Default.Download),
@@ -89,6 +89,16 @@ data class LongPressAction(
8989
enabled: (isPlayerRunning: Boolean) -> Boolean = { true },
9090
action: (context: Context) -> Unit,
9191
) = LongPressAction(this, action, enabled)
92+
93+
companion object {
94+
// ShowChannelDetails is not enabled by default, since navigating to channel details can
95+
// also be done by clicking on the uploader name in the long press menu header
96+
val DefaultEnabledActions: Array<Type> = arrayOf(
97+
Enqueue, EnqueueNext, Background, Popup, BackgroundFromHere, Download,
98+
AddToPlaylist, Share, OpenInBrowser, MarkAsWatched, Delete,
99+
Rename, SetAsPlaylistThumbnail, UnsetPlaylistThumbnail, Unsubscribe
100+
)
101+
}
92102
}
93103

94104
companion object {

0 commit comments

Comments
 (0)