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 "ui/gfx/screen.h" | 5 #include "ui/gfx/screen.h" |
6 | 6 |
7 #import <ApplicationServices/ApplicationServices.h> | 7 #import <ApplicationServices/ApplicationServices.h> |
8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 int area = intersection.width() * intersection.height(); | 35 int area = intersection.width() * intersection.height(); |
36 if (area > max_area) { | 36 if (area > max_area) { |
37 max_area = area; | 37 max_area = area; |
38 max_screen = screen; | 38 max_screen = screen; |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 return max_screen; | 42 return max_screen; |
43 } | 43 } |
44 | 44 |
45 gfx::Display GetDisplayForScreen(NSScreen* screen, bool is_primary) { | 45 gfx::Display GetDisplayForScreen(NSScreen* screen) { |
46 NSRect frame = [screen frame]; | 46 NSRect frame = [screen frame]; |
47 // TODO(oshima): Implement ID and Observer. | 47 // TODO(oshima): Implement ID and Observer. |
48 gfx::Display display(0, gfx::Rect(NSRectToCGRect(frame))); | 48 gfx::Display display(0, gfx::Rect(NSRectToCGRect(frame))); |
49 | 49 |
50 NSRect visible_frame = [screen visibleFrame]; | 50 NSRect visible_frame = [screen visibleFrame]; |
| 51 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; |
51 | 52 |
52 // Convert work area's coordinate systems. | 53 // Convert work area's coordinate systems. |
53 if (is_primary) { | 54 if ([screen isEqual:primary]) { |
54 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame)); | 55 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame)); |
55 work_area.set_y(frame.size.height - visible_frame.origin.y - | 56 work_area.set_y(frame.size.height - visible_frame.origin.y - |
56 visible_frame.size.height); | 57 visible_frame.size.height); |
57 display.set_work_area(work_area); | 58 display.set_work_area(work_area); |
58 } else { | 59 } else { |
59 display.set_bounds(ConvertCoordinateSystem(frame)); | 60 display.set_bounds(ConvertCoordinateSystem(frame)); |
60 display.set_work_area(ConvertCoordinateSystem(visible_frame)); | 61 display.set_work_area(ConvertCoordinateSystem(visible_frame)); |
61 } | 62 } |
62 CGFloat scale; | 63 CGFloat scale; |
63 if ([screen respondsToSelector:@selector(backingScaleFactor)]) | 64 if ([screen respondsToSelector:@selector(backingScaleFactor)]) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 } | 136 } |
136 | 137 |
137 virtual gfx::Display GetDisplayNearestWindow( | 138 virtual gfx::Display GetDisplayNearestWindow( |
138 gfx::NativeView view) const OVERRIDE { | 139 gfx::NativeView view) const OVERRIDE { |
139 NSWindow* window = [view window]; | 140 NSWindow* window = [view window]; |
140 if (!window) | 141 if (!window) |
141 return GetPrimaryDisplay(); | 142 return GetPrimaryDisplay(); |
142 NSScreen* match_screen = [window screen]; | 143 NSScreen* match_screen = [window screen]; |
143 if (!match_screen) | 144 if (!match_screen) |
144 return GetPrimaryDisplay(); | 145 return GetPrimaryDisplay(); |
145 return GetDisplayForScreen(match_screen, false /* may not be primary */); | 146 return GetDisplayForScreen(match_screen); |
146 } | 147 } |
147 | 148 |
148 virtual gfx::Display GetDisplayNearestPoint( | 149 virtual gfx::Display GetDisplayNearestPoint( |
149 const gfx::Point& point) const OVERRIDE { | 150 const gfx::Point& point) const OVERRIDE { |
150 NSPoint ns_point = NSPointFromCGPoint(point.ToCGPoint()); | 151 NSPoint ns_point = NSPointFromCGPoint(point.ToCGPoint()); |
151 | 152 |
152 NSArray* screens = [NSScreen screens]; | 153 NSArray* screens = [NSScreen screens]; |
153 NSScreen* primary = [screens objectAtIndex:0]; | 154 NSScreen* primary = [screens objectAtIndex:0]; |
154 ns_point.y = NSMaxY([primary frame]) - ns_point.y; | 155 ns_point.y = NSMaxY([primary frame]) - ns_point.y; |
155 for (NSScreen* screen in screens) { | 156 for (NSScreen* screen in screens) { |
156 if (NSMouseInRect(ns_point, [screen frame], NO)) | 157 if (NSMouseInRect(ns_point, [screen frame], NO)) |
157 return GetDisplayForScreen(screen, screen == primary); | 158 return GetDisplayForScreen(screen); |
158 } | 159 } |
159 return GetPrimaryDisplay(); | 160 return GetPrimaryDisplay(); |
160 } | 161 } |
161 | 162 |
162 // Returns the display that most closely intersects the provided bounds. | 163 // Returns the display that most closely intersects the provided bounds. |
163 virtual gfx::Display GetDisplayMatching( | 164 virtual gfx::Display GetDisplayMatching( |
164 const gfx::Rect& match_rect) const OVERRIDE { | 165 const gfx::Rect& match_rect) const OVERRIDE { |
165 NSScreen* match_screen = GetMatchingScreen(match_rect); | 166 NSScreen* match_screen = GetMatchingScreen(match_rect); |
166 return GetDisplayForScreen(match_screen, false /* may not be primary */); | 167 return GetDisplayForScreen(match_screen); |
167 } | 168 } |
168 | 169 |
169 // Returns the primary display. | 170 // Returns the primary display. |
170 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE { | 171 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE { |
171 // Primary display is defined as the display with the menubar, | 172 // Primary display is defined as the display with the menubar, |
172 // which is always at index 0. | 173 // which is always at index 0. |
173 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; | 174 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; |
174 gfx::Display display = GetDisplayForScreen(primary, true /* primary */); | 175 gfx::Display display = GetDisplayForScreen(primary); |
175 return display; | 176 return display; |
176 } | 177 } |
177 | 178 |
178 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE { | 179 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE { |
179 // TODO(oshima): crbug.com/122863. | 180 // TODO(oshima): crbug.com/122863. |
180 } | 181 } |
181 | 182 |
182 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE { | 183 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE { |
183 // TODO(oshima): crbug.com/122863. | 184 // TODO(oshima): crbug.com/122863. |
184 } | 185 } |
185 | 186 |
186 private: | 187 private: |
187 DISALLOW_COPY_AND_ASSIGN(ScreenMac); | 188 DISALLOW_COPY_AND_ASSIGN(ScreenMac); |
188 }; | 189 }; |
189 | 190 |
190 } // namespace | 191 } // namespace |
191 | 192 |
192 namespace gfx { | 193 namespace gfx { |
193 | 194 |
194 Screen* CreateNativeScreen() { | 195 Screen* CreateNativeScreen() { |
195 return new ScreenMac; | 196 return new ScreenMac; |
196 } | 197 } |
197 | 198 |
198 } | 199 } |
OLD | NEW |