transmission/macosx/PiecesView.mm

217 lines
5.6 KiB
Plaintext
Raw Normal View History

// This file Copyright © 2006-2022 Transmission authors and contributors.
// 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
#include <vector>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.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"
#import "NSApplicationAdditions.h"
2007-09-16 01:02:06 +00:00
#define MAX_ACROSS 18
#define BETWEEN 1.0
2007-09-16 01:02:06 +00:00
#define HIGH_PEERS 30
enum
{
PIECE_NONE,
PIECE_SOME,
PIECE_HIGH_PEERS,
PIECE_FINISHED,
PIECE_FLASHING
};
@interface PiecesView ()
@property(nonatomic) std::vector<int8_t> fPieces;
@property(nonatomic) NSInteger fNumPieces;
@property(nonatomic) NSInteger fAcross;
@property(nonatomic) NSInteger fWidth;
@property(nonatomic) NSInteger fExtraBorder;
@end
2007-09-16 01:02:06 +00:00
@implementation PiecesView
- (void)drawRect:(NSRect)dirtyRect
2007-09-16 01:02:06 +00:00
{
[[NSColor.controlTextColor colorWithAlphaComponent:0.2] setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
- (void)awakeFromNib
{
self.torrent = nil;
2007-09-16 01:02:06 +00:00
}
- (void)viewDidChangeEffectiveAppearance
{
self.torrent = _torrent;
[self updateView];
}
- (void)dealloc
2007-09-16 01:02:06 +00:00
{
}
- (void)setTorrent:(Torrent*)torrent
2007-09-16 01:02:06 +00:00
{
[self clearView];
_torrent = (torrent && !torrent.magnet) ? torrent : nil;
if (_torrent)
2011-03-05 03:19:26 +00:00
{
//determine relevant values
_fNumPieces = MIN(_torrent.pieceCount, MAX_ACROSS * MAX_ACROSS);
_fAcross = ceil(sqrt(_fNumPieces));
CGFloat const width = self.bounds.size.width;
_fWidth = (width - (_fAcross + 1) * BETWEEN) / _fAcross;
_fExtraBorder = (width - ((_fWidth + BETWEEN) * _fAcross + BETWEEN)) / 2;
2011-03-05 03:19:26 +00:00
}
NSImage* back = [[NSImage alloc] initWithSize:self.bounds.size];
self.image = back;
[self setNeedsDisplay];
2007-09-16 01:02:06 +00:00
}
- (void)clearView
2008-06-09 21:20:01 +00:00
{
self.fPieces.clear();
2008-06-09 21:20:01 +00:00
}
- (void)updateView
2007-09-16 01:02:06 +00:00
{
if (!self.torrent)
{
2007-09-16 01:02:06 +00:00
return;
}
2008-06-09 21:20:01 +00:00
//determine if first time
BOOL const first = std::empty(self.fPieces);
if (first)
{
_fPieces.resize(self.fNumPieces);
}
2007-09-16 01:02:06 +00:00
auto pieces = std::vector<int8_t>{};
auto piecesPercent = std::vector<float>{};
BOOL const showAvailability = [NSUserDefaults.standardUserDefaults boolForKey:@"PiecesViewShowAvailability"];
if (showAvailability)
{
pieces.resize(self.fNumPieces);
[self.torrent getAvailability:std::data(pieces) size:std::size(pieces)];
2007-09-16 01:02:06 +00:00
}
else
{
piecesPercent.resize(self.fNumPieces);
[self.torrent getAmountFinished:std::data(piecesPercent) size:std::size(piecesPercent)];
2007-09-16 01:02:06 +00:00
}
NSImage* image = self.image;
NSRect fillRects[self.fNumPieces];
NSColor* fillColors[self.fNumPieces];
NSColor* defaultColor = NSApp.darkMode ? NSColor.blackColor : NSColor.whiteColor;
NSInteger usedCount = 0;
for (NSInteger index = 0; index < self.fNumPieces; index++)
{
NSColor* pieceColor = nil;
if (showAvailability ? pieces[index] == -1 : piecesPercent[index] == 1.0)
2007-09-16 01:02:06 +00:00
{
if (first || self.fPieces[index] != PIECE_FINISHED)
{
if (!first && self.fPieces[index] != PIECE_FLASHING)
2008-06-10 20:15:00 +00:00
{
pieceColor = NSColor.systemOrangeColor;
self.fPieces[index] = PIECE_FLASHING;
}
else
{
pieceColor = NSColor.systemBlueColor;
self.fPieces[index] = PIECE_FINISHED;
2008-06-10 20:15:00 +00:00
}
}
}
else if (showAvailability ? pieces[index] == 0 : piecesPercent[index] == 0.0)
{
if (first || self.fPieces[index] != PIECE_NONE)
{
pieceColor = defaultColor;
self.fPieces[index] = PIECE_NONE;
2007-09-16 01:02:06 +00:00
}
}
else if (showAvailability && pieces[index] >= HIGH_PEERS)
{
if (first || self.fPieces[index] != PIECE_HIGH_PEERS)
{
pieceColor = NSColor.systemGreenColor;
self.fPieces[index] = PIECE_HIGH_PEERS;
2007-09-16 01:02:06 +00:00
}
}
else
{
//always redraw "mixed"
CGFloat percent = showAvailability ? (CGFloat)pieces[index] / HIGH_PEERS : piecesPercent[index];
NSColor* fullColor = showAvailability ? NSColor.systemGreenColor : NSColor.systemBlueColor;
pieceColor = [defaultColor blendedColorWithFraction:percent ofColor:fullColor];
self.fPieces[index] = PIECE_SOME;
}
if (pieceColor)
{
NSInteger const across = index % self.fAcross;
NSInteger const down = index / self.fAcross;
fillRects[usedCount] = NSMakeRect(
across * (self.fWidth + BETWEEN) + BETWEEN + self.fExtraBorder,
image.size.width - (down + 1) * (self.fWidth + BETWEEN) - self.fExtraBorder,
self.fWidth,
self.fWidth);
fillColors[usedCount] = pieceColor;
usedCount++;
2007-09-16 01:02:06 +00:00
}
}
if (usedCount > 0)
{
[image lockFocus];
NSRectFillListWithColors(fillRects, fillColors, usedCount);
[image unlockFocus];
[self setNeedsDisplay];
}
2007-09-16 01:02:06 +00:00
}
- (BOOL)acceptsFirstMouse:(NSEvent*)event
2007-09-16 01:02:06 +00:00
{
return YES;
}
- (void)mouseDown:(NSEvent*)event
2007-09-16 01:02:06 +00:00
{
if (self.torrent)
{
BOOL const availability = ![NSUserDefaults.standardUserDefaults boolForKey:@"PiecesViewShowAvailability"];
[NSUserDefaults.standardUserDefaults setBool:availability forKey:@"PiecesViewShowAvailability"];
[self sendAction:self.action to:self.target];
}
[super mouseDown:event];
2007-09-16 01:02:06 +00:00
}
@end