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 [customWindow_ makeKeyAndOrderFront:nil];
88 - (void)updateSheetPosition {
89 ConstrainedWindowSheetController* controller =
90 [ConstrainedWindowSheetController controllerForSheet:self];
91 NSPoint origin = [controller originForSheet:self
92 withWindowSize:[customWindow_ frame].size];
93 [customWindow_ setFrameOrigin:origin];
96 - (NSWindow*)sheetWindow {
102 SingleWebContentsDialogManagerViewsMac::SingleWebContentsDialogManagerViewsMac(
104 web_modal::SingleWebContentsDialogManagerDelegate* delegate)
105 : delegate_(delegate), host_(nullptr) {
107 [[WrappedConstrainedWindowSheet alloc] initWithCustomWindow:dialog]);
108 widget_ = views::Widget::GetWidgetForNativeWindow(dialog);
110 widget_->AddObserver(this);
113 SingleWebContentsDialogManagerViewsMac::
114 ~SingleWebContentsDialogManagerViewsMac() {
115 DCHECK(!widget_->HasObserver(this));
118 void SingleWebContentsDialogManagerViewsMac::Show() {
119 if (widget_->IsVisible())
120 return; // Rely on the ConstrainedWindowSheetController to unhide instead.
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];
138 void SingleWebContentsDialogManagerViewsMac::Hide() {
139 // Hide is called by WebContentsModalDialogManager when it observes the
140 // WebContents being hidden. The constrained window controller will just
141 // hide the native window, so nothing to do.
144 void SingleWebContentsDialogManagerViewsMac::Close() {
145 // When the WebContents is destroyed, WebContentsModalDialogManager
146 // ::CloseAllDialogs will call this. Close the Widget in the same manner as
147 // the dialogs so that codepaths are consistent.
148 widget_->Close(); // Note: Synchronously calls OnWidgetClosing() below.
151 void SingleWebContentsDialogManagerViewsMac::Focus() {
152 // Handled by ConstrainedWindowSheetController.
154 void SingleWebContentsDialogManagerViewsMac::Pulse() {
155 // Handled by ConstrainedWindowSheetController.
158 void SingleWebContentsDialogManagerViewsMac::HostChanged(
159 web_modal::WebContentsModalDialogHost* new_host) {
160 // No need to observe the host. For Cocoa, the constrained window controller
161 // will reposition the dialog when necessary. The host can also never change.
162 // Tabs showing a dialog can not be dragged off a Cocoa browser window.
163 // However, closing a tab with a dialog open will set the host back to null.
164 DCHECK_NE(!!host_, !!new_host);
168 gfx::NativeWindow SingleWebContentsDialogManagerViewsMac::dialog() {
169 return [sheet_ sheetWindow];
172 // views::WidgetObserver:
173 void SingleWebContentsDialogManagerViewsMac::OnWidgetClosing(
174 views::Widget* widget) {
175 DCHECK_EQ(widget, widget_);
176 widget->RemoveObserver(this);
177 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
179 delegate_->WillClose(dialog()); // Deletes |this|.
182 void SingleWebContentsDialogManagerViewsMac::OnWidgetDestroying(
183 views::Widget* widget) {
184 // On Mac, this would only be reached if something called -[NSWindow close]
185 // on the dialog without going through Widget::Close or CloseNow(). Since
186 // dialogs have no titlebar, it won't come from the system. If something
187 // internally calls -[NSWindow close] it might break lifetime assumptions
188 // made by DialogDelegate.