Ignore:
Timestamp:
Feb 20, 2011, 12:22:55 AM (11 years ago)
Author:
livings124
Message:

move the status bar into its own xib file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/Controller.m

    r11988 r11990  
    4545#import "ToolbarSegmentedCell.h"
    4646#import "BlocklistDownloader.h"
    47 #import "StatusBarView.h"
     47#import "StatusBarController.h"
    4848#import "FilterBarView.h"
    4949#import "FilterButton.h"
     
    123123#define GROUP_FILTER_ALL_TAG    -2
    124124
     125#warning remove
    125126#define STATUS_RATIO_TOTAL      @"RatioTotal"
    126127#define STATUS_RATIO_SESSION    @"RatioSession"
     
    128129#define STATUS_TRANSFER_SESSION @"TransferSession"
    129130
     131#warning remove
    130132typedef enum
    131133{
     
    427429    [fWindow setMovableByWindowBackground: YES];
    428430   
    429     [[fTotalDLField cell] setBackgroundStyle: NSBackgroundStyleRaised];
    430     [[fTotalULField cell] setBackgroundStyle: NSBackgroundStyleRaised];
    431431    [[fTotalTorrentsField cell] setBackgroundStyle: NSBackgroundStyleRaised];
    432432   
     
    448448   
    449449    //set up status bar
    450     [fStatusBar setHidden: YES];
    451    
    452     [self updateSpeedFieldsToolTips];
    453    
    454     NSRect statusBarFrame = [fStatusBar frame];
    455     statusBarFrame.size.width = windowSize.width;
    456     [fStatusBar setFrame: statusBarFrame];
    457    
    458     [contentView addSubview: fStatusBar];
    459     [fStatusBar setFrameOrigin: NSMakePoint(0, NSMaxY([contentView frame]))];
    460450    [self showStatusBar: [fDefaults boolForKey: @"StatusBar"] animate: NO];
    461451   
     
    583573    [nc addObserver: self selector: @selector(updateGroupsFilters:)
    584574                    name: @"UpdateGroups" object: nil];
    585    
    586     //update when speed limits are changed
    587     [nc addObserver: self selector: @selector(updateSpeedFieldsToolTips)
    588                     name: @"SpeedLimitUpdate" object: nil];
    589575
    590576    //timer to update the interface every second
     
    774760    [fMessageController release];
    775761    [fPrefsController release];
     762   
     763    [fStatusBar release];
     764    [fFilterBar release];
    776765   
    777766    [fTorrents release];
     
    17151704            [self sortTorrents];
    17161705           
    1717             //update status bar
    1718             if (![fStatusBar isHidden])
    1719             {
    1720                 //set rates
    1721                 [fTotalDLField setStringValue: [NSString stringForSpeed: dlRate]];
    1722                 [fTotalULField setStringValue: [NSString stringForSpeed: ulRate]];
    1723                
    1724                 //set status button text
    1725                 NSString * statusLabel = [fDefaults stringForKey: @"StatusLabel"], * statusString;
    1726                 BOOL total;
    1727                 if ((total = [statusLabel isEqualToString: STATUS_RATIO_TOTAL]) || [statusLabel isEqualToString: STATUS_RATIO_SESSION])
    1728                 {
    1729                     tr_session_stats stats;
    1730                     if (total)
    1731                         tr_sessionGetCumulativeStats(fLib, &stats);
    1732                     else
    1733                         tr_sessionGetStats(fLib, &stats);
    1734                    
    1735                     statusString = [NSLocalizedString(@"Ratio", "status bar -> status label") stringByAppendingFormat: @": %@",
    1736                                     [NSString stringForRatio: stats.ratio]];
    1737                 }
    1738                 else //STATUS_TRANSFER_TOTAL or STATUS_TRANSFER_SESSION
    1739                 {
    1740                     total = [statusLabel isEqualToString: STATUS_TRANSFER_TOTAL];
    1741                    
    1742                     tr_session_stats stats;
    1743                     if (total)
    1744                         tr_sessionGetCumulativeStats(fLib, &stats);
    1745                     else
    1746                         tr_sessionGetStats(fLib, &stats);
    1747                    
    1748                     statusString = [NSString stringWithFormat: @"%@: %@  %@: %@",
    1749                             NSLocalizedString(@"DL", "status bar -> status label"), [NSString stringForFileSize: stats.downloadedBytes],
    1750                             NSLocalizedString(@"UL", "status bar -> status label"), [NSString stringForFileSize: stats.uploadedBytes]];
    1751                 }
    1752                
    1753                 [fStatusButton setTitle: statusString];
    1754                 [self resizeStatusButton];
    1755             }
     1706            [fStatusBar updateWithDownload: dlRate upload: ulRate];
    17561707        }
    17571708
     
    17631714    //badge dock
    17641715    [fBadger updateBadgeWithDownload: dlRate upload: ulRate];
    1765 }
    1766 
    1767 - (void) resizeStatusButton
    1768 {
    1769     [fStatusButton sizeToFit];
    1770    
    1771     //width ends up being too long
    1772     NSRect statusFrame = [fStatusButton frame];
    1773     statusFrame.size.width -= 25.0;
    1774    
    1775     CGFloat difference = NSMaxX(statusFrame) + 5.0 - [fTotalDLImageView frame].origin.x;
    1776     if (difference > 0)
    1777         statusFrame.size.width -= difference;
    1778    
    1779     [fStatusButton setFrame: statusFrame];
    17801716}
    17811717
     
    18011737   
    18021738    [fTotalTorrentsField setStringValue: totalTorrentsString];
    1803 }
    1804 
    1805 - (void) updateSpeedFieldsToolTips
    1806 {
    1807     NSString * uploadText, * downloadText;
    1808    
    1809     if ([fDefaults boolForKey: @"SpeedLimit"])
    1810     {
    1811         NSString * speedString = [NSString stringWithFormat: @"%@ (%@)", NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
    1812                                     NSLocalizedString(@"Speed Limit", "Status Bar -> speed tooltip")];
    1813        
    1814         uploadText = [NSString stringWithFormat: speedString, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]];
    1815         downloadText = [NSString stringWithFormat: speedString, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]];
    1816     }
    1817     else
    1818     {
    1819         if ([fDefaults boolForKey: @"CheckUpload"])
    1820             uploadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
    1821                             [fDefaults integerForKey: @"UploadLimit"]];
    1822         else
    1823             uploadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
    1824        
    1825         if ([fDefaults boolForKey: @"CheckDownload"])
    1826             downloadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
    1827                             [fDefaults integerForKey: @"DownloadLimit"]];
    1828         else
    1829             downloadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
    1830     }
    1831    
    1832     uploadText = [NSLocalizedString(@"Global upload limit", "Status Bar -> speed tooltip")
    1833                     stringByAppendingFormat: @": %@", uploadText];
    1834     downloadText = [NSLocalizedString(@"Global download limit", "Status Bar -> speed tooltip")
    1835                     stringByAppendingFormat: @": %@", downloadText];
    1836    
    1837     [fTotalULField setToolTip: uploadText];
    1838     [fTotalDLField setToolTip: downloadText];
    18391739}
    18401740
     
    24252325}
    24262326
    2427 - (void) setStatusLabel: (id) sender
    2428 {
    2429     NSString * statusLabel;
    2430     switch ([sender tag])
    2431     {
    2432         case STATUS_RATIO_TOTAL_TAG:
    2433             statusLabel = STATUS_RATIO_TOTAL;
    2434             break;
    2435         case STATUS_RATIO_SESSION_TAG:
    2436             statusLabel = STATUS_RATIO_SESSION;
    2437             break;
    2438         case STATUS_TRANSFER_TOTAL_TAG:
    2439             statusLabel = STATUS_TRANSFER_TOTAL;
    2440             break;
    2441         case STATUS_TRANSFER_SESSION_TAG:
    2442             statusLabel = STATUS_TRANSFER_SESSION;
    2443             break;
    2444         default:
    2445             NSAssert1(NO, @"Unknown status label tag received: %d", [sender tag]);
    2446             return;
    2447     }
    2448    
    2449     [fDefaults setObject: statusLabel forKey: @"StatusLabel"];
    2450     [self updateUI];
    2451 }
    2452 
    24532327- (void) menuNeedsUpdate: (NSMenu *) menu
    24542328{
     
    25752449{
    25762450    tr_sessionUseAltSpeed(fLib, [fDefaults boolForKey: @"SpeedLimit"]);
    2577     [self updateSpeedFieldsToolTips];
     2451    [fStatusBar updateSpeedFieldsToolTips];
    25782452}
    25792453
     
    25842458
    25852459    [fDefaults setBool: isLimited forKey: @"SpeedLimit"];
    2586     [self updateSpeedFieldsToolTips];
     2460    [fStatusBar updateSpeedFieldsToolTips];
    25872461   
    25882462    if (![[dict objectForKey: @"ByUser"] boolValue])
     
    31252999        {
    31263000            NSSize maxSize = [scrollView convertSize: [[fWindow screen] visibleFrame].size fromView: nil];
    3127             if ([fStatusBar isHidden])
    3128                 maxSize.height -= [fStatusBar frame].size.height;
     3001            if (!fStatusBar)
     3002                maxSize.height -= [[fStatusBar view] frame].size.height;
    31293003            if ([fFilterBar isHidden])
    31303004                maxSize.height -= [fFilterBar frame].size.height;
     
    31443018- (void) toggleStatusBar: (id) sender
    31453019{
    3146     [self showStatusBar: [fStatusBar isHidden] animate: YES];
    3147     [fDefaults setBool: ![fStatusBar isHidden] forKey: @"StatusBar"];
     3020    const BOOL show = fStatusBar == nil;
     3021    [self showStatusBar: show animate: YES];
     3022    [fDefaults setBool: show forKey: @"StatusBar"];
    31483023}
    31493024
     
    31513026- (void) showStatusBar: (BOOL) show animate: (BOOL) animate
    31523027{
    3153     if (show != [fStatusBar isHidden])
     3028    const BOOL prevShown = fStatusBar != nil;
     3029    if (show == prevShown)
    31543030        return;
    3155 
     3031   
    31563032    if (show)
    3157         [fStatusBar setHidden: NO];
    3158 
     3033    {
     3034        fStatusBar = [[StatusBarController alloc] initWithLib: fLib];
     3035       
     3036        NSView * contentView = [fWindow contentView];
     3037        const NSSize windowSize = [contentView convertSize: [fWindow frame].size fromView: nil];
     3038       
     3039        NSRect statusBarFrame = [[fStatusBar view] frame];
     3040        statusBarFrame.size.width = windowSize.width;
     3041        [[fStatusBar view] setFrame: statusBarFrame];
     3042       
     3043        [contentView addSubview: [fStatusBar view]];
     3044        [[fStatusBar view] setFrameOrigin: NSMakePoint(0, NSMaxY([contentView frame]))];
     3045    }
     3046   
    31593047    NSRect frame;
    3160     CGFloat heightChange = [fStatusBar frame].size.height;
     3048    CGFloat heightChange = [[fStatusBar view] frame].size.height;
    31613049    if (!show)
    31623050        heightChange *= -1;
     
    31813069   
    31823070    //set views to not autoresize
    3183     NSUInteger statsMask = [fStatusBar autoresizingMask];
    3184     NSUInteger filterMask = [fFilterBar autoresizingMask];
    3185     NSUInteger scrollMask = [scrollView autoresizingMask];
    3186     [fStatusBar setAutoresizingMask: NSViewNotSizable];
     3071    const NSUInteger statsMask = [[fStatusBar view] autoresizingMask];
     3072    [[fStatusBar view] setAutoresizingMask: NSViewNotSizable];
     3073    const NSUInteger filterMask = [fFilterBar autoresizingMask];
    31873074    [fFilterBar setAutoresizingMask: NSViewNotSizable];
     3075    const NSUInteger scrollMask = [scrollView autoresizingMask];
    31883076    [scrollView setAutoresizingMask: NSViewNotSizable];
    31893077   
     
    31923080   
    31933081    //re-enable autoresize
    3194     [fStatusBar setAutoresizingMask: statsMask];
     3082    [[fStatusBar view] setAutoresizingMask: statsMask];
    31953083    [fFilterBar setAutoresizingMask: filterMask];
    31963084    [scrollView setAutoresizingMask: scrollMask];
     
    32023090   
    32033091    if (!show)
    3204         [fStatusBar setHidden: YES];
     3092    {
     3093        [fStatusBar release];
     3094        fStatusBar = nil;
     3095    }
    32053096}
    32063097
     
    37433634    }
    37443635   
    3745     //enable sort options
    3746     if (action == @selector(setStatusLabel:))
    3747     {
    3748         NSString * statusLabel;
    3749         switch ([menuItem tag])
    3750         {
    3751             case STATUS_RATIO_TOTAL_TAG:
    3752                 statusLabel = STATUS_RATIO_TOTAL;
    3753                 break;
    3754             case STATUS_RATIO_SESSION_TAG:
    3755                 statusLabel = STATUS_RATIO_SESSION;
    3756                 break;
    3757             case STATUS_TRANSFER_TOTAL_TAG:
    3758                 statusLabel = STATUS_TRANSFER_TOTAL;
    3759                 break;
    3760             case STATUS_TRANSFER_SESSION_TAG:
    3761                 statusLabel = STATUS_TRANSFER_SESSION;
    3762                 break;
    3763             default:
    3764                 NSAssert1(NO, @"Unknown status label tag received: %d", [menuItem tag]);
    3765         }
    3766        
    3767         [menuItem setState: [statusLabel isEqualToString: [fDefaults stringForKey: @"StatusLabel"]] ? NSOnState : NSOffState];
    3768         return YES;
    3769     }
    3770    
    37713636    if (action == @selector(setGroup:))
    37723637    {
     
    38663731    if (action == @selector(toggleStatusBar:))
    38673732    {
    3868         NSString * title = [fStatusBar isHidden] ? NSLocalizedString(@"Show Status Bar", "View menu -> Status Bar")
     3733        NSString * title = !fStatusBar ? NSLocalizedString(@"Show Status Bar", "View menu -> Status Bar")
    38693734                            : NSLocalizedString(@"Hide Status Bar", "View menu -> Status Bar");
    38703735        [menuItem setTitle: title];
     
    43244189- (void) windowDidResize: (NSNotification *) notification
    43254190{
    4326     if (![fStatusBar isHidden])
    4327         [self resizeStatusButton];
    4328    
    4329     if ([fFilterBar isHidden])
    4330         return;
    4331 
    4332     [self resizeFilterBar];
     4191    if (![fFilterBar isHidden])
     4192        [self resizeFilterBar];
    43334193}
    43344194
Note: See TracChangeset for help on using the changeset viewer.