-
Notifications
You must be signed in to change notification settings - Fork 5
/
DragOverlayView.m
140 lines (111 loc) · 5.38 KB
/
DragOverlayView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/******************************************************************************
* $Id: DragOverlayView.m 9844 2010-01-01 21:12:04Z livings124 $
*
* Copyright (c) 2007-2010 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#import "DragOverlayView.h"
#define PADDING 10.0f
#define ICON_WIDTH 64.0f
@implementation DragOverlayView
- (id) initWithFrame: (NSRect) frame
{
if ((self = [super initWithFrame: frame]))
{
//create badge
NSRect badgeRect = NSMakeRect(0.0f, 0.0f, 325.0f, 84.0f);
NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: badgeRect xRadius: 15.0f yRadius: 15.0f];
fBackBadge = [[NSImage alloc] initWithSize: badgeRect.size];
[fBackBadge lockFocus];
[[NSColor colorWithCalibratedWhite: 0.0f alpha: 0.75f] set];
[bp fill];
[fBackBadge unlockFocus];
//create attributes
NSShadow * stringShadow = [[NSShadow alloc] init];
[stringShadow setShadowOffset: NSMakeSize(2.0f, -2.0f)];
[stringShadow setShadowBlurRadius: 4.0f];
NSFont * bigFont = [[NSFontManager sharedFontManager] convertFont:
[NSFont fontWithName: @"Lucida Grande" size: 18.0f] toHaveTrait: NSBoldFontMask],
* smallFont = [NSFont fontWithName: @"Lucida Grande" size: 14.0f];
NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
fMainLineAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSColor whiteColor], NSForegroundColorAttributeName,
bigFont, NSFontAttributeName, stringShadow, NSShadowAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil];
fSubLineAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSColor whiteColor], NSForegroundColorAttributeName,
smallFont, NSFontAttributeName, stringShadow, NSShadowAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil];
[stringShadow release];
[paragraphStyle release];
_imageView = [[NSImageView alloc] init];
}
return self;
}
- (void) dealloc
{
[fBackBadge release];
[fBadge release];
[fMainLineAttributes release];
[fSubLineAttributes release];
[_imageView release];
[super dealloc];
}
- (void) setOverlay: (NSImage *) icon mainLine: (NSString *) mainLine subLine: (NSString *) subLine
{
[fBadge release];
fBadge = [fBackBadge copy];
NSSize badgeSize = [fBadge size];
[fBadge lockFocus];
//place icon
[_imageView setImage:icon];
[_imageView setAnimates:YES];
//place main text
NSSize mainLineSize = [mainLine sizeWithAttributes: fMainLineAttributes];
NSSize subLineSize = subLine?[subLine sizeWithAttributes: fSubLineAttributes]:NSSizeFromString(@"{0,0}");
NSRect lineRect = NSMakeRect(PADDING + ICON_WIDTH + 5.0f,
(badgeSize.height + (subLineSize.height + 2.0f - mainLineSize.height)) * 0.5f,
badgeSize.width - (PADDING + ICON_WIDTH + 2.0f) - PADDING, mainLineSize.height);
[mainLine drawInRect: lineRect withAttributes: fMainLineAttributes];
//place sub text
if (subLine)
{
lineRect.origin.y -= subLineSize.height + 2.0f;
lineRect.size.height = subLineSize.height;
[subLine drawInRect: lineRect withAttributes: fSubLineAttributes];
}
[fBadge unlockFocus];
[self setNeedsDisplay: YES];
}
-(void) drawRect: (NSRect) rect
{
if (fBadge)
{
NSRect frame = [self frame];
NSSize imageSize = [fBadge size];
[fBadge compositeToPoint: NSMakePoint((frame.size.width - imageSize.width) * 0.5f,
(frame.size.height - imageSize.height) * 0.5f) operation: NSCompositeSourceOver];
NSRect iconFrame = NSMakeRect((frame.size.width - imageSize.width) * 0.5f+PADDING, (frame.size.height - imageSize.height) * 0.5f+(imageSize.height - ICON_WIDTH) * 0.5f, ICON_WIDTH, ICON_WIDTH);
[_imageView setFrame:iconFrame];
[self addSubview:_imageView];
}
}
@end