Skip to content

Commit e225326

Browse files
committed
Implement LongPressMenuEditor UI (still not persisted)
1 parent b26ab27 commit e225326

6 files changed

Lines changed: 465 additions & 5 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/PlayerService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ class PlayerService : MediaBrowserServiceCompat() {
148148
// a (dummy) foreground notification, otherwise we'd incur in
149149
// "Context.startForegroundService() did not then call Service.startForeground()". Then
150150
// we stop the service again.
151-
Log.d(TAG, "onStartCommand() got a useless intent, closing the service");
152-
NotificationUtil.startForegroundWithDummyNotification(this);
151+
Log.d(TAG, "onStartCommand() got a useless intent, closing the service")
152+
NotificationUtil.startForegroundWithDummyNotification(this)
153153
return START_NOT_STICKY
154154
}
155155

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
@@ -60,10 +60,10 @@ data class LongPressAction(
6060
Enqueue(R.string.enqueue, Icons.Default.AddToQueue),
6161
EnqueueNext(R.string.enqueue_next_stream, Icons.Default.QueuePlayNext),
6262
Background(R.string.controls_background_title, Icons.Default.Headset),
63-
BackgroundFromHere(R.string.background_from_here, Icons.Default.BackgroundFromHere),
6463
Popup(R.string.controls_popup_title, Icons.Default.PictureInPicture),
65-
PopupFromHere(R.string.popup_from_here, Icons.Default.PopupFromHere),
6664
Play(R.string.play, Icons.Default.PlayArrow),
65+
BackgroundFromHere(R.string.background_from_here, Icons.Default.BackgroundFromHere),
66+
PopupFromHere(R.string.popup_from_here, Icons.Default.PopupFromHere),
6767
PlayFromHere(R.string.play_from_here, Icons.Default.PlayFromHere),
6868
PlayWithKodi(R.string.play_with_kodi_title, Icons.Default.Cast),
6969
Download(R.string.download, Icons.Default.Download),
@@ -86,6 +86,16 @@ data class LongPressAction(
8686
enabled: (isPlayerRunning: Boolean) -> Boolean = { true },
8787
action: (context: Context) -> Unit,
8888
) = LongPressAction(this, action, enabled)
89+
90+
companion object {
91+
// ShowChannelDetails is not enabled by default, since navigating to channel details can
92+
// also be done by clicking on the uploader name in the long press menu header
93+
val DefaultEnabledActions: Array<Type> = arrayOf(
94+
Enqueue, EnqueueNext, Background, Popup, BackgroundFromHere, Download,
95+
AddToPlaylist, Share, OpenInBrowser, MarkAsWatched, Delete,
96+
Rename, SetAsPlaylistThumbnail, UnsetPlaylistThumbnail, Unsubscribe
97+
)
98+
}
8999
}
90100

91101
companion object {

0 commit comments

Comments
 (0)