-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNotificationView.m
More file actions
154 lines (128 loc) · 4 KB
/
Copy pathNotificationView.m
File metadata and controls
154 lines (128 loc) · 4 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// NotificationView.m
// FreeMan
//
// Created by Lin Fan on 3/12/12.
// Copyright (c) 2012 Galaworks Studio. All rights reserved.
//
#import "NotificationView.h"
#define MARGIN 12
#define ICON_SIZE 48
#define ICON_SPACE 12
#define TITLE_FONT 12
#define MESSAGE_FONT 12
#define TEXT_SPACE 8
@implementation NotificationView
- (void)dealloc
{
[icon release];
[title release];
[message release];
[super dealloc];
}
- (void)setIcon:(NSImage *)aIcon withTitle:(NSString *)aTitle andMessage:(NSString *)aMessage
{
if (icon != aIcon)
{
[icon release];
icon = [aIcon retain];
}
if (message != aMessage)
{
[message release];
message = [aMessage retain];
}
if (title != aTitle)
{
[title release];
title = [aTitle retain];
}
[self setNeedsDisplay:YES];
}
- (CGFloat)heightOfText:(NSString *)text withFont:(NSFont *)font inMaxWidth:(CGFloat)maxWidth
{
if (!text || [text length] == 0)
{
return 0;
}
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:text];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
[textStorage setFont:font];
[textContainer setContainerSize:NSMakeSize(maxWidth, FLT_MAX)];
[layoutManager glyphRangeForTextContainer:textContainer];
NSSize size = [layoutManager usedRectForTextContainer:textContainer].size;
[textStorage release];
[layoutManager release];
[textContainer release];
return size.height;
}
- (CGFloat)textHeight
{
float textWidth = NSWidth([self bounds]) - MARGIN * 2 - ICON_SIZE - ICON_SPACE;
float titleHeight = [self heightOfText:title
withFont:[NSFont boldSystemFontOfSize:TITLE_FONT]
inMaxWidth:textWidth];
float messageHeight = [self heightOfText:message
withFont:[NSFont systemFontOfSize:MESSAGE_FONT]
inMaxWidth:textWidth];
return titleHeight + messageHeight + TEXT_SPACE;
}
- (NSSize)drawText:(NSString *)text withFont:(NSFont *)font atPoint:(NSPoint)point
{
if (!text || [text length] == 0)
{
return NSZeroSize;
}
float textWidth = NSWidth([self bounds]) - MARGIN * 2 - ICON_SIZE - ICON_SPACE;
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:text];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
[textStorage setFont:font];
[textStorage setForegroundColor:[NSColor whiteColor]];
[textContainer setContainerSize:NSMakeSize(textWidth, FLT_MAX)];
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:point];
NSSize size = [layoutManager usedRectForTextContainer:textContainer].size;
[textStorage release];
[layoutManager release];
[textContainer release];
return size;
}
- (BOOL)isFlipped
{
return YES;
}
- (void)drawRect:(NSRect)rect
{
rect = [self bounds];
// fill background
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:8 yRadius:8];
[[NSColor colorWithCalibratedWhite:0.145 alpha:0.608] setFill];
[path fill];
// stroke border
[[NSColor whiteColor] setStroke];
[path setLineWidth:2.0];
[path setClip];
[path stroke];
// draw icon
[icon setFlipped:YES];
NSRect iconRect = NSMakeRect(MARGIN, (NSHeight(rect) - ICON_SIZE) / 2, ICON_SIZE, ICON_SIZE);
[icon drawInRect:iconRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[icon setFlipped:NO];
// calculate
NSPoint point = NSMakePoint(MARGIN + ICON_SIZE + ICON_SPACE,
(NSHeight(rect) - [self textHeight]) / 2.0);
// draw title
NSSize titleSize = [self drawText:title
withFont:[NSFont boldSystemFontOfSize:TITLE_FONT]
atPoint:point];
// draw message
point.y += (titleSize.height + TEXT_SPACE);
[self drawText:message withFont:[NSFont systemFontOfSize:MESSAGE_FONT] atPoint:point];
}
@end