-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
PackageInfoView.cpp
1059 lines (847 loc) · 23.5 KB
/
PackageInfoView.cpp
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
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "PackageInfoView.h"
#include <algorithm>
#include <stdio.h>
#include <Bitmap.h>
#include <Button.h>
#include <CardLayout.h>
#include <Catalog.h>
#include <Font.h>
#include <LayoutBuilder.h>
#include <LayoutUtils.h>
#include <Message.h>
#include <TabView.h>
#include <ScrollView.h>
#include <SpaceLayoutItem.h>
#include <StringView.h>
#include "PackageManager.h"
#include "TextView.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "PackageInfoView"
static const rgb_color kLightBlack = (rgb_color){ 60, 60, 60, 255 };
static const float kContentTint = (B_NO_TINT + B_LIGHTEN_1_TINT) / 2.0f;
class BitmapView : public BView {
public:
BitmapView(const char* name)
:
BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
fBitmap(NULL)
{
SetViewColor(B_TRANSPARENT_COLOR);
SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetDrawingMode(B_OP_OVER);
}
virtual ~BitmapView()
{
}
virtual void AttachedToWindow()
{
BView* parent = Parent();
if (parent != NULL)
SetLowColor(parent->ViewColor());
}
virtual void Draw(BRect updateRect)
{
BRect bounds(Bounds());
FillRect(updateRect, B_SOLID_LOW);
if (fBitmap == NULL)
return;
BRect bitmapBounds = fBitmap->Bounds();
if (bitmapBounds.Width() <= 0.0f || bitmapBounds.Height() <= 0.0f)
return;
float hScale = bounds.Width() / bitmapBounds.Width();
float vScale = bounds.Height() / bitmapBounds.Height();
float scale = std::min(hScale, vScale);
float width = bitmapBounds.Width() * scale;
float height = bitmapBounds.Height() * scale;
switch (LayoutAlignment().horizontal) {
case B_ALIGN_LEFT:
break;
case B_ALIGN_RIGHT:
bounds.left = floorf(bounds.right - width);
break;
default:
case B_ALIGN_HORIZONTAL_CENTER:
bounds.left = floorf(bounds.left
+ (bounds.Width() - width) / 2.0f);
break;
}
switch (LayoutAlignment().vertical) {
case B_ALIGN_TOP:
break;
case B_ALIGN_BOTTOM:
bounds.top = floorf(bounds.bottom - height);
break;
default:
case B_ALIGN_VERTICAL_CENTER:
bounds.top = floorf(bounds.top
+ (bounds.Height() - height) / 2.0f);
break;
}
bounds.right = ceilf(bounds.left + width);
bounds.bottom = ceilf(bounds.top + height);
DrawBitmap(fBitmap, bitmapBounds, bounds, B_FILTER_BITMAP_BILINEAR);
}
virtual BSize MinSize()
{
BSize size(0.0f, 0.0f);
if (fBitmap != NULL) {
BRect bounds = fBitmap->Bounds();
size.width = bounds.Width();
size.height = bounds.Height();
}
return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
}
virtual BSize PreferredSize()
{
BSize size = MinSize();
return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), size);
}
virtual BSize MaxSize()
{
BSize size = MinSize();
return BLayoutUtils::ComposeSize(ExplicitMaxSize(), size);
}
// virtual bool HasHeightForWidth()
// {
// return true;
// }
//
// virtual void GetHeightForWidth(float width, float* min, float* max,
// float* preferred)
// {
// float height = width;
//
// if (fBitmap != NULL) {
// BRect bounds = fBitmap->Bounds();
// if (bounds.Width() > 0.0f && bounds.Height() > 0.0f)
// height = (width / bounds.Width()) * bounds.Height();
// }
//
// if (min != NULL)
// *min = height;
// if (max != NULL)
// *max = height;
// if (preferred != NULL)
// *preferred = height;
// }
void SetBitmap(const BBitmap* bitmap)
{
if (bitmap == fBitmap)
return;
BSize size = MinSize();
fBitmap = bitmap;
BSize newSize = MinSize();
if (size != newSize)
InvalidateLayout();
Invalidate();
}
private:
const BBitmap* fBitmap;
};
//! Layouts the scrollbar so it looks nice with no border and the document
// window look.
class CustomScrollView : public BScrollView {
public:
CustomScrollView(const char* name, BView* target)
:
BScrollView(name, target, 0, false, true, B_NO_BORDER)
{
}
virtual void DoLayout()
{
BRect innerFrame = Bounds();
innerFrame.right -= B_V_SCROLL_BAR_WIDTH + 1;
BView* target = Target();
if (target != NULL) {
Target()->MoveTo(innerFrame.left, innerFrame.top);
Target()->ResizeTo(innerFrame.Width(), innerFrame.Height());
}
BScrollBar* scrollBar = ScrollBar(B_VERTICAL);
if (scrollBar != NULL) {
BRect rect = innerFrame;
rect.left = rect.right + 1;
rect.right = rect.left + B_V_SCROLL_BAR_WIDTH;
rect.bottom -= B_H_SCROLL_BAR_HEIGHT;
scrollBar->MoveTo(rect.left, rect.top);
scrollBar->ResizeTo(rect.Width(), rect.Height());
}
}
};
class RatingsScrollView : public CustomScrollView {
public:
RatingsScrollView(const char* name, BView* target)
:
CustomScrollView(name, target)
{
}
virtual void DoLayout()
{
CustomScrollView::DoLayout();
BScrollBar* scrollBar = ScrollBar(B_VERTICAL);
BView* target = Target();
if (target != NULL && scrollBar != NULL) {
// Set the scroll steps
BView* item = target->ChildAt(0);
if (item != NULL) {
scrollBar->SetSteps(item->MinSize().height + 1,
item->MinSize().height + 1);
}
}
}
};
// #pragma mark - AboutView
class RatingView : public BView {
public:
RatingView()
:
BView("package rating view", B_WILL_DRAW),
fStarBitmap(501),
fRating(-1.0f)
{
SetViewColor(B_TRANSPARENT_COLOR);
SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
}
virtual ~RatingView()
{
}
virtual void AttachedToWindow()
{
BView* parent = Parent();
if (parent != NULL)
SetLowColor(parent->ViewColor());
}
virtual void Draw(BRect updateRect)
{
FillRect(updateRect, B_SOLID_LOW);
if (fRating < 0.0f)
return;
const BBitmap* star = fStarBitmap.Bitmap(SharedBitmap::SIZE_16);
if (star == NULL) {
fprintf(stderr, "No star icon found in application resources.\n");
return;
}
SetDrawingMode(B_OP_OVER);
float x = 0;
for (int i = 0; i < 5; i++) {
DrawBitmap(star, BPoint(x, 0));
x += 16 + 2;
}
if (fRating >= 5.0f)
return;
SetDrawingMode(B_OP_OVER);
BRect rect(Bounds());
rect.left = ceilf(rect.left + (fRating / 5.0f) * rect.Width());
rgb_color color = LowColor();
color.alpha = 190;
SetHighColor(color);
SetDrawingMode(B_OP_ALPHA);
FillRect(rect, B_SOLID_HIGH);
}
virtual BSize MinSize()
{
return BSize(16 * 5 + 2 * 4, 16 + 2);
}
virtual BSize PreferredSize()
{
return MinSize();
}
virtual BSize MaxSize()
{
return MinSize();
}
void SetRating(float rating)
{
fRating = rating;
Invalidate();
}
private:
SharedBitmap fStarBitmap;
float fRating;
};
// #pragma mark - TitleView
enum {
MSG_PACKAGE_ACTION = 'pkga',
};
class TitleView : public BGroupView {
public:
TitleView()
:
BGroupView("title view", B_HORIZONTAL)
{
fIconView = new BitmapView("package icon view");
fTitleView = new BStringView("package title view", "");
fPublisherView = new BStringView("package publisher view", "");
// Title font
BFont font;
GetFont(&font);
font_family family;
font_style style;
font.SetSize(ceilf(font.Size() * 1.5f));
font.GetFamilyAndStyle(&family, &style);
font.SetFamilyAndStyle(family, "Bold");
fTitleView->SetFont(&font);
// Publisher font
GetFont(&font);
font.SetSize(std::max(9.0f, floorf(font.Size() * 0.92f)));
font.SetFamilyAndStyle(family, "Italic");
fPublisherView->SetFont(&font);
fPublisherView->SetHighColor(kLightBlack);
fPublisherView->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT,
B_ALIGN_VERTICAL_UNSET));
// slightly bigger font
GetFont(&font);
font.SetSize(ceilf(font.Size() * 1.2f));
// Version info
fVersionInfo = new BStringView("package version info", "");
fVersionInfo->SetFont(&font);
fVersionInfo->SetHighColor(kLightBlack);
// Rating view
fRatingView = new RatingView();
fAvgRating = new BStringView("package average rating", "");
fAvgRating->SetFont(&font);
fAvgRating->SetHighColor(kLightBlack);
fVoteInfo = new BStringView("package vote info", "");
// small font
GetFont(&font);
font.SetSize(std::max(9.0f, floorf(font.Size() * 0.85f)));
fVoteInfo->SetFont(&font);
fVoteInfo->SetHighColor(kLightBlack);
BLayoutBuilder::Group<>(this)
.Add(fIconView)
.AddGroup(B_VERTICAL, 1.0f)
.Add(fTitleView)
.Add(fPublisherView)
.End()
.AddGlue(0.1f)
.AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING)
.Add(fRatingView)
.Add(fAvgRating)
.Add(fVoteInfo)
.End()
.AddGlue(0.2f)
.Add(fVersionInfo)
.AddGlue(3.0f)
;
Clear();
}
virtual ~TitleView()
{
}
void SetPackage(const PackageInfo& package)
{
if (package.Icon().Get() != NULL)
fIconView->SetBitmap(package.Icon()->Bitmap(SharedBitmap::SIZE_32));
else
fIconView->SetBitmap(NULL);
fTitleView->SetText(package.Title());
BString publisher = B_TRANSLATE("by %Publisher%");
publisher.ReplaceAll("%Publisher%", package.Publisher().Name());
fPublisherView->SetText(publisher);
BString version = B_TRANSLATE("%Version%");
version.ReplaceAll("%Version%", package.Version());
fVersionInfo->SetText(version);
RatingSummary ratingSummary = package.CalculateRatingSummary();
fRatingView->SetRating(ratingSummary.averageRating);
BString avgRating;
avgRating.SetToFormat("%.1f", ratingSummary.averageRating);
fAvgRating->SetText(avgRating);
BString votes;
votes.SetToFormat("%d", ratingSummary.ratingCount);
BString voteInfo(B_TRANSLATE("(%Votes%)"));
voteInfo.ReplaceAll("%Votes%", votes);
fVoteInfo->SetText(voteInfo);
InvalidateLayout();
Invalidate();
}
void Clear()
{
fIconView->SetBitmap(NULL);
fTitleView->SetText("");
fPublisherView->SetText("");
fVersionInfo->SetText("");
fRatingView->SetRating(-1.0f);
fAvgRating->SetText("");
fVoteInfo->SetText("");
}
private:
BitmapView* fIconView;
BStringView* fTitleView;
BStringView* fPublisherView;
BStringView* fVersionInfo;
RatingView* fRatingView;
BStringView* fAvgRating;
BStringView* fVoteInfo;
};
// #pragma mark - PackageActionView
class PackageActionView : public BView {
public:
PackageActionView(PackageManager* packageManager)
:
BView("about view", B_WILL_DRAW),
fPackageManager(packageManager),
fLayout(new BGroupLayout(B_HORIZONTAL))
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetLayout(fLayout);
}
virtual ~PackageActionView()
{
Clear();
}
virtual void MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_PACKAGE_ACTION:
{
int32 index;
if (message->FindInt32("index", &index) == B_OK) {
const PackageActionRef& action
= fPackageActions.ItemAt(index);
if (action.Get() != NULL) {
status_t result = action->Perform();
if (result != B_OK) {
fprintf(stderr, "Package action failed: %s '%s'\n",
action->Label(),
action->Package().Title().String());
}
}
}
break;
}
default:
BView::MessageReceived(message);
break;
}
}
void SetPackage(const PackageInfo& package)
{
Clear();
fPackageActions = fPackageManager->GetPackageActions(package);
// Add Buttons in reverse action order
for (int32 i = fPackageActions.CountItems() - 1; i >= 0; i--) {
const PackageActionRef& action = fPackageActions.ItemAtFast(i);
BMessage* message = new BMessage(MSG_PACKAGE_ACTION);
message->AddInt32("index", i);
BButton* button = new BButton(action->Label(), message);
fLayout->AddView(button);
button->SetTarget(this);
fButtons.AddItem(button);
}
}
void Clear()
{
for (int32 i = fButtons.CountItems() - 1; i >= 0; i--) {
BButton* button = (BButton*)fButtons.ItemAtFast(i);
button->RemoveSelf();
delete button;
}
fButtons.MakeEmpty();
}
private:
PackageManager* fPackageManager;
BGroupLayout* fLayout;
PackageActionList fPackageActions;
BList fButtons;
};
// #pragma mark - AboutView
class AboutView : public BView {
public:
AboutView()
:
BView("about view", 0),
fEmailIcon("text/x-email"),
fWebsiteIcon("text/html")
{
SetViewColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
kContentTint));
fDescriptionView = new BTextView("description view");
fDescriptionView->SetViewColor(ViewColor());
fDescriptionView->MakeEditable(false);
const float textInset = be_plain_font->Size();
fDescriptionView->SetInsets(textInset, textInset, textInset, 0.0f);
BScrollView* scrollView = new CustomScrollView(
"description scroll view", fDescriptionView);
BFont smallFont;
GetFont(&smallFont);
smallFont.SetSize(std::max(9.0f, ceilf(smallFont.Size() * 0.85f)));
// TODO: Clicking the screen shot view should open ShowImage with the
// the screen shot. This could be done by writing the screen shot to
// a temporary folder, launching ShowImage to display it, and writing
// all other screenshots associated with the package to the same folder
// so the user can use the ShowImage navigation to view the other
// screenshots.
fScreenshotView = new BitmapView("screenshot view");
fScreenshotView->SetExplicitMinSize(BSize(64.0f, 64.0f));
fScreenshotView->SetExplicitMaxSize(
BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED));
fScreenshotView->SetExplicitAlignment(
BAlignment(B_ALIGN_CENTER, B_ALIGN_TOP));
fEmailIconView = new BitmapView("email icon view");
fEmailLinkView = new BStringView("email link view", "");
fEmailLinkView->SetFont(&smallFont);
fEmailLinkView->SetHighColor(kLightBlack);
fWebsiteIconView = new BitmapView("website icon view");
fWebsiteLinkView = new BStringView("website link view", "");
fWebsiteLinkView->SetFont(&smallFont);
fWebsiteLinkView->SetHighColor(kLightBlack);
BGroupView* leftGroup = new BGroupView(B_VERTICAL,
B_USE_DEFAULT_SPACING);
leftGroup->SetViewColor(ViewColor());
BLayoutBuilder::Group<>(this, B_HORIZONTAL, 0.0f)
// .Add(BSpaceLayoutItem::CreateHorizontalStrut(32.0f))
.AddGroup(leftGroup)
.Add(fScreenshotView)
.AddGroup(B_HORIZONTAL)
// .AddGlue()
.AddGrid(B_USE_HALF_ITEM_SPACING, B_USE_HALF_ITEM_SPACING)
.Add(fEmailIconView, 0, 0)
.Add(fEmailLinkView, 1, 0)
.Add(fWebsiteIconView, 0, 1)
.Add(fWebsiteLinkView, 1, 1)
.End()
.End()
.SetInsets(B_USE_DEFAULT_SPACING)
.End()
.Add(scrollView, 1.0f)
.SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNLIMITED))
.SetInsets(0.0f, -1.0f, -1.0f, -1.0f)
;
}
virtual ~AboutView()
{
Clear();
}
void SetPackage(const PackageInfo& package)
{
fDescriptionView->SetText(package.ShortDescription());
fDescriptionView->Insert(fDescriptionView->TextLength(), "\n\n", 2);
fDescriptionView->Insert(fDescriptionView->TextLength(),
package.FullDescription(), package.FullDescription().Length());
fEmailIconView->SetBitmap(fEmailIcon.Bitmap(SharedBitmap::SIZE_16));
fEmailLinkView->SetText(package.Publisher().Email());
fWebsiteIconView->SetBitmap(fWebsiteIcon.Bitmap(SharedBitmap::SIZE_16));
fWebsiteLinkView->SetText(package.Publisher().Website());
const BBitmap* screenshot = NULL;
const BitmapList& screenShots = package.Screenshots();
if (screenShots.CountItems() > 0) {
const BitmapRef& bitmapRef = screenShots.ItemAtFast(0);
if (bitmapRef.Get() != NULL)
screenshot = bitmapRef->Bitmap(SharedBitmap::SIZE_ANY);
}
fScreenshotView->SetBitmap(screenshot);
}
void Clear()
{
fDescriptionView->SetText("");
fEmailIconView->SetBitmap(NULL);
fEmailLinkView->SetText("");
fWebsiteIconView->SetBitmap(NULL);
fWebsiteLinkView->SetText("");
fScreenshotView->SetBitmap(NULL);
}
private:
BTextView* fDescriptionView;
BitmapView* fScreenshotView;
SharedBitmap fEmailIcon;
BitmapView* fEmailIconView;
BStringView* fEmailLinkView;
SharedBitmap fWebsiteIcon;
BitmapView* fWebsiteIconView;
BStringView* fWebsiteLinkView;
};
// #pragma mark - UserRatingsView
class RatingItemView : public BGroupView {
public:
RatingItemView(const UserRating& rating)
:
BGroupView(B_HORIZONTAL, 0.0f)
{
SetViewColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
kContentTint));
fAvatarView = new BitmapView("avatar view");
if (rating.User().Avatar().Get() != NULL) {
fAvatarView->SetBitmap(
rating.User().Avatar()->Bitmap(SharedBitmap::SIZE_16));
}
fAvatarView->SetExplicitMinSize(BSize(16.0f, 16.0f));
fNameView = new BStringView("user name", rating.User().NickName());
BFont nameFont(be_bold_font);
nameFont.SetSize(std::max(9.0f, floorf(nameFont.Size() * 0.9f)));
fNameView->SetFont(&nameFont);
fNameView->SetHighColor(kLightBlack);
fNameView->SetExplicitMaxSize(
BSize(nameFont.StringWidth("xxxxxxxxxxxxxxxxxxxxxx"), B_SIZE_UNSET));
fRatingView = new RatingView();
fRatingView->SetRating(rating.Rating());
BString ratingLabel;
ratingLabel.SetToFormat("%.1f", rating.Rating());
fRatingLabelView = new BStringView("rating label", ratingLabel);
BString versionLabel(B_TRANSLATE("for %Version%"));
versionLabel.ReplaceAll("%Version%", rating.PackageVersion());
fPackageVersionView = new BStringView("package version",
versionLabel);
BFont versionFont(be_plain_font);
versionFont.SetSize(std::max(9.0f, floorf(versionFont.Size() * 0.85f)));
fPackageVersionView->SetFont(&versionFont);
fPackageVersionView->SetHighColor(kLightBlack);
fTextView = new TextView("rating text");
fTextView->SetViewColor(ViewColor());
fTextView->SetText(rating.Comment());
// const float textInset = be_plain_font->Size();
// fTextView->SetInsets(0.0f, floorf(textInset / 2), textInset, 0.0f);
BLayoutBuilder::Group<>(this)
.Add(fAvatarView, 0.2f)
.AddGroup(B_VERTICAL, 0.0f)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.Add(fNameView)
.Add(fRatingView)
.Add(fRatingLabelView)
.AddGlue(0.1f)
.Add(fPackageVersionView)
.AddGlue(5.0f)
.End()
.Add(fTextView)
.End()
.SetInsets(B_USE_DEFAULT_SPACING)
;
}
private:
BitmapView* fAvatarView;
BStringView* fNameView;
RatingView* fRatingView;
BStringView* fRatingLabelView;
BStringView* fPackageVersionView;
TextView* fTextView;
};
class RatingContainerView : public BGroupView {
public:
RatingContainerView()
:
BGroupView(B_VERTICAL, 0.0)
{
SetViewColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
kContentTint));
AddChild(BSpaceLayoutItem::CreateGlue());
}
virtual BSize MinSize()
{
BSize minSize = BGroupView::MinSize();
return BSize(minSize.width, 80);
}
protected:
virtual void DoLayout()
{
BGroupView::DoLayout();
if (BScrollBar* scrollBar = ScrollBar(B_VERTICAL)) {
BRect layoutArea = GroupLayout()->LayoutArea();
float layoutHeight = layoutArea.Height();
// Min size is not reliable with HasHeightForWidth() children,
// since it does not reflect how thos children are currently
// laid out, but what their theoretical minimum size would be.
BLayoutItem* lastItem = GroupLayout()->ItemAt(
GroupLayout()->CountItems() - 1);
if (lastItem != NULL) {
layoutHeight = lastItem->Frame().bottom;
}
float viewHeight = Bounds().Height();
float max = layoutHeight- viewHeight;
scrollBar->SetRange(0, max);
if (layoutHeight > 0)
scrollBar->SetProportion(viewHeight / layoutHeight);
else
scrollBar->SetProportion(1);
}
}
};
class UserRatingsView : public BGroupView {
public:
UserRatingsView()
:
BGroupView("package ratings view", B_VERTICAL)
{
SetViewColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
kContentTint));
RatingContainerView* ratingsContainerView = new RatingContainerView();
fRatingContainerLayout = ratingsContainerView->GroupLayout();
BScrollView* scrollView = new RatingsScrollView(
"ratings scroll view", ratingsContainerView);
BLayoutBuilder::Group<>(this)
.Add(scrollView, 1.0f)
.SetInsets(B_USE_DEFAULT_SPACING, -1.0f, -1.0f, -1.0f)
;
}
virtual ~UserRatingsView()
{
Clear();
}
void SetPackage(const PackageInfo& package)
{
Clear();
const UserRatingList& userRatings = package.UserRatings();
// TODO: Sort by age or usefullness rating
// TODO: Optionally hide ratings that are not in the system language
for (int i = userRatings.CountItems() - 1; i >= 0; i--) {
const UserRating& rating = userRatings.ItemAtFast(i);
RatingItemView* view = new RatingItemView(rating);
fRatingContainerLayout->AddView(0, view);
}
}
void Clear()
{
for (int32 i = fRatingContainerLayout->CountItems() - 1;
BLayoutItem* item = fRatingContainerLayout->ItemAt(i); i--) {
RatingItemView* view = dynamic_cast<RatingItemView*>(
item->View());
if (view != NULL) {
view->RemoveSelf();
delete view;
}
}
}
private:
BGroupLayout* fRatingContainerLayout;
};
// #pragma mark - ChangelogView
class ChangelogView : public BView {
public:
ChangelogView()
:
BView("package changelog view", B_WILL_DRAW),
fLayout(new BGroupLayout(B_HORIZONTAL))
{
SetViewColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
kContentTint));
SetLayout(fLayout);
fTextView = new BTextView("changelog view");
fTextView->SetViewColor(ViewColor());
fTextView->MakeEditable(false);
const float textInset = be_plain_font->Size();
fTextView->SetInsets(textInset, textInset, textInset, 0.0f);
BScrollView* scrollView = new CustomScrollView(
"changelog scroll view", fTextView);
BLayoutBuilder::Group<>(fLayout)
.Add(BSpaceLayoutItem::CreateHorizontalStrut(32.0f))
.Add(scrollView, 1.0f)
.SetInsets(B_USE_DEFAULT_SPACING, -1.0f, -1.0f, -1.0f)
;
}
virtual ~ChangelogView()
{
Clear();
}
virtual void Draw(BRect updateRect)
{
}
void SetPackage(const PackageInfo& package)
{
fTextView->SetText(package.Changelog());
}
void Clear()
{
fTextView->SetText("");
}
private:
BGroupLayout* fLayout;
BTextView* fTextView;
};
// #pragma mark - PagesView
class PagesView : public BTabView {
public:
PagesView()
:
BTabView("pages view", B_WIDTH_FROM_WIDEST),
fLayout(new BCardLayout())
{
SetBorder(B_NO_BORDER);
fAboutView = new AboutView();
fUserRatingsView = new UserRatingsView();
fChangelogView = new ChangelogView();
AddTab(fAboutView);
AddTab(fUserRatingsView);
AddTab(fChangelogView);
TabAt(0)->SetLabel(B_TRANSLATE("About"));
TabAt(1)->SetLabel(B_TRANSLATE("Ratings"));
TabAt(2)->SetLabel(B_TRANSLATE("Changelog"));
Select(0);
}
virtual ~PagesView()
{
Clear();
}
void SetPackage(const PackageInfo& package)
{
Select(0);
fAboutView->SetPackage(package);
fUserRatingsView->SetPackage(package);
fChangelogView->SetPackage(package);
}
void Clear()
{
fAboutView->Clear();
fUserRatingsView->Clear();
fChangelogView->Clear();
}
private:
BCardLayout* fLayout;
AboutView* fAboutView;
UserRatingsView* fUserRatingsView;
ChangelogView* fChangelogView;
};
// #pragma mark - PackageInfoView
PackageInfoView::PackageInfoView(PackageManager* packageManager)
:
BGroupView("package info view", B_VERTICAL)
{
fTitleView = new TitleView();
fPackageActionView = new PackageActionView(packageManager);
fPagesView = new PagesView();
BLayoutBuilder::Group<>(this)