source: trunk/macosx/BadgeView.m @ 5687

Last change on this file since 5687 was 5687, checked in by livings124, 14 years ago

don't redraw the dock tile if there is no change

  • Property svn:keywords set to Date Rev Author Id
File size: 4.7 KB
Line 
1/******************************************************************************
2 * $Id: BadgeView.m 5687 2008-04-24 21:24:42Z livings124 $
3 *
4 * Copyright (c) 2007-2008 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 "BadgeView.h"
26#import "NSStringAdditions.h"
27
28#define BETWEEN_PADDING 2.0
29
30@interface BadgeView (Private)
31
32- (void) badge: (NSImage *) badge string: (NSString *) string atHeight: (float) height adjustForQuit: (BOOL) quit;
33
34@end
35
36@implementation BadgeView
37
38- (id) initWithFrame: (NSRect) frame lib: (tr_handle *) lib
39{
40    if ((self = [super initWithFrame: frame]))
41    {
42        fLib = lib;
43       
44        fDownloadRate = 0.0;
45        fUploadRate = 0.0;
46        fQuitting = NO;
47    }
48    return self;
49}
50
51- (void) dealloc
52{
53    [fAttributes release];
54    [super dealloc];
55}
56
57- (BOOL) setRatesWithDownload: (float) downloadRate upload: (float) uploadRate
58{
59    //only needs update if the badges were displayed or are displayed now
60    BOOL needsUpdate = fDownloadRate != downloadRate || fUploadRate != uploadRate;
61    if (needsUpdate)
62    {
63        fDownloadRate = downloadRate;
64        fUploadRate = uploadRate;
65    }
66   
67    return needsUpdate;
68}
69
70- (void) setQuitting
71{
72    fQuitting = YES;
73    [self display];
74}
75
76- (void) drawRect: (NSRect) rect
77{
78    [[NSImage imageNamed: @"NSApplicationIcon"] drawInRect: rect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
79   
80    if (fQuitting)
81    {
82        NSImage * quitBadge = [NSImage imageNamed: @"QuitBadge.png"];
83        [self badge: quitBadge string: NSLocalizedString(@"Quitting", "Dock Badger -> quit")
84                atHeight: (rect.size.height - [quitBadge size].height) * 0.5 adjustForQuit: YES];
85        return;
86    }
87   
88    BOOL upload = fUploadRate >= 0.1,
89        download = fDownloadRate >= 0.1;
90    float bottom = 0.0;
91    if (upload)
92    {
93        NSImage * uploadBadge = [NSImage imageNamed: @"UploadBadge.png"];
94        [self badge: uploadBadge string: [NSString stringForSpeedAbbrev: fUploadRate] atHeight: bottom adjustForQuit: NO];
95        if (download)
96            bottom += [uploadBadge size].height + BETWEEN_PADDING; //download rate above upload rate
97    }
98    if (download)
99        [self badge: [NSImage imageNamed: @"DownloadBadge.png"] string: [NSString stringForSpeedAbbrev: fDownloadRate]
100                atHeight: bottom adjustForQuit: NO];
101}
102
103@end
104
105@implementation BadgeView (Private)
106
107//dock icon must have locked focus
108- (void) badge: (NSImage *) badge string: (NSString *) string atHeight: (float) height adjustForQuit: (BOOL) quit
109{
110    if (!fAttributes)
111    {
112        NSShadow * stringShadow = [[NSShadow alloc] init];
113        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
114        [stringShadow setShadowBlurRadius: 4.0];
115       
116        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
117            [NSColor whiteColor], NSForegroundColorAttributeName,
118            [NSFont boldSystemFontOfSize: 26.0], NSFontAttributeName, stringShadow, NSShadowAttributeName, nil];
119       
120        [stringShadow release];
121    }
122   
123    NSRect badgeRect = NSZeroRect;
124    badgeRect.size = [badge size];
125    badgeRect.origin.y = height;
126   
127    [badge drawInRect: badgeRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
128   
129    //string is in center of image
130    NSSize stringSize = [string sizeWithAttributes: fAttributes];
131   
132    NSRect stringRect = badgeRect;
133    stringRect.origin.x += (badgeRect.size.width - stringSize.width) * 0.5;
134    stringRect.origin.y += (badgeRect.size.height - stringSize.height) * 0.5 + (quit ? 2.0 : 1.0); //adjust for shadow, extra for quit
135    stringRect.size = stringSize;
136   
137    [string drawInRect: stringRect withAttributes: fAttributes];
138}
139
140@end
Note: See TracBrowser for help on using the repository browser.