source: trunk/macosx/Badger.m @ 103

Last change on this file since 103 was 103, checked in by titer, 16 years ago

Updated badging

File size: 5.2 KB
Line 
1//
2//  Badger.m
3//  Transmission
4//
5//  Created by Mitchell Livingston on 12/21/05.
6//
7
8#import "Badger.h"
9
10@interface Badger (Private)
11
12- (void) badgeString: (NSString *) string forRect: (NSRect) rect;
13
14@end
15
16@implementation Badger
17
18- (id) init
19{
20    if ((self = [super init]))
21    {
22        fBadge = [NSImage imageNamed: @"Badge"];
23        fDockIcon = [[NSApp applicationIconImage] copy];
24        fBadgedDockIcon = [fDockIcon copy];
25        fUploadBadge = [NSImage imageNamed: @"UploadBadge"];
26        fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"];
27       
28        NSShadow * stringShadow = [[NSShadow alloc] init];
29        [stringShadow setShadowOffset: NSMakeSize(2, -2)];
30        [stringShadow setShadowBlurRadius: 4];
31       
32        fAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
33                            [NSColor whiteColor], NSForegroundColorAttributeName,
34                            [NSFont fontWithName: @"Helvetica-Bold" size: 28], NSFontAttributeName,
35                            stringShadow, NSShadowAttributeName,
36                            nil] retain];
37        [stringShadow release];
38       
39        fCompleted = 0;
40        fSpeedShown = NO;
41    }
42   
43    return self;
44}
45
46- (void) dealloc
47{
48    [fDockIcon release];
49    [fBadgedDockIcon release];
50
51    [fAttributes release];
52
53    [super dealloc];
54}
55
56- (void) updateBadgeWithCompleted: (int) completed
57                    uploadRate: (NSString *) uploadRate
58                    downloadRate: (NSString *) downloadRate
59{
60    NSImage * dockIcon = nil;
61    NSSize iconSize = [fDockIcon size];
62
63    //set seeding and downloading badges if there was a change
64    if (completed != fCompleted)
65    {
66        fCompleted = completed;
67       
68        dockIcon = [fDockIcon copy];
69       
70        //set completed badge to top right
71        if (completed > 0)
72        {
73            NSRect badgeRect;
74            badgeRect.size = [fBadge size];
75            badgeRect.origin.x = iconSize.width - badgeRect.size.width;
76            badgeRect.origin.y = iconSize.height - badgeRect.size.height;
77                                       
78            [dockIcon lockFocus];
79           
80            //place badge
81            [fBadge compositeToPoint: badgeRect.origin
82                        operation: NSCompositeSourceOver];
83           
84            //ignore shadow of badge when placing string
85            float badgeBottomExtra = 5.0;
86            badgeRect.size.height -= badgeBottomExtra;
87            badgeRect.origin.y += badgeBottomExtra;
88           
89            //place badge text
90            [self badgeString: [NSString stringWithFormat: @"%d", completed]
91                        forRect: badgeRect];
92                       
93            [dockIcon unlockFocus];
94        }
95       
96        [fBadgedDockIcon release];
97        fBadgedDockIcon = [dockIcon copy];
98    }
99
100    //display upload and download rates
101    BOOL speedShown = NO;
102    if (uploadRate || downloadRate)
103    {
104        speedShown = YES;
105   
106        NSRect badgeRect, stringRect;
107        badgeRect.size = [fUploadBadge size];
108        badgeRect.origin = NSZeroPoint;
109       
110        //ignore shadow of badge when placing string
111        float badgeBottomExtra = 2.0;
112        stringRect = badgeRect;
113        stringRect.size.height -= badgeBottomExtra;
114        stringRect.origin.y += badgeBottomExtra;
115
116        if (!dockIcon)
117            dockIcon = [fBadgedDockIcon copy];
118       
119        [dockIcon lockFocus];
120       
121        if (uploadRate)
122        {
123            //place badge
124            [fUploadBadge compositeToPoint: badgeRect.origin
125                        operation: NSCompositeSourceOver];
126           
127            //place badge text
128            [self badgeString: uploadRate forRect: stringRect];
129        }
130       
131        if (downloadRate)
132        {
133            //download rate above upload rate
134            if (uploadRate)
135            {
136                float spaceBetween = badgeRect.size.height + 2.0;
137                badgeRect.origin.y += spaceBetween;
138                stringRect.origin.y += spaceBetween;
139            }
140       
141            //place badge
142            [fDownloadBadge compositeToPoint: badgeRect.origin
143                        operation: NSCompositeSourceOver];
144           
145            //place badge text
146            [self badgeString: downloadRate forRect: stringRect];
147        }
148       
149        [dockIcon unlockFocus];
150    }
151
152    if (dockIcon || fSpeedShown)
153    {
154        if (!dockIcon)
155            dockIcon = [fBadgedDockIcon copy];
156           
157        [NSApp setApplicationIconImage: dockIcon];
158       
159        [dockIcon release];
160    }
161   
162    fSpeedShown = speedShown;
163}
164
165- (void) clearBadge
166{
167    [fBadgedDockIcon release];
168    fBadgedDockIcon = [fDockIcon copy];
169
170    [NSApp setApplicationIconImage: fDockIcon];
171}
172
173@end
174
175@implementation Badger (Private)
176
177//dock icon must have locked focus
178- (void) badgeString: (NSString *) string forRect: (NSRect) rect
179{
180    NSSize stringSize = [string sizeWithAttributes: fAttributes];
181   
182    //string is in center of image
183    rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
184    rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
185                       
186    [string drawAtPoint: rect.origin withAttributes: fAttributes];
187}
188
189@end
Note: See TracBrowser for help on using the repository browser.