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 #ifndef CHROME_BROWSER_UI_WINDOW_SIZER_H_ 6 #define CHROME_BROWSER_UI_WINDOW_SIZER_H_ 7 #pragma once 8 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "ui/gfx/rect.h" 13 14 class Browser; 15 16 /////////////////////////////////////////////////////////////////////////////// 17 // WindowSizer 18 // 19 // A class that determines the best new size and position for a window to be 20 // shown at based several factors, including the position and size of the last 21 // window of the same type, the last saved bounds of the window from the 22 // previous session, and default system metrics if neither of the above two 23 // conditions exist. The system has built-in providers for monitor metrics 24 // and persistent storage (using preferences) but can be overrided with mocks 25 // for testing. 26 // 27 class WindowSizer { 28 public: 29 class MonitorInfoProvider; 30 class StateProvider; 31 32 // The WindowSizer assumes ownership of these objects. 33 WindowSizer(StateProvider* state_provider, 34 MonitorInfoProvider* monitor_info_provider); 35 virtual ~WindowSizer(); 36 37 // Static factory methods to create default MonitorInfoProvider 38 // instances. The returned object is owned by the caller. 39 static MonitorInfoProvider* CreateDefaultMonitorInfoProvider(); 40 41 // An interface implemented by an object that can retrieve information about 42 // the monitors on the system. 43 class MonitorInfoProvider { 44 public: 45 MonitorInfoProvider(); 46 virtual ~MonitorInfoProvider(); 47 48 // Returns the bounds of the work area of the primary monitor. 49 virtual gfx::Rect GetPrimaryMonitorWorkArea() const = 0; 50 51 // Returns the bounds of the primary monitor. 52 virtual gfx::Rect GetPrimaryMonitorBounds() const = 0; 53 54 // Returns the bounds of the work area of the monitor that most closely 55 // intersects the provided bounds. 56 virtual gfx::Rect GetMonitorWorkAreaMatching( 57 const gfx::Rect& match_rect) const = 0; 58 59 // Returns the delta between the work area and the monitor bounds for the 60 // monitor that most closely intersects the provided bounds. 61 virtual gfx::Point GetBoundsOffsetMatching( 62 const gfx::Rect& match_rect) const = 0; 63 64 // Ensures number and coordinates of work areas are up-to-date. You must 65 // call this before calling either of the below functions, as work areas can 66 // change while the program is running. 67 virtual void UpdateWorkAreas() = 0; 68 69 // Returns the number of monitors on the system. 70 size_t GetMonitorCount() const { 71 return work_areas_.size(); 72 } 73 74 // Returns the bounds of the work area of the monitor at the specified 75 // index. 76 gfx::Rect GetWorkAreaAt(size_t index) const { 77 return work_areas_[index]; 78 } 79 80 protected: 81 std::vector<gfx::Rect> work_areas_; 82 }; 83 84 // An interface implemented by an object that can retrieve state from either a 85 // persistent store or an existing window. 86 class StateProvider { 87 public: 88 virtual ~StateProvider() { } 89 90 // Retrieve the persisted bounds of the window. Returns true if there was 91 // persisted data to retrieve state information, false otherwise. 92 virtual bool GetPersistentState(gfx::Rect* bounds, 93 bool* maximized, 94 gfx::Rect* work_area) const = 0; 95 96 // Retrieve the bounds of the most recent window of the matching type. 97 // Returns true if there was a last active window to retrieve state 98 // information from, false otherwise. 99 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const = 0; 100 }; 101 102 // Determines the position, size and maximized state for a window as it is 103 // created. This function uses several strategies to figure out optimal size 104 // and placement, first looking for an existing active window, then falling 105 // back to persisted data from a previous session, finally utilizing a default 106 // algorithm. If |specified_bounds| are non-empty, this value is returned 107 // instead. For use only in testing. 108 // 109 // NOTE: |maximized| is only set if we're restoring a saved maximized window. 110 // When creating a new window based on an existing active window, standard 111 // Windows behavior is to have it always be nonmaximized, even if the existing 112 // window is maximized. 113 void DetermineWindowBounds(const gfx::Rect& specified_bounds, 114 gfx::Rect* bounds, 115 bool* maximized) const; 116 117 // Determines the size, position and maximized state for the browser window. 118 // See documentation for DetermineWindowBounds above. Normally, 119 // |window_bounds| is calculated by calling GetLastActiveWindowState(). To 120 // explicitly specify a particular window to base the bounds on, pass in a 121 // non-NULL value for |browser|. 122 static void GetBrowserWindowBounds(const std::string& app_name, 123 const gfx::Rect& specified_bounds, 124 const Browser* browser, 125 gfx::Rect* window_bounds, 126 bool* maximized); 127 128 // Returns the default origin for popups of the given size. 129 static gfx::Point GetDefaultPopupOrigin(const gfx::Size& size); 130 131 // How much horizontal and vertical offset there is between newly 132 // opened windows. This value may be different on each platform. 133 static const int kWindowTilePixels; 134 135 private: 136 // The edge of the screen to check for out-of-bounds. 137 enum Edge { TOP, LEFT, BOTTOM, RIGHT }; 138 139 explicit WindowSizer(const std::string& app_name); 140 141 void Init(StateProvider* state_provider, 142 MonitorInfoProvider* monitor_info_provider); 143 144 // Gets the size and placement of the last window. Returns true if this data 145 // is valid, false if there is no last window and the application should 146 // restore saved state from preferences using RestoreWindowPosition. 147 bool GetLastWindowBounds(gfx::Rect* bounds) const; 148 149 // Gets the size and placement of the last window in the last session, saved 150 // in local state preferences. Returns true if local state exists containing 151 // this information, false if this information does not exist and a default 152 // size should be used. 153 bool GetSavedWindowBounds(gfx::Rect* bounds, bool* maximized) const; 154 155 // Gets the default window position and size if there is no last window and 156 // no saved window placement in prefs. This function determines the default 157 // size based on monitor size, etc. 158 void GetDefaultWindowBounds(gfx::Rect* default_bounds) const; 159 160 // Returns true if the specified position is "offscreen" for the given edge, 161 // meaning that it's outside all work areas in the direction of that edge. 162 bool PositionIsOffscreen(int position, Edge edge) const; 163 164 // Adjusts |bounds| to be visible onscreen, biased toward the work area of the 165 // monitor containing |other_bounds|. Despite the name, this doesn't 166 // guarantee the bounds are fully contained within this monitor's work rect; 167 // it just tried to ensure the edges are visible on _some_ work rect. 168 // If |saved_work_area| is non-empty, it is used to determine whether the 169 // monitor cofiguration has changed. If it has, bounds are repositioned and 170 // resized if necessary to make them completely contained in the current work 171 // area. 172 void AdjustBoundsToBeVisibleOnMonitorContaining( 173 const gfx::Rect& other_bounds, 174 const gfx::Rect& saved_work_area, 175 gfx::Rect* bounds) const; 176 177 // Providers for persistent storage and monitor metrics. 178 StateProvider* state_provider_; 179 MonitorInfoProvider* monitor_info_provider_; 180 181 DISALLOW_COPY_AND_ASSIGN(WindowSizer); 182 }; 183 184 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_H_ 185 186