Move render_view_context_menu.* and related files out of tab_contents.
[chromium-blink-merge.git] / ash / wm / window_positioner.cc
blob8aaabc71e80522ad8f09fedbc87ef0dbcd759546
1 // Copyright 2013 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 "ash/wm/window_positioner.h"
7 #include "ash/ash_switches.h"
8 #include "ash/screen_util.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/mru_window_tracker.h"
12 #include "ash/wm/window_resizer.h"
13 #include "ash/wm/window_state.h"
14 #include "ash/wm/window_util.h"
15 #include "base/command_line.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_delegate.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/compositor/layer.h"
20 #include "ui/compositor/scoped_layer_animation_settings.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/views/corewm/window_animations.h"
24 namespace ash {
26 const int WindowPositioner::kMinimumWindowOffset = 32;
28 // The number of pixels which are kept free top, left and right when a window
29 // gets positioned to its default location.
30 // static
31 const int WindowPositioner::kDesktopBorderSize = 16;
33 // Maximum width of a window even if there is more room on the desktop.
34 // static
35 const int WindowPositioner::kMaximumWindowWidth = 1100;
37 namespace {
39 // When a window gets opened in default mode and the screen is less than or
40 // equal to this width, the window will get opened in maximized mode. This value
41 // can be reduced to a "tame" number if the feature is disabled.
42 const int kForceMaximizeWidthLimit = 1366;
44 // The time in milliseconds which should be used to visually move a window
45 // through an automatic "intelligent" window management option.
46 const int kWindowAutoMoveDurationMS = 125;
48 // If set to true all window repositioning actions will be ignored. Set through
49 // WindowPositioner::SetIgnoreActivations().
50 static bool disable_auto_positioning = false;
52 // If set to true, by default the first window in ASH will be maxmized.
53 static bool maximize_first_window = false;
55 // Check if any management should be performed (with a given |window|).
56 bool UseAutoWindowManager(const aura::Window* window) {
57 if (disable_auto_positioning)
58 return false;
59 const wm::WindowState* window_state = wm::GetWindowState(window);
60 return !window_state->is_dragged() && window_state->window_position_managed();
63 // Check if a given |window| can be managed. This includes that it's state is
64 // not minimized/maximized/the user has changed it's size by hand already.
65 // It furthermore checks for the WindowIsManaged status.
66 bool WindowPositionCanBeManaged(const aura::Window* window) {
67 if (disable_auto_positioning)
68 return false;
69 const wm::WindowState* window_state = wm::GetWindowState(window);
70 return window_state->window_position_managed() &&
71 !window_state->IsMinimized() &&
72 !window_state->IsMaximized() &&
73 !window_state->bounds_changed_by_user();
76 // Get the work area for a given |window| in parent coordinates.
77 gfx::Rect GetWorkAreaForWindowInParent(aura::Window* window) {
78 #if defined(OS_WIN)
79 // On Win 8, the host window can't be resized, so
80 // use window's bounds instead.
81 // TODO(oshima): Emulate host window resize on win8.
82 gfx::Rect work_area = gfx::Rect(window->parent()->bounds().size());
83 work_area.Inset(Shell::GetScreen()->GetDisplayMatching(
84 window->parent()->GetBoundsInScreen()).GetWorkAreaInsets());
85 return work_area;
86 #else
87 return ScreenUtil::GetDisplayWorkAreaBoundsInParent(window);
88 #endif
91 // Move the given |bounds| on the available |work_area| in the direction
92 // indicated by |move_right|. If |move_right| is true, the rectangle gets moved
93 // to the right edge, otherwise to the left one.
94 bool MoveRectToOneSide(const gfx::Rect& work_area,
95 bool move_right,
96 gfx::Rect* bounds) {
97 if (move_right) {
98 if (work_area.right() > bounds->right()) {
99 bounds->set_x(work_area.right() - bounds->width());
100 return true;
102 } else {
103 if (work_area.x() < bounds->x()) {
104 bounds->set_x(work_area.x());
105 return true;
108 return false;
111 // Move a |window| to a new |bound|. Animate if desired by user.
112 // Note: The function will do nothing if the bounds did not change.
113 void SetBoundsAnimated(aura::Window* window, const gfx::Rect& bounds) {
114 if (bounds == window->GetTargetBounds())
115 return;
117 if (views::corewm::WindowAnimationsDisabled(window)) {
118 window->SetBounds(bounds);
119 return;
122 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
123 settings.SetTransitionDuration(
124 base::TimeDelta::FromMilliseconds(kWindowAutoMoveDurationMS));
125 window->SetBounds(bounds);
128 // Move |window| into the center of the screen - or restore it to the previous
129 // position.
130 void AutoPlaceSingleWindow(aura::Window* window, bool animated) {
131 gfx::Rect work_area = GetWorkAreaForWindowInParent(window);
132 gfx::Rect bounds = window->bounds();
133 const gfx::Rect* user_defined_area =
134 wm::GetWindowState(window)->pre_auto_manage_window_bounds();
135 if (user_defined_area) {
136 bounds = *user_defined_area;
137 ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area, &bounds);
138 } else {
139 // Center the window (only in x).
140 bounds.set_x(work_area.x() + (work_area.width() - bounds.width()) / 2);
143 if (animated)
144 SetBoundsAnimated(window, bounds);
145 else
146 window->SetBounds(bounds);
149 // Get the first open (non minimized) window which is on the screen defined.
150 aura::Window* GetReferenceWindow(const aura::Window* root_window,
151 const aura::Window* exclude,
152 bool *single_window) {
153 if (single_window)
154 *single_window = true;
155 // Get the active window.
156 aura::Window* active = ash::wm::GetActiveWindow();
157 if (active && active->GetRootWindow() != root_window)
158 active = NULL;
160 // Get a list of all windows.
161 const std::vector<aura::Window*> windows =
162 ash::MruWindowTracker::BuildWindowList(false);
164 if (windows.empty())
165 return NULL;
167 aura::Window::Windows::const_iterator iter = windows.begin();
168 // Find the index of the current active window.
169 if (active)
170 iter = std::find(windows.begin(), windows.end(), active);
172 int index = (iter == windows.end()) ? 0 : (iter - windows.begin());
174 // Scan the cycle list backwards to see which is the second topmost window
175 // (and so on). Note that we might cycle a few indices twice if there is no
176 // suitable window. However - since the list is fairly small this should be
177 // very fast anyways.
178 aura::Window* found = NULL;
179 for (int i = index + windows.size(); i >= 0; i--) {
180 aura::Window* window = windows[i % windows.size()];
181 if (window != exclude && window->type() == ui::wm::WINDOW_TYPE_NORMAL &&
182 window->GetRootWindow() == root_window && window->TargetVisibility() &&
183 wm::GetWindowState(window)->window_position_managed()) {
184 if (found && found != window) {
185 // no need to check !signle_window because the function must have
186 // been already returned in the "if (!single_window)" below.
187 *single_window = false;
188 return found;
190 found = window;
191 // If there is no need to check single window, return now.
192 if (!single_window)
193 return found;
196 return found;
199 } // namespace
201 // static
202 int WindowPositioner::GetForceMaximizedWidthLimit() {
203 return kForceMaximizeWidthLimit;
206 // static
207 void WindowPositioner::GetBoundsAndShowStateForNewWindow(
208 const gfx::Screen* screen,
209 const aura::Window* new_window,
210 bool is_saved_bounds,
211 ui::WindowShowState show_state_in,
212 gfx::Rect* bounds_in_out,
213 ui::WindowShowState* show_state_out) {
215 // Always open new window in the target display.
216 aura::Window* target = Shell::GetTargetRootWindow();
218 aura::Window* top_window = GetReferenceWindow(target, NULL, NULL);
219 // Our window should not have any impact if we are already on top.
220 if (top_window == new_window)
221 top_window = NULL;
223 // If there is no valid other window we take and adjust the passed coordinates
224 // and show state.
225 if (!top_window) {
226 gfx::Rect work_area = screen->GetDisplayNearestWindow(target).work_area();
228 bounds_in_out->AdjustToFit(work_area);
229 // Use adjusted saved bounds, if there is one.
230 if (is_saved_bounds)
231 return;
232 // When using "small screens" we want to always open in full screen mode.
233 if (show_state_in == ui::SHOW_STATE_DEFAULT && (maximize_first_window ||
234 (work_area.width() <= GetForceMaximizedWidthLimit() &&
235 (!new_window || !wm::GetWindowState(new_window)->IsFullscreen())))) {
236 *show_state_out = ui::SHOW_STATE_MAXIMIZED;
238 return;
240 bool maximized = wm::GetWindowState(top_window)->IsMaximized();
241 // We ignore the saved show state, but look instead for the top level
242 // window's show state.
243 if (show_state_in == ui::SHOW_STATE_DEFAULT) {
244 *show_state_out = maximized ? ui::SHOW_STATE_MAXIMIZED :
245 ui::SHOW_STATE_DEFAULT;
248 // Use the size of the other window. The window's bound will be rearranged
249 // in ash::WorkspaceLayoutManager using this location.
250 *bounds_in_out = top_window->GetBoundsInScreen();
253 // static
254 void WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(
255 const aura::Window* removed_window) {
256 if (!UseAutoWindowManager(removed_window))
257 return;
258 // Find a single open browser window.
259 bool single_window;
260 aura::Window* other_shown_window = GetReferenceWindow(
261 removed_window->GetRootWindow(), removed_window, &single_window);
262 if (!other_shown_window || !single_window ||
263 !WindowPositionCanBeManaged(other_shown_window))
264 return;
265 AutoPlaceSingleWindow(other_shown_window, true);
268 // static
269 bool WindowPositioner::DisableAutoPositioning(bool ignore) {
270 bool old_state = disable_auto_positioning;
271 disable_auto_positioning = ignore;
272 return old_state;
275 // static
276 void WindowPositioner::RearrangeVisibleWindowOnShow(
277 aura::Window* added_window) {
278 wm::WindowState* added_window_state = wm::GetWindowState(added_window);
279 if (!added_window->TargetVisibility())
280 return;
282 if (!UseAutoWindowManager(added_window) ||
283 added_window_state->bounds_changed_by_user()) {
284 if (added_window_state->minimum_visibility()) {
285 // Guarante minimum visibility within the work area.
286 gfx::Rect work_area = GetWorkAreaForWindowInParent(added_window);
287 gfx::Rect bounds = added_window->bounds();
288 gfx::Rect new_bounds = bounds;
289 ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area,
290 &new_bounds);
291 if (new_bounds != bounds)
292 added_window->SetBounds(new_bounds);
294 return;
296 // Find a single open managed window.
297 bool single_window;
298 aura::Window* other_shown_window = GetReferenceWindow(
299 added_window->GetRootWindow(), added_window, &single_window);
301 if (!other_shown_window) {
302 // It could be that this window is the first window joining the workspace.
303 if (!WindowPositionCanBeManaged(added_window) || other_shown_window)
304 return;
305 // Since we might be going from 0 to 1 window, we have to arrange the new
306 // window to a good default.
307 AutoPlaceSingleWindow(added_window, false);
308 return;
311 gfx::Rect other_bounds = other_shown_window->bounds();
312 gfx::Rect work_area = GetWorkAreaForWindowInParent(added_window);
313 bool move_other_right =
314 other_bounds.CenterPoint().x() > work_area.x() + work_area.width() / 2;
316 // Push the other window to the size only if there are two windows left.
317 if (single_window) {
318 // When going from one to two windows both windows loose their
319 // "positioned by user" flags.
320 added_window_state->set_bounds_changed_by_user(false);
321 wm::WindowState* other_window_state =
322 wm::GetWindowState(other_shown_window);
323 other_window_state->set_bounds_changed_by_user(false);
325 if (WindowPositionCanBeManaged(other_shown_window)) {
326 // Don't override pre auto managed bounds as the current bounds
327 // may not be original.
328 if (!other_window_state->pre_auto_manage_window_bounds())
329 other_window_state->SetPreAutoManageWindowBounds(other_bounds);
331 // Push away the other window after remembering its current position.
332 if (MoveRectToOneSide(work_area, move_other_right, &other_bounds))
333 SetBoundsAnimated(other_shown_window, other_bounds);
337 // Remember the current location of the window if it's new and push
338 // it also to the opposite location if needed. Since it is just
339 // being shown, we do not need to animate it.
340 gfx::Rect added_bounds = added_window->bounds();
341 if (!added_window_state->pre_auto_manage_window_bounds())
342 added_window_state->SetPreAutoManageWindowBounds(added_bounds);
343 if (MoveRectToOneSide(work_area, !move_other_right, &added_bounds))
344 added_window->SetBounds(added_bounds);
347 WindowPositioner::WindowPositioner()
348 : pop_position_offset_increment_x(0),
349 pop_position_offset_increment_y(0),
350 popup_position_offset_from_screen_corner_x(0),
351 popup_position_offset_from_screen_corner_y(0),
352 last_popup_position_x_(0),
353 last_popup_position_y_(0) {
356 WindowPositioner::~WindowPositioner() {
359 gfx::Rect WindowPositioner::GetDefaultWindowBounds(
360 const gfx::Display& display) {
361 const gfx::Rect work_area = display.work_area();
362 // There should be a 'desktop' border around the window at the left and right
363 // side.
364 int default_width = work_area.width() - 2 * kDesktopBorderSize;
365 // There should also be a 'desktop' border around the window at the top.
366 // Since the workspace excludes the tray area we only need one border size.
367 int default_height = work_area.height() - kDesktopBorderSize;
368 int offset_x = kDesktopBorderSize;
369 if (default_width > kMaximumWindowWidth) {
370 // The window should get centered on the screen and not follow the grid.
371 offset_x = (work_area.width() - kMaximumWindowWidth) / 2;
372 default_width = kMaximumWindowWidth;
374 return gfx::Rect(work_area.x() + offset_x,
375 work_area.y() + kDesktopBorderSize,
376 default_width,
377 default_height);
380 gfx::Rect WindowPositioner::GetPopupPosition(const gfx::Rect& old_pos) {
381 int grid = kMinimumWindowOffset;
382 popup_position_offset_from_screen_corner_x = grid;
383 popup_position_offset_from_screen_corner_y = grid;
384 if (!pop_position_offset_increment_x) {
385 // When the popup position increment is , the last popup position
386 // was not yet initialized.
387 last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
388 last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
390 pop_position_offset_increment_x = grid;
391 pop_position_offset_increment_y = grid;
392 // We handle the Multi monitor support by retrieving the active window's
393 // work area.
394 aura::Window* window = wm::GetActiveWindow();
395 const gfx::Rect work_area = window && window->IsVisible() ?
396 Shell::GetScreen()->GetDisplayNearestWindow(window).work_area() :
397 Shell::GetScreen()->GetPrimaryDisplay().work_area();
398 // Only try to reposition the popup when it is not spanning the entire
399 // screen.
400 if ((old_pos.width() + popup_position_offset_from_screen_corner_x >=
401 work_area.width()) ||
402 (old_pos.height() + popup_position_offset_from_screen_corner_y >=
403 work_area.height()))
404 return AlignPopupPosition(old_pos, work_area, grid);
405 const gfx::Rect result = SmartPopupPosition(old_pos, work_area, grid);
406 if (!result.IsEmpty())
407 return AlignPopupPosition(result, work_area, grid);
408 return NormalPopupPosition(old_pos, work_area);
411 // static
412 void WindowPositioner::SetMaximizeFirstWindow(bool maximize) {
413 maximize_first_window = maximize;
416 gfx::Rect WindowPositioner::NormalPopupPosition(
417 const gfx::Rect& old_pos,
418 const gfx::Rect& work_area) {
419 int w = old_pos.width();
420 int h = old_pos.height();
421 // Note: The 'last_popup_position' is checked and kept relative to the
422 // screen size. The offsetting will be done in the last step when the
423 // target rectangle gets returned.
424 bool reset = false;
425 if (last_popup_position_y_ + h > work_area.height() ||
426 last_popup_position_x_ + w > work_area.width()) {
427 // Popup does not fit on screen. Reset to next diagonal row.
428 last_popup_position_x_ -= last_popup_position_y_ -
429 popup_position_offset_from_screen_corner_x -
430 pop_position_offset_increment_x;
431 last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
432 reset = true;
434 if (last_popup_position_x_ + w > work_area.width()) {
435 // Start again over.
436 last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
437 last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
438 reset = true;
440 int x = last_popup_position_x_;
441 int y = last_popup_position_y_;
442 if (!reset) {
443 last_popup_position_x_ += pop_position_offset_increment_x;
444 last_popup_position_y_ += pop_position_offset_increment_y;
446 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
449 gfx::Rect WindowPositioner::SmartPopupPosition(
450 const gfx::Rect& old_pos,
451 const gfx::Rect& work_area,
452 int grid) {
453 const std::vector<aura::Window*> windows =
454 MruWindowTracker::BuildWindowList(false);
456 std::vector<const gfx::Rect*> regions;
457 // Process the window list and check if we can bail immediately.
458 for (size_t i = 0; i < windows.size(); i++) {
459 // We only include opaque and visible windows.
460 if (windows[i] && windows[i]->IsVisible() && windows[i]->layer() &&
461 (!windows[i]->transparent() ||
462 windows[i]->layer()->GetTargetOpacity() == 1.0)) {
463 wm::WindowState* window_state = wm::GetWindowState(windows[i]);
464 // When any window is maximized we cannot find any free space.
465 if (window_state->IsMaximizedOrFullscreen())
466 return gfx::Rect(0, 0, 0, 0);
467 if (window_state->IsNormalOrSnapped())
468 regions.push_back(&windows[i]->bounds());
472 if (regions.empty())
473 return gfx::Rect(0, 0, 0, 0);
475 int w = old_pos.width();
476 int h = old_pos.height();
477 int x_end = work_area.width() / 2;
478 int x, x_increment;
479 // We parse for a proper location on the screen. We do this in two runs:
480 // The first run will start from the left, parsing down, skipping any
481 // overlapping windows it will encounter until the popup's height can not
482 // be served anymore. Then the next grid position to the right will be
483 // taken, and the same cycle starts again. This will be repeated until we
484 // hit the middle of the screen (or we find a suitable location).
485 // In the second run we parse beginning from the right corner downwards and
486 // then to the left.
487 // When no location was found, an empty rectangle will be returned.
488 for (int run = 0; run < 2; run++) {
489 if (run == 0) { // First run: Start left, parse right till mid screen.
490 x = 0;
491 x_increment = pop_position_offset_increment_x;
492 } else { // Second run: Start right, parse left till mid screen.
493 x = work_area.width() - w;
494 x_increment = -pop_position_offset_increment_x;
496 // Note: The passing (x,y,w,h) window is always relative to the work area's
497 // origin.
498 for (; x_increment > 0 ? (x < x_end) : (x > x_end); x += x_increment) {
499 int y = 0;
500 while (y + h <= work_area.height()) {
501 size_t i;
502 for (i = 0; i < regions.size(); i++) {
503 if (regions[i]->Intersects(gfx::Rect(x + work_area.x(),
504 y + work_area.y(), w, h))) {
505 y = regions[i]->bottom() - work_area.y();
506 break;
509 if (i >= regions.size())
510 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
514 return gfx::Rect(0, 0, 0, 0);
517 gfx::Rect WindowPositioner::AlignPopupPosition(
518 const gfx::Rect& pos,
519 const gfx::Rect& work_area,
520 int grid) {
521 if (grid <= 1)
522 return pos;
524 int x = pos.x() - (pos.x() - work_area.x()) % grid;
525 int y = pos.y() - (pos.y() - work_area.y()) % grid;
526 int w = pos.width();
527 int h = pos.height();
529 // If the alignment was pushing the window out of the screen, we ignore the
530 // alignment for that call.
531 if (abs(pos.right() - work_area.right()) < grid)
532 x = work_area.right() - w;
533 if (abs(pos.bottom() - work_area.bottom()) < grid)
534 y = work_area.bottom() - h;
535 return gfx::Rect(x, y, w, h);
538 } // namespace ash