2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2007-2023 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
|
|
|
|
|
|
|
#import "FilePriorityCell.h"
|
2007-10-20 19:13:52 +00:00
|
|
|
#import "FileOutlineView.h"
|
2008-05-22 18:39:49 +00:00
|
|
|
#import "FileListNode.h"
|
2011-10-22 20:45:20 +00:00
|
|
|
#import "NSImageAdditions.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "Torrent.h"
|
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static CGFloat const kImageOverlap = 1.0;
|
2011-05-30 00:09:22 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@implementation FilePriorityCell
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)init
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
self.trackingMode = NSSegmentSwitchTrackingSelectAny;
|
2021-10-31 15:18:27 +00:00
|
|
|
self.controlSize = NSControlSizeMini;
|
2021-08-07 07:27:56 +00:00
|
|
|
self.segmentCount = 3;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
for (NSInteger i = 0; i < self.segmentCount; i++)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[self setLabel:@"" forSegment:i];
|
|
|
|
[self setWidth:9.0f forSegment:i]; //9 is minimum size to get proper look
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[self setImage:[NSImage imageNamed:@"PriorityControlLow"] forSegment:0];
|
|
|
|
[self setImage:[NSImage imageNamed:@"PriorityControlNormal"] forSegment:1];
|
|
|
|
[self setImage:[NSImage imageNamed:@"PriorityControlHigh"] forSegment:2];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_hovered = NO;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (id)copyWithZone:(NSZone*)zone
|
2011-05-19 22:04:46 +00:00
|
|
|
{
|
2023-03-02 14:31:23 +00:00
|
|
|
FilePriorityCell* copy = [super copyWithZone:zone];
|
|
|
|
[copy setRepresentedObject:self.representedObject];
|
|
|
|
return copy;
|
2011-05-19 22:04:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setSelected:(BOOL)flag forSegment:(NSInteger)segment
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[super setSelected:flag forSegment:segment];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//only for when clicking manually
|
2008-11-01 15:22:32 +00:00
|
|
|
NSInteger priority;
|
2007-10-20 19:38:10 +00:00
|
|
|
switch (segment)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
case 0:
|
|
|
|
priority = TR_PRI_LOW;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
priority = TR_PRI_NORMAL;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
priority = TR_PRI_HIGH;
|
|
|
|
break;
|
2022-06-07 19:27:40 +00:00
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown segment: %ld", segment);
|
|
|
|
return;
|
2007-10-20 19:38:10 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
FileListNode* node = self.representedObject;
|
|
|
|
Torrent* torrent = node.torrent;
|
|
|
|
[torrent setFilePriority:priority forIndexes:node.indexes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
FileOutlineView* controlView = (FileOutlineView*)self.controlView;
|
2021-08-07 07:27:56 +00:00
|
|
|
controlView.needsDisplay = YES;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)addTrackingAreasForView:(NSView*)controlView
|
|
|
|
inRect:(NSRect)cellFrame
|
|
|
|
withUserInfo:(NSDictionary*)userInfo
|
|
|
|
mouseLocation:(NSPoint)mouseLocation
|
2008-01-12 03:39:26 +00:00
|
|
|
{
|
|
|
|
NSTrackingAreaOptions options = NSTrackingEnabledDuringMouseDrag | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
if (NSMouseInRect(mouseLocation, cellFrame, controlView.flipped))
|
2008-01-12 03:39:26 +00:00
|
|
|
{
|
|
|
|
options |= NSTrackingAssumeInside;
|
2021-08-15 09:41:48 +00:00
|
|
|
[controlView setNeedsDisplayInRect:cellFrame];
|
2008-01-12 03:39:26 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSTrackingArea* area = [[NSTrackingArea alloc] initWithRect:cellFrame options:options owner:controlView userInfo:userInfo];
|
|
|
|
[controlView addTrackingArea:area];
|
2008-01-12 03:39:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
FileListNode* node = self.representedObject;
|
|
|
|
Torrent* torrent = node.torrent;
|
|
|
|
NSSet* priorities = [torrent filePrioritiesForIndexes:node.indexes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSUInteger const count = priorities.count;
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.hovered && count > 0)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[super setSelected:[priorities containsObject:@(TR_PRI_LOW)] forSegment:0];
|
|
|
|
[super setSelected:[priorities containsObject:@(TR_PRI_NORMAL)] forSegment:1];
|
|
|
|
[super setSelected:[priorities containsObject:@(TR_PRI_HIGH)] forSegment:2];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[super drawWithFrame:cellFrame inView:controlView];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableArray* images = [NSMutableArray arrayWithCapacity:MAX(count, 1u)];
|
2011-05-30 00:09:22 +00:00
|
|
|
CGFloat totalWidth;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
if (count == 0)
|
2011-05-30 00:09:22 +00:00
|
|
|
{
|
2021-10-31 15:18:27 +00:00
|
|
|
//if ([self backgroundStyle] != NSBackgroundStyleEmphasized)
|
2011-10-22 20:45:20 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* image = [[NSImage imageNamed:@"PriorityNormalTemplate"] imageWithColor:NSColor.lightGrayColor];
|
|
|
|
[images addObject:image];
|
2021-08-07 07:27:56 +00:00
|
|
|
totalWidth = image.size.width;
|
2011-10-22 20:45:20 +00:00
|
|
|
}
|
2011-05-30 00:09:22 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
else
|
|
|
|
{
|
2021-10-31 15:18:27 +00:00
|
|
|
NSColor* priorityColor = self.backgroundStyle == NSBackgroundStyleEmphasized ? NSColor.whiteColor : NSColor.darkGrayColor;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-05-30 00:09:22 +00:00
|
|
|
totalWidth = 0.0;
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([priorities containsObject:@(TR_PRI_LOW)])
|
2011-05-30 00:09:22 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* image = [[NSImage imageNamed:@"PriorityLowTemplate"] imageWithColor:priorityColor];
|
|
|
|
[images addObject:image];
|
2021-08-07 07:27:56 +00:00
|
|
|
totalWidth += image.size.width;
|
2011-05-30 00:09:22 +00:00
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([priorities containsObject:@(TR_PRI_NORMAL)])
|
2007-10-20 19:25:14 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* image = [[NSImage imageNamed:@"PriorityNormalTemplate"] imageWithColor:priorityColor];
|
|
|
|
[images addObject:image];
|
2021-08-07 07:27:56 +00:00
|
|
|
totalWidth += image.size.width;
|
2011-05-30 00:09:22 +00:00
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([priorities containsObject:@(TR_PRI_HIGH)])
|
2011-05-30 00:09:22 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* image = [[NSImage imageNamed:@"PriorityHighTemplate"] imageWithColor:priorityColor];
|
|
|
|
[images addObject:image];
|
2021-08-07 07:27:56 +00:00
|
|
|
totalWidth += image.size.width;
|
2007-10-20 19:25:14 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-05-30 00:09:22 +00:00
|
|
|
if (count > 1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
totalWidth -= kImageOverlap * (count - 1);
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-05-30 00:09:22 +00:00
|
|
|
CGFloat currentWidth = floor(NSMidX(cellFrame) - totalWidth * 0.5);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSImage* image in images)
|
2009-08-29 23:10:53 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSSize const imageSize = image.size;
|
|
|
|
NSRect const imageRect = NSMakeRect(
|
|
|
|
currentWidth,
|
|
|
|
floor(NSMidY(cellFrame) - imageSize.height * 0.5),
|
|
|
|
imageSize.width,
|
|
|
|
imageSize.height);
|
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
[image drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1.0
|
|
|
|
respectFlipped:YES
|
2021-08-15 09:41:48 +00:00
|
|
|
hints:nil];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
currentWidth += imageSize.width - kImageOverlap;
|
2009-08-29 23:10:53 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|