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

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: DeviceScaleFactor 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
« no previous file with comments | « ui/gfx/screen_gtk.cc ('k') | ui/gfx/screen_unittest.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 #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 static gfx::SimpleMonitor* monitor = new gfx::SimpleMonitor();
dhollowa 2012/04/09 23:41:37 This will cause SimpleMonitor destructor to fire p
oshima 2012/04/10 00:09:08 This isn't uncommon for near POD objects such as F
46
47 NSRect frame = [screen frame];
48 monitor->set_bounds(gfx::Rect(NSRectToCGRect(frame)));
49
50 NSRect visible_frame = [screen visibleFrame];
51
52 // Convert work area's coordinate systems.
53 if (is_primary) {
54 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame));
55 work_area.set_y(frame.size.height - visible_frame.origin.y -
56 visible_frame.size.height);
57 monitor->set_work_area(work_area);
58 } else {
59 monitor->set_work_area(ConvertCoordinateSystem(visible_frame));
60 }
61 return monitor;
62 }
63
41 } // namespace 64 } // namespace
42 65
43 namespace gfx { 66 namespace gfx {
44 67
45 // static 68 // static
46 gfx::Point Screen::GetCursorScreenPoint() { 69 gfx::Point Screen::GetCursorScreenPoint() {
47 NSPoint mouseLocation = [NSEvent mouseLocation]; 70 NSPoint mouseLocation = [NSEvent mouseLocation];
48 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. 71 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
49 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; 72 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
50 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; 73 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y;
51 return gfx::Point(mouseLocation.x, mouseLocation.y); 74 return gfx::Point(mouseLocation.x, mouseLocation.y);
52 } 75 }
53 76
54 // static 77 // static
55 gfx::Rect Screen::GetPrimaryMonitorWorkArea() { 78 const gfx::Monitor* Screen::GetPrimaryMonitor() {
56 // Primary monitor is defined as the monitor with the menubar, 79 // Primary monitor is defined as the monitor with the menubar,
57 // which is always at index 0. 80 // which is always at index 0.
58 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; 81 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
59 NSRect frame = [primary frame]; 82 gfx::Monitor* monitor = GetMonitorForScreen(primary, true /* primary */);
60 NSRect visible_frame = [primary visibleFrame];
61 83
62 // Convert coordinate systems. 84 CGDirectDisplayID main_display = CGMainDisplayID();
63 gfx::Rect rect = gfx::Rect(NSRectToCGRect(visible_frame)); 85 CHECK_EQ(static_cast<const int>(CGDisplayPixelsWide(main_display)),
64 rect.set_y(frame.size.height - visible_frame.origin.y - 86 monitor->GetSize().width());
65 visible_frame.size.height); 87 CHECK_EQ(static_cast<const int>(CGDisplayPixelsHigh(main_display)),
66 return rect; 88 monitor->GetSize().height());
89 return monitor;
67 } 90 }
68 91
69 // static 92 // static
70 gfx::Rect Screen::GetPrimaryMonitorBounds() { 93 const gfx::Monitor* Screen::GetMonitorMatching(const gfx::Rect& match_rect) {
71 // Primary monitor is defined as the monitor with the menubar, 94 NSScreen* match_screen = GetMatchingScreen(match_rect);
72 // which is always at index 0. 95 return GetMonitorForScreen(match_screen, false /* may not be primary */);
73 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
74 return gfx::Rect(NSRectToCGRect([primary frame]));
75 } 96 }
76 97
77 // static 98 // 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() { 99 int Screen::GetNumMonitors() {
92 // Don't just return the number of online displays. It includes displays 100 // 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 101 // that mirror other displays, which are not desired in the count. It's
94 // tempting to use the count returned by CGGetActiveDisplayList, but active 102 // tempting to use the count returned by CGGetActiveDisplayList, but active
95 // displays exclude sleeping displays, and those are desired in the count. 103 // displays exclude sleeping displays, and those are desired in the count.
96 104
97 // It would be ridiculous to have this many displays connected, but 105 // It would be ridiculous to have this many displays connected, but
98 // CGDirectDisplayID is just an integer, so supporting up to this many 106 // CGDirectDisplayID is just an integer, so supporting up to this many
99 // doesn't hurt. 107 // doesn't hurt.
100 CGDirectDisplayID online_displays[128]; 108 CGDirectDisplayID online_displays[128];
(...skipping 15 matching lines...) Expand all
116 // The primary display in a mirrored set will be counted, but those that 124 // The primary display in a mirrored set will be counted, but those that
117 // mirror it will not be. 125 // mirror it will not be.
118 ++display_count; 126 ++display_count;
119 } 127 }
120 } 128 }
121 129
122 return display_count; 130 return display_count;
123 } 131 }
124 132
125 } // namespace gfx 133 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/screen_gtk.cc ('k') | ui/gfx/screen_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698