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

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

Issue 1380083005: Mac: Use [NSArray firstObject] for [NSScreen screens] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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
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> 10 #include <map>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/mac/sdk_forward_declarations.h" 13 #include "base/mac/sdk_forward_declarations.h"
14 #include "base/timer/timer.h" 14 #include "base/timer/timer.h"
15 #include "ui/gfx/display.h" 15 #include "ui/gfx/display.h"
16 #include "ui/gfx/display_change_notifier.h" 16 #include "ui/gfx/display_change_notifier.h"
17 17
18 namespace { 18 namespace {
19 19
20 // The delay to handle the display configuration changes. 20 // The delay to handle the display configuration changes.
21 // See comments in ScreenMac::HandleDisplayReconfiguration. 21 // See comments in ScreenMac::HandleDisplayReconfiguration.
22 const int64 kConfigureDelayMs = 500; 22 const int64 kConfigureDelayMs = 500;
23 23
24 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) { 24 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) {
25 // Primary monitor is defined as the monitor with the menubar, 25 // Primary monitor is defined as the monitor with the menubar,
26 // which is always at index 0. 26 // which is always at index 0.
27 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; 27 NSScreen* primary_screen = [[NSScreen screens] firstObject];
28 float primary_screen_height = [primary_screen frame].size.height; 28 float primary_screen_height = [primary_screen frame].size.height;
29 gfx::Rect rect(NSRectToCGRect(ns_rect)); 29 gfx::Rect rect(NSRectToCGRect(ns_rect));
30 rect.set_y(primary_screen_height - rect.y() - rect.height()); 30 rect.set_y(primary_screen_height - rect.y() - rect.height());
31 return rect; 31 return rect;
32 } 32 }
33 33
34 NSScreen* GetMatchingScreen(const gfx::Rect& match_rect) { 34 NSScreen* GetMatchingScreen(const gfx::Rect& match_rect) {
35 // Default to the monitor with the current keyboard focus, in case 35 // Default to the monitor with the current keyboard focus, in case
36 // |match_rect| is not on any screen at all. 36 // |match_rect| is not on any screen at all.
37 NSScreen* max_screen = [NSScreen mainScreen]; 37 NSScreen* max_screen = [NSScreen mainScreen];
(...skipping 13 matching lines...) Expand all
51 } 51 }
52 52
53 gfx::Display GetDisplayForScreen(NSScreen* screen) { 53 gfx::Display GetDisplayForScreen(NSScreen* screen) {
54 NSRect frame = [screen frame]; 54 NSRect frame = [screen frame];
55 55
56 CGDirectDisplayID display_id = [[[screen deviceDescription] 56 CGDirectDisplayID display_id = [[[screen deviceDescription]
57 objectForKey:@"NSScreenNumber"] unsignedIntValue]; 57 objectForKey:@"NSScreenNumber"] unsignedIntValue];
58 58
59 gfx::Display display(display_id, gfx::Rect(NSRectToCGRect(frame))); 59 gfx::Display display(display_id, gfx::Rect(NSRectToCGRect(frame)));
60 NSRect visible_frame = [screen visibleFrame]; 60 NSRect visible_frame = [screen visibleFrame];
61 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; 61 NSScreen* primary = [[NSScreen screens] firstObject];
62 62
63 // Convert work area's coordinate systems. 63 // Convert work area's coordinate systems.
64 if ([screen isEqual:primary]) { 64 if ([screen isEqual:primary]) {
65 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame)); 65 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame));
66 work_area.set_y(frame.size.height - visible_frame.origin.y - 66 work_area.set_y(frame.size.height - visible_frame.origin.y -
67 visible_frame.size.height); 67 visible_frame.size.height);
68 display.set_work_area(work_area); 68 display.set_work_area(work_area);
69 } else { 69 } else {
70 display.set_bounds(ConvertCoordinateSystem(frame)); 70 display.set_bounds(ConvertCoordinateSystem(frame));
71 display.set_work_area(ConvertCoordinateSystem(visible_frame)); 71 display.set_work_area(ConvertCoordinateSystem(visible_frame));
(...skipping 24 matching lines...) Expand all
96 } 96 }
97 97
98 ~ScreenMac() override { 98 ~ScreenMac() override {
99 CGDisplayRemoveReconfigurationCallback( 99 CGDisplayRemoveReconfigurationCallback(
100 ScreenMac::DisplayReconfigurationCallBack, this); 100 ScreenMac::DisplayReconfigurationCallBack, this);
101 } 101 }
102 102
103 gfx::Point GetCursorScreenPoint() override { 103 gfx::Point GetCursorScreenPoint() override {
104 NSPoint mouseLocation = [NSEvent mouseLocation]; 104 NSPoint mouseLocation = [NSEvent mouseLocation];
105 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. 105 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
106 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; 106 NSScreen* screen = [[NSScreen screens] firstObject];
107 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; 107 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y;
108 return gfx::Point(mouseLocation.x, mouseLocation.y); 108 return gfx::Point(mouseLocation.x, mouseLocation.y);
109 } 109 }
110 110
111 gfx::NativeWindow GetWindowUnderCursor() override { 111 gfx::NativeWindow GetWindowUnderCursor() override {
112 NOTIMPLEMENTED(); 112 NOTIMPLEMENTED();
113 return gfx::NativeWindow(); 113 return gfx::NativeWindow();
114 } 114 }
115 115
116 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override { 116 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 // Returns the display that most closely intersects the provided bounds. 154 // Returns the display that most closely intersects the provided bounds.
155 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override { 155 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override {
156 NSScreen* match_screen = GetMatchingScreen(match_rect); 156 NSScreen* match_screen = GetMatchingScreen(match_rect);
157 return GetDisplayForScreen(match_screen); 157 return GetDisplayForScreen(match_screen);
158 } 158 }
159 159
160 // Returns the primary display. 160 // Returns the primary display.
161 gfx::Display GetPrimaryDisplay() const override { 161 gfx::Display GetPrimaryDisplay() const override {
162 // Primary display is defined as the display with the menubar, 162 // Primary display is defined as the display with the menubar,
163 // which is always at index 0. 163 // which is always at index 0.
164 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; 164 NSScreen* primary = [[NSScreen screens] firstObject];
165 gfx::Display display = GetDisplayForScreen(primary); 165 gfx::Display display = GetDisplayForScreen(primary);
166 return display; 166 return display;
167 } 167 }
168 168
169 void AddObserver(gfx::DisplayObserver* observer) override { 169 void AddObserver(gfx::DisplayObserver* observer) override {
170 change_notifier_.AddObserver(observer); 170 change_notifier_.AddObserver(observer);
171 } 171 }
172 172
173 void RemoveObserver(gfx::DisplayObserver* observer) override { 173 void RemoveObserver(gfx::DisplayObserver* observer) override {
174 change_notifier_.RemoveObserver(observer); 174 change_notifier_.RemoveObserver(observer);
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 273
274 namespace gfx { 274 namespace gfx {
275 275
276 #if !defined(USE_AURA) 276 #if !defined(USE_AURA)
277 Screen* CreateNativeScreen() { 277 Screen* CreateNativeScreen() {
278 return new ScreenMac; 278 return new ScreenMac;
279 } 279 }
280 #endif 280 #endif
281 281
282 } 282 }
OLDNEW
« no previous file with comments | « ui/gfx/mac/coordinate_conversion_unittest.mm ('k') | ui/message_center/cocoa/popup_collection.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698