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 #include "chrome/browser/ui/window_sizer/window_sizer.h"
7 #include "base/command_line.h"
8 #include "base/compiler_specific.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_list.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/browser_window_state.h"
16 #include "chrome/browser/ui/host_desktop.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "ui/gfx/screen.h"
22 #include "ash/shell.h"
23 #include "ash/wm/window_positioner.h"
24 #include "chrome/browser/ui/ash/ash_init.h"
29 // Minimum height of the visible part of a window.
30 const int kMinVisibleHeight
= 30;
31 // Minimum width of the visible part of a window.
32 const int kMinVisibleWidth
= 30;
34 ///////////////////////////////////////////////////////////////////////////////
35 // An implementation of WindowSizer::StateProvider that gets the last active
36 // and persistent state from the browser window and the user's profile.
37 class DefaultStateProvider
: public WindowSizer::StateProvider
{
39 DefaultStateProvider(const std::string
& app_name
, const Browser
* browser
)
40 : app_name_(app_name
), browser_(browser
) {
43 // Overridden from WindowSizer::StateProvider:
44 virtual bool GetPersistentState(
47 ui::WindowShowState
* show_state
) const OVERRIDE
{
51 if (!browser_
|| !browser_
->profile()->GetPrefs())
54 std::string
window_name(chrome::GetWindowPlacementKey(browser_
));
55 const base::DictionaryValue
* wp_pref
=
56 browser_
->profile()->GetPrefs()->GetDictionary(window_name
.c_str());
57 int top
= 0, left
= 0, bottom
= 0, right
= 0;
58 bool maximized
= false;
59 bool has_prefs
= wp_pref
&&
60 wp_pref
->GetInteger("top", &top
) &&
61 wp_pref
->GetInteger("left", &left
) &&
62 wp_pref
->GetInteger("bottom", &bottom
) &&
63 wp_pref
->GetInteger("right", &right
) &&
64 wp_pref
->GetBoolean("maximized", &maximized
);
65 bounds
->SetRect(left
, top
, std::max(0, right
- left
),
66 std::max(0, bottom
- top
));
68 int work_area_top
= 0;
69 int work_area_left
= 0;
70 int work_area_bottom
= 0;
71 int work_area_right
= 0;
73 wp_pref
->GetInteger("work_area_top", &work_area_top
);
74 wp_pref
->GetInteger("work_area_left", &work_area_left
);
75 wp_pref
->GetInteger("work_area_bottom", &work_area_bottom
);
76 wp_pref
->GetInteger("work_area_right", &work_area_right
);
77 if (*show_state
== ui::SHOW_STATE_DEFAULT
&& maximized
)
78 *show_state
= ui::SHOW_STATE_MAXIMIZED
;
80 work_area
->SetRect(work_area_left
, work_area_top
,
81 std::max(0, work_area_right
- work_area_left
),
82 std::max(0, work_area_bottom
- work_area_top
));
87 virtual bool GetLastActiveWindowState(
89 ui::WindowShowState
* show_state
) const OVERRIDE
{
91 // Applications are always restored with the same position.
92 if (!app_name_
.empty())
95 // If a reference browser is set, use its window. Otherwise find last
96 // active. Panels are never used as reference browsers as panels are
97 // specially positioned.
98 BrowserWindow
* window
= NULL
;
99 // Window may be null if browser is just starting up.
100 if (browser_
&& browser_
->window()) {
101 window
= browser_
->window();
103 // This code is only ran on the native desktop (on the ash
104 // desktop, GetTabbedBrowserBoundsAsh should take over below
105 // before this is reached). TODO(gab): This code should go in a
106 // native desktop specific window sizer as part of fixing
108 const BrowserList
* native_browser_list
=
109 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
);
110 for (BrowserList::const_reverse_iterator it
=
111 native_browser_list
->begin_last_active();
112 it
!= native_browser_list
->end_last_active(); ++it
) {
113 Browser
* last_active
= *it
;
114 if (last_active
&& last_active
->is_type_tabbed()) {
115 window
= last_active
->window();
123 *bounds
= window
->GetRestoredBounds();
124 if (*show_state
== ui::SHOW_STATE_DEFAULT
&& window
->IsMaximized())
125 *show_state
= ui::SHOW_STATE_MAXIMIZED
;
133 std::string app_name_
;
135 // If set, is used as the reference browser for GetLastActiveWindowState.
136 const Browser
* browser_
;
137 DISALLOW_COPY_AND_ASSIGN(DefaultStateProvider
);
140 class DefaultTargetDisplayProvider
: public WindowSizer::TargetDisplayProvider
{
142 DefaultTargetDisplayProvider() {}
143 virtual ~DefaultTargetDisplayProvider() {}
145 virtual gfx::Display
GetTargetDisplay(
146 const gfx::Screen
* screen
,
147 const gfx::Rect
& bounds
) const OVERRIDE
{
149 bool force_ash
= false;
150 // On Windows check if the browser is launched to serve ASH. If yes then
151 // we should get the display for the corresponding root window created for
152 // ASH. This ensures that the display gets the correct workarea, etc.
153 // If the ASH shell does not exist then the current behavior is to open
154 // browser windows if any on the desktop. Preserve that for now.
156 // This effectively means that the running browser process is in a split
157 // personality mode, part of it running in ASH and the other running in
158 // desktop. This may cause apps and other widgets to not work correctly.
159 // Revisit and address.
161 force_ash
= ash::Shell::HasInstance() &&
162 CommandLine::ForCurrentProcess()->HasSwitch(switches::kViewerConnect
);
164 // Use the target display on ash.
165 if (chrome::ShouldOpenAshOnStartup() || force_ash
) {
166 aura::Window
* target
= ash::Shell::GetTargetRootWindow();
167 return screen
->GetDisplayNearestWindow(target
);
170 // Find the size of the work area of the monitor that intersects the bounds
171 // of the anchor window.
172 return screen
->GetDisplayMatching(bounds
);
176 DISALLOW_COPY_AND_ASSIGN(DefaultTargetDisplayProvider
);
181 ///////////////////////////////////////////////////////////////////////////////
182 // WindowSizer, public:
184 WindowSizer::WindowSizer(
185 scoped_ptr
<StateProvider
> state_provider
,
186 scoped_ptr
<TargetDisplayProvider
> target_display_provider
,
187 const Browser
* browser
)
188 : state_provider_(state_provider
.Pass()),
189 target_display_provider_(target_display_provider
.Pass()),
190 // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312
191 screen_(gfx::Screen::GetNativeScreen()),
195 WindowSizer::WindowSizer(
196 scoped_ptr
<StateProvider
> state_provider
,
197 scoped_ptr
<TargetDisplayProvider
> target_display_provider
,
199 const Browser
* browser
)
200 : state_provider_(state_provider
.Pass()),
201 target_display_provider_(target_display_provider
.Pass()),
207 WindowSizer::~WindowSizer() {
211 void WindowSizer::GetBrowserWindowBoundsAndShowState(
212 const std::string
& app_name
,
213 const gfx::Rect
& specified_bounds
,
214 const Browser
* browser
,
215 gfx::Rect
* window_bounds
,
216 ui::WindowShowState
* show_state
) {
217 scoped_ptr
<StateProvider
> state_provider(
218 new DefaultStateProvider(app_name
, browser
));
219 scoped_ptr
<TargetDisplayProvider
> target_display_provider(
220 new DefaultTargetDisplayProvider
);
221 const WindowSizer
sizer(state_provider
.Pass(),
222 target_display_provider
.Pass(),
224 sizer
.DetermineWindowBoundsAndShowState(specified_bounds
,
229 ///////////////////////////////////////////////////////////////////////////////
230 // WindowSizer, private:
232 void WindowSizer::DetermineWindowBoundsAndShowState(
233 const gfx::Rect
& specified_bounds
,
235 ui::WindowShowState
* show_state
) const {
238 // Pre-populate the window state with our default.
239 *show_state
= GetWindowDefaultShowState();
240 *bounds
= specified_bounds
;
243 // See if ash should decide the window placement.
244 if (GetBrowserBoundsAsh(bounds
, show_state
))
248 if (bounds
->IsEmpty()) {
249 // See if there's last active window's placement information.
250 if (GetLastActiveWindowBounds(bounds
, show_state
))
252 // See if there's saved placement information.
253 if (GetSavedWindowBounds(bounds
, show_state
))
256 // No saved placement, figure out some sensible default size based on
257 // the user's screen size.
258 GetDefaultWindowBounds(GetTargetDisplay(gfx::Rect()), bounds
);
262 // In case that there was a bound given we need to make sure that it is
263 // visible and fits on the screen.
264 // Find the size of the work area of the monitor that intersects the bounds
265 // of the anchor window. Note: AdjustBoundsToBeVisibleOnMonitorContaining
266 // does not exactly what we want: It makes only sure that "a minimal part"
267 // is visible on the screen.
268 gfx::Rect work_area
= screen_
->GetDisplayMatching(*bounds
).work_area();
269 // Resize so that it fits.
270 bounds
->AdjustToFit(work_area
);
273 bool WindowSizer::GetLastActiveWindowBounds(
275 ui::WindowShowState
* show_state
) const {
278 if (!state_provider_
.get() ||
279 !state_provider_
->GetLastActiveWindowState(bounds
, show_state
))
281 gfx::Rect last_window_bounds
= *bounds
;
282 bounds
->Offset(kWindowTilePixels
, kWindowTilePixels
);
283 AdjustBoundsToBeVisibleOnDisplay(screen_
->GetDisplayMatching(*bounds
),
289 bool WindowSizer::GetSavedWindowBounds(gfx::Rect
* bounds
,
290 ui::WindowShowState
* show_state
) const {
293 gfx::Rect saved_work_area
;
294 if (!state_provider_
.get() ||
295 !state_provider_
->GetPersistentState(bounds
,
299 AdjustBoundsToBeVisibleOnDisplay(GetTargetDisplay(*bounds
),
305 void WindowSizer::GetDefaultWindowBounds(const gfx::Display
& display
,
306 gfx::Rect
* default_bounds
) const {
307 DCHECK(default_bounds
);
309 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312
310 if (chrome::ShouldOpenAshOnStartup()) {
311 *default_bounds
= ash::WindowPositioner::GetDefaultWindowBounds(display
);
315 gfx::Rect work_area
= display
.work_area();
317 // The default size is either some reasonably wide width, or if the work
318 // area is narrower, then the work area width less some aesthetic padding.
319 int default_width
= std::min(work_area
.width() - 2 * kWindowTilePixels
, 1050);
320 int default_height
= work_area
.height() - 2 * kWindowTilePixels
;
322 // For wider aspect ratio displays at higher resolutions, we might size the
323 // window narrower to allow two windows to easily be placed side-by-side.
324 gfx::Rect screen_size
= screen_
->GetPrimaryDisplay().bounds();
325 double width_to_height
=
326 static_cast<double>(screen_size
.width()) / screen_size
.height();
328 // The least wide a screen can be to qualify for the halving described above.
329 static const int kMinScreenWidthForWindowHalving
= 1600;
330 // We assume 16:9/10 is a fairly standard indicator of a wide aspect ratio
332 if (((width_to_height
* 10) >= 16) &&
333 work_area
.width() > kMinScreenWidthForWindowHalving
) {
334 // Halve the work area, subtracting aesthetic padding on either side.
335 // The padding is set so that two windows, side by side have
336 // kWindowTilePixels between screen edge and each other.
337 default_width
= static_cast<int>(work_area
.width() / 2. -
338 1.5 * kWindowTilePixels
);
340 default_bounds
->SetRect(kWindowTilePixels
+ work_area
.x(),
341 kWindowTilePixels
+ work_area
.y(),
342 default_width
, default_height
);
345 void WindowSizer::AdjustBoundsToBeVisibleOnDisplay(
346 const gfx::Display
& display
,
347 const gfx::Rect
& saved_work_area
,
348 gfx::Rect
* bounds
) const {
351 // If |bounds| is empty, reset to the default size.
352 if (bounds
->IsEmpty()) {
353 gfx::Rect default_bounds
;
354 GetDefaultWindowBounds(display
, &default_bounds
);
355 if (bounds
->height() <= 0)
356 bounds
->set_height(default_bounds
.height());
357 if (bounds
->width() <= 0)
358 bounds
->set_width(default_bounds
.width());
361 // Ensure the minimum height and width.
362 bounds
->set_height(std::max(kMinVisibleHeight
, bounds
->height()));
363 bounds
->set_width(std::max(kMinVisibleWidth
, bounds
->width()));
365 gfx::Rect work_area
= display
.work_area();
366 // Ensure that the title bar is not above the work area.
367 if (bounds
->y() < work_area
.y())
368 bounds
->set_y(work_area
.y());
370 // Reposition and resize the bounds if the saved_work_area is different from
371 // the current work area and the current work area doesn't completely contain
373 if (!saved_work_area
.IsEmpty() &&
374 saved_work_area
!= work_area
&&
375 !work_area
.Contains(*bounds
)) {
376 bounds
->set_width(std::min(bounds
->width(), work_area
.width()));
377 bounds
->set_height(std::min(bounds
->height(), work_area
.height()));
379 std::max(work_area
.x(),
380 std::min(bounds
->x(), work_area
.right() - bounds
->width())));
382 std::max(work_area
.y(),
383 std::min(bounds
->y(), work_area
.bottom() - bounds
->height())));
386 #if defined(OS_MACOSX)
387 // Limit the maximum height. On the Mac the sizer is on the
388 // bottom-right of the window, and a window cannot be moved "up"
389 // past the menubar. If the window is too tall you'll never be able
390 // to shrink it again. Windows does not have this limitation
391 // (e.g. can be resized from the top).
392 bounds
->set_height(std::min(work_area
.height(), bounds
->height()));
394 // On mac, we want to be aggressive about repositioning windows that are
395 // partially offscreen. If the window is partially offscreen horizontally,
396 // move it to be flush with the left edge of the work area.
397 if (bounds
->x() < work_area
.x() || bounds
->right() > work_area
.right())
398 bounds
->set_x(work_area
.x());
400 // If the window is partially offscreen vertically, move it to be flush with
401 // the top of the work area.
402 if (bounds
->y() < work_area
.y() || bounds
->bottom() > work_area
.bottom())
403 bounds
->set_y(work_area
.y());
405 // On non-Mac platforms, we are less aggressive about repositioning. Simply
406 // ensure that at least kMinVisibleWidth * kMinVisibleHeight is visible.
407 const int min_y
= work_area
.y() + kMinVisibleHeight
- bounds
->height();
408 const int min_x
= work_area
.x() + kMinVisibleWidth
- bounds
->width();
409 const int max_y
= work_area
.bottom() - kMinVisibleHeight
;
410 const int max_x
= work_area
.right() - kMinVisibleWidth
;
411 bounds
->set_y(std::max(min_y
, std::min(max_y
, bounds
->y())));
412 bounds
->set_x(std::max(min_x
, std::min(max_x
, bounds
->x())));
413 #endif // defined(OS_MACOSX)
416 gfx::Display
WindowSizer::GetTargetDisplay(const gfx::Rect
& bounds
) const {
417 return target_display_provider_
->GetTargetDisplay(screen_
, bounds
);
420 ui::WindowShowState
WindowSizer::GetWindowDefaultShowState() const {
422 return ui::SHOW_STATE_DEFAULT
;
424 // Only tabbed browsers use the command line or preference state, with the
425 // exception of devtools.
426 bool show_state
= !browser_
->is_type_tabbed() && !browser_
->is_devtools();
428 #if defined(USE_AURA)
429 // We use the apps save state on aura.
430 show_state
&= !browser_
->is_app();
434 return browser_
->initial_show_state();
436 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kStartMaximized
))
437 return ui::SHOW_STATE_MAXIMIZED
;
439 if (browser_
->initial_show_state() != ui::SHOW_STATE_DEFAULT
)
440 return browser_
->initial_show_state();
442 // Otherwise we use the default which can be overridden later on.
443 return ui::SHOW_STATE_DEFAULT
;