1 | /****************************************************************************** |
---|
2 | * $Id: Badger.m 1055 2006-11-05 20:35:00Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "Badger.h" |
---|
26 | #import "StringAdditions.h" |
---|
27 | |
---|
28 | @interface Badger (Private) |
---|
29 | |
---|
30 | - (void) badgeString: (NSString *) string forRect: (NSRect) rect; |
---|
31 | |
---|
32 | @end |
---|
33 | |
---|
34 | @implementation Badger |
---|
35 | |
---|
36 | - (id) init |
---|
37 | { |
---|
38 | if ((self = [super init])) |
---|
39 | { |
---|
40 | fBadge = [NSImage imageNamed: @"Badge"]; |
---|
41 | fDockIcon = [[NSApp applicationIconImage] copy]; |
---|
42 | fUploadBadge = [NSImage imageNamed: @"UploadBadge"]; |
---|
43 | fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"]; |
---|
44 | |
---|
45 | NSShadow * stringShadow = [[NSShadow alloc] init]; |
---|
46 | [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)]; |
---|
47 | [stringShadow setShadowBlurRadius: 4.0]; |
---|
48 | |
---|
49 | NSFont * boldFont = [[NSFontManager sharedFontManager] convertFont: |
---|
50 | [NSFont fontWithName: @"Helvetica" size: 28.0] toHaveTrait: NSBoldFontMask]; |
---|
51 | |
---|
52 | fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
53 | [NSColor whiteColor], NSForegroundColorAttributeName, |
---|
54 | boldFont, NSFontAttributeName, stringShadow, NSShadowAttributeName, nil]; |
---|
55 | |
---|
56 | [stringShadow release]; |
---|
57 | |
---|
58 | fNonDefault = NO; |
---|
59 | } |
---|
60 | |
---|
61 | return self; |
---|
62 | } |
---|
63 | |
---|
64 | - (void) dealloc |
---|
65 | { |
---|
66 | [fDockIcon release]; |
---|
67 | [fAttributes release]; |
---|
68 | |
---|
69 | [super dealloc]; |
---|
70 | } |
---|
71 | |
---|
72 | - (void) updateBadgeWithCompleted: (int) completed uploadRate: (float) uploadRate downloadRate: (float) downloadRate |
---|
73 | { |
---|
74 | NSSize iconSize = [fDockIcon size]; |
---|
75 | |
---|
76 | NSImage * dockIcon = nil; |
---|
77 | |
---|
78 | //set completed badge to top right |
---|
79 | if (completed > 0) |
---|
80 | { |
---|
81 | dockIcon = [fDockIcon copy]; |
---|
82 | |
---|
83 | NSRect badgeRect; |
---|
84 | badgeRect.size = [fBadge size]; |
---|
85 | badgeRect.origin.x = iconSize.width - badgeRect.size.width; |
---|
86 | badgeRect.origin.y = iconSize.height - badgeRect.size.height; |
---|
87 | |
---|
88 | [dockIcon lockFocus]; |
---|
89 | |
---|
90 | //place badge |
---|
91 | [fBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver]; |
---|
92 | |
---|
93 | //ignore shadow of badge when placing string |
---|
94 | float badgeBottomExtra = 5.0; |
---|
95 | badgeRect.size.height -= badgeBottomExtra; |
---|
96 | badgeRect.origin.y += badgeBottomExtra; |
---|
97 | |
---|
98 | //place badge text |
---|
99 | [self badgeString: [NSString stringWithInt: completed] forRect: badgeRect]; |
---|
100 | |
---|
101 | [dockIcon unlockFocus]; |
---|
102 | } |
---|
103 | |
---|
104 | //set upload and download rate badges |
---|
105 | NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; |
---|
106 | NSString * uploadRateString = uploadRate >= 0.1 && [defaults boolForKey: @"BadgeUploadRate"] |
---|
107 | ? [NSString stringForSpeedAbbrev: uploadRate] : nil, |
---|
108 | * downloadRateString = downloadRate >= 0.1 && [defaults boolForKey: @"BadgeDownloadRate"] |
---|
109 | ? [NSString stringForSpeedAbbrev: downloadRate] : nil; |
---|
110 | |
---|
111 | BOOL speedShown = uploadRateString || downloadRateString; |
---|
112 | if (speedShown) |
---|
113 | { |
---|
114 | if (!dockIcon) |
---|
115 | dockIcon = [fDockIcon copy]; |
---|
116 | |
---|
117 | NSRect badgeRect; |
---|
118 | badgeRect.size = [fUploadBadge size]; |
---|
119 | badgeRect.origin = NSZeroPoint; |
---|
120 | |
---|
121 | //ignore shadow of badge when placing string |
---|
122 | NSRect stringRect = badgeRect; |
---|
123 | float badgeBottomExtra = 2.0; |
---|
124 | stringRect.size.height -= badgeBottomExtra; |
---|
125 | stringRect.origin.y += badgeBottomExtra; |
---|
126 | |
---|
127 | [dockIcon lockFocus]; |
---|
128 | |
---|
129 | if (uploadRateString) |
---|
130 | { |
---|
131 | //place badge |
---|
132 | [fUploadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver]; |
---|
133 | |
---|
134 | //place badge text |
---|
135 | [self badgeString: uploadRateString forRect: stringRect]; |
---|
136 | } |
---|
137 | |
---|
138 | if (downloadRateString) |
---|
139 | { |
---|
140 | //download rate above upload rate |
---|
141 | if (uploadRateString) |
---|
142 | { |
---|
143 | float spaceBetween = badgeRect.size.height + 2.0; |
---|
144 | badgeRect.origin.y += spaceBetween; |
---|
145 | stringRect.origin.y += spaceBetween; |
---|
146 | } |
---|
147 | |
---|
148 | //place badge |
---|
149 | [fDownloadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver]; |
---|
150 | |
---|
151 | //place badge text |
---|
152 | [self badgeString: downloadRateString forRect: stringRect]; |
---|
153 | } |
---|
154 | |
---|
155 | [dockIcon unlockFocus]; |
---|
156 | } |
---|
157 | |
---|
158 | if (fNonDefault || dockIcon) |
---|
159 | { |
---|
160 | if (!dockIcon) |
---|
161 | { |
---|
162 | fNonDefault = NO; |
---|
163 | dockIcon = [fDockIcon retain]; |
---|
164 | } |
---|
165 | else |
---|
166 | fNonDefault = YES; |
---|
167 | |
---|
168 | [NSApp setApplicationIconImage: dockIcon]; |
---|
169 | [dockIcon release]; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | - (void) clearBadge |
---|
174 | { |
---|
175 | [NSApp setApplicationIconImage: fDockIcon]; |
---|
176 | fNonDefault = NO; |
---|
177 | } |
---|
178 | |
---|
179 | @end |
---|
180 | |
---|
181 | @implementation Badger (Private) |
---|
182 | |
---|
183 | //dock icon must have locked focus |
---|
184 | - (void) badgeString: (NSString *) string forRect: (NSRect) rect |
---|
185 | { |
---|
186 | NSSize stringSize = [string sizeWithAttributes: fAttributes]; |
---|
187 | |
---|
188 | //string is in center of image |
---|
189 | rect.origin.x += (rect.size.width - stringSize.width) * 0.5; |
---|
190 | rect.origin.y += (rect.size.height - stringSize.height) * 0.5; |
---|
191 | |
---|
192 | [string drawAtPoint: rect.origin withAttributes: fAttributes]; |
---|
193 | } |
---|
194 | |
---|
195 | @end |
---|