Skip to content

Commit 3dba420

Browse files
committed
Add ChaptersSeekBar to render chapter markers on the seekbar
1 parent 0e591ab commit 3dba420

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (C) NewPipe Contributors
3+
* ChaptersSeekBar.java 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.Color;
23+
import android.graphics.Paint;
24+
import android.util.AttributeSet;
25+
import android.util.Log;
26+
27+
import androidx.annotation.NonNull;
28+
import androidx.annotation.Nullable;
29+
30+
import org.schabi.newpipe.extractor.stream.StreamSegment;
31+
32+
import java.util.Collections;
33+
import java.util.List;
34+
35+
/**
36+
* A {@link FocusAwareSeekBar} that draws thin white vertical tick marks at chapter boundaries.
37+
* Call {@link #setChapters(List, long)} whenever a new stream loads.
38+
*/
39+
public final class ChaptersSeekBar extends FocusAwareSeekBar {
40+
41+
private static final String TAG = "ChaptersSeekBar";
42+
43+
private static final int TICK_ALPHA = 180; // ~70% opacity
44+
private static final float TICK_WIDTH_DP = 2f;
45+
private static final float TICK_HEIGHT_FRACTION = 0.6f; // fraction of view height
46+
47+
private final Paint tickPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
48+
49+
@NonNull private List<StreamSegment> chapters = Collections.emptyList();
50+
private long durationSeconds = 0;
51+
52+
public ChaptersSeekBar(@NonNull final Context context) {
53+
super(context);
54+
init();
55+
}
56+
57+
public ChaptersSeekBar(@NonNull final Context context,
58+
@Nullable final AttributeSet attrs) {
59+
super(context, attrs);
60+
init();
61+
}
62+
63+
public ChaptersSeekBar(@NonNull final Context context,
64+
@Nullable final AttributeSet attrs,
65+
final int defStyleAttr) {
66+
super(context, attrs, defStyleAttr);
67+
init();
68+
}
69+
70+
private void init() {
71+
tickPaint.setColor(Color.WHITE);
72+
tickPaint.setAlpha(TICK_ALPHA);
73+
tickPaint.setStyle(Paint.Style.FILL);
74+
Log.d(TAG, "init: ChaptersSeekBar created");
75+
}
76+
77+
/**
78+
* Stores chapter data for rendering tick marks.
79+
*
80+
* @param newChapters list of {@link StreamSegment}s; may be empty but never null
81+
* @param newDurationSecs total duration in seconds; used to compute fractional positions
82+
*/
83+
public void setChapters(@NonNull final List<StreamSegment> newChapters,
84+
final long newDurationSecs) {
85+
chapters = newChapters;
86+
durationSeconds = newDurationSecs;
87+
Log.d(TAG, "setChapters: count=" + newChapters.size()
88+
+ " durationSeconds=" + newDurationSecs);
89+
for (final StreamSegment seg : newChapters) {
90+
Log.d(TAG, " chapter: startSec=" + seg.getStartTimeSeconds()
91+
+ " title=" + seg.getTitle());
92+
}
93+
invalidate();
94+
}
95+
96+
@Override
97+
protected void onDraw(@NonNull final Canvas canvas) {
98+
super.onDraw(canvas);
99+
100+
if (chapters.isEmpty() || durationSeconds <= 0) {
101+
Log.d(TAG, "onDraw: skipped — chapters=" + chapters.size()
102+
+ " durationSeconds=" + durationSeconds);
103+
return;
104+
}
105+
106+
final float density = getResources().getDisplayMetrics().density;
107+
final float tickWidthPx = TICK_WIDTH_DP * density;
108+
109+
// Track bounds: AbsSeekBar pads the track by getPaddingLeft/getPaddingRight
110+
final int paddingLeft = getPaddingLeft();
111+
final int paddingRight = getPaddingRight();
112+
final float trackWidth = getWidth() - paddingLeft - paddingRight;
113+
114+
Log.d(TAG, "onDraw: w=" + getWidth() + " h=" + getHeight()
115+
+ " paddingL=" + paddingLeft + " paddingR=" + paddingRight
116+
+ " trackWidth=" + trackWidth + " chapters=" + chapters.size()
117+
+ " durationSeconds=" + durationSeconds);
118+
119+
if (trackWidth <= 0) {
120+
Log.d(TAG, "onDraw: trackWidth<=0, skipping");
121+
return;
122+
}
123+
124+
// Center ticks vertically, scaling height as a fraction of the view
125+
final float tickHeight = getHeight() * TICK_HEIGHT_FRACTION;
126+
final float tickTop = (getHeight() - tickHeight) / 2f;
127+
final float tickBottom = tickTop + tickHeight;
128+
129+
for (final StreamSegment seg : chapters) {
130+
final int startSec = seg.getStartTimeSeconds();
131+
// Skip the very beginning and anything at or past the end
132+
if (startSec <= 0 || startSec >= durationSeconds) {
133+
Log.d(TAG, " skipping seg startSec=" + startSec);
134+
continue;
135+
}
136+
final float x = paddingLeft + (startSec / (float) durationSeconds) * trackWidth;
137+
Log.d(TAG, " drawing tick at x=" + x + " for startSec=" + startSec
138+
+ " title=" + seg.getTitle());
139+
canvas.drawRect(
140+
x - tickWidthPx / 2f,
141+
tickTop,
142+
x + tickWidthPx / 2f,
143+
tickBottom,
144+
tickPaint);
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)