| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "base/basictypes.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "ui/gfx/rect.h" | |
| 12 | |
| 13 class Browser; | |
| 14 | |
| 15 // An interface implemented by an object that can retrieve information about | |
| 16 // the monitors on the system. | |
| 17 class MonitorInfoProvider { | |
| 18 public: | |
| 19 virtual ~MonitorInfoProvider() {} | |
| 20 | |
| 21 // Returns the bounds of the work area of the primary monitor. | |
| 22 virtual gfx::Rect GetPrimaryDisplayWorkArea() const = 0; | |
| 23 | |
| 24 // Returns the bounds of the primary monitor. | |
| 25 virtual gfx::Rect GetPrimaryDisplayBounds() const = 0; | |
| 26 | |
| 27 // Returns the bounds of the work area of the monitor that most closely | |
| 28 // intersects the provided bounds. | |
| 29 virtual gfx::Rect GetMonitorWorkAreaMatching( | |
| 30 const gfx::Rect& match_rect) const = 0; | |
| 31 }; | |
| 32 | |
| 33 /////////////////////////////////////////////////////////////////////////////// | |
| 34 // WindowSizer | |
| 35 // | |
| 36 // A class that determines the best new size and position for a window to be | |
| 37 // shown at based several factors, including the position and size of the last | |
| 38 // window of the same type, the last saved bounds of the window from the | |
| 39 // previous session, and default system metrics if neither of the above two | |
| 40 // conditions exist. The system has built-in providers for monitor metrics | |
| 41 // and persistent storage (using preferences) but can be overrided with mocks | |
| 42 // for testing. | |
| 43 // | |
| 44 class WindowSizer { | |
| 45 public: | |
| 46 class StateProvider; | |
| 47 | |
| 48 // WindowSizer owns |state_provider| and will create a default | |
| 49 // MonitorInfoProvider using the physical screen. | |
| 50 WindowSizer(StateProvider* state_provider, const Browser* browser); | |
| 51 | |
| 52 // WindowSizer owns |state_provider| and |monitor_info_provider|. | |
| 53 // It will use the supplied monitor info provider. Used only for testing. | |
| 54 WindowSizer(StateProvider* state_provider, | |
| 55 MonitorInfoProvider* monitor_info_provider, | |
| 56 const Browser* browser); | |
| 57 | |
| 58 virtual ~WindowSizer(); | |
| 59 | |
| 60 // An interface implemented by an object that can retrieve state from either a | |
| 61 // persistent store or an existing window. | |
| 62 class StateProvider { | |
| 63 public: | |
| 64 virtual ~StateProvider() {} | |
| 65 | |
| 66 // Retrieve the persisted bounds of the window. Returns true if there was | |
| 67 // persisted data to retrieve state information, false otherwise. | |
| 68 virtual bool GetPersistentState(gfx::Rect* bounds, | |
| 69 gfx::Rect* work_area) const = 0; | |
| 70 | |
| 71 // Retrieve the bounds of the most recent window of the matching type. | |
| 72 // Returns true if there was a last active window to retrieve state | |
| 73 // information from, false otherwise. | |
| 74 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const = 0; | |
| 75 }; | |
| 76 | |
| 77 // Determines the position and size for a window as it is created. This | |
| 78 // function uses several strategies to figure out optimal size and placement, | |
| 79 // first looking for an existing active window, then falling back to persisted | |
| 80 // data from a previous session, finally utilizing a default | |
| 81 // algorithm. If |specified_bounds| are non-empty, this value is returned | |
| 82 // instead. For use only in testing. | |
| 83 void DetermineWindowBounds(const gfx::Rect& specified_bounds, | |
| 84 gfx::Rect* bounds) const; | |
| 85 | |
| 86 // Determines the size, position and maximized state for the browser window. | |
| 87 // See documentation for DetermineWindowBounds above. Normally, | |
| 88 // |window_bounds| is calculated by calling GetLastActiveWindowState(). To | |
| 89 // explicitly specify a particular window to base the bounds on, pass in a | |
| 90 // non-NULL value for |browser|. | |
| 91 static void GetBrowserWindowBounds(const std::string& app_name, | |
| 92 const gfx::Rect& specified_bounds, | |
| 93 const Browser* browser, | |
| 94 gfx::Rect* window_bounds); | |
| 95 | |
| 96 // Returns the default origin for popups of the given size. | |
| 97 static gfx::Point GetDefaultPopupOrigin(const gfx::Size& size); | |
| 98 | |
| 99 // The number of pixels which are kept free top, left and right when a window | |
| 100 // gets positioned to its default location. | |
| 101 static const int kDesktopBorderSize; | |
| 102 | |
| 103 // How much horizontal and vertical offset there is between newly | |
| 104 // opened windows. This value may be different on each platform. | |
| 105 static const int kWindowTilePixels; | |
| 106 | |
| 107 private: | |
| 108 // The edge of the screen to check for out-of-bounds. | |
| 109 enum Edge { TOP, LEFT, BOTTOM, RIGHT }; | |
| 110 | |
| 111 // Gets the size and placement of the last window. Returns true if this data | |
| 112 // is valid, false if there is no last window and the application should | |
| 113 // restore saved state from preferences using RestoreWindowPosition. | |
| 114 bool GetLastWindowBounds(gfx::Rect* bounds) const; | |
| 115 | |
| 116 // Gets the size and placement of the last window in the last session, saved | |
| 117 // in local state preferences. Returns true if local state exists containing | |
| 118 // this information, false if this information does not exist and a default | |
| 119 // size should be used. | |
| 120 bool GetSavedWindowBounds(gfx::Rect* bounds) const; | |
| 121 | |
| 122 // Gets the default window position and size if there is no last window and | |
| 123 // no saved window placement in prefs. This function determines the default | |
| 124 // size based on monitor size, etc. | |
| 125 void GetDefaultWindowBounds(gfx::Rect* default_bounds) const; | |
| 126 #if defined(USE_ASH) | |
| 127 void GetDefaultWindowBoundsAsh(gfx::Rect* default_bounds) const; | |
| 128 #endif | |
| 129 | |
| 130 // Adjusts |bounds| to be visible on-screen, biased toward the work area of | |
| 131 // the monitor containing |other_bounds|. Despite the name, this doesn't | |
| 132 // guarantee the bounds are fully contained within this monitor's work rect; | |
| 133 // it just tried to ensure the edges are visible on _some_ work rect. | |
| 134 // If |saved_work_area| is non-empty, it is used to determine whether the | |
| 135 // monitor configuration has changed. If it has, bounds are repositioned and | |
| 136 // resized if necessary to make them completely contained in the current work | |
| 137 // area. | |
| 138 void AdjustBoundsToBeVisibleOnMonitorContaining( | |
| 139 const gfx::Rect& other_bounds, | |
| 140 const gfx::Rect& saved_work_area, | |
| 141 gfx::Rect* bounds) const; | |
| 142 | |
| 143 // Determines the position and size for a window as it gets created. This | |
| 144 // will be called before DetermineWindowBounds. It will return true when the | |
| 145 // function was setting the bounds structure to the desired size. Otherwise | |
| 146 // another algorithm should get used to determine the correct bounds. | |
| 147 bool GetBoundsIgnoringPreviousState(const gfx::Rect& specified_bounds, | |
| 148 gfx::Rect* bounds) const; | |
| 149 #if defined(USE_ASH) | |
| 150 bool GetBoundsIgnoringPreviousStateAsh(const gfx::Rect& specified_bounds, | |
| 151 gfx::Rect* bounds) const; | |
| 152 #endif | |
| 153 | |
| 154 // Providers for persistent storage and monitor metrics. | |
| 155 scoped_ptr<StateProvider> state_provider_; | |
| 156 scoped_ptr<MonitorInfoProvider> monitor_info_provider_; | |
| 157 | |
| 158 // Note that this browser handle might be NULL. | |
| 159 const Browser* browser_; | |
| 160 | |
| 161 DISALLOW_COPY_AND_ASSIGN(WindowSizer); | |
| 162 }; | |
| 163 | |
| 164 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_H_ | |
| OLD | NEW |