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/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. | |
| 108 // Instead, positions top left corner in parent top left corner so widget | |
| 109 // extends 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) != NULL); | |
|
Mike Wittman
2016/08/18 19:22:54
nits: no need for the enclosing parens, and compar
rbpotter
2016/08/19 01:59:09
Done.
| |
| 147 #else | |
| 148 bool has_independent_bounds = false; | |
| 149 #endif | |
| 150 if (!has_independent_bounds) { | |
| 151 size.SetToMin(max_size); | |
| 152 } | |
| 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 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents) | 171 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents) |
| 145 ->ShowModalDialog(widget->GetNativeWindow()); | 172 ->ShowModalDialog(widget->GetNativeWindow()); |
| 146 return widget; | 173 return widget; |
| 147 } | 174 } |
| 148 | 175 |
| 176 views::Widget* ShowNonClippedWebModalDialogViews( | |
| 177 views::WidgetDelegate* dialog, | |
| 178 content::WebContents* initiator_web_contents) { | |
| 179 DCHECK(constrained_window_views_client); | |
| 180 // For embedded WebContents, use the embedder's WebContents for constrained | |
| 181 // window. | |
| 182 content::WebContents* web_contents = | |
| 183 guest_view::GuestViewBase::GetTopLevelWebContents(initiator_web_contents); | |
| 184 views::Widget* widget = CreateWebModalDialogViews(dialog, web_contents); | |
| 185 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents) | |
| 186 ->ShowNonClippedModalDialog(widget->GetNativeWindow()); | |
| 187 return widget; | |
| 188 } | |
| 189 | |
| 149 views::Widget* CreateWebModalDialogViews(views::WidgetDelegate* dialog, | 190 views::Widget* CreateWebModalDialogViews(views::WidgetDelegate* dialog, |
| 150 content::WebContents* web_contents) { | 191 content::WebContents* web_contents) { |
| 151 DCHECK_EQ(ui::MODAL_TYPE_CHILD, dialog->GetModalType()); | 192 DCHECK_EQ(ui::MODAL_TYPE_CHILD, dialog->GetModalType()); |
| 152 return views::DialogDelegate::CreateDialogWidget( | 193 return views::DialogDelegate::CreateDialogWidget( |
| 153 dialog, nullptr, | 194 dialog, nullptr, |
| 154 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents) | 195 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents) |
| 155 ->delegate() | 196 ->delegate() |
| 156 ->GetWebContentsModalDialogHost() | 197 ->GetWebContentsModalDialogHost() |
| 157 ->GetHostView()); | 198 ->GetHostView()); |
| 158 } | 199 } |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 185 if (host) { | 226 if (host) { |
| 186 DCHECK_EQ(parent_view, host->GetHostView()); | 227 DCHECK_EQ(parent_view, host->GetHostView()); |
| 187 ModalDialogHostObserver* dialog_host_observer = | 228 ModalDialogHostObserver* dialog_host_observer = |
| 188 new WidgetModalDialogHostObserverViews( | 229 new WidgetModalDialogHostObserverViews( |
| 189 host, widget, kWidgetModalDialogHostObserverViewsKey); | 230 host, widget, kWidgetModalDialogHostObserverViewsKey); |
| 190 dialog_host_observer->OnPositionRequiresUpdate(); | 231 dialog_host_observer->OnPositionRequiresUpdate(); |
| 191 } | 232 } |
| 192 return widget; | 233 return widget; |
| 193 } | 234 } |
| 194 | 235 |
| 195 } // namespace constrained window | 236 } // namespace constrained_window |
| OLD | NEW |