2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2018-09-30 10:37:30 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "PiecesView.h"
|
2008-11-01 15:22:32 +00:00
|
|
|
#import "Torrent.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "InfoWindowController.h"
|
2022-06-25 06:03:07 +00:00
|
|
|
#import "NSApplicationAdditions.h"
|
2010-04-23 16:59:14 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static NSInteger const kMaxAcross = 18;
|
2023-01-17 15:10:23 +00:00
|
|
|
static NSInteger const kMaxCells = kMaxAcross * kMaxAcross;
|
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static CGFloat const kBetweenPadding = 1.0;
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static int8_t const kHighPeers = 10;
|
2008-06-10 19:56:53 +00:00
|
|
|
|
2023-01-17 15:10:23 +00:00
|
|
|
static NSColor* const DoneColor = NSColor.systemBlueColor;
|
|
|
|
static NSColor* const BlinkColor = NSColor.systemOrangeColor;
|
|
|
|
static NSColor* const HighColor = NSColor.systemGreenColor; // high availability
|
|
|
|
|
|
|
|
typedef struct PieceInfo
|
2009-10-18 02:12:43 +00:00
|
|
|
{
|
2023-01-17 15:10:23 +00:00
|
|
|
int8_t available[kMaxCells];
|
|
|
|
float complete[kMaxCells];
|
|
|
|
} PieceInfo;
|
2008-06-10 19:56:53 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@interface PiecesView ()
|
2023-01-17 15:10:23 +00:00
|
|
|
@end
|
2022-02-22 16:04:20 +00:00
|
|
|
|
2023-01-17 15:10:23 +00:00
|
|
|
@implementation PiecesView
|
|
|
|
{
|
|
|
|
PieceInfo fPieceInfo;
|
|
|
|
NSString* fRenderedHashString;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSColor*)backgroundColor
|
|
|
|
{
|
|
|
|
return NSApp.darkMode ? NSColor.blackColor : NSColor.whiteColor;
|
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
|
2023-01-17 15:10:23 +00:00
|
|
|
- (BOOL)isCompletenessDone:(float)val
|
|
|
|
{
|
|
|
|
return val >= 1.0F;
|
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
|
2023-01-17 15:10:23 +00:00
|
|
|
- (BOOL)isCompletenessNone:(float)val
|
|
|
|
{
|
|
|
|
return val <= 0.0F;
|
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
|
2023-01-17 15:10:23 +00:00
|
|
|
- (NSColor*)completenessColor:(float)oldVal newVal:(float)newVal noBlink:(BOOL)noBlink
|
|
|
|
{
|
|
|
|
if ([self isCompletenessDone:newVal])
|
|
|
|
{
|
|
|
|
return noBlink || [self isCompletenessDone:oldVal] ? DoneColor : BlinkColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([self isCompletenessNone:newVal])
|
|
|
|
{
|
|
|
|
return noBlink || [self isCompletenessNone:oldVal] ? [self backgroundColor] : BlinkColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [[self backgroundColor] blendedColorWithFraction:newVal ofColor:DoneColor];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isAvailabilityDone:(uint8_t)val
|
|
|
|
{
|
|
|
|
return val == (uint8_t)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isAvailabilityNone:(uint8_t)val
|
|
|
|
{
|
|
|
|
return val == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isAvailabilityHigh:(uint8_t)val
|
|
|
|
{
|
|
|
|
return val >= kHighPeers;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSColor*)availabilityColor:(int8_t)oldVal newVal:(int8_t)newVal noBlink:(bool)noBlink
|
|
|
|
{
|
|
|
|
if ([self isAvailabilityDone:newVal])
|
|
|
|
{
|
|
|
|
return noBlink || [self isAvailabilityDone:oldVal] ? DoneColor : BlinkColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([self isAvailabilityNone:newVal])
|
|
|
|
{
|
|
|
|
return noBlink || [self isAvailabilityNone:oldVal] ? [self backgroundColor] : BlinkColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([self isAvailabilityHigh:newVal])
|
|
|
|
{
|
|
|
|
return noBlink || [self isAvailabilityHigh:oldVal] ? HighColor : BlinkColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGFloat percent = CGFloat(newVal) / kHighPeers;
|
|
|
|
return [[self backgroundColor] blendedColorWithFraction:percent ofColor:HighColor];
|
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2022-06-25 06:03:07 +00:00
|
|
|
- (void)drawRect:(NSRect)dirtyRect
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-06-25 06:03:07 +00:00
|
|
|
[[NSColor.controlTextColor colorWithAlphaComponent:0.2] setFill];
|
|
|
|
NSRectFill(dirtyRect);
|
|
|
|
[super drawRect:dirtyRect];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-06-25 06:03:07 +00:00
|
|
|
- (void)awakeFromNib
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.torrent = nil;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 06:03:07 +00:00
|
|
|
- (void)viewDidChangeEffectiveAppearance
|
|
|
|
{
|
2022-06-29 04:20:42 +00:00
|
|
|
self.torrent = _torrent;
|
2022-06-25 06:03:07 +00:00
|
|
|
[self updateView];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)dealloc
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setTorrent:(Torrent*)torrent
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
_torrent = (torrent && !torrent.magnet) ? torrent : nil;
|
2023-01-17 15:10:23 +00:00
|
|
|
self.image = [[NSImage alloc] initWithSize:self.bounds.size];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-01-17 15:10:23 +00:00
|
|
|
[self clearView];
|
2023-06-18 18:32:34 +00:00
|
|
|
self.needsDisplay = YES;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)clearView
|
2008-06-09 21:20:01 +00:00
|
|
|
{
|
2023-01-17 15:10:23 +00:00
|
|
|
fRenderedHashString = nil;
|
|
|
|
memset(&fPieceInfo, 0, sizeof(PieceInfo));
|
2008-06-09 21:20:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateView
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!self.torrent)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2007-09-16 01:02:06 +00:00
|
|
|
return;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-01-17 15:10:23 +00:00
|
|
|
// get the previous state
|
|
|
|
PieceInfo const oldInfo = fPieceInfo;
|
|
|
|
BOOL const first = ![self.torrent.hashString isEqualToString:fRenderedHashString];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-01-17 15:10:23 +00:00
|
|
|
// get the current state
|
2022-04-21 14:28:38 +00:00
|
|
|
BOOL const showAvailability = [NSUserDefaults.standardUserDefaults boolForKey:@"PiecesViewShowAvailability"];
|
2023-01-17 15:10:23 +00:00
|
|
|
NSInteger const numCells = MIN(_torrent.pieceCount, kMaxCells);
|
|
|
|
PieceInfo info;
|
|
|
|
[self.torrent getAvailability:info.available size:numCells];
|
|
|
|
[self.torrent getAmountFinished:info.complete size:numCells];
|
|
|
|
|
|
|
|
// compute bounds and color of each cell
|
|
|
|
NSInteger const across = (NSInteger)ceil(sqrt(numCells));
|
|
|
|
CGFloat const fullWidth = self.bounds.size.width;
|
|
|
|
NSInteger const cellWidth = (NSInteger)((fullWidth - (across + 1) * kBetweenPadding) / across);
|
|
|
|
NSInteger const extraBorder = (NSInteger)((fullWidth - ((cellWidth + kBetweenPadding) * across + kBetweenPadding)) / 2);
|
|
|
|
NSMutableArray<NSValue*>* cellBounds = [NSMutableArray arrayWithCapacity:numCells];
|
|
|
|
NSMutableArray<NSColor*>* cellColors = [NSMutableArray arrayWithCapacity:numCells];
|
|
|
|
for (NSInteger index = 0; index < numCells; index++)
|
2009-10-18 02:23:10 +00:00
|
|
|
{
|
2023-01-17 15:10:23 +00:00
|
|
|
NSInteger const row = index / across;
|
|
|
|
NSInteger const col = index % across;
|
|
|
|
|
|
|
|
cellBounds[index] = [NSValue valueWithRect:NSMakeRect(
|
|
|
|
col * (cellWidth + kBetweenPadding) + kBetweenPadding + extraBorder,
|
|
|
|
fullWidth - (row + 1) * (cellWidth + kBetweenPadding) - extraBorder,
|
|
|
|
cellWidth,
|
|
|
|
cellWidth)];
|
|
|
|
|
|
|
|
cellColors[index] = showAvailability ?
|
|
|
|
[self availabilityColor:oldInfo.available[index] newVal:info.available[index] noBlink:first] :
|
|
|
|
[self completenessColor:oldInfo.complete[index] newVal:info.complete[index] noBlink:first];
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-01-17 15:10:23 +00:00
|
|
|
// build an image with the cells
|
|
|
|
if (numCells > 0)
|
2008-06-10 21:46:34 +00:00
|
|
|
{
|
2023-01-17 15:10:23 +00:00
|
|
|
self.image = [NSImage imageWithSize:self.bounds.size flipped:NO drawingHandler:^BOOL(NSRect /*dstRect*/) {
|
|
|
|
NSRect cFillRects[numCells];
|
|
|
|
for (NSInteger i = 0; i < numCells; ++i)
|
2022-12-22 22:37:10 +00:00
|
|
|
{
|
2023-01-17 15:10:23 +00:00
|
|
|
cFillRects[i] = cellBounds[i].rectValue;
|
2022-12-22 22:37:10 +00:00
|
|
|
}
|
2023-01-17 15:10:23 +00:00
|
|
|
NSColor* cFillColors[numCells];
|
|
|
|
for (NSInteger i = 0; i < numCells; ++i)
|
2022-12-22 22:37:10 +00:00
|
|
|
{
|
2023-01-17 15:10:23 +00:00
|
|
|
cFillColors[i] = cellColors[i];
|
2022-12-22 22:37:10 +00:00
|
|
|
}
|
2023-01-17 15:10:23 +00:00
|
|
|
NSRectFillListWithColors(cFillRects, cFillColors, numCells);
|
2022-12-22 22:37:10 +00:00
|
|
|
return YES;
|
|
|
|
}];
|
2023-06-18 18:32:34 +00:00
|
|
|
self.needsDisplay = YES;
|
2008-06-10 21:46:34 +00:00
|
|
|
}
|
2023-01-17 15:10:23 +00:00
|
|
|
|
|
|
|
// save the current state so we can compare it later
|
|
|
|
fPieceInfo = info;
|
|
|
|
fRenderedHashString = self.torrent.hashString;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)acceptsFirstMouse:(NSEvent*)event
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)mouseDown:(NSEvent*)event
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.torrent)
|
2010-04-06 01:44:38 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
BOOL const availability = ![NSUserDefaults.standardUserDefaults boolForKey:@"PiecesViewShowAvailability"];
|
|
|
|
[NSUserDefaults.standardUserDefaults setBool:availability forKey:@"PiecesViewShowAvailability"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[self sendAction:self.action to:self.target];
|
2010-04-06 01:44:38 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[super mouseDown:event];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|