OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "ash/display/screen_ash.h" | 5 #include "ash/display/screen_ash.h" |
6 | 6 |
7 #include "ash/display/display_manager.h" | 7 #include "ash/display/display_manager.h" |
8 #include "ash/display/window_tree_host_manager.h" | 8 #include "ash/display/window_tree_host_manager.h" |
9 #include "ash/root_window_controller.h" | 9 #include "ash/root_window_controller.h" |
10 #include "ash/root_window_settings.h" | 10 #include "ash/root_window_settings.h" |
11 #include "ash/shelf/shelf_layout_manager.h" | 11 #include "ash/shelf/shelf_layout_manager.h" |
12 #include "ash/shelf/shelf_widget.h" | 12 #include "ash/shelf/shelf_widget.h" |
13 #include "ash/shell.h" | 13 #include "ash/shell.h" |
14 #include "ash/wm/coordinate_conversion.h" | 14 #include "ash/wm/coordinate_conversion.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "ui/aura/client/screen_position_client.h" | 16 #include "ui/aura/client/screen_position_client.h" |
17 #include "ui/aura/env.h" | 17 #include "ui/aura/env.h" |
18 #include "ui/aura/window_event_dispatcher.h" | 18 #include "ui/aura/window_event_dispatcher.h" |
19 #include "ui/gfx/display.h" | 19 #include "ui/gfx/display.h" |
| 20 #include "ui/gfx/display_finder.h" |
20 #include "ui/gfx/screen.h" | 21 #include "ui/gfx/screen.h" |
21 | 22 |
22 namespace ash { | 23 namespace ash { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 DisplayManager* GetDisplayManager() { | 27 DisplayManager* GetDisplayManager() { |
27 return Shell::GetInstance()->display_manager(); | 28 return Shell::GetInstance()->display_manager(); |
28 } | 29 } |
29 | 30 |
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; | |
41 } | |
42 } | |
43 // There should always be at least one display that is less than INT_MAX away. | |
44 DCHECK(nearest_display); | |
45 return *nearest_display; | |
46 } | |
47 | |
48 const gfx::Display* FindDisplayMatching( | |
49 const std::vector<gfx::Display>& displays, | |
50 const gfx::Rect& match_rect) { | |
51 int max_area = 0; | |
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) { | |
59 max_area = area; | |
60 matching = &display; | |
61 } | |
62 } | |
63 return matching; | |
64 } | |
65 | |
66 class ScreenForShutdown : public gfx::Screen { | 31 class ScreenForShutdown : public gfx::Screen { |
67 public: | 32 public: |
68 explicit ScreenForShutdown(ScreenAsh* screen_ash) | 33 explicit ScreenForShutdown(ScreenAsh* screen_ash) |
69 : display_list_(screen_ash->GetAllDisplays()), | 34 : display_list_(screen_ash->GetAllDisplays()), |
70 primary_display_(screen_ash->GetPrimaryDisplay()) { | 35 primary_display_(screen_ash->GetPrimaryDisplay()) { |
71 } | 36 } |
72 | 37 |
73 // gfx::Screen overrides: | 38 // gfx::Screen overrides: |
74 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); } | 39 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); } |
75 gfx::NativeWindow GetWindowUnderCursor() override { return NULL; } | 40 gfx::NativeWindow GetWindowUnderCursor() override { return NULL; } |
76 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override { | 41 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override { |
77 return NULL; | 42 return NULL; |
78 } | 43 } |
79 int GetNumDisplays() const override { return display_list_.size(); } | 44 int GetNumDisplays() const override { return display_list_.size(); } |
80 std::vector<gfx::Display> GetAllDisplays() const override { | 45 std::vector<gfx::Display> GetAllDisplays() const override { |
81 return display_list_; | 46 return display_list_; |
82 } | 47 } |
83 gfx::Display GetDisplayNearestWindow(gfx::NativeView view) const override { | 48 gfx::Display GetDisplayNearestWindow(gfx::NativeView view) const override { |
84 return primary_display_; | 49 return primary_display_; |
85 } | 50 } |
86 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override { | 51 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override { |
87 return FindDisplayNearestPoint(display_list_, point); | 52 return *gfx::FindDisplayNearestPoint(display_list_, point); |
88 } | 53 } |
89 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override { | 54 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override { |
90 const gfx::Display* matching = | 55 const gfx::Display* matching = |
91 FindDisplayMatching(display_list_, match_rect); | 56 gfx::FindDisplayWithBiggestIntersection(display_list_, match_rect); |
92 // Fallback to the primary display if there is no matching display. | 57 // Fallback to the primary display if there is no matching display. |
93 return matching ? *matching : GetPrimaryDisplay(); | 58 return matching ? *matching : GetPrimaryDisplay(); |
94 } | 59 } |
95 gfx::Display GetPrimaryDisplay() const override { return primary_display_; } | 60 gfx::Display GetPrimaryDisplay() const override { return primary_display_; } |
96 void AddObserver(gfx::DisplayObserver* observer) override { | 61 void AddObserver(gfx::DisplayObserver* observer) override { |
97 NOTREACHED() << "Observer should not be added during shutdown"; | 62 NOTREACHED() << "Observer should not be added during shutdown"; |
98 } | 63 } |
99 void RemoveObserver(gfx::DisplayObserver* observer) override {} | 64 void RemoveObserver(gfx::DisplayObserver* observer) override {} |
100 | 65 |
101 private: | 66 private: |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 145 } |
181 | 146 |
182 gfx::Display ScreenAsh::GetDisplayNearestPoint(const gfx::Point& point) const { | 147 gfx::Display ScreenAsh::GetDisplayNearestPoint(const gfx::Point& point) const { |
183 const gfx::Display& display = | 148 const gfx::Display& display = |
184 GetDisplayManager()->FindDisplayContainingPoint(point); | 149 GetDisplayManager()->FindDisplayContainingPoint(point); |
185 if (display.is_valid()) | 150 if (display.is_valid()) |
186 return display; | 151 return display; |
187 // Fallback to the display that has the shortest Manhattan distance from | 152 // Fallback to the display that has the shortest Manhattan distance from |
188 // the |point|. This is correct in the only areas that matter, namely in the | 153 // the |point|. This is correct in the only areas that matter, namely in the |
189 // corners between the physical screens. | 154 // corners between the physical screens. |
190 return FindDisplayNearestPoint(GetDisplayManager()->active_display_list(), | 155 return *gfx::FindDisplayNearestPoint( |
191 point); | 156 GetDisplayManager()->active_display_list(), point); |
192 } | 157 } |
193 | 158 |
194 gfx::Display ScreenAsh::GetDisplayMatching(const gfx::Rect& match_rect) const { | 159 gfx::Display ScreenAsh::GetDisplayMatching(const gfx::Rect& match_rect) const { |
195 if (match_rect.IsEmpty()) | 160 if (match_rect.IsEmpty()) |
196 return GetDisplayNearestPoint(match_rect.origin()); | 161 return GetDisplayNearestPoint(match_rect.origin()); |
197 const gfx::Display* matching = FindDisplayMatching( | 162 const gfx::Display* matching = gfx::FindDisplayWithBiggestIntersection( |
198 GetDisplayManager()->active_display_list(), match_rect); | 163 GetDisplayManager()->active_display_list(), match_rect); |
199 // Fallback to the primary display if there is no matching display. | 164 // Fallback to the primary display if there is no matching display. |
200 return matching ? *matching : GetPrimaryDisplay(); | 165 return matching ? *matching : GetPrimaryDisplay(); |
201 } | 166 } |
202 | 167 |
203 gfx::Display ScreenAsh::GetPrimaryDisplay() const { | 168 gfx::Display ScreenAsh::GetPrimaryDisplay() const { |
204 return GetDisplayManager()->GetDisplayForId( | 169 return GetDisplayManager()->GetDisplayForId( |
205 WindowTreeHostManager::GetPrimaryDisplayId()); | 170 WindowTreeHostManager::GetPrimaryDisplayId()); |
206 } | 171 } |
207 | 172 |
208 void ScreenAsh::AddObserver(gfx::DisplayObserver* observer) { | 173 void ScreenAsh::AddObserver(gfx::DisplayObserver* observer) { |
209 observers_.AddObserver(observer); | 174 observers_.AddObserver(observer); |
210 } | 175 } |
211 | 176 |
212 void ScreenAsh::RemoveObserver(gfx::DisplayObserver* observer) { | 177 void ScreenAsh::RemoveObserver(gfx::DisplayObserver* observer) { |
213 observers_.RemoveObserver(observer); | 178 observers_.RemoveObserver(observer); |
214 } | 179 } |
215 | 180 |
216 gfx::Screen* ScreenAsh::CloneForShutdown() { | 181 gfx::Screen* ScreenAsh::CloneForShutdown() { |
217 return new ScreenForShutdown(this); | 182 return new ScreenForShutdown(this); |
218 } | 183 } |
219 | 184 |
220 } // namespace ash | 185 } // namespace ash |
OLD | NEW |