| 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 "chrome/browser/ui/views/native_web_contents_modal_dialog_manager_views
.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "chrome/browser/platform_util.h" | |
| 10 #include "components/constrained_window/constrained_window_views.h" | |
| 11 #include "components/web_modal/web_contents_modal_dialog_host.h" | |
| 12 #include "components/web_modal/web_contents_modal_dialog_manager.h" | |
| 13 #include "ui/gfx/geometry/point.h" | |
| 14 #include "ui/gfx/geometry/size.h" | |
| 15 #include "ui/views/border.h" | |
| 16 #include "ui/views/widget/widget.h" | |
| 17 #include "ui/views/widget/widget_delegate.h" | |
| 18 #include "ui/views/window/dialog_delegate.h" | |
| 19 #include "ui/views/window/non_client_view.h" | |
| 20 | |
| 21 #if defined(USE_AURA) | |
| 22 #include "ui/aura/client/aura_constants.h" | |
| 23 #include "ui/aura/window.h" | |
| 24 #include "ui/wm/core/visibility_controller.h" | |
| 25 #include "ui/wm/core/window_animations.h" | |
| 26 #include "ui/wm/core/window_modality_controller.h" | |
| 27 #endif | |
| 28 | |
| 29 using web_modal::SingleWebContentsDialogManager; | |
| 30 using web_modal::SingleWebContentsDialogManagerDelegate; | |
| 31 using web_modal::WebContentsModalDialogHost; | |
| 32 using web_modal::ModalDialogHostObserver; | |
| 33 | |
| 34 NativeWebContentsModalDialogManagerViews:: | |
| 35 NativeWebContentsModalDialogManagerViews( | |
| 36 gfx::NativeWindow dialog, | |
| 37 SingleWebContentsDialogManagerDelegate* native_delegate) | |
| 38 : native_delegate_(native_delegate), | |
| 39 dialog_(dialog), | |
| 40 host_(NULL), | |
| 41 host_destroying_(false) { | |
| 42 ManageDialog(); | |
| 43 } | |
| 44 | |
| 45 NativeWebContentsModalDialogManagerViews:: | |
| 46 ~NativeWebContentsModalDialogManagerViews() { | |
| 47 if (host_) | |
| 48 host_->RemoveObserver(this); | |
| 49 | |
| 50 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); | |
| 51 it != observed_widgets_.end(); ++it) { | |
| 52 (*it)->RemoveObserver(this); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 void NativeWebContentsModalDialogManagerViews::ManageDialog() { | |
| 57 views::Widget* widget = GetWidget(dialog()); | |
| 58 widget->AddObserver(this); | |
| 59 observed_widgets_.insert(widget); | |
| 60 widget->set_movement_disabled(true); | |
| 61 | |
| 62 #if defined(USE_AURA) | |
| 63 // TODO(wittman): remove once the new visual style is complete | |
| 64 widget->GetNativeWindow()->SetProperty(aura::client::kConstrainedWindowKey, | |
| 65 true); | |
| 66 | |
| 67 wm::SetWindowVisibilityAnimationType( | |
| 68 widget->GetNativeWindow(), wm::WINDOW_VISIBILITY_ANIMATION_TYPE_ROTATE); | |
| 69 | |
| 70 gfx::NativeView parent = platform_util::GetParent(widget->GetNativeView()); | |
| 71 wm::SetChildWindowVisibilityChangesAnimated(parent); | |
| 72 // No animations should get performed on the window since that will re-order | |
| 73 // the window stack which will then cause many problems. | |
| 74 if (parent && parent->parent()) { | |
| 75 parent->parent()->SetProperty(aura::client::kAnimationsDisabledKey, true); | |
| 76 } | |
| 77 | |
| 78 wm::SetModalParent(widget->GetNativeWindow(), | |
| 79 native_delegate_->GetWebContents()->GetNativeView()); | |
| 80 #endif | |
| 81 } | |
| 82 | |
| 83 // SingleWebContentsDialogManager: | |
| 84 | |
| 85 void NativeWebContentsModalDialogManagerViews::Show() { | |
| 86 // The host destroying means the dialogs will be destroyed in short order. | |
| 87 // Avoid showing dialogs at this point as the necessary native window | |
| 88 // services may not be present. | |
| 89 if (host_destroying_) | |
| 90 return; | |
| 91 | |
| 92 views::Widget* widget = GetWidget(dialog()); | |
| 93 #if defined(USE_AURA) | |
| 94 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend; | |
| 95 if (shown_widgets_.find(widget) != shown_widgets_.end()) { | |
| 96 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations( | |
| 97 widget->GetNativeWindow()->parent())); | |
| 98 } | |
| 99 #endif | |
| 100 // Host may be NULL during tab drag on Views/Win32. | |
| 101 if (host_) | |
| 102 constrained_window::UpdateWebContentsModalDialogPosition(widget, host_); | |
| 103 widget->Show(); | |
| 104 Focus(); | |
| 105 | |
| 106 #if defined(USE_AURA) | |
| 107 // TODO(pkotwicz): Control the z-order of the constrained dialog via | |
| 108 // views::kHostViewKey. We will need to ensure that the parent window's | |
| 109 // shadows are below the constrained dialog in z-order when we do this. | |
| 110 shown_widgets_.insert(widget); | |
| 111 #endif | |
| 112 } | |
| 113 | |
| 114 void NativeWebContentsModalDialogManagerViews::Hide() { | |
| 115 views::Widget* widget = GetWidget(dialog()); | |
| 116 #if defined(USE_AURA) | |
| 117 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend; | |
| 118 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations( | |
| 119 widget->GetNativeWindow()->parent())); | |
| 120 #endif | |
| 121 widget->Hide(); | |
| 122 } | |
| 123 | |
| 124 void NativeWebContentsModalDialogManagerViews::Close() { | |
| 125 GetWidget(dialog())->Close(); | |
| 126 } | |
| 127 | |
| 128 void NativeWebContentsModalDialogManagerViews::Focus() { | |
| 129 views::Widget* widget = GetWidget(dialog()); | |
| 130 if (widget->widget_delegate() && | |
| 131 widget->widget_delegate()->GetInitiallyFocusedView()) | |
| 132 widget->widget_delegate()->GetInitiallyFocusedView()->RequestFocus(); | |
| 133 #if defined(USE_AURA) | |
| 134 // We don't necessarily have a RootWindow yet. | |
| 135 if (widget->GetNativeView()->GetRootWindow()) | |
| 136 widget->GetNativeView()->Focus(); | |
| 137 #endif | |
| 138 } | |
| 139 | |
| 140 void NativeWebContentsModalDialogManagerViews::Pulse() {} | |
| 141 | |
| 142 // web_modal::ModalDialogHostObserver: | |
| 143 | |
| 144 void NativeWebContentsModalDialogManagerViews::OnPositionRequiresUpdate() { | |
| 145 DCHECK(host_); | |
| 146 | |
| 147 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); | |
| 148 it != observed_widgets_.end(); ++it) { | |
| 149 constrained_window::UpdateWebContentsModalDialogPosition(*it, host_); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 void NativeWebContentsModalDialogManagerViews::OnHostDestroying() { | |
| 154 host_->RemoveObserver(this); | |
| 155 host_ = NULL; | |
| 156 host_destroying_ = true; | |
| 157 } | |
| 158 | |
| 159 // views::WidgetObserver: | |
| 160 | |
| 161 void NativeWebContentsModalDialogManagerViews::OnWidgetClosing( | |
| 162 views::Widget* widget) { | |
| 163 WidgetClosing(widget); | |
| 164 } | |
| 165 | |
| 166 void NativeWebContentsModalDialogManagerViews::OnWidgetDestroying( | |
| 167 views::Widget* widget) { | |
| 168 WidgetClosing(widget); | |
| 169 } | |
| 170 | |
| 171 void NativeWebContentsModalDialogManagerViews::HostChanged( | |
| 172 WebContentsModalDialogHost* new_host) { | |
| 173 if (host_) | |
| 174 host_->RemoveObserver(this); | |
| 175 | |
| 176 host_ = new_host; | |
| 177 | |
| 178 // |host_| may be null during WebContents destruction or Win32 tab dragging. | |
| 179 if (host_) { | |
| 180 host_->AddObserver(this); | |
| 181 | |
| 182 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); | |
| 183 it != observed_widgets_.end(); ++it) { | |
| 184 views::Widget::ReparentNativeView((*it)->GetNativeView(), | |
| 185 host_->GetHostView()); | |
| 186 } | |
| 187 | |
| 188 OnPositionRequiresUpdate(); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 gfx::NativeWindow NativeWebContentsModalDialogManagerViews::dialog() { | |
| 193 return dialog_; | |
| 194 } | |
| 195 | |
| 196 views::Widget* NativeWebContentsModalDialogManagerViews::GetWidget( | |
| 197 gfx::NativeWindow dialog) { | |
| 198 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(dialog); | |
| 199 DCHECK(widget); | |
| 200 return widget; | |
| 201 } | |
| 202 | |
| 203 void NativeWebContentsModalDialogManagerViews::WidgetClosing( | |
| 204 views::Widget* widget) { | |
| 205 #if defined(USE_AURA) | |
| 206 gfx::NativeView view = platform_util::GetParent(widget->GetNativeView()); | |
| 207 // Allow the parent to animate again. | |
| 208 if (view && view->parent()) | |
| 209 view->parent()->ClearProperty(aura::client::kAnimationsDisabledKey); | |
| 210 #endif | |
| 211 widget->RemoveObserver(this); | |
| 212 observed_widgets_.erase(widget); | |
| 213 | |
| 214 #if defined(USE_AURA) | |
| 215 shown_widgets_.erase(widget); | |
| 216 #endif | |
| 217 | |
| 218 // Will cause this object to be deleted. | |
| 219 native_delegate_->WillClose(widget->GetNativeWindow()); | |
| 220 } | |
| OLD | NEW |