1 // Copyright (c) 2012 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.
5 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h"
7 #include "base/files/file_path.h"
8 #include "base/location.h"
9 #import "base/mac/foundation_util.h"
10 #include "base/native_library.h"
11 #include "ui/gfx/animation/tween.h"
13 // The window animations in this file use private APIs as described here:
14 // http://code.google.com/p/undocumented-goodness/source/browse/trunk/CoreGraphics/CGSPrivate.h
15 // There are two important things to keep in mind when modifying this file:
16 // - For most operations the origin of the coordinate system is top left.
17 // - Perspective and shear transformations get clipped if they are bigger
18 // than the window size. This does not seem to apply to scale transformations.
20 // Length of the animation in seconds.
21 const NSTimeInterval kAnimationDuration = 0.18;
23 // The number of pixels above the final destination to animate from.
24 const CGFloat kShowHideVerticalOffset = 20;
26 // Scale the window by this factor when animating.
27 const CGFloat kShowHideScaleFactor = 0.99;
29 // Size of the perspective effect as a factor of the window width.
30 const CGFloat kShowHidePerspectiveFactor = 0.04;
32 // Forward declare private CoreGraphics APIs used to transform windows.
35 typedef float float32;
37 typedef int32 CGSWindow;
38 typedef int32 CGSConnection;
50 CGSConnection _CGSDefaultConnection();
51 CGError CGSSetWindowTransform(const CGSConnection cid,
53 CGAffineTransform transform);
54 CGError CGSSetWindowWarp(const CGSConnection cid,
59 CGError CGSSetWindowAlpha(const CGSConnection cid,
72 // Get the window location relative to the top left of the main screen.
73 // Most Cocoa APIs use a coordinate system where the screen origin is the
74 // bottom left. The various CGSSetWindow* APIs use a coordinate system where
75 // the screen origin is the top left.
76 NSPoint GetCGSWindowScreenOrigin(NSWindow* window) {
77 NSArray* screens = [NSScreen screens];
78 if ([screens count] == 0)
80 // Origin is relative to the screen with the menu bar (the screen at index 0).
81 // Note, this is not the same as mainScreen which is the screen with the key
83 NSScreen* main_screen = [screens objectAtIndex:0];
85 NSRect window_frame = [window frame];
86 NSRect screen_frame = [main_screen frame];
87 return NSMakePoint(NSMinX(window_frame),
88 NSHeight(screen_frame) - NSMaxY(window_frame));
91 // Set the transparency of the window.
92 void SetWindowAlpha(NSWindow* window, float alpha) {
93 CGSConnection cid = _CGSDefaultConnection();
94 CGSSetWindowAlpha(cid, [window windowNumber], alpha);
97 // Scales the window and translates it so that it stays centered relative
98 // to its original position.
99 void SetWindowScale(NSWindow* window, float scale) {
100 CGFloat scale_delta = 1.0 - scale;
101 CGFloat cur_scale = 1.0 + scale_delta;
102 CGAffineTransform transform =
103 CGAffineTransformMakeScale(cur_scale, cur_scale);
105 // Translate the window to keep it centered at the original location.
106 NSSize window_size = [window frame].size;
107 CGFloat scale_offset_x = window_size.width * (1 - cur_scale) / 2.0;
108 CGFloat scale_offset_y = window_size.height * (1 - cur_scale) / 2.0;
110 NSPoint origin = GetCGSWindowScreenOrigin(window);
111 CGFloat new_x = -origin.x + scale_offset_x;
112 CGFloat new_y = -origin.y + scale_offset_y;
113 transform = CGAffineTransformTranslate(transform, new_x, new_y);
115 CGSConnection cid = _CGSDefaultConnection();
116 CGSSetWindowTransform(cid, [window windowNumber], transform);
119 // Unsets any window warp that may have been previously applied.
120 // Window warp prevents other effects such as CGSSetWindowTransform from
122 void ClearWindowWarp(NSWindow* window) {
123 CGSConnection cid = _CGSDefaultConnection();
124 CGSSetWindowWarp(cid, [window windowNumber], 0, 0, NULL);
127 // Applies various transformations using a warp effect. The window is
128 // translated vertically by |y_offset|. The window is scaled by |scale| and
129 // translated so that the it remains centered relative to its original position.
130 // Finally, perspective is effect is applied by shrinking the top of the window.
131 void SetWindowWarp(NSWindow* window,
134 float perspective_offset) {
135 NSRect win_rect = [window frame];
136 win_rect.origin = NSZeroPoint;
137 NSRect screen_rect = win_rect;
138 screen_rect.origin = GetCGSWindowScreenOrigin(window);
140 // Apply a vertical translate.
141 screen_rect.origin.y -= y_offset;
143 // Apply a scale and translate to keep the window centered.
144 screen_rect.origin.x += (NSWidth(win_rect) - NSWidth(screen_rect)) / 2.0;
145 screen_rect.origin.y += (NSHeight(win_rect) - NSHeight(screen_rect)) / 2.0;
147 // A 2 x 2 mesh that maps each corner of the window to a location in screen
148 // coordinates. Note that the origin of the coordinate system is top, left.
149 CGPointWarp mesh[2][2] = {
152 {NSMinX(win_rect), NSMinY(win_rect)},
153 {NSMinX(screen_rect) + perspective_offset, NSMinY(screen_rect)},
157 {NSMaxX(win_rect), NSMinY(win_rect)},
158 {NSMaxX(screen_rect) - perspective_offset, NSMinY(screen_rect)}, }},
161 {NSMinX(win_rect), NSMaxY(win_rect)},
162 {NSMinX(screen_rect), NSMaxY(screen_rect)},
166 {NSMaxX(win_rect), NSMaxY(win_rect)},
167 {NSMaxX(screen_rect), NSMaxY(screen_rect)}, }},
170 CGSConnection cid = _CGSDefaultConnection();
171 CGSSetWindowWarp(cid, [window windowNumber], 2, 2, &(mesh[0][0]));
174 // Sets the various effects that are a part of the Show/Hide animation.
175 // Value is a number between 0 and 1 where 0 means the window is completely
176 // hidden and 1 means the window is fully visible.
177 void UpdateWindowShowHideAnimationState(NSWindow* window, CGFloat value) {
178 CGFloat inverse_value = 1.0 - value;
180 SetWindowAlpha(window, value);
181 CGFloat y_offset = kShowHideVerticalOffset * inverse_value;
182 CGFloat scale = 1.0 - (1.0 - kShowHideScaleFactor) * inverse_value;
183 CGFloat perspective_offset =
184 ([window frame].size.width * kShowHidePerspectiveFactor) * inverse_value;
186 SetWindowWarp(window, y_offset, scale, perspective_offset);
191 @interface ConstrainedWindowAnimationBase ()
192 // Subclasses should override these to update the window state for the current
194 - (void)setWindowStateForStart;
195 - (void)setWindowStateForValue:(float)value;
196 - (void)setWindowStateForEnd;
199 @implementation ConstrainedWindowAnimationBase
201 - (id)initWithWindow:(NSWindow*)window {
202 if ((self = [self initWithDuration:kAnimationDuration
203 animationCurve:NSAnimationEaseInOut])) {
204 window_.reset([window retain]);
205 [self setAnimationBlockingMode:NSAnimationBlocking];
206 [self setWindowStateForStart];
211 - (void)stopAnimation {
212 [super stopAnimation];
213 [self setWindowStateForEnd];
214 if ([[self delegate] respondsToSelector:@selector(animationDidEnd:)])
215 [[self delegate] animationDidEnd:self];
218 - (void)setCurrentProgress:(NSAnimationProgress)progress {
219 [super setCurrentProgress:progress];
221 if (progress >= 1.0) {
222 [self setWindowStateForEnd];
225 [self setWindowStateForValue:[self currentValue]];
228 - (void)setWindowStateForStart {
229 // Subclasses can optionally override this method.
232 - (void)setWindowStateForValue:(float)value {
233 // Subclasses must override this method.
237 - (void)setWindowStateForEnd {
238 // Subclasses can optionally override this method.
243 @implementation ConstrainedWindowAnimationShow
245 - (void)setWindowStateForStart {
246 SetWindowAlpha(window_, 0.0);
249 - (void)setWindowStateForValue:(float)value {
250 UpdateWindowShowHideAnimationState(window_, value);
253 - (void)setWindowStateForEnd {
254 SetWindowAlpha(window_, 1.0);
255 ClearWindowWarp(window_);
260 @implementation ConstrainedWindowAnimationHide
262 - (void)setWindowStateForValue:(float)value {
263 UpdateWindowShowHideAnimationState(window_, 1.0 - value);
266 - (void)setWindowStateForEnd {
267 SetWindowAlpha(window_, 0.0);
268 ClearWindowWarp(window_);
273 @implementation ConstrainedWindowAnimationPulse
275 // Sets the window scale based on the animation progress.
276 - (void)setWindowStateForValue:(float)value {
277 KeyFrame frames[] = {
278 {0.00, 1.0}, {0.40, 1.02}, {0.60, 1.02}, {1.00, 1.0},
282 for (int i = arraysize(frames) - 1; i >= 0; --i) {
283 if (value >= frames[i].value) {
284 CGFloat delta = frames[i + 1].value - frames[i].value;
285 CGFloat frame_progress = (value - frames[i].value) / delta;
286 scale = gfx::Tween::FloatValueBetween(frame_progress, frames[i].scale,
287 frames[i + 1].scale);
292 SetWindowScale(window_, scale);
295 - (void)setWindowStateForEnd {
296 SetWindowScale(window_, 1.0);