Make the custom launcher page's WebContents transparent.
[chromium-blink-merge.git] / components / constrained_window / constrained_window_views.cc
blob577ee039a2f36e710fb4659cad5d7e59e45f53f7
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"
7 #include <algorithm>
9 #include "components/constrained_window/constrained_window_views_client.h"
10 #include "components/web_modal/popup_manager.h"
11 #include "components/web_modal/web_contents_modal_dialog_host.h"
12 #include "ui/views/border.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/widget/widget_observer.h"
15 #include "ui/views/window/dialog_delegate.h"
17 using web_modal::ModalDialogHost;
18 using web_modal::ModalDialogHostObserver;
20 namespace constrained_window {
21 namespace {
23 ConstrainedWindowViewsClient* constrained_window_views_client = NULL;
25 // The name of a key to store on the window handle to associate
26 // WidgetModalDialogHostObserverViews with the Widget.
27 const char* const kWidgetModalDialogHostObserverViewsKey =
28 "__WIDGET_MODAL_DIALOG_HOST_OBSERVER_VIEWS__";
30 // Applies positioning changes from the ModalDialogHost to the Widget.
31 class WidgetModalDialogHostObserverViews
32 : public views::WidgetObserver,
33 public ModalDialogHostObserver {
34 public:
35 WidgetModalDialogHostObserverViews(ModalDialogHost* host,
36 views::Widget* target_widget,
37 const char *const native_window_property)
38 : host_(host),
39 target_widget_(target_widget),
40 native_window_property_(native_window_property) {
41 DCHECK(host_);
42 DCHECK(target_widget_);
43 host_->AddObserver(this);
44 target_widget_->AddObserver(this);
47 ~WidgetModalDialogHostObserverViews() override {
48 if (host_)
49 host_->RemoveObserver(this);
50 target_widget_->RemoveObserver(this);
51 target_widget_->SetNativeWindowProperty(native_window_property_, NULL);
54 // WidgetObserver overrides
55 void OnWidgetClosing(views::Widget* widget) override { delete this; }
57 // WebContentsModalDialogHostObserver overrides
58 void OnPositionRequiresUpdate() override {
59 UpdateWidgetModalDialogPosition(target_widget_, host_);
62 void OnHostDestroying() override {
63 host_->RemoveObserver(this);
64 host_ = NULL;
67 private:
68 ModalDialogHost* host_;
69 views::Widget* target_widget_;
70 const char* const native_window_property_;
72 DISALLOW_COPY_AND_ASSIGN(WidgetModalDialogHostObserverViews);
75 void UpdateModalDialogPosition(views::Widget* widget,
76 web_modal::ModalDialogHost* dialog_host,
77 const gfx::Size& size) {
78 // Do not forcibly update the dialog widget position if it is being dragged.
79 if (widget->HasCapture())
80 return;
82 gfx::Point position = dialog_host->GetDialogPosition(size);
83 views::Border* border = widget->non_client_view()->frame_view()->border();
84 // Border may be null during widget initialization.
85 if (border) {
86 // Align the first row of pixels inside the border. This is the apparent
87 // top of the dialog.
88 position.set_y(position.y() - border->GetInsets().top());
91 if (widget->is_top_level()) {
92 position +=
93 views::Widget::GetWidgetForNativeView(dialog_host->GetHostView())->
94 GetClientAreaBoundsInScreen().OffsetFromOrigin();
97 widget->SetBounds(gfx::Rect(position, size));
100 } // namespace
102 // static
103 void SetConstrainedWindowViewsClient(
104 scoped_ptr<ConstrainedWindowViewsClient> new_client) {
105 delete constrained_window_views_client;
106 constrained_window_views_client = new_client.release();
109 void UpdateWebContentsModalDialogPosition(
110 views::Widget* widget,
111 web_modal::WebContentsModalDialogHost* dialog_host) {
112 gfx::Size size = widget->GetRootView()->GetPreferredSize();
113 gfx::Size max_size = dialog_host->GetMaximumDialogSize();
114 // Enlarge the max size by the top border, as the dialog will be shifted
115 // outside the area specified by the dialog host by this amount later.
116 views::Border* border =
117 widget->non_client_view()->frame_view()->border();
118 // Border may be null during widget initialization.
119 if (border)
120 max_size.Enlarge(0, border->GetInsets().top());
121 size.SetToMin(max_size);
122 UpdateModalDialogPosition(widget, dialog_host, size);
125 void UpdateWidgetModalDialogPosition(views::Widget* widget,
126 web_modal::ModalDialogHost* dialog_host) {
127 UpdateModalDialogPosition(widget, dialog_host,
128 widget->GetRootView()->GetPreferredSize());
131 views::Widget* ShowWebModalDialogViews(
132 views::WidgetDelegate* dialog,
133 content::WebContents* initiator_web_contents) {
134 DCHECK(constrained_window_views_client);
135 // For embedded WebContents, use the embedder's WebContents for constrained
136 // window.
137 content::WebContents* web_contents = constrained_window_views_client->
138 GetEmbedderWebContents(initiator_web_contents);
139 views::Widget* widget = CreateWebModalDialogViews(dialog, web_contents);
140 web_modal::PopupManager* popup_manager =
141 web_modal::PopupManager::FromWebContents(web_contents);
142 popup_manager->ShowModalDialog(widget->GetNativeWindow(), web_contents);
143 return widget;
146 views::Widget* CreateWebModalDialogViews(views::WidgetDelegate* dialog,
147 content::WebContents* web_contents) {
148 DCHECK_EQ(ui::MODAL_TYPE_CHILD, dialog->GetModalType());
149 web_modal::PopupManager* popup_manager =
150 web_modal::PopupManager::FromWebContents(web_contents);
151 const gfx::NativeView parent = popup_manager->GetHostView();
152 return views::DialogDelegate::CreateDialogWidget(dialog, NULL, parent);
155 // TODO(gbillock): Replace this with PopupManager calls.
156 views::Widget* CreateBrowserModalDialogViews(views::DialogDelegate* dialog,
157 gfx::NativeWindow parent) {
158 DCHECK_NE(ui::MODAL_TYPE_CHILD, dialog->GetModalType());
159 DCHECK_NE(ui::MODAL_TYPE_NONE, dialog->GetModalType());
161 DCHECK(constrained_window_views_client);
162 gfx::NativeView parent_view =
163 parent ? constrained_window_views_client->GetDialogHostView(parent)
164 : nullptr;
165 views::Widget* widget =
166 views::DialogDelegate::CreateDialogWidget(dialog, NULL, parent_view);
167 if (!dialog->UseNewStyleForThisDialog())
168 return widget;
169 ModalDialogHost* host = constrained_window_views_client->
170 GetModalDialogHost(parent);
171 if (host) {
172 DCHECK_EQ(parent_view, host->GetHostView());
173 ModalDialogHostObserver* dialog_host_observer =
174 new WidgetModalDialogHostObserverViews(
175 host, widget, kWidgetModalDialogHostObserverViewsKey);
176 dialog_host_observer->OnPositionRequiresUpdate();
178 return widget;
181 } // namespace constrained window