PageRenderTime 46ms CodeModel.GetById 12ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMNSAnimation+Duration.m

http://macfuse.googlecode.com/
Objective C | 95 lines | 57 code | 15 blank | 23 comment | 10 complexity | 097dd91118e3253dc9b3e1706d343a3b MD5 | raw file
 1//
 2//  GTMNSAnimation+Duration.m
 3//
 4//  Copyright 2009 Google Inc.
 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 "GTMNSAnimation+Duration.h"
20
21const NSUInteger kGTMLeftMouseUpAndKeyDownMask
22  = NSLeftMouseUpMask | NSKeyDownMask;
23
24NSTimeInterval GTMModifyDurationBasedOnCurrentState(NSTimeInterval duration,
25                                                    NSUInteger eventMask) {
26  NSEvent *currentEvent = [NSApp currentEvent];
27  NSUInteger currentEventMask = NSEventMaskFromType([currentEvent type]);
28  if (eventMask & currentEventMask) {
29    NSUInteger modifiers = [currentEvent modifierFlags];
30    if (!(modifiers & (NSAlternateKeyMask |
31                       NSCommandKeyMask))) {
32      if (modifiers & NSShiftKeyMask) {
33        // 25 is the ascii code generated for a shift-tab (End-of-message)
34        // The shift modifier is ignored if it is applied to a Tab key down/up.
35        // Tab and shift-tab are often used for navigating around UI elements,
36        // and in the majority of cases slowing down the animations while
37        // navigating around UI elements is not desired.
38        BOOL isShiftTab = (currentEventMask & (NSKeyDownMask | NSKeyUpMask))
39          && !(modifiers & NSControlKeyMask)
40          && ([[currentEvent characters] length] == 1)
41          && ([[currentEvent characters] characterAtIndex:0] == 25);
42        if (!isShiftTab) {
43          duration *= 5.0;
44        }
45      }
46      // These are additive, so shift+control returns 10 * duration.
47      if (modifiers & NSControlKeyMask) {
48        duration *= 2.0;
49      }
50    }
51  }
52  return duration;
53}
54
55@implementation NSAnimation (GTMNSAnimationDurationAdditions)
56
57- (id)gtm_initWithDuration:(NSTimeInterval)duration
58                 eventMask:(NSUInteger)eventMask
59            animationCurve:(NSAnimationCurve)animationCurve {
60  return [self initWithDuration:GTMModifyDurationBasedOnCurrentState(duration,
61                                                                     eventMask)
62                 animationCurve:animationCurve];
63}
64
65- (void)gtm_setDuration:(NSTimeInterval)duration
66              eventMask:(NSUInteger)eventMask {
67  [self setDuration:GTMModifyDurationBasedOnCurrentState(duration,
68                                                         eventMask)];
69}
70
71@end
72
73#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
74
75@implementation NSAnimationContext (GTMNSAnimationDurationAdditions)
76
77- (void)gtm_setDuration:(NSTimeInterval)duration
78              eventMask:(NSUInteger)eventMask {
79  [self setDuration:GTMModifyDurationBasedOnCurrentState(duration,
80                                                         eventMask)];
81}
82
83@end
84
85@implementation CAAnimation (GTMCAAnimationDurationAdditions)
86
87- (void)gtm_setDuration:(CFTimeInterval)duration
88              eventMask:(NSUInteger)eventMask {
89  [self setDuration:GTMModifyDurationBasedOnCurrentState(duration,
90                                                         eventMask)];
91}
92
93@end
94
95#endif  // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5