source: trunk/macosx/Badger.m @ 709

Last change on this file since 709 was 709, checked in by livings124, 15 years ago

small change in badger

  • Property svn:keywords set to Date Rev Author Id
File size: 6.8 KB
Line 
1/******************************************************************************
2 * $Id: Badger.m 709 2006-07-30 19:42:24Z 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        dockIcon = [fDockIcon copy];
86       
87        //set completed badge to top right
88        if (completed > 0)
89        {
90            NSRect badgeRect;
91            badgeRect.size = [fBadge size];
92            badgeRect.origin.x = iconSize.width - badgeRect.size.width;
93            badgeRect.origin.y = iconSize.height - badgeRect.size.height;
94                                       
95            [dockIcon lockFocus];
96           
97            //place badge
98            [fBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
99           
100            //ignore shadow of badge when placing string
101            float badgeBottomExtra = 5.0;
102            badgeRect.size.height -= badgeBottomExtra;
103            badgeRect.origin.y += badgeBottomExtra;
104           
105            //place badge text
106            [self badgeString: [NSString stringWithInt: completed] forRect: badgeRect];
107                       
108            [dockIcon unlockFocus];
109        }
110       
111        [fBadgedDockIcon release];
112        fBadgedDockIcon = [dockIcon copy];
113       
114        fCompleted = completed;
115    }
116
117    //set upload and download rate badges
118    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
119    NSString * uploadRateString = uploadRate >= 0.1 && [defaults boolForKey: @"BadgeUploadRate"]
120                                    ? [NSString stringForSpeedAbbrev: uploadRate] : nil,
121            * downloadRateString = downloadRate >= 0.1 && [defaults boolForKey: @"BadgeDownloadRate"]
122                                    ? [NSString stringForSpeedAbbrev: downloadRate] : nil;
123   
124    BOOL speedShown = uploadRateString || downloadRateString;
125    if (speedShown)
126    {
127        NSRect badgeRect;
128        badgeRect.size = [fUploadBadge size];
129        badgeRect.origin = NSZeroPoint;
130       
131        //ignore shadow of badge when placing string
132        NSRect stringRect = badgeRect;
133        float badgeBottomExtra = 2.0;
134        stringRect.size.height -= badgeBottomExtra;
135        stringRect.origin.y += badgeBottomExtra;
136
137        if (!dockIcon)
138            dockIcon = [fBadgedDockIcon copy];
139       
140        [dockIcon lockFocus];
141       
142        if (uploadRateString)
143        {
144            //place badge
145            [fUploadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
146           
147            //place badge text
148            [self badgeString: uploadRateString forRect: stringRect];
149        }
150       
151        if (downloadRateString)
152        {
153            //download rate above upload rate
154            if (uploadRateString)
155            {
156                float spaceBetween = badgeRect.size.height + 2.0;
157                badgeRect.origin.y += spaceBetween;
158                stringRect.origin.y += spaceBetween;
159            }
160       
161            //place badge
162            [fDownloadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
163           
164            //place badge text
165            [self badgeString: downloadRateString forRect: stringRect];
166        }
167       
168        [dockIcon unlockFocus];
169    }
170
171    if (dockIcon || fSpeedShown)
172    {
173        if (!dockIcon)
174            dockIcon = [fBadgedDockIcon copy];
175           
176        [NSApp setApplicationIconImage: dockIcon];
177        [dockIcon release];
178    }
179   
180    fSpeedShown = speedShown;
181}
182
183- (void) clearBadge
184{
185    [fBadgedDockIcon release];
186    fBadgedDockIcon = [fDockIcon copy];
187
188    [NSApp setApplicationIconImage: fDockIcon];
189}
190
191@end
192
193@implementation Badger (Private)
194
195//dock icon must have locked focus
196- (void) badgeString: (NSString *) string forRect: (NSRect) rect
197{
198    NSSize stringSize = [string sizeWithAttributes: fAttributes];
199   
200    //string is in center of image
201    rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
202    rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
203                       
204    [string drawAtPoint: rect.origin withAttributes: fAttributes];
205}
206
207@end
Note: See TracBrowser for help on using the repository browser.