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.
5 #ifndef CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_H_
6 #define CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/ui/host_desktop.h"
11 #include "ui/base/ui_base_types.h"
12 #include "ui/gfx/rect.h"
21 ///////////////////////////////////////////////////////////////////////////////
24 // A class that determines the best new size and position for a window to be
25 // shown at based several factors, including the position and size of the last
26 // window of the same type, the last saved bounds of the window from the
27 // previous session, and default system metrics if neither of the above two
28 // conditions exist. The system has built-in providers for monitor metrics
29 // and persistent storage (using preferences) but can be overrided with mocks
36 // WindowSizer owns |state_provider| and will create a default
37 // MonitorInfoProvider using the physical screen.
38 WindowSizer(StateProvider* state_provider, const Browser* browser);
40 // WindowSizer owns |state_provider| and will use the supplied |screen|.
41 // Used only for testing.
42 WindowSizer(StateProvider* state_provider,
44 const Browser* browser);
46 virtual ~WindowSizer();
48 // An interface implemented by an object that can retrieve state from either a
49 // persistent store or an existing window.
52 virtual ~StateProvider() {}
54 // Retrieve the persisted bounds of the window. Returns true if there was
55 // persisted data to retrieve state information, false otherwise.
56 // The |show_state| variable will only be touched if there was persisted
57 // data and the |show_state| variable is SHOW_STATE_DEFAULT.
58 virtual bool GetPersistentState(gfx::Rect* bounds,
60 ui::WindowShowState* show_state) const = 0;
62 // Retrieve the bounds of the most recent window of the matching type.
63 // Returns true if there was a last active window to retrieve state
64 // information from, false otherwise.
65 // The |show_state| variable will only be touched if we have found a
66 // suitable window and the |show_state| variable is SHOW_STATE_DEFAULT.
67 virtual bool GetLastActiveWindowState(
69 ui::WindowShowState* show_state) const = 0;
72 // Determines the position and size for a window as it is created as well
73 // as the initial state. This function uses several strategies to figure out
74 // optimal size and placement, first looking for an existing active window,
75 // then falling back to persisted data from a previous session, finally
76 // utilizing a default algorithm. If |specified_bounds| are non-empty, this
77 // value is returned instead. For use only in testing.
78 // |show_state| will be overwritten and return the initial visual state of
80 void DetermineWindowBoundsAndShowState(
81 const gfx::Rect& specified_bounds,
83 ui::WindowShowState* show_state) const;
85 // Determines the size, position and maximized state for the browser window.
86 // See documentation for DetermineWindowBounds above. Normally,
87 // |window_bounds| is calculated by calling GetLastActiveWindowState(). To
88 // explicitly specify a particular window to base the bounds on, pass in a
89 // non-NULL value for |browser|.
90 static void GetBrowserWindowBoundsAndShowState(
91 const std::string& app_name,
92 const gfx::Rect& specified_bounds,
93 const Browser* browser,
94 gfx::Rect* window_bounds,
95 ui::WindowShowState* show_state);
97 // Returns the default origin for popups of the given size.
98 static gfx::Point GetDefaultPopupOrigin(const gfx::Size& size,
99 chrome::HostDesktopType type);
101 // How much horizontal and vertical offset there is between newly
102 // opened windows. This value may be different on each platform.
103 static const int kWindowTilePixels;
106 // The edge of the screen to check for out-of-bounds.
107 enum Edge { TOP, LEFT, BOTTOM, RIGHT };
109 // Gets the size and placement of the last window. Returns true if this data
110 // is valid, false if there is no last window and the application should
111 // restore saved state from preferences using RestoreWindowPosition.
112 // |show_state| will only be changed if it was set to SHOW_STATE_DEFAULT.
113 bool GetLastWindowBounds(gfx::Rect* bounds,
114 ui::WindowShowState* show_state) const;
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 // |show_state| will only be changed if it was set to SHOW_STATE_DEFAULT.
121 bool GetSavedWindowBounds(gfx::Rect* bounds,
122 ui::WindowShowState* show_state) const;
124 // Gets the default window position and size to be shown on
125 // |display| if there is no last window and no saved window
126 // placement in prefs. This function determines the default size
127 // based on monitor size, etc.
128 void GetDefaultWindowBounds(const gfx::Display& display,
129 gfx::Rect* default_bounds) const;
131 // Adjusts |bounds| to be visible on-screen, biased toward the work area of
132 // the monitor containing |other_bounds|. Despite the name, this doesn't
133 // guarantee the bounds are fully contained within this monitor's work rect;
134 // it just tried to ensure the edges are visible on _some_ work rect.
135 // If |saved_work_area| is non-empty, it is used to determine whether the
136 // monitor configuration has changed. If it has, bounds are repositioned and
137 // resized if necessary to make them completely contained in the current work
139 void AdjustBoundsToBeVisibleOnMonitorContaining(
140 const gfx::Rect& other_bounds,
141 const gfx::Rect& saved_work_area,
142 gfx::Rect* bounds) const;
145 // Determines the position and size for a tabbed browser window in
146 // ash as it gets created. This will be called before other standard
147 // placement logic. |show_state| will only be changed
148 // if it was set to SHOW_STATE_DEFAULT.
149 void GetTabbedBrowserBoundsAsh(gfx::Rect* bounds_in_screen,
150 ui::WindowShowState* show_state) const;
153 // Determine the default show state for the window - not looking at other
154 // windows or at persistent information.
155 ui::WindowShowState GetWindowDefaultShowState() const;
158 bool IsTabbedBrowserInAsh() const;
159 bool IsPopupBrowserInAsh() const;
162 // Providers for persistent storage and monitor metrics.
163 scoped_ptr<StateProvider> state_provider_;
164 gfx::Screen* screen_; // not owned.
166 // Note that this browser handle might be NULL.
167 const Browser* browser_;
169 DISALLOW_COPY_AND_ASSIGN(WindowSizer);
172 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_H_