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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/web_contents_modal_dialog_manager_views_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #import "base/mac/foundation_util.h"
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet.h"
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
12 #include "content/public/browser/web_contents.h"
13 #include "components/web_modal/web_contents_modal_dialog_host.h"
14 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h"
15 #include "ui/views/widget/widget.h"
17 // A wrapper for a views::Widget dialog to interact with a Cocoa browser's
18 // ContrainedWindowSheetController. Similar to CustomConstrainedWindowSheet, but
19 // since Widgets of dialog type animate themselves, and also manage their own
20 // parenting, there's not much to do except position properly.
21 @interface WrappedConstrainedWindowSheet : NSObject<ConstrainedWindowSheet> {
23 base::scoped_nsobject<NSWindow> customWindow_;
25 - (id)initWithCustomWindow:(NSWindow*)customWindow;
28 @implementation WrappedConstrainedWindowSheet
30 - (id)initWithCustomWindow:(NSWindow*)customWindow {
31 if ((self = [super init])) {
32 customWindow_.reset([customWindow retain]);
37 // ConstrainedWindowSheet implementation.
39 - (void)showSheetForWindow:(NSWindow*)window {
40 // This is only called for the initial show, then calls go to unhideSheet.
41 // Since Widget::Show() will be called, just update the position.
42 [self updateSheetPosition];
45 - (void)closeSheetWithAnimation:(BOOL)withAnimation {
46 // Nothing to do here. Either SingleWebContentsDialogManagerViewsMac::Close()
47 // was called or Widget::Close(). Both cases ending up in OnWidgetClosing() to
48 // call [ConstrainedWindowSheetController closeSheet:], which calls this.
49 // However, the Widget is already closing in those cases.
51 // OnWidgetClosing() is also the _only_ trigger. The exception would be
52 // -[ConstrainedWindowSheetController onParentWindowWillClose:] which also
53 // calls closeSheetWithAnimation:, but a Widget never gets there because
54 // WebContentsModalDialogManager::CloseAllDialogs() is triggered from
55 // -[BrowserWindowController windowShouldClose:], but the notification that
56 // calls onParentWindowWillClose always happens once that has returned YES.
58 // So, since onParentWindowWillClose never calls this, we can assert that
59 // withAnimation is YES, otherwise there's some code path that might not be
61 DCHECK(withAnimation);
65 // Hide the sheet window by setting the alpha to 0. This technique is used
66 // instead of -orderOut: because that may cause a Spaces change or window
68 [customWindow_ setAlphaValue:0.0];
69 // TODO(tapted): Support child windows.
70 DCHECK_EQ(0u, [[customWindow_ childWindows] count]);
74 [customWindow_ setAlphaValue:1.0];
75 DCHECK_EQ(0u, [[customWindow_ childWindows] count]);
79 base::scoped_nsobject<NSAnimation> animation(
80 [[ConstrainedWindowAnimationPulse alloc] initWithWindow:customWindow_]);
81 [animation startAnimation];
84 - (void)makeSheetKeyAndOrderFront {
85 // If the window is not visible, do nothing. Widget::Show() is responsible for
86 // showing, and it may want to animate it.
87 if ([customWindow_ isVisible])
88 [customWindow_ makeKeyAndOrderFront:nil];
91 - (void)updateSheetPosition {
92 ConstrainedWindowSheetController* controller =
93 [ConstrainedWindowSheetController controllerForSheet:self];
94 NSPoint origin = [controller originForSheet:self
95 withWindowSize:[customWindow_ frame].size];
96 [customWindow_ setFrameOrigin:origin];
99 - (NSWindow*)sheetWindow {
100 return customWindow_;
105 SingleWebContentsDialogManagerViewsMac::SingleWebContentsDialogManagerViewsMac(
107 web_modal::SingleWebContentsDialogManagerDelegate* delegate)
108 : delegate_(delegate), host_(nullptr) {
110 [[WrappedConstrainedWindowSheet alloc] initWithCustomWindow:dialog]);
111 widget_ = views::Widget::GetWidgetForNativeWindow(dialog);
113 widget_->AddObserver(this);
116 SingleWebContentsDialogManagerViewsMac::
117 ~SingleWebContentsDialogManagerViewsMac() {
118 DCHECK(!widget_->HasObserver(this));
121 void SingleWebContentsDialogManagerViewsMac::Show() {
124 NSView* parent_view = host_->GetHostView();
125 // Note that simply [parent_view window] for an inactive tab is nil. However,
126 // the following should always be non-nil for all WebContents containers.
127 NSWindow* parent_window =
128 delegate_->GetWebContents()->GetTopLevelNativeWindow();
129 // Register with the ConstrainedWindowSheetController. This ensures that, e.g.
130 // the NSView that overlays the Cocoa WebContents to intercept clicks is
131 // installed and managed.
132 [[ConstrainedWindowSheetController controllerForParentWindow:parent_window]
134 forParentView:parent_view];
136 if (!widget_->IsVisible())
140 void SingleWebContentsDialogManagerViewsMac::Hide() {
141 NSWindow* parent_window =
142 delegate_->GetWebContents()->GetTopLevelNativeWindow();
143 [[ConstrainedWindowSheetController controllerForParentWindow:parent_window]
147 void SingleWebContentsDialogManagerViewsMac::Close() {
148 // When the WebContents is destroyed, WebContentsModalDialogManager
149 // ::CloseAllDialogs will call this. Close the Widget in the same manner as
150 // the dialogs so that codepaths are consistent.
151 widget_->Close(); // Note: Synchronously calls OnWidgetClosing() below.
154 void SingleWebContentsDialogManagerViewsMac::Focus() {
155 // Handled by ConstrainedWindowSheetController.
157 void SingleWebContentsDialogManagerViewsMac::Pulse() {
158 // Handled by ConstrainedWindowSheetController.
161 void SingleWebContentsDialogManagerViewsMac::HostChanged(
162 web_modal::WebContentsModalDialogHost* new_host) {
163 // No need to observe the host. For Cocoa, the constrained window controller
164 // will reposition the dialog when necessary. The host can also never change.
165 // Tabs showing a dialog can not be dragged off a Cocoa browser window.
166 // However, closing a tab with a dialog open will set the host back to null.
167 DCHECK_NE(!!host_, !!new_host);
171 gfx::NativeWindow SingleWebContentsDialogManagerViewsMac::dialog() {
172 return [sheet_ sheetWindow];
175 // views::WidgetObserver:
176 void SingleWebContentsDialogManagerViewsMac::OnWidgetClosing(
177 views::Widget* widget) {
178 DCHECK_EQ(widget, widget_);
179 widget->RemoveObserver(this);
180 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
182 delegate_->WillClose(dialog()); // Deletes |this|.
185 void SingleWebContentsDialogManagerViewsMac::OnWidgetDestroying(
186 views::Widget* widget) {
187 // On Mac, this would only be reached if something called -[NSWindow close]
188 // on the dialog without going through Widget::Close or CloseNow(). Since
189 // dialogs have no titlebar, it won't come from the system. If something
190 // internally calls -[NSWindow close] it might break lifetime assumptions
191 // made by DialogDelegate.