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 virtual bool IsDIPEnabled() OVERRIDE {
77 virtual gfx::Point GetCursorScreenPoint() OVERRIDE {
80 virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
83 virtual gfx::NativeWindow GetWindowAtScreenPoint(
84 const gfx::Point& point) OVERRIDE {
87 virtual int GetNumDisplays() const OVERRIDE {
88 return display_list_.size();
90 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
93 virtual gfx::Display GetDisplayNearestWindow(gfx::NativeView view)
95 return primary_display_;
97 virtual gfx::Display GetDisplayNearestPoint(
98 const gfx::Point& point) const OVERRIDE {
99 return FindDisplayNearestPoint(display_list_, point);
101 virtual gfx::Display GetDisplayMatching(const gfx::Rect& match_rect)
103 const gfx::Display* matching =
104 FindDisplayMatching(display_list_, match_rect);
105 // Fallback to the primary display if there is no matching display.
106 return matching ? *matching : GetPrimaryDisplay();
108 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
109 return primary_display_;
111 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
112 NOTREACHED() << "Observer should not be added during shutdown";
114 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
118 const std::vector<gfx::Display> display_list_;
119 const gfx::Display primary_display_;
121 DISALLOW_COPY_AND_ASSIGN(ScreenForShutdown);
126 ScreenAsh::ScreenAsh() {
129 ScreenAsh::~ScreenAsh() {
133 gfx::Display ScreenAsh::FindDisplayContainingPoint(const gfx::Point& point) {
134 return GetDisplayManager()->FindDisplayContainingPoint(point);
138 gfx::Rect ScreenAsh::GetMaximizedWindowBoundsInParent(aura::Window* window) {
139 if (GetRootWindowController(window->GetRootWindow())->shelf())
140 return GetDisplayWorkAreaBoundsInParent(window);
142 return GetDisplayBoundsInParent(window);
146 gfx::Rect ScreenAsh::GetDisplayBoundsInParent(aura::Window* window) {
147 return ConvertRectFromScreen(
149 Shell::GetScreen()->GetDisplayNearestWindow(window).bounds());
153 gfx::Rect ScreenAsh::GetDisplayWorkAreaBoundsInParent(aura::Window* window) {
154 return ConvertRectFromScreen(
156 Shell::GetScreen()->GetDisplayNearestWindow(window).work_area());
160 gfx::Rect ScreenAsh::ConvertRectToScreen(aura::Window* window,
161 const gfx::Rect& rect) {
162 gfx::Point point = rect.origin();
163 aura::client::GetScreenPositionClient(window->GetRootWindow())->
164 ConvertPointToScreen(window, &point);
165 return gfx::Rect(point, rect.size());
169 gfx::Rect ScreenAsh::ConvertRectFromScreen(aura::Window* window,
170 const gfx::Rect& rect) {
171 gfx::Point point = rect.origin();
172 aura::client::GetScreenPositionClient(window->GetRootWindow())->
173 ConvertPointFromScreen(window, &point);
174 return gfx::Rect(point, rect.size());
178 const gfx::Display& ScreenAsh::GetSecondaryDisplay() {
179 DisplayManager* display_manager = GetDisplayManager();
180 CHECK_EQ(2U, display_manager->GetNumDisplays());
181 return display_manager->GetDisplayAt(0).id() ==
182 Shell::GetScreen()->GetPrimaryDisplay().id() ?
183 display_manager->GetDisplayAt(1) : display_manager->GetDisplayAt(0);
187 const gfx::Display& ScreenAsh::GetDisplayForId(int64 display_id) {
188 return GetDisplayManager()->GetDisplayForId(display_id);
191 void ScreenAsh::NotifyBoundsChanged(const gfx::Display& display) {
192 FOR_EACH_OBSERVER(gfx::DisplayObserver, observers_,
193 OnDisplayBoundsChanged(display));
196 void ScreenAsh::NotifyDisplayAdded(const gfx::Display& display) {
197 FOR_EACH_OBSERVER(gfx::DisplayObserver, observers_, OnDisplayAdded(display));
200 void ScreenAsh::NotifyDisplayRemoved(const gfx::Display& display) {
202 gfx::DisplayObserver, observers_, OnDisplayRemoved(display));
205 bool ScreenAsh::IsDIPEnabled() {
209 gfx::Point ScreenAsh::GetCursorScreenPoint() {
210 return aura::Env::GetInstance()->last_mouse_location();
213 gfx::NativeWindow ScreenAsh::GetWindowUnderCursor() {
214 return GetWindowAtScreenPoint(Shell::GetScreen()->GetCursorScreenPoint());
217 gfx::NativeWindow ScreenAsh::GetWindowAtScreenPoint(const gfx::Point& point) {
218 return wm::GetRootWindowAt(point)->GetTopWindowContainingPoint(point);
221 int ScreenAsh::GetNumDisplays() const {
222 return GetDisplayManager()->GetNumDisplays();
225 std::vector<gfx::Display> ScreenAsh::GetAllDisplays() const {
226 return GetDisplayManager()->displays();
229 gfx::Display ScreenAsh::GetDisplayNearestWindow(gfx::NativeView window) const {
231 return GetPrimaryDisplay();
232 const aura::Window* root_window = window->GetRootWindow();
234 return GetPrimaryDisplay();
235 const RootWindowSettings* rws = GetRootWindowSettings(root_window);
237 return GetPrimaryDisplay();
239 int64 id = rws->display_id;
240 // if id is |kInvaildDisplayID|, it's being deleted.
241 DCHECK(id != gfx::Display::kInvalidDisplayID);
243 DisplayManager* display_manager = GetDisplayManager();
244 // RootWindow needs Display to determine its device scale factor
245 // for non desktop display.
246 if (display_manager->non_desktop_display().id() == id)
247 return display_manager->non_desktop_display();
248 return display_manager->GetDisplayForId(id);
251 gfx::Display ScreenAsh::GetDisplayNearestPoint(const gfx::Point& point) const {
252 const gfx::Display& display =
253 GetDisplayManager()->FindDisplayContainingPoint(point);
254 if (display.is_valid())
256 // Fallback to the display that has the shortest Manhattan distance from
257 // the |point|. This is correct in the only areas that matter, namely in the
258 // corners between the physical screens.
259 return FindDisplayNearestPoint(GetDisplayManager()->displays(), point);
262 gfx::Display ScreenAsh::GetDisplayMatching(const gfx::Rect& match_rect) const {
263 if (match_rect.IsEmpty())
264 return GetDisplayNearestPoint(match_rect.origin());
265 const gfx::Display* matching =
266 FindDisplayMatching(GetDisplayManager()->displays(), match_rect);
267 // Fallback to the primary display if there is no matching display.
268 return matching ? *matching : GetPrimaryDisplay();
271 gfx::Display ScreenAsh::GetPrimaryDisplay() const {
272 return GetDisplayManager()->GetDisplayForId(
273 DisplayController::GetPrimaryDisplayId());
276 void ScreenAsh::AddObserver(gfx::DisplayObserver* observer) {
277 observers_.AddObserver(observer);
280 void ScreenAsh::RemoveObserver(gfx::DisplayObserver* observer) {
281 observers_.RemoveObserver(observer);
284 gfx::Screen* ScreenAsh::CloneForShutdown() {
285 return new ScreenForShutdown(this);