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

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

Issue 22888006: Use gfx::Screen to provide display information in WindowSizer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 4 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/command_line.h" 7 #include "base/command_line.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/ash/ash_init.h" 12 #include "chrome/browser/ui/ash/ash_init.h"
13 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_list.h" 14 #include "chrome/browser/ui/browser_list.h"
15 #include "chrome/browser/ui/browser_window.h" 15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/browser_window_state.h" 16 #include "chrome/browser/ui/browser_window_state.h"
17 #include "chrome/browser/ui/host_desktop.h" 17 #include "chrome/browser/ui/host_desktop.h"
18 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
20 #include "ui/gfx/screen.h" 20 #include "ui/gfx/screen.h"
21 21
22 // Minimum height of the visible part of a window. 22 // Minimum height of the visible part of a window.
23 const int kMinVisibleHeight = 30; 23 const int kMinVisibleHeight = 30;
24 // Minimum width of the visible part of a window. 24 // Minimum width of the visible part of a window.
25 const int kMinVisibleWidth = 30; 25 const int kMinVisibleWidth = 30;
26 26
27 class DefaultMonitorInfoProvider : public MonitorInfoProvider {
28 public:
29 explicit DefaultMonitorInfoProvider(const gfx::Screen* screen)
30 : screen_(screen) {}
31 // Overridden from MonitorInfoProvider:
32 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE {
33 return screen_->GetPrimaryDisplay().work_area();
34 }
35 virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE {
36 return screen_->GetPrimaryDisplay().bounds();
37 }
38 virtual gfx::Rect GetMonitorWorkAreaMatching(
39 const gfx::Rect& match_rect) const OVERRIDE {
40 return screen_->GetDisplayMatching(match_rect).work_area();
41 }
42 private:
43 const gfx::Screen* screen_;
44 DISALLOW_COPY_AND_ASSIGN(DefaultMonitorInfoProvider);
45 };
46
47 /////////////////////////////////////////////////////////////////////////////// 27 ///////////////////////////////////////////////////////////////////////////////
48 // An implementation of WindowSizer::StateProvider that gets the last active 28 // An implementation of WindowSizer::StateProvider that gets the last active
49 // and persistent state from the browser window and the user's profile. 29 // and persistent state from the browser window and the user's profile.
50 class DefaultStateProvider : public WindowSizer::StateProvider { 30 class DefaultStateProvider : public WindowSizer::StateProvider {
51 public: 31 public:
52 DefaultStateProvider(const std::string& app_name, const Browser* browser) 32 DefaultStateProvider(const std::string& app_name, const Browser* browser)
53 : app_name_(app_name), browser_(browser) { 33 : app_name_(app_name), browser_(browser) {
54 } 34 }
55 35
56 // Overridden from WindowSizer::StateProvider: 36 // Overridden from WindowSizer::StateProvider:
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 // gets positioned to its default location. 136 // gets positioned to its default location.
157 // static 137 // static
158 const int WindowSizer::kDesktopBorderSize = 16; 138 const int WindowSizer::kDesktopBorderSize = 16;
159 139
160 // Maximum width of a window even if there is more room on the desktop. 140 // Maximum width of a window even if there is more room on the desktop.
161 // static 141 // static
162 const int WindowSizer::kMaximumWindowWidth = 1100; 142 const int WindowSizer::kMaximumWindowWidth = 1100;
163 143
164 WindowSizer::WindowSizer(StateProvider* state_provider, const Browser* browser) 144 WindowSizer::WindowSizer(StateProvider* state_provider, const Browser* browser)
165 : state_provider_(state_provider), 145 : state_provider_(state_provider),
166 monitor_info_provider_(new DefaultMonitorInfoProvider( 146 // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312
167 // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312 147 screen_(gfx::Screen::GetNativeScreen()),
168 gfx::Screen::GetNativeScreen())),
169 browser_(browser) { 148 browser_(browser) {
170 } 149 }
171 150
172 WindowSizer::WindowSizer(StateProvider* state_provider, 151 WindowSizer::WindowSizer(StateProvider* state_provider,
173 MonitorInfoProvider* monitor_info_provider, 152 gfx::Screen* screen,
174 const Browser* browser) 153 const Browser* browser)
175 : state_provider_(state_provider), 154 : state_provider_(state_provider),
176 monitor_info_provider_(monitor_info_provider), 155 screen_(screen),
177 browser_(browser) { 156 browser_(browser) {
157 DCHECK(screen_);
178 } 158 }
179 159
180 WindowSizer::~WindowSizer() { 160 WindowSizer::~WindowSizer() {
181 } 161 }
182 162
183 // static 163 // static
184 void WindowSizer::GetBrowserWindowBoundsAndShowState( 164 void WindowSizer::GetBrowserWindowBoundsAndShowState(
185 const std::string& app_name, 165 const std::string& app_name,
186 const gfx::Rect& specified_bounds, 166 const gfx::Rect& specified_bounds,
187 const Browser* browser, 167 const Browser* browser,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 // No saved placement, figure out some sensible default size based on 203 // No saved placement, figure out some sensible default size based on
224 // the user's screen size. 204 // the user's screen size.
225 GetDefaultWindowBounds(bounds); 205 GetDefaultWindowBounds(bounds);
226 } else { 206 } else {
227 // In case that there was a bound given we need to make sure that it is 207 // In case that there was a bound given we need to make sure that it is
228 // visible and fits on the screen. 208 // visible and fits on the screen.
229 // Find the size of the work area of the monitor that intersects the bounds 209 // Find the size of the work area of the monitor that intersects the bounds
230 // of the anchor window. Note: AdjustBoundsToBeVisibleOnMonitorContaining 210 // of the anchor window. Note: AdjustBoundsToBeVisibleOnMonitorContaining
231 // does not exactly what we want: It makes only sure that "a minimal part" 211 // does not exactly what we want: It makes only sure that "a minimal part"
232 // is visible on the screen. 212 // is visible on the screen.
233 gfx::Rect work_area = 213 gfx::Rect work_area = screen_->GetDisplayMatching(*bounds).work_area();
234 monitor_info_provider_->GetMonitorWorkAreaMatching(*bounds);
235 // Resize so that it fits. 214 // Resize so that it fits.
236 bounds->AdjustToFit(work_area); 215 bounds->AdjustToFit(work_area);
237 } 216 }
238 } 217 }
239 218
240 bool WindowSizer::GetLastWindowBounds(gfx::Rect* bounds, 219 bool WindowSizer::GetLastWindowBounds(gfx::Rect* bounds,
241 ui::WindowShowState* show_state) const { 220 ui::WindowShowState* show_state) const {
242 DCHECK(bounds); 221 DCHECK(bounds);
243 DCHECK(show_state); 222 DCHECK(show_state);
244 if (!state_provider_.get() || 223 if (!state_provider_.get() ||
(...skipping 23 matching lines...) Expand all
268 247
269 void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const { 248 void WindowSizer::GetDefaultWindowBounds(gfx::Rect* default_bounds) const {
270 #if defined(USE_ASH) 249 #if defined(USE_ASH)
271 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312 250 // TODO(beng): insufficient but currently necessary. http://crbug.com/133312
272 if (chrome::ShouldOpenAshOnStartup()) { 251 if (chrome::ShouldOpenAshOnStartup()) {
273 GetDefaultWindowBoundsAsh(default_bounds); 252 GetDefaultWindowBoundsAsh(default_bounds);
274 return; 253 return;
275 } 254 }
276 #endif 255 #endif
277 DCHECK(default_bounds); 256 DCHECK(default_bounds);
278 DCHECK(monitor_info_provider_.get());
279 257
280 gfx::Rect work_area = monitor_info_provider_->GetPrimaryDisplayWorkArea(); 258 gfx::Rect work_area = screen_->GetPrimaryDisplay().work_area();
281 259
282 // The default size is either some reasonably wide width, or if the work 260 // The default size is either some reasonably wide width, or if the work
283 // area is narrower, then the work area width less some aesthetic padding. 261 // area is narrower, then the work area width less some aesthetic padding.
284 int default_width = std::min(work_area.width() - 2 * kWindowTilePixels, 1050); 262 int default_width = std::min(work_area.width() - 2 * kWindowTilePixels, 1050);
285 int default_height = work_area.height() - 2 * kWindowTilePixels; 263 int default_height = work_area.height() - 2 * kWindowTilePixels;
286 264
287 // For wider aspect ratio displays at higher resolutions, we might size the 265 // For wider aspect ratio displays at higher resolutions, we might size the
288 // window narrower to allow two windows to easily be placed side-by-side. 266 // window narrower to allow two windows to easily be placed side-by-side.
289 gfx::Rect screen_size = monitor_info_provider_->GetPrimaryDisplayBounds(); 267 gfx::Rect screen_size = screen_->GetPrimaryDisplay().bounds();
290 double width_to_height = 268 double width_to_height =
291 static_cast<double>(screen_size.width()) / screen_size.height(); 269 static_cast<double>(screen_size.width()) / screen_size.height();
292 270
293 // The least wide a screen can be to qualify for the halving described above. 271 // The least wide a screen can be to qualify for the halving described above.
294 static const int kMinScreenWidthForWindowHalving = 1600; 272 static const int kMinScreenWidthForWindowHalving = 1600;
295 // We assume 16:9/10 is a fairly standard indicator of a wide aspect ratio 273 // We assume 16:9/10 is a fairly standard indicator of a wide aspect ratio
296 // computer display. 274 // computer display.
297 if (((width_to_height * 10) >= 16) && 275 if (((width_to_height * 10) >= 16) &&
298 work_area.width() > kMinScreenWidthForWindowHalving) { 276 work_area.width() > kMinScreenWidthForWindowHalving) {
299 // Halve the work area, subtracting aesthetic padding on either side. 277 // Halve the work area, subtracting aesthetic padding on either side.
300 // The padding is set so that two windows, side by side have 278 // The padding is set so that two windows, side by side have
301 // kWindowTilePixels between screen edge and each other. 279 // kWindowTilePixels between screen edge and each other.
302 default_width = static_cast<int>(work_area.width() / 2. - 280 default_width = static_cast<int>(work_area.width() / 2. -
303 1.5 * kWindowTilePixels); 281 1.5 * kWindowTilePixels);
304 } 282 }
305 default_bounds->SetRect(kWindowTilePixels + work_area.x(), 283 default_bounds->SetRect(kWindowTilePixels + work_area.x(),
306 kWindowTilePixels + work_area.y(), 284 kWindowTilePixels + work_area.y(),
307 default_width, default_height); 285 default_width, default_height);
308 } 286 }
309 287
310 void WindowSizer::AdjustBoundsToBeVisibleOnMonitorContaining( 288 void WindowSizer::AdjustBoundsToBeVisibleOnMonitorContaining(
311 const gfx::Rect& other_bounds, 289 const gfx::Rect& other_bounds,
312 const gfx::Rect& saved_work_area, 290 const gfx::Rect& saved_work_area,
313 gfx::Rect* bounds) const { 291 gfx::Rect* bounds) const {
314 DCHECK(bounds); 292 DCHECK(bounds);
315 DCHECK(monitor_info_provider_.get());
316 293
317 // Find the size of the work area of the monitor that intersects the bounds 294 // Find the size of the work area of the monitor that intersects the bounds
318 // of the anchor window. 295 // of the anchor window.
319 gfx::Rect work_area = 296 gfx::Rect work_area = screen_->GetDisplayMatching(other_bounds).work_area();
320 monitor_info_provider_->GetMonitorWorkAreaMatching(other_bounds);
321 297
322 // If height or width are 0, reset to the default size. 298 // If height or width are 0, reset to the default size.
323 gfx::Rect default_bounds; 299 gfx::Rect default_bounds;
324 GetDefaultWindowBounds(&default_bounds); 300 GetDefaultWindowBounds(&default_bounds);
325 if (bounds->height() <= 0) 301 if (bounds->height() <= 0)
326 bounds->set_height(default_bounds.height()); 302 bounds->set_height(default_bounds.height());
327 if (bounds->width() <= 0) 303 if (bounds->width() <= 0)
328 bounds->set_width(default_bounds.width()); 304 bounds->set_width(default_bounds.width());
329 305
330 // Ensure the minimum height and width. 306 // Ensure the minimum height and width.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 375
400 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kStartMaximized)) 376 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kStartMaximized))
401 return ui::SHOW_STATE_MAXIMIZED; 377 return ui::SHOW_STATE_MAXIMIZED;
402 378
403 if (browser_->initial_show_state() != ui::SHOW_STATE_DEFAULT) 379 if (browser_->initial_show_state() != ui::SHOW_STATE_DEFAULT)
404 return browser_->initial_show_state(); 380 return browser_->initial_show_state();
405 381
406 // Otherwise we use the default which can be overridden later on. 382 // Otherwise we use the default which can be overridden later on.
407 return ui::SHOW_STATE_DEFAULT; 383 return ui::SHOW_STATE_DEFAULT;
408 } 384 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/window_sizer/window_sizer.h ('k') | chrome/browser/ui/window_sizer/window_sizer_ash.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698