Changeset 12572


Ignore:
Timestamp:
Jul 21, 2011, 2:36:27 AM (10 years ago)
Author:
livings124
Message:

#4375 Lion: animate filtering message window

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/MessageWindowController.m

    r11617 r12572  
    2525#import "MessageWindowController.h"
    2626#import "NSApplicationAdditions.h"
     27#import "NSMutableArrayAdditions.h"
    2728#import "NSStringAdditions.h"
    2829#import <transmission.h>
     
    3940- (void) resizeColumn;
    4041- (BOOL) shouldIncludeMessageForFilter: (NSString *) filterString message: (NSDictionary *) message;
     42- (void) updateListForFilter;
    4143- (NSString *) stringForMessage: (NSDictionary *) message;
    4244
     
    4850{
    4951    return [super initWithWindowNibName: @"MessageWindow"];
    50 }
    51 
    52 - (void) dealloc
    53 {
    54     [[NSNotificationCenter defaultCenter] removeObserver: self];
    55    
    56     [fTimer invalidate];
    57     [fLock release];
    58    
    59     [fMessages release];
    60     [fDisplayedMessages release];
    61    
    62     [fAttributes release];
    63    
    64     [super dealloc];
    6552}
    6653
     
    142129   
    143130    fLock = [[NSLock alloc] init];
     131}
     132
     133- (void) dealloc
     134{
     135    [[NSNotificationCenter defaultCenter] removeObserver: self];
     136   
     137    [fTimer invalidate];
     138    [fLock release];
     139   
     140    [fMessages release];
     141    [fDisplayedMessages release];
     142   
     143    [fAttributes release];
     144   
     145    [super dealloc];
    144146}
    145147
     
    342344        return;
    343345   
     346    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
     347   
    344348    [fLock lock];
    345349   
    346     [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
    347    
    348     NSString * filterString = [fFilterField stringValue];
    349    
     350    [self updateListForFilter];
     351   
     352    [fLock unlock];
     353}
     354
     355- (void) changeFilter: (id) sender
     356{
     357    [fLock lock];
     358   
     359    [self updateListForFilter];
     360   
     361    [fLock unlock];
     362}
     363
     364- (void) clearLog: (id) sender
     365{
     366    [fLock lock];
     367   
     368    [fMessages removeAllObjects];
     369   
     370    const BOOL onLion = [NSApp isOnLionOrBetter];
     371   
     372    if (onLion)
     373        [fMessageTable beginUpdates];
     374   
     375    if (onLion)
     376        [fMessageTable removeRowsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fDisplayedMessages count])] withAnimation: NSTableViewAnimationSlideLeft];
    350377    [fDisplayedMessages removeAllObjects];
    351     for (NSDictionary * message in fMessages)
    352         if ([[message objectForKey: @"Level"] integerValue] <= level
    353             && [self shouldIncludeMessageForFilter: filterString message: message])
    354             [fDisplayedMessages addObject: message];
    355    
    356     [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
    357    
    358     [fMessageTable reloadData];
    359    
    360     if ([fDisplayedMessages count] > 0)
    361     {
    362         [fMessageTable deselectAll: self];
    363         [fMessageTable scrollRowToVisible: [fMessageTable numberOfRows]-1];
    364     }
    365    
    366     [fLock unlock];
    367 }
    368 
    369 - (void) changeFilter: (id) sender
    370 {
    371     [fLock lock];
    372    
    373     const NSInteger level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
    374     NSString * filterString = [fFilterField stringValue];
    375    
    376     [fDisplayedMessages removeAllObjects];
    377     for (NSDictionary * message in fMessages)
    378         if ([[message objectForKey: @"Level"] integerValue] <= level
    379             && [self shouldIncludeMessageForFilter: filterString message: message])
    380             [fDisplayedMessages addObject: message];
    381    
    382     [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
    383    
    384     [fMessageTable reloadData];
    385    
    386     if ([fDisplayedMessages count] > 0)
    387     {
    388         [fMessageTable deselectAll: self];
    389         [fMessageTable scrollRowToVisible: [fMessageTable numberOfRows]-1];
    390     }
    391    
    392     [fLock unlock];
    393 }
    394 
    395 - (void) clearLog: (id) sender
    396 {
    397     [fLock lock];
    398    
    399     [fMessages removeAllObjects];
    400     [fDisplayedMessages removeAllObjects];
    401     [fMessageTable reloadData];
     378   
     379    if (onLion)
     380        [fMessageTable endUpdates];
     381    else
     382        [fMessageTable reloadData];
    402383   
    403384    [fLock unlock];
     
    470451}
    471452
     453- (void) updateListForFilter
     454{
     455    const NSInteger level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
     456    NSString * filterString = [fFilterField stringValue];
     457   
     458    NSMutableArray * tempMessages = [NSMutableArray arrayWithCapacity: [fMessages count]]; //rough guess
     459   
     460    for (NSDictionary * message in fMessages)
     461    {
     462        if ([[message objectForKey: @"Level"] integerValue] <= level
     463            && [self shouldIncludeMessageForFilter: filterString message: message])
     464            [tempMessages addObject: message];
     465    }
     466   
     467    [tempMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
     468   
     469    const BOOL onLion = [NSApp isOnLionOrBetter];
     470   
     471    if (onLion)
     472        [fMessageTable beginUpdates];
     473   
     474    //figure out which rows were added/moved
     475    NSUInteger currentIndex = 0, totalCount = 0;
     476    NSMutableArray * itemsToAdd = [NSMutableArray array];
     477    NSMutableIndexSet * itemsToAddIndexes = [NSMutableIndexSet indexSet];
     478   
     479    for (NSDictionary * message in tempMessages)
     480    {
     481        const NSUInteger previousIndex = [fDisplayedMessages indexOfObject: message inRange: NSMakeRange(currentIndex, [fDisplayedMessages count]-currentIndex)];
     482        if (previousIndex == NSNotFound)
     483        {
     484            [itemsToAdd addObject: message];
     485            [itemsToAddIndexes addIndex: totalCount];
     486        }
     487        else
     488        {
     489            if (previousIndex != currentIndex)
     490            {
     491                [fDisplayedMessages moveObjectAtIndex: previousIndex toIndex: currentIndex];
     492                if (onLion)
     493                    [fMessageTable moveRowAtIndex: previousIndex toIndex: currentIndex];
     494            }
     495            ++currentIndex;
     496        }
     497       
     498        ++totalCount;
     499    }
     500   
     501    //remove trailing items - those are the unused
     502    if (currentIndex < [fDisplayedMessages count])
     503    {
     504        const NSRange removeRange = NSMakeRange(currentIndex, [fDisplayedMessages count]-currentIndex);
     505        [fDisplayedMessages removeObjectsInRange: removeRange];
     506        if (onLion)
     507            [fMessageTable removeRowsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: removeRange] withAnimation: NSTableViewAnimationSlideDown];
     508    }
     509   
     510    //add new items
     511    [fDisplayedMessages insertObjects: itemsToAdd atIndexes: itemsToAddIndexes];
     512    if (onLion)
     513        [fMessageTable insertRowsAtIndexes: itemsToAddIndexes withAnimation: NSTableViewAnimationSlideUp];
     514   
     515    if (onLion)
     516        [fMessageTable endUpdates];
     517    else
     518    {
     519        [fMessageTable reloadData];
     520       
     521        if ([fDisplayedMessages count] > 0)
     522            [fMessageTable deselectAll: self];
     523    }
     524   
     525    NSAssert([fDisplayedMessages isEqualToArray: tempMessages], @"Inconsistency between message arrays! %@ %@", fDisplayedMessages, tempMessages);
     526}
     527
    472528- (NSString *) stringForMessage: (NSDictionary *) message
    473529{
Note: See TracChangeset for help on using the changeset viewer.