Chromium Code Reviews| Index: chrome/browser/ui/cocoa/native_web_contents_modal_dialog_manager_views_mac.mm |
| diff --git a/chrome/browser/ui/cocoa/native_web_contents_modal_dialog_manager_views_mac.mm b/chrome/browser/ui/cocoa/native_web_contents_modal_dialog_manager_views_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7fc3cd2799b25b080bc4e3d27aaba7bcaea47131 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/native_web_contents_modal_dialog_manager_views_mac.mm |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2016 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 <Cocoa/Cocoa.h> |
| + |
| +#include "chrome/browser/ui/views/native_web_contents_modal_dialog_manager_views.h" |
| +#include "components/constrained_window/constrained_window_views.h" |
| +#include "components/guest_view/browser/guest_view_base.h" |
| +#include "components/web_modal/web_contents_modal_dialog_manager.h" |
| +#include "content/public/browser/web_contents.h" |
| +#import "ui/gfx/mac/coordinate_conversion.h" |
| +#include "ui/views/widget/widget.h" |
| +#include "ui/views/widget/widget_delegate.h" |
| + |
| +using web_modal::WebContentsModalDialogManager; |
| +using web_modal::SingleWebContentsDialogManager; |
| + |
| +namespace { |
| + |
| +// Class for parenting a Mac Cocoa sheet on a views tab modal dialog off of a |
| +// views browser, e.g. for tab-modal Cocoa sheets. Since Cocoa sheets are modal |
| +// to the parent window, the sheet is instead parented to an invisible views |
| +// overlay window which is tab-modal. |
| +class NativeWebContentsModalDialogManagerViewsMac |
| + : public NativeWebContentsModalDialogManagerViews { |
| + public: |
| + NativeWebContentsModalDialogManagerViewsMac( |
| + gfx::NativeWindow dialog, |
| + web_modal::SingleWebContentsDialogManagerDelegate* native_delegate) |
| + : NativeWebContentsModalDialogManagerViews(dialog, native_delegate) {} |
| + |
| + // NativeWebContentsModalDialogManagerViews: |
| + void OnPositionRequiresUpdate() override { |
| + NativeWebContentsModalDialogManagerViews::OnPositionRequiresUpdate(); |
| + |
| + views::Widget* widget = GetWidget(dialog()); |
| + // Because the animation of SFCertificatePanel will change depending on the |
| + // size of the parent, i.e. |widget|, make sure its size is the same |
| + // as the area under the chrome UI. The origin of the dialog then also needs |
| + // to be updated to position the certificate viewer in the middle |
| + // horizontally. |
| + content::WebContents* web_contents = native_delegate()->GetWebContents(); |
| + CGFloat window_width = |
| + NSWidth([web_contents->GetTopLevelNativeWindow() frame]); |
| + gfx::Rect tab_view_size = web_contents->GetContainerBounds(); |
| + widget->SetBounds(gfx::Rect(tab_view_size.x(), |
| + widget->GetWindowBoundsInScreen().y(), |
| + window_width, tab_view_size.height())); |
| + } |
| + |
| + void ShowWidget(views::Widget* widget) override { |
| + NSWindow* dialog_window = widget->GetNativeWindow(); |
| + // Detect whether this is the first call to open the dialog. If yes, do this |
| + // via the normal views method. If not, overlay and sheet are both already |
| + // opened, and should be invisible, so return the sheet to full opacity. |
| + if (![dialog_window attachedSheet]) { |
| + NativeWebContentsModalDialogManagerViews::ShowWidget(widget); |
| + // Make sure the dialog is sized correctly for the correct animations. |
| + OnPositionRequiresUpdate(); |
| + return; |
| + } |
| + |
| + // Account for window resizes that happen while another tab is open. |
| + OnPositionRequiresUpdate(); |
| + [dialog_window setAlphaValue:0.0]; |
| + SetSheetVisible(dialog_window, true); |
| + } |
| + |
| + void HideWidget(views::Widget* widget) override { |
| + NSWindow* dialog_window = widget->GetNativeWindow(); |
| + // Avoid views::Widget::Hide(), as a call to orderOut: on a NSWindow with an |
| + // attached sheet will close the sheet. Instead, just set the sheet to 0 |
| + // opacity and don't accept click events. |
| + SetSheetVisible(dialog_window, false); |
| + } |
| + |
| + private: |
| + // Sets visibility and mouse events for the overlay and its sheet. |
| + void SetSheetVisible(gfx::NativeWindow overlay, bool visible) { |
| + CGFloat alpha = visible ? 1.0 : 0.0; |
| + BOOL ignore_events = visible ? NO : YES; |
| + |
| + // Don't allow interaction with the tab underneath the overlay. |
| + [overlay setIgnoresMouseEvents:ignore_events]; |
| + |
| + [[overlay attachedSheet] setAlphaValue:alpha]; |
| + [[overlay attachedSheet] setIgnoresMouseEvents:ignore_events]; |
| + } |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NativeWebContentsModalDialogManagerViewsMac); |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace constrained_window { |
| + |
| +views::Widget* ShowWebModalDialogWithOverlayViews( |
|
tapted
2016/06/09 04:40:00
so, having this here (under chrome/browser/ui, rat
msw
2016/06/09 17:36:30
I agree, this is odd; these managers/impls all bel
Patti Lor
2016/08/19 06:43:26
Rebased on top of https://codereview.chromium.org/
|
| + views::WidgetDelegate* dialog, |
| + content::WebContents* initiator_web_contents) { |
| + // For embedded WebContents, use the embedder's WebContents for constrained |
| + // window. |
| + content::WebContents* web_contents = |
| + guest_view::GuestViewBase::GetTopLevelWebContents(initiator_web_contents); |
| + views::Widget* widget = CreateWebModalDialogViews(dialog, web_contents); |
| + WebContentsModalDialogManager* manager = |
| + WebContentsModalDialogManager::FromWebContents(web_contents); |
| + std::unique_ptr<SingleWebContentsDialogManager> dialog_manager( |
| + new NativeWebContentsModalDialogManagerViewsMac(widget->GetNativeWindow(), |
| + manager)); |
| + manager->ShowDialogWithManager(widget->GetNativeWindow(), |
| + std::move(dialog_manager)); |
| + return widget; |
| +} |
| + |
| +} // namespace constrained_window |