1 /* 2 * Copyright 2017 Julian Harnath <julian.harnath@rwth-aachen.de> 3 * Copyright 2020 Andrew Lindesay <apl@lindesay.co.nz> 4 * All rights reserved. Distributed under the terms of the MIT license. 5 */ 6 7 8 #include "WorkStatusView.h" 9 10 #include <CardLayout.h> 11 #include <Catalog.h> 12 #include <LayoutBuilder.h> 13 #include <SeparatorView.h> 14 #include <StatusBar.h> 15 #include <StringFormat.h> 16 #include <StringView.h> 17 18 #include <stdio.h> 19 20 #include "BarberPole.h" 21 22 23 #undef B_TRANSLATION_CONTEXT 24 #define B_TRANSLATION_CONTEXT "WorkStatusView" 25 26 #define VIEW_INDEX_BARBER_POLE (int32) 0 27 #define VIEW_INDEX_PROGRESS_BAR (int32) 1 28 29 30 static const BSize kStatusBarSize = BSize(100,20); 31 32 33 WorkStatusView::WorkStatusView(const char* name) 34 : 35 BView(name, 0), 36 fProgressBar(new BStatusBar("progress bar")), 37 fBarberPole(new BarberPole("barber pole")), 38 fProgressLayout(new BCardLayout()), 39 fProgressView(new BView("progress view", 0)), 40 fStatusText(new BStringView("status text", NULL)) 41 { 42 fProgressView->SetLayout(fProgressLayout); 43 fProgressLayout->AddView(fBarberPole); 44 fProgressLayout->AddView(fProgressBar); 45 46 fBarberPole->SetExplicitSize(kStatusBarSize); 47 fProgressBar->SetMaxValue(1.0f); 48 fProgressBar->SetBarHeight(kStatusBarSize.Height()); 49 fProgressBar->SetExplicitSize(kStatusBarSize); 50 51 fStatusText->SetFontSize(be_plain_font->Size() * 0.9f); 52 53 BLayoutBuilder::Group<>(this, B_VERTICAL, 0) 54 .Add(new BSeparatorView()) 55 .AddGroup(B_HORIZONTAL) 56 .SetInsets(5, 2, 5, 2) 57 .Add(fProgressLayout, 0.2f) 58 .Add(fStatusText) 59 .AddGlue() 60 .End() 61 ; 62 } 63 64 65 WorkStatusView::~WorkStatusView() 66 { 67 } 68 69 70 void 71 WorkStatusView::SetBusy(const BString& text) 72 { 73 SetText(text); 74 SetBusy(); 75 } 76 77 78 void 79 WorkStatusView::SetBusy() 80 { 81 fBarberPole->Start(); 82 if (fProgressLayout->VisibleIndex() != VIEW_INDEX_BARBER_POLE) 83 fProgressLayout->SetVisibleItem(VIEW_INDEX_BARBER_POLE); 84 } 85 86 87 void 88 WorkStatusView::SetIdle() 89 { 90 fBarberPole->Stop(); 91 fProgressLayout->SetVisibleItem(VIEW_INDEX_BARBER_POLE); 92 SetText(NULL); 93 } 94 95 96 void 97 WorkStatusView::SetProgress(float value) 98 { 99 fProgressBar->SetTo(value); 100 if (fProgressLayout->VisibleIndex() != VIEW_INDEX_PROGRESS_BAR) 101 fProgressLayout->SetVisibleItem(VIEW_INDEX_PROGRESS_BAR); 102 } 103 104 105 void 106 WorkStatusView::SetText(const BString& text) 107 { 108 fStatusText->SetText(text); 109 } 110 111 112 void 113 WorkStatusView::PackageStatusChanged(const PackageInfoRef& package) 114 { 115 switch (package->State()) { 116 case DOWNLOADING: 117 fPendingPackages.erase(package->Name()); 118 if (package->Name() != fDownloadingPackage) { 119 fDownloadingPackage = package->Name(); 120 _SetTextDownloading(package->Title()); 121 } 122 SetProgress(package->DownloadProgress()); 123 break; 124 125 case PENDING: 126 fPendingPackages.insert(package->Name()); 127 if (package->Name() == fDownloadingPackage) 128 fDownloadingPackage = ""; 129 if (fDownloadingPackage.IsEmpty()) { 130 _SetTextPendingDownloads(); 131 SetBusy(); 132 } 133 break; 134 135 case NONE: 136 case ACTIVATED: 137 case INSTALLED: 138 case UNINSTALLED: 139 if (package->Name() == fDownloadingPackage) 140 fDownloadingPackage = ""; 141 fPendingPackages.erase(package->Name()); 142 if (fPendingPackages.empty()) 143 SetIdle(); 144 else { 145 _SetTextPendingDownloads(); 146 SetBusy(); 147 } 148 break; 149 } 150 } 151 152 153 void 154 WorkStatusView::_SetTextPendingDownloads() 155 { 156 BString text; 157 static BStringFormat format(B_TRANSLATE("{0, plural," 158 "one{1 package to download}" 159 "other{# packages to download}}")); 160 format.Format(text, fPendingPackages.size()); 161 162 SetText(text); 163 } 164 165 166 void 167 WorkStatusView::_SetTextDownloading(const BString& title) 168 { 169 BString text(B_TRANSLATE("Downloading package '%name%'")); 170 text.ReplaceFirst("%name%", title); 171 172 if (!fPendingPackages.empty()) { 173 BString more; 174 static BStringFormat format(B_TRANSLATE("{0, plural," 175 "one{(1 more to download)}" 176 "other{(# more to download)}}")); 177 format.Format(more, fPendingPackages.size()); 178 text += " "; 179 text += more; 180 } 181 SetText(text); 182 } 183