-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
PackageInfoView.cpp
804 lines (627 loc) · 16.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
/*
* 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"
#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),
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;
DrawBitmap(fBitmap, fBitmap->Bounds(), bounds);
}
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);
}
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());
}
}
};
// #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 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));
// Rating view
fRatingView = new RatingView();
fAvgRating = new BStringView("package average rating", "");
// small font
GetFont(&font);
font.SetSize(ceilf(font.Size() * 1.2f));
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.2f)
.AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING)
.Add(fRatingView)
.Add(fAvgRating)
.Add(fVoteInfo)
.End()
.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);
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("");
fRatingView->SetRating(-1.0f);
fAvgRating->SetText("");
fVoteInfo->SetText("");
}
private:
BitmapView* fIconView;
BStringView* fTitleView;
BStringView* fPublisherView;
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)));
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);
BLayoutBuilder::Group<>(this, B_HORIZONTAL, 0.0f)
// .Add(BSpaceLayoutItem::CreateHorizontalStrut(32.0f))
.AddGroup(B_VERTICAL, 0.0f)
.AddGlue()
.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)
.SetInsets(B_USE_DEFAULT_SPACING)
.End()
.End()
.End()
.Add(scrollView, 1.0f)
.SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNLIMITED))
.SetInsets(B_USE_DEFAULT_SPACING, -1.0f, -1.0f, -1.0f)
;
scrollView->ScrollBar(B_VERTICAL)->ResizeBy(0, -B_H_SCROLL_BAR_HEIGHT);
}
virtual ~AboutView()
{
Clear();
}
void SetPackage(const PackageInfo& package)
{
fDescriptionView->SetText(package.FullDescription());
fEmailIconView->SetBitmap(fEmailIcon.Bitmap(SharedBitmap::SIZE_16));
fEmailLinkView->SetText(package.Publisher().Email());
fWebsiteIconView->SetBitmap(fWebsiteIcon.Bitmap(SharedBitmap::SIZE_16));
fWebsiteLinkView->SetText(package.Publisher().Website());
}
void Clear()
{
fDescriptionView->SetText("");
fEmailIconView->SetBitmap(NULL);
fEmailLinkView->SetText("");
fWebsiteIconView->SetBitmap(NULL);
fWebsiteLinkView->SetText("");
}
private:
BTextView* fDescriptionView;
SharedBitmap fEmailIcon;
BitmapView* fEmailIconView;
BStringView* fEmailLinkView;
SharedBitmap fWebsiteIcon;
BitmapView* fWebsiteIconView;
BStringView* fWebsiteLinkView;
};
// #pragma mark - UserRatingsView
class UserRatingsView : public BView {
public:
UserRatingsView()
:
BView("package ratings view", 0),
fLayout(new BGroupLayout(B_HORIZONTAL))
{
SetViewColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
kContentTint));
SetLayout(fLayout);
fTextView = new BTextView("ratings 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(
"ratings 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 ~UserRatingsView()
{
Clear();
}
void SetPackage(const PackageInfo& package)
{
fTextView->SetText("");
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);
const BString& comment = rating.Comment();
if (comment.Length() == 0)
continue;
int offset = fTextView->TextLength();
if (offset > 0) {
// Insert separator lines
fTextView->Insert(offset, "\n\n", 2);
offset += 2;
}
fTextView->Insert(offset, comment.String(), comment.Length());
}
}
void Clear()
{
fTextView->SetText("");
}
private:
BGroupLayout* fLayout;
BTextView* fTextView;
};
// #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)
.AddGroup(B_HORIZONTAL, 0.0f)
.Add(fTitleView)
.Add(fPackageActionView)
.SetInsets(
B_USE_DEFAULT_SPACING, 0.0f,
B_USE_DEFAULT_SPACING, 0.0f)
.End()
.Add(fPagesView)
;
Clear();
}
PackageInfoView::~PackageInfoView()
{
}
void
PackageInfoView::AttachedToWindow()
{
}
void
PackageInfoView::MessageReceived(BMessage* message)
{
switch (message->what) {
default:
BGroupView::MessageReceived(message);
break;
}
}
void
PackageInfoView::SetPackage(const PackageInfo& package)
{
fTitleView->SetPackage(package);
fPackageActionView->SetPackage(package);
fPagesView->SetPackage(package);
if (fPagesView->IsHidden(fPagesView))
fPagesView->Show();
}
void
PackageInfoView::Clear()
{
fTitleView->Clear();
fPackageActionView->Clear();
fPagesView->Clear();
if (!fPagesView->IsHidden(fPagesView))
fPagesView->Hide();
}