| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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.h" | 5 #include "chrome/browser/ui/window_sizer.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_list.h" | 11 #include "chrome/browser/ui/browser_list.h" |
| 12 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 | 14 |
| 15 /////////////////////////////////////////////////////////////////////////////// | 15 /////////////////////////////////////////////////////////////////////////////// |
| 16 // An implementation of WindowSizer::StateProvider that gets the last active | 16 // An implementation of WindowSizer::StateProvider that gets the last active |
| 17 // and persistent state from the browser window and the user's profile. | 17 // and persistent state from the browser window and the user's profile. |
| 18 class DefaultStateProvider : public WindowSizer::StateProvider { | 18 class DefaultStateProvider : public WindowSizer::StateProvider { |
| 19 public: | 19 public: |
| 20 DefaultStateProvider(const std::string& app_name, const Browser* browser) | 20 DefaultStateProvider(const std::string& app_name, const Browser* browser) |
| 21 : app_name_(app_name), browser_(browser) { | 21 : app_name_(app_name), browser_(browser) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 // Overridden from WindowSizer::StateProvider: | 24 // Overridden from WindowSizer::StateProvider: |
| 25 virtual bool GetPersistentState(gfx::Rect* bounds, | 25 virtual bool GetPersistentState(gfx::Rect* bounds, |
| 26 bool* maximized, | |
| 27 gfx::Rect* work_area) const { | 26 gfx::Rect* work_area) const { |
| 28 DCHECK(bounds && maximized); | 27 DCHECK(bounds); |
| 29 | |
| 30 std::string key(prefs::kBrowserWindowPlacement); | |
| 31 if (!app_name_.empty()) { | |
| 32 key.append("_"); | |
| 33 key.append(app_name_); | |
| 34 } | |
| 35 | 28 |
| 36 if (!browser_ || !browser_->profile()->GetPrefs()) | 29 if (!browser_ || !browser_->profile()->GetPrefs()) |
| 37 return false; | 30 return false; |
| 38 | 31 |
| 32 std::string window_name(browser_->GetWindowPlacementKey()); |
| 39 const DictionaryValue* wp_pref = | 33 const DictionaryValue* wp_pref = |
| 40 browser_->profile()->GetPrefs()->GetDictionary(key.c_str()); | 34 browser_->profile()->GetPrefs()->GetDictionary(window_name.c_str()); |
| 41 int top = 0, left = 0, bottom = 0, right = 0; | 35 int top = 0, left = 0, bottom = 0, right = 0; |
| 42 bool has_prefs = | 36 bool has_prefs = wp_pref && |
| 43 wp_pref && | 37 wp_pref->GetInteger("top", &top) && |
| 44 wp_pref->GetInteger("top", &top) && | 38 wp_pref->GetInteger("left", &left) && |
| 45 wp_pref->GetInteger("left", &left) && | 39 wp_pref->GetInteger("bottom", &bottom) && |
| 46 wp_pref->GetInteger("bottom", &bottom) && | 40 wp_pref->GetInteger("right", &right); |
| 47 wp_pref->GetInteger("right", &right) && | |
| 48 wp_pref->GetBoolean("maximized", maximized); | |
| 49 bounds->SetRect(left, top, std::max(0, right - left), | 41 bounds->SetRect(left, top, std::max(0, right - left), |
| 50 std::max(0, bottom - top)); | 42 std::max(0, bottom - top)); |
| 51 | 43 |
| 52 int work_area_top = 0; | 44 int work_area_top = 0; |
| 53 int work_area_left = 0; | 45 int work_area_left = 0; |
| 54 int work_area_bottom = 0; | 46 int work_area_bottom = 0; |
| 55 int work_area_right = 0; | 47 int work_area_right = 0; |
| 56 if (wp_pref) { | 48 if (wp_pref) { |
| 57 wp_pref->GetInteger("work_area_top", &work_area_top); | 49 wp_pref->GetInteger("work_area_top", &work_area_top); |
| 58 wp_pref->GetInteger("work_area_left", &work_area_left); | 50 wp_pref->GetInteger("work_area_left", &work_area_left); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 if (state_provider_) | 113 if (state_provider_) |
| 122 delete state_provider_; | 114 delete state_provider_; |
| 123 if (monitor_info_provider_) | 115 if (monitor_info_provider_) |
| 124 delete monitor_info_provider_; | 116 delete monitor_info_provider_; |
| 125 } | 117 } |
| 126 | 118 |
| 127 // static | 119 // static |
| 128 void WindowSizer::GetBrowserWindowBounds(const std::string& app_name, | 120 void WindowSizer::GetBrowserWindowBounds(const std::string& app_name, |
| 129 const gfx::Rect& specified_bounds, | 121 const gfx::Rect& specified_bounds, |
| 130 const Browser* browser, | 122 const Browser* browser, |
| 131 gfx::Rect* window_bounds, | 123 gfx::Rect* window_bounds) { |
| 132 bool* maximized) { | |
| 133 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), | 124 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), |
| 134 CreateDefaultMonitorInfoProvider()); | 125 CreateDefaultMonitorInfoProvider()); |
| 135 sizer.DetermineWindowBounds(specified_bounds, window_bounds, maximized); | 126 sizer.DetermineWindowBounds(specified_bounds, window_bounds); |
| 136 } | 127 } |
| 137 | 128 |
| 138 /////////////////////////////////////////////////////////////////////////////// | 129 /////////////////////////////////////////////////////////////////////////////// |
| 139 // WindowSizer, private: | 130 // WindowSizer, private: |
| 140 | 131 |
| 141 void WindowSizer::DetermineWindowBounds(const gfx::Rect& specified_bounds, | 132 void WindowSizer::DetermineWindowBounds(const gfx::Rect& specified_bounds, |
| 142 gfx::Rect* bounds, | 133 gfx::Rect* bounds) const { |
| 143 bool* maximized) const { | |
| 144 *bounds = specified_bounds; | 134 *bounds = specified_bounds; |
| 145 if (bounds->IsEmpty()) { | 135 if (bounds->IsEmpty()) { |
| 146 // See if there's saved placement information. | 136 // See if there's saved placement information. |
| 147 if (!GetLastWindowBounds(bounds)) { | 137 if (!GetLastWindowBounds(bounds)) { |
| 148 if (!GetSavedWindowBounds(bounds, maximized)) { | 138 if (!GetSavedWindowBounds(bounds)) { |
| 149 // No saved placement, figure out some sensible default size based on | 139 // No saved placement, figure out some sensible default size based on |
| 150 // the user's screen size. | 140 // the user's screen size. |
| 151 GetDefaultWindowBounds(bounds); | 141 GetDefaultWindowBounds(bounds); |
| 152 } | 142 } |
| 153 } | 143 } |
| 154 } | 144 } |
| 155 } | 145 } |
| 156 | 146 |
| 157 bool WindowSizer::GetLastWindowBounds(gfx::Rect* bounds) const { | 147 bool WindowSizer::GetLastWindowBounds(gfx::Rect* bounds) const { |
| 158 DCHECK(bounds); | 148 DCHECK(bounds); |
| 159 if (!state_provider_ || !state_provider_->GetLastActiveWindowState(bounds)) | 149 if (!state_provider_ || !state_provider_->GetLastActiveWindowState(bounds)) |
| 160 return false; | 150 return false; |
| 161 gfx::Rect last_window_bounds = *bounds; | 151 gfx::Rect last_window_bounds = *bounds; |
| 162 bounds->Offset(kWindowTilePixels, kWindowTilePixels); | 152 bounds->Offset(kWindowTilePixels, kWindowTilePixels); |
| 163 AdjustBoundsToBeVisibleOnMonitorContaining(last_window_bounds, | 153 AdjustBoundsToBeVisibleOnMonitorContaining(last_window_bounds, |
| 164 gfx::Rect(), | 154 gfx::Rect(), |
| 165 bounds); | 155 bounds); |
| 166 return true; | 156 return true; |
| 167 } | 157 } |
| 168 | 158 |
| 169 bool WindowSizer::GetSavedWindowBounds(gfx::Rect* bounds, | 159 bool WindowSizer::GetSavedWindowBounds(gfx::Rect* bounds) const { |
| 170 bool* maximized) const { | 160 DCHECK(bounds); |
| 171 DCHECK(bounds && maximized); | |
| 172 gfx::Rect saved_work_area; | 161 gfx::Rect saved_work_area; |
| 173 if (!state_provider_ || | 162 if (!state_provider_ || |
| 174 !state_provider_->GetPersistentState(bounds, maximized, &saved_work_area)) | 163 !state_provider_->GetPersistentState(bounds, &saved_work_area)) |
| 175 return false; | 164 return false; |
| 176 AdjustBoundsToBeVisibleOnMonitorContaining(*bounds, saved_work_area, bounds); | 165 AdjustBoundsToBeVisibleOnMonitorContaining(*bounds, saved_work_area, bounds); |
| 177 return true; | 166 return true; |
| 178 } | 167 } |
| 179 | 168 |
| 180 void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const { | 169 void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const { |
| 181 DCHECK(default_bounds); | 170 DCHECK(default_bounds); |
| 182 DCHECK(monitor_info_provider_); | 171 DCHECK(monitor_info_provider_); |
| 183 | 172 |
| 184 gfx::Rect work_area = monitor_info_provider_->GetPrimaryMonitorWorkArea(); | 173 gfx::Rect work_area = monitor_info_provider_->GetPrimaryMonitorWorkArea(); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 // On non-Mac platforms, we are less aggressive about repositioning. Simply | 300 // On non-Mac platforms, we are less aggressive about repositioning. Simply |
| 312 // ensure that at least kMinVisibleWidth * kMinVisibleHeight is visible. | 301 // ensure that at least kMinVisibleWidth * kMinVisibleHeight is visible. |
| 313 const int min_y = work_area.y() + kMinVisibleHeight - bounds->height(); | 302 const int min_y = work_area.y() + kMinVisibleHeight - bounds->height(); |
| 314 const int min_x = work_area.x() + kMinVisibleWidth - bounds->width(); | 303 const int min_x = work_area.x() + kMinVisibleWidth - bounds->width(); |
| 315 const int max_y = work_area.bottom() - kMinVisibleHeight; | 304 const int max_y = work_area.bottom() - kMinVisibleHeight; |
| 316 const int max_x = work_area.right() - kMinVisibleWidth; | 305 const int max_x = work_area.right() - kMinVisibleWidth; |
| 317 bounds->set_y(std::max(min_y, std::min(max_y, bounds->y()))); | 306 bounds->set_y(std::max(min_y, std::min(max_y, bounds->y()))); |
| 318 bounds->set_x(std::max(min_x, std::min(max_x, bounds->x()))); | 307 bounds->set_x(std::max(min_x, std::min(max_x, bounds->x()))); |
| 319 #endif // defined(OS_MACOSX) | 308 #endif // defined(OS_MACOSX) |
| 320 } | 309 } |
| OLD | NEW |