| 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 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "components/constrained_window/constrained_window_views_client.h" | 11 #include "components/constrained_window/constrained_window_views_client.h" |
| 12 #include "components/guest_view/browser/guest_view_base.h" | 12 #include "components/guest_view/browser/guest_view_base.h" |
| 13 #include "components/web_modal/web_contents_modal_dialog_host.h" | 13 #include "components/web_modal/web_contents_modal_dialog_host.h" |
| 14 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 14 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 15 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" | 15 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" |
| 16 #include "ui/views/border.h" | 16 #include "ui/views/border.h" |
| 17 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
| 18 #include "ui/views/widget/widget_observer.h" | 18 #include "ui/views/widget/widget_observer.h" |
| 19 #include "ui/views/window/dialog_delegate.h" | 19 #include "ui/views/window/dialog_delegate.h" |
| 20 | 20 |
| 21 #if defined(USE_AURA) |
| 22 #include "ui/aura/window.h" |
| 23 #include "ui/aura/window_targeter.h" |
| 24 #endif |
| 25 |
| 21 using web_modal::ModalDialogHost; | 26 using web_modal::ModalDialogHost; |
| 22 using web_modal::ModalDialogHostObserver; | 27 using web_modal::ModalDialogHostObserver; |
| 23 | 28 |
| 24 namespace constrained_window { | 29 namespace constrained_window { |
| 25 namespace { | 30 namespace { |
| 26 | 31 |
| 27 ConstrainedWindowViewsClient* constrained_window_views_client = NULL; | 32 ConstrainedWindowViewsClient* constrained_window_views_client = NULL; |
| 28 | 33 |
| 29 // The name of a key to store on the window handle to associate | 34 // The name of a key to store on the window handle to associate |
| 30 // WidgetModalDialogHostObserverViews with the Widget. | 35 // WidgetModalDialogHostObserverViews with the Widget. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 if (border) { | 94 if (border) { |
| 90 // Align the first row of pixels inside the border. This is the apparent | 95 // Align the first row of pixels inside the border. This is the apparent |
| 91 // top of the dialog. | 96 // top of the dialog. |
| 92 position.set_y(position.y() - border->GetInsets().top()); | 97 position.set_y(position.y() - border->GetInsets().top()); |
| 93 } | 98 } |
| 94 | 99 |
| 95 if (widget->is_top_level()) { | 100 if (widget->is_top_level()) { |
| 96 position += | 101 position += |
| 97 views::Widget::GetWidgetForNativeView(dialog_host->GetHostView())-> | 102 views::Widget::GetWidgetForNativeView(dialog_host->GetHostView())-> |
| 98 GetClientAreaBoundsInScreen().OffsetFromOrigin(); | 103 GetClientAreaBoundsInScreen().OffsetFromOrigin(); |
| 104 } else { |
| 105 #if defined(USE_AURA) |
| 106 // Prevents widgets that extend outside the parent window from having their |
| 107 // horizontal position centered on the center of the parent window. Instead, |
| 108 // positions top left corner in parent top left corner so widget extends |
| 109 // below and to the right of the parent window. |
| 110 if (widget->GetNativeWindowProperty(aura::kHasIndependentBoundsKey)) { |
| 111 gfx::Rect bound = dialog_host->GetHostView()->GetBoundsInScreen(); |
| 112 if (bound.width() - border->GetInsets().left() < size.width()) { |
| 113 position.set_x(position.x() + (size.width() - bound.width()) / 2 + |
| 114 border->GetInsets().left()); |
| 115 } |
| 116 } |
| 117 #endif |
| 99 } | 118 } |
| 100 | 119 |
| 101 widget->SetBounds(gfx::Rect(position, size)); | 120 widget->SetBounds(gfx::Rect(position, size)); |
| 102 } | 121 } |
| 103 | 122 |
| 104 } // namespace | 123 } // namespace |
| 105 | 124 |
| 106 // static | 125 // static |
| 107 void SetConstrainedWindowViewsClient( | 126 void SetConstrainedWindowViewsClient( |
| 108 std::unique_ptr<ConstrainedWindowViewsClient> new_client) { | 127 std::unique_ptr<ConstrainedWindowViewsClient> new_client) { |
| 109 delete constrained_window_views_client; | 128 delete constrained_window_views_client; |
| 110 constrained_window_views_client = new_client.release(); | 129 constrained_window_views_client = new_client.release(); |
| 111 } | 130 } |
| 112 | 131 |
| 113 void UpdateWebContentsModalDialogPosition( | 132 void UpdateWebContentsModalDialogPosition( |
| 114 views::Widget* widget, | 133 views::Widget* widget, |
| 115 web_modal::WebContentsModalDialogHost* dialog_host) { | 134 web_modal::WebContentsModalDialogHost* dialog_host) { |
| 116 gfx::Size size = widget->GetRootView()->GetPreferredSize(); | 135 gfx::Size size = widget->GetRootView()->GetPreferredSize(); |
| 117 gfx::Size max_size = dialog_host->GetMaximumDialogSize(); | 136 gfx::Size max_size = dialog_host->GetMaximumDialogSize(); |
| 118 // Enlarge the max size by the top border, as the dialog will be shifted | 137 // Enlarge the max size by the top border, as the dialog will be shifted |
| 119 // outside the area specified by the dialog host by this amount later. | 138 // outside the area specified by the dialog host by this amount later. |
| 120 views::Border* border = | 139 views::Border* border = |
| 121 widget->non_client_view()->frame_view()->border(); | 140 widget->non_client_view()->frame_view()->border(); |
| 122 // Border may be null during widget initialization. | 141 // Border may be null during widget initialization. |
| 123 if (border) | 142 if (border) |
| 124 max_size.Enlarge(0, border->GetInsets().top()); | 143 max_size.Enlarge(0, border->GetInsets().top()); |
| 125 size.SetToMin(max_size); | 144 #if defined(USE_AURA) |
| 145 bool has_independent_bounds = |
| 146 widget->GetNativeWindowProperty(aura::kHasIndependentBoundsKey) != |
| 147 nullptr; |
| 148 #else |
| 149 bool has_independent_bounds = false; |
| 150 #endif |
| 151 if (!has_independent_bounds) |
| 152 size.SetToMin(max_size); |
| 126 UpdateModalDialogPosition(widget, dialog_host, size); | 153 UpdateModalDialogPosition(widget, dialog_host, size); |
| 127 } | 154 } |
| 128 | 155 |
| 129 void UpdateWidgetModalDialogPosition(views::Widget* widget, | 156 void UpdateWidgetModalDialogPosition(views::Widget* widget, |
| 130 web_modal::ModalDialogHost* dialog_host) { | 157 web_modal::ModalDialogHost* dialog_host) { |
| 131 UpdateModalDialogPosition(widget, dialog_host, | 158 UpdateModalDialogPosition(widget, dialog_host, |
| 132 widget->GetRootView()->GetPreferredSize()); | 159 widget->GetRootView()->GetPreferredSize()); |
| 133 } | 160 } |
| 134 | 161 |
| 135 views::Widget* ShowWebModalDialogViews( | 162 views::Widget* ShowWebModalDialogViews( |
| 136 views::WidgetDelegate* dialog, | 163 views::WidgetDelegate* dialog, |
| 137 content::WebContents* initiator_web_contents) { | 164 content::WebContents* initiator_web_contents) { |
| 138 DCHECK(constrained_window_views_client); | 165 DCHECK(constrained_window_views_client); |
| 139 // For embedded WebContents, use the embedder's WebContents for constrained | 166 // For embedded WebContents, use the embedder's WebContents for constrained |
| 140 // window. | 167 // window. |
| 141 content::WebContents* web_contents = | 168 content::WebContents* web_contents = |
| 142 guest_view::GuestViewBase::GetTopLevelWebContents(initiator_web_contents); | 169 guest_view::GuestViewBase::GetTopLevelWebContents(initiator_web_contents); |
| 143 views::Widget* widget = CreateWebModalDialogViews(dialog, web_contents); | 170 views::Widget* widget = CreateWebModalDialogViews(dialog, web_contents); |
| 144 ShowModalDialog(widget->GetNativeWindow(), web_contents); | 171 ShowModalDialog(widget->GetNativeWindow(), web_contents); |
| 145 return widget; | 172 return widget; |
| 146 } | 173 } |
| 147 | 174 |
| 175 views::Widget* ShowNonClippedWebModalDialogViews( |
| 176 views::WidgetDelegate* dialog, |
| 177 content::WebContents* initiator_web_contents) { |
| 178 DCHECK(constrained_window_views_client); |
| 179 // For embedded WebContents, use the embedder's WebContents for constrained |
| 180 // window. |
| 181 content::WebContents* web_contents = |
| 182 guest_view::GuestViewBase::GetTopLevelWebContents(initiator_web_contents); |
| 183 views::Widget* widget = CreateWebModalDialogViews(dialog, web_contents); |
| 184 ShowNonClippedModalDialog(widget->GetNativeWindow(), web_contents); |
| 185 return widget; |
| 186 } |
| 187 |
| 148 views::Widget* CreateWebModalDialogViews(views::WidgetDelegate* dialog, | 188 views::Widget* CreateWebModalDialogViews(views::WidgetDelegate* dialog, |
| 149 content::WebContents* web_contents) { | 189 content::WebContents* web_contents) { |
| 150 DCHECK_EQ(ui::MODAL_TYPE_CHILD, dialog->GetModalType()); | 190 DCHECK_EQ(ui::MODAL_TYPE_CHILD, dialog->GetModalType()); |
| 151 return views::DialogDelegate::CreateDialogWidget( | 191 return views::DialogDelegate::CreateDialogWidget( |
| 152 dialog, nullptr, | 192 dialog, nullptr, |
| 153 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents) | 193 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents) |
| 154 ->delegate() | 194 ->delegate() |
| 155 ->GetWebContentsModalDialogHost() | 195 ->GetWebContentsModalDialogHost() |
| 156 ->GetHostView()); | 196 ->GetHostView()); |
| 157 } | 197 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 184 if (host) { | 224 if (host) { |
| 185 DCHECK_EQ(parent_view, host->GetHostView()); | 225 DCHECK_EQ(parent_view, host->GetHostView()); |
| 186 ModalDialogHostObserver* dialog_host_observer = | 226 ModalDialogHostObserver* dialog_host_observer = |
| 187 new WidgetModalDialogHostObserverViews( | 227 new WidgetModalDialogHostObserverViews( |
| 188 host, widget, kWidgetModalDialogHostObserverViewsKey); | 228 host, widget, kWidgetModalDialogHostObserverViewsKey); |
| 189 dialog_host_observer->OnPositionRequiresUpdate(); | 229 dialog_host_observer->OnPositionRequiresUpdate(); |
| 190 } | 230 } |
| 191 return widget; | 231 return widget; |
| 192 } | 232 } |
| 193 | 233 |
| 194 } // namespace constrained window | 234 } // namespace constrained_window |
| OLD | NEW |