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
3 // found in the LICENSE file.
5 #include "ash/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/shelf/shelf_layout_manager.h"
11 #include "ash/shelf/shelf_widget.h"
12 #include "ash/shell.h"
13 #include "ash/wm/coordinate_conversion.h"
14 #include "base/logging.h"
15 #include "ui/aura/client/screen_position_client.h"
16 #include "ui/aura/env.h"
17 #include "ui/aura/root_window.h"
18 #include "ui/gfx/display.h"
19 #include "ui/gfx/screen.h"
24 internal::DisplayManager
* GetDisplayManager() {
25 return Shell::GetInstance()->display_manager();
28 DisplayController
* GetDisplayController() {
29 return Shell::GetInstance()->display_controller();
33 ScreenAsh::ScreenAsh() {
36 ScreenAsh::~ScreenAsh() {
40 gfx::Display
ScreenAsh::FindDisplayContainingPoint(const gfx::Point
& point
) {
41 return GetDisplayManager()->FindDisplayContainingPoint(point
);
45 gfx::Rect
ScreenAsh::GetMaximizedWindowBoundsInParent(aura::Window
* window
) {
46 if (internal::GetRootWindowController(window
->GetRootWindow())->shelf())
47 return GetDisplayWorkAreaBoundsInParent(window
);
49 return GetDisplayBoundsInParent(window
);
53 gfx::Rect
ScreenAsh::GetDisplayBoundsInParent(aura::Window
* window
) {
54 return ConvertRectFromScreen(
56 Shell::GetScreen()->GetDisplayNearestWindow(window
).bounds());
60 gfx::Rect
ScreenAsh::GetDisplayWorkAreaBoundsInParent(aura::Window
* window
) {
61 return ConvertRectFromScreen(
63 Shell::GetScreen()->GetDisplayNearestWindow(window
).work_area());
67 gfx::Rect
ScreenAsh::ConvertRectToScreen(aura::Window
* window
,
68 const gfx::Rect
& rect
) {
69 gfx::Point point
= rect
.origin();
70 aura::client::GetScreenPositionClient(window
->GetRootWindow())->
71 ConvertPointToScreen(window
, &point
);
72 return gfx::Rect(point
, rect
.size());
76 gfx::Rect
ScreenAsh::ConvertRectFromScreen(aura::Window
* window
,
77 const gfx::Rect
& rect
) {
78 gfx::Point point
= rect
.origin();
79 aura::client::GetScreenPositionClient(window
->GetRootWindow())->
80 ConvertPointFromScreen(window
, &point
);
81 return gfx::Rect(point
, rect
.size());
85 const gfx::Display
& ScreenAsh::GetSecondaryDisplay() {
86 internal::DisplayManager
* display_manager
= GetDisplayManager();
87 CHECK_EQ(2U, display_manager
->GetNumDisplays());
88 return display_manager
->GetDisplayAt(0).id() ==
89 DisplayController::GetPrimaryDisplay().id() ?
90 display_manager
->GetDisplayAt(1) : display_manager
->GetDisplayAt(0);
94 const gfx::Display
& ScreenAsh::GetDisplayForId(int64 display_id
) {
95 return GetDisplayManager()->GetDisplayForId(display_id
);
98 void ScreenAsh::NotifyBoundsChanged(const gfx::Display
& display
) {
99 FOR_EACH_OBSERVER(gfx::DisplayObserver
, observers_
,
100 OnDisplayBoundsChanged(display
));
103 void ScreenAsh::NotifyDisplayAdded(const gfx::Display
& display
) {
104 FOR_EACH_OBSERVER(gfx::DisplayObserver
, observers_
, OnDisplayAdded(display
));
107 void ScreenAsh::NotifyDisplayRemoved(const gfx::Display
& display
) {
109 gfx::DisplayObserver
, observers_
, OnDisplayRemoved(display
));
112 bool ScreenAsh::IsDIPEnabled() {
116 gfx::Point
ScreenAsh::GetCursorScreenPoint() {
117 return aura::Env::GetInstance()->last_mouse_location();
120 gfx::NativeWindow
ScreenAsh::GetWindowUnderCursor() {
121 return GetWindowAtScreenPoint(Shell::GetScreen()->GetCursorScreenPoint());
124 gfx::NativeWindow
ScreenAsh::GetWindowAtScreenPoint(const gfx::Point
& point
) {
125 return wm::GetRootWindowAt(point
)->GetTopWindowContainingPoint(point
);
128 int ScreenAsh::GetNumDisplays() const {
129 return DisplayController::GetNumDisplays();
132 std::vector
<gfx::Display
> ScreenAsh::GetAllDisplays() const {
133 return GetDisplayManager()->displays();
136 gfx::Display
ScreenAsh::GetDisplayNearestWindow(gfx::NativeView window
) const {
137 return GetDisplayController()->GetDisplayNearestWindow(window
);
140 gfx::Display
ScreenAsh::GetDisplayNearestPoint(const gfx::Point
& point
) const {
141 return GetDisplayController()->GetDisplayNearestPoint(point
);
144 gfx::Display
ScreenAsh::GetDisplayMatching(const gfx::Rect
& match_rect
) const {
145 return GetDisplayController()->GetDisplayMatching(match_rect
);
148 gfx::Display
ScreenAsh::GetPrimaryDisplay() const {
149 return DisplayController::GetPrimaryDisplay();
152 void ScreenAsh::AddObserver(gfx::DisplayObserver
* observer
) {
153 observers_
.AddObserver(observer
);
156 void ScreenAsh::RemoveObserver(gfx::DisplayObserver
* observer
) {
157 observers_
.RemoveObserver(observer
);