]> granicus.if.org Git - transmission/commitdiff
Simplify the progress bar drawing code using NSDivideRect(). This also allows the...
authorMitchell Livingston <livings124@transmissionbt.com>
Sun, 6 Sep 2009 21:09:33 +0000 (21:09 +0000)
committerMitchell Livingston <livings124@transmissionbt.com>
Sun, 6 Sep 2009 21:09:33 +0000 (21:09 +0000)
macosx/Torrent.h
macosx/Torrent.m
macosx/TorrentCell.m

index e5a983561f5a9f733dd4e545caf9acf1db25089a..1763dbb25515994adaf6f70452ef5de571269fb0 100644 (file)
 
 - (CGFloat) progress;
 - (CGFloat) progressDone;
-- (CGFloat) progressLeft;
 - (CGFloat) checkingProgress;
 
 - (NSInteger) eta;
 
-- (CGFloat) notAvailableDesired;
+- (CGFloat) availableDesired;
 
 - (BOOL) isActive;
 - (BOOL) isSeeding;
index ef647cbb4da37cd7d5d1dbdd9193521c68f0b41e..1cd401d99b04436f8dd5c95efc53959af767bb42 100644 (file)
@@ -860,11 +860,6 @@ int trashDataFile(const char * filename)
     return fStat->percentDone;
 }
 
-- (CGFloat) progressLeft
-{
-    return (CGFloat)[self sizeLeft] / [self size];
-}
-
 - (CGFloat) checkingProgress
 {
     return fStat->recheckProgress;
@@ -875,7 +870,7 @@ int trashDataFile(const char * filename)
     return fStat->eta;
 }
 
-- (CGFloat) notAvailableDesired
+- (CGFloat) availableDesired
 {
     return 1.0 - (CGFloat)fStat->desiredAvailable / [self sizeLeft];
 }
