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
|
|
|
|
|
|
|
#import "DragOverlayWindow.h"
|
|
|
|
#import "DragOverlayView.h"
|
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
#include <libtransmission/torrent-metainfo.h>
|
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface DragOverlayWindow ()
|
2010-01-28 03:16:55 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic, readonly) NSViewAnimation* fFadeInAnimation;
|
|
|
|
@property(nonatomic, readonly) NSViewAnimation* fFadeOutAnimation;
|
|
|
|
|
2010-01-28 03:16:55 +00:00
|
|
|
@end
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@implementation DragOverlayWindow
|
|
|
|
|
2022-12-21 20:21:16 +00:00
|
|
|
- (instancetype)initForWindow:(NSWindow*)window
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-03-30 21:52:23 +00:00
|
|
|
if ((self = ([super initWithContentRect:window.frame styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered
|
2021-10-31 15:18:27 +00:00
|
|
|
defer:NO])))
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
self.backgroundColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.5];
|
2021-08-07 07:27:56 +00:00
|
|
|
self.alphaValue = 0.0;
|
|
|
|
self.opaque = NO;
|
|
|
|
self.hasShadow = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
DragOverlayView* view = [[DragOverlayView alloc] initWithFrame:self.frame];
|
2021-08-07 07:27:56 +00:00
|
|
|
self.contentView = view;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.releasedWhenClosed = NO;
|
|
|
|
self.ignoresMouseEvents = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fFadeInAnimation = [[NSViewAnimation alloc] initWithViewAnimations:@[
|
2021-08-15 09:41:48 +00:00
|
|
|
@{ NSViewAnimationTargetKey : self, NSViewAnimationEffectKey : NSViewAnimationFadeInEffect }
|
|
|
|
]];
|
2022-02-22 16:04:20 +00:00
|
|
|
_fFadeInAnimation.duration = 0.15;
|
|
|
|
_fFadeInAnimation.animationBlockingMode = NSAnimationNonblockingThreaded;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fFadeOutAnimation = [[NSViewAnimation alloc] initWithViewAnimations:@[
|
2021-08-15 09:41:48 +00:00
|
|
|
@{ NSViewAnimationTargetKey : self, NSViewAnimationEffectKey : NSViewAnimationFadeOutEffect }
|
|
|
|
]];
|
2022-02-22 16:04:20 +00:00
|
|
|
_fFadeOutAnimation.duration = 0.5;
|
|
|
|
_fFadeOutAnimation.animationBlockingMode = NSAnimationNonblockingThreaded;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[window addChildWindow:self ordered:NSWindowAbove];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-11-23 23:09:32 +00:00
|
|
|
- (void)setTorrents:(NSArray<NSString*>*)files
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
uint64_t size = 0;
|
2022-05-14 05:31:24 +00:00
|
|
|
NSUInteger count = 0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* name;
|
2022-05-14 05:31:24 +00:00
|
|
|
NSUInteger fileCount = 0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSString* file in files)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([[NSWorkspace.sharedWorkspace typeOfFile:file error:NULL] isEqualToString:@"org.bittorrent.torrent"] ||
|
|
|
|
[file.pathExtension caseInsensitiveCompare:@"torrent"] == NSOrderedSame)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-12-26 03:12:32 +00:00
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
2023-04-23 01:25:55 +00:00
|
|
|
if (metainfo.parse_torrent_file(file.UTF8String))
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-12-26 03:12:32 +00:00
|
|
|
++count;
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
size += metainfo.total_size();
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const n_files = metainfo.file_count();
|
2021-12-26 03:12:32 +00:00
|
|
|
fileCount += n_files;
|
2022-12-23 02:07:45 +00:00
|
|
|
// only useful when one torrent
|
|
|
|
if (count == 1)
|
2007-12-22 03:31:22 +00:00
|
|
|
{
|
2022-12-23 02:07:45 +00:00
|
|
|
if (n_files == 1)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
name = [NSString convertedStringFromCString:metainfo.file_subpath(0).c_str()];
|
2022-12-23 02:07:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
name = @(metainfo.name().c_str());
|
|
|
|
}
|
2007-12-22 03:31:22 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
if (count <= 0)
|
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
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//set strings and icon
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* secondString = [NSString stringForFileSize:size];
|
2021-12-26 03:12:32 +00:00
|
|
|
if (count > 1)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* fileString;
|
2007-09-16 01:02:06 +00:00
|
|
|
if (fileCount == 1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-04-07 13:25:55 +00:00
|
|
|
fileString = NSLocalizedString(@"1 file", "Drag overlay -> torrents");
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-11-14 18:30:03 +00:00
|
|
|
fileString = [NSString localizedStringWithFormat:NSLocalizedString(@"%lu files", "Drag overlay -> torrents"), fileCount];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
|
|
|
secondString = [NSString stringWithFormat:@"%@, %@", fileString, secondString];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* icon;
|
2007-09-16 01:02:06 +00:00
|
|
|
if (count == 1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-12-23 02:07:45 +00:00
|
|
|
icon = [NSWorkspace.sharedWorkspace
|
|
|
|
iconForFileType:fileCount <= 1 ? name.pathExtension : NSFileTypeForHFSTypeCode(kGenericFolderIcon)];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
else
|
|
|
|
{
|
2022-11-14 18:30:03 +00:00
|
|
|
name = [NSString localizedStringWithFormat:NSLocalizedString(@"%lu Torrent Files", "Drag overlay -> torrents"), count];
|
2021-08-15 09:41:48 +00:00
|
|
|
secondString = [secondString stringByAppendingString:@" total"];
|
|
|
|
icon = [NSImage imageNamed:@"TransmissionDocument.icns"];
|
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.contentView setOverlay:icon mainLine:name subLine:secondString];
|
2007-09-16 01:02:06 +00:00
|
|
|
[self fadeIn];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setFile:(NSString*)file
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[self.contentView setOverlay:[NSImage imageNamed:@"CreateLarge"]
|
|
|
|
mainLine:NSLocalizedString(@"Create a Torrent File", "Drag overlay -> file")
|
|
|
|
subLine:file];
|
2007-09-16 01:02:06 +00:00
|
|
|
[self fadeIn];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setURL:(NSString*)url
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[self.contentView setOverlay:[NSImage imageNamed:@"Globe"] mainLine:NSLocalizedString(@"Web Address", "Drag overlay -> url")
|
|
|
|
subLine:url];
|
2007-09-16 01:02:06 +00:00
|
|
|
[self fadeIn];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)fadeIn
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
//stop other animation and set to same progress
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fFadeOutAnimation.animating)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fFadeOutAnimation stopAnimation];
|
|
|
|
self.fFadeInAnimation.currentProgress = 1.0 - self.fFadeOutAnimation.currentProgress;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fFadeInAnimation startAnimation];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)fadeOut
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
//stop other animation and set to same progress
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fFadeInAnimation.animating)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fFadeInAnimation stopAnimation];
|
|
|
|
self.fFadeOutAnimation.currentProgress = 1.0 - self.fFadeInAnimation.currentProgress;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2021-08-07 07:27:56 +00:00
|
|
|
if (self.alphaValue > 0.0)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fFadeOutAnimation startAnimation];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2010-01-28 03:16:55 +00:00
|
|
|
@end
|