diff options
5 files changed, 105 insertions, 124 deletions
diff --git a/chrome/browser/ui/window_sizer/window_sizer.cc b/chrome/browser/ui/window_sizer/window_sizer.cc index 2300a55..afd2910 100644 --- a/chrome/browser/ui/window_sizer/window_sizer.cc +++ b/chrome/browser/ui/window_sizer/window_sizer.cc @@ -24,26 +24,6 @@ const int kMinVisibleHeight = 30; // Minimum width of the visible part of a window. const int kMinVisibleWidth = 30; -class DefaultMonitorInfoProvider : public MonitorInfoProvider { - public: - explicit DefaultMonitorInfoProvider(const gfx::Screen* screen) - : screen_(screen) {} - // Overridden from MonitorInfoProvider: - virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE { - return screen_->GetPrimaryDisplay().work_area(); - } - virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE { - return screen_->GetPrimaryDisplay().bounds(); - } - virtual gfx::Rect GetMonitorWorkAreaMatching( - const gfx::Rect& match_rect) const OVERRIDE { - return screen_->GetDisplayMatching(match_rect).work_area(); - } - private: - const gfx::Screen* screen_; - DISALLOW_COPY_AND_ASSIGN(DefaultMonitorInfoProvider); -}; - /////////////////////////////////////////////////////////////////////////////// // An implementation of WindowSizer::StateProvider that gets the last active // and persistent state from the browser window and the user's profile. @@ -163,18 +143,18 @@ const int WindowSizer::kMaximumWindowWidth = 1100; WindowSizer::WindowSizer(StateProvider* state_provider, const Browser* browser) : state_provider_(state_provider), - monitor_info_provider_(new DefaultMonitorInfoProvider( - // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312 - gfx::Screen::GetNativeScreen())), + // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312 + screen_(gfx::Screen::GetNativeScreen()), browser_(browser) { } WindowSizer::WindowSizer(StateProvider* state_provider, - MonitorInfoProvider* monitor_info_provider, + gfx::Screen* screen, const Browser* browser) : state_provider_(state_provider), - monitor_info_provider_(monitor_info_provider), + screen_(screen), browser_(browser) { + DCHECK(screen_); } WindowSizer::~WindowSizer() { @@ -230,8 +210,7 @@ void WindowSizer::DetermineWindowBoundsAndShowState( // of the anchor window. Note: AdjustBoundsToBeVisibleOnMonitorContaining // does not exactly what we want: It makes only sure that "a minimal part" // is visible on the screen. - gfx::Rect work_area = - monitor_info_provider_->GetMonitorWorkAreaMatching(*bounds); + gfx::Rect work_area = screen_->GetDisplayMatching(*bounds).work_area(); // Resize so that it fits. bounds->AdjustToFit(work_area); } @@ -275,9 +254,8 @@ void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const { } #endif DCHECK(default_bounds); - DCHECK(monitor_info_provider_.get()); - gfx::Rect work_area = monitor_info_provider_->GetPrimaryDisplayWorkArea(); + gfx::Rect work_area = screen_->GetPrimaryDisplay().work_area(); // The default size is either some reasonably wide width, or if the work // area is narrower, then the work area width less some aesthetic padding. @@ -286,7 +264,7 @@ void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const { // For wider aspect ratio displays at higher resolutions, we might size the // window narrower to allow two windows to easily be placed side-by-side. - gfx::Rect screen_size = monitor_info_provider_->GetPrimaryDisplayBounds(); + gfx::Rect screen_size = screen_->GetPrimaryDisplay().bounds(); double width_to_height = static_cast<double>(screen_size.width()) / screen_size.height(); @@ -312,12 +290,10 @@ void WindowSizer::AdjustBoundsToBeVisibleOnMonitorContaining( const gfx::Rect& saved_work_area, gfx::Rect* bounds) const { DCHECK(bounds); - DCHECK(monitor_info_provider_.get()); // Find the size of the work area of the monitor that intersects the bounds // of the anchor window. - gfx::Rect work_area = - monitor_info_provider_->GetMonitorWorkAreaMatching(other_bounds); + gfx::Rect work_area = screen_->GetDisplayMatching(other_bounds).work_area(); // If height or width are 0, reset to the default size. gfx::Rect default_bounds; diff --git a/chrome/browser/ui/window_sizer/window_sizer.h b/chrome/browser/ui/window_sizer/window_sizer.h index 391bdbc..7648104 100644 --- a/chrome/browser/ui/window_sizer/window_sizer.h +++ b/chrome/browser/ui/window_sizer/window_sizer.h @@ -13,23 +13,9 @@ class Browser; -// An interface implemented by an object that can retrieve information about -// the monitors on the system. -class MonitorInfoProvider { - public: - virtual ~MonitorInfoProvider() {} - - // Returns the bounds of the work area of the primary monitor. - virtual gfx::Rect GetPrimaryDisplayWorkArea() const = 0; - - // Returns the bounds of the primary monitor. - virtual gfx::Rect GetPrimaryDisplayBounds() const = 0; - - // Returns the bounds of the work area of the monitor that most closely - // intersects the provided bounds. - virtual gfx::Rect GetMonitorWorkAreaMatching( - const gfx::Rect& match_rect) const = 0; -}; +namespace gfx { +class Screen; +} /////////////////////////////////////////////////////////////////////////////// // WindowSizer @@ -50,10 +36,10 @@ class WindowSizer { // MonitorInfoProvider using the physical screen. WindowSizer(StateProvider* state_provider, const Browser* browser); - // WindowSizer owns |state_provider| and |monitor_info_provider|. - // It will use the supplied monitor info provider. Used only for testing. + // WindowSizer owns |state_provider| and will use the supplied |screen|. + // Used only for testing. WindowSizer(StateProvider* state_provider, - MonitorInfoProvider* monitor_info_provider, + gfx::Screen* screen, const Browser* browser); virtual ~WindowSizer(); @@ -185,7 +171,7 @@ class WindowSizer { // Providers for persistent storage and monitor metrics. scoped_ptr<StateProvider> state_provider_; - scoped_ptr<MonitorInfoProvider> monitor_info_provider_; + gfx::Screen* screen_; // not owned. // Note that this browser handle might be NULL. const Browser* browser_; diff --git a/chrome/browser/ui/window_sizer/window_sizer_ash.cc b/chrome/browser/ui/window_sizer/window_sizer_ash.cc index a4ee1d1..144cd07 100644 --- a/chrome/browser/ui/window_sizer/window_sizer_ash.cc +++ b/chrome/browser/ui/window_sizer/window_sizer_ash.cc @@ -182,7 +182,7 @@ bool WindowSizer::GetBoundsOverrideAsh(gfx::Rect* bounds_in_screen, // Always open new window in the active display. gfx::Rect active_area = active->GetBoundsInScreen(); gfx::Rect work_area = - monitor_info_provider_->GetMonitorWorkAreaMatching(active_area); + screen_->GetDisplayMatching(active_area).work_area(); // This is a window / app. See if there is no window and try to place it. int count = GetNumberOfValidTopLevelBrowserWindows(work_area); @@ -238,9 +238,8 @@ bool WindowSizer::GetBoundsOverrideAsh(gfx::Rect* bounds_in_screen, void WindowSizer::GetDefaultWindowBoundsAsh(gfx::Rect* default_bounds) const { DCHECK(default_bounds); - DCHECK(monitor_info_provider_.get()); - gfx::Rect work_area = monitor_info_provider_->GetPrimaryDisplayWorkArea(); + gfx::Rect work_area = screen_->GetPrimaryDisplay().work_area(); // There should be a 'desktop' border around the window at the left and right // side. diff --git a/chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc b/chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc index 954f89e..6f4bd8d 100644 --- a/chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc +++ b/chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc @@ -10,49 +10,95 @@ #include "chrome/common/chrome_switches.h" #include "chrome/test/base/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" +#include "ui/gfx/display.h" +#include "ui/gfx/screen.h" -TestMonitorInfoProvider::TestMonitorInfoProvider() {}; +namespace { -TestMonitorInfoProvider::~TestMonitorInfoProvider() {}; +class TestScreen : public gfx::Screen { + public: + TestScreen() {} + virtual ~TestScreen() {} -void TestMonitorInfoProvider::AddMonitor(const gfx::Rect& bounds, - const gfx::Rect& work_area) { - DCHECK(bounds.Contains(work_area)); - monitor_bounds_.push_back(bounds); - work_areas_.push_back(work_area); -} + // Overridden from gfx::Screen: + virtual bool IsDIPEnabled() OVERRIDE { + NOTREACHED(); + return false; + } -// Overridden from WindowSizer::MonitorInfoProvider: -gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayWorkArea() const { - return work_areas_[0]; -} + virtual gfx::Point GetCursorScreenPoint() OVERRIDE { + NOTREACHED(); + return gfx::Point(); + } -gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayBounds() const { - return monitor_bounds_[0]; -} + virtual gfx::NativeWindow GetWindowAtCursorScreenPoint() OVERRIDE { + NOTREACHED(); + return NULL; + } -gfx::Rect TestMonitorInfoProvider::GetMonitorWorkAreaMatching( - const gfx::Rect& match_rect) const { - return work_areas_[GetMonitorIndexMatchingBounds(match_rect)]; -} + virtual int GetNumDisplays() const OVERRIDE { + return displays_.size(); + } + + virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE { + return displays_; + } + + virtual gfx::Display GetDisplayNearestWindow( + gfx::NativeView view) const OVERRIDE { + NOTREACHED(); + return gfx::Display(); + } + + virtual gfx::Display GetDisplayNearestPoint( + const gfx::Point& point) const OVERRIDE { + NOTREACHED(); + return gfx::Display(); + } -size_t TestMonitorInfoProvider::GetMonitorIndexMatchingBounds( - const gfx::Rect& match_rect) const { - int max_area = 0; - size_t max_area_index = 0; - // Loop through all the monitors, finding the one that intersects the - // largest area of the supplied match rect. - for (size_t i = 0; i < work_areas_.size(); ++i) { - gfx::Rect overlap = work_areas_[i]; - overlap.Intersect(match_rect); - int area = overlap.width() * overlap.height(); - if (area > max_area) { - max_area = area; - max_area_index = i; + virtual gfx::Display GetDisplayMatching( + const gfx::Rect& match_rect) const OVERRIDE { + int max_area = 0; + size_t max_area_index = 0; + + for (size_t i = 0; i < displays_.size(); ++i) { + gfx::Rect overlap = displays_[i].bounds(); + overlap.Intersect(match_rect); + int area = overlap.width() * overlap.height(); + if (area > max_area) { + max_area = area; + max_area_index = i; + } } + return displays_[max_area_index]; } - return max_area_index; -} + + virtual gfx::Display GetPrimaryDisplay() const OVERRIDE { + return displays_[0]; + } + + virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE { + NOTREACHED(); + } + + virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE { + NOTREACHED(); + } + + void AddDisplay(const gfx::Rect& bounds, + const gfx::Rect& work_area) { + gfx::Display display(displays_.size(), bounds); + display.set_work_area(work_area); + displays_.push_back(display); + } + + private: + std::vector<gfx::Display> displays_; + + DISALLOW_COPY_AND_ASSIGN(TestScreen); +}; + +} // namespace TestStateProvider::TestStateProvider(): has_persistent_data_(false), @@ -118,17 +164,17 @@ void GetWindowBoundsAndShowState( gfx::Rect* out_bounds, ui::WindowShowState* out_show_state) { DCHECK(out_show_state); - TestMonitorInfoProvider* mip = new TestMonitorInfoProvider; - mip->AddMonitor(monitor1_bounds, monitor1_work_area); + TestScreen test_screen; + test_screen.AddDisplay(monitor1_bounds, monitor1_work_area); if (!monitor2_bounds.IsEmpty()) - mip->AddMonitor(monitor2_bounds, monitor2_bounds); + test_screen.AddDisplay(monitor2_bounds, monitor2_bounds); TestStateProvider* sp = new TestStateProvider; if (source == PERSISTED || source == BOTH) sp->SetPersistentState(bounds, work_area, show_state_persisted, true); if (source == LAST_ACTIVE || source == BOTH) sp->SetLastActiveState(bounds, show_state_last, true); - WindowSizer sizer(sp, mip, browser); + WindowSizer sizer(sp, &test_screen, browser); sizer.DetermineWindowBoundsAndShowState(passed_in, out_bounds, out_show_state); @@ -158,15 +204,15 @@ ui::WindowShowState GetWindowShowState( const gfx::Rect& display_config) { gfx::Rect bounds = display_config; gfx::Rect work_area = display_config; - TestMonitorInfoProvider* mip = new TestMonitorInfoProvider; - mip->AddMonitor(display_config, display_config); + TestScreen test_screen; + test_screen.AddDisplay(display_config, display_config); TestStateProvider* sp = new TestStateProvider; if (source == PERSISTED || source == BOTH) sp->SetPersistentState(bounds, work_area, show_state_persisted, true); if (source == LAST_ACTIVE || source == BOTH) sp->SetLastActiveState(bounds, show_state_last, true); - WindowSizer sizer(sp, mip, browser); + WindowSizer sizer(sp, &test_screen, browser); ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT; gfx::Rect out_bounds; diff --git a/chrome/browser/ui/window_sizer/window_sizer_common_unittest.h b/chrome/browser/ui/window_sizer/window_sizer_common_unittest.h index 5ad48a9..efab710 100644 --- a/chrome/browser/ui/window_sizer/window_sizer_common_unittest.h +++ b/chrome/browser/ui/window_sizer/window_sizer_common_unittest.h @@ -48,32 +48,6 @@ static const gfx::Rect taskbar_right_work_area(0, 0, 917, 768); extern int kWindowTilePixels; -// Testing implementation of WindowSizer::MonitorInfoProvider that we can use -// to fake various monitor layouts and sizes. -class TestMonitorInfoProvider : public MonitorInfoProvider { - public: - TestMonitorInfoProvider(); - virtual ~TestMonitorInfoProvider(); - - void AddMonitor(const gfx::Rect& bounds, const gfx::Rect& work_area); - - // Overridden from WindowSizer::MonitorInfoProvider: - virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE; - - virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE; - - virtual gfx::Rect GetMonitorWorkAreaMatching( - const gfx::Rect& match_rect) const OVERRIDE; - - private: - size_t GetMonitorIndexMatchingBounds(const gfx::Rect& match_rect) const; - - std::vector<gfx::Rect> monitor_bounds_; - std::vector<gfx::Rect> work_areas_; - - DISALLOW_COPY_AND_ASSIGN(TestMonitorInfoProvider); -}; - // Testing implementation of WindowSizer::StateProvider that we use to fake // persistent storage and existing windows. class TestStateProvider : public WindowSizer::StateProvider { |