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

Unified Diff: chrome/browser/ui/cocoa/browser_window_fullscreen_transition.mm

Issue 1276383004: Implemented fullscreen exit animation with AppKit (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactored the code to log UMA metrics Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/cocoa/browser_window_fullscreen_transition.mm
diff --git a/chrome/browser/ui/cocoa/browser_window_enter_fullscreen_transition.mm b/chrome/browser/ui/cocoa/browser_window_fullscreen_transition.mm
similarity index 61%
rename from chrome/browser/ui/cocoa/browser_window_enter_fullscreen_transition.mm
rename to chrome/browser/ui/cocoa/browser_window_fullscreen_transition.mm
index 7eca5ec2087b2c953a1e2a21254a7a57f1700c26..e410c10631249011ccd457c72139394d1cab1aca 100644
--- a/chrome/browser/ui/cocoa/browser_window_enter_fullscreen_transition.mm
+++ b/chrome/browser/ui/cocoa/browser_window_fullscreen_transition.mm
@@ -2,13 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#import "chrome/browser/ui/cocoa/browser_window_enter_fullscreen_transition.h"
+#import "chrome/browser/ui/cocoa/browser_window_fullscreen_transition.h"
#include <QuartzCore/QuartzCore.h>
-#include "base/mac/scoped_cftyperef.h"
-#include "base/mac/scoped_nsobject.h"
-#include "base/mac/sdk_forward_declarations.h"
+#include "base/mac/bind_objc_block.h"
+#include "base/mac/foundation_util.h"
+#include "base/mac/mac_util.h"
+#import "base/mac/scoped_nsobject.h"
+#import "base/mac/sdk_forward_declarations.h"
+#include "base/memory/scoped_ptr.h"
+#import "chrome/browser/ui/cocoa/full_size_content_window.h"
namespace {
@@ -23,11 +27,29 @@ NSString* TransformAnimationTimingFunction() {
return kCAMediaTimingFunctionEaseInEaseOut;
}
+// 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.
+// ensures that the lock gets released.
+class FrameAndStyleLock {
+ public:
+ base::scoped_nsobject<FramedBrowserWindow> window_;
erikchen 2015/08/11 22:42:55 window_ should be private.
spqchan1 2015/08/12 19:34:42 Done.
+
+ FrameAndStyleLock(FramedBrowserWindow* window) {
+ window_.reset([window retain]);
+ }
+
+ void setLock(bool lock) { [window_ setFrameAndStyleMaskLock:lock]; }
+
+ ~FrameAndStyleLock() { setLock(NO); }
+};
+
} // namespace
-@interface BrowserWindowEnterFullscreenTransition () {
+@interface BrowserWindowFullscreenTransition () {
+ // Flag to keep track of whether we are entering or exiting full screen
+ BOOL isEnteringFullscreen;
+
// The window which is undergoing the fullscreen transition.
- base::scoped_nsobject<NSWindow> primaryWindow_;
+ base::scoped_nsobject<FramedBrowserWindow> primaryWindow_;
// A layer that holds a snapshot of the original state of |primaryWindow_|.
base::scoped_nsobject<CALayer> snapshotLayer_;
@@ -35,9 +57,6 @@ NSString* TransformAnimationTimingFunction() {
// A temporary window that holds |snapshotLayer_|.
base::scoped_nsobject<NSWindow> snapshotWindow_;
- // The frame of the |primaryWindow_| before the transition began.
- NSRect primaryWindowInitialFrame_;
-
// The background color of |primaryWindow_| before the transition began.
base::scoped_nsobject<NSColor> primaryWindowInitialBackgroundColor_;
@@ -48,9 +67,19 @@ NSString* TransformAnimationTimingFunction() {
// |primaryWindow_|.
BOOL changingPrimaryWindowSize_;
+ // The frame of the |primaryWindow_| before it starts the transition.
+ NSRect initialFrame_;
+
+ // A frame that represents the value of the |initialFrame_| that's relative
+ // to the current screen coordinates
erikchen 2015/08/11 22:42:55 missing a period
spqchan1 2015/08/12 19:34:41 Done.
+ NSRect initialFrameRelativeToScreen_;
+
// The frame that |primaryWindow_| is expected to have after the transition
// is finished.
- NSRect primaryWindowFinalFrame_;
+ NSRect finalFrame_;
+
+ // Locks and unlocks the FullSizeContentWindow.
+ scoped_ptr<FrameAndStyleLock> lock_;
}
// Takes a snapshot of |primaryWindow_| and puts it in |snapshotLayer_|.
@@ -62,48 +91,84 @@ NSString* TransformAnimationTimingFunction() {
// This method has several effects on |primaryWindow_|:
// - Saves current state.
// - Makes window transparent, with clear background.
-// - Adds NSFullScreenWindowMask style mask.
-// - Sets the size to the screen's size.
+// - If we are entering full screen, it will also:
+// - Adds NSFullScreenWindowMask style mask.
+// - 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.
- (void)preparePrimaryWindowForAnimation;
+// Returns the windows to be used in the custom transition.
+// - Takes a snapshot of the current window.
+// - Makes a new snapshot window which shows the snapshot in the same
+// location as the current window.
+// - Adds the style mask NSFullScreenWindowMask to the current window.
+// - Makes the current window transparent, and resizes the current window to
+// be the same size as the screen.
+- (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
+
// Applies the fullscreen animation to |snapshotLayer_|.
- (void)animateSnapshotWindowWithDuration:(CGFloat)duration;
-// Applies the fullscreen animation to the root layer of |primaryWindow_|.
-- (void)animatePrimaryWindowWithDuration:(CGFloat)duration;
-
-// Override of CAAnimation delegate method.
-- (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)flag;
+- (void)setPrimaryWindowToFinalFrame;
-// Returns the layer of the root view of |window|.
- (CALayer*)rootLayerOfWindow:(NSWindow*)window;
@end
-@implementation BrowserWindowEnterFullscreenTransition
+@implementation BrowserWindowFullscreenTransition
// -------------------------Public Methods----------------------------
-- (instancetype)initWithWindow:(NSWindow*)window {
+- (instancetype)initEnterWithWindow:(FramedBrowserWindow*)window {
+ DCHECK(window);
+ DCHECK([self rootLayerOfWindow:window]);
+ if ((self = [super init])) {
+ primaryWindow_.reset([window retain]);
+
+ isEnteringFullscreen = YES;
+ initialFrame_ = [primaryWindow_ frame];
+ finalFrame_ = [[primaryWindow_ screen] frame];
+ }
+ return self;
+}
+
+- (instancetype)initExitWithWindow:(FramedBrowserWindow*)window
+ frame:(NSRect)frame {
DCHECK(window);
DCHECK([self rootLayerOfWindow:window]);
if ((self = [super init])) {
primaryWindow_.reset([window retain]);
+
+ isEnteringFullscreen = NO;
+ finalFrame_ = frame;
+ initialFrame_ = [[primaryWindow_ screen] frame];
+
+ // Calculate the inital frame that is relative to coordinates of the
+ // screen which it's on.
+ initialFrameRelativeToScreen_ = finalFrame_;
+ CGFloat screenOriginX = finalFrame_.origin.x - initialFrame_.origin.x;
+ CGFloat screenOriginY = finalFrame_.origin.y - initialFrame_.origin.y;
+ initialFrameRelativeToScreen_.origin =
+ CGPointMake(screenOriginX, screenOriginY);
+
+ lock_.reset(new FrameAndStyleLock(window));
}
return self;
}
-- (NSArray*)customWindowsToEnterFullScreen {
+- (NSArray*)customWindowsForFullScreenTransition {
[self takeSnapshot];
[self makeAndPrepareSnapshotWindow];
- [self preparePrimaryWindowForAnimation];
+
return @[ primaryWindow_.get(), snapshotWindow_.get() ];
}
-- (void)startCustomAnimationToEnterFullScreenWithDuration:
- (NSTimeInterval)duration {
- [self animateSnapshotWindowWithDuration:duration];
+// Right before the animation begins, change the content view size
+// and lock the primary window so that OSX can't make changes to it
+// before we finish the animation.
+- (void)startCustomFullScreenAnimationWithDuration:(NSTimeInterval)duration {
+ [self preparePrimaryWindowForAnimation];
[self animatePrimaryWindowWithDuration:duration];
+ [self animateSnapshotWindowWithDuration:duration];
}
- (BOOL)shouldWindowBeUnconstrained {
@@ -128,17 +193,16 @@ NSString* TransformAnimationTimingFunction() {
- (void)makeAndPrepareSnapshotWindow {
DCHECK(snapshotLayer_);
- snapshotWindow_.reset(
- [[NSWindow alloc] initWithContentRect:[[primaryWindow_ screen] frame]
- styleMask:0
- backing:NSBackingStoreBuffered
- defer:NO]);
+ snapshotWindow_.reset([[NSWindow alloc]
+ initWithContentRect:[[primaryWindow_ screen] frame]
+ styleMask:0
+ backing:NSBackingStoreBuffered
+ defer:NO]);
[[snapshotWindow_ contentView] setWantsLayer:YES];
[snapshotWindow_ setOpaque:NO];
[snapshotWindow_ setBackgroundColor:[NSColor clearColor]];
[snapshotWindow_ setAnimationBehavior:NSWindowAnimationBehaviorNone];
- [snapshotWindow_ orderFront:nil];
[[[snapshotWindow_ contentView] layer] addSublayer:snapshotLayer_];
// Compute the frame of the snapshot layer such that the snapshot is
@@ -149,46 +213,43 @@ NSString* TransformAnimationTimingFunction() {
}
- (void)preparePrimaryWindowForAnimation {
- // Save initial state of |primaryWindow_|.
- primaryWindowInitialFrame_ = [primaryWindow_ frame];
+ // Save the initial state of the primary window.
primaryWindowInitialBackgroundColor_.reset(
[[primaryWindow_ backgroundColor] copy]);
primaryWindowInitialOpaque_ = [primaryWindow_ isOpaque];
- primaryWindowFinalFrame_ = [[primaryWindow_ screen] frame];
-
- // Make |primaryWindow_| invisible. This must happen before the window is
- // resized, since resizing the window will call drawRect: and cause content
- // to flash over the entire screen.
[primaryWindow_ setOpaque:NO];
- [primaryWindow_ setBackgroundColor:[NSColor clearColor]];
- CALayer* rootLayer = [self rootLayerOfWindow:primaryWindow_];
- rootLayer.opacity = 0;
-
- // As soon as the style mask includes the flag NSFullScreenWindowMask, the
- // window is expected to receive fullscreen layout. This must be set before
- // the window is resized, as that causes a relayout.
- [primaryWindow_
- setStyleMask:[primaryWindow_ styleMask] | NSFullScreenWindowMask];
- // Resize |primaryWindow_|.
- changingPrimaryWindowSize_ = YES;
- [primaryWindow_ setFrame:primaryWindowFinalFrame_ display:YES];
- changingPrimaryWindowSize_ = NO;
+ CALayer* root = [self rootLayerOfWindow:primaryWindow_];
+ root.opacity = 0;
+ root.frame = initialFrameRelativeToScreen_;
+
+ if (isEnteringFullscreen) {
+ [primaryWindow_
+ setStyleMask:[primaryWindow_ styleMask] | NSFullScreenWindowMask];
+ [self setPrimaryWindowToFinalFrame];
+ } else {
+ FullSizeContentView* content =
+ base::mac::ObjCCast<FullSizeContentView>([primaryWindow_ contentView]);
+ [content forceFrameSize:finalFrame_.size];
+ lock_->setLock(YES);
+ }
}
- (void)animateSnapshotWindowWithDuration:(CGFloat)duration {
- // Move the snapshot layer until it's bottom-left corner is at the
- // bottom-left corner of the screen.
+ [snapshotWindow_ orderFront:nil];
+ NSRect finalScreenFrame = [snapshotWindow_ convertRectFromScreen:finalFrame_];
+
CABasicAnimation* positionAnimation =
[CABasicAnimation animationWithKeyPath:@"position"];
- positionAnimation.toValue = [NSValue valueWithPoint:NSZeroPoint];
+ positionAnimation.toValue = [NSValue valueWithPoint:finalScreenFrame.origin];
positionAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:TransformAnimationTimingFunction()];
- // Expand the bounds until it covers the screen.
- NSRect finalBounds = NSMakeRect(0, 0, NSWidth(primaryWindowFinalFrame_),
- NSHeight(primaryWindowFinalFrame_));
+ // Resize the bounds until it reaches the expected size at the end of the
+ // animation.
+ NSRect finalBounds =
+ NSMakeRect(0, 0, NSWidth(finalFrame_), NSHeight(finalFrame_));
CABasicAnimation* boundsAnimation =
[CABasicAnimation animationWithKeyPath:@"bounds"];
boundsAnimation.toValue = [NSValue valueWithRect:finalBounds];
@@ -227,8 +288,8 @@ NSString* TransformAnimationTimingFunction() {
// The root layer's size should start scaled down to the initial size of
// |primaryWindow_|. The animation increases the size until the root layer
// fills the screen.
- NSRect initialFrame = primaryWindowInitialFrame_;
- NSRect endFrame = primaryWindowFinalFrame_;
+ NSRect initialFrame = initialFrame_;
+ NSRect endFrame = finalFrame_;
CGFloat xScale = NSWidth(initialFrame) / NSWidth(endFrame);
CGFloat yScale = NSHeight(initialFrame) / NSHeight(endFrame);
CATransform3D initial = CATransform3DMakeScale(xScale, yScale, 1);
@@ -236,10 +297,9 @@ NSString* TransformAnimationTimingFunction() {
[CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.fromValue = [NSValue valueWithCATransform3D:initial];
- CALayer* root = [self rootLayerOfWindow:primaryWindow_];
-
// Calculate the initial position of the root layer. This calculation is
// agnostic of the anchorPoint.
+ CALayer* root = [self rootLayerOfWindow:primaryWindow_];
CGFloat layerStartPositionDeltaX = NSMidX(initialFrame) - NSMidX(endFrame);
CGFloat layerStartPositionDeltaY = NSMidY(initialFrame) - NSMidY(endFrame);
NSPoint layerStartPosition =
@@ -255,7 +315,7 @@ NSString* TransformAnimationTimingFunction() {
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
group.animations =
- @[ transformAnimation, opacityAnimation, positionAnimation ];
+ @[ opacityAnimation, positionAnimation, transformAnimation ];
group.timingFunction = [CAMediaTimingFunction
functionWithName:TransformAnimationTimingFunction()];
group.duration = duration;
@@ -265,8 +325,16 @@ NSString* TransformAnimationTimingFunction() {
[root addAnimation:group forKey:kPrimaryWindowAnimationID];
}
+- (void)setPrimaryWindowToFinalFrame {
+ changingPrimaryWindowSize_ = YES;
+ [primaryWindow_ setFrame:finalFrame_ display:NO];
+ changingPrimaryWindowSize_ = NO;
+}
+
- (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)flag {
NSString* animationID = [theAnimation valueForKey:kAnimationIDKey];
+
+ // Remove the snapshot window.
if ([animationID isEqual:kSnapshotWindowAnimationID]) {
[snapshotWindow_ orderOut:nil];
snapshotWindow_.reset();
@@ -275,11 +343,23 @@ NSString* TransformAnimationTimingFunction() {
}
if ([animationID isEqual:kPrimaryWindowAnimationID]) {
- [primaryWindow_ setOpaque:YES];
+ // If we're exiting full screen, we want to set the primary window
+ // size to the expected frame at the end. The window's frame will also
+ // need to be unlocked.
+ if (!isEnteringFullscreen) {
+ lock_->setLock(NO);
+ [primaryWindow_
+ setStyleMask:[primaryWindow_ styleMask] & ~NSFullScreenWindowMask];
+ [self setPrimaryWindowToFinalFrame];
+ }
+
+ // Restore the state of the primary window and make it visible again.
+ [primaryWindow_ setOpaque:primaryWindowInitialOpaque_];
[primaryWindow_ setBackgroundColor:primaryWindowInitialBackgroundColor_];
+
CALayer* root = [self rootLayerOfWindow:primaryWindow_];
- root.opacity = 1;
[root removeAnimationForKey:kPrimaryWindowAnimationID];
+ root.opacity = 1;
}
}
@@ -287,4 +367,9 @@ NSString* TransformAnimationTimingFunction() {
return [[[window contentView] superview] layer];
}
+- (NSSize)getDesiredWindowLayoutSize {
+ return (isEnteringFullscreen) ? [primaryWindow_ frame].size
+ : [[primaryWindow_ contentView] bounds].size;
+}
+
@end

Powered by Google App Engine
This is Rietveld 408576698