[Extensions UI Mac] Make extensions overflow menu keyboard-accessible
[chromium-blink-merge.git] / components / constrained_window / constrained_window_views.cc
blob2f3c3dac2dce9dc4ea3b06eba533812f35bcb393
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/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 {
22 namespace {
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 {
35 public:
36 WidgetModalDialogHostObserverViews(ModalDialogHost* host,
37 views::Widget* target_widget,
38 const char *const native_window_property)
39 : host_(host),
40 target_widget_(target_widget),
41 native_window_property_(native_window_property) {
42 DCHECK(host_);
43 DCHECK(target_widget_);
44 host_->AddObserver(this);
45 target_widget_->AddObserver(this);
48 ~WidgetModalDialogHostObserverViews() override {
49 if (host_)
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);
65 host_ = NULL;
68 private:
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())
81 return;
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.
86 if (border) {
87 // Align the first row of pixels inside the border. This is the apparent
88 // top of the dialog.
89 position.set_y(position.y() - border->GetInsets().top());
92 if (widget->is_top_level()) {
93 position +=
94 views::Widget::GetWidgetForNativeView(dialog_host->GetHostView())->
95 GetClientAreaBoundsInScreen().OffsetFromOrigin();
98 widget->SetBounds(gfx::Rect(position, size));
101 } // namespace
103 // static
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.
120 if (border)
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
137 // window.
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);
144 return widget;
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)
165 : nullptr;
166 views::Widget* widget =
167 views::DialogDelegate::CreateDialogWidget(dialog, NULL, parent_view);
168 if (!dialog->UseNewStyleForThisDialog())
169 return widget;
170 ModalDialogHost* host = constrained_window_views_client->
171 GetModalDialogHost(parent);
172 if (host) {
173 DCHECK_EQ(parent_view, host->GetHostView());
174 ModalDialogHostObserver* dialog_host_observer =
175 new WidgetModalDialogHostObserverViews(
176 host, widget, kWidgetModalDialogHostObserverViewsKey);
177 dialog_host_observer->OnPositionRequiresUpdate();
179 return widget;
182 } // namespace constrained window