1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/display/screen_ash.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/root_window_settings.h"
11 #include "ash/shelf/shelf_layout_manager.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "ash/wm/coordinate_conversion.h"
15 #include "base/logging.h"
16 #include "ui/aura/client/screen_position_client.h"
17 #include "ui/aura/env.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/screen.h"
26 DisplayManager
* GetDisplayManager() {
27 return Shell::GetInstance()->display_manager();
30 gfx::Display
FindDisplayNearestPoint(const std::vector
<gfx::Display
>& displays
,
31 const gfx::Point
& point
) {
32 int min_distance
= INT_MAX
;
33 const gfx::Display
* nearest_display
= NULL
;
34 for (std::vector
<gfx::Display
>::const_iterator iter
= displays
.begin();
35 iter
!= displays
.end(); ++iter
) {
36 const gfx::Display
& display
= *iter
;
37 int distance
= display
.bounds().ManhattanDistanceToPoint(point
);
38 if (distance
< min_distance
) {
39 min_distance
= distance
;
40 nearest_display
= &display
;
43 // There should always be at least one display that is less than INT_MAX away.
44 DCHECK(nearest_display
);
45 return *nearest_display
;
48 const gfx::Display
* FindDisplayMatching(
49 const std::vector
<gfx::Display
>& displays
,
50 const gfx::Rect
& match_rect
) {
52 const gfx::Display
* matching
= NULL
;
53 for (std::vector
<gfx::Display
>::const_iterator iter
= displays
.begin();
54 iter
!= displays
.end(); ++iter
) {
55 const gfx::Display
& display
= *iter
;
56 gfx::Rect intersect
= gfx::IntersectRects(display
.bounds(), match_rect
);
57 int area
= intersect
.width() * intersect
.height();
58 if (area
> max_area
) {
66 class ScreenForShutdown
: public gfx::Screen
{
68 explicit ScreenForShutdown(ScreenAsh
* screen_ash
)
69 : display_list_(screen_ash
->GetAllDisplays()),
70 primary_display_(screen_ash
->GetPrimaryDisplay()) {
73 // gfx::Screen overrides:
74 gfx::Point
GetCursorScreenPoint() override
{ return gfx::Point(); }
75 gfx::NativeWindow
GetWindowUnderCursor() override
{ return NULL
; }
76 gfx::NativeWindow
GetWindowAtScreenPoint(const gfx::Point
& point
) override
{
79 int GetNumDisplays() const override
{ return display_list_
.size(); }
80 std::vector
<gfx::Display
> GetAllDisplays() const override
{
83 gfx::Display
GetDisplayNearestWindow(gfx::NativeView view
) const override
{
84 return primary_display_
;
86 gfx::Display
GetDisplayNearestPoint(const gfx::Point
& point
) const override
{
87 return FindDisplayNearestPoint(display_list_
, point
);
89 gfx::Display
GetDisplayMatching(const gfx::Rect
& match_rect
) const override
{
90 const gfx::Display
* matching
=
91 FindDisplayMatching(display_list_
, match_rect
);
92 // Fallback to the primary display if there is no matching display.
93 return matching
? *matching
: GetPrimaryDisplay();
95 gfx::Display
GetPrimaryDisplay() const override
{ return primary_display_
; }
96 void AddObserver(gfx::DisplayObserver
* observer
) override
{
97 NOTREACHED() << "Observer should not be added during shutdown";
99 void RemoveObserver(gfx::DisplayObserver
* observer
) override
{}
102 const std::vector
<gfx::Display
> display_list_
;
103 const gfx::Display primary_display_
;
105 DISALLOW_COPY_AND_ASSIGN(ScreenForShutdown
);
110 ScreenAsh::ScreenAsh() {
113 ScreenAsh::~ScreenAsh() {
117 gfx::Display
ScreenAsh::FindDisplayContainingPoint(const gfx::Point
& point
) {
118 return GetDisplayManager()->FindDisplayContainingPoint(point
);
122 gfx::Rect
ScreenAsh::GetMaximizedWindowBoundsInParent(aura::Window
* window
) {
123 if (GetRootWindowController(window
->GetRootWindow())->shelf())
124 return GetDisplayWorkAreaBoundsInParent(window
);
126 return GetDisplayBoundsInParent(window
);
130 gfx::Rect
ScreenAsh::GetDisplayBoundsInParent(aura::Window
* window
) {
131 return ConvertRectFromScreen(
133 Shell::GetScreen()->GetDisplayNearestWindow(window
).bounds());
137 gfx::Rect
ScreenAsh::GetDisplayWorkAreaBoundsInParent(aura::Window
* window
) {
138 return ConvertRectFromScreen(
140 Shell::GetScreen()->GetDisplayNearestWindow(window
).work_area());
144 gfx::Rect
ScreenAsh::ConvertRectToScreen(aura::Window
* window
,
145 const gfx::Rect
& rect
) {
146 gfx::Point point
= rect
.origin();
147 aura::client::GetScreenPositionClient(window
->GetRootWindow())->
148 ConvertPointToScreen(window
, &point
);
149 return gfx::Rect(point
, rect
.size());
153 gfx::Rect
ScreenAsh::ConvertRectFromScreen(aura::Window
* window
,
154 const gfx::Rect
& rect
) {
155 gfx::Point point
= rect
.origin();
156 aura::client::GetScreenPositionClient(window
->GetRootWindow())->
157 ConvertPointFromScreen(window
, &point
);
158 return gfx::Rect(point
, rect
.size());
162 const gfx::Display
& ScreenAsh::GetSecondaryDisplay() {
163 DisplayManager
* display_manager
= GetDisplayManager();
164 CHECK_EQ(2U, display_manager
->GetNumDisplays());
165 return display_manager
->GetDisplayAt(0).id() ==
166 Shell::GetScreen()->GetPrimaryDisplay().id() ?
167 display_manager
->GetDisplayAt(1) : display_manager
->GetDisplayAt(0);
171 const gfx::Display
& ScreenAsh::GetDisplayForId(int64 display_id
) {
172 return GetDisplayManager()->GetDisplayForId(display_id
);
175 void ScreenAsh::NotifyMetricsChanged(const gfx::Display
& display
,
177 FOR_EACH_OBSERVER(gfx::DisplayObserver
,
179 OnDisplayMetricsChanged(display
, metrics
));
182 void ScreenAsh::NotifyDisplayAdded(const gfx::Display
& display
) {
183 FOR_EACH_OBSERVER(gfx::DisplayObserver
, observers_
, OnDisplayAdded(display
));
186 void ScreenAsh::NotifyDisplayRemoved(const gfx::Display
& display
) {
188 gfx::DisplayObserver
, observers_
, OnDisplayRemoved(display
));
191 gfx::Point
ScreenAsh::GetCursorScreenPoint() {
192 return aura::Env::GetInstance()->last_mouse_location();
195 gfx::NativeWindow
ScreenAsh::GetWindowUnderCursor() {
196 return GetWindowAtScreenPoint(Shell::GetScreen()->GetCursorScreenPoint());
199 gfx::NativeWindow
ScreenAsh::GetWindowAtScreenPoint(const gfx::Point
& point
) {
200 return wm::GetRootWindowAt(point
)->GetTopWindowContainingPoint(point
);
203 int ScreenAsh::GetNumDisplays() const {
204 return GetDisplayManager()->GetNumDisplays();
207 std::vector
<gfx::Display
> ScreenAsh::GetAllDisplays() const {
208 return GetDisplayManager()->displays();
211 gfx::Display
ScreenAsh::GetDisplayNearestWindow(gfx::NativeView window
) const {
213 return GetPrimaryDisplay();
214 const aura::Window
* root_window
= window
->GetRootWindow();
216 return GetPrimaryDisplay();
217 const RootWindowSettings
* rws
= GetRootWindowSettings(root_window
);
218 int64 id
= rws
->display_id
;
219 // if id is |kInvaildDisplayID|, it's being deleted.
220 DCHECK(id
!= gfx::Display::kInvalidDisplayID
);
221 if (id
== gfx::Display::kInvalidDisplayID
)
222 return GetPrimaryDisplay();
224 DisplayManager
* display_manager
= GetDisplayManager();
225 // RootWindow needs Display to determine its device scale factor
226 // for non desktop display.
227 if (display_manager
->non_desktop_display().id() == id
)
228 return display_manager
->non_desktop_display();
229 return display_manager
->GetDisplayForId(id
);
232 gfx::Display
ScreenAsh::GetDisplayNearestPoint(const gfx::Point
& point
) const {
233 const gfx::Display
& display
=
234 GetDisplayManager()->FindDisplayContainingPoint(point
);
235 if (display
.is_valid())
237 // Fallback to the display that has the shortest Manhattan distance from
238 // the |point|. This is correct in the only areas that matter, namely in the
239 // corners between the physical screens.
240 return FindDisplayNearestPoint(GetDisplayManager()->displays(), point
);
243 gfx::Display
ScreenAsh::GetDisplayMatching(const gfx::Rect
& match_rect
) const {
244 if (match_rect
.IsEmpty())
245 return GetDisplayNearestPoint(match_rect
.origin());
246 const gfx::Display
* matching
=
247 FindDisplayMatching(GetDisplayManager()->displays(), match_rect
);
248 // Fallback to the primary display if there is no matching display.
249 return matching
? *matching
: GetPrimaryDisplay();
252 gfx::Display
ScreenAsh::GetPrimaryDisplay() const {
253 return GetDisplayManager()->GetDisplayForId(
254 DisplayController::GetPrimaryDisplayId());
257 void ScreenAsh::AddObserver(gfx::DisplayObserver
* observer
) {
258 observers_
.AddObserver(observer
);
261 void ScreenAsh::RemoveObserver(gfx::DisplayObserver
* observer
) {
262 observers_
.RemoveObserver(observer
);
265 gfx::Screen
* ScreenAsh::CloneForShutdown() {
266 return new ScreenForShutdown(this);