PageRenderTime 33ms CodeModel.GetById 14ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/AppKit/GTMNSAnimatablePropertyContainer.m

http://macfuse.googlecode.com/
Objective C | 124 lines | 71 code | 21 blank | 32 comment | 9 complexity | 1cdbf3ca56da792d281516d08117d816 MD5 | raw file
  1//
  2//  GTMNSAnimatablePropertyContainer.m
  3//
  4//  Copyright (c) 2010 Google Inc. All rights reserved.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMNSAnimatablePropertyContainer.h"
 20
 21#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
 22
 23@interface GTMAnimatorStopper : NSObject {
 24 @private
 25  NSObject<NSAnimatablePropertyContainer> *container_;
 26}
 27@end
 28
 29@implementation GTMAnimatorStopper
 30- (id)initWithAnimatablePropertyContainer:(NSObject<NSAnimatablePropertyContainer>*) container {
 31  if ((self = [super init])) {
 32    container_ = [container retain];
 33  }
 34  return self;
 35}
 36
 37- (void)dealloc {
 38  [container_ release];
 39  [super dealloc];
 40}
 41
 42- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
 43  return [container_ methodSignatureForSelector:aSelector];
 44}
 45
 46- (void)forwardInvocation:(NSInvocation *)anInvocation {
 47  SEL selector = [anInvocation selector];
 48  NSString *selectorName = NSStringFromSelector(selector);
 49
 50  // NSWindow animator handles setFrame:display: which is an odd case
 51  // for animator. All other methods take just a key value, so we convert
 52  // this to it's equivalent key value.
 53  if ([selectorName isEqual:@"setFrame:display:"]) {
 54    selectorName = @"setFrame:";
 55  }
 56
 57  // Check to make sure our selector is valid (starts with set and has a
 58  // single : at the end.
 59  NSRange colonRange = [selectorName rangeOfString:@":"];
 60  NSUInteger selectorLength = [selectorName length];
 61  if ([selectorName hasPrefix:@"set"]
 62      && colonRange.location == selectorLength - 1
 63      && selectorLength > 4) {
 64    // transform our selector into a keyValue by removing the set
 65    // and the colon and converting the first char down to lowercase.
 66    NSString *keyValue = [selectorName substringFromIndex:3];
 67    NSString *firstChar = [[keyValue substringToIndex:1] lowercaseString];
 68    NSRange rest = NSMakeRange(1, [keyValue length] - 2);
 69    NSString *restOfKey = [keyValue substringWithRange:rest];
 70    keyValue = [firstChar stringByAppendingString:restOfKey];
 71
 72    // Save a copy of our old animations.
 73    NSDictionary *oldAnimations
 74      = [[[container_ animations] copy] autorelease];
 75
 76    // For frame the animator doesn't actually animate the rect but gets
 77    // animators for the size and the origin independently. In case this changes
 78    // in the future (similar to bounds), we will stop the animations for the
 79    // frame as well as the frameSize and frameOrigin.
 80    NSDictionary *animations = nil;
 81    NSNull *null = [NSNull null];
 82    if ([keyValue isEqual:@"frame"]) {
 83      animations = [NSDictionary dictionaryWithObjectsAndKeys:
 84                    null, @"frame",
 85                    null, @"frameSize",
 86                    null, @"frameOrigin", nil];
 87    } else {
 88      animations = [NSDictionary dictionaryWithObject:null forKey:keyValue];
 89    }
 90
 91    // Set our animations to NULL which will force them to stop.
 92    [container_ setAnimations:animations];
 93    // Call our original invocation on our animator.
 94    [anInvocation setTarget:[container_ animator]];
 95    [anInvocation invoke];
 96
 97    // Reset the animations.
 98    [container_ setAnimations:oldAnimations];
 99  } else {
100    [self doesNotRecognizeSelector:selector];
101  }
102}
103
104@end
105
106@implementation NSView(GTMNSAnimatablePropertyContainer)
107
108- (id)gtm_animatorStopper {
109  return [[[GTMAnimatorStopper alloc] initWithAnimatablePropertyContainer:self]
110          autorelease];
111}
112
113@end
114
115@implementation NSWindow(GTMNSAnimatablePropertyContainer)
116
117- (id)gtm_animatorStopper {
118  return [[[GTMAnimatorStopper alloc] initWithAnimatablePropertyContainer:self]
119          autorelease];
120}
121
122@end
123
124#endif  // MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5