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. 4 5 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h" 6 7 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_animation.h" 8 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h" 9 10 @implementation CustomConstrainedWindowSheet 11 12 - (id)initWithCustomWindow:(NSWindow*)customWindow { 13 if ((self = [super init])) { 14 customWindow_.reset([customWindow retain]); 15 } 16 return self; 17 } 18 19 - (void)showSheetForWindow:(NSWindow*)window { 20 base::scoped_nsobject<NSAnimation> animation( 21 [[ConstrainedWindowAnimationShow alloc] initWithWindow:customWindow_]); 22 [window addChildWindow:customWindow_ 23 ordered:NSWindowAbove]; 24 [self unhideSheet]; 25 [self updateSheetPosition]; 26 [customWindow_ makeKeyAndOrderFront:nil]; 27 [animation startAnimation]; 28 } 29 30 - (void)closeSheetWithAnimation:(BOOL)withAnimation { 31 if (withAnimation) { 32 base::scoped_nsobject<NSAnimation> animation( 33 [[ConstrainedWindowAnimationHide alloc] initWithWindow:customWindow_]); 34 [animation startAnimation]; 35 } 36 37 [[customWindow_ parentWindow] removeChildWindow:customWindow_]; 38 [customWindow_ close]; 39 } 40 41 - (void)hideSheet { 42 // Hide the sheet window, and any of its direct child windows, by setting the 43 // alpha to 0. This technique is used instead of -orderOut: because that may 44 // cause a Spaces change or window ordering change. 45 [customWindow_ setAlphaValue:0.0]; 46 for (NSWindow* window : [customWindow_ childWindows]) { 47 [window setAlphaValue:0.0]; 48 } 49 } 50 51 - (void)unhideSheet { 52 [customWindow_ setAlphaValue:1.0]; 53 for (NSWindow* window : [customWindow_ childWindows]) { 54 [window setAlphaValue:1.0]; 55 } 56 } 57 58 - (void)pulseSheet { 59 base::scoped_nsobject<NSAnimation> animation( 60 [[ConstrainedWindowAnimationPulse alloc] initWithWindow:customWindow_]); 61 [animation startAnimation]; 62 } 63 64 - (void)makeSheetKeyAndOrderFront { 65 [customWindow_ makeKeyAndOrderFront:nil]; 66 } 67 68 - (void)updateSheetPosition { 69 ConstrainedWindowSheetController* controller = 70 [ConstrainedWindowSheetController controllerForSheet:self]; 71 NSPoint origin = [controller originForSheet:self 72 withWindowSize:[customWindow_ frame].size]; 73 [customWindow_ setFrameOrigin:origin]; 74 } 75 76 @end 77