diff options
Diffstat (limited to 'chrome/browser/window_sizer_mac.mm')
-rw-r--r-- | chrome/browser/window_sizer_mac.mm | 64 |
1 files changed, 55 insertions, 9 deletions
diff --git a/chrome/browser/window_sizer_mac.mm b/chrome/browser/window_sizer_mac.mm index ccfc627..aa91211 100644 --- a/chrome/browser/window_sizer_mac.mm +++ b/chrome/browser/window_sizer_mac.mm @@ -40,26 +40,72 @@ class DefaultMonitorInfoProvider : public WindowSizer::MonitorInfoProvider { virtual gfx::Rect GetMonitorWorkAreaMatching( const gfx::Rect& match_rect) const { - // TODO(rohitrao): Support multiple monitors. - return GetPrimaryMonitorWorkArea(); + NSScreen* match_screen = GetMatchingScreen(match_rect); + return ConvertCoordinateSystem([match_screen visibleFrame]); } virtual gfx::Point GetBoundsOffsetMatching( const gfx::Rect& match_rect) const { - // TODO(rohitrao): Support multiple monitors. - return GetPrimaryMonitorWorkArea().origin(); + NSScreen* match_screen = GetMatchingScreen(match_rect); + gfx::Rect bounds = ConvertCoordinateSystem([match_screen frame]); + gfx::Rect work_area = ConvertCoordinateSystem([match_screen visibleFrame]); + return gfx::Point(work_area.x() - bounds.x(), work_area.y() - bounds.y()); } - void UpdateWorkAreas() { - // TODO(rohitrao): Support multiple monitors. - work_areas_.clear(); - work_areas_.push_back(GetPrimaryMonitorWorkArea()); - } + virtual void UpdateWorkAreas(); 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 { + // 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; + } + + // 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(DefaultMonitorInfoProvider); }; +void DefaultMonitorInfoProvider::UpdateWorkAreas() { + work_areas_.clear(); + + for (NSScreen* screen in [NSScreen screens]) { + gfx::Rect rect = ConvertCoordinateSystem([screen visibleFrame]); + work_areas_.push_back(rect); + } +} + +gfx::Rect DefaultMonitorInfoProvider::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 |