-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArtworkView.cpp
More file actions
417 lines (348 loc) · 10.5 KB
/
Copy pathArtworkView.cpp
File metadata and controls
417 lines (348 loc) · 10.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "ArtworkView.h"
#include "Config.h"
#include "network/ImageCache.h"
#include <algorithm>
#include <Bitmap.h>
#include <Catalog.h>
#include <cmath>
#include <Message.h>
#include <Messenger.h>
#include <View.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "ArtworkView"
namespace {
static const uint32 kMsgArtworkLoaded = 'avLd';
static float Luminance(rgb_color color) {
return (0.299f * color.red + 0.587f * color.green + 0.114f * color.blue) /
255.0f;
}
static int32 PixelWidth(const BBitmap *bitmap) {
return bitmap ? bitmap->Bounds().IntegerWidth() + 1 : 0;
}
static int32 PixelHeight(const BBitmap *bitmap) {
return bitmap ? bitmap->Bounds().IntegerHeight() + 1 : 0;
}
static BBitmap *ScaleBitmap(const BBitmap *source, int32 width, int32 height) {
if (!source || !source->IsValid() || width <= 0 || height <= 0)
return nullptr;
BRect bounds(0, 0, width - 1, height - 1);
BBitmap *target = new BBitmap(bounds, B_RGB32, true);
if (!target->IsValid()) {
delete target;
return nullptr;
}
BView *canvas = new BView(bounds, "artwork scaler", B_FOLLOW_NONE, 0);
target->AddChild(canvas);
canvas->LockLooper();
canvas->SetDrawingMode(B_OP_COPY);
canvas->DrawBitmap(source, source->Bounds(), bounds, B_FILTER_BITMAP_BILINEAR);
canvas->Sync();
canvas->UnlockLooper();
target->RemoveChild(canvas);
delete canvas;
return target;
}
static BBitmap *ScaleBitmapStepwise(const BBitmap *source, int32 targetWidth,
int32 targetHeight) {
int32 currentWidth = PixelWidth(source);
int32 currentHeight = PixelHeight(source);
if (currentWidth <= 0 || currentHeight <= 0)
return nullptr;
const BBitmap *current = source;
BBitmap *owned = nullptr;
while (currentWidth / 2 >= targetWidth && currentHeight / 2 >= targetHeight) {
int32 nextWidth = std::max(targetWidth, currentWidth / 2);
int32 nextHeight = std::max(targetHeight, currentHeight / 2);
BBitmap *next = ScaleBitmap(current, nextWidth, nextHeight);
if (!next)
break;
delete owned;
owned = next;
current = owned;
currentWidth = nextWidth;
currentHeight = nextHeight;
}
BBitmap *result = ScaleBitmap(current, targetWidth, targetHeight);
delete owned;
return result;
}
}
ArtworkView::ArtworkView(const char* name)
: BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE)
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
}
ArtworkView::ArtworkView(BMessage* archive)
: BView(archive)
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
}
void ArtworkView::AttachedToWindow() {
BView::AttachedToWindow();
rgb_color bg = Parent() ? Parent()->ViewColor()
: ui_color(B_PANEL_BACKGROUND_COLOR);
SetViewColor(bg);
SetLowColor(bg);
if (fLoadPending)
_StartLoad(fReloadPending);
}
BArchivable* ArtworkView::Instantiate(BMessage* data) {
if (validate_instantiation(data, "ArtworkView"))
return new ArtworkView(data);
return nullptr;
}
status_t ArtworkView::Archive(BMessage* data, bool) const {
BView::Archive(data, false);
data->AddString("add_on", HAIFY_MIME_SIG);
return B_OK;
}
ArtworkView::~ArtworkView() {
delete fBitmap;
fBitmap = nullptr;
delete fScaledBitmap;
fScaledBitmap = nullptr;
}
void ArtworkView::SetBitmap(BBitmap *bmp) {
BBitmap *clone = nullptr;
if (bmp && bmp->IsValid()) {
clone = new BBitmap(bmp);
if (!clone->IsValid()) {
delete clone;
clone = nullptr;
}
}
delete fBitmap;
fBitmap = clone;
fLoadState = clone ? kLoaded
: (fArtworkUrl.empty() ? kNoCover : kLoadFailed);
_InvalidateScaledBitmap();
ArtworkStateChanged();
Invalidate();
}
void ArtworkView::AdoptBitmap(BBitmap* bmp) {
if (bmp && !bmp->IsValid()) {
delete bmp;
bmp = nullptr;
}
delete fBitmap;
fBitmap = bmp;
fLoadState = bmp ? kLoaded
: (fArtworkUrl.empty() ? kNoCover : kLoadFailed);
_InvalidateScaledBitmap();
ArtworkStateChanged();
Invalidate();
}
void ArtworkView::ShowLoading() {
fArtworkUrl.clear();
++fLoadGeneration;
fLoadPending = false;
fReloadPending = false;
_ClearBitmap();
fLoadState = kLoading;
ArtworkStateChanged();
Invalidate();
}
void ArtworkView::LoadUrl(const std::string& url) {
if (url == fArtworkUrl
&& ((!url.empty() && fLoadState != kNoCover)
|| (url.empty() && fLoadState == kNoCover)))
return;
fArtworkUrl = url;
++fLoadGeneration;
fLoadPending = false;
fReloadPending = false;
_ClearBitmap();
if (fArtworkUrl.empty()) {
fLoadState = kNoCover;
ArtworkStateChanged();
Invalidate();
return;
}
fLoadState = kLoading;
ArtworkStateChanged();
Invalidate();
if (Looper())
_StartLoad(false);
else
fLoadPending = true;
}
void ArtworkView::ReloadUrl() {
if (fArtworkUrl.empty())
return;
++fLoadGeneration;
fLoadPending = false;
fReloadPending = false;
_ClearBitmap();
fLoadState = kLoading;
ArtworkStateChanged();
Invalidate();
if (Looper())
_StartLoad(true);
else {
fLoadPending = true;
fReloadPending = true;
}
}
void ArtworkView::_StartLoad(bool reload) {
fLoadPending = false;
fReloadPending = false;
const std::string requestUrl = fArtworkUrl;
const uint64 generation = fLoadGeneration;
BMessenger self(this);
ImageCallback callback = [self, requestUrl, generation](BBitmap* bitmap) {
BMessage message(kMsgArtworkLoaded);
message.AddString("url", requestUrl.c_str());
message.AddInt64("generation", static_cast<int64>(generation));
if (bitmap)
message.AddPointer("bitmap", bitmap);
if (self.SendMessage(&message) != B_OK)
delete bitmap;
};
if (reload)
ImageCache::ReloadImage(requestUrl, callback);
else
ImageCache::GetImage(requestUrl, callback);
}
void ArtworkView::_ClearBitmap() {
delete fBitmap;
fBitmap = nullptr;
_InvalidateScaledBitmap();
}
bool ArtworkView::HasBitmap() const {
return fBitmap && fBitmap->IsValid();
}
ArtworkView::LoadState ArtworkView::State() const {
return fLoadState;
}
const std::string& ArtworkView::ArtworkUrl() const {
return fArtworkUrl;
}
void ArtworkView::ArtworkStateChanged() {
}
void ArtworkView::Draw(BRect updateRect) {
rgb_color panelBg = ui_color(B_PANEL_BACKGROUND_COLOR);
SetHighColor(panelBg);
FillRect(Bounds());
if (!fBitmap || !fBitmap->IsValid()) {
BRect b = Bounds();
rgb_color placeholderBg =
Luminance(panelBg) > 0.5f ? tint_color(panelBg, B_DARKEN_1_TINT)
: tint_color(panelBg, B_LIGHTEN_1_TINT);
rgb_color mutedText =
tint_color(ui_color(B_PANEL_TEXT_COLOR), B_DISABLED_LABEL_TINT);
rgb_color placeholderText = tint_color(mutedText, B_LIGHTEN_2_TINT);
rgb_color borderCol = tint_color(mutedText, B_LIGHTEN_2_TINT);
SetHighColor(placeholderBg);
FillRect(b);
SetHighColor(borderCol);
StrokeRect(b);
SetHighColor(placeholderText);
const char* text = B_TRANSLATE("No Cover");
if (fLoadState == kLoading)
text = B_TRANSLATE("Loading...");
else if (fLoadState == kLoadFailed)
text = B_TRANSLATE("Load failed");
font_height fh;
GetFontHeight(&fh);
float textWidth = StringWidth(text);
BPoint textPt(b.left + (b.Width() - textWidth) / 2.0f,
b.top + (b.Height() + (fh.ascent - fh.descent)) / 2.0f);
DrawString(text, textPt);
return;
}
BRect frame = _CoverFrame();
_UpdateScaledBitmap();
if (fScaledBitmap) {
SetDrawingMode(B_OP_COPY);
DrawBitmapAsync(fScaledBitmap, fScaledFrame.LeftTop());
} else if (frame.IsValid()) {
SetDrawingMode(B_OP_ALPHA);
DrawBitmapAsync(fBitmap, fBitmap->Bounds(), frame, B_FILTER_BITMAP_BILINEAR);
}
SetDrawingMode(B_OP_COPY);
}
void ArtworkView::MessageReceived(BMessage *msg) {
switch (msg->what) {
case kMsgArtworkLoaded:
{
BBitmap* bitmap = nullptr;
msg->FindPointer("bitmap", reinterpret_cast<void**>(&bitmap));
const char* url = msg->GetString("url", "");
int64 generation = msg->GetInt64("generation", -1);
if (generation == static_cast<int64>(fLoadGeneration)
&& fArtworkUrl == url) {
AdoptBitmap(bitmap);
bitmap = nullptr;
}
delete bitmap;
break;
}
case B_COLORS_UPDATED:
Invalidate();
break;
default:
BView::MessageReceived(msg);
break;
}
}
void ArtworkView::GetPreferredSize(float *w, float *h) {
if (w)
*w = 200;
if (h)
*h = 200;
}
void ArtworkView::FrameResized(float width, float height) {
BView::FrameResized(width, height);
_InvalidateScaledBitmap();
}
bool ArtworkView::HasHeightForWidth() {
BSize mn = ExplicitMinSize();
BSize mx = ExplicitMaxSize();
if (mn.width == mx.width && mn.width != B_SIZE_UNSET)
return false;
return true;
}
void ArtworkView::GetHeightForWidth(float width, float* min, float* max, float* pref) {
if (min) *min = width;
if (max) *max = width;
if (pref) *pref = width;
}
BRect ArtworkView::_CoverFrame() const {
BRect bounds = Bounds();
if (!fBitmap || !fBitmap->IsValid() || !bounds.IsValid())
return BRect();
int32 sourceWidth = PixelWidth(fBitmap);
int32 sourceHeight = PixelHeight(fBitmap);
int32 boundsWidth = bounds.IntegerWidth() + 1;
int32 boundsHeight = bounds.IntegerHeight() + 1;
if (sourceWidth <= 0 || sourceHeight <= 0 || boundsWidth <= 0 ||
boundsHeight <= 0)
return BRect();
float scale = std::min(static_cast<float>(boundsWidth) / sourceWidth,
static_cast<float>(boundsHeight) / sourceHeight);
int32 targetWidth =
std::max<int32>(1, static_cast<int32>(std::round(sourceWidth * scale)));
int32 targetHeight =
std::max<int32>(1, static_cast<int32>(std::round(sourceHeight * scale)));
float left = bounds.left + std::floor((boundsWidth - targetWidth) / 2.0f);
float top = bounds.top + std::floor((boundsHeight - targetHeight) / 2.0f);
return BRect(left, top, left + targetWidth - 1, top + targetHeight - 1);
}
void ArtworkView::_InvalidateScaledBitmap() {
delete fScaledBitmap;
fScaledBitmap = nullptr;
fScaledFrame = BRect();
}
void ArtworkView::_UpdateScaledBitmap() {
BRect frame = _CoverFrame();
if (!frame.IsValid())
return;
int32 width = frame.IntegerWidth() + 1;
int32 height = frame.IntegerHeight() + 1;
if (fScaledBitmap && fScaledFrame == frame &&
PixelWidth(fScaledBitmap) == width && PixelHeight(fScaledBitmap) == height)
return;
delete fScaledBitmap;
fScaledBitmap = ScaleBitmapStepwise(fBitmap, width, height);
fScaledFrame = fScaledBitmap ? frame : BRect();
}