Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/browser_window_enter_fullscreen_transition.h" | 5 #import "chrome/browser/ui/cocoa/browser_window_fullscreen_transition.h" |
| 6 | 6 |
| 7 #include <QuartzCore/QuartzCore.h> | 7 #include <QuartzCore/QuartzCore.h> |
| 8 | 8 |
| 9 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/bind_objc_block.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/mac/sdk_forward_declarations.h" | 11 #include "base/mac/mac_util.h" |
| 12 #import "base/mac/scoped_nsobject.h" | |
| 13 #import "base/mac/sdk_forward_declarations.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #import "chrome/browser/ui/cocoa/full_size_content_window.h" | |
| 12 | 16 |
| 13 namespace { | 17 namespace { |
| 14 | 18 |
| 15 NSString* const kPrimaryWindowAnimationID = @"PrimaryWindowAnimationID"; | 19 NSString* const kPrimaryWindowAnimationID = @"PrimaryWindowAnimationID"; |
| 16 NSString* const kSnapshotWindowAnimationID = @"SnapshotWindowAnimationID"; | 20 NSString* const kSnapshotWindowAnimationID = @"SnapshotWindowAnimationID"; |
| 17 NSString* const kAnimationIDKey = @"AnimationIDKey"; | 21 NSString* const kAnimationIDKey = @"AnimationIDKey"; |
| 18 | 22 |
| 19 // This class has two simultaneous animations to resize and reposition layers. | 23 // This class has two simultaneous animations to resize and reposition layers. |
| 20 // These animations must use the same timing function, otherwise there will be | 24 // These animations must use the same timing function, otherwise there will be |
| 21 // visual discordance. | 25 // visual discordance. |
| 22 NSString* TransformAnimationTimingFunction() { | 26 NSString* TransformAnimationTimingFunction() { |
| 23 return kCAMediaTimingFunctionEaseInEaseOut; | 27 return kCAMediaTimingFunctionEaseInEaseOut; |
| 24 } | 28 } |
| 25 | 29 |
| 30 // This class locks and unlocks the FrameBrowserWindow. Its deconstructor | |
|
erikchen
2015/08/11 22:42:55
s/deconstructor/destructor
spqchan1
2015/08/12 19:34:41
Done.
| |
| 31 // ensures that the lock gets released. | |
| 32 class FrameAndStyleLock { | |
| 33 public: | |
| 34 base::scoped_nsobject<FramedBrowserWindow> window_; | |
|
erikchen
2015/08/11 22:42:55
window_ should be private.
spqchan1
2015/08/12 19:34:42
Done.
| |
| 35 | |
| 36 FrameAndStyleLock(FramedBrowserWindow* window) { | |
| 37 window_.reset([window retain]); | |
| 38 } | |
| 39 | |
| 40 void setLock(bool lock) { [window_ setFrameAndStyleMaskLock:lock]; } | |
| 41 | |
| 42 ~FrameAndStyleLock() { setLock(NO); } | |
| 43 }; | |
| 44 | |
| 26 } // namespace | 45 } // namespace |
| 27 | 46 |
| 28 @interface BrowserWindowEnterFullscreenTransition () { | 47 @interface BrowserWindowFullscreenTransition () { |
| 48 // Flag to keep track of whether we are entering or exiting full screen | |
| 49 BOOL isEnteringFullscreen; | |
| 50 | |
| 29 // The window which is undergoing the fullscreen transition. | 51 // The window which is undergoing the fullscreen transition. |
| 30 base::scoped_nsobject<NSWindow> primaryWindow_; | 52 base::scoped_nsobject<FramedBrowserWindow> primaryWindow_; |
| 31 | 53 |
| 32 // A layer that holds a snapshot of the original state of |primaryWindow_|. | 54 // A layer that holds a snapshot of the original state of |primaryWindow_|. |
| 33 base::scoped_nsobject<CALayer> snapshotLayer_; | 55 base::scoped_nsobject<CALayer> snapshotLayer_; |
| 34 | 56 |
| 35 // A temporary window that holds |snapshotLayer_|. | 57 // A temporary window that holds |snapshotLayer_|. |
| 36 base::scoped_nsobject<NSWindow> snapshotWindow_; | 58 base::scoped_nsobject<NSWindow> snapshotWindow_; |
| 37 | 59 |
| 38 // The frame of the |primaryWindow_| before the transition began. | |
| 39 NSRect primaryWindowInitialFrame_; | |
| 40 | |
| 41 // The background color of |primaryWindow_| before the transition began. | 60 // The background color of |primaryWindow_| before the transition began. |
| 42 base::scoped_nsobject<NSColor> primaryWindowInitialBackgroundColor_; | 61 base::scoped_nsobject<NSColor> primaryWindowInitialBackgroundColor_; |
| 43 | 62 |
| 44 // Whether |primaryWindow_| was opaque before the transition began. | 63 // Whether |primaryWindow_| was opaque before the transition began. |
| 45 BOOL primaryWindowInitialOpaque_; | 64 BOOL primaryWindowInitialOpaque_; |
| 46 | 65 |
| 47 // Whether the instance is in the process of changing the size of | 66 // Whether the instance is in the process of changing the size of |
| 48 // |primaryWindow_|. | 67 // |primaryWindow_|. |
| 49 BOOL changingPrimaryWindowSize_; | 68 BOOL changingPrimaryWindowSize_; |
| 50 | 69 |
| 70 // The frame of the |primaryWindow_| before it starts the transition. | |
| 71 NSRect initialFrame_; | |
| 72 | |
| 73 // A frame that represents the value of the |initialFrame_| that's relative | |
| 74 // to the current screen coordinates | |
|
erikchen
2015/08/11 22:42:55
missing a period
spqchan1
2015/08/12 19:34:41
Done.
| |
| 75 NSRect initialFrameRelativeToScreen_; | |
| 76 | |
| 51 // The frame that |primaryWindow_| is expected to have after the transition | 77 // The frame that |primaryWindow_| is expected to have after the transition |
| 52 // is finished. | 78 // is finished. |
| 53 NSRect primaryWindowFinalFrame_; | 79 NSRect finalFrame_; |
| 80 | |
| 81 // Locks and unlocks the FullSizeContentWindow. | |
| 82 scoped_ptr<FrameAndStyleLock> lock_; | |
| 54 } | 83 } |
| 55 | 84 |
| 56 // Takes a snapshot of |primaryWindow_| and puts it in |snapshotLayer_|. | 85 // Takes a snapshot of |primaryWindow_| and puts it in |snapshotLayer_|. |
| 57 - (void)takeSnapshot; | 86 - (void)takeSnapshot; |
| 58 | 87 |
| 59 // Creates |snapshotWindow_| and adds |snapshotLayer_| to it. | 88 // Creates |snapshotWindow_| and adds |snapshotLayer_| to it. |
| 60 - (void)makeAndPrepareSnapshotWindow; | 89 - (void)makeAndPrepareSnapshotWindow; |
| 61 | 90 |
| 62 // This method has several effects on |primaryWindow_|: | 91 // This method has several effects on |primaryWindow_|: |
| 63 // - Saves current state. | 92 // - Saves current state. |
| 64 // - Makes window transparent, with clear background. | 93 // - Makes window transparent, with clear background. |
| 65 // - Adds NSFullScreenWindowMask style mask. | 94 // - If we are entering full screen, it will also: |
| 66 // - Sets the size to the screen's size. | 95 // - Adds NSFullScreenWindowMask style mask. |
| 96 // - Sets the size to the screen's size. | |
|
erikchen
2015/08/11 22:42:55
s/Sets/Set
spqchan1
2015/08/12 19:34:41
Done.
| |
| 67 - (void)preparePrimaryWindowForAnimation; | 97 - (void)preparePrimaryWindowForAnimation; |
| 68 | 98 |
| 99 // Returns the windows to be used in the custom transition. | |
| 100 // - Takes a snapshot of the current window. | |
| 101 // - Makes a new snapshot window which shows the snapshot in the same | |
| 102 // location as the current window. | |
| 103 // - Adds the style mask NSFullScreenWindowMask to the current window. | |
| 104 // - Makes the current window transparent, and resizes the current window to | |
| 105 // be the same size as the screen. | |
| 106 - (NSArray*)customWindowsForFullScreenTransition; | |
|
erikchen
2015/08/11 22:42:55
This method is present in the header, there's no n
spqchan1
2015/08/12 19:34:41
Removed it
| |
| 107 | |
| 69 // Applies the fullscreen animation to |snapshotLayer_|. | 108 // Applies the fullscreen animation to |snapshotLayer_|. |
| 70 - (void)animateSnapshotWindowWithDuration:(CGFloat)duration; | 109 - (void)animateSnapshotWindowWithDuration:(CGFloat)duration; |
| 71 | 110 |
| 72 // Applies the fullscreen animation to the root layer of |primaryWindow_|. | 111 - (void)setPrimaryWindowToFinalFrame; |
| 73 - (void)animatePrimaryWindowWithDuration:(CGFloat)duration; | |
| 74 | 112 |
| 75 // Override of CAAnimation delegate method. | |
| 76 - (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)flag; | |
| 77 | |
| 78 // Returns the layer of the root view of |window|. | |
| 79 - (CALayer*)rootLayerOfWindow:(NSWindow*)window; | 113 - (CALayer*)rootLayerOfWindow:(NSWindow*)window; |
| 80 | 114 |
| 81 @end | 115 @end |
| 82 | 116 |
| 83 @implementation BrowserWindowEnterFullscreenTransition | 117 @implementation BrowserWindowFullscreenTransition |
| 84 | 118 |
| 85 // -------------------------Public Methods---------------------------- | 119 // -------------------------Public Methods---------------------------- |
| 86 | 120 |
| 87 - (instancetype)initWithWindow:(NSWindow*)window { | 121 - (instancetype)initEnterWithWindow:(FramedBrowserWindow*)window { |
| 88 DCHECK(window); | 122 DCHECK(window); |
| 89 DCHECK([self rootLayerOfWindow:window]); | 123 DCHECK([self rootLayerOfWindow:window]); |
| 90 if ((self = [super init])) { | 124 if ((self = [super init])) { |
| 91 primaryWindow_.reset([window retain]); | 125 primaryWindow_.reset([window retain]); |
| 126 | |
| 127 isEnteringFullscreen = YES; | |
| 128 initialFrame_ = [primaryWindow_ frame]; | |
| 129 finalFrame_ = [[primaryWindow_ screen] frame]; | |
| 92 } | 130 } |
| 93 return self; | 131 return self; |
| 94 } | 132 } |
| 95 | 133 |
| 96 - (NSArray*)customWindowsToEnterFullScreen { | 134 - (instancetype)initExitWithWindow:(FramedBrowserWindow*)window |
| 135 frame:(NSRect)frame { | |
| 136 DCHECK(window); | |
| 137 DCHECK([self rootLayerOfWindow:window]); | |
| 138 if ((self = [super init])) { | |
| 139 primaryWindow_.reset([window retain]); | |
| 140 | |
| 141 isEnteringFullscreen = NO; | |
| 142 finalFrame_ = frame; | |
| 143 initialFrame_ = [[primaryWindow_ screen] frame]; | |
| 144 | |
| 145 // Calculate the inital frame that is relative to coordinates of the | |
| 146 // screen which it's on. | |
| 147 initialFrameRelativeToScreen_ = finalFrame_; | |
| 148 CGFloat screenOriginX = finalFrame_.origin.x - initialFrame_.origin.x; | |
| 149 CGFloat screenOriginY = finalFrame_.origin.y - initialFrame_.origin.y; | |
| 150 initialFrameRelativeToScreen_.origin = | |
| 151 CGPointMake(screenOriginX, screenOriginY); | |
| 152 | |
| 153 lock_.reset(new FrameAndStyleLock(window)); | |
| 154 } | |
| 155 return self; | |
| 156 } | |
| 157 | |
| 158 - (NSArray*)customWindowsForFullScreenTransition { | |
| 97 [self takeSnapshot]; | 159 [self takeSnapshot]; |
| 98 [self makeAndPrepareSnapshotWindow]; | 160 [self makeAndPrepareSnapshotWindow]; |
| 99 [self preparePrimaryWindowForAnimation]; | 161 |
| 100 return @[ primaryWindow_.get(), snapshotWindow_.get() ]; | 162 return @[ primaryWindow_.get(), snapshotWindow_.get() ]; |
| 101 } | 163 } |
| 102 | 164 |
| 103 - (void)startCustomAnimationToEnterFullScreenWithDuration: | 165 // Right before the animation begins, change the content view size |
| 104 (NSTimeInterval)duration { | 166 // and lock the primary window so that OSX can't make changes to it |
| 167 // before we finish the animation. | |
| 168 - (void)startCustomFullScreenAnimationWithDuration:(NSTimeInterval)duration { | |
| 169 [self preparePrimaryWindowForAnimation]; | |
| 170 [self animatePrimaryWindowWithDuration:duration]; | |
| 105 [self animateSnapshotWindowWithDuration:duration]; | 171 [self animateSnapshotWindowWithDuration:duration]; |
| 106 [self animatePrimaryWindowWithDuration:duration]; | |
| 107 } | 172 } |
| 108 | 173 |
| 109 - (BOOL)shouldWindowBeUnconstrained { | 174 - (BOOL)shouldWindowBeUnconstrained { |
| 110 return changingPrimaryWindowSize_; | 175 return changingPrimaryWindowSize_; |
| 111 } | 176 } |
| 112 | 177 |
| 113 // -------------------------Private Methods---------------------------- | 178 // -------------------------Private Methods---------------------------- |
| 114 | 179 |
| 115 - (void)takeSnapshot { | 180 - (void)takeSnapshot { |
| 116 base::ScopedCFTypeRef<CGImageRef> windowSnapshot(CGWindowListCreateImage( | 181 base::ScopedCFTypeRef<CGImageRef> windowSnapshot(CGWindowListCreateImage( |
| 117 CGRectNull, kCGWindowListOptionIncludingWindow, | 182 CGRectNull, kCGWindowListOptionIncludingWindow, |
| 118 [primaryWindow_ windowNumber], kCGWindowImageBoundsIgnoreFraming)); | 183 [primaryWindow_ windowNumber], kCGWindowImageBoundsIgnoreFraming)); |
| 119 snapshotLayer_.reset([[CALayer alloc] init]); | 184 snapshotLayer_.reset([[CALayer alloc] init]); |
| 120 [snapshotLayer_ setFrame:NSRectToCGRect([primaryWindow_ frame])]; | 185 [snapshotLayer_ setFrame:NSRectToCGRect([primaryWindow_ frame])]; |
| 121 [snapshotLayer_ setContents:static_cast<id>(windowSnapshot.get())]; | 186 [snapshotLayer_ setContents:static_cast<id>(windowSnapshot.get())]; |
| 122 [snapshotLayer_ setAnchorPoint:CGPointMake(0, 0)]; | 187 [snapshotLayer_ setAnchorPoint:CGPointMake(0, 0)]; |
| 123 CGColorRef colorRef = CGColorCreateGenericRGB(0, 0, 0, 0); | 188 CGColorRef colorRef = CGColorCreateGenericRGB(0, 0, 0, 0); |
| 124 [snapshotLayer_ setBackgroundColor:colorRef]; | 189 [snapshotLayer_ setBackgroundColor:colorRef]; |
| 125 CGColorRelease(colorRef); | 190 CGColorRelease(colorRef); |
| 126 } | 191 } |
| 127 | 192 |
| 128 - (void)makeAndPrepareSnapshotWindow { | 193 - (void)makeAndPrepareSnapshotWindow { |
| 129 DCHECK(snapshotLayer_); | 194 DCHECK(snapshotLayer_); |
| 130 | 195 |
| 131 snapshotWindow_.reset( | 196 snapshotWindow_.reset([[NSWindow alloc] |
| 132 [[NSWindow alloc] initWithContentRect:[[primaryWindow_ screen] frame] | 197 initWithContentRect:[[primaryWindow_ screen] frame] |
| 133 styleMask:0 | 198 styleMask:0 |
| 134 backing:NSBackingStoreBuffered | 199 backing:NSBackingStoreBuffered |
| 135 defer:NO]); | 200 defer:NO]); |
| 136 [[snapshotWindow_ contentView] setWantsLayer:YES]; | 201 [[snapshotWindow_ contentView] setWantsLayer:YES]; |
| 137 [snapshotWindow_ setOpaque:NO]; | 202 [snapshotWindow_ setOpaque:NO]; |
| 138 [snapshotWindow_ setBackgroundColor:[NSColor clearColor]]; | 203 [snapshotWindow_ setBackgroundColor:[NSColor clearColor]]; |
| 139 [snapshotWindow_ setAnimationBehavior:NSWindowAnimationBehaviorNone]; | 204 [snapshotWindow_ setAnimationBehavior:NSWindowAnimationBehaviorNone]; |
| 140 | 205 |
| 141 [snapshotWindow_ orderFront:nil]; | |
| 142 [[[snapshotWindow_ contentView] layer] addSublayer:snapshotLayer_]; | 206 [[[snapshotWindow_ contentView] layer] addSublayer:snapshotLayer_]; |
| 143 | 207 |
| 144 // Compute the frame of the snapshot layer such that the snapshot is | 208 // Compute the frame of the snapshot layer such that the snapshot is |
| 145 // positioned exactly on top of the original position of |primaryWindow_|. | 209 // positioned exactly on top of the original position of |primaryWindow_|. |
| 146 NSRect snapshotLayerFrame = | 210 NSRect snapshotLayerFrame = |
| 147 [snapshotWindow_ convertRectFromScreen:[primaryWindow_ frame]]; | 211 [snapshotWindow_ convertRectFromScreen:[primaryWindow_ frame]]; |
| 148 [snapshotLayer_ setFrame:snapshotLayerFrame]; | 212 [snapshotLayer_ setFrame:snapshotLayerFrame]; |
| 149 } | 213 } |
| 150 | 214 |
| 151 - (void)preparePrimaryWindowForAnimation { | 215 - (void)preparePrimaryWindowForAnimation { |
| 152 // Save initial state of |primaryWindow_|. | 216 // Save the initial state of the primary window. |
| 153 primaryWindowInitialFrame_ = [primaryWindow_ frame]; | |
| 154 primaryWindowInitialBackgroundColor_.reset( | 217 primaryWindowInitialBackgroundColor_.reset( |
| 155 [[primaryWindow_ backgroundColor] copy]); | 218 [[primaryWindow_ backgroundColor] copy]); |
| 156 primaryWindowInitialOpaque_ = [primaryWindow_ isOpaque]; | 219 primaryWindowInitialOpaque_ = [primaryWindow_ isOpaque]; |
| 157 | 220 |
| 158 primaryWindowFinalFrame_ = [[primaryWindow_ screen] frame]; | 221 [primaryWindow_ setOpaque:NO]; |
| 159 | 222 |
| 160 // Make |primaryWindow_| invisible. This must happen before the window is | 223 CALayer* root = [self rootLayerOfWindow:primaryWindow_]; |
| 161 // resized, since resizing the window will call drawRect: and cause content | 224 root.opacity = 0; |
| 162 // to flash over the entire screen. | 225 root.frame = initialFrameRelativeToScreen_; |
| 163 [primaryWindow_ setOpaque:NO]; | |
| 164 [primaryWindow_ setBackgroundColor:[NSColor clearColor]]; | |
| 165 CALayer* rootLayer = [self rootLayerOfWindow:primaryWindow_]; | |
| 166 rootLayer.opacity = 0; | |
| 167 | 226 |
| 168 // As soon as the style mask includes the flag NSFullScreenWindowMask, the | 227 if (isEnteringFullscreen) { |
| 169 // window is expected to receive fullscreen layout. This must be set before | 228 [primaryWindow_ |
| 170 // the window is resized, as that causes a relayout. | 229 setStyleMask:[primaryWindow_ styleMask] | NSFullScreenWindowMask]; |
| 171 [primaryWindow_ | 230 [self setPrimaryWindowToFinalFrame]; |
| 172 setStyleMask:[primaryWindow_ styleMask] | NSFullScreenWindowMask]; | 231 } else { |
| 173 | 232 FullSizeContentView* content = |
| 174 // Resize |primaryWindow_|. | 233 base::mac::ObjCCast<FullSizeContentView>([primaryWindow_ contentView]); |
| 175 changingPrimaryWindowSize_ = YES; | 234 [content forceFrameSize:finalFrame_.size]; |
| 176 [primaryWindow_ setFrame:primaryWindowFinalFrame_ display:YES]; | 235 lock_->setLock(YES); |
| 177 changingPrimaryWindowSize_ = NO; | 236 } |
| 178 } | 237 } |
| 179 | 238 |
| 180 - (void)animateSnapshotWindowWithDuration:(CGFloat)duration { | 239 - (void)animateSnapshotWindowWithDuration:(CGFloat)duration { |
| 181 // Move the snapshot layer until it's bottom-left corner is at the | 240 [snapshotWindow_ orderFront:nil]; |
| 182 // bottom-left corner of the screen. | 241 NSRect finalScreenFrame = [snapshotWindow_ convertRectFromScreen:finalFrame_]; |
| 242 | |
| 183 CABasicAnimation* positionAnimation = | 243 CABasicAnimation* positionAnimation = |
| 184 [CABasicAnimation animationWithKeyPath:@"position"]; | 244 [CABasicAnimation animationWithKeyPath:@"position"]; |
| 185 positionAnimation.toValue = [NSValue valueWithPoint:NSZeroPoint]; | 245 positionAnimation.toValue = [NSValue valueWithPoint:finalScreenFrame.origin]; |
| 186 positionAnimation.timingFunction = [CAMediaTimingFunction | 246 positionAnimation.timingFunction = [CAMediaTimingFunction |
| 187 functionWithName:TransformAnimationTimingFunction()]; | 247 functionWithName:TransformAnimationTimingFunction()]; |
| 188 | 248 |
| 189 // Expand the bounds until it covers the screen. | 249 // Resize the bounds until it reaches the expected size at the end of the |
| 190 NSRect finalBounds = NSMakeRect(0, 0, NSWidth(primaryWindowFinalFrame_), | 250 // animation. |
| 191 NSHeight(primaryWindowFinalFrame_)); | 251 NSRect finalBounds = |
| 252 NSMakeRect(0, 0, NSWidth(finalFrame_), NSHeight(finalFrame_)); | |
| 192 CABasicAnimation* boundsAnimation = | 253 CABasicAnimation* boundsAnimation = |
| 193 [CABasicAnimation animationWithKeyPath:@"bounds"]; | 254 [CABasicAnimation animationWithKeyPath:@"bounds"]; |
| 194 boundsAnimation.toValue = [NSValue valueWithRect:finalBounds]; | 255 boundsAnimation.toValue = [NSValue valueWithRect:finalBounds]; |
| 195 boundsAnimation.timingFunction = [CAMediaTimingFunction | 256 boundsAnimation.timingFunction = [CAMediaTimingFunction |
| 196 functionWithName:TransformAnimationTimingFunction()]; | 257 functionWithName:TransformAnimationTimingFunction()]; |
| 197 | 258 |
| 198 // Fade out the snapshot layer. | 259 // Fade out the snapshot layer. |
| 199 CABasicAnimation* opacityAnimation = | 260 CABasicAnimation* opacityAnimation = |
| 200 [CABasicAnimation animationWithKeyPath:@"opacity"]; | 261 [CABasicAnimation animationWithKeyPath:@"opacity"]; |
| 201 opacityAnimation.toValue = @(0.0); | 262 opacityAnimation.toValue = @(0.0); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 220 // set back to 1. There are a couple of ways to do this. The easiest is to | 281 // set back to 1. There are a couple of ways to do this. The easiest is to |
| 221 // just have a dummy animation as part of the same animation group. | 282 // just have a dummy animation as part of the same animation group. |
| 222 CABasicAnimation* opacityAnimation = | 283 CABasicAnimation* opacityAnimation = |
| 223 [CABasicAnimation animationWithKeyPath:@"opacity"]; | 284 [CABasicAnimation animationWithKeyPath:@"opacity"]; |
| 224 opacityAnimation.fromValue = @(1.0); | 285 opacityAnimation.fromValue = @(1.0); |
| 225 opacityAnimation.toValue = @(1.0); | 286 opacityAnimation.toValue = @(1.0); |
| 226 | 287 |
| 227 // The root layer's size should start scaled down to the initial size of | 288 // The root layer's size should start scaled down to the initial size of |
| 228 // |primaryWindow_|. The animation increases the size until the root layer | 289 // |primaryWindow_|. The animation increases the size until the root layer |
| 229 // fills the screen. | 290 // fills the screen. |
| 230 NSRect initialFrame = primaryWindowInitialFrame_; | 291 NSRect initialFrame = initialFrame_; |
| 231 NSRect endFrame = primaryWindowFinalFrame_; | 292 NSRect endFrame = finalFrame_; |
| 232 CGFloat xScale = NSWidth(initialFrame) / NSWidth(endFrame); | 293 CGFloat xScale = NSWidth(initialFrame) / NSWidth(endFrame); |
| 233 CGFloat yScale = NSHeight(initialFrame) / NSHeight(endFrame); | 294 CGFloat yScale = NSHeight(initialFrame) / NSHeight(endFrame); |
| 234 CATransform3D initial = CATransform3DMakeScale(xScale, yScale, 1); | 295 CATransform3D initial = CATransform3DMakeScale(xScale, yScale, 1); |
| 235 CABasicAnimation* transformAnimation = | 296 CABasicAnimation* transformAnimation = |
| 236 [CABasicAnimation animationWithKeyPath:@"transform"]; | 297 [CABasicAnimation animationWithKeyPath:@"transform"]; |
| 237 transformAnimation.fromValue = [NSValue valueWithCATransform3D:initial]; | 298 transformAnimation.fromValue = [NSValue valueWithCATransform3D:initial]; |
| 238 | 299 |
| 239 CALayer* root = [self rootLayerOfWindow:primaryWindow_]; | |
| 240 | |
| 241 // Calculate the initial position of the root layer. This calculation is | 300 // Calculate the initial position of the root layer. This calculation is |
| 242 // agnostic of the anchorPoint. | 301 // agnostic of the anchorPoint. |
| 302 CALayer* root = [self rootLayerOfWindow:primaryWindow_]; | |
| 243 CGFloat layerStartPositionDeltaX = NSMidX(initialFrame) - NSMidX(endFrame); | 303 CGFloat layerStartPositionDeltaX = NSMidX(initialFrame) - NSMidX(endFrame); |
| 244 CGFloat layerStartPositionDeltaY = NSMidY(initialFrame) - NSMidY(endFrame); | 304 CGFloat layerStartPositionDeltaY = NSMidY(initialFrame) - NSMidY(endFrame); |
| 245 NSPoint layerStartPosition = | 305 NSPoint layerStartPosition = |
| 246 NSMakePoint(root.position.x + layerStartPositionDeltaX, | 306 NSMakePoint(root.position.x + layerStartPositionDeltaX, |
| 247 root.position.y + layerStartPositionDeltaY); | 307 root.position.y + layerStartPositionDeltaY); |
| 248 | 308 |
| 249 // Animate the primary window from its initial position. | 309 // Animate the primary window from its initial position. |
| 250 CABasicAnimation* positionAnimation = | 310 CABasicAnimation* positionAnimation = |
| 251 [CABasicAnimation animationWithKeyPath:@"position"]; | 311 [CABasicAnimation animationWithKeyPath:@"position"]; |
| 252 positionAnimation.fromValue = [NSValue valueWithPoint:layerStartPosition]; | 312 positionAnimation.fromValue = [NSValue valueWithPoint:layerStartPosition]; |
| 253 | 313 |
| 254 CAAnimationGroup* group = [CAAnimationGroup animation]; | 314 CAAnimationGroup* group = [CAAnimationGroup animation]; |
| 255 group.removedOnCompletion = NO; | 315 group.removedOnCompletion = NO; |
| 256 group.fillMode = kCAFillModeForwards; | 316 group.fillMode = kCAFillModeForwards; |
| 257 group.animations = | 317 group.animations = |
| 258 @[ transformAnimation, opacityAnimation, positionAnimation ]; | 318 @[ opacityAnimation, positionAnimation, transformAnimation ]; |
| 259 group.timingFunction = [CAMediaTimingFunction | 319 group.timingFunction = [CAMediaTimingFunction |
| 260 functionWithName:TransformAnimationTimingFunction()]; | 320 functionWithName:TransformAnimationTimingFunction()]; |
| 261 group.duration = duration; | 321 group.duration = duration; |
| 262 [group setValue:kPrimaryWindowAnimationID forKey:kAnimationIDKey]; | 322 [group setValue:kPrimaryWindowAnimationID forKey:kAnimationIDKey]; |
| 263 group.delegate = self; | 323 group.delegate = self; |
| 264 | 324 |
| 265 [root addAnimation:group forKey:kPrimaryWindowAnimationID]; | 325 [root addAnimation:group forKey:kPrimaryWindowAnimationID]; |
| 266 } | 326 } |
| 267 | 327 |
| 328 - (void)setPrimaryWindowToFinalFrame { | |
| 329 changingPrimaryWindowSize_ = YES; | |
| 330 [primaryWindow_ setFrame:finalFrame_ display:NO]; | |
| 331 changingPrimaryWindowSize_ = NO; | |
| 332 } | |
| 333 | |
| 268 - (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)flag { | 334 - (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)flag { |
| 269 NSString* animationID = [theAnimation valueForKey:kAnimationIDKey]; | 335 NSString* animationID = [theAnimation valueForKey:kAnimationIDKey]; |
| 336 | |
| 337 // Remove the snapshot window. | |
| 270 if ([animationID isEqual:kSnapshotWindowAnimationID]) { | 338 if ([animationID isEqual:kSnapshotWindowAnimationID]) { |
| 271 [snapshotWindow_ orderOut:nil]; | 339 [snapshotWindow_ orderOut:nil]; |
| 272 snapshotWindow_.reset(); | 340 snapshotWindow_.reset(); |
| 273 snapshotLayer_.reset(); | 341 snapshotLayer_.reset(); |
| 274 return; | 342 return; |
| 275 } | 343 } |
| 276 | 344 |
| 277 if ([animationID isEqual:kPrimaryWindowAnimationID]) { | 345 if ([animationID isEqual:kPrimaryWindowAnimationID]) { |
| 278 [primaryWindow_ setOpaque:YES]; | 346 // If we're exiting full screen, we want to set the primary window |
| 347 // size to the expected frame at the end. The window's frame will also | |
| 348 // need to be unlocked. | |
| 349 if (!isEnteringFullscreen) { | |
| 350 lock_->setLock(NO); | |
| 351 [primaryWindow_ | |
| 352 setStyleMask:[primaryWindow_ styleMask] & ~NSFullScreenWindowMask]; | |
| 353 [self setPrimaryWindowToFinalFrame]; | |
| 354 } | |
| 355 | |
| 356 // Restore the state of the primary window and make it visible again. | |
| 357 [primaryWindow_ setOpaque:primaryWindowInitialOpaque_]; | |
| 279 [primaryWindow_ setBackgroundColor:primaryWindowInitialBackgroundColor_]; | 358 [primaryWindow_ setBackgroundColor:primaryWindowInitialBackgroundColor_]; |
| 359 | |
| 280 CALayer* root = [self rootLayerOfWindow:primaryWindow_]; | 360 CALayer* root = [self rootLayerOfWindow:primaryWindow_]; |
| 361 [root removeAnimationForKey:kPrimaryWindowAnimationID]; | |
| 281 root.opacity = 1; | 362 root.opacity = 1; |
| 282 [root removeAnimationForKey:kPrimaryWindowAnimationID]; | |
| 283 } | 363 } |
| 284 } | 364 } |
| 285 | 365 |
| 286 - (CALayer*)rootLayerOfWindow:(NSWindow*)window { | 366 - (CALayer*)rootLayerOfWindow:(NSWindow*)window { |
| 287 return [[[window contentView] superview] layer]; | 367 return [[[window contentView] superview] layer]; |
| 288 } | 368 } |
| 289 | 369 |
| 370 - (NSSize)getDesiredWindowLayoutSize { | |
| 371 return (isEnteringFullscreen) ? [primaryWindow_ frame].size | |
| 372 : [[primaryWindow_ contentView] bounds].size; | |
| 373 } | |
| 374 | |
| 290 @end | 375 @end |
| OLD | NEW |