Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: chrome/browser/ui/window_sizer/window_sizer.cc

Issue 11072002: Combined window positioning and show state determiniation in the WindowSizer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/prefs/pref_service.h" 9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/ash/ash_init.h" 11 #include "chrome/browser/ui/ash/ash_init.h"
12 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_list.h" 13 #include "chrome/browser/ui/browser_list.h"
14 #include "chrome/browser/ui/browser_window.h" 14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/browser_window_state.h" 15 #include "chrome/browser/ui/browser_window_state.h"
16 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/chrome_switches.cc"
17 #include "ui/gfx/screen.h" 18 #include "ui/gfx/screen.h"
19 #include "ui/views/widget/widget.h"
sky 2012/10/05 22:54:03 This is cross platform code, don't use views
Mr4D (OOO till 08-26) 2012/10/06 01:10:10 When I checked for minimize that was the only way
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:
27 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE { 29 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE {
(...skipping 12 matching lines...) Expand all
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);
52 55
53 if (!browser_ || !browser_->profile()->GetPrefs()) 56 if (!browser_ || !browser_->profile()->GetPrefs())
54 return false; 57 return false;
55 58
56 std::string window_name(chrome::GetWindowPlacementKey(browser_)); 59 std::string window_name(chrome::GetWindowPlacementKey(browser_));
57 const DictionaryValue* wp_pref = 60 const DictionaryValue* wp_pref =
58 browser_->profile()->GetPrefs()->GetDictionary(window_name.c_str()); 61 browser_->profile()->GetPrefs()->GetDictionary(window_name.c_str());
59 int top = 0, left = 0, bottom = 0, right = 0; 62 int top = 0, left = 0, bottom = 0, right = 0;
63 bool maximized = false;
60 bool has_prefs = wp_pref && 64 bool has_prefs = wp_pref &&
61 wp_pref->GetInteger("top", &top) && 65 wp_pref->GetInteger("top", &top) &&
62 wp_pref->GetInteger("left", &left) && 66 wp_pref->GetInteger("left", &left) &&
63 wp_pref->GetInteger("bottom", &bottom) && 67 wp_pref->GetInteger("bottom", &bottom) &&
64 wp_pref->GetInteger("right", &right); 68 wp_pref->GetInteger("right", &right) &&
69 wp_pref->GetBoolean("maximized", &maximized);
65 bounds->SetRect(left, top, std::max(0, right - left), 70 bounds->SetRect(left, top, std::max(0, right - left),
66 std::max(0, bottom - top)); 71 std::max(0, bottom - top));
67 72
68 int work_area_top = 0; 73 int work_area_top = 0;
69 int work_area_left = 0; 74 int work_area_left = 0;
70 int work_area_bottom = 0; 75 int work_area_bottom = 0;
71 int work_area_right = 0; 76 int work_area_right = 0;
72 if (wp_pref) { 77 if (wp_pref) {
73 wp_pref->GetInteger("work_area_top", &work_area_top); 78 wp_pref->GetInteger("work_area_top", &work_area_top);
74 wp_pref->GetInteger("work_area_left", &work_area_left); 79 wp_pref->GetInteger("work_area_left", &work_area_left);
75 wp_pref->GetInteger("work_area_bottom", &work_area_bottom); 80 wp_pref->GetInteger("work_area_bottom", &work_area_bottom);
76 wp_pref->GetInteger("work_area_right", &work_area_right); 81 wp_pref->GetInteger("work_area_right", &work_area_right);
82 if (show_state == ui::SHOW_STATE_DEFAULT && maximized)
83 show_state = ui::SHOW_STATE_MAXIMIZED;
77 } 84 }
78 work_area->SetRect(work_area_left, work_area_top, 85 work_area->SetRect(work_area_left, work_area_top,
79 std::max(0, work_area_right - work_area_left), 86 std::max(0, work_area_right - work_area_left),
80 std::max(0, work_area_bottom - work_area_top)); 87 std::max(0, work_area_bottom - work_area_top));
81 88
82 return has_prefs; 89 return has_prefs;
83 } 90 }
84 91
85 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const { 92 virtual bool GetLastActiveWindowState(gfx::Rect* bounds,
93 ui::WindowShowState& show_state) const {
86 // Applications are always restored with the same position. 94 // Applications are always restored with the same position.
87 if (!app_name_.empty()) 95 if (!app_name_.empty())
88 return false; 96 return false;
89 97
90 // If a reference browser is set, use its window. Otherwise find last 98 // If a reference browser is set, use its window. Otherwise find last
91 // active. Panels are never used as reference browsers as panels are 99 // active. Panels are never used as reference browsers as panels are
92 // specially positioned. 100 // specially positioned.
93 BrowserWindow* window = NULL; 101 BrowserWindow* window = NULL;
94 // Window may be null if browser is just starting up. 102 // Window may be null if browser is just starting up.
95 if (browser_ && browser_->window() && !browser_->window()->IsPanel()) { 103 if (browser_ && browser_->window() && !browser_->window()->IsPanel()) {
96 window = browser_->window(); 104 window = browser_->window();
97 } else { 105 } else {
98 BrowserList::const_reverse_iterator it = BrowserList::begin_last_active(); 106 BrowserList::const_reverse_iterator it = BrowserList::begin_last_active();
99 BrowserList::const_reverse_iterator end = BrowserList::end_last_active(); 107 BrowserList::const_reverse_iterator end = BrowserList::end_last_active();
100 for (; (it != end); ++it) { 108 for (; (it != end); ++it) {
101 Browser* last_active = *it; 109 Browser* last_active = *it;
102 if (last_active && last_active->is_type_tabbed()) { 110 if (last_active && last_active->is_type_tabbed()) {
103 window = last_active->window(); 111 window = last_active->window();
104 DCHECK(window); 112 DCHECK(window);
105 break; 113 break;
106 } 114 }
107 } 115 }
108 } 116 }
109 117
110 if (window) { 118 if (window) {
111 *bounds = window->GetRestoredBounds(); 119 *bounds = window->GetRestoredBounds();
120 bool maximized = views::Widget::GetWidgetForNativeWindow(
sky 2012/10/05 22:54:03 You want window->IsMaximized(), which works on all
Mr4D (OOO till 08-26) 2012/10/06 01:10:10 This is now funny. I was looking all over the syst
121 window->GetNativeWindow())->IsMaximized();
122 if (show_state == ui::SHOW_STATE_DEFAULT && maximized)
123 show_state = ui::SHOW_STATE_MAXIMIZED;
112 return true; 124 return true;
113 } 125 }
114 126
115 return false; 127 return false;
116 } 128 }
117 129
118 private: 130 private:
119 std::string app_name_; 131 std::string app_name_;
120 132
121 // If set, is used as the reference browser for GetLastActiveWindowState. 133 // If set, is used as the reference browser for GetLastActiveWindowState.
(...skipping 24 matching lines...) Expand all
146 const Browser* browser) 158 const Browser* browser)
147 : state_provider_(state_provider), 159 : state_provider_(state_provider),
148 monitor_info_provider_(monitor_info_provider), 160 monitor_info_provider_(monitor_info_provider),
149 browser_(browser) { 161 browser_(browser) {
150 } 162 }
151 163
152 WindowSizer::~WindowSizer() { 164 WindowSizer::~WindowSizer() {
153 } 165 }
154 166
155 // static 167 // static
168 void WindowSizer::GetBrowserWindowBoundsAndShowState(
169 const std::string& app_name,
170 const gfx::Rect& specified_bounds,
171 const Browser* browser,
172 gfx::Rect* window_bounds,
173 ui::WindowShowState& show_state) {
174 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), browser);
175 sizer.DetermineWindowBoundsAndShowState(specified_bounds,
176 window_bounds,
177 show_state);
178 }
179
180 // static
156 void WindowSizer::GetBrowserWindowBounds(const std::string& app_name, 181 void WindowSizer::GetBrowserWindowBounds(const std::string& app_name,
157 const gfx::Rect& specified_bounds, 182 const gfx::Rect& specified_bounds,
158 const Browser* browser, 183 const Browser* browser,
159 gfx::Rect* window_bounds) { 184 gfx::Rect* window_bounds) {
160 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), browser); 185 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), browser);
161 sizer.DetermineWindowBounds(specified_bounds, window_bounds); 186 ui::WindowShowState show_state;
187 sizer.DetermineWindowBoundsAndShowState(specified_bounds,
188 window_bounds,
189 show_state);
162 } 190 }
163 191
164 /////////////////////////////////////////////////////////////////////////////// 192 ///////////////////////////////////////////////////////////////////////////////
165 // WindowSizer, private: 193 // WindowSizer, private:
166 194
167 void WindowSizer::DetermineWindowBounds(const gfx::Rect& specified_bounds, 195 void WindowSizer::DetermineWindowBoundsAndShowState(
168 gfx::Rect* bounds) const { 196 const gfx::Rect& specified_bounds,
197 gfx::Rect* bounds,
198 ui::WindowShowState& show_state) const {
199 // Pre-populate the window state with our default.
200 show_state = GetWindowDefaultShowState();
169 *bounds = specified_bounds; 201 *bounds = specified_bounds;
170 if (bounds->IsEmpty()) { 202 if (bounds->IsEmpty()) {
171 if (GetBoundsOverride(specified_bounds, bounds)) 203 if (GetBoundsOverride(specified_bounds, bounds, show_state))
172 return; 204 return;
173 // See if there's saved placement information. 205 // See if there's saved placement information.
174 if (!GetLastWindowBounds(bounds)) { 206 if (!GetLastWindowBounds(bounds, show_state)) {
175 if (!GetSavedWindowBounds(bounds)) { 207 if (!GetSavedWindowBounds(bounds, show_state)) {
176 // No saved placement, figure out some sensible default size based on 208 // No saved placement, figure out some sensible default size based on
177 // the user's screen size. 209 // the user's screen size.
178 GetDefaultWindowBounds(bounds); 210 GetDefaultWindowBounds(bounds);
179 } 211 }
180 } 212 }
181 } else { 213 } else {
182 // In case that there was a bound given we need to make sure that it is 214 // In case that there was a bound given we need to make sure that it is
183 // visible and fits on the screen. 215 // visible and fits on the screen.
184 // Find the size of the work area of the monitor that intersects the bounds 216 // Find the size of the work area of the monitor that intersects the bounds
185 // of the anchor window. Note: AdjustBoundsToBeVisibleOnMonitorContaining 217 // of the anchor window. Note: AdjustBoundsToBeVisibleOnMonitorContaining
186 // does not exactly what we want: It makes only sure that "a minimal part" 218 // does not exactly what we want: It makes only sure that "a minimal part"
187 // is visible on the screen. 219 // is visible on the screen.
188 gfx::Rect work_area = 220 gfx::Rect work_area =
189 monitor_info_provider_->GetMonitorWorkAreaMatching(*bounds); 221 monitor_info_provider_->GetMonitorWorkAreaMatching(*bounds);
190 // Resize so that it fits. 222 // Resize so that it fits.
191 *bounds = bounds->AdjustToFit(work_area); 223 *bounds = bounds->AdjustToFit(work_area);
192 } 224 }
193 } 225 }
194 226
195 bool WindowSizer::GetLastWindowBounds(gfx::Rect* bounds) const { 227 bool WindowSizer::GetLastWindowBounds(gfx::Rect* bounds,
228 ui::WindowShowState& show_state) const {
196 DCHECK(bounds); 229 DCHECK(bounds);
197 if (!state_provider_.get() || 230 if (!state_provider_.get() ||
198 !state_provider_->GetLastActiveWindowState(bounds)) 231 !state_provider_->GetLastActiveWindowState(bounds, show_state))
199 return false; 232 return false;
200 gfx::Rect last_window_bounds = *bounds; 233 gfx::Rect last_window_bounds = *bounds;
201 bounds->Offset(kWindowTilePixels, kWindowTilePixels); 234 bounds->Offset(kWindowTilePixels, kWindowTilePixels);
202 AdjustBoundsToBeVisibleOnMonitorContaining(last_window_bounds, 235 AdjustBoundsToBeVisibleOnMonitorContaining(last_window_bounds,
203 gfx::Rect(), 236 gfx::Rect(),
204 bounds); 237 bounds);
205 return true; 238 return true;
206 } 239 }
207 240
208 bool WindowSizer::GetSavedWindowBounds(gfx::Rect* bounds) const { 241 bool WindowSizer::GetSavedWindowBounds(gfx::Rect* bounds,
242 ui::WindowShowState& show_state) const {
209 DCHECK(bounds); 243 DCHECK(bounds);
210 gfx::Rect saved_work_area; 244 gfx::Rect saved_work_area;
211 if (!state_provider_.get() || 245 if (!state_provider_.get() ||
212 !state_provider_->GetPersistentState(bounds, &saved_work_area)) 246 !state_provider_->GetPersistentState(bounds,
247 &saved_work_area,
248 show_state))
213 return false; 249 return false;
214 AdjustBoundsToBeVisibleOnMonitorContaining(*bounds, saved_work_area, bounds); 250 AdjustBoundsToBeVisibleOnMonitorContaining(*bounds, saved_work_area, bounds);
215 return true; 251 return true;
216 } 252 }
217 253
218 void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const { 254 void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const {
219 #if defined(USE_ASH) 255 #if defined(USE_ASH)
220 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312 256 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312
221 if (chrome::ShouldOpenAshOnStartup()) { 257 if (chrome::ShouldOpenAshOnStartup()) {
222 GetDefaultWindowBoundsAsh(default_bounds); 258 GetDefaultWindowBoundsAsh(default_bounds);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 const int min_x = work_area.x() + kMinVisibleWidth - bounds->width(); 361 const int min_x = work_area.x() + kMinVisibleWidth - bounds->width();
326 const int max_y = work_area.bottom() - kMinVisibleHeight; 362 const int max_y = work_area.bottom() - kMinVisibleHeight;
327 const int max_x = work_area.right() - kMinVisibleWidth; 363 const int max_x = work_area.right() - kMinVisibleWidth;
328 bounds->set_y(std::max(min_y, std::min(max_y, bounds->y()))); 364 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()))); 365 bounds->set_x(std::max(min_x, std::min(max_x, bounds->x())));
330 #endif // defined(OS_MACOSX) 366 #endif // defined(OS_MACOSX)
331 } 367 }
332 368
333 bool WindowSizer::GetBoundsOverride( 369 bool WindowSizer::GetBoundsOverride(
334 const gfx::Rect& specified_bounds, 370 const gfx::Rect& specified_bounds,
335 gfx::Rect* bounds) const { 371 gfx::Rect* bounds,
372 ui::WindowShowState& show_state) const {
336 #if defined(USE_ASH) 373 #if defined(USE_ASH)
337 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312 374 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312
338 if (chrome::ShouldOpenAshOnStartup()) 375 if (chrome::ShouldOpenAshOnStartup())
339 return GetBoundsOverrideAsh(specified_bounds, bounds); 376 return GetBoundsOverrideAsh(specified_bounds, bounds, show_state);
340 #endif 377 #endif
341 return false; 378 return false;
342 } 379 }
380
381 ui::WindowShowState WindowSizer::GetWindowDefaultShowState() const {
382 if (!browser_)
383 return ui::SHOW_STATE_DEFAULT;
384
385 // Only tabbed browsers use the command line or preference state, with the
386 // exception of devtools.
387 bool show_state = !browser_->is_type_tabbed() && !browser_->is_devtools();
388
389 #if defined(USE_AURA)
390 // We use the apps save state on aura.
391 show_state &= !browser_->is_app();
392 #endif
393
394 if (show_state)
395 return browser_->initial_show_state();
396
397 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kStartMaximized))
398 return ui::SHOW_STATE_MAXIMIZED;
399
400 if (browser_->initial_show_state() != ui::SHOW_STATE_DEFAULT)
401 return browser_->initial_show_state();
402
403 // Otherwise we use the default which can be overridden later on.
404 return ui::SHOW_STATE_DEFAULT;
405 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698