| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/constrained_window_views.h" | 5 #include "components/constrained_window/constrained_window_views.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #if defined(OS_MACOSX) |
| 9 #include <utility> |
| 10 #endif |
| 8 | 11 |
| 9 #include "base/macros.h" | 12 #include "base/macros.h" |
| 10 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 11 #include "components/constrained_window/constrained_window_views_client.h" | 14 #include "components/constrained_window/constrained_window_views_client.h" |
| 12 #include "components/guest_view/browser/guest_view_base.h" | 15 #include "components/guest_view/browser/guest_view_base.h" |
| 13 #include "components/web_modal/web_contents_modal_dialog_host.h" | 16 #include "components/web_modal/web_contents_modal_dialog_host.h" |
| 14 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 17 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 15 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" | 18 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" |
| 16 #include "ui/views/border.h" | 19 #include "ui/views/border.h" |
| 17 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 18 #include "ui/views/widget/widget_observer.h" | 21 #include "ui/views/widget/widget_observer.h" |
| 19 #include "ui/views/window/dialog_delegate.h" | 22 #include "ui/views/window/dialog_delegate.h" |
| 20 | 23 |
| 24 #if defined(USE_AURA) |
| 25 #include "ui/aura/window.h" |
| 26 #include "ui/wm/core/window_modality_controller.h" |
| 27 #endif |
| 28 |
| 21 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
| 22 #import "components/constrained_window/native_web_contents_modal_dialog_manager_
views_mac.h" | 30 #import "components/constrained_window/native_web_contents_modal_dialog_manager_
views_mac.h" |
| 23 #endif | 31 #endif |
| 24 | 32 |
| 25 using web_modal::ModalDialogHost; | 33 using web_modal::ModalDialogHost; |
| 26 using web_modal::ModalDialogHostObserver; | 34 using web_modal::ModalDialogHostObserver; |
| 27 | 35 |
| 28 namespace constrained_window { | 36 namespace constrained_window { |
| 29 namespace { | 37 namespace { |
| 30 | 38 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 // Border may be null during widget initialization. | 112 // Border may be null during widget initialization. |
| 105 if (border) { | 113 if (border) { |
| 106 // Align the first row of pixels inside the border. This is the apparent | 114 // Align the first row of pixels inside the border. This is the apparent |
| 107 // top of the dialog. | 115 // top of the dialog. |
| 108 position.set_y(position.y() - border->GetInsets().top()); | 116 position.set_y(position.y() - border->GetInsets().top()); |
| 109 } | 117 } |
| 110 | 118 |
| 111 if (widget->is_top_level()) | 119 if (widget->is_top_level()) |
| 112 position += host_widget->GetClientAreaBoundsInScreen().OffsetFromOrigin(); | 120 position += host_widget->GetClientAreaBoundsInScreen().OffsetFromOrigin(); |
| 113 | 121 |
| 122 #if defined(USE_AURA) |
| 123 // Prevents widgets that extend outside the parent window from having their |
| 124 // horizontal position centered on the center of the parent window. Instead, |
| 125 // positions top left corner in parent top left corner so widget extends |
| 126 // below and to the right of the parent window. |
| 127 if (widget->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)) { |
| 128 gfx::Rect bound = dialog_host->GetHostView()->GetBoundsInScreen(); |
| 129 if (bound.width() - border->GetInsets().left() < size.width()) { |
| 130 position.set_x(position.x() + (size.width() - bound.width()) / 2 + |
| 131 border->GetInsets().left()); |
| 132 } |
| 133 } |
| 134 #endif |
| 114 widget->SetBounds(gfx::Rect(position, size)); | 135 widget->SetBounds(gfx::Rect(position, size)); |
| 115 } | 136 } |
| 116 | 137 |
| 117 } // namespace | 138 } // namespace |
| 118 | 139 |
| 119 // static | 140 // static |
| 120 void SetConstrainedWindowViewsClient( | 141 void SetConstrainedWindowViewsClient( |
| 121 std::unique_ptr<ConstrainedWindowViewsClient> new_client) { | 142 std::unique_ptr<ConstrainedWindowViewsClient> new_client) { |
| 122 delete constrained_window_views_client; | 143 delete constrained_window_views_client; |
| 123 constrained_window_views_client = new_client.release(); | 144 constrained_window_views_client = new_client.release(); |
| 124 } | 145 } |
| 125 | 146 |
| 126 void UpdateWebContentsModalDialogPosition( | 147 void UpdateWebContentsModalDialogPosition( |
| 127 views::Widget* widget, | 148 views::Widget* widget, |
| 128 web_modal::WebContentsModalDialogHost* dialog_host) { | 149 web_modal::WebContentsModalDialogHost* dialog_host) { |
| 129 gfx::Size size = widget->GetRootView()->GetPreferredSize(); | 150 gfx::Size size = widget->GetRootView()->GetPreferredSize(); |
| 130 gfx::Size max_size = dialog_host->GetMaximumDialogSize(); | 151 gfx::Size max_size = dialog_host->GetMaximumDialogSize(); |
| 131 // Enlarge the max size by the top border, as the dialog will be shifted | 152 // Enlarge the max size by the top border, as the dialog will be shifted |
| 132 // outside the area specified by the dialog host by this amount later. | 153 // outside the area specified by the dialog host by this amount later. |
| 133 views::Border* border = | 154 views::Border* border = |
| 134 widget->non_client_view()->frame_view()->border(); | 155 widget->non_client_view()->frame_view()->border(); |
| 135 // Border may be null during widget initialization. | 156 // Border may be null during widget initialization. |
| 136 if (border) | 157 if (border) |
| 137 max_size.Enlarge(0, border->GetInsets().top()); | 158 max_size.Enlarge(0, border->GetInsets().top()); |
| 138 size.SetToMin(max_size); | 159 #if defined(USE_AURA) |
| 160 bool has_independent_bounds = |
| 161 !!(widget->GetNativeWindowProperty(wm::kAllowTransientParentEventsKey)); |
| 162 #else |
| 163 bool has_independent_bounds = false; |
| 164 #endif |
| 165 if (!has_independent_bounds) |
| 166 size.SetToMin(max_size); |
| 139 UpdateModalDialogPosition(widget, dialog_host, size); | 167 UpdateModalDialogPosition(widget, dialog_host, size); |
| 140 } | 168 } |
| 141 | 169 |
| 142 void UpdateWidgetModalDialogPosition(views::Widget* widget, | 170 void UpdateWidgetModalDialogPosition(views::Widget* widget, |
| 143 web_modal::ModalDialogHost* dialog_host) { | 171 web_modal::ModalDialogHost* dialog_host) { |
| 144 UpdateModalDialogPosition(widget, dialog_host, | 172 UpdateModalDialogPosition(widget, dialog_host, |
| 145 widget->GetRootView()->GetPreferredSize()); | 173 widget->GetRootView()->GetPreferredSize()); |
| 146 } | 174 } |
| 147 | 175 |
| 148 views::Widget* ShowWebModalDialogViews( | 176 views::Widget* ShowWebModalDialogViews( |
| (...skipping 23 matching lines...) Expand all Loading... |
| 172 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents); | 200 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents); |
| 173 std::unique_ptr<web_modal::SingleWebContentsDialogManager> dialog_manager( | 201 std::unique_ptr<web_modal::SingleWebContentsDialogManager> dialog_manager( |
| 174 new NativeWebContentsModalDialogManagerViewsMac(widget->GetNativeWindow(), | 202 new NativeWebContentsModalDialogManagerViewsMac(widget->GetNativeWindow(), |
| 175 manager)); | 203 manager)); |
| 176 manager->ShowDialogWithManager(widget->GetNativeWindow(), | 204 manager->ShowDialogWithManager(widget->GetNativeWindow(), |
| 177 std::move(dialog_manager)); | 205 std::move(dialog_manager)); |
| 178 return widget; | 206 return widget; |
| 179 } | 207 } |
| 180 #endif | 208 #endif |
| 181 | 209 |
| 210 views::Widget* ShowTopLevelWebModalDialogViews( |
| 211 views::WidgetDelegate* dialog, |
| 212 content::WebContents* initiator_web_contents, |
| 213 ui::AcceleratorTarget* target) { |
| 214 DCHECK(constrained_window_views_client); |
| 215 // For embedded WebContents, use the embedder's WebContents for constrained |
| 216 // window. |
| 217 content::WebContents* web_contents = |
| 218 guest_view::GuestViewBase::GetTopLevelWebContents(initiator_web_contents); |
| 219 gfx::NativeWindow parent = web_contents->GetTopLevelNativeWindow(); |
| 220 gfx::NativeView parent_view = |
| 221 parent ? constrained_window_views_client->GetDialogHostView(parent) |
| 222 : nullptr; |
| 223 views::Widget* widget = |
| 224 views::DialogDelegate::CreateDialogWidget(dialog, nullptr, parent_view); |
| 225 ShowTopLevelModalDialog(widget->GetNativeWindow(), web_contents, target); |
| 226 return widget; |
| 227 } |
| 228 |
| 182 views::Widget* CreateWebModalDialogViews(views::WidgetDelegate* dialog, | 229 views::Widget* CreateWebModalDialogViews(views::WidgetDelegate* dialog, |
| 183 content::WebContents* web_contents) { | 230 content::WebContents* web_contents) { |
| 184 DCHECK_EQ(ui::MODAL_TYPE_CHILD, dialog->GetModalType()); | 231 DCHECK_EQ(ui::MODAL_TYPE_CHILD, dialog->GetModalType()); |
| 185 return views::DialogDelegate::CreateDialogWidget( | 232 return views::DialogDelegate::CreateDialogWidget( |
| 186 dialog, nullptr, | 233 dialog, nullptr, |
| 187 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents) | 234 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents) |
| 188 ->delegate() | 235 ->delegate() |
| 189 ->GetWebContentsModalDialogHost() | 236 ->GetWebContentsModalDialogHost() |
| 190 ->GetHostView()); | 237 ->GetHostView()); |
| 191 } | 238 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 218 if (host) { | 265 if (host) { |
| 219 DCHECK_EQ(parent_view, host->GetHostView()); | 266 DCHECK_EQ(parent_view, host->GetHostView()); |
| 220 ModalDialogHostObserver* dialog_host_observer = | 267 ModalDialogHostObserver* dialog_host_observer = |
| 221 new WidgetModalDialogHostObserverViews( | 268 new WidgetModalDialogHostObserverViews( |
| 222 host, widget, kWidgetModalDialogHostObserverViewsKey); | 269 host, widget, kWidgetModalDialogHostObserverViewsKey); |
| 223 dialog_host_observer->OnPositionRequiresUpdate(); | 270 dialog_host_observer->OnPositionRequiresUpdate(); |
| 224 } | 271 } |
| 225 return widget; | 272 return widget; |
| 226 } | 273 } |
| 227 | 274 |
| 228 } // namespace constrained window | 275 } // namespace constrained_window |
| OLD | NEW |