Disable flaky test ExtensionActionContextMenuTest.RunInspectPopup
[chromium-blink-merge.git] / ui / gfx / screen_mac.mm
blob15bbf3728139395636176ee4b37f1c4afbf7baaa
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 "ui/gfx/display.h"
13 #if !defined(MAC_OS_X_VERSION_10_7) || \
14     MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
16 @interface NSScreen (LionAPI)
17 - (CGFloat)backingScaleFactor;
18 @end
20 #endif  // 10.7
22 namespace {
24 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) {
25   // Primary monitor is defined as the monitor with the menubar,
26   // which is always at index 0.
27   NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0];
28   float primary_screen_height = [primary_screen frame].size.height;
29   gfx::Rect rect(NSRectToCGRect(ns_rect));
30   rect.set_y(primary_screen_height - rect.y() - rect.height());
31   return rect;
34 NSScreen* GetMatchingScreen(const gfx::Rect& match_rect) {
35   // Default to the monitor with the current keyboard focus, in case
36   // |match_rect| is not on any screen at all.
37   NSScreen* max_screen = [NSScreen mainScreen];
38   int max_area = 0;
40   for (NSScreen* screen in [NSScreen screens]) {
41     gfx::Rect monitor_area = ConvertCoordinateSystem([screen frame]);
42     gfx::Rect intersection = gfx::IntersectRects(monitor_area, match_rect);
43     int area = intersection.width() * intersection.height();
44     if (area > max_area) {
45       max_area = area;
46       max_screen = screen;
47     }
48   }
50   return max_screen;
53 gfx::Display GetDisplayForScreen(NSScreen* screen, bool is_primary) {
54   NSRect frame = [screen frame];
55   // TODO(oshima): Implement ID and Observer.
56   gfx::Display display(0, gfx::Rect(NSRectToCGRect(frame)));
58   NSRect visible_frame = [screen visibleFrame];
60   // Convert work area's coordinate systems.
61   if (is_primary) {
62     gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame));
63     work_area.set_y(frame.size.height - visible_frame.origin.y -
64                     visible_frame.size.height);
65     display.set_work_area(work_area);
66   } else {
67     display.set_work_area(ConvertCoordinateSystem(visible_frame));
68   }
69   CGFloat scale;
70   if ([screen respondsToSelector:@selector(backingScaleFactor)])
71     scale = [screen backingScaleFactor];
72   else
73     scale = [screen userSpaceScaleFactor];
74   display.set_device_scale_factor(scale);
75   return display;
78 class ScreenMac : public gfx::Screen {
79  public:
80   ScreenMac() {}
82   virtual bool IsDIPEnabled() OVERRIDE {
83     return true;
84   }
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   }
94   virtual gfx::NativeWindow GetWindowAtCursorScreenPoint() OVERRIDE {
95     NOTIMPLEMENTED();
96     return gfx::NativeWindow();
97   }
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.
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     }
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     }
130     return display_count;
131   }
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   }
142   virtual gfx::Display GetDisplayNearestPoint(
143       const gfx::Point& point) const OVERRIDE {
144     NSPoint ns_point = NSPointFromCGPoint(point.ToCGPoint());
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   }
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   }
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   }
171   virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
172     // TODO(oshima): crbug.com/122863.
173   }
175   virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
176     // TODO(oshima): crbug.com/122863.
177   }
179  private:
180   DISALLOW_COPY_AND_ASSIGN(ScreenMac);
183 }  // namespace
185 namespace gfx {
187 Screen* CreateNativeScreen() {
188   return new ScreenMac;