| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/window_sizer/window_sizer.h" | 5 #include "chrome/browser/ui/window_sizer/window_sizer.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/command_line.h" |
| 8 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/ash/ash_init.h" | 12 #include "chrome/browser/ui/ash/ash_init.h" |
| 12 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
| 14 #include "chrome/browser/ui/browser_window.h" | 15 #include "chrome/browser/ui/browser_window.h" |
| 15 #include "chrome/browser/ui/browser_window_state.h" | 16 #include "chrome/browser/ui/browser_window_state.h" |
| 16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/common/chrome_switches.h" |
| 17 #include "ui/gfx/screen.h" | 19 #include "ui/gfx/screen.h" |
| 18 | 20 |
| 19 // Minimum height of the visible part of a window. | 21 // Minimum height of the visible part of a window. |
| 20 const int kMinVisibleHeight = 30; | 22 const int kMinVisibleHeight = 30; |
| 21 // Minimum width of the visible part of a window. | 23 // Minimum width of the visible part of a window. |
| 22 const int kMinVisibleWidth = 30; | 24 const int kMinVisibleWidth = 30; |
| 23 | 25 |
| 24 class DefaultMonitorInfoProvider : public MonitorInfoProvider { | 26 class DefaultMonitorInfoProvider : public MonitorInfoProvider { |
| 25 public: | 27 public: |
| 26 // Overridden from MonitorInfoProvider: | 28 // Overridden from MonitorInfoProvider: |
| (...skipping 13 matching lines...) Expand all Loading... |
| 40 // An implementation of WindowSizer::StateProvider that gets the last active | 42 // An implementation of WindowSizer::StateProvider that gets the last active |
| 41 // and persistent state from the browser window and the user's profile. | 43 // and persistent state from the browser window and the user's profile. |
| 42 class DefaultStateProvider : public WindowSizer::StateProvider { | 44 class DefaultStateProvider : public WindowSizer::StateProvider { |
| 43 public: | 45 public: |
| 44 DefaultStateProvider(const std::string& app_name, const Browser* browser) | 46 DefaultStateProvider(const std::string& app_name, const Browser* browser) |
| 45 : app_name_(app_name), browser_(browser) { | 47 : app_name_(app_name), browser_(browser) { |
| 46 } | 48 } |
| 47 | 49 |
| 48 // Overridden from WindowSizer::StateProvider: | 50 // Overridden from WindowSizer::StateProvider: |
| 49 virtual bool GetPersistentState(gfx::Rect* bounds, | 51 virtual bool GetPersistentState(gfx::Rect* bounds, |
| 50 gfx::Rect* work_area) const { | 52 gfx::Rect* work_area, |
| 53 ui::WindowShowState* show_state) const { |
| 51 DCHECK(bounds); | 54 DCHECK(bounds); |
| 55 DCHECK(show_state); |
| 52 | 56 |
| 53 if (!browser_ || !browser_->profile()->GetPrefs()) | 57 if (!browser_ || !browser_->profile()->GetPrefs()) |
| 54 return false; | 58 return false; |
| 55 | 59 |
| 56 std::string window_name(chrome::GetWindowPlacementKey(browser_)); | 60 std::string window_name(chrome::GetWindowPlacementKey(browser_)); |
| 57 const DictionaryValue* wp_pref = | 61 const DictionaryValue* wp_pref = |
| 58 browser_->profile()->GetPrefs()->GetDictionary(window_name.c_str()); | 62 browser_->profile()->GetPrefs()->GetDictionary(window_name.c_str()); |
| 59 int top = 0, left = 0, bottom = 0, right = 0; | 63 int top = 0, left = 0, bottom = 0, right = 0; |
| 64 bool maximized = false; |
| 60 bool has_prefs = wp_pref && | 65 bool has_prefs = wp_pref && |
| 61 wp_pref->GetInteger("top", &top) && | 66 wp_pref->GetInteger("top", &top) && |
| 62 wp_pref->GetInteger("left", &left) && | 67 wp_pref->GetInteger("left", &left) && |
| 63 wp_pref->GetInteger("bottom", &bottom) && | 68 wp_pref->GetInteger("bottom", &bottom) && |
| 64 wp_pref->GetInteger("right", &right); | 69 wp_pref->GetInteger("right", &right) && |
| 70 wp_pref->GetBoolean("maximized", &maximized); |
| 65 bounds->SetRect(left, top, std::max(0, right - left), | 71 bounds->SetRect(left, top, std::max(0, right - left), |
| 66 std::max(0, bottom - top)); | 72 std::max(0, bottom - top)); |
| 67 | 73 |
| 68 int work_area_top = 0; | 74 int work_area_top = 0; |
| 69 int work_area_left = 0; | 75 int work_area_left = 0; |
| 70 int work_area_bottom = 0; | 76 int work_area_bottom = 0; |
| 71 int work_area_right = 0; | 77 int work_area_right = 0; |
| 72 if (wp_pref) { | 78 if (wp_pref) { |
| 73 wp_pref->GetInteger("work_area_top", &work_area_top); | 79 wp_pref->GetInteger("work_area_top", &work_area_top); |
| 74 wp_pref->GetInteger("work_area_left", &work_area_left); | 80 wp_pref->GetInteger("work_area_left", &work_area_left); |
| 75 wp_pref->GetInteger("work_area_bottom", &work_area_bottom); | 81 wp_pref->GetInteger("work_area_bottom", &work_area_bottom); |
| 76 wp_pref->GetInteger("work_area_right", &work_area_right); | 82 wp_pref->GetInteger("work_area_right", &work_area_right); |
| 83 if (*show_state == ui::SHOW_STATE_DEFAULT && maximized) |
| 84 *show_state = ui::SHOW_STATE_MAXIMIZED; |
| 77 } | 85 } |
| 78 work_area->SetRect(work_area_left, work_area_top, | 86 work_area->SetRect(work_area_left, work_area_top, |
| 79 std::max(0, work_area_right - work_area_left), | 87 std::max(0, work_area_right - work_area_left), |
| 80 std::max(0, work_area_bottom - work_area_top)); | 88 std::max(0, work_area_bottom - work_area_top)); |
| 81 | 89 |
| 82 return has_prefs; | 90 return has_prefs; |
| 83 } | 91 } |
| 84 | 92 |
| 85 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const { | 93 virtual bool GetLastActiveWindowState( |
| 94 gfx::Rect* bounds, |
| 95 ui::WindowShowState* show_state) const { |
| 96 DCHECK(show_state); |
| 86 // Applications are always restored with the same position. | 97 // Applications are always restored with the same position. |
| 87 if (!app_name_.empty()) | 98 if (!app_name_.empty()) |
| 88 return false; | 99 return false; |
| 89 | 100 |
| 90 // If a reference browser is set, use its window. Otherwise find last | 101 // If a reference browser is set, use its window. Otherwise find last |
| 91 // active. Panels are never used as reference browsers as panels are | 102 // active. Panels are never used as reference browsers as panels are |
| 92 // specially positioned. | 103 // specially positioned. |
| 93 BrowserWindow* window = NULL; | 104 BrowserWindow* window = NULL; |
| 94 // Window may be null if browser is just starting up. | 105 // Window may be null if browser is just starting up. |
| 95 if (browser_ && browser_->window() && !browser_->window()->IsPanel()) { | 106 if (browser_ && browser_->window() && !browser_->window()->IsPanel()) { |
| 96 window = browser_->window(); | 107 window = browser_->window(); |
| 97 } else { | 108 } else { |
| 98 BrowserList::const_reverse_iterator it = BrowserList::begin_last_active(); | 109 BrowserList::const_reverse_iterator it = BrowserList::begin_last_active(); |
| 99 BrowserList::const_reverse_iterator end = BrowserList::end_last_active(); | 110 BrowserList::const_reverse_iterator end = BrowserList::end_last_active(); |
| 100 for (; (it != end); ++it) { | 111 for (; (it != end); ++it) { |
| 101 Browser* last_active = *it; | 112 Browser* last_active = *it; |
| 102 if (last_active && last_active->is_type_tabbed()) { | 113 if (last_active && last_active->is_type_tabbed()) { |
| 103 window = last_active->window(); | 114 window = last_active->window(); |
| 104 DCHECK(window); | 115 DCHECK(window); |
| 105 break; | 116 break; |
| 106 } | 117 } |
| 107 } | 118 } |
| 108 } | 119 } |
| 109 | 120 |
| 110 if (window) { | 121 if (window) { |
| 111 *bounds = window->GetRestoredBounds(); | 122 *bounds = window->GetRestoredBounds(); |
| 123 if (*show_state == ui::SHOW_STATE_DEFAULT && window->IsMaximized()) |
| 124 *show_state = ui::SHOW_STATE_MAXIMIZED; |
| 112 return true; | 125 return true; |
| 113 } | 126 } |
| 114 | 127 |
| 115 return false; | 128 return false; |
| 116 } | 129 } |
| 117 | 130 |
| 118 private: | 131 private: |
| 119 std::string app_name_; | 132 std::string app_name_; |
| 120 | 133 |
| 121 // If set, is used as the reference browser for GetLastActiveWindowState. | 134 // If set, is used as the reference browser for GetLastActiveWindowState. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 146 const Browser* browser) | 159 const Browser* browser) |
| 147 : state_provider_(state_provider), | 160 : state_provider_(state_provider), |
| 148 monitor_info_provider_(monitor_info_provider), | 161 monitor_info_provider_(monitor_info_provider), |
| 149 browser_(browser) { | 162 browser_(browser) { |
| 150 } | 163 } |
| 151 | 164 |
| 152 WindowSizer::~WindowSizer() { | 165 WindowSizer::~WindowSizer() { |
| 153 } | 166 } |
| 154 | 167 |
| 155 // static | 168 // static |
| 156 void WindowSizer::GetBrowserWindowBounds(const std::string& app_name, | 169 void WindowSizer::GetBrowserWindowBoundsAndShowState( |
| 157 const gfx::Rect& specified_bounds, | 170 const std::string& app_name, |
| 158 const Browser* browser, | 171 const gfx::Rect& specified_bounds, |
| 159 gfx::Rect* window_bounds) { | 172 const Browser* browser, |
| 173 gfx::Rect* window_bounds, |
| 174 ui::WindowShowState* show_state) { |
| 160 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), browser); | 175 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), browser); |
| 161 sizer.DetermineWindowBounds(specified_bounds, window_bounds); | 176 sizer.DetermineWindowBoundsAndShowState(specified_bounds, |
| 177 window_bounds, |
| 178 show_state); |
| 162 } | 179 } |
| 163 | 180 |
| 164 /////////////////////////////////////////////////////////////////////////////// | 181 /////////////////////////////////////////////////////////////////////////////// |
| 165 // WindowSizer, private: | 182 // WindowSizer, private: |
| 166 | 183 |
| 167 void WindowSizer::DetermineWindowBounds(const gfx::Rect& specified_bounds, | 184 void WindowSizer::DetermineWindowBoundsAndShowState( |
| 168 gfx::Rect* bounds) const { | 185 const gfx::Rect& specified_bounds, |
| 186 gfx::Rect* bounds, |
| 187 ui::WindowShowState* show_state) const { |
| 188 DCHECK(bounds); |
| 189 DCHECK(show_state); |
| 190 // Pre-populate the window state with our default. |
| 191 *show_state = GetWindowDefaultShowState(); |
| 169 *bounds = specified_bounds; | 192 *bounds = specified_bounds; |
| 170 if (bounds->IsEmpty()) { | 193 if (bounds->IsEmpty()) { |
| 171 if (GetBoundsOverride(specified_bounds, bounds)) | 194 if (GetBoundsOverride(specified_bounds, bounds, show_state)) |
| 172 return; | 195 return; |
| 173 // See if there's saved placement information. | 196 // See if there's saved placement information. |
| 174 if (!GetLastWindowBounds(bounds)) { | 197 if (!GetLastWindowBounds(bounds, show_state)) { |
| 175 if (!GetSavedWindowBounds(bounds)) { | 198 if (!GetSavedWindowBounds(bounds, show_state)) { |
| 176 // No saved placement, figure out some sensible default size based on | 199 // No saved placement, figure out some sensible default size based on |
| 177 // the user's screen size. | 200 // the user's screen size. |
| 178 GetDefaultWindowBounds(bounds); | 201 GetDefaultWindowBounds(bounds); |
| 179 } | 202 } |
| 180 } | 203 } |
| 181 } else { | 204 } else { |
| 182 // In case that there was a bound given we need to make sure that it is | 205 // In case that there was a bound given we need to make sure that it is |
| 183 // visible and fits on the screen. | 206 // visible and fits on the screen. |
| 184 // Find the size of the work area of the monitor that intersects the bounds | 207 // Find the size of the work area of the monitor that intersects the bounds |
| 185 // of the anchor window. Note: AdjustBoundsToBeVisibleOnMonitorContaining | 208 // of the anchor window. Note: AdjustBoundsToBeVisibleOnMonitorContaining |
| 186 // does not exactly what we want: It makes only sure that "a minimal part" | 209 // does not exactly what we want: It makes only sure that "a minimal part" |
| 187 // is visible on the screen. | 210 // is visible on the screen. |
| 188 gfx::Rect work_area = | 211 gfx::Rect work_area = |
| 189 monitor_info_provider_->GetMonitorWorkAreaMatching(*bounds); | 212 monitor_info_provider_->GetMonitorWorkAreaMatching(*bounds); |
| 190 // Resize so that it fits. | 213 // Resize so that it fits. |
| 191 *bounds = bounds->AdjustToFit(work_area); | 214 *bounds = bounds->AdjustToFit(work_area); |
| 192 } | 215 } |
| 193 } | 216 } |
| 194 | 217 |
| 195 bool WindowSizer::GetLastWindowBounds(gfx::Rect* bounds) const { | 218 bool WindowSizer::GetLastWindowBounds(gfx::Rect* bounds, |
| 219 ui::WindowShowState* show_state) const { |
| 196 DCHECK(bounds); | 220 DCHECK(bounds); |
| 221 DCHECK(show_state); |
| 197 if (!state_provider_.get() || | 222 if (!state_provider_.get() || |
| 198 !state_provider_->GetLastActiveWindowState(bounds)) | 223 !state_provider_->GetLastActiveWindowState(bounds, show_state)) |
| 199 return false; | 224 return false; |
| 200 gfx::Rect last_window_bounds = *bounds; | 225 gfx::Rect last_window_bounds = *bounds; |
| 201 bounds->Offset(kWindowTilePixels, kWindowTilePixels); | 226 bounds->Offset(kWindowTilePixels, kWindowTilePixels); |
| 202 AdjustBoundsToBeVisibleOnMonitorContaining(last_window_bounds, | 227 AdjustBoundsToBeVisibleOnMonitorContaining(last_window_bounds, |
| 203 gfx::Rect(), | 228 gfx::Rect(), |
| 204 bounds); | 229 bounds); |
| 205 return true; | 230 return true; |
| 206 } | 231 } |
| 207 | 232 |
| 208 bool WindowSizer::GetSavedWindowBounds(gfx::Rect* bounds) const { | 233 bool WindowSizer::GetSavedWindowBounds(gfx::Rect* bounds, |
| 234 ui::WindowShowState* show_state) const { |
| 209 DCHECK(bounds); | 235 DCHECK(bounds); |
| 236 DCHECK(show_state); |
| 210 gfx::Rect saved_work_area; | 237 gfx::Rect saved_work_area; |
| 211 if (!state_provider_.get() || | 238 if (!state_provider_.get() || |
| 212 !state_provider_->GetPersistentState(bounds, &saved_work_area)) | 239 !state_provider_->GetPersistentState(bounds, |
| 240 &saved_work_area, |
| 241 show_state)) |
| 213 return false; | 242 return false; |
| 214 AdjustBoundsToBeVisibleOnMonitorContaining(*bounds, saved_work_area, bounds); | 243 AdjustBoundsToBeVisibleOnMonitorContaining(*bounds, saved_work_area, bounds); |
| 215 return true; | 244 return true; |
| 216 } | 245 } |
| 217 | 246 |
| 218 void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const { | 247 void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const { |
| 219 #if defined(USE_ASH) | 248 #if defined(USE_ASH) |
| 220 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312 | 249 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312 |
| 221 if (chrome::ShouldOpenAshOnStartup()) { | 250 if (chrome::ShouldOpenAshOnStartup()) { |
| 222 GetDefaultWindowBoundsAsh(default_bounds); | 251 GetDefaultWindowBoundsAsh(default_bounds); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 const int min_x = work_area.x() + kMinVisibleWidth - bounds->width(); | 354 const int min_x = work_area.x() + kMinVisibleWidth - bounds->width(); |
| 326 const int max_y = work_area.bottom() - kMinVisibleHeight; | 355 const int max_y = work_area.bottom() - kMinVisibleHeight; |
| 327 const int max_x = work_area.right() - kMinVisibleWidth; | 356 const int max_x = work_area.right() - kMinVisibleWidth; |
| 328 bounds->set_y(std::max(min_y, std::min(max_y, bounds->y()))); | 357 bounds->set_y(std::max(min_y, std::min(max_y, bounds->y()))); |
| 329 bounds->set_x(std::max(min_x, std::min(max_x, bounds->x()))); | 358 bounds->set_x(std::max(min_x, std::min(max_x, bounds->x()))); |
| 330 #endif // defined(OS_MACOSX) | 359 #endif // defined(OS_MACOSX) |
| 331 } | 360 } |
| 332 | 361 |
| 333 bool WindowSizer::GetBoundsOverride( | 362 bool WindowSizer::GetBoundsOverride( |
| 334 const gfx::Rect& specified_bounds, | 363 const gfx::Rect& specified_bounds, |
| 335 gfx::Rect* bounds) const { | 364 gfx::Rect* bounds, |
| 365 ui::WindowShowState* show_state) const { |
| 336 #if defined(USE_ASH) | 366 #if defined(USE_ASH) |
| 337 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312 | 367 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312 |
| 338 if (chrome::ShouldOpenAshOnStartup()) | 368 if (chrome::ShouldOpenAshOnStartup()) |
| 339 return GetBoundsOverrideAsh(specified_bounds, bounds); | 369 return GetBoundsOverrideAsh(specified_bounds, bounds, show_state); |
| 340 #endif | 370 #endif |
| 341 return false; | 371 return false; |
| 342 } | 372 } |
| 373 |
| 374 ui::WindowShowState WindowSizer::GetWindowDefaultShowState() const { |
| 375 if (!browser_) |
| 376 return ui::SHOW_STATE_DEFAULT; |
| 377 |
| 378 // Only tabbed browsers use the command line or preference state, with the |
| 379 // exception of devtools. |
| 380 bool show_state = !browser_->is_type_tabbed() && !browser_->is_devtools(); |
| 381 |
| 382 #if defined(USE_AURA) |
| 383 // We use the apps save state on aura. |
| 384 show_state &= !browser_->is_app(); |
| 385 #endif |
| 386 |
| 387 if (show_state) |
| 388 return browser_->initial_show_state(); |
| 389 |
| 390 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kStartMaximized)) |
| 391 return ui::SHOW_STATE_MAXIMIZED; |
| 392 |
| 393 if (browser_->initial_show_state() != ui::SHOW_STATE_DEFAULT) |
| 394 return browser_->initial_show_state(); |
| 395 |
| 396 // Otherwise we use the default which can be overridden later on. |
| 397 return ui::SHOW_STATE_DEFAULT; |
| 398 } |
| OLD | NEW |