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