2022-01-20 18:27:56 +00:00
|
|
|
// 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 "DragOverlayView.h"
|
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static CGFloat const kPadding = 10.0;
|
|
|
|
static CGFloat const kIconWidth = 64.0;
|
2007-10-13 13:54:19 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@interface DragOverlayView ()
|
|
|
|
|
|
|
|
@property(nonatomic) NSImage* fBadge;
|
|
|
|
|
|
|
|
@property(nonatomic, readonly) NSDictionary* fMainLineAttributes;
|
|
|
|
@property(nonatomic, readonly) NSDictionary* fSubLineAttributes;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@implementation DragOverlayView
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithFrame:(NSRect)frame
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((self = [super initWithFrame:frame]))
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
//create attributes
|
2021-08-15 09:41:48 +00:00
|
|
|
NSShadow* stringShadow = [[NSShadow alloc] init];
|
2021-08-07 07:27:56 +00:00
|
|
|
stringShadow.shadowOffset = NSMakeSize(2.0, -2.0);
|
|
|
|
stringShadow.shadowBlurRadius = 4.0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSFont *bigFont = [NSFont boldSystemFontOfSize:18.0], *smallFont = [NSFont systemFontOfSize:14.0];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableParagraphStyle* paragraphStyle = [NSParagraphStyle.defaultParagraphStyle mutableCopy];
|
2021-08-07 07:27:56 +00:00
|
|
|
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingMiddle;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fMainLineAttributes = @{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSForegroundColorAttributeName : NSColor.whiteColor,
|
|
|
|
NSFontAttributeName : bigFont,
|
|
|
|
NSShadowAttributeName : stringShadow,
|
|
|
|
NSParagraphStyleAttributeName : paragraphStyle
|
|
|
|
};
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fSubLineAttributes = @{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSForegroundColorAttributeName : NSColor.whiteColor,
|
|
|
|
NSFontAttributeName : smallFont,
|
|
|
|
NSShadowAttributeName : stringShadow,
|
|
|
|
NSParagraphStyleAttributeName : paragraphStyle
|
|
|
|
};
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setOverlay:(NSImage*)icon mainLine:(NSString*)mainLine subLine:(NSString*)subLine
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2010-01-28 03:36:47 +00:00
|
|
|
//create badge
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRect const badgeRect = NSMakeRect(0.0, 0.0, 325.0, 84.0);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fBadge = [[NSImage alloc] initWithSize:badgeRect.size];
|
|
|
|
[self.fBadge lockFocus];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSBezierPath* bp = [NSBezierPath bezierPathWithRoundedRect:badgeRect xRadius:15.0 yRadius:15.0];
|
|
|
|
[[NSColor colorWithCalibratedWhite:0.0 alpha:0.75] set];
|
2010-01-28 03:36:47 +00:00
|
|
|
[bp fill];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-12-04 16:47:00 +00:00
|
|
|
//place icon
|
2022-10-19 19:28:21 +00:00
|
|
|
[icon drawInRect:NSMakeRect(kPadding, (NSHeight(badgeRect) - kIconWidth) * 0.5, kIconWidth, kIconWidth) fromRect:NSZeroRect
|
2021-10-31 15:18:27 +00:00
|
|
|
operation:NSCompositingOperationSourceOver
|
2021-08-15 09:41:48 +00:00
|
|
|
fraction:1.0];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//place main text
|
2022-02-22 16:04:20 +00:00
|
|
|
NSSize const mainLineSize = [mainLine sizeWithAttributes:self.fMainLineAttributes];
|
|
|
|
NSSize const subLineSize = [subLine sizeWithAttributes:self.fSubLineAttributes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRect lineRect = NSMakeRect(
|
2022-10-19 19:28:21 +00:00
|
|
|
kPadding + kIconWidth + 5.0,
|
2021-08-15 09:41:48 +00:00
|
|
|
(NSHeight(badgeRect) + (subLineSize.height + 2.0 - mainLineSize.height)) * 0.5,
|
2022-10-19 19:28:21 +00:00
|
|
|
NSWidth(badgeRect) - (kPadding + kIconWidth + 2.0) - kPadding,
|
2021-08-15 09:41:48 +00:00
|
|
|
mainLineSize.height);
|
2022-02-22 16:04:20 +00:00
|
|
|
[mainLine drawInRect:lineRect withAttributes:self.fMainLineAttributes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//place sub text
|
2010-01-28 03:36:47 +00:00
|
|
|
lineRect.origin.y -= subLineSize.height + 2.0;
|
2007-09-16 01:02:06 +00:00
|
|
|
lineRect.size.height = subLineSize.height;
|
2022-02-22 16:04:20 +00:00
|
|
|
[subLine drawInRect:lineRect withAttributes:self.fSubLineAttributes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fBadge unlockFocus];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.needsDisplay = YES;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)drawRect:(NSRect)rect
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fBadge)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRect const frame = self.frame;
|
2022-02-22 16:04:20 +00:00
|
|
|
NSSize const imageSize = self.fBadge.size;
|
|
|
|
[self.fBadge drawAtPoint:NSMakePoint((NSWidth(frame) - imageSize.width) * 0.5, (NSHeight(frame) - imageSize.height) * 0.5)
|
2022-03-30 21:52:23 +00:00
|
|
|
fromRect:NSZeroRect
|
|
|
|
operation:NSCompositingOperationSourceOver
|
|
|
|
fraction:1.0];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|