|
| 1 | +/* |
| 2 | + * Copyright (C) NewPipe Contributors |
| 3 | + * ChaptersSeekBar.kt is part of NewPipe. |
| 4 | + * |
| 5 | + * NewPipe is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * NewPipe is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with NewPipe. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package org.schabi.newpipe.views |
| 19 | + |
| 20 | +import android.content.Context |
| 21 | +import android.graphics.Canvas |
| 22 | +import android.graphics.Paint |
| 23 | +import android.graphics.PorterDuff |
| 24 | +import android.graphics.PorterDuffXfermode |
| 25 | +import android.util.AttributeSet |
| 26 | +import org.schabi.newpipe.extractor.stream.StreamSegment |
| 27 | + |
| 28 | +/** |
| 29 | + * A [FocusAwareSeekBar] that renders narrow transparent gaps at chapter boundaries, |
| 30 | + * giving the seekbar a segmented "chopped" appearance. |
| 31 | + * Call [setChapters] whenever a new stream loads. |
| 32 | + */ |
| 33 | +class ChaptersSeekBar : FocusAwareSeekBar { |
| 34 | + |
| 35 | + private val gapPaint = Paint().apply { |
| 36 | + style = Paint.Style.FILL |
| 37 | + xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) |
| 38 | + } |
| 39 | + |
| 40 | + private var chapters: List<StreamSegment> = emptyList() |
| 41 | + private var durationSeconds: Long = 0 |
| 42 | + |
| 43 | + constructor(context: Context) : super(context) |
| 44 | + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) |
| 45 | + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : |
| 46 | + super(context, attrs, defStyleAttr) |
| 47 | + |
| 48 | + /** |
| 49 | + * Stores chapter data for rendering segment gaps. |
| 50 | + * |
| 51 | + * @param newChapters list of [StreamSegment]s; may be empty but never null |
| 52 | + * @param newDurationSecs total duration in seconds; used to compute fractional positions |
| 53 | + */ |
| 54 | + fun setChapters(newChapters: List<StreamSegment>, newDurationSecs: Long) { |
| 55 | + chapters = newChapters |
| 56 | + durationSeconds = newDurationSecs |
| 57 | + invalidate() |
| 58 | + } |
| 59 | + |
| 60 | + override fun onDraw(canvas: Canvas) { |
| 61 | + if (chapters.isEmpty() || durationSeconds <= 0) { |
| 62 | + super.onDraw(canvas) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // Draw the seekbar into an offscreen layer so CLEAR mode can punch transparent gaps |
| 67 | + val sc = canvas.saveLayer(null, null) |
| 68 | + super.onDraw(canvas) |
| 69 | + |
| 70 | + val density = resources.displayMetrics.density |
| 71 | + val gapHalfWidth = (GAP_WIDTH_DP * density) / 2f |
| 72 | + val left = paddingLeft |
| 73 | + val trackWidth = (width - left - paddingRight).toFloat() |
| 74 | + |
| 75 | + if (trackWidth > 0) { |
| 76 | + for (seg in chapters) { |
| 77 | + val startSec = seg.startTimeSeconds |
| 78 | + // Skip the very first position and anything at or past the end |
| 79 | + if (startSec <= 0 || startSec.toLong() >= durationSeconds) { |
| 80 | + continue |
| 81 | + } |
| 82 | + val x = left + (startSec.toFloat() / durationSeconds.toFloat()) * trackWidth |
| 83 | + canvas.drawRect(x - gapHalfWidth, 0f, x + gapHalfWidth, height.toFloat(), gapPaint) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + canvas.restoreToCount(sc) |
| 88 | + |
| 89 | + // Redraw the thumb on top so it visually overlaps the gaps |
| 90 | + val t = thumb |
| 91 | + if (t != null) { |
| 92 | + val thumbSave = canvas.save() |
| 93 | + canvas.translate((paddingLeft - thumbOffset).toFloat(), paddingTop.toFloat()) |
| 94 | + t.draw(canvas) |
| 95 | + canvas.restoreToCount(thumbSave) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + companion object { |
| 100 | + private const val GAP_WIDTH_DP = 2f |
| 101 | + } |
| 102 | +} |
0 commit comments