OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "components/constrained_window/native_web_contents_modal_dialog_manager
_views_mac.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "components/constrained_window/constrained_window_views.h" |
| 10 #include "components/guest_view/browser/guest_view_base.h" |
| 11 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #import "ui/gfx/mac/coordinate_conversion.h" |
| 14 #include "ui/views/widget/widget.h" |
| 15 #include "ui/views/widget/widget_delegate.h" |
| 16 |
| 17 using web_modal::WebContentsModalDialogManager; |
| 18 using web_modal::SingleWebContentsDialogManager; |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Sets visibility and mouse events for a Cocoa NSWindow* and an attached sheet. |
| 23 void SetSheetVisible(gfx::NativeWindow overlay, bool visible) { |
| 24 CGFloat alpha = visible ? 1.0 : 0.0; |
| 25 BOOL ignore_events = visible ? NO : YES; |
| 26 |
| 27 // Don't allow interaction with the tab underneath the overlay. |
| 28 [overlay setIgnoresMouseEvents:ignore_events]; |
| 29 |
| 30 [[overlay attachedSheet] setAlphaValue:alpha]; |
| 31 [[overlay attachedSheet] setIgnoresMouseEvents:ignore_events]; |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 namespace constrained_window { |
| 37 |
| 38 NativeWebContentsModalDialogManagerViewsMac:: |
| 39 NativeWebContentsModalDialogManagerViewsMac( |
| 40 gfx::NativeWindow dialog, |
| 41 web_modal::SingleWebContentsDialogManagerDelegate* native_delegate) |
| 42 : NativeWebContentsModalDialogManagerViews(dialog, native_delegate) {} |
| 43 |
| 44 // NativeWebContentsModalDialogManagerViews: |
| 45 void NativeWebContentsModalDialogManagerViewsMac::OnPositionRequiresUpdate() { |
| 46 NativeWebContentsModalDialogManagerViews::OnPositionRequiresUpdate(); |
| 47 |
| 48 views::Widget* widget = GetWidget(dialog()); |
| 49 // Because the animation of SFCertificatePanel will change depending on the |
| 50 // size of the parent, i.e. |widget|, make sure its size is the same as the |
| 51 // area under the Chrome UI. The origin of the dialog then also needs to be |
| 52 // updated to position the certificate viewer in the middle horizontally. |
| 53 content::WebContents* web_contents = native_delegate()->GetWebContents(); |
| 54 // Note: Can't use WebContents container bounds here because it doesn't |
| 55 // include the DevTool panel width. |
| 56 CGFloat window_width = |
| 57 NSWidth([web_contents->GetTopLevelNativeWindow() frame]); |
| 58 gfx::Rect tab_view_size = web_contents->GetContainerBounds(); |
| 59 widget->SetBounds(gfx::Rect(tab_view_size.x(), |
| 60 widget->GetWindowBoundsInScreen().y(), |
| 61 window_width, tab_view_size.height())); |
| 62 } |
| 63 |
| 64 void NativeWebContentsModalDialogManagerViewsMac::ShowWidget( |
| 65 views::Widget* widget) { |
| 66 NSWindow* dialog_window = widget->GetNativeWindow(); |
| 67 [dialog_window setAlphaValue:0.0]; |
| 68 // Because |dialog_window| is transparent, it won't accept mouse events until |
| 69 // ignoresMouseEvents is set. NSWindows start off accepting mouse events only |
| 70 // in non-transparent areas - setting this explicitly will make the NSWindow |
| 71 // accept mouse events everywhere regardless of window transparency. |
| 72 [dialog_window setIgnoresMouseEvents:NO]; |
| 73 |
| 74 // Detect whether this is the first call to open the dialog. If yes, do this |
| 75 // via the normal views method. If not, overlay and sheet are both already |
| 76 // opened, and should be invisible, so return the sheet to full opacity. |
| 77 if (![dialog_window attachedSheet]) { |
| 78 NativeWebContentsModalDialogManagerViews::ShowWidget(widget); |
| 79 // Make sure the dialog is sized correctly for the correct animations. |
| 80 OnPositionRequiresUpdate(); |
| 81 return; |
| 82 } |
| 83 |
| 84 // Account for window resizes that happen while another tab is open. |
| 85 OnPositionRequiresUpdate(); |
| 86 SetSheetVisible(dialog_window, true); |
| 87 } |
| 88 |
| 89 void NativeWebContentsModalDialogManagerViewsMac::HideWidget( |
| 90 views::Widget* widget) { |
| 91 NSWindow* dialog_window = widget->GetNativeWindow(); |
| 92 // Avoid views::Widget::Hide(), as a call to orderOut: on a NSWindow with an |
| 93 // attached sheet will close the sheet. Instead, just set the sheet to 0 |
| 94 // opacity and don't accept click events. |
| 95 SetSheetVisible(dialog_window, false); |
| 96 } |
| 97 |
| 98 } // namespace constrained_window |
OLD | NEW |