diff options
author | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-16 22:34:11 +0000 |
---|---|---|
committer | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-16 22:34:11 +0000 |
commit | fb4d2aabcd801c55f46769223bd5770d2598c19e (patch) | |
tree | d8e3e98525b67427e1897a1c5593faa13b052cff /chrome | |
parent | 434c21e053a20e5d417125a62f6f9de31258a8ce (diff) | |
download | chromium_src-fb4d2aabcd801c55f46769223bd5770d2598c19e.zip chromium_src-fb4d2aabcd801c55f46769223bd5770d2598c19e.tar.gz chromium_src-fb4d2aabcd801c55f46769223bd5770d2598c19e.tar.bz2 |
Adds window position save/restore support for multiple monitors on Mac.
Fixes a bug where windows would creep up the screen every time you quit/restarted.
BUG=http://crbug.com/16176
BUG=http://crbug.com/12854
TEST=Windows should save/restore in the correct location, with either a single
monitor or multiple monitors.
Review URL: http://codereview.chromium.org/155647
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20911 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/cocoa/browser_window_controller.mm | 13 | ||||
-rw-r--r-- | chrome/browser/window_sizer_mac.mm | 64 |
2 files changed, 59 insertions, 18 deletions
diff --git a/chrome/browser/cocoa/browser_window_controller.mm b/chrome/browser/cocoa/browser_window_controller.mm index 7115e1f..f43724c 100644 --- a/chrome/browser/cocoa/browser_window_controller.mm +++ b/chrome/browser/cocoa/browser_window_controller.mm @@ -892,18 +892,13 @@ willPositionSheet:(NSWindow*)sheet } - (void)saveWindowPositionToPrefs:(PrefService*)prefs { - // Window placements are stored relative to the work area bounds, - // not the monitor bounds. - NSRect workFrame = [[[self window] screen] visibleFrame]; + // Window positions are stored relative to the origin of the primary monitor. + NSRect monitorFrame = [[[NSScreen screens] objectAtIndex:0] frame]; // Start with the window's frame, which is in virtual coordinates. - // Subtract the origin of the visibleFrame to get the window frame - // relative to the work area. - gfx::Rect bounds(NSRectToCGRect([[self window] frame])); - bounds.Offset(-workFrame.origin.x, -workFrame.origin.y); - // Do some y twiddling to flip the coordinate system. - bounds.set_y(workFrame.size.height - bounds.y() - bounds.height()); + gfx::Rect bounds(NSRectToCGRect([[self window] frame])); + bounds.set_y(monitorFrame.size.height - bounds.y() - bounds.height()); DictionaryValue* windowPreferences = prefs->GetMutableDictionary( browser_->GetWindowPlacementKey().c_str()); 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 |