Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: ui/gfx/screen_mac.mm

Issue 8549008: Extract MonitorInfoProvider from WindowSizer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add docs Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/screen_gtk.cc ('k') | ui/gfx/screen_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 namespace {
11
12 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) {
13 // Primary monitor is defined as the monitor with the menubar,
14 // which is always at index 0.
15 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0];
16 float primary_screen_height = [primary_screen frame].size.height;
17 gfx::Rect rect(NSRectToCGRect(ns_rect));
18 rect.set_y(primary_screen_height - rect.y() - rect.height());
19 return rect;
20 }
21
22 NSScreen* GetMatchingScreen(const gfx::Rect& match_rect) {
23 // Default to the monitor with the current keyboard focus, in case
24 // |match_rect| is not on any screen at all.
25 NSScreen* max_screen = [NSScreen mainScreen];
26 int max_area = 0;
27
28 for (NSScreen* screen in [NSScreen screens]) {
29 gfx::Rect monitor_area = ConvertCoordinateSystem([screen frame]);
30 gfx::Rect intersection = monitor_area.Intersect(match_rect);
31 int area = intersection.width() * intersection.height();
32 if (area > max_area) {
33 max_area = area;
34 max_screen = screen;
35 }
36 }
37
38 return max_screen;
39 }
40
41 } // namespace
42
10 namespace gfx { 43 namespace gfx {
11 44
12 // static 45 // static
13 gfx::Point Screen::GetCursorScreenPoint() { 46 gfx::Point Screen::GetCursorScreenPoint() {
14 NSPoint mouseLocation = [NSEvent mouseLocation]; 47 NSPoint mouseLocation = [NSEvent mouseLocation];
15 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. 48 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
16 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; 49 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
17 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; 50 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y;
18 return gfx::Point(mouseLocation.x, mouseLocation.y); 51 return gfx::Point(mouseLocation.x, mouseLocation.y);
19 } 52 }
20 53
21 // static 54 // static
55 gfx::Rect Screen::GetPrimaryMonitorWorkArea() {
56 // Primary monitor is defined as the monitor with the menubar,
57 // which is always at index 0.
58 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
59 NSRect frame = [primary frame];
60 NSRect visible_frame = [primary visibleFrame];
61
62 // Convert coordinate systems.
63 gfx::Rect rect = gfx::Rect(NSRectToCGRect(visible_frame));
64 rect.set_y(frame.size.height - visible_frame.origin.y -
65 visible_frame.size.height);
66 return rect;
67 }
68
69 // static
70 gfx::Rect Screen::GetPrimaryMonitorBounds() {
71 // Primary monitor is defined as the monitor with the menubar,
72 // which is always at index 0.
73 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
74 return gfx::Rect(NSRectToCGRect([primary frame]));
75 }
76
77 // static
78 gfx::Rect Screen::GetMonitorWorkAreaMatching(const gfx::Rect& match_rect) {
79 NSScreen* match_screen = GetMatchingScreen(match_rect);
80 return ConvertCoordinateSystem([match_screen visibleFrame]);
81 }
82
83 // static
22 gfx::Size Screen::GetPrimaryMonitorSize() { 84 gfx::Size Screen::GetPrimaryMonitorSize() {
23 CGDirectDisplayID main_display = CGMainDisplayID(); 85 CGDirectDisplayID main_display = CGMainDisplayID();
24 return gfx::Size(CGDisplayPixelsWide(main_display), 86 return gfx::Size(CGDisplayPixelsWide(main_display),
25 CGDisplayPixelsHigh(main_display)); 87 CGDisplayPixelsHigh(main_display));
26 } 88 }
27 89
28 // static 90 // static
29 int Screen::GetNumMonitors() { 91 int Screen::GetNumMonitors() {
30 // Don't just return the number of online displays. It includes displays 92 // Don't just return the number of online displays. It includes displays
31 // that mirror other displays, which are not desired in the count. It's 93 // that mirror other displays, which are not desired in the count. It's
(...skipping 22 matching lines...) Expand all
54 // The primary display in a mirrored set will be counted, but those that 116 // The primary display in a mirrored set will be counted, but those that
55 // mirror it will not be. 117 // mirror it will not be.
56 ++display_count; 118 ++display_count;
57 } 119 }
58 } 120 }
59 121
60 return display_count; 122 return display_count;
61 } 123 }
62 124
63 } // namespace gfx 125 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/screen_gtk.cc ('k') | ui/gfx/screen_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698