|
| 1 | +package org.schabi.newpipe.util.text |
| 2 | + |
| 3 | +import androidx.compose.foundation.MarqueeSpacing |
| 4 | +import androidx.compose.foundation.basicMarquee |
| 5 | +import androidx.compose.foundation.layout.padding |
| 6 | +import androidx.compose.ui.Modifier |
| 7 | +import androidx.compose.ui.draw.drawWithContent |
| 8 | +import androidx.compose.ui.geometry.Offset |
| 9 | +import androidx.compose.ui.geometry.Size |
| 10 | +import androidx.compose.ui.graphics.BlendMode |
| 11 | +import androidx.compose.ui.graphics.Brush |
| 12 | +import androidx.compose.ui.graphics.Color |
| 13 | +import androidx.compose.ui.graphics.CompositingStrategy |
| 14 | +import androidx.compose.ui.graphics.drawscope.ContentDrawScope |
| 15 | +import androidx.compose.ui.graphics.graphicsLayer |
| 16 | +import androidx.compose.ui.unit.Dp |
| 17 | + |
| 18 | +/** |
| 19 | + * Note: the values in [basicMarquee] are hardcoded, but feel free to expose them as parameters |
| 20 | + * in case that will be needed in the future. |
| 21 | + * |
| 22 | + * Taken from sample [androidx.compose.foundation.samples.BasicMarqueeWithFadedEdgesSample]. |
| 23 | + */ |
| 24 | +fun Modifier.fadedMarquee(edgeWidth: Dp): Modifier { |
| 25 | + fun ContentDrawScope.drawFadedEdge(leftEdge: Boolean) { |
| 26 | + val edgeWidthPx = edgeWidth.toPx() |
| 27 | + drawRect( |
| 28 | + topLeft = Offset(if (leftEdge) 0f else size.width - edgeWidthPx, 0f), |
| 29 | + size = Size(edgeWidthPx, size.height), |
| 30 | + brush = Brush.horizontalGradient( |
| 31 | + colors = listOf(Color.Transparent, Color.Black), |
| 32 | + startX = if (leftEdge) 0f else size.width, |
| 33 | + endX = if (leftEdge) edgeWidthPx else size.width - edgeWidthPx |
| 34 | + ), |
| 35 | + blendMode = BlendMode.DstIn |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + return this |
| 40 | + .graphicsLayer { compositingStrategy = CompositingStrategy.Offscreen } |
| 41 | + .drawWithContent { |
| 42 | + drawContent() |
| 43 | + drawFadedEdge(leftEdge = true) |
| 44 | + drawFadedEdge(leftEdge = false) |
| 45 | + } |
| 46 | + .basicMarquee( |
| 47 | + repeatDelayMillis = 2000, |
| 48 | + // wait some time before starting animations, to not distract the user |
| 49 | + initialDelayMillis = 4000, |
| 50 | + iterations = Int.MAX_VALUE, |
| 51 | + spacing = MarqueeSpacing(edgeWidth) |
| 52 | + ) |
| 53 | + .padding(start = edgeWidth) |
| 54 | +} |
0 commit comments