source: trunk/macosx/Badger.m @ 613

Last change on this file since 613 was 613, checked in by livings124, 16 years ago

Better way to determine highlighted color

  • Property svn:keywords set to Date Rev Author Id
File size: 6.8 KB
Line 
1/******************************************************************************
2 * $Id: Badger.m 613 2006-07-16 17:12:25Z 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        fBadgedDockIcon = [fDockIcon copy];
43        fUploadBadge = [NSImage imageNamed: @"UploadBadge"];
44        fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"];
45       
46        NSShadow * stringShadow = [[NSShadow alloc] init];
47        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
48        [stringShadow setShadowBlurRadius: 4.0];
49       
50        NSFont * boldFont = [[NSFontManager sharedFontManager] convertFont:
51                                [NSFont fontWithName: @"Helvetica" size: 28.0] toHaveTrait: NSBoldFontMask];
52       
53        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
54            [NSColor whiteColor], NSForegroundColorAttributeName,
55            boldFont, NSFontAttributeName, stringShadow, NSShadowAttributeName, nil];
56       
57        [stringShadow release];
58       
59        fCompleted = 0;
60        fSpeedShown = NO;
61    }
62   
63    return self;
64}
65
66- (void) dealloc
67{
68    [fDockIcon release];
69    [fBadgedDockIcon release];
70    [fAttributes release];
71
72    [super dealloc];
73}
74
75- (void) updateBadgeWithCompleted: (int) completed
76                    uploadRate: (float) uploadRate
77                    downloadRate: (float) downloadRate
78{
79    NSImage * dockIcon = nil;
80    NSSize iconSize = [fDockIcon size];
81
82    //set completed badge
83    if (fCompleted != completed)
84    {
85        fCompleted = completed;
86        dockIcon = [fDockIcon copy];
87       
88        //set completed badge to top right
89        if (completed > 0)
90        {
91            NSRect badgeRect;
92            badgeRect.size = [fBadge size];
93            badgeRect.origin.x = iconSize.width - badgeRect.size.width;
94            badgeRect.origin.y = iconSize.height - badgeRect.size.height;
95                                       
96            [dockIcon lockFocus];
97           
98            //place badge
99            [fBadge compositeToPoint: badgeRect.origin
100                        operation: NSCompositeSourceOver];
101           
102            //ignore shadow of badge when placing string
103            float badgeBottomExtra = 5.0;
104            badgeRect.size.height -= badgeBottomExtra;
105            badgeRect.origin.y += badgeBottomExtra;
106           
107            //place badge text
108            [self badgeString: [NSString stringWithInt: completed]
109                        forRect: badgeRect];
110                       
111            [dockIcon unlockFocus];
112        }
113       
114        [fBadgedDockIcon release];
115        fBadgedDockIcon = [dockIcon copy];
116    }
117
118    //set upload and download rate badges
119    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
120    NSString * uploadRateString = uploadRate >= 0.1 && [defaults boolForKey: @"BadgeUploadRate"]
121                                    ? [NSString stringForSpeedAbbrev: uploadRate] : nil,
122            * downloadRateString = downloadRate >= 0.1 && [defaults boolForKey: @"BadgeDownloadRate"]
123                                    ? [NSString stringForSpeedAbbrev: downloadRate] : nil;
124   
125    BOOL speedShown = uploadRateString || downloadRateString;
126    if (speedShown)
127    {
128        NSRect badgeRect;
129        badgeRect.size = [fUploadBadge size];
130        badgeRect.origin = NSZeroPoint;
131       
132        //ignore shadow of badge when placing string
133        NSRect stringRect = badgeRect;
134        float badgeBottomExtra = 2.0;
135        stringRect.size.height -= badgeBottomExtra;
136        stringRect.origin.y += badgeBottomExtra;
137
138        if (!dockIcon)
139            dockIcon = [fBadgedDockIcon copy];
140       
141        [dockIcon lockFocus];
142       
143        if (uploadRateString)
144        {
145            //place badge
146            [fUploadBadge compositeToPoint: badgeRect.origin
147                        operation: NSCompositeSourceOver];
148           
149            //place badge text
150            [self badgeString: uploadRateString forRect: stringRect];
151        }
152       
153        if (downloadRateString)
154        {
155            //download rate above upload rate
156            if (uploadRateString)
157            {
158                float spaceBetween = badgeRect.size.height + 2.0;
159                badgeRect.origin.y += spaceBetween;
160                stringRect.origin.y += spaceBetween;
161            }
162       
163            //place badge
164            [fDownloadBadge compositeToPoint: badgeRect.origin
165                        operation: NSCompositeSourceOver];
166           
167            //place badge text
168            [self badgeString: downloadRateString forRect: stringRect];
169        }
170       
171        [dockIcon unlockFocus];
172    }
173
174    if (dockIcon || fSpeedShown)
175    {
176        if (!dockIcon)
177            dockIcon = [fBadgedDockIcon copy];
178           
179        [NSApp setApplicationIconImage: dockIcon];
180        [dockIcon release];
181    }
182   
183    fSpeedShown = speedShown;
184}
185
186- (void) clearBadge
187{
188    [fBadgedDockIcon release];
189    fBadgedDockIcon = [fDockIcon copy];
190
191    [NSApp setApplicationIconImage: fDockIcon];
192}
193
194@end
195
196@implementation Badger (Private)
197
198//dock icon must have locked focus
199- (void) badgeString: (NSString *) string forRect: (NSRect) rect
200{
201    NSSize stringSize = [string sizeWithAttributes: fAttributes];
202   
203    //string is in center of image
204    rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
205    rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
206                       
207    [string drawAtPoint: rect.origin withAttributes: fAttributes];
208}
209
210@end
Note: See TracBrowser for help on using the repository browser.