1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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_fullscreen_transition.h"
#include <QuartzCore/QuartzCore.h>
#include <memory>
#include "base/mac/bind_objc_block.h"
#include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h"
#import "base/mac/sdk_forward_declarations.h"
#include "base/macros.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#import "chrome/browser/ui/cocoa/framed_browser_window.h"
#import "chrome/browser/ui/cocoa/tabs/tab_strip_background_view.h"
namespace {
NSString* const kPrimaryWindowAnimationID = @"PrimaryWindowAnimationID";
NSString* const kSnapshotWindowAnimationID = @"SnapshotWindowAnimationID";
NSString* const kAnimationIDKey = @"AnimationIDKey";
// The fraction of the duration from AppKit's startCustomAnimation methods
// that we want our animation to run in. Yosemite's fraction is smaller
// since its fullscreen transition is significantly slower.
CGFloat const kAnimationDurationFraction = 0.5;
CGFloat const kAnimationDurationFractionYosemite = 0.3;
// This class has two simultaneous animations to resize and reposition layers.
// These animations must use the same timing function, otherwise there will be
// visual discordance.
NSString* TransformAnimationTimingFunction() {
return kCAMediaTimingFunctionEaseInEaseOut;
}
// This class locks and unlocks the FrameBrowserWindow. Its destructor ensures
// that the lock gets released.
class FrameAndStyleLock {
public:
explicit FrameAndStyleLock(FramedBrowserWindow* window) : window_(window) {}
~FrameAndStyleLock() { set_lock(NO); }
void set_lock(bool lock) { [window_ setStyleMaskLock:lock]; }
private:
FramedBrowserWindow* window_; // weak
DISALLOW_COPY_AND_ASSIGN(FrameAndStyleLock);
};
} // namespace
// This view draws a dummy toolbar over the resized content view during
// the exit fullscreen animation. It is removed at the end of the animation.
@interface FullscreenTabStripBackgroundView : NSView {
base::scoped_nsobject<NSColor> windowBackgroundColor_;
}
- (instancetype)initWithFrame:(NSRect)frame background:(NSColor*)color;
@end
@implementation FullscreenTabStripBackgroundView
- (instancetype)initWithFrame:(NSRect)frame background:(NSColor*)color {
if ((self = [super initWithFrame:frame])) {
windowBackgroundColor_.reset([color copy]);
}
return self;
}
// Override this method so that we can paint the toolbar in this view.
// This method first fill itself with the toolbar's background. After that,
// it will paint the window's theme if applicable.
- (void)drawRect:(NSRect)frame {
[windowBackgroundColor_ set];
NSRectFillUsingOperation(frame, NSCompositeDestinationOver);
[FramedBrowserWindow drawWindowThemeInDirtyRect:frame
forView:self
bounds:[self bounds]
forceBlackBackground:NO];
}
@end
@interface BrowserWindowFullscreenTransition ()
<CAAnimationDelegate, CALayerDelegate> {
// Flag to keep track of whether we are entering or exiting fullscreen.
BOOL isEnteringFullscreen_;
// The window which is undergoing the fullscreen transition.
base::scoped_nsobject<FramedBrowserWindow> primaryWindow_;
// The window which is undergoing the fullscreen transition.
BrowserWindowController* controller_; // weak
// A layer that holds a snapshot of the original state of |primaryWindow_|.
base::scoped_nsobject<CALayer> snapshotLayer_;
// A temporary window that holds |snapshotLayer_|.
base::scoped_nsobject<NSWindow> snapshotWindow_;
// The tabstrip background view in the window. During the exit fullscreen
// animation, this view be hidden while a dummy tabstrip background will be
// drawn over the content view.
base::scoped_nsobject<NSView> tabStripBackgroundView_;
// The background color of |primaryWindow_| before the transition began.
base::scoped_nsobject<NSColor> primaryWindowInitialBackgroundColor_;
// Whether |primaryWindow_| was opaque before the transition began.
BOOL primaryWindowInitialOpaque_;
// The initial anchor point of the root layer.
CGPoint initialRootAnchorPoint_;
// The initial origin of the content view.
NSPoint initialContentViewOrigin_;
// Whether the instance is in the process of changing the size of
// |primaryWindow_|.
BOOL changingPrimaryWindowSize_;
// The frame of the |primaryWindow_| before it starts the transition.
NSRect initialFrame_;
// The frame that |primaryWindow_| is expected to have after the transition
// is finished.
NSRect finalFrame_;
// This view draws the tabstrip background during the exit animation.
base::scoped_nsobject<FullscreenTabStripBackgroundView>
fullscreenTabStripBackgroundView_;
// Locks and unlocks the FullSizeContentWindow.
std::unique_ptr<FrameAndStyleLock> lock_;
// Flag that indicates if the animation was completed. Sets to true at the
// end of the animation.
BOOL completedTransition_;
}
// Takes a snapshot of |primaryWindow_| and puts it in |snapshotLayer_|.
- (void)takeSnapshot;
// Creates |snapshotWindow_| and adds |snapshotLayer_| to it.
- (void)makeAndPrepareSnapshotWindow;
// This method has several effects on |primaryWindow_|:
// - Saves current state.
// - Makes window transparent, with clear background.
// - If we are entering fullscreen, it will also:
// - Add NSFullScreenWindowMask style mask.
// - Set the size to the screen's size.
- (void)preparePrimaryWindowForAnimation;
// Applies the fullscreen animation to |snapshotLayer_|.
- (void)animateSnapshotWindowWithDuration:(CGFloat)duration;
// Sets |primaryWindow_|'s frame to the expected frame.
- (void)changePrimaryWindowToFinalFrame;
// Overrides of CAAnimation delegate methods.
- (void)animationDidStart:(CAAnimation*)theAnimation;
- (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)finished;
// Returns the layer of the root view of |window|.
- (CALayer*)rootLayerOfWindow:(NSWindow*)window;
// Convert the point to be relative to the screen the primary window is on.
// This is important because if we're using multiple screens, the coordinate
// system extends to the second screen.
//
// For example, if the screen width is 1440, the second screen's frame origin
// is located at (1440, 0) and any x coordinate on the second screen will be
// >= 1440. If we move a window on the first screen to the same location on
// second screen, the window's frame origin will change from (x, y) to
// (x + 1440, y).
//
// When we animate the window, we want to use (x, y), the coordinates that are
// relative to the second screen. As a result, we use this method to convert a
// NSPoint so that it's relative to the screen it's on.
- (NSPoint)pointRelativeToCurrentScreen:(NSPoint)point;
@end
@implementation BrowserWindowFullscreenTransition
// -------------------------Public Methods----------------------------
- (instancetype)initEnterWithController:(BrowserWindowController*)controller {
DCHECK(controller);
DCHECK([self rootLayerOfWindow:[controller window]]);
if ((self = [super init])) {
controller_ = controller;
FramedBrowserWindow* framedBrowserWindow =
base::mac::ObjCCast<FramedBrowserWindow>([controller window]);
primaryWindow_.reset([framedBrowserWindow retain]);
isEnteringFullscreen_ = YES;
initialFrame_ = [primaryWindow_ frame];
finalFrame_ = [[primaryWindow_ screen] frame];
}
return self;
}
- (instancetype)initExitWithController:(BrowserWindowController*)controller {
DCHECK(controller);
DCHECK([self rootLayerOfWindow:[controller window]]);
if ((self = [super init])) {
controller_ = controller;
FramedBrowserWindow* framedBrowserWindow =
base::mac::ObjCCast<FramedBrowserWindow>([controller window]);
primaryWindow_.reset([framedBrowserWindow retain]);
isEnteringFullscreen_ = NO;
initialFrame_ = [[primaryWindow_ screen] frame];
finalFrame_ = [controller savedRegularWindowFrame];
tabStripBackgroundView_.reset([[controller tabStripBackgroundView] retain]);
lock_.reset(new FrameAndStyleLock(framedBrowserWindow));
}
return self;
}
- (NSArray*)customWindowsForFullScreenTransition {
[self takeSnapshot];
[self makeAndPrepareSnapshotWindow];
return @[ primaryWindow_.get(), snapshotWindow_.get() ];
}
- (BOOL)isTransitionCompleted {
return completedTransition_;
}
- (void)startCustomFullScreenAnimationWithDuration:(NSTimeInterval)duration {
CGFloat durationFraction = base::mac::IsOS10_10()
? kAnimationDurationFractionYosemite
: kAnimationDurationFraction;
CGFloat animationDuration = duration * durationFraction;
[self preparePrimaryWindowForAnimation];
[self animatePrimaryWindowWithDuration:animationDuration];
[self animateSnapshotWindowWithDuration:animationDuration];
}
- (BOOL)shouldWindowBeUnconstrained {
return changingPrimaryWindowSize_;
}
- (NSSize)desiredWindowLayoutSize {
return isEnteringFullscreen_ ? [primaryWindow_ frame].size
: [[primaryWindow_ contentView] bounds].size;
}
- (void)browserWillBeDestroyed {
CALayer* root = [self rootLayerOfWindow:primaryWindow_];
[root removeAllAnimations];
[snapshotLayer_ removeAllAnimations];
controller_ = nil;
}
// -------------------------Private Methods----------------------------
- (void)takeSnapshot {
base::ScopedCFTypeRef<CGImageRef> windowSnapshot(CGWindowListCreateImage(
CGRectNull, kCGWindowListOptionIncludingWindow,
[primaryWindow_ windowNumber], kCGWindowImageBoundsIgnoreFraming));
snapshotLayer_.reset([[CALayer alloc] init]);
[snapshotLayer_ setFrame:NSRectToCGRect([primaryWindow_ frame])];
[snapshotLayer_ setContents:static_cast<id>(windowSnapshot.get())];
[snapshotLayer_ setAnchorPoint:CGPointMake(0, 0)];
CGColorRef colorRef = CGColorCreateGenericRGB(0, 0, 0, 0);
[snapshotLayer_ setBackgroundColor:colorRef];
CGColorRelease(colorRef);
}
- (void)makeAndPrepareSnapshotWindow {
DCHECK(snapshotLayer_);
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_ contentView] layer] addSublayer:snapshotLayer_];
// Compute the frame of the snapshot layer such that the snapshot is
// positioned exactly on top of the original position of |primaryWindow_|.
NSRect snapshotLayerFrame =
[snapshotWindow_ convertRectFromScreen:[primaryWindow_ frame]];
[snapshotLayer_ setFrame:snapshotLayerFrame];
// If the primary window is in fullscreen mode, we can't move the snapshot
// window in front of it. As a result, at the beginning of the transition to
// exit fullscreen, we should order the snapshot window to the front ASAP.
if (isEnteringFullscreen_)
[snapshotWindow_ orderFront:nil];
}
- (void)preparePrimaryWindowForAnimation {
// Save the initial state of the primary window.
primaryWindowInitialBackgroundColor_.reset(
[[primaryWindow_ backgroundColor] copy]);
primaryWindowInitialOpaque_ = [primaryWindow_ isOpaque];
// 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];
if (isEnteringFullscreen_) {
// 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.
CALayer* root = [self rootLayerOfWindow:primaryWindow_];
root.opacity = 0;
[primaryWindow_
setStyleMask:[primaryWindow_ styleMask] | NSFullScreenWindowMask];
[self changePrimaryWindowToFinalFrame];
} else {
[snapshotWindow_ orderFront:nil];
NSView* contentView = [primaryWindow_ contentView];
NSView* rootView = [contentView superview];
// Since only the content view is resized, the window's background
// must be transparent. This is a hack that forces the layer to remove
// the textured background and replace it with clearColor.
[rootView setWantsLayer:NO];
[primaryWindow_ setBackgroundColor:[NSColor clearColor]];
[primaryWindow_ setStyleMask:[primaryWindow_ styleMask] &
~NSTexturedBackgroundWindowMask];
[rootView setWantsLayer:YES];
CALayer* root = [self rootLayerOfWindow:primaryWindow_];
root.opacity = 0;
// Right before the animation begins, change the contentView size to the
// expected size at the end of the animation. Afterwards, lock the
// |primaryWindow_| so that AppKit will not be able to make unwanted
// changes to it during the animation.
initialContentViewOrigin_ = [[primaryWindow_ contentView] frame].origin;
initialRootAnchorPoint_ = root.anchorPoint;
NSPoint contentViewOrigin =
[self pointRelativeToCurrentScreen:finalFrame_.origin];
NSRect relativeContentFinalFrame =
NSMakeRect(contentViewOrigin.x, contentViewOrigin.y,
finalFrame_.size.width, finalFrame_.size.height);
[primaryWindow_ forceContentViewFrame:relativeContentFinalFrame];
fullscreenTabStripBackgroundView_.reset(
[[FullscreenTabStripBackgroundView alloc]
initWithFrame:finalFrame_
background:primaryWindowInitialBackgroundColor_]);
[fullscreenTabStripBackgroundView_ setFrameOrigin:NSZeroPoint];
[contentView addSubview:fullscreenTabStripBackgroundView_.get()
positioned:NSWindowBelow
relativeTo:nil];
[tabStripBackgroundView_ setHidden:YES];
// Set anchor point to be the center of the content view
CGFloat anchorPointX =
NSMidX(relativeContentFinalFrame) / NSWidth(initialFrame_);
CGFloat anchorPointY =
NSMidY(relativeContentFinalFrame) / NSHeight(initialFrame_);
root.anchorPoint = CGPointMake(anchorPointX, anchorPointY);
lock_->set_lock(YES);
}
}
- (void)animateSnapshotWindowWithDuration:(CGFloat)duration {
// Calculate the frame so that it's relative to the screen.
NSRect finalFrameRelativeToScreen =
[snapshotWindow_ convertRectFromScreen:finalFrame_];
// Move the snapshot layer until it's bottom-left corner is at the the
// bottom-left corner of the expected frame.
CABasicAnimation* positionAnimation =
[CABasicAnimation animationWithKeyPath:@"position"];
positionAnimation.toValue =
[NSValue valueWithPoint:finalFrameRelativeToScreen.origin];
positionAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:TransformAnimationTimingFunction()];
// 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];
boundsAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:TransformAnimationTimingFunction()];
// Fade out the snapshot layer.
CABasicAnimation* opacityAnimation =
[CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.toValue = @(0.0);
opacityAnimation.timingFunction =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
// Fill forwards, and don't remove the animation. When the animation
// completes, the entire window will be removed.
CAAnimationGroup* group = [CAAnimationGroup animation];
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
group.animations = @[ positionAnimation, boundsAnimation, opacityAnimation ];
group.duration = duration;
[group setValue:kSnapshotWindowAnimationID forKey:kAnimationIDKey];
group.delegate = self;
[snapshotLayer_ addAnimation:group forKey:nil];
}
- (void)animatePrimaryWindowWithDuration:(CGFloat)duration {
// As soon as the window's root layer is scaled down, the opacity should be
// set back to 1. There are a couple of ways to do this. The easiest is to
// just have a dummy animation as part of the same animation group.
CABasicAnimation* opacityAnimation =
[CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.fromValue = @(1.0);
opacityAnimation.toValue = @(1.0);
// 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 = initialFrame_;
NSRect endFrame = finalFrame_;
CGFloat xScale = NSWidth(initialFrame) / NSWidth(endFrame);
CGFloat yScale = NSHeight(initialFrame) / NSHeight(endFrame);
CATransform3D initial = CATransform3DMakeScale(xScale, yScale, 1);
CABasicAnimation* transformAnimation =
[CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.fromValue = [NSValue valueWithCATransform3D:initial];
// Animate the primary window from its initial position, the center of the
// initial window.
CABasicAnimation* positionAnimation =
[CABasicAnimation animationWithKeyPath:@"position"];
NSPoint centerOfInitialFrame =
NSMakePoint(NSMidX(initialFrame), NSMidY(initialFrame));
NSPoint startingLayerPoint =
[self pointRelativeToCurrentScreen:centerOfInitialFrame];
positionAnimation.fromValue = [NSValue valueWithPoint:startingLayerPoint];
NSPoint endingLayerPoint =
[self pointRelativeToCurrentScreen:NSMakePoint(NSMidX(endFrame),
NSMidY(endFrame))];
positionAnimation.toValue = [NSValue valueWithPoint:endingLayerPoint];
CAAnimationGroup* group = [CAAnimationGroup animation];
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
group.animations =
@[ opacityAnimation, positionAnimation, transformAnimation ];
group.timingFunction = [CAMediaTimingFunction
functionWithName:TransformAnimationTimingFunction()];
group.duration = duration;
[group setValue:kPrimaryWindowAnimationID forKey:kAnimationIDKey];
group.delegate = self;
CALayer* root = [self rootLayerOfWindow:primaryWindow_];
[root addAnimation:group forKey:kPrimaryWindowAnimationID];
}
- (void)changePrimaryWindowToFinalFrame {
changingPrimaryWindowSize_ = YES;
[primaryWindow_ setFrame:finalFrame_ display:NO];
changingPrimaryWindowSize_ = NO;
}
- (void)animationDidStart:(CAAnimation*)theAnimation {
// CAAnimationDelegate method added on OSX 10.12.
}
- (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)finished {
NSString* animationID = [theAnimation valueForKey:kAnimationIDKey];
// Remove the snapshot window.
if ([animationID isEqual:kSnapshotWindowAnimationID]) {
[snapshotWindow_ orderOut:nil];
snapshotWindow_.reset();
snapshotLayer_.reset();
return;
}
if ([animationID isEqual:kPrimaryWindowAnimationID]) {
// If we're exiting full screen, we want to set the |primaryWindow_|'s
// frame to the expected frame at the end of the animation. The window's
// lock must also be released.
if (!isEnteringFullscreen_) {
lock_->set_lock(NO);
CALayer* root = [self rootLayerOfWindow:primaryWindow_];
root.anchorPoint = initialRootAnchorPoint_;
NSUInteger styleMask =
([primaryWindow_ styleMask] & ~NSFullScreenWindowMask) |
NSTexturedBackgroundWindowMask;
[primaryWindow_ setStyleMask:styleMask];
NSView* content = [primaryWindow_ contentView];
[content setFrameOrigin:initialContentViewOrigin_];
[self changePrimaryWindowToFinalFrame];
[tabStripBackgroundView_ setHidden:NO];
[fullscreenTabStripBackgroundView_ removeFromSuperview];
}
// Check if the contentView size is correct.
// TODO (spqchan): Currently, a popup window will fail this check since
// AppKit will shift the contentView up right after the window is resized.
// This will create a small janky movement at the end of the animation.
NSSize expectedSize = finalFrame_.size;
NSView* content = [primaryWindow_ contentView];
DCHECK_EQ(NSHeight(content.frame), expectedSize.height);
DCHECK_EQ(NSWidth(content.frame), expectedSize.width);
// Restore the state of the primary window and make it visible again.
[primaryWindow_ setOpaque:primaryWindowInitialOpaque_];
[primaryWindow_ setBackgroundColor:primaryWindowInitialBackgroundColor_];
CALayer* root = [self rootLayerOfWindow:primaryWindow_];
[root removeAnimationForKey:kPrimaryWindowAnimationID];
root.opacity = 1;
completedTransition_ = YES;
if (!isEnteringFullscreen_)
[controller_ exitFullscreenAnimationFinished];
}
}
- (CALayer*)rootLayerOfWindow:(NSWindow*)window {
return [[[window contentView] superview] layer];
}
- (NSPoint)pointRelativeToCurrentScreen:(NSPoint)point {
NSRect screenFrame = [[primaryWindow_ screen] frame];
return NSMakePoint(point.x - screenFrame.origin.x,
point.y - screenFrame.origin.y);
}
@end
|