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