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 "ui/gfx/screen.h"
7 #import <ApplicationServices/ApplicationServices.h>
8 #import <Cocoa/Cocoa.h>
10 #include "base/logging.h"
11 #include "base/mac/sdk_forward_declarations.h"
12 #include "ui/gfx/display.h"
16 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) {
17 // Primary monitor is defined as the monitor with the menubar,
18 // which is always at index 0.
19 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0];
20 float primary_screen_height = [primary_screen frame].size.height;
21 gfx::Rect rect(NSRectToCGRect(ns_rect));
22 rect.set_y(primary_screen_height - rect.y() - rect.height());
26 NSScreen* GetMatchingScreen(const gfx::Rect& match_rect) {
27 // Default to the monitor with the current keyboard focus, in case
28 // |match_rect| is not on any screen at all.
29 NSScreen* max_screen = [NSScreen mainScreen];
32 for (NSScreen* screen in [NSScreen screens]) {
33 gfx::Rect monitor_area = ConvertCoordinateSystem([screen frame]);
34 gfx::Rect intersection = gfx::IntersectRects(monitor_area, match_rect);
35 int area = intersection.width() * intersection.height();
36 if (area > max_area) {
45 gfx::Display GetDisplayForScreen(NSScreen* screen) {
46 NSRect frame = [screen frame];
47 // TODO(oshima): Implement ID and Observer.
48 gfx::Display display(0, gfx::Rect(NSRectToCGRect(frame)));
50 NSRect visible_frame = [screen visibleFrame];
51 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
53 // Convert work area's coordinate systems.
54 if ([screen isEqual:primary]) {
55 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame));
56 work_area.set_y(frame.size.height - visible_frame.origin.y -
57 visible_frame.size.height);
58 display.set_work_area(work_area);
60 display.set_bounds(ConvertCoordinateSystem(frame));
61 display.set_work_area(ConvertCoordinateSystem(visible_frame));
64 if ([screen respondsToSelector:@selector(backingScaleFactor)])
65 scale = [screen backingScaleFactor];
67 scale = [screen userSpaceScaleFactor];
68 display.set_device_scale_factor(scale);
72 class ScreenMac : public gfx::Screen {
76 virtual bool IsDIPEnabled() OVERRIDE {
80 virtual gfx::Point GetCursorScreenPoint() OVERRIDE {
81 NSPoint mouseLocation = [NSEvent mouseLocation];
82 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
83 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
84 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y;
85 return gfx::Point(mouseLocation.x, mouseLocation.y);
88 virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
90 return gfx::NativeWindow();
93 virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
96 return gfx::NativeWindow();
99 virtual int GetNumDisplays() const 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.
105 // It would be ridiculous to have this many displays connected, but
106 // CGDirectDisplayID is just an integer, so supporting up to this many
108 CGDirectDisplayID online_displays[128];
109 CGDisplayCount online_display_count = 0;
110 if (CGGetOnlineDisplayList(arraysize(online_displays),
112 &online_display_count) != kCGErrorSuccess) {
113 // 1 is a reasonable assumption.
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.
130 return display_count;
133 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
135 return std::vector<gfx::Display>(1, GetPrimaryDisplay());
138 virtual gfx::Display GetDisplayNearestWindow(
139 gfx::NativeView view) const OVERRIDE {
140 NSWindow* window = [view window];
142 return GetPrimaryDisplay();
143 NSScreen* match_screen = [window screen];
145 return GetPrimaryDisplay();
146 return GetDisplayForScreen(match_screen);
149 virtual gfx::Display GetDisplayNearestPoint(
150 const gfx::Point& point) const OVERRIDE {
151 NSPoint ns_point = NSPointFromCGPoint(point.ToCGPoint());
153 NSArray* screens = [NSScreen screens];
154 NSScreen* primary = [screens objectAtIndex:0];
155 ns_point.y = NSMaxY([primary frame]) - ns_point.y;
156 for (NSScreen* screen in screens) {
157 if (NSMouseInRect(ns_point, [screen frame], NO))
158 return GetDisplayForScreen(screen);
160 return GetPrimaryDisplay();
163 // Returns the display that most closely intersects the provided bounds.
164 virtual gfx::Display GetDisplayMatching(
165 const gfx::Rect& match_rect) const OVERRIDE {
166 NSScreen* match_screen = GetMatchingScreen(match_rect);
167 return GetDisplayForScreen(match_screen);
170 // Returns the primary display.
171 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
172 // Primary display is defined as the display with the menubar,
173 // which is always at index 0.
174 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
175 gfx::Display display = GetDisplayForScreen(primary);
179 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
180 // TODO(oshima): crbug.com/122863.
183 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
184 // TODO(oshima): crbug.com/122863.
188 DISALLOW_COPY_AND_ASSIGN(ScreenMac);
195 Screen* CreateNativeScreen() {
196 return new ScreenMac;