| 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 @interface NSScreen (LionAPI) | 10 @interface NSScreen (LionAPI) |
| 11 - (CGFloat)backingScaleFactor; | 11 - (CGFloat)backingScaleFactor; |
| 12 @end | 12 @end |
| 13 | 13 |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "ui/gfx/monitor.h" | 15 #include "ui/gfx/display.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) { | 19 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) { |
| 20 // Primary monitor is defined as the monitor with the menubar, | 20 // Primary monitor is defined as the monitor with the menubar, |
| 21 // which is always at index 0. | 21 // which is always at index 0. |
| 22 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; | 22 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; |
| 23 float primary_screen_height = [primary_screen frame].size.height; | 23 float primary_screen_height = [primary_screen frame].size.height; |
| 24 gfx::Rect rect(NSRectToCGRect(ns_rect)); | 24 gfx::Rect rect(NSRectToCGRect(ns_rect)); |
| 25 rect.set_y(primary_screen_height - rect.y() - rect.height()); | 25 rect.set_y(primary_screen_height - rect.y() - rect.height()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 38 int area = intersection.width() * intersection.height(); | 38 int area = intersection.width() * intersection.height(); |
| 39 if (area > max_area) { | 39 if (area > max_area) { |
| 40 max_area = area; | 40 max_area = area; |
| 41 max_screen = screen; | 41 max_screen = screen; |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 return max_screen; | 45 return max_screen; |
| 46 } | 46 } |
| 47 | 47 |
| 48 gfx::Monitor GetMonitorForScreen(NSScreen* screen, bool is_primary) { | 48 gfx::Display GetDisplayForScreen(NSScreen* screen, bool is_primary) { |
| 49 NSRect frame = [screen frame]; | 49 NSRect frame = [screen frame]; |
| 50 // TODO(oshima): Implement ID and Observer. | 50 // TODO(oshima): Implement ID and Observer. |
| 51 gfx::Monitor monitor(0, gfx::Rect(NSRectToCGRect(frame))); | 51 gfx::Display display(0, gfx::Rect(NSRectToCGRect(frame))); |
| 52 | 52 |
| 53 NSRect visible_frame = [screen visibleFrame]; | 53 NSRect visible_frame = [screen visibleFrame]; |
| 54 | 54 |
| 55 // Convert work area's coordinate systems. | 55 // Convert work area's coordinate systems. |
| 56 if (is_primary) { | 56 if (is_primary) { |
| 57 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame)); | 57 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame)); |
| 58 work_area.set_y(frame.size.height - visible_frame.origin.y - | 58 work_area.set_y(frame.size.height - visible_frame.origin.y - |
| 59 visible_frame.size.height); | 59 visible_frame.size.height); |
| 60 monitor.set_work_area(work_area); | 60 display.set_work_area(work_area); |
| 61 } else { | 61 } else { |
| 62 monitor.set_work_area(ConvertCoordinateSystem(visible_frame)); | 62 display.set_work_area(ConvertCoordinateSystem(visible_frame)); |
| 63 } | 63 } |
| 64 CGFloat scale; | 64 CGFloat scale; |
| 65 if ([screen respondsToSelector:@selector(backingScaleFactor)]) | 65 if ([screen respondsToSelector:@selector(backingScaleFactor)]) |
| 66 scale = [screen backingScaleFactor]; | 66 scale = [screen backingScaleFactor]; |
| 67 else | 67 else |
| 68 scale = [screen userSpaceScaleFactor]; | 68 scale = [screen userSpaceScaleFactor]; |
| 69 monitor.set_device_scale_factor(scale); | 69 display.set_device_scale_factor(scale); |
| 70 return monitor; | 70 return display; |
| 71 } | 71 } |
| 72 | 72 |
| 73 } // namespace | 73 } // namespace |
| 74 | 74 |
| 75 namespace gfx { | 75 namespace gfx { |
| 76 | 76 |
| 77 // static | 77 // static |
| 78 bool Screen::IsDIPEnabled() { | 78 bool Screen::IsDIPEnabled() { |
| 79 return false; | 79 return false; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // static | 82 // static |
| 83 gfx::Point Screen::GetCursorScreenPoint() { | 83 gfx::Point Screen::GetCursorScreenPoint() { |
| 84 NSPoint mouseLocation = [NSEvent mouseLocation]; | 84 NSPoint mouseLocation = [NSEvent mouseLocation]; |
| 85 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. | 85 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. |
| 86 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | 86 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 87 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; | 87 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; |
| 88 return gfx::Point(mouseLocation.x, mouseLocation.y); | 88 return gfx::Point(mouseLocation.x, mouseLocation.y); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // static | 91 // static |
| 92 gfx::Monitor Screen::GetMonitorNearestWindow(gfx::NativeView view) { | 92 gfx::Display Screen::GetMonitorNearestWindow(gfx::NativeView view) { |
| 93 NSWindow* window = [view window]; | 93 NSWindow* window = [view window]; |
| 94 if (!window) | 94 if (!window) |
| 95 return GetPrimaryMonitor(); | 95 return GetPrimaryMonitor(); |
| 96 NSScreen* match_screen = [window screen]; | 96 NSScreen* match_screen = [window screen]; |
| 97 return GetMonitorForScreen(match_screen, false /* may not be primary */); | 97 return GetDisplayForScreen(match_screen, false /* may not be primary */); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // static | 100 // static |
| 101 gfx::Monitor Screen::GetPrimaryMonitor() { | 101 gfx::Display Screen::GetPrimaryMonitor() { |
| 102 // Primary monitor is defined as the monitor with the menubar, | 102 // Primary display is defined as the display with the menubar, |
| 103 // which is always at index 0. | 103 // which is always at index 0. |
| 104 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; | 104 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; |
| 105 gfx::Monitor monitor = GetMonitorForScreen(primary, true /* primary */); | 105 gfx::Display display = GetDisplayForScreen(primary, true /* primary */); |
| 106 return monitor; | 106 return display; |
| 107 } | 107 } |
| 108 | 108 |
| 109 // static | 109 // static |
| 110 gfx::Monitor Screen::GetMonitorMatching(const gfx::Rect& match_rect) { | 110 gfx::Display Screen::GetMonitorMatching(const gfx::Rect& match_rect) { |
| 111 NSScreen* match_screen = GetMatchingScreen(match_rect); | 111 NSScreen* match_screen = GetMatchingScreen(match_rect); |
| 112 return GetMonitorForScreen(match_screen, false /* may not be primary */); | 112 return GetDisplayForScreen(match_screen, false /* may not be primary */); |
| 113 } | 113 } |
| 114 | 114 |
| 115 // static | 115 // static |
| 116 int Screen::GetNumMonitors() { | 116 int Screen::GetNumMonitors() { |
| 117 // Don't just return the number of online displays. It includes displays | 117 // Don't just return the number of online displays. It includes displays |
| 118 // that mirror other displays, which are not desired in the count. It's | 118 // that mirror other displays, which are not desired in the count. It's |
| 119 // tempting to use the count returned by CGGetActiveDisplayList, but active | 119 // tempting to use the count returned by CGGetActiveDisplayList, but active |
| 120 // displays exclude sleeping displays, and those are desired in the count. | 120 // displays exclude sleeping displays, and those are desired in the count. |
| 121 | 121 |
| 122 // It would be ridiculous to have this many displays connected, but | 122 // It would be ridiculous to have this many displays connected, but |
| (...skipping 18 matching lines...) Expand all Loading... |
| 141 // The primary display in a mirrored set will be counted, but those that | 141 // The primary display in a mirrored set will be counted, but those that |
| 142 // mirror it will not be. | 142 // mirror it will not be. |
| 143 ++display_count; | 143 ++display_count; |
| 144 } | 144 } |
| 145 } | 145 } |
| 146 | 146 |
| 147 return display_count; | 147 return display_count; |
| 148 } | 148 } |
| 149 | 149 |
| 150 } // namespace gfx | 150 } // namespace gfx |
| OLD | NEW |