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

Unified Diff: chrome/browser/ui/monitor_info_provider_mac.mm

Issue 8549008: Extract MonitorInfoProvider from WindowSizer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase 2 Created 9 years 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/monitor_info_provider_mac.mm
diff --git a/chrome/browser/ui/monitor_info_provider_mac.mm b/chrome/browser/ui/monitor_info_provider_mac.mm
new file mode 100644
index 0000000000000000000000000000000000000000..3a8339d1be06e26a1f2f047e8c4b8585bb1d0a07
--- /dev/null
+++ b/chrome/browser/ui/monitor_info_provider_mac.mm
@@ -0,0 +1,121 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/ui/monitor_info_provider.h"
+
+#import <Cocoa/Cocoa.h>
+
+#include "base/compiler_specific.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/browser/ui/browser_list.h"
+#include "chrome/browser/ui/browser_window.h"
+
+namespace {
+
+class MonitorInfoProviderMac : public MonitorInfoProvider {
+ public:
+ MonitorInfoProviderMac();
+ virtual ~MonitorInfoProviderMac();
+
+ // Overridden from MonitorInfoProvider:
+ virtual gfx::Rect GetPrimaryMonitorWorkArea() const OVERRIDE;
+ virtual gfx::Rect GetPrimaryMonitorBounds() const OVERRIDE;
+ virtual gfx::Rect GetMonitorWorkAreaMatching(
+ const gfx::Rect& match_rect) const OVERRIDE;
+ virtual void UpdateWorkAreas() OVERRIDE;
+
+ private:
+ // Returns a pointer to the screen that most closely matches the
+ // given |match_rect|. This function currently returns the screen
+ // that contains the largest amount of |match_rect| by area.
+ NSScreen* GetMatchingScreen(const gfx::Rect& match_rect) const;
+
+ // The lower left corner of screen 0 is always at (0, 0). The
+ // cross-platform code expects the origin to be in the upper left,
+ // so we have to translate |ns_rect| to the new coordinate
+ // system.
+ gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) const;
+
+ DISALLOW_COPY_AND_ASSIGN(MonitorInfoProviderMac);
+};
+
+MonitorInfoProviderMac::MonitorInfoProviderMac() {
+}
+
+MonitorInfoProviderMac::~MonitorInfoProviderMac() {
+}
+
+gfx::Rect MonitorInfoProviderMac::GetPrimaryMonitorWorkArea() const {
+ // Primary monitor is defined as the monitor with the menubar,
+ // which is always at index 0.
+ NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
+ NSRect frame = [primary frame];
+ NSRect visible_frame = [primary visibleFrame];
+
+ // Convert coordinate systems.
+ gfx::Rect rect = gfx::Rect(NSRectToCGRect(visible_frame));
+ rect.set_y(frame.size.height -
+ visible_frame.origin.y - visible_frame.size.height);
+ return rect;
+}
+
+gfx::Rect MonitorInfoProviderMac::GetPrimaryMonitorBounds() const {
+ // Primary monitor is defined as the monitor with the menubar,
+ // which is always at index 0.
+ NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
+ return gfx::Rect(NSRectToCGRect([primary frame]));
+}
+
+gfx::Rect MonitorInfoProviderMac::GetMonitorWorkAreaMatching(
+ const gfx::Rect& match_rect) const {
+ NSScreen* match_screen = GetMatchingScreen(match_rect);
+ return ConvertCoordinateSystem([match_screen visibleFrame]);
+}
+
+void MonitorInfoProviderMac::UpdateWorkAreas() {
+ work_areas_.clear();
+
+ for (NSScreen* screen in [NSScreen screens]) {
+ gfx::Rect rect = ConvertCoordinateSystem([screen visibleFrame]);
+ work_areas_.push_back(rect);
+ }
+}
+
+NSScreen* MonitorInfoProviderMac::GetMatchingScreen(
+ const gfx::Rect& match_rect) const {
+ // Default to the monitor with the current keyboard focus, in case
+ // |match_rect| is not on any screen at all.
+ NSScreen* max_screen = [NSScreen mainScreen];
+ int max_area = 0;
+
+ for (NSScreen* screen in [NSScreen screens]) {
+ gfx::Rect monitor_area = ConvertCoordinateSystem([screen frame]);
+ gfx::Rect intersection = monitor_area.Intersect(match_rect);
+ int area = intersection.width() * intersection.height();
+ if (area > max_area) {
+ max_area = area;
+ max_screen = screen;
+ }
+ }
+
+ return max_screen;
+}
+
+gfx::Rect MonitorInfoProviderMac::ConvertCoordinateSystem(
+ NSRect ns_rect) const {
+ // Primary monitor is defined as the monitor with the menubar,
+ // which is always at index 0.
+ NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0];
+ float primary_screen_height = [primary_screen frame].size.height;
+ gfx::Rect rect(NSRectToCGRect(ns_rect));
+ rect.set_y(primary_screen_height - rect.y() - rect.height());
+ return rect;
+}
+
+} // namespace
+
+// static
+MonitorInfoProvider* MonitorInfoProvider::Create() {
+ return new MonitorInfoProviderMac();
+}

Powered by Google App Engine
This is Rietveld 408576698