transmission/macosx/BadgeView.mm

123 lines
4.4 KiB
Plaintext
Raw Normal View History

// This file Copyright © Transmission authors and contributors.
// It may be used under the MIT (SPDX: MIT) license.
// License text can be found in the licenses/ folder.
#import "BadgeView.h"
#import "NSStringAdditions.h"
#import "NSImageAdditions.h"
chore: fix warnings when compiling macOS client with either Xcode or CMake (#6676) * chore: fix CGFloat comparison warnings in macOS code. There are 2 cases: 1. Speed comparisons. The lowest significant value displayed in UI is 0.1 bytes per sec. See [NSString stringForSpeed] and [NSString stringForSpeedCompact]. 2. Ratio limit comparison. The lowest significant value displayed in UI is 0.01 (no unit). This is based on maximumFractionDigits=2 set in related XIB file. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: fix warning about shadowed variable CGFloat const difference was used twice in the same scope Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: fix unused block parameter warnings Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: disable GCC_WARN_64_TO_32_BIT_CONVERSION for libtransmission This warnings are not reported with CMake build. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: disable CLANG_WARN_STRICT_PROTOTYPES for dht This is third party target, warning is not enabled with CMake build. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: disable '-Wexit-time-destructors' warning with CMake. There are two cases when this is reported in libtransmission: 1. `log_state` in anonymous namespace in `log.cc`. 2. static inline `dh_pool_mutex` in `tr_handshake` in `handshake.h`. I don't see how this may be improved or how this affects correctness, so don't nag about that. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> --------- Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com>
2024-03-15 05:16:26 +00:00
#import "Utils.h"
static CGFloat const kBetweenPadding = 2.0;
static NSImage* kWhiteUpArrow = [[NSImage imageNamed:@"UpArrowTemplate"] imageWithColor:NSColor.whiteColor];
static NSImage* kWhiteDownArrow = [[NSImage imageNamed:@"DownArrowTemplate"] imageWithColor:NSColor.whiteColor];
static CGSize kArrowInset;
static CGSize kArrowSize;
typedef NS_ENUM(NSInteger, ArrowDirection) {
ArrowDirectionUp,
ArrowDirectionDown,
};
@interface BadgeView ()
@property(nonatomic) NSMutableDictionary* fAttributes;
@property(nonatomic) CGFloat fDownloadRate;
@property(nonatomic) CGFloat fUploadRate;
@end
@implementation BadgeView
2022-12-21 20:21:16 +00:00
- (instancetype)init
{
if ((self = [super init]))
{
_fDownloadRate = 0.0;
_fUploadRate = 0.0;
NSShadow* stringShadow = [[NSShadow alloc] init];
stringShadow.shadowOffset = NSMakeSize(2.0, -2.0);
stringShadow.shadowBlurRadius = 4.0;
_fAttributes = [[NSMutableDictionary alloc] initWithCapacity:3];
_fAttributes[NSForegroundColorAttributeName] = NSColor.whiteColor;
_fAttributes[NSShadowAttributeName] = stringShadow;
_fAttributes[NSFontAttributeName] = [NSFont boldSystemFontOfSize:26.0];
// DownloadBadge and UploadBadge should have the same size
NSSize badgeSize = [NSImage imageNamed:@"DownloadBadge"].size;
// DownArrowTemplate and UpArrowTemplate should have the same size
CGFloat arrowWidthHeightRatio = kWhiteDownArrow.size.width / kWhiteDownArrow.size.height;
// arrow height equal to font capital letter height + shadow
CGFloat arrowHeight = [_fAttributes[NSFontAttributeName] capHeight] + 4;
kArrowInset = { badgeSize.height * 0.2, badgeSize.height * 0.1 };
kArrowSize = { arrowHeight * arrowWidthHeightRatio, arrowHeight };
}
return self;
}
- (BOOL)setRatesWithDownload:(CGFloat)downloadRate upload:(CGFloat)uploadRate
{
//only needs update if the badges were displayed or are displayed now
chore: fix warnings when compiling macOS client with either Xcode or CMake (#6676) * chore: fix CGFloat comparison warnings in macOS code. There are 2 cases: 1. Speed comparisons. The lowest significant value displayed in UI is 0.1 bytes per sec. See [NSString stringForSpeed] and [NSString stringForSpeedCompact]. 2. Ratio limit comparison. The lowest significant value displayed in UI is 0.01 (no unit). This is based on maximumFractionDigits=2 set in related XIB file. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: fix warning about shadowed variable CGFloat const difference was used twice in the same scope Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: fix unused block parameter warnings Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: disable GCC_WARN_64_TO_32_BIT_CONVERSION for libtransmission This warnings are not reported with CMake build. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: disable CLANG_WARN_STRICT_PROTOTYPES for dht This is third party target, warning is not enabled with CMake build. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: disable '-Wexit-time-destructors' warning with CMake. There are two cases when this is reported in libtransmission: 1. `log_state` in anonymous namespace in `log.cc`. 2. static inline `dh_pool_mutex` in `tr_handshake` in `handshake.h`. I don't see how this may be improved or how this affects correctness, so don't nag about that. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> --------- Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com>
2024-03-15 05:16:26 +00:00
if (isSpeedEqual(self.fDownloadRate, downloadRate) && isSpeedEqual(self.fUploadRate, uploadRate))
{
2009-01-03 06:06:21 +00:00
return NO;
}
self.fDownloadRate = downloadRate;
self.fUploadRate = uploadRate;
2009-01-03 06:06:21 +00:00
return YES;
}
- (void)drawRect:(NSRect)rect
{
[NSApp.applicationIconImage drawInRect:rect fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1.0];
BOOL const upload = self.fUploadRate >= 0.1;
BOOL const download = self.fDownloadRate >= 0.1;
CGFloat bottom = 0.0;
if (download)
{
NSImage* downloadBadge = [NSImage imageNamed:@"DownloadBadge"];
[self badge:downloadBadge arrow:ArrowDirectionDown string:[NSString stringForSpeedAbbrevCompact:self.fDownloadRate]
atHeight:bottom];
if (upload)
{
bottom += downloadBadge.size.height + kBetweenPadding; //upload rate above download rate
}
}
if (upload)
{
[self badge:[NSImage imageNamed:@"UploadBadge"] arrow:ArrowDirectionUp
string:[NSString stringForSpeedAbbrevCompact:self.fUploadRate]
atHeight:bottom];
}
}
- (void)badge:(NSImage*)badge arrow:(ArrowDirection)arrowDirection string:(NSString*)string atHeight:(CGFloat)height
{
// background
NSRect badgeRect = { { 0.0, height }, badge.size };
[badge drawInRect:badgeRect fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1.0];
//string is in center of image
NSSize stringSize = [string sizeWithAttributes:self.fAttributes];
2009-12-12 18:52:03 +00:00
NSRect stringRect;
stringRect.origin.x = NSMidX(badgeRect) - stringSize.width * 0.5 + kArrowInset.width; // adjust for arrow
stringRect.origin.y = NSMidY(badgeRect) - stringSize.height * 0.5 + 1.0; // adjust for shadow
2007-12-05 18:23:51 +00:00
stringRect.size = stringSize;
[string drawInRect:stringRect withAttributes:self.fAttributes];
// arrow
NSImage* arrow = arrowDirection == ArrowDirectionUp ? kWhiteUpArrow : kWhiteDownArrow;
NSRect arrowRect = { { kArrowInset.width, stringRect.origin.y + kArrowInset.height + (arrowDirection == ArrowDirectionUp ? 0.5 : -0.5) },
kArrowSize };
[arrow drawInRect:arrowRect fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1.0];
}
@end