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

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

Issue 9960042: Refactor screen/monitor so that gfx::Screen returns monitor object. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 8 years, 8 months 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
OLDNEW
1 // Copyright (c) 2011 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"
11 #include "ui/gfx/monitor.h"
12
10 namespace { 13 namespace {
11 14
12 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) { 15 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) {
13 // Primary monitor is defined as the monitor with the menubar, 16 // Primary monitor is defined as the monitor with the menubar,
14 // which is always at index 0. 17 // which is always at index 0.
15 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; 18 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0];
16 float primary_screen_height = [primary_screen frame].size.height; 19 float primary_screen_height = [primary_screen frame].size.height;
17 gfx::Rect rect(NSRectToCGRect(ns_rect)); 20 gfx::Rect rect(NSRectToCGRect(ns_rect));
18 rect.set_y(primary_screen_height - rect.y() - rect.height()); 21 rect.set_y(primary_screen_height - rect.y() - rect.height());
19 return rect; 22 return rect;
(...skipping 11 matching lines...) Expand all
31 int area = intersection.width() * intersection.height(); 34 int area = intersection.width() * intersection.height();
32 if (area > max_area) { 35 if (area > max_area) {
33 max_area = area; 36 max_area = area;
34 max_screen = screen; 37 max_screen = screen;
35 } 38 }
36 } 39 }
37 40
38 return max_screen; 41 return max_screen;
39 } 42 }
40 43
44 gfx::Monitor GetMonitorForScreen(NSScreen* screen, bool is_primary) {
45 NSRect frame = [screen frame];
46 gfx::Monitor monitor(gfx::Rect(NSRectToCGRect(frame)));
47
48 NSRect visible_frame = [screen visibleFrame];
49
50 // Convert work area's coordinate systems.
51 if (is_primary) {
52 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame));
53 work_area.set_y(frame.size.height - visible_frame.origin.y -
54 visible_frame.size.height);
55 monitor.set_work_area(work_area);
56 } else {
57 monitor.set_work_area(ConvertCoordinateSystem(visible_frame));
58 }
59 return monitor;
60 }
61
41 } // namespace 62 } // namespace
42 63
43 namespace gfx { 64 namespace gfx {
44 65
45 // static 66 // static
46 gfx::Point Screen::GetCursorScreenPoint() { 67 gfx::Point Screen::GetCursorScreenPoint() {
47 NSPoint mouseLocation = [NSEvent mouseLocation]; 68 NSPoint mouseLocation = [NSEvent mouseLocation];
48 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. 69 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
49 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; 70 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
50 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; 71 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y;
51 return gfx::Point(mouseLocation.x, mouseLocation.y); 72 return gfx::Point(mouseLocation.x, mouseLocation.y);
52 } 73 }
53 74
54 // static 75 // static
55 gfx::Rect Screen::GetPrimaryMonitorWorkArea() { 76 gfx::Monitor Screen::GetPrimaryMonitor() {
56 // Primary monitor is defined as the monitor with the menubar, 77 // Primary monitor is defined as the monitor with the menubar,
57 // which is always at index 0. 78 // which is always at index 0.
58 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; 79 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
59 NSRect frame = [primary frame]; 80 gfx::Monitor monitor = GetMonitorForScreen(primary, true /* primary */);
60 NSRect visible_frame = [primary visibleFrame];
61 81
62 // Convert coordinate systems. 82 CGDirectDisplayID main_display = CGMainDisplayID();
63 gfx::Rect rect = gfx::Rect(NSRectToCGRect(visible_frame)); 83 CHECK_EQ(static_cast<const int>(CGDisplayPixelsWide(main_display)),
64 rect.set_y(frame.size.height - visible_frame.origin.y - 84 monitor.size().width());
65 visible_frame.size.height); 85 CHECK_EQ(static_cast<const int>(CGDisplayPixelsHigh(main_display)),
66 return rect; 86 monitor.size().height());
87 return monitor;
67 } 88 }
68 89
69 // static 90 // static
70 gfx::Rect Screen::GetPrimaryMonitorBounds() { 91 gfx::Monitor Screen::GetMonitorMatching(const gfx::Rect& match_rect) {
71 // Primary monitor is defined as the monitor with the menubar, 92 NSScreen* match_screen = GetMatchingScreen(match_rect);
72 // which is always at index 0. 93 return GetMonitorForScreen(match_screen, false /* may not be primary */);
73 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
74 return gfx::Rect(NSRectToCGRect([primary frame]));
75 } 94 }
76 95
77 // static 96 // 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
84 gfx::Size Screen::GetPrimaryMonitorSize() {
85 CGDirectDisplayID main_display = CGMainDisplayID();
86 return gfx::Size(CGDisplayPixelsWide(main_display),
87 CGDisplayPixelsHigh(main_display));
88 }
89
90 // static
91 int Screen::GetNumMonitors() { 97 int Screen::GetNumMonitors() {
92 // Don't just return the number of online displays. It includes displays 98 // Don't just return the number of online displays. It includes displays
93 // that mirror other displays, which are not desired in the count. It's 99 // that mirror other displays, which are not desired in the count. It's
94 // tempting to use the count returned by CGGetActiveDisplayList, but active 100 // tempting to use the count returned by CGGetActiveDisplayList, but active
95 // displays exclude sleeping displays, and those are desired in the count. 101 // displays exclude sleeping displays, and those are desired in the count.
96 102
97 // It would be ridiculous to have this many displays connected, but 103 // It would be ridiculous to have this many displays connected, but
98 // CGDirectDisplayID is just an integer, so supporting up to this many 104 // CGDirectDisplayID is just an integer, so supporting up to this many
99 // doesn't hurt. 105 // doesn't hurt.
100 CGDirectDisplayID online_displays[128]; 106 CGDirectDisplayID online_displays[128];
(...skipping 15 matching lines...) Expand all
116 // The primary display in a mirrored set will be counted, but those that 122 // The primary display in a mirrored set will be counted, but those that
117 // mirror it will not be. 123 // mirror it will not be.
118 ++display_count; 124 ++display_count;
119 } 125 }
120 } 126 }
121 127
122 return display_count; 128 return display_count;
123 } 129 }
124 130
125 } // namespace gfx 131 } // namespace gfx
OLDNEW
« ui/gfx/screen_aura.cc ('K') | « ui/gfx/screen_impl.h ('k') | ui/gfx/screen_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698