index 95120be4d1452beebdcde9e1cb3ec9d72aa4cdcf..95c5061d2ea024b6209c89ff31009b46678c6396 100644 (file)
 {
     Torrent * torrent = [self representedObject];
     
-    NSInteger leftWidth = barRect.size.width;
-    CGFloat progress = [torrent progress];
+    NSRect leftRect, rightRect;
+    NSDivideRect(barRect, &leftRect, &rightRect, [torrent progress] * NSWidth(barRect), NSMinXEdge);
     
-    if (progress < 1.0f)
+    //don't-have section
+    if (!NSIsEmptyRect(rightRect))
     {
-        CGFloat rightProgress = 1.0f - progress, progressLeft = [torrent progressLeft];
-        NSInteger rightWidth = leftWidth * rightProgress;
-        leftWidth -= rightWidth;
+        NSRect unwantedRect = rightRect;
         
-        if (progressLeft < rightProgress)
+        if (![torrent allDownloaded])
         {
-            NSInteger rightNoIncludeWidth = rightWidth * ((rightProgress - progressLeft) / rightProgress);
-            rightWidth -= rightNoIncludeWidth;
+            //the ratio of total progress to total width equals ratio of progress of amount wanted to wanted width
+            const CGFloat widthRemaining = NSWidth(barRect) * (1.0 - [torrent progressDone]) / [torrent progress];
             
-            NSRect noIncludeRect = barRect;
-            noIncludeRect.origin.x += barRect.size.width - rightNoIncludeWidth;
-            noIncludeRect.size.width = rightNoIncludeWidth;
+            NSRect wantedRect;
+            NSDivideRect(rightRect, &wantedRect, &unwantedRect, widthRemaining, NSMinXEdge);
             
-            [[ProgressGradients progressLightGrayGradient] drawInRect: noIncludeRect angle: 90];
-        }
-        
-        if (rightWidth > 0)
-        {
-            if ([torrent isActive] && ![torrent allDownloaded] && ![torrent isChecking]
-                && [fDefaults boolForKey: @"DisplayProgressBarAvailable"])
+            if (!NSIsEmptyRect(wantedRect))
             {
-                NSInteger notAvailableWidth = ceil(rightWidth * [torrent notAvailableDesired]);
-                if (notAvailableWidth > 0)
+                //not-available section
+                if ([torrent isActive] && ![torrent isChecking] && [fDefaults boolForKey: @"DisplayProgressBarAvailable"])
                 {
-                    rightWidth -= notAvailableWidth;
-                    
-                    NSRect notAvailableRect = barRect;
-                    notAvailableRect.origin.x += leftWidth + rightWidth;
-                    notAvailableRect.size.width = notAvailableWidth;
+                    NSRect unavailableRect;
+                    NSDivideRect(wantedRect, &wantedRect, &unavailableRect, [torrent availableDesired] * NSWidth(wantedRect), NSMinXEdge);
                     
-                    [[ProgressGradients progressRedGradient] drawInRect: notAvailableRect angle: 90];
+                    [[ProgressGradients progressRedGradient] drawInRect: unavailableRect angle: 90];
                 }
-            }
-            
-            if (rightWidth > 0)
-            {
-                NSRect includeRect = barRect;
-                includeRect.origin.x += leftWidth;
-                includeRect.size.width = rightWidth;
                 
-                [[ProgressGradients progressWhiteGradient] drawInRect: includeRect angle: 90];
+                //remaining section
+                [[ProgressGradients progressWhiteGradient] drawInRect: wantedRect angle: 90];
             }
         }
+        
+        //unwanted section
+        [[ProgressGradients progressLightGrayGradient] drawInRect: unwantedRect angle: 90];
     }
     
-    if (leftWidth > 0)
+    if (!NSIsEmptyRect(leftRect))
     {
-        NSRect completeRect = barRect;
-        completeRect.size.width = leftWidth;
-        
         if ([torrent isActive])
         {
             if ([torrent isChecking])
-                [[ProgressGradients progressYellowGradient] drawInRect: completeRect angle: 90];
+                [[ProgressGradients progressYellowGradient] drawInRect: leftRect angle: 90];
             else if ([torrent isSeeding])
             {
-                NSInteger ratioLeftWidth = leftWidth * (1.0f - [torrent progressStopRatio]);
-                leftWidth -= ratioLeftWidth;
-                
-                if (ratioLeftWidth > 0)
-                {
-                    NSRect ratioLeftRect = barRect;
-                    ratioLeftRect.origin.x += leftWidth;
-                    ratioLeftRect.size.width = ratioLeftWidth;
-                    
-                    [[ProgressGradients progressLightGreenGradient] drawInRect: ratioLeftRect angle: 90];
-                }
+                NSRect ratioHaveRect, ratioRemainingRect;
+                NSDivideRect(leftRect, &ratioHaveRect, &ratioRemainingRect, [torrent progressStopRatio] * NSWidth(leftRect), NSMinXEdge);
                 
-                if (leftWidth > 0)
-                {
-                    completeRect.size.width = leftWidth;
-                    
-                    [[ProgressGradients progressGreenGradient] drawInRect: completeRect angle: 90];
-                }
+                [[ProgressGradients progressGreenGradient] drawInRect: ratioHaveRect angle: 90];
+                [[ProgressGradients progressLightGreenGradient] drawInRect: ratioRemainingRect angle: 90];
             }
             else
-                [[ProgressGradients progressBlueGradient] drawInRect: completeRect angle: 90];
+                [[ProgressGradients progressBlueGradient] drawInRect: leftRect angle: 90];
         }
         else
         {
             if ([torrent waitingToStart])
             {
-                if ([torrent progressLeft] <= 0.0f)
-                    [[ProgressGradients progressDarkGreenGradient] drawInRect: completeRect angle: 90];
+                if ([torrent progressDone] == 0.0)
+                    [[ProgressGradients progressDarkGreenGradient] drawInRect: leftRect angle: 90];
                 else
-                    [[ProgressGradients progressDarkBlueGradient] drawInRect: completeRect angle: 90];
+                    [[ProgressGradients progressDarkBlueGradient] drawInRect: leftRect angle: 90];
             }
             else
-                [[ProgressGradients progressGrayGradient] drawInRect: completeRect angle: 90];
+                [[ProgressGradients progressGrayGradient] drawInRect: leftRect angle: 90];
         }
     }
 }