Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(932)

Side by Side Diff: chrome/browser/cocoa/animatable_image.mm

Issue 3014005: [Mac] Display a quick animation when a popup is blocked so the user notices it in the Omnibox. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: CGImage Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 #import "chrome/browser/cocoa/animatable_image.h"
6
7 #include "base/logging.h"
8 #include "base/scoped_cftyperef.h"
9 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h"
10
11 @interface AnimatableImage (Private)
12 - (void)setLayerContents:(CALayer*)layer;
13 @end
14
15 @implementation AnimatableImage
16
17 @synthesize startFrame = startFrame_;
18 @synthesize endFrame = endFrame_;
19 @synthesize startOpacity = startOpacity_;
20 @synthesize endOpacity = endOpacity_;
21 @synthesize duration = duration_;
22
23 - (id)initWithImage:(NSImage*)image
24 animationFrame:(NSRect)animationFrame {
25 if ((self = [super initWithContentRect:animationFrame
26 styleMask:NSBorderlessWindowMask
27 backing:NSBackingStoreBuffered
28 defer:NO])) {
29 DCHECK(image);
30 image_.reset([image retain]);
31 duration_ = 1.0;
32 startOpacity_ = 1.0;
33 endOpacity_ = 1.0;
34
35 [self setOpaque:NO];
36 [self setBackgroundColor:[NSColor clearColor]];
37 [self setIgnoresMouseEvents:YES];
38
39 // Must be set or else self will be leaked.
40 [self setReleasedWhenClosed:YES];
41 }
42 return self;
43 }
44
45 - (void)startAnimation {
46 // Set up the root layer. By calling -setLayer: followed by -setWantsLayer:
47 // the view becomes a layer hosting view as opposed to a layer backed view.
48 NSView* view = [self contentView];
49 CALayer* rootLayer = [CALayer layer];
50 [view setLayer:rootLayer];
51 [view setWantsLayer:YES];
52
53 // Create the layer that will be animated.
54 CALayer* layer = [CALayer layer];
55 [self setLayerContents:layer];
56 [layer setAnchorPoint:CGPointMake(0, 1)];
57 [layer setFrame:[self startFrame]];
58 [layer setNeedsDisplayOnBoundsChange:YES];
59 [rootLayer addSublayer:layer];
60
61 // Common timing function for all animations.
62 CAMediaTimingFunction* mediaFunction =
63 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
64
65 // Animate the bounds only if the image is resized.
66 CABasicAnimation* boundsAnimation = nil;
67 if (CGRectGetWidth([self startFrame]) != CGRectGetWidth([self endFrame]) ||
68 CGRectGetHeight([self startFrame]) != CGRectGetHeight([self endFrame])) {
69 boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
70 NSRect startRect = NSMakeRect(0, 0,
71 CGRectGetWidth([self startFrame]),
72 CGRectGetHeight([self startFrame]));
73 [boundsAnimation setFromValue:[NSValue valueWithRect:startRect]];
74 NSRect endRect = NSMakeRect(0, 0,
75 CGRectGetWidth([self endFrame]),
76 CGRectGetHeight([self endFrame]));
77 [boundsAnimation setToValue:[NSValue valueWithRect:endRect]];
78 [boundsAnimation gtm_setDuration:[self duration]
79 eventMask:NSLeftMouseUpMask];
80 [boundsAnimation setTimingFunction:mediaFunction];
81 }
82
83 // Positional animation.
84 CABasicAnimation* positionAnimation =
85 [CABasicAnimation animationWithKeyPath:@"position"];
86 [positionAnimation setFromValue:
87 [NSValue valueWithPoint:NSPointFromCGPoint([self startFrame].origin)]];
88 [positionAnimation setToValue:
89 [NSValue valueWithPoint:NSPointFromCGPoint([self endFrame].origin)]];
90 [positionAnimation gtm_setDuration:[self duration]
91 eventMask:NSLeftMouseUpMask];
92 [positionAnimation setTimingFunction:mediaFunction];
93
94 // Opacity animation.
95 CABasicAnimation* opacityAnimation =
96 [CABasicAnimation animationWithKeyPath:@"opacity"];
97 [opacityAnimation setFromValue:
98 [NSNumber numberWithFloat:[self startOpacity]]];
99 [opacityAnimation setToValue:[NSNumber numberWithFloat:[self endOpacity]]];
100 [opacityAnimation gtm_setDuration:[self duration]
101 eventMask:NSLeftMouseUpMask];
102 [opacityAnimation setTimingFunction:mediaFunction];
103 // Set the delegate just for one of the animations so that this window can
104 // be closed upon completion.
105 [opacityAnimation setDelegate:self];
106
107 // The CAAnimations only affect the presentational value of a layer, not the
108 // model value. This means that after the animation is done, it can flicker
109 // back to the original values. To avoid this, create an implicit animation of
110 // the values, which are then overridden with the CABasicAnimations.
111 [layer setPosition:[self endFrame].origin];
112 [layer setOpacity:[self endOpacity]];
113
114 // Start the animations.
115 [CATransaction begin];
116 [CATransaction setValue:[NSNumber numberWithFloat:[self duration]]
117 forKey:kCATransactionAnimationDuration];
118 if (boundsAnimation) {
119 [layer addAnimation:boundsAnimation forKey:@"bounds"];
120 }
121 [layer addAnimation:positionAnimation forKey:@"position"];
122 [layer addAnimation:opacityAnimation forKey:@"opacity"];
123 [CATransaction commit];
124 }
125
126 // CALayer expects a CGImageRef contents. If the image is a PDF, 10.5 CGImage
127 // cannot handle the conversion to bitmap. To get it to work, rasterize the
128 // image into a bitmap CGImageRef. This is based loosely on
129 // http://www.cocoadev.com/index.pl?CGImageRef.
130 - (void)setLayerContents:(CALayer*)layer {
131 NSSize size = [image_ size];
132 CGContextRef context =
133 CGBitmapContextCreate(NULL, // Allow CG to allocate memory.
134 size.width,
135 size.height,
136 8, // bitsPerComponent
137 0, // bytesPerRow - CG will calculate by default.
138 [[NSColorSpace genericRGBColorSpace] CGColorSpace],
139 kCGBitmapByteOrder32Host |
140 kCGImageAlphaPremultipliedFirst);
141
142 [NSGraphicsContext saveGraphicsState];
143 [NSGraphicsContext setCurrentContext:
144 [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
145 [image_ drawInRect:NSMakeRect(0,0, size.width, size.height)
146 fromRect:NSZeroRect
147 operation:NSCompositeCopy
148 fraction:1.0];
149 [NSGraphicsContext restoreGraphicsState];
Nico 2010/07/19 23:00:35 Please put this into a utility function somewhere
150
151 scoped_cftyperef<CGImageRef> cgImage(CGBitmapContextCreateImage(context));
152 CGContextRelease(context);
153
154 // Create the layer that will be animated.
155 [layer setContents:(id)cgImage.get()];
156 }
157
158 // CAAnimation delegate method called when the animation is complete.
159 - (void)animationDidStop:(CAAnimation*)animation finished:(BOOL)flag {
160 // Close the window, releasing self.
161 [self close];
162 }
163
164 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/animatable_image.h ('k') | chrome/browser/cocoa/animatable_image_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698