-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContentColumnView.cpp
More file actions
1152 lines (998 loc) · 34.3 KB
/
ContentColumnView.cpp
File metadata and controls
1152 lines (998 loc) · 34.3 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ContentColumnView.h"
#include "Entry.h"
#include "MainWindow.h"
#include "Messages.h"
#include "MusicSource.h"
#include "TagSync.h"
#include <Catalog.h>
#include <Font.h>
#include <Handler.h>
#include <Looper.h>
#include <MenuItem.h>
#include <Message.h>
#include <MessageFilter.h>
#include <Path.h>
#include <PopUpMenu.h>
#include <View.h>
#include <Window.h>
#include <cinttypes>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "ContentColumnView"
/**
* @brief Calculate row height based on font for HiDPI scaling.
* @return The calculated row height with 40% padding.
*/
static float CalculateRowHeight() {
font_height fh;
be_plain_font->GetHeight(&fh);
float fontHeight = fh.ascent + fh.descent + fh.leading;
return ceilf(fontHeight * 1.4f);
}
/**
* @class MediaRow
* @brief Custom BRow subclass to store the associated MediaItem.
*/
class MediaRow : public BRow {
public:
explicit MediaRow(const MediaItem &mi)
: BRow(CalculateRowHeight()), fItem(mi) {}
const MediaItem &Item() const { return fItem; }
private:
MediaItem fItem;
};
/**
* @class RightClickFilter
* @brief Message filter for handling mouse events on the content list view.
*
* This filter handles:
* - Right-click: Shows context menu via kMsgShowCtx
* - Left-click on selected row: Initiates drag & drop if mouse moves >16px
*
* The filter checks if the click target is within the owner view hierarchy
* before processing. Modifier keys (Shift, Cmd, Ctrl, Option) disable drag.
*/
class ContentColumnView::RightClickFilter : public BMessageFilter {
public:
explicit RightClickFilter(ContentColumnView *owner)
: BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, B_MOUSE_DOWN),
fOwner(owner) {}
filter_result Filter(BMessage *msg, BHandler **target) override {
if (!fOwner || !msg || msg->what != B_MOUSE_DOWN)
return B_DISPATCH_MESSAGE;
int32 buttons = 0;
if (msg->FindInt32("buttons", &buttons) != B_OK)
return B_DISPATCH_MESSAGE;
BView *v = dynamic_cast<BView *>(*target);
if (!v)
return B_DISPATCH_MESSAGE;
bool inside = false;
for (BView *p = v; p; p = p->Parent()) {
if (p == static_cast<BView *>(fOwner)) {
inside = true;
break;
}
}
if (!inside)
return B_DISPATCH_MESSAGE;
BPoint screenWhere;
if (msg->FindPoint("screen_where", &screenWhere) != B_OK) {
BPoint where;
if (msg->FindPoint("where", &where) != B_OK)
return B_DISPATCH_MESSAGE;
screenWhere = v->ConvertToScreen(where);
}
if (buttons & B_SECONDARY_MOUSE_BUTTON) {
BMessage show(ContentColumnView::kMsgShowCtx);
show.AddPoint("screen_where", screenWhere);
if (fOwner->Looper())
fOwner->Looper()->PostMessage(&show, fOwner);
return B_SKIP_MESSAGE;
}
if (buttons & B_PRIMARY_MOUSE_BUTTON) {
int32 clicks = 1;
msg->FindInt32("clicks", &clicks);
if (clicks >= 2) {
return B_DISPATCH_MESSAGE;
}
int32 modifiers = 0;
if (msg->FindInt32("modifiers", &modifiers) == B_OK) {
if (modifiers &
(B_SHIFT_KEY | B_COMMAND_KEY | B_CONTROL_KEY | B_OPTION_KEY)) {
return B_DISPATCH_MESSAGE;
}
}
BPoint where;
if (msg->FindPoint("where", &where) != B_OK)
return B_DISPATCH_MESSAGE;
BRow *row = fOwner->RowAt(where);
bool isSelected = false;
if (row) {
for (BRow *r = fOwner->CurrentSelection(); r;
r = fOwner->CurrentSelection(r)) {
if (r == row) {
isSelected = true;
break;
}
}
}
if (isSelected) {
BPoint p;
uint32 btns;
v->GetMouse(&p, &btns);
BPoint startP = v->ConvertFromScreen(screenWhere);
while (btns) {
float deltaX = p.x - startP.x;
float deltaY = p.y - startP.y;
if ((deltaX * deltaX + deltaY * deltaY) > 16.0f) {
fOwner->fDragSourceIndex = fOwner->IndexOf(row);
fOwner->InitiateDrag(where, true);
return B_SKIP_MESSAGE;
}
snooze(10000);
v->GetMouse(&p, &btns);
}
}
}
return B_DISPATCH_MESSAGE;
}
private:
ContentColumnView *fOwner;
};
/**
* @class DropFilter
* @brief Message filter for handling internal drag & drop reordering.
*
* Intercepts B_SIMPLE_DATA messages on the ScrollView when fDragSourceIndex
* is set (indicating an internal drag). On drop, sends MSG_REORDER_PLAYLIST
* to perform the actual reordering.
*/
class ContentColumnView::DropFilter : public BMessageFilter {
public:
explicit DropFilter(ContentColumnView *owner)
: BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, B_SIMPLE_DATA),
fOwner(owner) {}
filter_result Filter(BMessage *msg, BHandler **target) override {
if (!fOwner || !msg || msg->what != B_SIMPLE_DATA)
return B_DISPATCH_MESSAGE;
if (fOwner->fDragSourceIndex < 0)
return B_DISPATCH_MESSAGE;
BView *v = dynamic_cast<BView *>(*target);
if (!v)
return B_DISPATCH_MESSAGE;
BPoint dropPoint;
v->GetMouse(&dropPoint, nullptr);
BRow *targetRow = fOwner->RowAt(dropPoint);
int32 targetIndex =
targetRow ? fOwner->IndexOf(targetRow) : fOwner->CountRows() - 1;
int32 sourceIndex = fOwner->fDragSourceIndex;
printf("[DropFilter] Drop detected: source=%ld, target=%ld"
"\n",
(long)sourceIndex, (long)targetIndex);
fflush(stdout);
if (sourceIndex != targetIndex && sourceIndex >= 0 && targetIndex >= 0) {
BMessage reorderMsg(MSG_REORDER_PLAYLIST);
reorderMsg.AddInt32("from_index", sourceIndex);
reorderMsg.AddInt32("to_index", targetIndex);
if (fOwner->Looper()) {
fOwner->Looper()->PostMessage(&reorderMsg);
}
}
fOwner->fDragSourceIndex = -1;
return B_SKIP_MESSAGE;
}
private:
ContentColumnView *fOwner;
};
/**
* @brief Appends indices of all selected rows to a message.
* @param view The content view to query selections from.
* @param into The message to append "index" fields to.
*/
static void AppendSelectedIndices(ContentColumnView *view, BMessage &into) {
for (BRow *r = view->CurrentSelection(); r; r = view->CurrentSelection(r)) {
int32 idx = view->IndexOf(r);
if (idx >= 0)
into.AddInt32("index", idx);
}
}
/**
* @brief Builds a message with file refs for all selected items.
* @param view The content view to query selections from.
* @param filesMsg The message to populate with "refs" entries.
*/
static void BuildFilesMessage(ContentColumnView *view, BMessage &filesMsg) {
filesMsg.MakeEmpty();
filesMsg.what = 0;
for (BRow *r = view->CurrentSelection(); r; r = view->CurrentSelection(r)) {
auto *mr = dynamic_cast<MediaRow *>(r);
if (!mr)
continue;
entry_ref ref;
if (get_ref_for_path(mr->Item().path.String(), &ref) == B_OK)
filesMsg.AddRef("refs", &ref);
}
}
/**
* @class StatusStringField
* @brief BStringField subclass that tracks whether the file is missing.
*
* Used to gray out text for missing files in the list view.
* Also stores the item path for now-playing comparison.
*/
class StatusStringField : public BStringField {
public:
StatusStringField(const char *string, bool missing, const BString &path = "",
SourceType source = SOURCE_TAGS)
: BStringField(string), fMissing(missing), fPath(path), fSource(source) {}
bool IsMissing() const { return fMissing; }
const BString &Path() const { return fPath; }
SourceType Source() const { return fSource; }
private:
bool fMissing;
BString fPath;
SourceType fSource;
};
/**
* @class StatusIntegerField
* @brief BIntegerField subclass that tracks whether the file is missing.
*/
class StatusIntegerField : public BIntegerField {
public:
StatusIntegerField(int32 number, bool missing,
SourceType source = SOURCE_TAGS, const BString &path = "")
: BIntegerField(number), fMissing(missing), fSource(source), fPath(path) {
}
bool IsMissing() const { return fMissing; }
SourceType Source() const { return fSource; }
const BString &Path() const { return fPath; }
private:
bool fMissing;
SourceType fSource;
BString fPath;
};
/**
* @class StatusStringColumn
* @brief Column that renders text in gray if the file is missing,
* and bold if the row is currently playing.
*/
class StatusStringColumn : public BStringColumn {
public:
StatusStringColumn(const char *title, float width, float minWidth,
float maxWidth, uint32 truncate,
const char *attrName = nullptr,
alignment align = B_ALIGN_LEFT,
ContentColumnView *owner = nullptr)
: BStringColumn(title, width, minWidth, maxWidth, truncate, align),
fAttrName(attrName), fOwner(owner), fTitle(title) {}
void SetOwner(ContentColumnView *owner) { fOwner = owner; }
const char *Title() const { return fTitle.String(); }
void DrawField(BField *field, BRect rect, BView *parent) override {
StatusStringField *f = dynamic_cast<StatusStringField *>(field);
rgb_color oldColor = parent->HighColor();
bool isGray = (f && f->IsMissing());
bool isBold = false;
if (f && f->Source() == SOURCE_BFS && !fAttrName.IsEmpty() &&
!f->Path().IsEmpty()) {
BNode node(f->Path().String());
if (node.InitCheck() == B_OK) {
BString val;
if (node.ReadAttrString(fAttrName.String(), &val) == B_OK) {
f->SetString(val.String());
}
}
}
if (f && fOwner && !fOwner->NowPlayingPath().IsEmpty() &&
!f->Path().IsEmpty() && f->Path() == fOwner->NowPlayingPath()) {
isBold = true;
}
BFont oldFont;
if (isBold) {
parent->GetFont(&oldFont);
BFont boldFont = oldFont;
boldFont.SetFace(B_BOLD_FACE);
parent->SetFont(&boldFont);
}
if (isGray) {
parent->SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_DISABLED_LABEL_TINT));
}
if (field)
BStringColumn::DrawField(field, rect, parent);
parent->SetHighColor(oldColor);
if (isBold) {
parent->SetFont(&oldFont);
}
}
private:
BString fAttrName;
ContentColumnView *fOwner = nullptr;
BString fTitle;
};
/**
* @class StatusIntegerColumn
* @brief Column that renders integers in gray if the file is missing.
*/
class StatusIntegerColumn : public BIntegerColumn {
public:
StatusIntegerColumn(const char *title, float width, float minWidth,
float maxWidth, const char *attrName = nullptr,
alignment align = B_ALIGN_LEFT)
: BIntegerColumn(title, width, minWidth, maxWidth, align),
fAttrName(attrName), fTitle(title) {}
const char *Title() const { return fTitle.String(); }
void DrawField(BField *field, BRect rect, BView *parent) override {
StatusIntegerField *f = dynamic_cast<StatusIntegerField *>(field);
rgb_color oldColor = parent->HighColor();
bool isGray = (f && f->IsMissing());
if (f && f->Source() == SOURCE_BFS && !fAttrName.IsEmpty() &&
!f->Path().IsEmpty()) {
BNode node(f->Path().String());
if (node.InitCheck() == B_OK) {
int32 val = 0;
if (node.ReadAttr(fAttrName.String(), B_INT32_TYPE, 0, &val,
sizeof(int32)) > 0) {
f->SetValue(val);
}
}
}
if (isGray) {
parent->SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_DISABLED_LABEL_TINT));
}
if (field)
BIntegerColumn::DrawField(field, rect, parent);
parent->SetHighColor(oldColor);
}
private:
BString fAttrName;
BString fTitle;
};
/**
* @class RatingColumn
* @brief Column that renders rating stars.
*/
class RatingColumn : public BStringColumn {
public:
RatingColumn(const char *title, float width, float minWidth, float maxWidth)
: BStringColumn(title, width, minWidth, maxWidth, B_TRUNCATE_END,
B_ALIGN_LEFT),
fTitle(title) {}
const char *Title() const { return fTitle.String(); }
/**
* @brief Converts a numeric rating (0-10) to a star string representation.
* @param rating The rating value from 0 to 10.
* @return A BString containing the star representation.
*/
static BString RatingToStars(int32 rating) {
if (rating < 0)
rating = 0;
if (rating > 10)
rating = 10;
int fullStars = rating / 2;
bool halfStar = (rating % 2) == 1;
int emptyStars = 5 - fullStars - (halfStar ? 1 : 0);
BString result;
for (int i = 0; i < fullStars; i++) {
result << "★";
}
if (halfStar) {
result << "⯪";
}
for (int i = 0; i < emptyStars; i++) {
result << "☆";
}
return result;
}
void DrawField(BField *field, BRect rect, BView *parent) override {
StatusStringField *sf = dynamic_cast<StatusStringField *>(field);
if (!sf)
return;
if (sf && !sf->Path().IsEmpty()) {
BNode node(sf->Path().String());
if (node.InitCheck() == B_OK) {
int32 rating = 0;
if (node.ReadAttr("Media:Rating", B_INT32_TYPE, 0, &rating,
sizeof(int32)) > 0) {
sf->SetString(RatingToStars(rating).String());
}
}
}
BFont oldFont;
parent->GetFont(&oldFont);
BStringColumn::DrawField(field, rect, parent);
parent->SetFont(&oldFont);
}
private:
BString fTitle;
};
/**
* @brief Constructor for the ContentColumnView.
* @param name The name of the view.
*/
ContentColumnView::ContentColumnView(const char *name)
: BColumnListView(name, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE) {
SetSelectionMode(B_MULTIPLE_SELECTION_LIST);
SetColor(B_COLOR_BACKGROUND, ui_color(B_LIST_BACKGROUND_COLOR));
SetColor(B_COLOR_TEXT, ui_color(B_LIST_ITEM_TEXT_COLOR));
SetColor(B_COLOR_SELECTION, ui_color(B_LIST_SELECTED_BACKGROUND_COLOR));
SetColor(B_COLOR_SELECTION_TEXT, ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
SetColor(B_COLOR_ROW_DIVIDER, B_TRANSPARENT_COLOR);
SetColor(B_COLOR_HEADER_BACKGROUND, ui_color(B_PANEL_BACKGROUND_COLOR));
SetColor(B_COLOR_HEADER_TEXT, ui_color(B_PANEL_TEXT_COLOR));
AddColumn(new StatusStringColumn(B_TRANSLATE("Title"), 200, 50, 500,
B_TRUNCATE_END, "Media:Title"),
0);
AddColumn(new StatusStringColumn(B_TRANSLATE("Artist"), 150, 50, 300,
B_TRUNCATE_END, "Audio:Artist"),
1);
AddColumn(new StatusStringColumn(B_TRANSLATE("Album"), 150, 50, 300,
B_TRUNCATE_END, "Audio:Album"),
2);
AddColumn(new StatusStringColumn(B_TRANSLATE("Album Artist"), 150, 50, 300,
B_TRUNCATE_END, "Media:AlbumArtist"),
3);
AddColumn(new StatusStringColumn(B_TRANSLATE("Genre"), 100, 30, 200,
B_TRUNCATE_END, "Media:Genre"),
4);
AddColumn(new StatusStringColumn(B_TRANSLATE("Year"), 60, 30, 80,
B_TRUNCATE_END, "Media:Year", B_ALIGN_RIGHT),
5);
AddColumn(new StatusStringColumn(B_TRANSLATE("Duration"), 60, 30, 80,
B_TRUNCATE_END, "Media:Length",
B_ALIGN_RIGHT),
6);
AddColumn(new StatusIntegerColumn(B_TRANSLATE("Track"), 50, 20, 80,
"Audio:Track", B_ALIGN_RIGHT),
7);
AddColumn(new StatusIntegerColumn(B_TRANSLATE("Disc"), 50, 20, 80,
"Media:Disc", B_ALIGN_RIGHT),
8);
AddColumn(new StatusIntegerColumn(B_TRANSLATE("Bitrate"), 80, 50, 100,
"Audio:Bitrate", B_ALIGN_RIGHT),
9);
AddColumn(new StatusStringColumn(B_TRANSLATE("Path"), 300, 100, 1000,
B_TRUNCATE_END),
10);
AddColumn(new RatingColumn(B_TRANSLATE("Rating"), 80, 60, 100), 11);
SetInvocationMessage(new BMessage(MSG_PLAY));
SetSelectionMessage(new BMessage(MSG_SELECTION_CHANGED_CONTENT));
for (int32 i = 0; i < CountColumns(); ++i) {
if (auto *col = dynamic_cast<StatusStringColumn *>(ColumnAt(i))) {
col->SetOwner(this);
}
}
}
/**
* @brief Sets the path of the currently playing media item.
* @param path The file path of the currently playing item.
*
* This triggers a redraw of the relevant rows to update the bold state.
*/
void ContentColumnView::SetNowPlayingPath(const BString &path) {
if (fNowPlayingPath != path) {
BString oldPath = fNowPlayingPath;
fNowPlayingPath = path;
for (int32 i = 0; i < CountRows(); ++i) {
MediaRow *mr = dynamic_cast<MediaRow *>(RowAt(i));
if (mr) {
if (mr->Item().path == oldPath || mr->Item().path == path) {
InvalidateRow(mr);
}
}
}
}
}
/**
* @brief Destructor.
*/
ContentColumnView::~ContentColumnView() {}
/**
* @brief Adds a single media item to the list view.
* @param mi The media item to add.
*/
void ContentColumnView::AddEntry(const MediaItem &mi) {
MediaRow *row = new MediaRow(mi);
bool m = mi.missing;
SourceType src = SOURCE_TAGS;
if (!mi.path.IsEmpty()) {
MusicSource ms = MusicSource::GetSourceForPath(mi.path);
src = ms.primary;
}
row->SetField(new StatusStringField(mi.title, m, mi.path, src), 0);
row->SetField(new StatusStringField(mi.artist, m, mi.path, src), 1);
row->SetField(new StatusStringField(mi.album, m, mi.path, src), 2);
row->SetField(new StatusStringField(mi.albumArtist, m, mi.path, src), 3);
row->SetField(new StatusStringField(mi.genre, m, mi.path, src), 4);
BString yearStr;
yearStr << mi.year;
row->SetField(new StatusStringField(yearStr, m, mi.path, src), 5);
BString durStr;
int32 min = mi.duration / 60;
int32 sec = mi.duration % 60;
durStr.SetToFormat("%ld:%02ld", (long)min, (long)sec);
row->SetField(new StatusStringField(durStr, m, mi.path, src), 6);
row->SetField(new StatusIntegerField(mi.track, m, src, mi.path), 7);
row->SetField(new StatusIntegerField(mi.disc, m, src, mi.path), 8);
row->SetField(new StatusIntegerField(mi.bitrate, m, src, mi.path), 9);
row->SetField(new StatusStringField(mi.path, m, mi.path, src), 10);
row->SetField(new StatusStringField(RatingColumn::RatingToStars(mi.rating), m,
mi.path, src),
11);
AddRow(row);
}
/**
* @brief Adds multiple media items to the list view in batches.
* @param items The vector of media items to add.
*/
void ContentColumnView::AddEntries(const std::vector<MediaItem> &items) {
fPendingItems = items;
fPendingIndex = 0;
_AddBatch(50);
}
/**
* @brief Adds a batch of pending items to the list view.
* @param count The number of items to add in this batch.
*/
void ContentColumnView::_AddBatch(size_t count) {
if (fPendingIndex >= fPendingItems.size())
return;
bool bulk = (count > 100);
BWindow *win = Window();
if (bulk && win)
win->DisableUpdates();
SetSortingEnabled(false);
size_t end = fPendingIndex + count;
if (end > fPendingItems.size())
end = fPendingItems.size();
for (size_t i = fPendingIndex; i < end; ++i) {
AddEntry(fPendingItems[i]);
}
fPendingIndex = end;
SetSortingEnabled(true);
if (bulk && win)
win->EnableUpdates();
if (fPendingIndex < fPendingItems.size()) {
if (Looper())
Looper()->PostMessage(kMsgChunkAdd, this);
} else {
if (Looper())
Looper()->PostMessage(MSG_COUNT_UPDATED);
}
}
/**
* @brief Clears all entries from the list view.
*/
void ContentColumnView::ClearEntries() {
Clear();
RefreshScrollbars();
}
/**
* @brief Refreshes the scrollbars by invalidating the layout.
*/
void ContentColumnView::RefreshScrollbars() { InvalidateLayout(); }
/**
* @brief Initiates a drag operation for selected items.
* @param point The point where the drag started.
* @param wasSelected Whether the item at the point was selected.
* @return True if drag was initiated, false otherwise.
*/
bool ContentColumnView::InitiateDrag(BPoint point, bool wasSelected) {
BMessage dragMsg(B_SIMPLE_DATA);
BRow *firstSelected = CurrentSelection();
if (firstSelected) {
fDragSourceIndex = IndexOf(firstSelected);
dragMsg.AddInt32("source_index", fDragSourceIndex);
} else {
fDragSourceIndex = -1;
}
MediaRow *row = nullptr;
while ((row = dynamic_cast<MediaRow *>(CurrentSelection(row))) != nullptr) {
const MediaItem &mi = row->Item();
entry_ref ref;
if (get_ref_for_path(mi.path.String(), &ref) == B_OK) {
dragMsg.AddRef("refs", &ref);
}
}
if (dragMsg.HasRef("refs")) {
BRow *firstRow = RowAt(point);
if (firstRow) {
BRect dragRect;
GetRowRect(firstRow, &dragRect);
DragMessage(&dragMsg, dragRect, this);
} else {
DragMessage(&dragMsg, Bounds(), this);
}
return true;
}
fDragSourceIndex = -1;
return false;
}
/**
* @brief Handles key down events.
* @param bytes The raw key data.
* @param numBytes The number of bytes in the key data.
*
* Handles deletion of items and moving items up/down with Option+Arrow keys.
*/
void ContentColumnView::KeyDown(const char *bytes, int32 numBytes) {
if (numBytes == 1 && bytes[0] == B_DELETE) {
BMessage msg(MSG_DELETE_ITEM);
Looper()->PostMessage(&msg);
return;
}
if (numBytes == 1) {
uint32 modifiers = 0;
BMessage *currentMsg = Window() ? Window()->CurrentMessage() : nullptr;
if (currentMsg)
currentMsg->FindInt32("modifiers", (int32 *)&modifiers);
if (modifiers & B_OPTION_KEY) {
if (bytes[0] == B_UP_ARROW) {
BMessage msg(MSG_MOVE_UP);
BRow *row = CurrentSelection();
if (row) {
msg.AddInt32("index", IndexOf(row));
Looper()->PostMessage(&msg);
}
return;
} else if (bytes[0] == B_DOWN_ARROW) {
BMessage msg(MSG_MOVE_DOWN);
BRow *row = CurrentSelection();
if (row) {
msg.AddInt32("index", IndexOf(row));
Looper()->PostMessage(&msg);
}
return;
}
}
}
BColumnListView::KeyDown(bytes, numBytes);
}
/**
* @brief Handles mouse movement events.
* @param where The point where the mouse is.
* @param transit The transit code (entered, exited, inside).
* @param dragMsg The drag message, if any.
*/
void ContentColumnView::MouseMoved(BPoint where, uint32 transit,
const BMessage *dragMsg) {
if (fDragSourceIndex >= 0 && dragMsg && dragMsg->what == B_SIMPLE_DATA) {
fLastDropPoint = where;
}
BColumnListView::MouseMoved(where, transit, dragMsg);
}
/**
* @brief Called when the view is attached to a window.
*
* Adds the RightClickFilter and DropFilter to the scroll view.
*/
void ContentColumnView::AttachedToWindow() {
BColumnListView::AttachedToWindow();
if (BView *outline = ScrollView()) {
outline->AddFilter(new RightClickFilter(this));
outline->AddFilter(new DropFilter(this));
outline->SetViewColor(B_TRANSPARENT_COLOR);
}
}
/**
* @brief Called when the view is detached from a window.
*/
void ContentColumnView::DetachedFromWindow() {
BColumnListView::DetachedFromWindow();
}
/**
* @brief Handles received messages.
* @param msg The message to handle.
*
* Handles context menu, chunk addition, color updates, and drag & drop
* reordering.
*/
void ContentColumnView::MessageReceived(BMessage *msg) {
switch (msg->what) {
case kMsgShowCtx: {
BPoint screen;
if (msg->FindPoint("screen_where", &screen) != B_OK)
break;
BPoint where = screen;
if (BView *outline = ScrollView()) {
outline->ConvertFromScreen(&where);
} else {
ConvertFromScreen(&where);
}
BRow *row = RowAt(where);
if (!row)
break;
bool rowAlreadySelected = false;
for (BRow *r = CurrentSelection(); r; r = CurrentSelection(r)) {
if (r == row) {
rowAlreadySelected = true;
break;
}
}
if (!rowAlreadySelected) {
if (SelectionMode() != B_MULTIPLE_SELECTION_LIST)
DeselectAll();
AddToSelection(row);
}
BPopUpMenu menu("content-ctx", false, false);
menu.AddItem(new BMenuItem(B_TRANSLATE("Play"), new BMessage(MSG_PLAY)));
BMenu *addSub = new BMenu(B_TRANSLATE("Add to Playlist"));
{
BMessage *m = new BMessage(MSG_NEW_PLAYLIST);
BMessage files;
BuildFilesMessage(this, files);
if (files.HasRef("refs"))
m->AddMessage("files", &files);
addSub->AddItem(new BMenuItem(B_TRANSLATE("New Playlist..."), m));
}
addSub->AddSeparatorItem();
BMessage reply;
if (auto *mw = dynamic_cast<MainWindow *>(Window())) {
mw->GetPlaylistNames(reply, true);
}
int32 count = 0;
reply.GetInfo("name", nullptr, &count);
if (count == 0) {
auto *none = new BMenuItem(B_TRANSLATE("<no playlists>"), nullptr);
none->SetEnabled(false);
addSub->AddItem(none);
} else {
for (int32 i = 0; i < count; ++i) {
const char *pname = nullptr;
if (reply.FindString("name", i, &pname) == B_OK && pname) {
BMessage *m = new BMessage(MSG_ADD_TO_PLAYLIST);
AppendSelectedIndices(this, *m);
m->AddString("playlist", pname);
addSub->AddItem(new BMenuItem(pname, m));
}
}
}
menu.AddItem(addSub);
// menu.AddSeparatorItem(); //maybe looks better without it?
BMenu *ratingMenu = new BMenu(B_TRANSLATE("Rating"));
BMessage filesMsg;
BuildFilesMessage(this, filesMsg);
for (int32 i = 0; i <= 10; ++i) {
BString label = RatingColumn::RatingToStars(i);
BMessage *msg = new BMessage(MSG_SET_RATING);
msg->AddInt32("rating", i);
if (filesMsg.HasRef("refs")) {
msg->AddMessage("files", &filesMsg);
}
ratingMenu->AddItem(new BMenuItem(label.String(), msg));
}
menu.AddItem(ratingMenu);
menu.AddSeparatorItem();
{
BMessage *m = new BMessage(MSG_REVEAL_IN_TRACKER);
BMessage files;
BuildFilesMessage(this, files);
if (files.HasRef("refs"))
m->AddMessage("files", &files);
menu.AddItem(new BMenuItem(B_TRANSLATE("Show in Tracker"), m));
}
bool inPlaylist = false;
if (auto *mw = dynamic_cast<MainWindow *>(Window())) {
inPlaylist = mw->IsPlaylistSelected();
}
if (inPlaylist) {
menu.AddSeparatorItem();
{
BMessage *m = new BMessage(MSG_MOVE_UP);
BRow *row = CurrentSelection();
if (row)
m->AddInt32("index", IndexOf(row));
menu.AddItem(new BMenuItem(B_TRANSLATE("Move Up"), m));
}
{
BMessage *m = new BMessage(MSG_MOVE_DOWN);
BRow *row = CurrentSelection();
if (row)
m->AddInt32("index", IndexOf(row));
menu.AddItem(new BMenuItem(B_TRANSLATE("Move Down"), m));
}
menu.AddItem(new BMenuItem(B_TRANSLATE("Remove from Playlist"),
new BMessage(MSG_DELETE_ITEM)));
}
menu.AddSeparatorItem();
menu.AddItem(new BMenuItem(B_TRANSLATE("Properties..."),
new BMessage(MSG_PROPERTIES)));
if (BMenuItem *chosen =
menu.Go(screen, true, false, BRect(screen, screen), false)) {
if (Looper())
Looper()->PostMessage(chosen->Message(), this);
}
break;
}
case kMsgChunkAdd:
_AddBatch(200);
break;
case B_COLORS_UPDATED: {
SetColor(B_COLOR_BACKGROUND, ui_color(B_LIST_BACKGROUND_COLOR));
SetColor(B_COLOR_TEXT, ui_color(B_LIST_ITEM_TEXT_COLOR));
SetColor(B_COLOR_SELECTION, ui_color(B_LIST_SELECTED_BACKGROUND_COLOR));
SetColor(B_COLOR_ROW_DIVIDER, ui_color(B_LIST_BACKGROUND_COLOR));
SetColor(B_COLOR_HEADER_BACKGROUND, ui_color(B_PANEL_BACKGROUND_COLOR));
SetColor(B_COLOR_HEADER_TEXT, ui_color(B_PANEL_TEXT_COLOR));
Invalidate();
break;
}
case B_SIMPLE_DATA: {
printf("[ContentColumnView] B_SIMPLE_DATA received, fDragSourceIndex=%ld"
"\n",
(long)fDragSourceIndex);
printf("[ContentColumnView] fLastDropPoint=(%f,%f)\n", fLastDropPoint.x,
fLastDropPoint.y);
fflush(stdout);
if (fDragSourceIndex < 0) {
printf("[ContentColumnView] Not internal drag, forwarding\n");
fflush(stdout);
BColumnListView::MessageReceived(msg);
break;
}
int32 sourceIndex = fDragSourceIndex;
fDragSourceIndex = -1;
BRow *targetRow = RowAt(fLastDropPoint);
int32 targetIndex = targetRow ? IndexOf(targetRow) : CountRows() - 1;
printf("[ContentColumnView] sourceIndex=%ld, targetIndex=%ld"
"\n",
(long)sourceIndex, (long)targetIndex);
fflush(stdout);
if (sourceIndex == targetIndex || sourceIndex < 0 || targetIndex < 0)
break;
printf("[ContentColumnView] Sending MSG_REORDER_PLAYLIST\n");
fflush(stdout);
BMessage reorderMsg(MSG_REORDER_PLAYLIST);
reorderMsg.AddInt32("from_index", sourceIndex);
reorderMsg.AddInt32("to_index", targetIndex);
if (Looper())
Looper()->PostMessage(&reorderMsg);
break;
}
default:
BColumnListView::MessageReceived(msg);
}
}
/**
* @brief Returns the internal MediaItem pointer for the currently selected row.
* @return Pointer to the MediaItem of the selected row, or nullptr if none
* selected.
*/
const MediaItem *ContentColumnView::SelectedItem() const {