OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/monitor_info_provider.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "base/compiler_specific.h" |
| 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_list.h" |
| 12 #include "chrome/browser/ui/browser_window.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 class MonitorInfoProviderMac : public MonitorInfoProvider { |
| 17 public: |
| 18 MonitorInfoProviderMac(); |
| 19 virtual ~MonitorInfoProviderMac(); |
| 20 |
| 21 // Overridden from MonitorInfoProvider: |
| 22 virtual gfx::Rect GetPrimaryMonitorWorkArea() const OVERRIDE; |
| 23 virtual gfx::Rect GetPrimaryMonitorBounds() const OVERRIDE; |
| 24 virtual gfx::Rect GetMonitorWorkAreaMatching( |
| 25 const gfx::Rect& match_rect) const OVERRIDE; |
| 26 virtual void UpdateWorkAreas() OVERRIDE; |
| 27 |
| 28 private: |
| 29 // Returns a pointer to the screen that most closely matches the |
| 30 // given |match_rect|. This function currently returns the screen |
| 31 // that contains the largest amount of |match_rect| by area. |
| 32 NSScreen* GetMatchingScreen(const gfx::Rect& match_rect) const; |
| 33 |
| 34 // The lower left corner of screen 0 is always at (0, 0). The |
| 35 // cross-platform code expects the origin to be in the upper left, |
| 36 // so we have to translate |ns_rect| to the new coordinate |
| 37 // system. |
| 38 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) const; |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(MonitorInfoProviderMac); |
| 41 }; |
| 42 |
| 43 MonitorInfoProviderMac::MonitorInfoProviderMac() { |
| 44 } |
| 45 |
| 46 MonitorInfoProviderMac::~MonitorInfoProviderMac() { |
| 47 } |
| 48 |
| 49 gfx::Rect MonitorInfoProviderMac::GetPrimaryMonitorWorkArea() const { |
| 50 // Primary monitor is defined as the monitor with the menubar, |
| 51 // which is always at index 0. |
| 52 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; |
| 53 NSRect frame = [primary frame]; |
| 54 NSRect visible_frame = [primary visibleFrame]; |
| 55 |
| 56 // Convert coordinate systems. |
| 57 gfx::Rect rect = gfx::Rect(NSRectToCGRect(visible_frame)); |
| 58 rect.set_y(frame.size.height - |
| 59 visible_frame.origin.y - visible_frame.size.height); |
| 60 return rect; |
| 61 } |
| 62 |
| 63 gfx::Rect MonitorInfoProviderMac::GetPrimaryMonitorBounds() const { |
| 64 // Primary monitor is defined as the monitor with the menubar, |
| 65 // which is always at index 0. |
| 66 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; |
| 67 return gfx::Rect(NSRectToCGRect([primary frame])); |
| 68 } |
| 69 |
| 70 gfx::Rect MonitorInfoProviderMac::GetMonitorWorkAreaMatching( |
| 71 const gfx::Rect& match_rect) const { |
| 72 NSScreen* match_screen = GetMatchingScreen(match_rect); |
| 73 return ConvertCoordinateSystem([match_screen visibleFrame]); |
| 74 } |
| 75 |
| 76 void MonitorInfoProviderMac::UpdateWorkAreas() { |
| 77 work_areas_.clear(); |
| 78 |
| 79 for (NSScreen* screen in [NSScreen screens]) { |
| 80 gfx::Rect rect = ConvertCoordinateSystem([screen visibleFrame]); |
| 81 work_areas_.push_back(rect); |
| 82 } |
| 83 } |
| 84 |
| 85 NSScreen* MonitorInfoProviderMac::GetMatchingScreen( |
| 86 const gfx::Rect& match_rect) const { |
| 87 // Default to the monitor with the current keyboard focus, in case |
| 88 // |match_rect| is not on any screen at all. |
| 89 NSScreen* max_screen = [NSScreen mainScreen]; |
| 90 int max_area = 0; |
| 91 |
| 92 for (NSScreen* screen in [NSScreen screens]) { |
| 93 gfx::Rect monitor_area = ConvertCoordinateSystem([screen frame]); |
| 94 gfx::Rect intersection = monitor_area.Intersect(match_rect); |
| 95 int area = intersection.width() * intersection.height(); |
| 96 if (area > max_area) { |
| 97 max_area = area; |
| 98 max_screen = screen; |
| 99 } |
| 100 } |
| 101 |
| 102 return max_screen; |
| 103 } |
| 104 |
| 105 gfx::Rect MonitorInfoProviderMac::ConvertCoordinateSystem( |
| 106 NSRect ns_rect) const { |
| 107 // Primary monitor is defined as the monitor with the menubar, |
| 108 // which is always at index 0. |
| 109 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; |
| 110 float primary_screen_height = [primary_screen frame].size.height; |
| 111 gfx::Rect rect(NSRectToCGRect(ns_rect)); |
| 112 rect.set_y(primary_screen_height - rect.y() - rect.height()); |
| 113 return rect; |
| 114 } |
| 115 |
| 116 } // namespace |
| 117 |
| 118 // static |
| 119 MonitorInfoProvider* MonitorInfoProvider::Create() { |
| 120 return new MonitorInfoProviderMac(); |
| 121 } |
OLD | NEW |