Changeset 103
- Timestamp:
- Feb 8, 2006, 5:44:07 PM (15 years ago)
- Location:
- trunk/macosx
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/macosx/Badger.h
r36 r103 13 13 @interface Badger : NSObject { 14 14 15 NSImage * fBadge, * fDockIcon, * fBadgedDockIcon; 16 17 NSDictionary * fBadgeAttributes, * fStringAttributes; 18 19 NSColor * fUploadingColor, * fDownloadingColor; 20 21 int fCompleted;15 NSImage * fDockIcon, * fBadgedDockIcon, 16 * fBadge, * fUploadBadge, * fDownloadBadge; 17 18 NSDictionary * fAttributes; 19 20 int fCompleted; 21 BOOL fSpeedShown; 22 22 } 23 23 24 24 - (void) updateBadgeWithCompleted: (int) completed 25 26 25 uploadRate: (NSString *) uploadRate 26 downloadRate: (NSString *) downloadRate; 27 27 - (void) clearBadge; 28 28 -
trunk/macosx/Badger.m
r36 r103 10 10 @interface Badger (Private) 11 11 12 - ( NSImage *) badgeWithNum: (int) num;12 - (void) badgeString: (NSString *) string forRect: (NSRect) rect; 13 13 14 14 @end … … 18 18 - (id) init 19 19 { 20 if ((self = [super init])) 21 { 22 fBadge = [NSImage imageNamed: @"Badge"]; 23 fDockIcon = [[NSApp applicationIconImage] copy]; 24 fBadgedDockIcon = [fDockIcon copy]; 25 26 fBadgeAttributes = [[NSDictionary dictionaryWithObjectsAndKeys: 27 [NSColor whiteColor], NSForegroundColorAttributeName, 28 [NSFont fontWithName: @"Helvetica-Bold" size: 24], NSFontAttributeName, 29 nil] retain]; 30 31 fStringAttributes = [[NSDictionary dictionaryWithObjectsAndKeys: 32 [NSColor whiteColor], NSForegroundColorAttributeName, 33 [NSFont fontWithName: @"Helvetica-Bold" size: 20], NSFontAttributeName, 34 nil] retain]; 35 36 fUploadingColor = [[[NSColor greenColor] colorWithAlphaComponent: 0.65] retain]; 37 fDownloadingColor = [[[NSColor blueColor] colorWithAlphaComponent: 0.65] retain]; 38 39 fCompleted = 0; 40 } 41 42 return self; 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; 43 44 } 44 45 45 46 - (void) dealloc 46 47 { 47 48 48 [fDockIcon release]; 49 [fBadgedDockIcon release]; 49 50 50 [fBadgeAttributes release]; 51 [fStringAttributes release]; 52 53 [fUploadingColor release]; 54 [fDownloadingColor release]; 51 [fAttributes release]; 55 52 56 53 [super dealloc]; 57 54 } 58 55 59 56 - (void) updateBadgeWithCompleted: (int) completed 60 61 57 uploadRate: (NSString *) uploadRate 58 downloadRate: (NSString *) downloadRate 62 59 { 63 NSImage * dockIcon; 64 NSSize iconSize = [fDockIcon size]; 65 66 //set seeding and downloading badges if there was a change 67 if (completed != fCompleted) 68 { 69 fCompleted = completed; 70 71 dockIcon = [fDockIcon copy]; 72 [dockIcon lockFocus]; 73 74 //set completed badge to top right 75 if (completed > 0) 76 { 77 NSSize badgeSize = [fBadge size]; 78 [[self badgeWithNum: completed] 79 compositeToPoint: NSMakePoint(iconSize.width - badgeSize.width, 80 iconSize.height - badgeSize.height) 81 operation: NSCompositeSourceOver]; 82 } 60 NSImage * dockIcon = nil; 61 NSSize iconSize = [fDockIcon size]; 83 62 84 [dockIcon unlockFocus]; 85 86 [fBadgedDockIcon release]; 87 fBadgedDockIcon = [dockIcon copy]; 88 } 89 else 90 dockIcon = [fBadgedDockIcon copy]; 91 92 if (uploadRate || downloadRate) 93 { 94 //upload rate at bottom 95 float mainY = 5, 96 mainHeight = 25; 97 NSRect shapeRect = NSMakeRect(12.5, mainY, iconSize.width - 25, mainHeight); 98 99 NSRect leftRect, rightRect; 100 leftRect.origin.x = 0; 101 leftRect.origin.y = mainY; 102 leftRect.size.width = shapeRect.origin.x * 2.0; 103 leftRect.size.height = mainHeight; 104 105 rightRect = leftRect; 106 rightRect.origin.x = iconSize.width - rightRect.size.width; 107 108 NSRect textRect; 109 textRect.origin.y = mainY; 110 textRect.size.height = mainHeight; 111 112 [dockIcon lockFocus]; 113 114 if (uploadRate) 115 { 116 float width = [uploadRate sizeWithAttributes: fStringAttributes].width; 117 textRect.origin.x = (iconSize.width - width) * 0.5; 118 textRect.size.width = width; 119 120 NSBezierPath * uploadOval = [NSBezierPath bezierPathWithRect: shapeRect]; 121 [uploadOval appendBezierPathWithOvalInRect: leftRect]; 122 [uploadOval appendBezierPathWithOvalInRect: rightRect]; 123 124 [fUploadingColor set]; 125 [uploadOval fill]; 126 [uploadRate drawInRect: textRect withAttributes: fStringAttributes]; 127 128 //shift up for download rate if there is an upload rate 129 float heightDiff = 27; 130 shapeRect.origin.y += heightDiff; 131 leftRect.origin.y += heightDiff; 132 rightRect.origin.y += heightDiff; 133 textRect.origin.y += heightDiff; 134 } 135 136 //download rate above upload rate 137 if (downloadRate) 138 { 139 float width = [downloadRate sizeWithAttributes: fStringAttributes].width; 140 textRect.origin.x = (iconSize.width - width) * 0.5; 141 textRect.size.width = width; 142 143 NSBezierPath * downloadOval = [NSBezierPath bezierPathWithRect: shapeRect]; 144 [downloadOval appendBezierPathWithOvalInRect: leftRect]; 145 [downloadOval appendBezierPathWithOvalInRect: rightRect]; 146 147 [fDownloadingColor set]; 148 [downloadOval fill]; 149 [downloadRate drawInRect: textRect withAttributes: fStringAttributes]; 150 } 151 152 [dockIcon unlockFocus]; 153 } 154 155 [NSApp setApplicationIconImage: dockIcon]; 156 [dockIcon release]; 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; 157 163 } 158 164 159 165 - (void) clearBadge 160 166 { 161 162 167 [fBadgedDockIcon release]; 168 fBadgedDockIcon = [fDockIcon copy]; 163 169 164 170 [NSApp setApplicationIconImage: fDockIcon]; 165 171 } 166 172 … … 169 175 @implementation Badger (Private) 170 176 171 - (NSImage *) badgeWithNum: (int) num 172 { 173 NSImage * badge = [[fBadge copy] autorelease]; 174 NSString * numString = [NSString stringWithFormat: @"%d", num]; 175 176 //number is in center of image 177 NSRect badgeRect; 178 NSSize numSize = [numString sizeWithAttributes: fBadgeAttributes]; 179 badgeRect.size = [badge size]; 180 badgeRect.origin.x = (badgeRect.size.width - numSize.width) * 0.5; 181 badgeRect.origin.y = badgeRect.size.height * 0.5 - numSize.height * 1.2; 182 183 [badge lockFocus]; 184 [numString drawInRect: badgeRect withAttributes: fBadgeAttributes]; 185 [badge unlockFocus]; 186 187 return badge; 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]; 188 187 } 189 188 -
trunk/macosx/Controller.m
r94 r103 613 613 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 614 614 [fBadger updateBadgeWithCompleted: [defaults boolForKey: @"BadgeCompleted"] ? fCompleted : 0 615 uploadRate: ul >= 0.1 && [defaults boolForKey: @"BadgeUploadRate"] ? uploadRate: nil616 downloadRate: dl >= 0.1 && [defaults boolForKey: @"BadgeDownloadRate"] ? downloadRate: nil];615 uploadRate: ul >= 0.1 && [defaults boolForKey: @"BadgeUploadRate"] ? [NSString stringForSpeedAbbrev: ul] : nil 616 downloadRate: dl >= 0.1 && [defaults boolForKey: @"BadgeDownloadRate"] ? [NSString stringForSpeedAbbrev: dl] : nil]; 617 617 } 618 618 -
trunk/macosx/StringAdditions.h
r94 r103 13 13 + (NSString *) stringForFileSize: (uint64_t) size; 14 14 + (NSString *) stringForSpeed: (float) speed; 15 + (NSString *) stringForSpeedAbbrev: (float) speed; 15 16 + (NSString *) stringForRatio: (uint64_t) down upload: (uint64_t) up; 16 17 - (NSString *) stringFittingInWidth: (float) width -
trunk/macosx/StringAdditions.m
r98 r103 29 29 + (NSString *) stringForSpeed: (float) speed 30 30 { 31 return [[self stringForSpeedAbbrev: speed] 32 stringByAppendingString: @"B/s"]; 33 } 34 35 + (NSString *) stringForSpeedAbbrev: (float) speed 36 { 31 37 if (speed < 1024) 32 return [NSString stringWithFormat: @"%.1f K B/s", speed];38 return [NSString stringWithFormat: @"%.1f K", speed]; 33 39 else if (speed < 1048576) 34 return [NSString stringWithFormat: @"%.2f M B/s", speed / 1024];40 return [NSString stringWithFormat: @"%.2f M", speed / 1024]; 35 41 else 36 return [NSString stringWithFormat: @"%.2f G B/s", speed / 1048576];42 return [NSString stringWithFormat: @"%.2f G", speed / 1048576]; 37 43 } 38 44 -
trunk/macosx/Transmission.xcodeproj/project.pbxproj
r38 r103 24 24 4DA6FDC5091141AD00450CB1 /* ResumeOff.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DA6FDC3091141AD00450CB1 /* ResumeOff.png */; }; 25 25 4DA6FDC6091141AD00450CB1 /* ResumeOn.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DA6FDC4091141AD00450CB1 /* ResumeOn.png */; }; 26 4DDFDD22099A5D8E00189D81 /* DownloadBadge.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DDFDD20099A5D8E00189D81 /* DownloadBadge.png */; }; 27 4DDFDD23099A5D8E00189D81 /* UploadBadge.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DDFDD21099A5D8E00189D81 /* UploadBadge.png */; }; 26 28 4DE5CC9D0980656F00BE280E /* StringAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DE5CC9C0980656F00BE280E /* StringAdditions.m */; }; 27 29 4DE5CCA70980735700BE280E /* Badger.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DE5CCA60980735700BE280E /* Badger.m */; }; … … 97 99 4DA6FDC3091141AD00450CB1 /* ResumeOff.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = ResumeOff.png; path = Images/ResumeOff.png; sourceTree = "<group>"; }; 98 100 4DA6FDC4091141AD00450CB1 /* ResumeOn.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = ResumeOn.png; path = Images/ResumeOn.png; sourceTree = "<group>"; }; 101 4DDFDD20099A5D8E00189D81 /* DownloadBadge.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = DownloadBadge.png; path = Images/DownloadBadge.png; sourceTree = "<group>"; }; 102 4DDFDD21099A5D8E00189D81 /* UploadBadge.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = UploadBadge.png; path = Images/UploadBadge.png; sourceTree = "<group>"; }; 99 103 4DE5CC9B0980656F00BE280E /* StringAdditions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = StringAdditions.h; sourceTree = "<group>"; }; 100 104 4DE5CC9C0980656F00BE280E /* StringAdditions.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = StringAdditions.m; sourceTree = "<group>"; }; … … 217 221 4D8CEF90095870E00063BAEA /* Network.png */, 218 222 4DE5CCA80980739100BE280E /* Badge.png */, 223 4DDFDD20099A5D8E00189D81 /* DownloadBadge.png */, 224 4DDFDD21099A5D8E00189D81 /* UploadBadge.png */, 219 225 4DE5CCB80981D27700BE280E /* ResumeAll.png */, 220 226 4DE5CCB90981D27700BE280E /* PauseAll.png */, … … 314 320 4DE5CCBB0981D27700BE280E /* PauseAll.png in Resources */, 315 321 4DE5CCCB0981D9BE00BE280E /* Defaults.plist in Resources */, 322 4DDFDD22099A5D8E00189D81 /* DownloadBadge.png in Resources */, 323 4DDFDD23099A5D8E00189D81 /* UploadBadge.png in Resources */, 316 324 ); 317 325 runOnlyForDeploymentPostprocessing = 0;
Note: See TracChangeset
for help on using the changeset viewer.