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/display/display.h" | |
| 17 #include "ui/display/display_finder.h" | |
| 18 #include "ui/display/screen.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 |
| 21 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
| 22 #import "components/constrained_window/native_web_contents_modal_dialog_manager_ views_mac.h" | 25 #import "components/constrained_window/native_web_contents_modal_dialog_manager_ views_mac.h" |
| 23 #endif | 26 #endif |
| 24 | 27 |
| 25 using web_modal::ModalDialogHost; | 28 using web_modal::ModalDialogHost; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 } | 76 } |
| 74 | 77 |
| 75 private: | 78 private: |
| 76 ModalDialogHost* host_; | 79 ModalDialogHost* host_; |
| 77 views::Widget* target_widget_; | 80 views::Widget* target_widget_; |
| 78 const char* const native_window_property_; | 81 const char* const native_window_property_; |
| 79 | 82 |
| 80 DISALLOW_COPY_AND_ASSIGN(WidgetModalDialogHostObserverViews); | 83 DISALLOW_COPY_AND_ASSIGN(WidgetModalDialogHostObserverViews); |
| 81 }; | 84 }; |
| 82 | 85 |
| 86 gfx::Point MoveRectToNearestHostDisplay(const gfx::Rect& screen_rect, | |
|
msw
2016/12/14 22:37:08
Inline this function or rename it, since it doesn'
| |
| 87 const gfx::Rect& host_rect) { | |
| 88 gfx::Rect display_rect = screen_rect; | |
| 89 const display::Display* display = display::FindDisplayNearestPoint( | |
|
msw
2016/12/14 22:37:08
Use Screen::GetDisplayNearestWindow(dialog_host->G
| |
| 90 display::Screen::GetScreen()->GetAllDisplays(), host_rect.CenterPoint()); | |
| 91 DCHECK(display); | |
|
msw
2016/12/14 22:37:08
optional nit: DCHECK immediately before deference
| |
| 92 const gfx::Rect work_area = display->work_area(); | |
| 93 if (!work_area.Contains(display_rect)) | |
| 94 display_rect.AdjustToFit(work_area); | |
| 95 return display_rect.origin(); | |
| 96 } | |
| 97 | |
| 83 void UpdateModalDialogPosition(views::Widget* widget, | 98 void UpdateModalDialogPosition(views::Widget* widget, |
| 84 web_modal::ModalDialogHost* dialog_host, | 99 web_modal::ModalDialogHost* dialog_host, |
| 85 const gfx::Size& size) { | 100 const gfx::Size& size) { |
| 86 // Do not forcibly update the dialog widget position if it is being dragged. | 101 // Do not forcibly update the dialog widget position if it is being dragged. |
| 87 if (widget->HasCapture()) | 102 if (widget->HasCapture()) |
| 88 return; | 103 return; |
| 89 | 104 |
| 90 views::Widget* host_widget = | 105 views::Widget* host_widget = |
| 91 views::Widget::GetWidgetForNativeView(dialog_host->GetHostView()); | 106 views::Widget::GetWidgetForNativeView(dialog_host->GetHostView()); |
| 92 | 107 |
| 93 // If the host view is not backed by a Views::Widget, just update the widget | 108 // If the host view is not backed by a Views::Widget, just update the widget |
| 94 // size. This can happen on MacViews under the Cocoa browser where the window | 109 // size. This can happen on MacViews under the Cocoa browser where the window |
| 95 // modal dialogs are displayed as sheets, and their position is managed by a | 110 // modal dialogs are displayed as sheets, and their position is managed by a |
| 96 // ConstrainedWindowSheetController instance. | 111 // ConstrainedWindowSheetController instance. |
| 97 if (!host_widget) { | 112 if (!host_widget) { |
| 98 widget->SetSize(size); | 113 widget->SetSize(size); |
| 99 return; | 114 return; |
| 100 } | 115 } |
| 101 | 116 |
| 102 gfx::Point position = dialog_host->GetDialogPosition(size); | 117 gfx::Point position = dialog_host->GetDialogPosition(size); |
| 103 views::Border* border = widget->non_client_view()->frame_view()->border(); | 118 views::Border* border = widget->non_client_view()->frame_view()->border(); |
| 104 // Border may be null during widget initialization. | 119 // Border may be null during widget initialization. |
| 105 if (border) { | 120 if (border) { |
| 106 // Align the first row of pixels inside the border. This is the apparent | 121 // Align the first row of pixels inside the border. This is the apparent |
| 107 // top of the dialog. | 122 // top of the dialog. |
| 108 position.set_y(position.y() - border->GetInsets().top()); | 123 position.set_y(position.y() - border->GetInsets().top()); |
| 109 } | 124 } |
| 110 | 125 |
| 111 if (widget->is_top_level()) | 126 if (widget->is_top_level()) { |
| 112 position += host_widget->GetClientAreaBoundsInScreen().OffsetFromOrigin(); | 127 position += host_widget->GetClientAreaBoundsInScreen().OffsetFromOrigin(); |
| 128 // If the dialog extends partially off any display, clamp its position to | |
|
msw
2016/12/14 22:37:08
This should be a function comment on the helper (u
| |
| 129 // be fully visible within that display. If the dialog doesn't intersect | |
| 130 // with any display clamp its position to be fully on the nearest display. | |
| 131 position = MoveRectToNearestHostDisplay( | |
| 132 gfx::Rect(position, size), host_widget->GetWindowBoundsInScreen()); | |
| 133 } | |
| 113 | 134 |
| 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; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 DCHECK_EQ(parent_view, host->GetHostView()); | 247 DCHECK_EQ(parent_view, host->GetHostView()); |
| 227 ModalDialogHostObserver* dialog_host_observer = | 248 ModalDialogHostObserver* dialog_host_observer = |
| 228 new WidgetModalDialogHostObserverViews( | 249 new WidgetModalDialogHostObserverViews( |
| 229 host, widget, kWidgetModalDialogHostObserverViewsKey); | 250 host, widget, kWidgetModalDialogHostObserverViewsKey); |
| 230 dialog_host_observer->OnPositionRequiresUpdate(); | 251 dialog_host_observer->OnPositionRequiresUpdate(); |
| 231 } | 252 } |
| 232 return widget; | 253 return widget; |
| 233 } | 254 } |
| 234 | 255 |
| 235 } // namespace constrained window | 256 } // namespace constrained window |
| OLD | NEW |