transmission/macosx/DragOverlayWindow.mm

166 lines
5.0 KiB
Plaintext
Raw Normal View History

// This file Copyright © 2007-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
#import "DragOverlayWindow.h"
#import "DragOverlayView.h"
#import "NSStringAdditions.h"
#include <libtransmission/torrent-metainfo.h>
@interface DragOverlayWindow ()
@property(nonatomic, readonly) NSViewAnimation* fFadeInAnimation;
@property(nonatomic, readonly) NSViewAnimation* fFadeOutAnimation;
@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
{
if ((self = ([super initWithContentRect:window.frame styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered
defer:NO])))
2007-09-16 01:02:06 +00:00
{
self.backgroundColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.5];
self.alphaValue = 0.0;
self.opaque = NO;
self.hasShadow = NO;
DragOverlayView* view = [[DragOverlayView alloc] initWithFrame:self.frame];
self.contentView = view;
self.releasedWhenClosed = NO;
self.ignoresMouseEvents = YES;
_fFadeInAnimation = [[NSViewAnimation alloc] initWithViewAnimations:@[
@{ NSViewAnimationTargetKey : self, NSViewAnimationEffectKey : NSViewAnimationFadeInEffect }
]];
_fFadeInAnimation.duration = 0.15;
_fFadeInAnimation.animationBlockingMode = NSAnimationNonblockingThreaded;
_fFadeOutAnimation = [[NSViewAnimation alloc] initWithViewAnimations:@[
@{ NSViewAnimationTargetKey : self, NSViewAnimationEffectKey : NSViewAnimationFadeOutEffect }
]];
_fFadeOutAnimation.duration = 0.5;
_fFadeOutAnimation.animationBlockingMode = NSAnimationNonblockingThreaded;
[window addChildWindow:self ordered:NSWindowAbove];
2007-09-16 01:02:06 +00:00
}
return self;
}
- (void)dealloc
2007-09-16 01:02:06 +00:00
{
[NSNotificationCenter.defaultCenter removeObserver:self];
2007-09-16 01:02:06 +00:00
}
- (void)setTorrents:(NSArray<NSString*>*)files
2007-09-16 01:02:06 +00:00
{
uint64_t size = 0;
NSUInteger count = 0;
NSString* name;
NSUInteger fileCount = 0;
for (NSString* file in files)
2007-09-16 01:02:06 +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
{
auto metainfo = tr_torrent_metainfo{};
if (metainfo.parseTorrentFile(file.UTF8String))
2007-09-16 01:02:06 +00:00
{
++count;
size += metainfo.totalSize();
auto const n_files = metainfo.fileCount();
fileCount += n_files;
if (n_files == 1)
{
name = @(metainfo.name().c_str());
}
2007-09-16 01:02:06 +00:00
}
}
}
2007-09-16 01:02:06 +00:00
if (count <= 0)
{
2007-09-16 01:02:06 +00:00
return;
}
2007-09-16 01:02:06 +00:00
//set strings and icon
NSString* secondString = [NSString stringForFileSize:size];
if (count > 1)
2007-09-16 01:02:06 +00:00
{
NSString* fileString;
2007-09-16 01:02:06 +00:00
if (fileCount == 1)
{
2008-04-07 13:25:55 +00:00
fileString = NSLocalizedString(@"1 file", "Drag overlay -> torrents");
}
2007-09-16 01:02:06 +00:00
else
{
fileString = [NSString localizedStringWithFormat:NSLocalizedString(@"%lu files", "Drag overlay -> torrents"), fileCount];
}
secondString = [NSString stringWithFormat:@"%@, %@", fileString, secondString];
2007-09-16 01:02:06 +00:00
}
NSImage* icon;
2007-09-16 01:02:06 +00:00
if (count == 1)
{
icon = [NSWorkspace.sharedWorkspace iconForFileType:name ? name.pathExtension : NSFileTypeForHFSTypeCode(kGenericFolderIcon)];
}
2007-09-16 01:02:06 +00:00
else
{
name = [NSString localizedStringWithFormat:NSLocalizedString(@"%lu Torrent Files", "Drag overlay -> torrents"), count];
secondString = [secondString stringByAppendingString:@" total"];
icon = [NSImage imageNamed:@"TransmissionDocument.icns"];
2007-09-16 01:02:06 +00:00
}
[self.contentView setOverlay:icon mainLine:name subLine:secondString];
2007-09-16 01:02:06 +00:00
[self fadeIn];
}
- (void)setFile:(NSString*)file
2007-09-16 01:02:06 +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];
}
- (void)setURL:(NSString*)url
2007-09-16 01:02:06 +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];
}
- (void)fadeIn
2007-09-16 01:02:06 +00:00
{
//stop other animation and set to same progress
if (self.fFadeOutAnimation.animating)
2007-09-16 01:02:06 +00:00
{
[self.fFadeOutAnimation stopAnimation];
self.fFadeInAnimation.currentProgress = 1.0 - self.fFadeOutAnimation.currentProgress;
2007-09-16 01:02:06 +00:00
}
[self.fFadeInAnimation startAnimation];
2007-09-16 01:02:06 +00:00
}
- (void)fadeOut
2007-09-16 01:02:06 +00:00
{
//stop other animation and set to same progress
if (self.fFadeInAnimation.animating)
2007-09-16 01:02:06 +00:00
{
[self.fFadeInAnimation stopAnimation];
self.fFadeOutAnimation.currentProgress = 1.0 - self.fFadeInAnimation.currentProgress;
2007-09-16 01:02:06 +00:00
}
if (self.alphaValue > 0.0)
{
[self.fFadeOutAnimation startAnimation];
}
2007-09-16 01:02:06 +00:00
}
@end