1 // Copyright (c) 2012 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.
5 #include "components/constrained_window/constrained_window_views.h"
9 #include "components/constrained_window/constrained_window_views_client.h"
10 #include "components/guest_view/browser/guest_view_base.h"
11 #include "components/web_modal/popup_manager.h"
12 #include "components/web_modal/web_contents_modal_dialog_host.h"
13 #include "ui/views/border.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/widget/widget_observer.h"
16 #include "ui/views/window/dialog_delegate.h"
18 using web_modal::ModalDialogHost
;
19 using web_modal::ModalDialogHostObserver
;
21 namespace constrained_window
{
24 ConstrainedWindowViewsClient
* constrained_window_views_client
= NULL
;
26 // The name of a key to store on the window handle to associate
27 // WidgetModalDialogHostObserverViews with the Widget.
28 const char* const kWidgetModalDialogHostObserverViewsKey
=
29 "__WIDGET_MODAL_DIALOG_HOST_OBSERVER_VIEWS__";
31 // Applies positioning changes from the ModalDialogHost to the Widget.
32 class WidgetModalDialogHostObserverViews
33 : public views::WidgetObserver
,
34 public ModalDialogHostObserver
{
36 WidgetModalDialogHostObserverViews(ModalDialogHost
* host
,
37 views::Widget
* target_widget
,
38 const char *const native_window_property
)
40 target_widget_(target_widget
),
41 native_window_property_(native_window_property
) {
43 DCHECK(target_widget_
);
44 host_
->AddObserver(this);
45 target_widget_
->AddObserver(this);
48 ~WidgetModalDialogHostObserverViews() override
{
50 host_
->RemoveObserver(this);
51 target_widget_
->RemoveObserver(this);
52 target_widget_
->SetNativeWindowProperty(native_window_property_
, NULL
);
55 // WidgetObserver overrides
56 void OnWidgetClosing(views::Widget
* widget
) override
{ delete this; }
58 // WebContentsModalDialogHostObserver overrides
59 void OnPositionRequiresUpdate() override
{
60 UpdateWidgetModalDialogPosition(target_widget_
, host_
);
63 void OnHostDestroying() override
{
64 host_
->RemoveObserver(this);
69 ModalDialogHost
* host_
;
70 views::Widget
* target_widget_
;
71 const char* const native_window_property_
;
73 DISALLOW_COPY_AND_ASSIGN(WidgetModalDialogHostObserverViews
);
76 void UpdateModalDialogPosition(views::Widget
* widget
,
77 web_modal::ModalDialogHost
* dialog_host
,
78 const gfx::Size
& size
) {
79 // Do not forcibly update the dialog widget position if it is being dragged.
80 if (widget
->HasCapture())
83 gfx::Point position
= dialog_host
->GetDialogPosition(size
);
84 views::Border
* border
= widget
->non_client_view()->frame_view()->border();
85 // Border may be null during widget initialization.
87 // Align the first row of pixels inside the border. This is the apparent
89 position
.set_y(position
.y() - border
->GetInsets().top());
92 if (widget
->is_top_level()) {
94 views::Widget::GetWidgetForNativeView(dialog_host
->GetHostView())->
95 GetClientAreaBoundsInScreen().OffsetFromOrigin();
98 widget
->SetBounds(gfx::Rect(position
, size
));
104 void SetConstrainedWindowViewsClient(
105 scoped_ptr
<ConstrainedWindowViewsClient
> new_client
) {
106 delete constrained_window_views_client
;
107 constrained_window_views_client
= new_client
.release();
110 void UpdateWebContentsModalDialogPosition(
111 views::Widget
* widget
,
112 web_modal::WebContentsModalDialogHost
* dialog_host
) {
113 gfx::Size size
= widget
->GetRootView()->GetPreferredSize();
114 gfx::Size max_size
= dialog_host
->GetMaximumDialogSize();
115 // Enlarge the max size by the top border, as the dialog will be shifted
116 // outside the area specified by the dialog host by this amount later.
117 views::Border
* border
=
118 widget
->non_client_view()->frame_view()->border();
119 // Border may be null during widget initialization.
121 max_size
.Enlarge(0, border
->GetInsets().top());
122 size
.SetToMin(max_size
);
123 UpdateModalDialogPosition(widget
, dialog_host
, size
);
126 void UpdateWidgetModalDialogPosition(views::Widget
* widget
,
127 web_modal::ModalDialogHost
* dialog_host
) {
128 UpdateModalDialogPosition(widget
, dialog_host
,
129 widget
->GetRootView()->GetPreferredSize());
132 views::Widget
* ShowWebModalDialogViews(
133 views::WidgetDelegate
* dialog
,
134 content::WebContents
* initiator_web_contents
) {
135 DCHECK(constrained_window_views_client
);
136 // For embedded WebContents, use the embedder's WebContents for constrained
138 content::WebContents
* web_contents
=
139 guest_view::GuestViewBase::GetTopLevelWebContents(initiator_web_contents
);
140 views::Widget
* widget
= CreateWebModalDialogViews(dialog
, web_contents
);
141 web_modal::PopupManager
* popup_manager
=
142 web_modal::PopupManager::FromWebContents(web_contents
);
143 popup_manager
->ShowModalDialog(widget
->GetNativeWindow(), web_contents
);
147 views::Widget
* CreateWebModalDialogViews(views::WidgetDelegate
* dialog
,
148 content::WebContents
* web_contents
) {
149 DCHECK_EQ(ui::MODAL_TYPE_CHILD
, dialog
->GetModalType());
150 web_modal::PopupManager
* popup_manager
=
151 web_modal::PopupManager::FromWebContents(web_contents
);
152 const gfx::NativeView parent
= popup_manager
->GetHostView();
153 return views::DialogDelegate::CreateDialogWidget(dialog
, NULL
, parent
);
156 // TODO(gbillock): Replace this with PopupManager calls.
157 views::Widget
* CreateBrowserModalDialogViews(views::DialogDelegate
* dialog
,
158 gfx::NativeWindow parent
) {
159 DCHECK_NE(ui::MODAL_TYPE_CHILD
, dialog
->GetModalType());
160 DCHECK_NE(ui::MODAL_TYPE_NONE
, dialog
->GetModalType());
162 DCHECK(constrained_window_views_client
);
163 gfx::NativeView parent_view
=
164 parent
? constrained_window_views_client
->GetDialogHostView(parent
)
166 views::Widget
* widget
=
167 views::DialogDelegate::CreateDialogWidget(dialog
, NULL
, parent_view
);
168 if (!dialog
->UseNewStyleForThisDialog())
170 ModalDialogHost
* host
= constrained_window_views_client
->
171 GetModalDialogHost(parent
);
173 DCHECK_EQ(parent_view
, host
->GetHostView());
174 ModalDialogHostObserver
* dialog_host_observer
=
175 new WidgetModalDialogHostObserverViews(
176 host
, widget
, kWidgetModalDialogHostObserverViewsKey
);
177 dialog_host_observer
->OnPositionRequiresUpdate();
182 } // namespace constrained window