Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: components/constrained_window/native_web_contents_modal_dialog_manager_views.cc

Issue 2172363002: Created min size for print preview dialog and modified to allow the Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix mac build error Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_tree_client.h"
22 #include "ui/aura/window.h" 24 #include "ui/aura/window.h"
25 #include "ui/aura/window_observer.h"
23 #include "ui/wm/core/visibility_controller.h" 26 #include "ui/wm/core/visibility_controller.h"
24 #include "ui/wm/core/window_animations.h" 27 #include "ui/wm/core/window_animations.h"
25 #include "ui/wm/core/window_modality_controller.h" 28 #include "ui/wm/core/window_modality_controller.h"
29 #include "ui/wm/core/window_util.h"
26 #endif 30 #endif
27 31
28 using web_modal::SingleWebContentsDialogManager; 32 using web_modal::SingleWebContentsDialogManager;
29 using web_modal::SingleWebContentsDialogManagerDelegate; 33 using web_modal::SingleWebContentsDialogManagerDelegate;
30 using web_modal::WebContentsModalDialogHost; 34 using web_modal::WebContentsModalDialogHost;
31 using web_modal::ModalDialogHostObserver; 35 using web_modal::ModalDialogHostObserver;
32 36
33 namespace constrained_window { 37 namespace constrained_window {
34 38
35 NativeWebContentsModalDialogManagerViews:: 39 NativeWebContentsModalDialogManagerViews::
36 NativeWebContentsModalDialogManagerViews( 40 NativeWebContentsModalDialogManagerViews(
37 gfx::NativeWindow dialog, 41 gfx::NativeWindow dialog,
38 SingleWebContentsDialogManagerDelegate* native_delegate) 42 SingleWebContentsDialogManagerDelegate* native_delegate,
43 bool is_toplevel,
44 ui::AcceleratorTarget* target)
39 : native_delegate_(native_delegate), 45 : native_delegate_(native_delegate),
40 dialog_(dialog), 46 dialog_(dialog),
41 host_(NULL), 47 host_(NULL),
42 host_destroying_(false) { 48 host_destroying_(false),
43 ManageDialog(); 49 target_(target) {
50 #if !defined(USE_AURA)
51 target_ = NULL;
52 #endif
53 ManageDialog(is_toplevel);
44 } 54 }
45 55
46 NativeWebContentsModalDialogManagerViews:: 56 NativeWebContentsModalDialogManagerViews::
47 ~NativeWebContentsModalDialogManagerViews() { 57 ~NativeWebContentsModalDialogManagerViews() {
48 if (host_) 58 if (host_)
49 host_->RemoveObserver(this); 59 host_->RemoveObserver(this);
50 60
61 #if defined(USE_AURA)
62 if (host_ && host_->GetHostView() && host_->GetHostView()->HasObserver(this))
63 host_->GetHostView()->RemoveObserver(this);
64 #endif
65
51 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); 66 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin();
52 it != observed_widgets_.end(); ++it) { 67 it != observed_widgets_.end(); ++it) {
53 (*it)->RemoveObserver(this); 68 (*it)->RemoveObserver(this);
54 } 69 }
70
71 #if defined(USE_AURA)
72 if (host_ && dialog() &&
73 !!dialog()->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)) {
74 views::Widget * parent_widget = views::Widget::GetWidgetForNativeView(
75 host_->GetHostView());
76 if (parent_widget && parent_widget->GetFocusManager()) {
77 parent_widget->GetFocusManager()->UnregisterAccelerator(
78 ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE), target_);
79 }
80 }
81 #endif
55 } 82 }
56 83
57 void NativeWebContentsModalDialogManagerViews::ManageDialog() { 84 void NativeWebContentsModalDialogManagerViews::ManageDialog(
85 bool is_toplevel) {
58 views::Widget* widget = GetWidget(dialog()); 86 views::Widget* widget = GetWidget(dialog());
59 widget->AddObserver(this); 87 widget->AddObserver(this);
60 observed_widgets_.insert(widget); 88 observed_widgets_.insert(widget);
61 widget->set_movement_disabled(true); 89 widget->set_movement_disabled(true);
62 90
63 #if defined(USE_AURA) 91 #if defined(USE_AURA)
64 // TODO(wittman): remove once the new visual style is complete 92 // TODO(wittman): remove once the new visual style is complete
65 widget->GetNativeWindow()->SetProperty(aura::client::kConstrainedWindowKey, 93 widget->GetNativeWindow()->SetProperty(aura::client::kConstrainedWindowKey,
66 true); 94 true);
67 95
68 wm::SetWindowVisibilityAnimationType( 96 wm::SetWindowVisibilityAnimationType(
69 widget->GetNativeWindow(), wm::WINDOW_VISIBILITY_ANIMATION_TYPE_ROTATE); 97 widget->GetNativeWindow(), wm::WINDOW_VISIBILITY_ANIMATION_TYPE_ROTATE);
70 98
71 gfx::NativeView parent = widget->GetNativeView()->parent(); 99 if (is_toplevel) {
72 wm::SetChildWindowVisibilityChangesAnimated(parent); 100 gfx::NativeView transient_parent =
73 // No animations should get performed on the window since that will re-order 101 wm::GetTransientParent(widget->GetNativeWindow());
74 // the window stack which will then cause many problems. 102 wm::SetChildWindowVisibilityChangesAnimated(
75 if (parent && parent->parent()) { 103 widget->GetNativeView()->parent());
76 parent->parent()->SetProperty(aura::client::kAnimationsDisabledKey, true); 104 if (transient_parent && transient_parent->parent()) {
105 transient_parent->parent()->SetProperty(
106 aura::client::kAnimationsDisabledKey, true);
107 }
108 widget->SetNativeWindowProperty(wm::kAllowTransientParentEventsKey,
109 reinterpret_cast<void*>(true));
110 } else {
111 gfx::NativeView parent = widget->GetNativeView()->parent();
112 wm::SetChildWindowVisibilityChangesAnimated(parent);
113 // No animations should get performed on the window since that will re-order
114 // the window stack which will then cause many problems.
115 if (parent && parent->parent()) {
116 parent->parent()->SetProperty(aura::client::kAnimationsDisabledKey, true);
117 }
118 wm::SetModalParent(widget->GetNativeWindow(),
119 native_delegate_->GetWebContents()->GetNativeView());
77 } 120 }
78 121 #endif // defined(USE_AURA)
79 wm::SetModalParent(widget->GetNativeWindow(),
80 native_delegate_->GetWebContents()->GetNativeView());
81 #endif
82 } 122 }
83 123
84 // SingleWebContentsDialogManager: 124 // SingleWebContentsDialogManager:
85 125
86 void NativeWebContentsModalDialogManagerViews::Show() { 126 void NativeWebContentsModalDialogManagerViews::Show() {
87 // The host destroying means the dialogs will be destroyed in short order. 127 // The host destroying means the dialogs will be destroyed in short order.
88 // Avoid showing dialogs at this point as the necessary native window 128 // Avoid showing dialogs at this point as the necessary native window
89 // services may not be present. 129 // services may not be present.
90 if (host_destroying_) 130 if (host_destroying_)
91 return; 131 return;
92 132
93 views::Widget* widget = GetWidget(dialog()); 133 views::Widget* widget = GetWidget(dialog());
94 #if defined(USE_AURA) 134 #if defined(USE_AURA)
95 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend; 135 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend;
96 if (shown_widgets_.find(widget) != shown_widgets_.end()) { 136 if (shown_widgets_.find(widget) != shown_widgets_.end()) {
97 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations( 137 if (!widget->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)) {
98 widget->GetNativeWindow()->parent())); 138 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations(
139 widget->GetNativeWindow()->parent()));
140 }
99 } 141 }
100 #endif 142 #endif
101 ShowWidget(widget); 143 ShowWidget(widget);
102 Focus(); 144 Focus();
103 145
104 #if defined(USE_AURA) 146 #if defined(USE_AURA)
105 // TODO(pkotwicz): Control the z-order of the constrained dialog via 147 // TODO(pkotwicz): Control the z-order of the constrained dialog via
106 // views::kHostViewKey. We will need to ensure that the parent window's 148 // 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. 149 // shadows are below the constrained dialog in z-order when we do this.
108 shown_widgets_.insert(widget); 150 shown_widgets_.insert(widget);
109 #endif 151 #endif
110 } 152 }
111 153
112 void NativeWebContentsModalDialogManagerViews::Hide() { 154 void NativeWebContentsModalDialogManagerViews::Hide() {
113 views::Widget* widget = GetWidget(dialog()); 155 views::Widget* widget = GetWidget(dialog());
114 #if defined(USE_AURA) 156 #if defined(USE_AURA)
115 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend; 157 std::unique_ptr<wm::SuspendChildWindowVisibilityAnimations> suspend;
116 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations( 158 suspend.reset(new wm::SuspendChildWindowVisibilityAnimations(
117 widget->GetNativeWindow()->parent())); 159 widget->GetNativeWindow()->parent()));
118 #endif 160 #endif
119 HideWidget(widget); 161 HideWidget(widget);
120 } 162 }
121 163
122 void NativeWebContentsModalDialogManagerViews::Close() { 164 void NativeWebContentsModalDialogManagerViews::Close() {
123 GetWidget(dialog())->Close(); 165 GetWidget(dialog())->Close();
124 } 166 }
125 167
126 void NativeWebContentsModalDialogManagerViews::Focus() { 168 void NativeWebContentsModalDialogManagerViews::Focus() {
127 views::Widget* widget = GetWidget(dialog()); 169 views::Widget* widget = GetWidget(dialog());
(...skipping 15 matching lines...) Expand all
143 DCHECK(host_); 185 DCHECK(host_);
144 186
145 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); 187 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin();
146 it != observed_widgets_.end(); ++it) { 188 it != observed_widgets_.end(); ++it) {
147 constrained_window::UpdateWebContentsModalDialogPosition(*it, host_); 189 constrained_window::UpdateWebContentsModalDialogPosition(*it, host_);
148 } 190 }
149 } 191 }
150 192
151 void NativeWebContentsModalDialogManagerViews::OnHostDestroying() { 193 void NativeWebContentsModalDialogManagerViews::OnHostDestroying() {
152 host_->RemoveObserver(this); 194 host_->RemoveObserver(this);
195 #if defined(USE_AURA)
196 if (host_->GetHostView() && host_->GetHostView()->HasObserver(this))
197 host_->GetHostView()->RemoveObserver(this);
198 #endif
153 host_ = NULL; 199 host_ = NULL;
154 host_destroying_ = true; 200 host_destroying_ = true;
155 } 201 }
156 202
157 // views::WidgetObserver: 203 // views::WidgetObserver:
158 204
159 void NativeWebContentsModalDialogManagerViews::OnWidgetClosing( 205 void NativeWebContentsModalDialogManagerViews::OnWidgetClosing(
160 views::Widget* widget) { 206 views::Widget* widget) {
161 WidgetClosing(widget); 207 WidgetClosing(widget);
162 } 208 }
163 209
164 void NativeWebContentsModalDialogManagerViews::OnWidgetDestroying( 210 void NativeWebContentsModalDialogManagerViews::OnWidgetDestroying(
165 views::Widget* widget) { 211 views::Widget* widget) {
166 WidgetClosing(widget); 212 WidgetClosing(widget);
167 } 213 }
168 214
169 void NativeWebContentsModalDialogManagerViews::HostChanged( 215 void NativeWebContentsModalDialogManagerViews::HostChanged(
170 WebContentsModalDialogHost* new_host) { 216 WebContentsModalDialogHost* new_host) {
217 #if defined(USE_AURA)
218 if (new_host == host_ ||
219 (new_host && !new_host->GetHostView()->GetRootWindow()))
220 // This happens sometimes in testing and will cause a crash.
221 return;
222 #endif
171 if (host_) 223 if (host_)
172 host_->RemoveObserver(this); 224 host_->RemoveObserver(this);
173 225
226 // Remove window observer
227 #if defined(USE_AURA)
228 if (host_ && host_->GetHostView() && host_->GetHostView()->HasObserver(this))
229 host_->GetHostView()->RemoveObserver(this);
230
231 bool toplevel = !!(dialog()->GetNativeWindowProperty(
232 wm::kAllowTransientParentEventsKey));
233
234 // Remove accelerators so that web contents don't pick up accelerators on
235 // other hosts
236 if (host_ && toplevel) {
237 views::Widget * parent_widget = views::Widget::GetWidgetForNativeView(
238 host_->GetHostView());
239 if (parent_widget && parent_widget->GetFocusManager()) {
240 parent_widget->GetFocusManager()->UnregisterAccelerator(
241 ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE), target_);
242 }
243 }
244 #endif
245
174 host_ = new_host; 246 host_ = new_host;
175 247
176 // |host_| may be null during WebContents destruction or Win32 tab dragging. 248 // |host_| may be null during WebContents destruction or Win32 tab dragging.
177 if (host_) { 249 if (host_) {
178 host_->AddObserver(this); 250 host_->AddObserver(this);
179 251
180 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin(); 252 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin();
181 it != observed_widgets_.end(); ++it) { 253 it != observed_widgets_.end(); ++it) {
182 views::Widget::ReparentNativeView((*it)->GetNativeView(), 254 #if defined(USE_AURA)
183 host_->GetHostView()); 255 bool widget_toplevel = !!((*it)->GetNativeWindowProperty(
256 wm::kAllowTransientParentEventsKey));
257 #else
258 bool widget_toplevel = false;
259 #endif
260 if(widget_toplevel) {
261 #if defined(USE_AURA)
262 if ((*it)->GetNativeView()->parent())
263 (*it)->GetNativeView()->parent()->RemoveChild((*it)->GetNativeView());
264 wm::AddTransientChild(host_->GetHostView(), (*it)->GetNativeView());
265 aura::client::ParentWindowWithContext(
266 (*it)->GetNativeView(), host_->GetHostView()->GetRootWindow(),
267 (*it)->GetNativeView()->bounds());
268 #endif
269 } else {
270 views::Widget::ReparentNativeView((*it)->GetNativeView(),
271 host_->GetHostView());
272 }
184 } 273 }
185 274 #if defined(USE_AURA)
275 // If there are top level widgets, need to register the accelerator and set
276 // the window observer
277 if (toplevel) {
278 host_->GetHostView()->AddObserver(this);
279 views::Widget * parent_widget = views::Widget::GetWidgetForNativeView(
280 host_->GetHostView());
281 parent_widget->GetFocusManager()->RegisterAccelerator(
282 ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE),
283 ui::AcceleratorManager::kNormalPriority, target_);
284 }
285 #endif
186 OnPositionRequiresUpdate(); 286 OnPositionRequiresUpdate();
187 } 287 }
188 } 288 }
189 289
190 gfx::NativeWindow NativeWebContentsModalDialogManagerViews::dialog() { 290 gfx::NativeWindow NativeWebContentsModalDialogManagerViews::dialog() {
191 return dialog_; 291 return dialog_;
192 } 292 }
193 293
194 void NativeWebContentsModalDialogManagerViews::ShowWidget( 294 void NativeWebContentsModalDialogManagerViews::ShowWidget(
195 views::Widget* widget) { 295 views::Widget* widget) {
196 // |host_| may be NULL during tab drag on Views/Win32. 296 // |host_| may be NULL during tab drag on Views/Win32.
197 if (host_) 297 if (host_)
198 constrained_window::UpdateWebContentsModalDialogPosition(widget, host_); 298 constrained_window::UpdateWebContentsModalDialogPosition(widget, host_);
199 widget->Show(); 299 widget->Show();
200 } 300 }
201 301
202 void NativeWebContentsModalDialogManagerViews::HideWidget( 302 void NativeWebContentsModalDialogManagerViews::HideWidget(
203 views::Widget* widget) { 303 views::Widget* widget) {
204 widget->Hide(); 304 widget->Hide();
205 } 305 }
206 306
207 views::Widget* NativeWebContentsModalDialogManagerViews::GetWidget( 307 views::Widget* NativeWebContentsModalDialogManagerViews::GetWidget(
208 gfx::NativeWindow dialog) { 308 gfx::NativeWindow dialog) {
209 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(dialog); 309 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(dialog);
210 DCHECK(widget); 310 DCHECK(widget);
211 return widget; 311 return widget;
212 } 312 }
213 313
314 void NativeWebContentsModalDialogManagerViews::
315 OnNonClippedPositionRequiresUpdate() {
316 DCHECK(host_);
317 #if defined(USE_AURA)
318 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin();
319 it != observed_widgets_.end(); ++it) {
320 if (!!((*it)->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)))
321 constrained_window::UpdateWebContentsModalDialogPosition(*it, host_);
322 }
323 #endif
324 }
325
214 void NativeWebContentsModalDialogManagerViews::WidgetClosing( 326 void NativeWebContentsModalDialogManagerViews::WidgetClosing(
215 views::Widget* widget) { 327 views::Widget* widget) {
216 #if defined(USE_AURA) 328 #if defined(USE_AURA)
217 gfx::NativeView view = widget->GetNativeView()->parent(); 329 if (!!widget->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)) {
218 // Allow the parent to animate again. 330 gfx::NativeWindow parent =
219 if (view && view->parent()) 331 wm::GetTransientParent(widget->GetNativeWindow());
220 view->parent()->ClearProperty(aura::client::kAnimationsDisabledKey); 332 if (parent && parent->parent())
333 parent->parent()->ClearProperty(aura::client::kAnimationsDisabledKey);
334 } else {
335 gfx::NativeView view = widget->GetNativeView()->parent();
336 // Allow the parent to animate again.
337 if (view && view->parent())
338 view->parent()->ClearProperty(aura::client::kAnimationsDisabledKey);
339 }
221 #endif 340 #endif
222 widget->RemoveObserver(this); 341 widget->RemoveObserver(this);
223 observed_widgets_.erase(widget); 342 observed_widgets_.erase(widget);
224 343
225 #if defined(USE_AURA) 344 #if defined(USE_AURA)
226 shown_widgets_.erase(widget); 345 shown_widgets_.erase(widget);
346 if (host_ && !!widget->GetNativeWindowProperty(
347 wm::kAllowTransientParentEventsKey))
348 wm::RemoveTransientChild(host_->GetHostView(), widget->GetNativeView());
227 #endif 349 #endif
228 350
229 // Will cause this object to be deleted. 351 // Will cause this object to be deleted.
230 native_delegate_->WillClose(widget->GetNativeWindow()); 352 native_delegate_->WillClose(widget->GetNativeWindow());
231 } 353 }
232 354
355 #if defined(USE_AURA)
356 void NativeWebContentsModalDialogManagerViews::OnWindowBoundsChanged(
357 aura::Window* window,
358 const gfx::Rect& old_bounds,
359 const gfx::Rect& new_bounds) {
360 if (window != host_->GetHostView())
361 return;
362 OnNonClippedPositionRequiresUpdate();
363 }
364
365 // Not sure if this is needed or not.
366 void NativeWebContentsModalDialogManagerViews::OnWindowRemovingFromRootWindow(
367 aura::Window* window,
368 aura::Window* new_root) {
369 if (window != host_->GetHostView())
370 return;
371 LOG(ERROR) << "Window removing from root window";
372 for (std::set<views::Widget*>::iterator it = observed_widgets_.begin();
373 it != observed_widgets_.end(); ++it) {
374 if (!!((*it)->GetNativeWindowProperty(
375 wm::kAllowTransientParentEventsKey)))
376 aura::client::ParentWindowWithContext((*it)->GetNativeView(),
377 host_->GetHostView()->GetRootWindow(), (*it)->GetNativeView()->bounds());
378 }
379 }
380
381 void NativeWebContentsModalDialogManagerViews::OnWindowDestroying(
382 aura::Window* window) {
383 window->RemoveObserver(this);
384 }
385 #endif
233 } // namespace constrained_window 386 } // namespace constrained_window
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698