-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSeekBarView.cpp
More file actions
180 lines (152 loc) · 4.82 KB
/
SeekBarView.cpp
File metadata and controls
180 lines (152 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "SeekBarView.h"
#include "Messages.h"
#include <InterfaceDefs.h>
#include <Message.h>
#include <MessageRunner.h>
#include <Messenger.h>
#include <String.h>
#include <Window.h>
#include <algorithm>
#include <cstdio>
/**
* @brief Constructor.
* @param name The name of the view.
*/
SeekBarView::SeekBarView(const char *name)
: BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE), fDuration(0),
fPosition(0), fTracking(false) {
SetViewColor(B_TRANSPARENT_COLOR);
fBg = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), B_DARKEN_1_TINT);
fFill = ui_color(B_CONTROL_HIGHLIGHT_COLOR);
fBorder = tint_color(fBg, B_DARKEN_2_TINT);
// Calculate font-relative sizes for DPI scaling
font_height fh;
be_plain_font->GetHeight(&fh);
float fontHeight = fh.ascent + fh.descent + fh.leading;
SetExplicitMinSize(BSize(fontHeight * 14, fontHeight));
SetExplicitPreferredSize(BSize(fontHeight * 24, fontHeight));
}
void SeekBarView::AttachedToWindow() { SetLowColor(ViewColor()); }
/**
* @brief Formats a time in microseconds to "MM:SS".
* @param usec Time in microseconds.
* @param out Output BString.
*/
static void FormatTime(bigtime_t usec, BString &out) {
int seconds = usec / 1000000;
int min = seconds / 60;
int sec = seconds % 60;
out.SetToFormat("%d:%02d", min, sec);
}
/**
* @brief Sets the total duration of the track.
* @param duration Duration in microseconds.
*/
void SeekBarView::SetDuration(bigtime_t duration) {
if (duration < 0)
duration = 0;
fDuration = duration;
if (fPosition > fDuration)
fPosition = fDuration;
Invalidate();
}
/**
* @brief Sets the current playback position.
* @param pos Position in microseconds.
*/
void SeekBarView::SetPosition(bigtime_t pos) {
if (pos < 0)
pos = 0;
if (fDuration > 0 && pos > fDuration)
pos = fDuration;
fPosition = pos;
Invalidate();
}
/**
* @brief Sets custom colors for the seek bar.
*/
void SeekBarView::SetColors(rgb_color bg, rgb_color fill, rgb_color border) {
fBg = bg;
fFill = fill;
fBorder = border;
Invalidate();
}
void SeekBarView::Draw(BRect) { _DrawBar(Bounds()); }
/**
* @brief Internal method to draw the seek bar.
* @param r The rectangle to draw into (usually Bounds()).
*/
void SeekBarView::_DrawBar(const BRect &r) {
// Clear background first to avoid corner artifacts
SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
FillRect(r);
SetHighColor(fBg);
FillRoundRect(r, 2, 2);
SetHighColor(fBorder);
StrokeRoundRect(r, 2, 2);
if (fDuration > 0) {
float ratio = static_cast<float>(fPosition) / static_cast<float>(fDuration);
ratio = std::clamp(ratio, 0.0f, 1.0f);
BRect fillRect = r;
fillRect.InsetBy(1, 1);
fillRect.right = fillRect.left + ratio * r.Width();
if (fillRect.right < fillRect.left)
fillRect.right = fillRect.left;
SetHighColor(fFill);
FillRoundRect(fillRect, 2, 2);
BString left, right;
FormatTime(fPosition, left);
FormatTime(fDuration, right);
font_height fh;
GetFontHeight(&fh);
float y = Bounds().bottom - fh.descent;
SetHighColor(0, 0, 0);
DrawString(left.String(), BPoint(Bounds().left + 4, y));
float w = StringWidth(right.String());
DrawString(right.String(), BPoint(Bounds().right - w - 4, y));
}
}
void SeekBarView::MouseDown(BPoint where) {
_SeekFromPoint(where);
fTracking = true;
SetMouseEventMask(B_POINTER_EVENTS, 0);
}
void SeekBarView::MouseUp(BPoint) { fTracking = false; }
void SeekBarView::MouseMoved(BPoint where, uint32 transit, const BMessage *) {
if (fTracking)
_SeekFromPoint(where);
}
/**
* @brief Calculates seek position from mouse point and notifies target window.
*/
void SeekBarView::_SeekFromPoint(BPoint where) {
if (fDuration <= 0)
return;
float ratio = (where.x - Bounds().left) / Bounds().Width();
ratio = std::clamp(ratio, 0.0f, 1.0f);
bigtime_t newPos = static_cast<bigtime_t>(ratio * fDuration);
SetPosition(newPos);
BMessage msg(MSG_SEEK_REQUEST);
msg.AddInt64("position", newPos);
BMessenger msgr(NULL, Window());
msgr.SendMessage(&msg);
}
void SeekBarView::MessageReceived(BMessage *msg) {
// Handle drag-and-drop color from Color Picker
if (msg->WasDropped()) {
rgb_color *color;
ssize_t size;
if (msg->FindData("RGBColor", B_RGB_COLOR_TYPE, (const void **)&color,
&size) == B_OK &&
size == sizeof(rgb_color)) {
fFill = *color;
Invalidate();
// Notify MainWindow about color change
BMessage notify(MSG_SEEKBAR_COLOR_DROPPED);
notify.AddData("color", B_RGB_COLOR_TYPE, color, sizeof(rgb_color));
BMessenger(Window()).SendMessage(¬ify);
return;
}
}
BView::MessageReceived(msg);
}