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>
|
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "FileNameCell.h"
|
2007-10-20 19:25:14 +00:00
|
|
|
#import "FileOutlineView.h"
|
2007-10-20 19:13:52 +00:00
|
|
|
#import "Torrent.h"
|
2008-05-22 18:39:49 +00:00
|
|
|
#import "FileListNode.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static CGFloat const kPaddingHorizontal = 2.0;
|
|
|
|
static CGFloat const kImageFolderSize = 16.0;
|
|
|
|
static CGFloat const kImageIconSize = 32.0;
|
|
|
|
static CGFloat const kPaddingBetweenImageAndTitle = 4.0;
|
|
|
|
static CGFloat const kPaddingAboveTitleFile = 2.0;
|
|
|
|
static CGFloat const kPaddingBelowStatusFile = 2.0;
|
|
|
|
static CGFloat const kPaddingBetweenNameAndFolderStatus = 4.0;
|
|
|
|
static CGFloat const kPaddingExpansionFrame = 2.0;
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2023-06-12 16:03:22 +00:00
|
|
|
static NSMutableParagraphStyle* sParagraphStyle()
|
2008-01-07 00:07:03 +00:00
|
|
|
{
|
2023-06-12 16:03:22 +00:00
|
|
|
NSMutableParagraphStyle* paragraphStyle = [NSParagraphStyle.defaultParagraphStyle mutableCopy];
|
|
|
|
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingMiddle;
|
|
|
|
return paragraphStyle;
|
2008-01-07 00:07:03 +00:00
|
|
|
}
|
2023-06-12 16:03:22 +00:00
|
|
|
static NSMutableParagraphStyle* sStatusParagraphStyle()
|
2012-03-13 02:52:11 +00:00
|
|
|
{
|
2023-06-12 16:03:22 +00:00
|
|
|
NSMutableParagraphStyle* paragraphStyle = [NSParagraphStyle.defaultParagraphStyle mutableCopy];
|
|
|
|
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
|
|
return paragraphStyle;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-06-12 16:03:22 +00:00
|
|
|
static NSDictionary<NSAttributedStringKey, id>* const kTitleAttributes = @{
|
|
|
|
NSFontAttributeName : [NSFont messageFontOfSize:12.0],
|
|
|
|
NSParagraphStyleAttributeName : sParagraphStyle(),
|
|
|
|
NSForegroundColorAttributeName : NSColor.controlTextColor
|
|
|
|
};
|
|
|
|
static NSDictionary<NSAttributedStringKey, id>* const kStatusAttributes = @{
|
|
|
|
NSFontAttributeName : [NSFont messageFontOfSize:9.0],
|
|
|
|
NSParagraphStyleAttributeName : sStatusParagraphStyle(),
|
|
|
|
NSForegroundColorAttributeName : NSColor.secondaryLabelColor
|
|
|
|
};
|
|
|
|
static NSDictionary<NSAttributedStringKey, id>* const kTitleEmphasizedAttributes = @{
|
|
|
|
NSFontAttributeName : [NSFont messageFontOfSize:12.0],
|
|
|
|
NSParagraphStyleAttributeName : sParagraphStyle(),
|
|
|
|
NSForegroundColorAttributeName : NSColor.whiteColor
|
|
|
|
};
|
|
|
|
static NSDictionary<NSAttributedStringKey, id>* const kStatusEmphasizedAttributes = @{
|
|
|
|
NSFontAttributeName : [NSFont messageFontOfSize:9.0],
|
|
|
|
NSParagraphStyleAttributeName : sStatusParagraphStyle(),
|
|
|
|
NSForegroundColorAttributeName : NSColor.whiteColor
|
|
|
|
};
|
|
|
|
static NSDictionary<NSAttributedStringKey, id>* const kTitleDisabledAttributes = @{
|
|
|
|
NSFontAttributeName : [NSFont messageFontOfSize:12.0],
|
|
|
|
NSParagraphStyleAttributeName : sParagraphStyle(),
|
|
|
|
NSForegroundColorAttributeName : NSColor.disabledControlTextColor
|
|
|
|
};
|
|
|
|
static NSDictionary<NSAttributedStringKey, id>* const kStatusDisabledAttributes = @{
|
|
|
|
NSFontAttributeName : [NSFont messageFontOfSize:9.0],
|
|
|
|
NSParagraphStyleAttributeName : sStatusParagraphStyle(),
|
|
|
|
NSForegroundColorAttributeName : NSColor.disabledControlTextColor
|
|
|
|
};
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-06-12 16:03:22 +00:00
|
|
|
@implementation FileNameCell
|
2012-03-13 02:52:11 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSImage*)image
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
FileListNode* node = (FileListNode*)self.objectValue;
|
2021-08-07 07:27:56 +00:00
|
|
|
return node.icon;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSRect)imageRectForBounds:(NSRect)bounds
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
NSRect result = bounds;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
result.origin.x += kPaddingHorizontal;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
CGFloat const IMAGE_SIZE = ((FileListNode*)self.objectValue).isFolder ? kImageFolderSize : kImageIconSize;
|
2009-12-31 03:19:54 +00:00
|
|
|
result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5;
|
2007-09-16 01:02:06 +00:00
|
|
|
result.size = NSMakeSize(IMAGE_SIZE, IMAGE_SIZE);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
//icon
|
2022-03-30 21:52:23 +00:00
|
|
|
[self.image drawInRect:[self imageRectForBounds:cellFrame] fromRect:NSZeroRect operation:NSCompositingOperationSourceOver
|
|
|
|
fraction:1.0
|
2021-08-15 09:41:48 +00:00
|
|
|
respectFlipped:YES
|
|
|
|
hints:nil];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
FileListNode* node = self.objectValue;
|
2023-06-12 16:03:22 +00:00
|
|
|
AttributesStyle style;
|
2021-10-31 15:18:27 +00:00
|
|
|
if (self.backgroundStyle == NSBackgroundStyleEmphasized)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2023-06-12 16:03:22 +00:00
|
|
|
style = AttributesStyleEmphasized;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2021-10-31 15:18:27 +00:00
|
|
|
else if ([node.torrent checkForFiles:node.indexes] == NSControlStateValueOff)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2023-06-12 16:03:22 +00:00
|
|
|
style = AttributesStyleDisabled;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2009-01-02 01:10:54 +00:00
|
|
|
else
|
|
|
|
{
|
2023-06-12 16:03:22 +00:00
|
|
|
style = AttributesStyleNormal;
|
2009-01-02 01:10:54 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-01-02 01:10:54 +00:00
|
|
|
//title
|
2023-06-12 16:03:22 +00:00
|
|
|
NSAttributedString* titleString = [self attributedTitleWithStyle:style];
|
|
|
|
NSRect titleRect = [self rectForTitleWithStringSize:[titleString size] inBounds:cellFrame];
|
2021-08-15 09:41:48 +00:00
|
|
|
[titleString drawInRect:titleRect];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//status
|
2023-06-12 16:03:22 +00:00
|
|
|
NSAttributedString* statusString = [self attributedStatusWithStyle:style];
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRect statusRect = [self rectForStatusWithString:statusString withTitleRect:titleRect inBounds:cellFrame];
|
|
|
|
[statusString drawInRect:statusRect];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSRect)expansionFrameWithFrame:(NSRect)cellFrame inView:(NSView*)view
|
2012-10-29 22:17:08 +00:00
|
|
|
{
|
2023-06-12 16:03:22 +00:00
|
|
|
NSAttributedString* titleString = [self attributedTitleWithStyle:AttributesStyleNormal];
|
|
|
|
NSRect realRect = [self rectForTitleWithStringSize:[titleString size] inBounds:cellFrame];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([titleString size].width > NSWidth(realRect) &&
|
|
|
|
NSMouseInRect([view convertPoint:view.window.mouseLocationOutsideOfEventStream fromView:nil], realRect, view.flipped))
|
2012-10-29 22:17:08 +00:00
|
|
|
{
|
|
|
|
realRect.size.width = [titleString size].width;
|
2022-10-19 19:28:21 +00:00
|
|
|
return NSInsetRect(realRect, -kPaddingExpansionFrame, -kPaddingExpansionFrame);
|
2012-10-29 22:17:08 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-10-29 22:17:08 +00:00
|
|
|
return NSZeroRect;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)drawWithExpansionFrame:(NSRect)cellFrame inView:(NSView*)view
|
2012-10-29 22:17:08 +00:00
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
cellFrame.origin.x += kPaddingExpansionFrame;
|
|
|
|
cellFrame.origin.y += kPaddingExpansionFrame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-06-12 16:03:22 +00:00
|
|
|
NSAttributedString* titleString = [self attributedTitleWithStyle:AttributesStyleNormal];
|
2021-08-15 09:41:48 +00:00
|
|
|
[titleString drawInRect:cellFrame];
|
2012-10-29 22:17:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2023-06-12 16:03:22 +00:00
|
|
|
- (NSRect)rectForTitleWithStringSize:(NSSize)stringSize inBounds:(NSRect)bounds
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2023-06-12 16:03:22 +00:00
|
|
|
NSSize const titleSize = stringSize;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-10-29 00:04:18 +00:00
|
|
|
//no right padding, so that there's not too much space between this and the priority image
|
2009-12-31 03:19:54 +00:00
|
|
|
NSRect result;
|
2021-08-15 09:41:48 +00:00
|
|
|
if (!((FileListNode*)self.objectValue).isFolder)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
result.origin.x = NSMinX(bounds) + kPaddingHorizontal + kImageIconSize + kPaddingBetweenImageAndTitle;
|
|
|
|
result.origin.y = NSMinY(bounds) + kPaddingAboveTitleFile;
|
2012-10-29 00:04:18 +00:00
|
|
|
result.size.width = NSMaxX(bounds) - NSMinX(result);
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
result.origin.x = NSMinX(bounds) + kPaddingHorizontal + kImageFolderSize + kPaddingBetweenImageAndTitle;
|
2009-12-31 03:19:54 +00:00
|
|
|
result.origin.y = NSMidY(bounds) - titleSize.height * 0.5;
|
2012-10-29 00:04:18 +00:00
|
|
|
result.size.width = MIN(titleSize.width, NSMaxX(bounds) - NSMinX(result));
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2009-12-31 03:19:54 +00:00
|
|
|
result.size.height = titleSize.height;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSRect)rectForStatusWithString:(NSAttributedString*)string withTitleRect:(NSRect)titleRect inBounds:(NSRect)bounds
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSSize const statusSize = [string size];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-10-12 13:22:13 +00:00
|
|
|
NSRect result;
|
2021-08-15 09:41:48 +00:00
|
|
|
if (!((FileListNode*)self.objectValue).isFolder)
|
2008-10-12 13:22:13 +00:00
|
|
|
{
|
2010-07-08 01:49:41 +00:00
|
|
|
result.origin.x = NSMinX(titleRect);
|
2022-10-19 19:28:21 +00:00
|
|
|
result.origin.y = NSMaxY(bounds) - kPaddingBelowStatusFile - statusSize.height;
|
2010-07-08 01:49:41 +00:00
|
|
|
result.size.width = NSWidth(titleRect);
|
2008-10-12 13:22:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
result.origin.x = NSMaxX(titleRect) + kPaddingBetweenNameAndFolderStatus;
|
2009-12-31 03:19:54 +00:00
|
|
|
result.origin.y = NSMaxY(titleRect) - statusSize.height - 1.0;
|
2012-10-29 00:04:18 +00:00
|
|
|
result.size.width = NSMaxX(bounds) - NSMaxX(titleRect);
|
2008-10-12 13:22:13 +00:00
|
|
|
}
|
2009-12-31 03:19:54 +00:00
|
|
|
result.size.height = statusSize.height;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-06-12 16:03:22 +00:00
|
|
|
- (NSAttributedString*)attributedTitleWithStyle:(AttributesStyle)style
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* title = ((FileListNode*)self.objectValue).name;
|
2023-06-12 16:03:22 +00:00
|
|
|
return [[NSAttributedString alloc] initWithString:title attributes:style == AttributesStyleEmphasized ? kTitleEmphasizedAttributes :
|
|
|
|
style == AttributesStyleDisabled ? kTitleDisabledAttributes :
|
|
|
|
kTitleAttributes];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 16:03:22 +00:00
|
|
|
- (NSAttributedString*)attributedStatusWithStyle:(AttributesStyle)style
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
FileListNode* node = (FileListNode*)self.objectValue;
|
|
|
|
Torrent* torrent = node.torrent;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const progress = [torrent fileProgress:node];
|
|
|
|
NSString* percentString = [NSString percentString:progress longDecimals:YES];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* status = [NSString stringWithFormat:NSLocalizedString(@"%@ of %@", "Inspector -> Files tab -> file status string"),
|
|
|
|
percentString,
|
|
|
|
[NSString stringForFileSize:node.size]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-06-12 16:03:22 +00:00
|
|
|
return [[NSAttributedString alloc] initWithString:status attributes:style == AttributesStyleEmphasized ? kStatusEmphasizedAttributes :
|
|
|
|
style == AttributesStyleDisabled ? kStatusDisabledAttributes :
|
|
|
|
kStatusAttributes];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|