• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UI_BASE_COCOA_ANIMATION_UTILS_H
6 #define UI_BASE_COCOA_ANIMATION_UTILS_H
7 
8 #import <Cocoa/Cocoa.h>
9 #import <QuartzCore/QuartzCore.h>
10 
11 // This class is a stack-based helper useful for unit testing of Cocoa UI,
12 // and any other situation where you want to temporarily turn off Cocoa
13 // animation for the life of a function call or other limited scope.
14 // Just declare one of these, and all animations will complete instantly until
15 // this goes out of scope and pops our state off the Core Animation stack.
16 //
17 // Example:
18 //  MyUnitTest() {
19 //    WithNoAnimation at_all; // Turn off Cocoa auto animation in this scope.
20 
21 
22 class WithNoAnimation {
23  public:
WithNoAnimation()24   WithNoAnimation() {
25     [NSAnimationContext beginGrouping];
26     [[NSAnimationContext currentContext] setDuration:0.0];
27   }
28 
~WithNoAnimation()29   ~WithNoAnimation() {
30    [NSAnimationContext endGrouping];
31   }
32 };
33 
34 // Disables actions within a scope.
35 class ScopedCAActionDisabler {
36  public:
ScopedCAActionDisabler()37   ScopedCAActionDisabler() {
38     [CATransaction begin];
39     [CATransaction setValue:[NSNumber numberWithBool:YES]
40                      forKey:kCATransactionDisableActions];
41   }
42 
~ScopedCAActionDisabler()43   ~ScopedCAActionDisabler() {
44     [CATransaction commit];
45   }
46 };
47 
48 // Sets a duration on actions within a scope.
49 class ScopedCAActionSetDuration {
50  public:
ScopedCAActionSetDuration(NSTimeInterval duration)51   explicit ScopedCAActionSetDuration(NSTimeInterval duration) {
52     [CATransaction begin];
53     [CATransaction setValue:[NSNumber numberWithFloat:duration]
54                      forKey:kCATransactionAnimationDuration];
55   }
56 
~ScopedCAActionSetDuration()57   ~ScopedCAActionSetDuration() {
58     [CATransaction commit];
59   }
60 };
61 
62 #endif // UI_BASE_COCOA_ANIMATION_UTILS_H
63