Skip to content

Commit 90d6c7c

Browse files
committed
Improve style of item being dragged in LongPressMenuEditor
1 parent 34d4eae commit 90d6c7c

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ fun LongPressMenuEditorPage(onBackClick: () -> Unit) {
149149
ItemInListUi(
150150
item = item,
151151
focused = state.currentlyFocusedItem == i,
152+
beingDragged = false,
152153
// We only want placement animations: fade in/out animations interfere with
153154
// items being replaced by a drag marker while being dragged around, and a
154155
// fade in/out animation there does not make sense as the item was just
@@ -167,7 +168,8 @@ fun LongPressMenuEditorPage(onBackClick: () -> Unit) {
167168
}
168169
ItemInListUi(
169170
item = activeDragItem,
170-
focused = true,
171+
focused = false,
172+
beingDragged = true,
171173
modifier = Modifier
172174
.size(size)
173175
.offset { state.activeDragPosition }
@@ -287,12 +289,15 @@ private fun ActionOrHeaderBox(
287289
* different parameters
288290
* @param focused if `true`, a box will be drawn around the item to indicate that it is focused
289291
* (this will only ever be `true` when the user is navigating with DPAD, e.g. on Android TVs)
292+
* @param beingDragged if `true`, draw a semi-transparent background to show that the item is being
293+
* dragged
290294
*/
291295
@Composable
292296
private fun ItemInListUi(
293297
item: ItemInList,
294298
focused: Boolean,
295-
modifier: Modifier = Modifier
299+
beingDragged: Boolean,
300+
modifier: Modifier
296301
) {
297302
when (item) {
298303
ItemInList.EnabledCaption -> {
@@ -319,7 +324,9 @@ private fun ItemInListUi(
319324
focused = focused,
320325
icon = item.type.icon,
321326
text = item.type.label,
322-
contentColor = MaterialTheme.colorScheme.onSurface
327+
contentColor = MaterialTheme.colorScheme.onSurface,
328+
backgroundColor = MaterialTheme.colorScheme.surface
329+
.letIf(beingDragged) { copy(alpha = 0.7f) }
323330
)
324331
}
325332

@@ -330,7 +337,8 @@ private fun ItemInListUi(
330337
icon = Icons.Default.ArtTrack,
331338
text = R.string.long_press_menu_header,
332339
contentColor = MaterialTheme.colorScheme.onSurfaceVariant,
333-
backgroundColor = MaterialTheme.colorScheme.surfaceContainer,
340+
backgroundColor = MaterialTheme.colorScheme.surfaceContainer
341+
.letIf(beingDragged) { copy(alpha = 0.85f) },
334342
horizontalPadding = 12.dp
335343
)
336344
}
@@ -384,6 +392,7 @@ private fun QuickActionButtonPreview(
384392
ItemInListUi(
385393
item = itemInList,
386394
focused = itemInList.stableUniqueKey() % 2 == 0,
395+
beingDragged = false,
387396
modifier = Modifier.width(MinButtonWidth * (itemInList.columnSpan ?: 4))
388397
)
389398
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class LongPressMenuEditorState(
438438
val rawItem = gridState.layoutInfo.visibleItemsInfo
439439
.firstOrNull { it.index == focusedItem }
440440
?: return false
441-
beginDrag(rawItem.offset, rawItem)
441+
beginDrag(rawItem.center(), rawItem)
442442
return true
443443
} else {
444444
completeDragAndCleanUp()
@@ -487,7 +487,7 @@ class LongPressMenuEditorState(
487487
// position is moved past HiddenCaption by handleDragGestureChange() below.
488488
// However, it's not worth overcomplicating the logic just for correcting
489489
// the UI position of a drag hint on Android TVs.
490-
activeDragPosition = rawItem.offset
490+
activeDragPosition = rawItem.center()
491491
handleDragChange(dragItem, rawItem)
492492
}
493493
return true
@@ -549,3 +549,7 @@ sealed class ItemInList(
549549
}
550550
}
551551
}
552+
553+
fun LazyGridItemInfo.center(): IntOffset {
554+
return offset + IntOffset(size.width / 2, size.height / 2)
555+
}

0 commit comments

Comments
 (0)