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

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

Issue 40163002: Implement GetAllDisplays() for the Mac screen backend (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Implement GetAllDisplays() - variable naming Created 7 years, 1 month 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
« no previous file with comments | « no previous file | no next file » | 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) 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 #include <map>
11
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "base/mac/sdk_forward_declarations.h" 13 #include "base/mac/sdk_forward_declarations.h"
12 #include "ui/gfx/display.h" 14 #include "ui/gfx/display.h"
13 15
14 namespace { 16 namespace {
15 17
16 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) { 18 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) {
17 // Primary monitor is defined as the monitor with the menubar, 19 // Primary monitor is defined as the monitor with the menubar,
18 // which is always at index 0. 20 // which is always at index 0.
19 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; 21 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0];
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 return gfx::NativeWindow(); 92 return gfx::NativeWindow();
91 } 93 }
92 94
93 virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) 95 virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
94 OVERRIDE { 96 OVERRIDE {
95 NOTIMPLEMENTED(); 97 NOTIMPLEMENTED();
96 return gfx::NativeWindow(); 98 return gfx::NativeWindow();
97 } 99 }
98 100
99 virtual int GetNumDisplays() const OVERRIDE { 101 virtual int GetNumDisplays() const OVERRIDE {
100 // Don't just return the number of online displays. It includes displays 102 return GetAllDisplays().size();
101 // that mirror other displays, which are not desired in the count. It's 103
104 }
105
106 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
107 // Don't just return all online displays. This would include displays
108 // that mirror other displays, which are not desired in this list. It's
102 // tempting to use the count returned by CGGetActiveDisplayList, but active 109 // tempting to use the count returned by CGGetActiveDisplayList, but active
103 // displays exclude sleeping displays, and those are desired in the count. 110 // displays exclude sleeping displays, and those are desired.
104 111
105 // It would be ridiculous to have this many displays connected, but 112 // It would be ridiculous to have this many displays connected, but
106 // CGDirectDisplayID is just an integer, so supporting up to this many 113 // CGDirectDisplayID is just an integer, so supporting up to this many
107 // doesn't hurt. 114 // doesn't hurt.
108 CGDirectDisplayID online_displays[128]; 115 CGDirectDisplayID online_displays[128];
109 CGDisplayCount online_display_count = 0; 116 CGDisplayCount online_display_count = 0;
110 if (CGGetOnlineDisplayList(arraysize(online_displays), 117 if (CGGetOnlineDisplayList(arraysize(online_displays),
111 online_displays, 118 online_displays,
112 &online_display_count) != kCGErrorSuccess) { 119 &online_display_count) != kCGErrorSuccess) {
113 // 1 is a reasonable assumption. 120 return std::vector<gfx::Display>(1, GetPrimaryDisplay());
114 return 1;
115 } 121 }
116 122
117 int display_count = 0; 123 typedef std::map<int64, NSScreen*> ScreenIdsToScreensMap;
124 ScreenIdsToScreensMap screen_ids_to_screens;
125 for (NSScreen* screen in [NSScreen screens]) {
126 NSDictionary* screen_device_description = [screen deviceDescription];
127 int64 screen_id = [[screen_device_description
128 objectForKey:@"NSScreenNumber"] unsignedIntValue];
129 screen_ids_to_screens[screen_id] = screen;
130 }
131
132 std::vector<gfx::Display> displays;
118 for (CGDisplayCount online_display_index = 0; 133 for (CGDisplayCount online_display_index = 0;
119 online_display_index < online_display_count; 134 online_display_index < online_display_count;
120 ++online_display_index) { 135 ++online_display_index) {
121 CGDirectDisplayID online_display = online_displays[online_display_index]; 136 CGDirectDisplayID online_display = online_displays[online_display_index];
122 if (CGDisplayMirrorsDisplay(online_display) == kCGNullDirectDisplay) { 137 if (CGDisplayMirrorsDisplay(online_display) == kCGNullDirectDisplay) {
123 // If this display doesn't mirror any other, include it in the count. 138 // If this display doesn't mirror any other, include it in the list.
124 // The primary display in a mirrored set will be counted, but those that 139 // The primary display in a mirrored set will be counted, but those that
125 // mirror it will not be. 140 // mirror it will not be.
126 ++display_count; 141 ScreenIdsToScreensMap::iterator foundScreen =
142 screen_ids_to_screens.find(online_display);
143 if (foundScreen != screen_ids_to_screens.end()) {
144 displays.push_back(GetDisplayForScreen(foundScreen->second));
145 }
127 } 146 }
128 } 147 }
129 148
130 return display_count; 149 if (!displays.size())
131 } 150 return std::vector<gfx::Display>(1, GetPrimaryDisplay());
132 151
133 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE { 152 return displays;
134 NOTIMPLEMENTED();
135 return std::vector<gfx::Display>(1, GetPrimaryDisplay());
136 } 153 }
137 154
138 virtual gfx::Display GetDisplayNearestWindow( 155 virtual gfx::Display GetDisplayNearestWindow(
139 gfx::NativeView view) const OVERRIDE { 156 gfx::NativeView view) const OVERRIDE {
140 NSWindow* window = [view window]; 157 NSWindow* window = [view window];
141 if (!window) 158 if (!window)
142 return GetPrimaryDisplay(); 159 return GetPrimaryDisplay();
143 NSScreen* match_screen = [window screen]; 160 NSScreen* match_screen = [window screen];
144 if (!match_screen) 161 if (!match_screen)
145 return GetPrimaryDisplay(); 162 return GetPrimaryDisplay();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 207
191 } // namespace 208 } // namespace
192 209
193 namespace gfx { 210 namespace gfx {
194 211
195 Screen* CreateNativeScreen() { 212 Screen* CreateNativeScreen() {
196 return new ScreenMac; 213 return new ScreenMac;
197 } 214 }
198 215
199 } 216 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698