2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Nick Korotysh <nick.korotysh@gmail.com>
4 * Copyright (C) 2007-2023 Transmission authors and contributors.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
34 #include "base/utils/misc.h"
36 static const CGFloat kBetweenPadding = 2.0;
37 static const NSColor *const kUploadBadgeColor = [NSColor colorWithRed:0.094 green:0.243 blue:0.835 alpha:0.9]; // #183ed5
38 static const NSColor *const kDownloadBadgeColor = [NSColor colorWithRed:0.216 green:0.482 blue:0.173 alpha:0.9]; // #377b2c
39 static const NSSize kBadgeSize = {128.0, 30.0};
40 static const NSString *const kUploadArrow = @"\u2191"; // or U+2b61
41 static const NSString *const kDownloadArrow = @"\u2193"; // or U+2b63
42 static CGSize kArrowInset;
43 static CGSize kArrowSize;
45 @interface BadgeView ()
47 @property(nonatomic) NSMutableDictionary *fAttributes;
49 @property(nonatomic) int64_t fDownloadRate;
50 @property(nonatomic) int64_t fUploadRate;
54 @implementation BadgeView
58 if ((self = [super init]))
63 NSShadow *stringShadow = [[NSShadow alloc] init];
64 stringShadow.shadowOffset = NSMakeSize(2.0, -2.0);
65 stringShadow.shadowBlurRadius = 4.0;
67 _fAttributes = [[NSMutableDictionary alloc] initWithCapacity:3];
68 _fAttributes[NSForegroundColorAttributeName] = NSColor.whiteColor;
69 _fAttributes[NSShadowAttributeName] = stringShadow;
70 _fAttributes[NSFontAttributeName] = [NSFont boldSystemFontOfSize:26.0];
72 // DownloadBadge and UploadBadge should have the same size
73 // DownArrowTemplate and UpArrowTemplate should have the same size
74 kArrowInset = { kBadgeSize.height * 0.2, kBadgeSize.height * 0.1 };
75 kArrowSize = [kDownloadArrow sizeWithAttributes:self.fAttributes];
80 - (BOOL)setRatesWithDownload:(int64_t)downloadRate upload:(int64_t)uploadRate
82 // only needs update if the badges were displayed or are displayed now
83 if ((self.fDownloadRate == downloadRate) && (self.fUploadRate == uploadRate))
88 self.fDownloadRate = downloadRate;
89 self.fUploadRate = uploadRate;
94 - (void)drawRect:(NSRect)rect
96 [NSApp.applicationIconImage drawInRect:rect fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1.0];
98 const BOOL upload = self.fUploadRate >= 0.1;
99 const BOOL download = self.fDownloadRate >= 0.1;
100 CGFloat bottom = 0.0;
103 [self badge:kDownloadBadgeColor arrow:kDownloadArrow
104 string:Utils::Misc::friendlyUnitCompact(self.fDownloadRate).toNSString()
109 bottom += kBadgeSize.height + kBetweenPadding; // upload rate above download rate
114 [self badge:kUploadBadgeColor arrow:kUploadArrow
115 string:Utils::Misc::friendlyUnitCompact(self.fUploadRate).toNSString()
120 - (void)badge:(const NSColor*)badgeColor arrow:(const NSString*)arrowSymbol string:(const NSString*)string atHeight:(CGFloat)height
123 const NSRect badgeRect = { { 0.0, height }, kBadgeSize };
124 [badgeColor setFill];
125 const CGFloat r = kBadgeSize.height / 2.0;
126 [[NSBezierPath bezierPathWithRoundedRect:badgeRect xRadius:r yRadius:r] fill];
128 // string is in center of image
129 const NSSize stringSize = [string sizeWithAttributes:self.fAttributes];
131 stringRect.origin.x = NSMidX(badgeRect) - stringSize.width * 0.5 + kArrowInset.width; // adjust for arrow
132 stringRect.origin.y = NSMidY(badgeRect) - stringSize.height * 0.5 + 1.0; // adjust for shadow
133 stringRect.size = stringSize;
134 [string drawInRect:stringRect withAttributes:self.fAttributes];
137 const NSRect arrowRect = { { kArrowInset.width, stringRect.origin.y }, kArrowSize };
138 [arrowSymbol drawInRect:arrowRect withAttributes:self.fAttributes];