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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 } | 68 } |
69 CGFloat scale; | 69 CGFloat scale; |
70 if ([screen respondsToSelector:@selector(backingScaleFactor)]) | 70 if ([screen respondsToSelector:@selector(backingScaleFactor)]) |
71 scale = [screen backingScaleFactor]; | 71 scale = [screen backingScaleFactor]; |
72 else | 72 else |
73 scale = [screen userSpaceScaleFactor]; | 73 scale = [screen userSpaceScaleFactor]; |
74 display.set_device_scale_factor(scale); | 74 display.set_device_scale_factor(scale); |
75 return display; | 75 return display; |
76 } | 76 } |
77 | 77 |
| 78 class ScreenMac : public gfx::Screen { |
| 79 public: |
| 80 ScreenMac() {} |
| 81 |
| 82 virtual bool IsDIPEnabled() OVERRIDE { |
| 83 return true; |
| 84 } |
| 85 |
| 86 virtual gfx::Point GetCursorScreenPoint() OVERRIDE { |
| 87 NSPoint mouseLocation = [NSEvent mouseLocation]; |
| 88 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. |
| 89 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 90 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; |
| 91 return gfx::Point(mouseLocation.x, mouseLocation.y); |
| 92 } |
| 93 |
| 94 virtual gfx::NativeWindow GetWindowAtCursorScreenPoint() OVERRIDE { |
| 95 NOTIMPLEMENTED(); |
| 96 return gfx::NativeWindow(); |
| 97 } |
| 98 |
| 99 virtual int GetNumDisplays() OVERRIDE { |
| 100 // Don't just return the number of online displays. It includes displays |
| 101 // that mirror other displays, which are not desired in the count. It's |
| 102 // tempting to use the count returned by CGGetActiveDisplayList, but active |
| 103 // displays exclude sleeping displays, and those are desired in the count. |
| 104 |
| 105 // It would be ridiculous to have this many displays connected, but |
| 106 // CGDirectDisplayID is just an integer, so supporting up to this many |
| 107 // doesn't hurt. |
| 108 CGDirectDisplayID online_displays[128]; |
| 109 CGDisplayCount online_display_count = 0; |
| 110 if (CGGetOnlineDisplayList(arraysize(online_displays), |
| 111 online_displays, |
| 112 &online_display_count) != kCGErrorSuccess) { |
| 113 // 1 is a reasonable assumption. |
| 114 return 1; |
| 115 } |
| 116 |
| 117 int display_count = 0; |
| 118 for (CGDisplayCount online_display_index = 0; |
| 119 online_display_index < online_display_count; |
| 120 ++online_display_index) { |
| 121 CGDirectDisplayID online_display = online_displays[online_display_index]; |
| 122 if (CGDisplayMirrorsDisplay(online_display) == kCGNullDirectDisplay) { |
| 123 // If this display doesn't mirror any other, include it in the count. |
| 124 // The primary display in a mirrored set will be counted, but those that |
| 125 // mirror it will not be. |
| 126 ++display_count; |
| 127 } |
| 128 } |
| 129 |
| 130 return display_count; |
| 131 } |
| 132 |
| 133 virtual gfx::Display GetDisplayNearestWindow( |
| 134 gfx::NativeView view) const OVERRIDE { |
| 135 NSWindow* window = [view window]; |
| 136 if (!window) |
| 137 return GetPrimaryDisplay(); |
| 138 NSScreen* match_screen = [window screen]; |
| 139 return GetDisplayForScreen(match_screen, false /* may not be primary */); |
| 140 } |
| 141 |
| 142 virtual gfx::Display GetDisplayNearestPoint( |
| 143 const gfx::Point& point) const OVERRIDE { |
| 144 NSPoint ns_point = NSPointFromCGPoint(point.ToCGPoint()); |
| 145 |
| 146 NSArray* screens = [NSScreen screens]; |
| 147 NSScreen* primary = [screens objectAtIndex:0]; |
| 148 for (NSScreen* screen in screens) { |
| 149 if (NSMouseInRect(ns_point, [screen frame], NO)) |
| 150 return GetDisplayForScreen(screen, screen == primary); |
| 151 } |
| 152 return GetPrimaryDisplay(); |
| 153 } |
| 154 |
| 155 // Returns the display that most closely intersects the provided bounds. |
| 156 virtual gfx::Display GetDisplayMatching( |
| 157 const gfx::Rect& match_rect) const OVERRIDE { |
| 158 NSScreen* match_screen = GetMatchingScreen(match_rect); |
| 159 return GetDisplayForScreen(match_screen, false /* may not be primary */); |
| 160 } |
| 161 |
| 162 // Returns the primary display. |
| 163 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE { |
| 164 // Primary display is defined as the display with the menubar, |
| 165 // which is always at index 0. |
| 166 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; |
| 167 gfx::Display display = GetDisplayForScreen(primary, true /* primary */); |
| 168 return display; |
| 169 } |
| 170 |
| 171 private: |
| 172 DISALLOW_COPY_AND_ASSIGN(ScreenMac); |
| 173 }; |
| 174 |
78 } // namespace | 175 } // namespace |
79 | 176 |
80 namespace gfx { | 177 namespace gfx { |
81 | 178 |
82 // static | 179 Screen* CreateNativeScreen() { |
83 bool Screen::IsDIPEnabled() { | 180 return new ScreenMac; |
84 return true; | |
85 } | 181 } |
86 | 182 |
87 // static | |
88 gfx::Point Screen::GetCursorScreenPoint() { | |
89 NSPoint mouseLocation = [NSEvent mouseLocation]; | |
90 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. | |
91 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | |
92 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; | |
93 return gfx::Point(mouseLocation.x, mouseLocation.y); | |
94 } | 183 } |
95 | |
96 // static | |
97 gfx::Display Screen::GetDisplayNearestWindow(gfx::NativeView view) { | |
98 NSWindow* window = [view window]; | |
99 if (!window) | |
100 return GetPrimaryDisplay(); | |
101 NSScreen* match_screen = [window screen]; | |
102 return GetDisplayForScreen(match_screen, false /* may not be primary */); | |
103 } | |
104 | |
105 // static | |
106 gfx::Display Screen::GetDisplayMatching(const gfx::Rect& match_rect) { | |
107 NSScreen* match_screen = GetMatchingScreen(match_rect); | |
108 return GetDisplayForScreen(match_screen, false /* may not be primary */); | |
109 } | |
110 | |
111 // static | |
112 gfx::Display Screen::GetPrimaryDisplay() { | |
113 // Primary display is defined as the display with the menubar, | |
114 // which is always at index 0. | |
115 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; | |
116 gfx::Display display = GetDisplayForScreen(primary, true /* primary */); | |
117 return display; | |
118 } | |
119 | |
120 // static | |
121 int Screen::GetNumDisplays() { | |
122 // Don't just return the number of online displays. It includes displays | |
123 // that mirror other displays, which are not desired in the count. It's | |
124 // tempting to use the count returned by CGGetActiveDisplayList, but active | |
125 // displays exclude sleeping displays, and those are desired in the count. | |
126 | |
127 // It would be ridiculous to have this many displays connected, but | |
128 // CGDirectDisplayID is just an integer, so supporting up to this many | |
129 // doesn't hurt. | |
130 CGDirectDisplayID online_displays[128]; | |
131 CGDisplayCount online_display_count = 0; | |
132 if (CGGetOnlineDisplayList(arraysize(online_displays), | |
133 online_displays, | |
134 &online_display_count) != kCGErrorSuccess) { | |
135 // 1 is a reasonable assumption. | |
136 return 1; | |
137 } | |
138 | |
139 int display_count = 0; | |
140 for (CGDisplayCount online_display_index = 0; | |
141 online_display_index < online_display_count; | |
142 ++online_display_index) { | |
143 CGDirectDisplayID online_display = online_displays[online_display_index]; | |
144 if (CGDisplayMirrorsDisplay(online_display) == kCGNullDirectDisplay) { | |
145 // If this display doesn't mirror any other, include it in the count. | |
146 // The primary display in a mirrored set will be counted, but those that | |
147 // mirror it will not be. | |
148 ++display_count; | |
149 } | |
150 } | |
151 | |
152 return display_count; | |
153 } | |
154 | |
155 // static | |
156 gfx::Display Screen::GetDisplayNearestPoint(const gfx::Point& point) { | |
157 NSPoint ns_point = NSPointFromCGPoint(point.ToCGPoint()); | |
158 | |
159 NSArray* screens = [NSScreen screens]; | |
160 NSScreen* primary = [screens objectAtIndex:0]; | |
161 for (NSScreen* screen in screens) { | |
162 if (NSMouseInRect(ns_point, [screen frame], NO)) | |
163 return GetDisplayForScreen(screen, screen == primary); | |
164 } | |
165 return GetPrimaryDisplay(); | |
166 } | |
167 | |
168 } // namespace gfx | |
OLD | NEW |