| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/constrained_window/native_web_contents_modal_dialog_manager
_views.h" | 5 #include "components/constrained_window/native_web_contents_modal_dialog_manager
_views.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "components/constrained_window/constrained_window_views.h" | 9 #include "components/constrained_window/constrained_window_views.h" |
| 10 #include "components/web_modal/web_contents_modal_dialog_host.h" | 10 #include "components/web_modal/web_contents_modal_dialog_host.h" |
| 11 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 11 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 12 #include "ui/base/accelerators/accelerator.h" |
| 12 #include "ui/gfx/geometry/point.h" | 13 #include "ui/gfx/geometry/point.h" |
| 13 #include "ui/gfx/geometry/size.h" | 14 #include "ui/gfx/geometry/size.h" |
| 14 #include "ui/views/border.h" | 15 #include "ui/views/border.h" |
| 15 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
| 16 #include "ui/views/widget/widget_delegate.h" | 17 #include "ui/views/widget/widget_delegate.h" |
| 17 #include "ui/views/window/dialog_delegate.h" | 18 #include "ui/views/window/dialog_delegate.h" |
| 18 #include "ui/views/window/non_client_view.h" | 19 #include "ui/views/window/non_client_view.h" |
| 19 | 20 |
| 20 #if defined(USE_AURA) | 21 #if defined(USE_AURA) |
| 21 #include "ui/aura/client/aura_constants.h" | 22 #include "ui/aura/client/aura_constants.h" |
| 23 #include "ui/aura/client/window_parenting_client.h" |
| 22 #include "ui/aura/window.h" | 24 #include "ui/aura/window.h" |
| 25 #include "ui/aura/window_observer.h" |
| 26 #include "ui/aura/window_tree_host.h" |
| 27 #include "ui/aura/window_tree_host_observer.h" |
| 28 #include "ui/wm/core/transient_window_manager.h" |
| 23 #include "ui/wm/core/visibility_controller.h" | 29 #include "ui/wm/core/visibility_controller.h" |
| 24 #include "ui/wm/core/window_animations.h" | 30 #include "ui/wm/core/window_animations.h" |
| 25 #include "ui/wm/core/window_modality_controller.h" | 31 #include "ui/wm/core/window_modality_controller.h" |
| 26 #endif | 32 #include "ui/wm/core/window_util.h" |
| 33 #endif // USE_AURA |
| 34 |
| 35 #if defined(OS_CHROMEOS) |
| 36 #include "ash/aura/wm_window_aura.h" |
| 37 #include "ash/common/wm/window_state.h" |
| 38 #endif // OS_CHROMEOS |
| 27 | 39 |
| 28 using web_modal::SingleWebContentsDialogManager; | 40 using web_modal::SingleWebContentsDialogManager; |
| 29 using web_modal::SingleWebContentsDialogManagerDelegate; | 41 using web_modal::SingleWebContentsDialogManagerDelegate; |
| 30 using web_modal::WebContentsModalDialogHost; | 42 using web_modal::WebContentsModalDialogHost; |
| 31 using web_modal::ModalDialogHostObserver; | 43 using web_modal::ModalDialogHostObserver; |
| 32 | 44 |
| 33 namespace constrained_window { | 45 namespace constrained_window { |
| 34 | 46 |
| 35 NativeWebContentsModalDialogManagerViews:: | 47 NativeWebContentsModalDialogManagerViews:: |
| 36 NativeWebContentsModalDialogManagerViews( | 48 NativeWebContentsModalDialogManagerViews( |
| 37 gfx::NativeWindow dialog, | 49 gfx::NativeWindow dialog, |
| 38 SingleWebContentsDialogManagerDelegate* native_delegate) | 50 SingleWebContentsDialogManagerDelegate* native_delegate, |
| 51 bool is_toplevel, |
| 52 ui::AcceleratorTarget* target) |
| 39 : native_delegate_(native_delegate), | 53 : native_delegate_(native_delegate), |
| 40 dialog_(dialog), | 54 dialog_(dialog), |
| 41 host_(NULL), | 55 host_(NULL), |
| 42 host_destroying_(false) { | 56 host_destroying_(false), |
| 43 ManageDialog(); | 57 target_(target) { |
| 58 #if !defined(USE_AURA) |
| 59 target_ = nullptr; |
| 60 #endif |
| 61 ManageDialog(is_toplevel); |
| 44 } | 62 } |
| 45 | 63 |
| 46 NativeWebContentsModalDialogManagerViews:: | 64 NativeWebContentsModalDialogManagerViews:: |
| 47 ~NativeWebContentsModalDialogManagerViews() { | 65 ~NativeWebContentsModalDialogManagerViews() { |
| 48 if (host_) | 66 if (host_) |
| 49 host_->RemoveObserver(this); | 67 host_->RemoveObserver(this); |
| 50 | 68 #if defined(USE_AURA) |
| 69 if (host_ && host_->GetHostView() && host_->GetHostView()->HasObserver(this)) |
| 70 host_->GetHostView()->RemoveObserver(this); |
| 71 if (host_ && host_->GetHostView() && host_->GetHostView()->GetHost()) |
| 72 host_->GetHostView()->GetHost()->RemoveObserver(this); |
| 73 if (host_ && host_->GetHostView() && GetWidget(host_->GetHostView())) |
| 74 GetWidget(host_->GetHostView())->RemoveObserver(this); |
| 75 bool toplevel = false; |
| 76 #endif |
| 51 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); | 77 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); |
| 52 it != observed_widgets_.end(); ++it) { | 78 it != observed_widgets_.end(); ++it) { |
| 79 #if defined(USE_AURA) |
| 80 if (!!(*it)->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)) { |
| 81 toplevel = true; |
| 82 } |
| 83 #endif |
| 53 (*it)->RemoveObserver(this); | 84 (*it)->RemoveObserver(this); |
| 54 } | 85 } |
| 86 #if defined(USE_AURA) |
| 87 if (host_ && toplevel) { |
| 88 views::Widget * parent_widget = views::Widget::GetWidgetForNativeView( |
| 89 host_->GetHostView()); |
| 90 if (parent_widget && parent_widget->GetFocusManager()) { |
| 91 parent_widget->GetFocusManager()->UnregisterAccelerator( |
| 92 ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE), target_); |
| 93 } |
| 94 } |
| 95 #endif |
| 55 } | 96 } |
| 56 | 97 |
| 57 void NativeWebContentsModalDialogManagerViews::ManageDialog() { | 98 void NativeWebContentsModalDialogManagerViews::ManageDialog( |
| 99 bool is_toplevel) { |
| 58 views::Widget* widget = GetWidget(dialog()); | 100 views::Widget* widget = GetWidget(dialog()); |
| 59 widget->AddObserver(this); | 101 widget->AddObserver(this); |
| 60 observed_widgets_.insert(widget); | 102 observed_widgets_.insert(widget); |
| 61 widget->set_movement_disabled(true); | 103 widget->set_movement_disabled(true); |
| 62 | 104 |
| 63 #if defined(USE_AURA) | 105 #if defined(USE_AURA) |
| 64 // TODO(wittman): remove once the new visual style is complete | 106 // TODO(wittman): remove once the new visual style is complete |
| 65 widget->GetNativeWindow()->SetProperty(aura::client::kConstrainedWindowKey, | 107 widget->GetNativeWindow()->SetProperty(aura::client::kConstrainedWindowKey, |
| 66 true); | 108 true); |
| 67 | 109 |
| 68 wm::SetWindowVisibilityAnimationType( | 110 wm::SetWindowVisibilityAnimationType( |
| 69 widget->GetNativeWindow(), wm::WINDOW_VISIBILITY_ANIMATION_TYPE_ROTATE); | 111 widget->GetNativeWindow(), wm::WINDOW_VISIBILITY_ANIMATION_TYPE_ROTATE); |
| 70 | 112 |
| 71 gfx::NativeView parent = widget->GetNativeView()->parent(); | 113 if (is_toplevel) { |
| 72 wm::SetChildWindowVisibilityChangesAnimated(parent); | 114 gfx::NativeView transient_parent = |
| 73 // No animations should get performed on the window since that will re-order | 115 wm::GetTransientParent(widget->GetNativeWindow()); |
| 74 // the window stack which will then cause many problems. | 116 wm::SetChildWindowVisibilityChangesAnimated( |
| 75 if (parent && parent->parent()) { | 117 widget->GetNativeView()->parent()); |
| 76 parent->parent()->SetProperty(aura::client::kAnimationsDisabledKey, true); | 118 if (transient_parent) { |
| 119 transient_parent->SetProperty( |
| 120 aura::client::kAnimationsDisabledKey, true); |
| 121 } |
| 122 widget->SetNativeWindowProperty(wm::kAllowTransientParentEventsKey, |
| 123 reinterpret_cast<void*>(true)); |
| 124 } else { |
| 125 gfx::NativeView parent = widget->GetNativeView()->parent(); |
| 126 wm::SetChildWindowVisibilityChangesAnimated(parent); |
| 127 // No animations should get performed on the window since that will re-order |
| 128 // the window stack which will then cause many problems. |
| 129 if (parent && parent->parent()) { |
| 130 parent->parent()->SetProperty(aura::client::kAnimationsDisabledKey, true); |
| 131 } |
| 132 wm::SetModalParent(widget->GetNativeWindow(), |
| 133 native_delegate_->GetWebContents()->GetNativeView()); |
| 77 } | 134 } |
| 78 | 135 called_internal_ = true; |
| 79 wm::SetModalParent(widget->GetNativeWindow(), | 136 #endif // defined(USE_AURA) |
| 80 native_delegate_->GetWebContents()->GetNativeView()); | |
| 81 #endif | |
| 82 } | 137 } |
| 83 | 138 |
| 84 // SingleWebContentsDialogManager: | 139 // SingleWebContentsDialogManager: |
| 85 | 140 |
| 86 void NativeWebContentsModalDialogManagerViews::Show() { | 141 void NativeWebContentsModalDialogManagerViews::Show() { |
| 87 // The host destroying means the dialogs will be destroyed in short order. | 142 // The host destroying means the dialogs will be destroyed in short order. |
| 88 // Avoid showing dialogs at this point as the necessary native window | 143 // Avoid showing dialogs at this point as the necessary native window |
| 89 // services may not be present. | 144 // services may not be present. |
| 90 if (host_destroying_) | 145 if (host_destroying_) |
| 91 return; | 146 return; |
| 147 #if defined(OS_CHROMEOS) |
| 148 if (!called_internal_ && window_minimized_) { |
| 149 need_to_show_ = true; |
| 150 return; |
| 151 } |
| 152 #endif |
| 92 | 153 |
| 93 views::Widget* widget = GetWidget(dialog()); | 154 views::Widget* widget = GetWidget(dialog()); |
| 94 #if defined(USE_AURA) | 155 #if defined(USE_AURA) |
| 95 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend; | 156 #if defined(OS_WIN) |
| 96 if (shown_widgets_.find(widget) != shown_widgets_.end()) { | 157 bool suspend_animation = |
| 97 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations( | 158 !(widget->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)); |
| 159 #else |
| 160 bool suspend_animation = true; |
| 161 #endif |
| 162 if (suspend_animation) { |
| 163 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend; |
| 164 if (shown_widgets_.find(widget) != shown_widgets_.end()) { |
| 165 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations( |
| 98 widget->GetNativeWindow()->parent())); | 166 widget->GetNativeWindow()->parent())); |
| 167 } |
| 99 } | 168 } |
| 169 if (parent_inactive_ && window_minimized_) |
| 170 parent_inactive_ = false; |
| 100 #endif | 171 #endif |
| 101 ShowWidget(widget); | 172 ShowWidget(widget); |
| 102 Focus(); | 173 FocusInternal(widget); |
| 103 | 174 |
| 104 #if defined(USE_AURA) | 175 #if defined(USE_AURA) |
| 105 // TODO(pkotwicz): Control the z-order of the constrained dialog via | 176 // TODO(pkotwicz): Control the z-order of the constrained dialog via |
| 106 // views::kHostViewKey. We will need to ensure that the parent window's | 177 // views::kHostViewKey. We will need to ensure that the parent window's |
| 107 // shadows are below the constrained dialog in z-order when we do this. | 178 // shadows are below the constrained dialog in z-order when we do this. |
| 108 shown_widgets_.insert(widget); | 179 shown_widgets_.insert(widget); |
| 180 called_internal_ = false; |
| 109 #endif | 181 #endif |
| 110 } | 182 } |
| 111 | 183 |
| 112 void NativeWebContentsModalDialogManagerViews::Hide() { | 184 void NativeWebContentsModalDialogManagerViews::Hide() { |
| 113 views::Widget* widget = GetWidget(dialog()); | 185 views::Widget* widget = GetWidget(dialog()); |
| 114 #if defined(USE_AURA) | 186 #if defined(USE_AURA) |
| 115 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend; | 187 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend; |
| 116 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations( | 188 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations( |
| 117 widget->GetNativeWindow()->parent())); | 189 widget->GetNativeWindow()->parent())); |
| 190 if (parent_inactive_ && |
| 191 !!(widget->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey))) |
| 192 window_minimized_ = true; |
| 118 #endif | 193 #endif |
| 119 HideWidget(widget); | 194 HideWidget(widget); |
| 120 } | 195 } |
| 121 | 196 |
| 122 void NativeWebContentsModalDialogManagerViews::Close() { | 197 void NativeWebContentsModalDialogManagerViews::Close() { |
| 123 GetWidget(dialog())->Close(); | 198 GetWidget(dialog())->Close(); |
| 124 } | 199 } |
| 125 | 200 |
| 126 void NativeWebContentsModalDialogManagerViews::Focus() { | 201 void NativeWebContentsModalDialogManagerViews::Focus() { |
| 127 views::Widget* widget = GetWidget(dialog()); | 202 views::Widget* widget = GetWidget(dialog()); |
| 128 if (widget->widget_delegate() && | |
| 129 widget->widget_delegate()->GetInitiallyFocusedView()) | |
| 130 widget->widget_delegate()->GetInitiallyFocusedView()->RequestFocus(); | |
| 131 #if defined(USE_AURA) | 203 #if defined(USE_AURA) |
| 132 // We don't necessarily have a RootWindow yet. | 204 if (!!widget->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)) |
| 133 if (widget->GetNativeView()->GetRootWindow()) | 205 return; |
| 134 widget->GetNativeView()->Focus(); | |
| 135 #endif | 206 #endif |
| 207 FocusInternal(widget); |
| 136 } | 208 } |
| 137 | 209 |
| 138 void NativeWebContentsModalDialogManagerViews::Pulse() {} | 210 void NativeWebContentsModalDialogManagerViews::Pulse() {} |
| 139 | 211 |
| 140 // web_modal::ModalDialogHostObserver: | 212 // web_modal::ModalDialogHostObserver: |
| 141 | 213 |
| 142 void NativeWebContentsModalDialogManagerViews::OnPositionRequiresUpdate() { | 214 void NativeWebContentsModalDialogManagerViews::OnPositionRequiresUpdate() { |
| 143 DCHECK(host_); | 215 DCHECK(host_); |
| 144 | 216 |
| 145 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); | 217 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); |
| 146 it != observed_widgets_.end(); ++it) { | 218 it != observed_widgets_.end(); ++it) { |
| 147 constrained_window::UpdateWebContentsModalDialogPosition(*it, host_); | 219 constrained_window::UpdateWebContentsModalDialogPosition(*it, host_); |
| 148 } | 220 } |
| 149 } | 221 } |
| 150 | 222 |
| 223 void NativeWebContentsModalDialogManagerViews:: |
| 224 OnNonClippedPositionRequiresUpdate() { |
| 225 DCHECK(host_); |
| 226 #if defined(USE_AURA) |
| 227 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); |
| 228 it != observed_widgets_.end(); ++it) { |
| 229 if (!!((*it)->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey))) |
| 230 constrained_window::UpdateWebContentsModalDialogPosition(*it, host_); |
| 231 } |
| 232 #endif |
| 233 } |
| 234 |
| 151 void NativeWebContentsModalDialogManagerViews::OnHostDestroying() { | 235 void NativeWebContentsModalDialogManagerViews::OnHostDestroying() { |
| 152 host_->RemoveObserver(this); | 236 host_->RemoveObserver(this); |
| 237 #if defined(USE_AURA) |
| 238 if (host_->GetHostView()) |
| 239 GetWidget(host_->GetHostView())->RemoveObserver(this); |
| 240 if (host_->GetHostView() && host_->GetHostView()->HasObserver(this)) |
| 241 host_->GetHostView()->RemoveObserver(this); |
| 242 if (host_->GetHostView() && host_->GetHostView()->GetHost()) |
| 243 host_->GetHostView()->GetHost()->RemoveObserver(this); |
| 244 if (host_->GetHostView() && |
| 245 views::Widget::GetWidgetForNativeView(host_->GetHostView())) |
| 246 GetWidget(host_->GetHostView())->RemoveObserver(this); |
| 247 #endif |
| 153 host_ = NULL; | 248 host_ = NULL; |
| 154 host_destroying_ = true; | 249 host_destroying_ = true; |
| 155 } | 250 } |
| 156 | 251 |
| 157 // views::WidgetObserver: | 252 // views::WidgetObserver: |
| 158 | 253 |
| 159 void NativeWebContentsModalDialogManagerViews::OnWidgetClosing( | 254 void NativeWebContentsModalDialogManagerViews::OnWidgetClosing( |
| 160 views::Widget* widget) { | 255 views::Widget* widget) { |
| 161 WidgetClosing(widget); | 256 #if defined(USE_AURA) |
| 257 bool widget_is_host = host_ && |
| 258 widget == views::Widget::GetWidgetForNativeView(host_->GetHostView()); |
| 259 #else |
| 260 bool widget_is_host = false; |
| 261 #endif |
| 262 if (!widget_is_host) |
| 263 WidgetClosing(widget); |
| 162 } | 264 } |
| 163 | 265 |
| 164 void NativeWebContentsModalDialogManagerViews::OnWidgetDestroying( | 266 void NativeWebContentsModalDialogManagerViews::OnWidgetDestroying( |
| 165 views::Widget* widget) { | 267 views::Widget* widget) { |
| 166 WidgetClosing(widget); | 268 #if defined(USE_AURA) |
| 167 } | 269 bool widget_is_host = host_ && |
| 270 widget == views::Widget::GetWidgetForNativeView(host_->GetHostView()); |
| 271 #else |
| 272 bool widget_is_host = false; |
| 273 #endif |
| 274 if (!widget_is_host) |
| 275 WidgetClosing(widget); |
| 276 } |
| 277 |
| 278 #if defined(USE_AURA) |
| 279 void NativeWebContentsModalDialogManagerViews::OnWidgetActivationChanged( |
| 280 views::Widget* widget, bool active) { |
| 281 if (!host_ || host_changing_) |
| 282 return; |
| 283 #if !defined(OS_CHROMEOS) |
| 284 if (active && widget != GetWidget(dialog())) { |
| 285 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); |
| 286 it != observed_widgets_.end(); ++it) { |
| 287 if (!!((*it)->GetNativeWindowProperty( |
| 288 wm::kAllowTransientParentEventsKey))) { |
| 289 if ((*it)->IsVisible()) |
| 290 (*it)->StackAtTop(); |
| 291 } |
| 292 } |
| 293 } |
| 294 #else |
| 295 // This prevents the widget from activating too early and ensures it does |
| 296 // reappear after the parent is restored. Otherwise, a crash occurs due to |
| 297 // trying to focus a non-child of the active dialog while activation change |
| 298 // observers are still shifting the focus. |
| 299 |
| 300 // If the widget is active but the parent has become inactive, note that |
| 301 // parent is inactive in case the window is minimized. |
| 302 if (widget == GetWidget(host_->GetHostView()) && !active) { |
| 303 if (GetWidget(dialog())->IsActive()) |
| 304 parent_inactive_ = true; |
| 305 } |
| 306 |
| 307 // Set window minimized |
| 308 if (!GetWidget(host_->GetHostView())->IsActive() && |
| 309 !GetWidget(dialog())->IsActive()) { |
| 310 window_minimized_ |= ash::WmWindowAura::Get( |
| 311 host_->GetHostView())->GetWindowState()->IsMinimized(); |
| 312 } |
| 313 |
| 314 // Host view reactivated, widget deactivated. Update stacking if necessary |
| 315 // and update booleans. |
| 316 if (GetWidget(dialog()) == widget && !active && |
| 317 GetWidget(host_->GetHostView())->IsActive()) { |
| 318 StackWidgetAtTop(GetWidget(dialog())); |
| 319 if (window_minimized_ && !parent_inactive_) { |
| 320 // If the window was minimized, we need to restore it, and add the dialog |
| 321 // on the next restack. |
| 322 show_on_next_stack_ = true; |
| 323 } else if (window_minimized_) { |
| 324 // if the host reactivated from the dialog and the parent was just |
| 325 // inactive, the window isn't minimized. |
| 326 window_minimized_ = false; |
| 327 } |
| 328 // Host is now active. |
| 329 parent_inactive_ = false; |
| 330 } else if (widget == GetWidget(host_->GetHostView()) && active) { |
| 331 parent_inactive_ = false; |
| 332 // If window was minimized, show the dialog on the next restack. |
| 333 if (window_minimized_) { |
| 334 show_on_next_stack_ = true; |
| 335 } |
| 336 } |
| 337 #endif |
| 338 } |
| 339 #endif |
| 168 | 340 |
| 169 void NativeWebContentsModalDialogManagerViews::HostChanged( | 341 void NativeWebContentsModalDialogManagerViews::HostChanged( |
| 170 WebContentsModalDialogHost* new_host) { | 342 WebContentsModalDialogHost* new_host) { |
| 343 #if defined(USE_AURA) |
| 344 host_changing_ = true; |
| 345 if (new_host == host_ || |
| 346 (new_host && !new_host->GetHostView()->GetRootWindow())) |
| 347 // This happens sometimes in testing and will cause a crash. |
| 348 return; |
| 349 #endif |
| 171 if (host_) | 350 if (host_) |
| 172 host_->RemoveObserver(this); | 351 host_->RemoveObserver(this); |
| 173 | 352 |
| 353 bool toplevel = false; |
| 354 // Remove window observer |
| 355 #if defined(USE_AURA) |
| 356 if (host_ && host_->GetHostView() && host_->GetHostView()->HasObserver(this)) |
| 357 host_->GetHostView()->RemoveObserver(this); |
| 358 |
| 359 // Check if there are top level widgets. |
| 360 if (host_ && host_->GetHostView() && host_->GetHostView()->parent()) { |
| 361 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); |
| 362 it != observed_widgets_.end(); ++it) { |
| 363 if (!!((*it)->GetNativeWindowProperty( |
| 364 wm::kAllowTransientParentEventsKey))) |
| 365 toplevel = true; |
| 366 } |
| 367 } |
| 368 |
| 369 if (host_ && host_->GetHostView() && toplevel) { |
| 370 // Remove observers. |
| 371 if (host_->GetHostView()->GetHost()) |
| 372 host_->GetHostView()->GetHost()->RemoveObserver(this); |
| 373 views::Widget * parent_widget = views::Widget::GetWidgetForNativeView( |
| 374 host_->GetHostView()); |
| 375 if (parent_widget) |
| 376 parent_widget->RemoveObserver(this); |
| 377 |
| 378 // Unregister the accelerator |
| 379 if (parent_widget && parent_widget->GetFocusManager()) { |
| 380 parent_widget->GetFocusManager()->UnregisterAccelerator( |
| 381 ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE), target_); |
| 382 } |
| 383 |
| 384 // Clear animations disabled |
| 385 if (host_->GetHostView()->parent()) |
| 386 host_->GetHostView()->parent()->ClearProperty( |
| 387 aura::client::kAnimationsDisabledKey); |
| 388 } |
| 389 #endif |
| 390 |
| 174 host_ = new_host; | 391 host_ = new_host; |
| 175 | 392 toplevel = false; |
| 176 // |host_| may be null during WebContents destruction or Win32 tab dragging. | 393 // |host_| may be null during WebContents destruction or Win32 tab dragging. |
| 177 if (host_) { | 394 if (host_) { |
| 178 host_->AddObserver(this); | 395 host_->AddObserver(this); |
| 179 | 396 |
| 180 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); | 397 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); |
| 181 it != observed_widgets_.end(); ++it) { | 398 it != observed_widgets_.end(); ++it) { |
| 182 views::Widget::ReparentNativeView((*it)->GetNativeView(), | 399 #if defined(USE_AURA) |
| 183 host_->GetHostView()); | 400 bool widget_toplevel = !!((*it)->GetNativeWindowProperty( |
| 184 } | 401 wm::kAllowTransientParentEventsKey)); |
| 185 | 402 #else |
| 403 bool widget_toplevel = false; |
| 404 #endif |
| 405 if (widget_toplevel) { |
| 406 #if defined(USE_AURA) |
| 407 toplevel = true; |
| 408 #if !defined(OS_WIN) |
| 409 if ((*it)->GetNativeView()->parent()) |
| 410 (*it)->GetNativeView()->parent()->RemoveChild((*it)->GetNativeView()); |
| 411 wm::AddTransientChild(host_->GetHostView()->parent(), |
| 412 (*it)->GetNativeView()); |
| 413 aura::client::ParentWindowWithContext( |
| 414 (*it)->GetNativeView(), |
| 415 host_->GetHostView()->parent()->GetRootWindow(), |
| 416 (*it)->GetNativeView()->bounds()); |
| 417 if (host_->GetHostView()->parent()) { |
| 418 host_->GetHostView()->parent()->SetProperty( |
| 419 aura::client::kAnimationsDisabledKey, true); |
| 420 } |
| 421 #else |
| 422 wm::AddTransientChild(host_->GetHostView(), |
| 423 (*it)->GetNativeView()); |
| 424 #endif |
| 425 #endif |
| 426 } else { |
| 427 views::Widget::ReparentNativeView((*it)->GetNativeView(), |
| 428 host_->GetHostView()); |
| 429 } |
| 430 } |
| 431 #if defined(USE_AURA) |
| 432 // If there are top level widgets, need to register the accelerator and set |
| 433 // the window observed_widgets_ |
| 434 if (toplevel) { |
| 435 // Add window observer |
| 436 host_->GetHostView()->AddObserver(this); |
| 437 // Add host observer |
| 438 if (host_->GetHostView()->GetHost()) |
| 439 host_->GetHostView()->GetHost()->AddObserver(this); |
| 440 // Register accelerator |
| 441 views::Widget * parent_widget = views::Widget::GetWidgetForNativeView( |
| 442 host_->GetHostView()); |
| 443 if (parent_widget) |
| 444 parent_widget->AddObserver(this); |
| 445 parent_widget->GetFocusManager()->RegisterAccelerator( |
| 446 ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE), |
| 447 ui::AcceleratorManager::kNormalPriority, target_); |
| 448 // Set animations disabled |
| 449 if (host_->GetHostView()->parent()) { |
| 450 host_->GetHostView()->parent()->SetProperty( |
| 451 aura::client::kAnimationsDisabledKey, true); |
| 452 } |
| 453 parent_widget->StackAtTop(); |
| 454 } |
| 455 // Reset all of these |
| 456 window_minimized_ = false; |
| 457 parent_inactive_ = false; |
| 458 show_on_next_stack_ = false; |
| 459 host_changing_ = false; |
| 460 #endif |
| 186 OnPositionRequiresUpdate(); | 461 OnPositionRequiresUpdate(); |
| 187 } | 462 } |
| 188 } | 463 } |
| 189 | 464 |
| 190 gfx::NativeWindow NativeWebContentsModalDialogManagerViews::dialog() { | 465 gfx::NativeWindow NativeWebContentsModalDialogManagerViews::dialog() { |
| 191 return dialog_; | 466 return dialog_; |
| 192 } | 467 } |
| 193 | 468 |
| 194 void NativeWebContentsModalDialogManagerViews::ShowWidget( | 469 void NativeWebContentsModalDialogManagerViews::ShowWidget( |
| 195 views::Widget* widget) { | 470 views::Widget* widget) { |
| 196 // |host_| may be NULL during tab drag on Views/Win32. | 471 // |host_| may be NULL during tab drag on Views/Win32. |
| 197 if (host_) | 472 if (host_) |
| 198 constrained_window::UpdateWebContentsModalDialogPosition(widget, host_); | 473 constrained_window::UpdateWebContentsModalDialogPosition(widget, host_); |
| 199 widget->Show(); | 474 widget->Show(); |
| 200 } | 475 } |
| 201 | 476 |
| 202 void NativeWebContentsModalDialogManagerViews::HideWidget( | 477 void NativeWebContentsModalDialogManagerViews::HideWidget( |
| 203 views::Widget* widget) { | 478 views::Widget* widget) { |
| 204 widget->Hide(); | 479 widget->Hide(); |
| 205 } | 480 } |
| 206 | 481 |
| 482 void NativeWebContentsModalDialogManagerViews::FocusInternal( |
| 483 views::Widget* widget) { |
| 484 if (widget->widget_delegate() && |
| 485 widget->widget_delegate()->GetInitiallyFocusedView()) |
| 486 widget->widget_delegate()->GetInitiallyFocusedView()->RequestFocus(); |
| 487 #if defined(USE_AURA) |
| 488 // We don't necessarily have a RootWindow yet. |
| 489 if (widget->GetNativeView()->GetRootWindow()) |
| 490 widget->GetNativeView()->Focus(); |
| 491 #endif |
| 492 } |
| 493 |
| 207 views::Widget* NativeWebContentsModalDialogManagerViews::GetWidget( | 494 views::Widget* NativeWebContentsModalDialogManagerViews::GetWidget( |
| 208 gfx::NativeWindow dialog) { | 495 gfx::NativeWindow dialog) { |
| 209 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(dialog); | 496 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(dialog); |
| 210 DCHECK(widget); | 497 DCHECK(widget); |
| 211 return widget; | 498 return widget; |
| 212 } | 499 } |
| 213 | 500 |
| 214 void NativeWebContentsModalDialogManagerViews::WidgetClosing( | 501 void NativeWebContentsModalDialogManagerViews::WidgetClosing( |
| 215 views::Widget* widget) { | 502 views::Widget* widget) { |
| 216 #if defined(USE_AURA) | 503 #if defined(USE_AURA) |
| 217 gfx::NativeView view = widget->GetNativeView()->parent(); | 504 if (!!widget->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)) { |
| 218 // Allow the parent to animate again. | 505 if (host_ && host_->GetHostView() && !host_destroying_) { |
| 219 if (view && view->parent()) | 506 gfx::NativeWindow parent = host_->GetHostView()->parent(); |
| 220 view->parent()->ClearProperty(aura::client::kAnimationsDisabledKey); | 507 if (parent) |
| 508 parent->ClearProperty(aura::client::kAnimationsDisabledKey); |
| 509 } |
| 510 } else { |
| 511 gfx::NativeView view = widget->GetNativeView()->parent(); |
| 512 // Allow the parent to animate again. |
| 513 if (view && view->parent()) |
| 514 view->parent()->ClearProperty(aura::client::kAnimationsDisabledKey); |
| 515 } |
| 221 #endif | 516 #endif |
| 222 widget->RemoveObserver(this); | 517 widget->RemoveObserver(this); |
| 223 observed_widgets_.erase(widget); | 518 observed_widgets_.erase(widget); |
| 224 | 519 |
| 225 #if defined(USE_AURA) | 520 #if defined(USE_AURA) |
| 226 shown_widgets_.erase(widget); | 521 shown_widgets_.erase(widget); |
| 522 #if !defined(OS_WIN) |
| 523 if (host_ && !!widget->GetNativeWindowProperty( |
| 524 wm::kAllowTransientParentEventsKey)) |
| 525 wm::RemoveTransientChild(host_->GetHostView()->parent(), |
| 526 widget->GetNativeView()); |
| 527 #endif |
| 227 #endif | 528 #endif |
| 228 | 529 |
| 229 // Will cause this object to be deleted. | 530 // Will cause this object to be deleted. |
| 230 native_delegate_->WillClose(widget->GetNativeWindow()); | 531 native_delegate_->WillClose(widget->GetNativeWindow()); |
| 231 } | 532 } |
| 232 | 533 |
| 534 #if defined(USE_AURA) |
| 535 void NativeWebContentsModalDialogManagerViews::OnWindowBoundsChanged( |
| 536 aura::Window* window, |
| 537 const gfx::Rect& old_bounds, |
| 538 const gfx::Rect& new_bounds) { |
| 539 if (window != host_->GetHostView()) |
| 540 return; |
| 541 OnNonClippedPositionRequiresUpdate(); |
| 542 if (GetWidget(dialog())->IsVisible()){ |
| 543 GetWidget(dialog())->StackAtTop(); |
| 544 Show(); |
| 545 } |
| 546 } |
| 547 |
| 548 void NativeWebContentsModalDialogManagerViews::OnWindowDestroying( |
| 549 aura::Window* window) { |
| 550 window->RemoveObserver(this); |
| 551 } |
| 552 |
| 553 void NativeWebContentsModalDialogManagerViews::OnWindowStackingChanged( |
| 554 aura::Window* window) { |
| 555 // Can't stack if host is currently null or the wrong window was restacked |
| 556 if (!host_ || window != host_->GetHostView()) |
| 557 return; |
| 558 // Unless we are calling Show(), don't restack dialogs that are hidden. |
| 559 if (!show_on_next_stack_ && !dialog()->TargetVisibility()) |
| 560 return; |
| 561 // Can't stack if window has no parent. |
| 562 if (!window->parent()) |
| 563 return; |
| 564 |
| 565 for (std::set<views::Widget*>::iterator it = shown_widgets_.begin(); |
| 566 it != shown_widgets_.end(); ++it) { |
| 567 if (views::Widget::GetWidgetForNativeView(window) && |
| 568 window->parent() == wm::GetTransientParent((*it)->GetNativeView()) && |
| 569 !!((*it)->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey))) |
| 570 { |
| 571 if (window->parent()->children().empty() || |
| 572 (window->parent()->children().back() == (*it)->GetNativeView())) |
| 573 // Already stacked correctly, continue. |
| 574 continue; |
| 575 StackWidgetAtTop((*it)); |
| 576 if (window_minimized_ && show_on_next_stack_ && |
| 577 GetWidget(host_->GetHostView())->IsVisible() && need_to_show_) { |
| 578 // Call show here, as the web contents are done loading and |
| 579 // there will not be a DCHECK crash from trying to focus the dialog. |
| 580 called_internal_ = true; |
| 581 Show(); |
| 582 window_minimized_ = false; |
| 583 show_on_next_stack_ = false; |
| 584 need_to_show_ = false; |
| 585 } else if (window_minimized_ && show_on_next_stack_) { |
| 586 called_internal_ = true; |
| 587 } |
| 588 } |
| 589 } |
| 590 } |
| 591 |
| 592 void NativeWebContentsModalDialogManagerViews::OnWindowParentChanged( |
| 593 aura::Window* window, aura::Window* parent) { |
| 594 if (!host_ || !parent || window != host_->GetHostView()) |
| 595 return; |
| 596 |
| 597 bool has_visibility = false; |
| 598 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); |
| 599 it != observed_widgets_.end(); ++it) { |
| 600 if (!!((*it)->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey))) |
| 601 { |
| 602 aura::Window * nativeView = (*it)->GetNativeView(); |
| 603 if (parent != wm::GetTransientParent(nativeView)) |
| 604 wm::AddTransientChild(host_->GetHostView()->parent(), nativeView); |
| 605 has_visibility |= nativeView->TargetVisibility(); |
| 606 } |
| 607 } |
| 608 if (has_visibility) { |
| 609 called_internal_ = true; |
| 610 #if !defined(OS_WIN) |
| 611 Show(); |
| 612 #endif |
| 613 } |
| 614 } |
| 615 |
| 616 void NativeWebContentsModalDialogManagerViews::StackWidgetAtTop( |
| 617 views::Widget* widget) { |
| 618 aura::Window* transient_parent = wm::GetTransientParent( |
| 619 widget->GetNativeView()); |
| 620 while (transient_parent) { |
| 621 if (transient_parent->parent()) |
| 622 transient_parent->parent()->StackChildAtTop(transient_parent); |
| 623 transient_parent = wm::GetTransientParent(transient_parent); |
| 624 } |
| 625 if (!host_ || !host_->GetHostView() || !widget->GetNativeView()->parent() || |
| 626 widget->GetNativeView() == host_->GetHostView()) |
| 627 return; |
| 628 if (widget->GetNativeView()->parent() == host_->GetHostView()->parent()) { |
| 629 widget->GetNativeView()->parent()->StackChildAbove(widget->GetNativeView(), |
| 630 host_->GetHostView()); |
| 631 } else { |
| 632 widget->GetNativeView()->parent()->StackChildAtTop(widget->GetNativeView()); |
| 633 } |
| 634 } |
| 635 |
| 636 void NativeWebContentsModalDialogManagerViews::OnHostMovedInPixels( |
| 637 const aura::WindowTreeHost* host, const gfx::Point& new_origin_in_pixels) { |
| 638 if (!host_ || host_changing_ || host != host_->GetHostView()->GetHost()) |
| 639 return; |
| 640 views::Widget* parent_widget = |
| 641 views::Widget::GetWidgetForNativeView(host_->GetHostView()); |
| 642 parent_widget->StackAtTop(); |
| 643 |
| 644 OnNonClippedPositionRequiresUpdate(); |
| 645 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); |
| 646 it != observed_widgets_.end(); ++it) { |
| 647 if (!!((*it)->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey))) |
| 648 { |
| 649 if ((*it)->IsVisible()){ |
| 650 (*it)->StackAtTop(); |
| 651 Show(); |
| 652 } |
| 653 } |
| 654 } |
| 655 } |
| 656 #endif // defined(USE_AURA) |
| 233 } // namespace constrained_window | 657 } // namespace constrained_window |
| OLD | NEW |