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

Side by Side Diff: ash/wm/common/window_positioner.cc

Issue 2030593002: Renames ash/wm/common into ash/common/wm (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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
« no previous file with comments | « ash/wm/common/window_positioner.h ('k') | ash/wm/common/window_positioning_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "ash/wm/common/window_positioner.h"
6
7 #include "ash/wm/common/window_positioning_utils.h"
8 #include "ash/wm/common/window_state.h"
9 #include "ash/wm/common/wm_globals.h"
10 #include "ash/wm/common/wm_screen_util.h"
11 #include "ash/wm/common/wm_window.h"
12 #include "ui/compositor/layer.h"
13 #include "ui/display/display.h"
14 #include "ui/display/screen.h"
15 #include "ui/gfx/geometry/insets.h"
16
17 namespace ash {
18
19 const int WindowPositioner::kMinimumWindowOffset = 32;
20
21 // The number of pixels which are kept free top, left and right when a window
22 // gets positioned to its default location.
23 // static
24 const int WindowPositioner::kDesktopBorderSize = 16;
25
26 // Maximum width of a window even if there is more room on the desktop.
27 // static
28 const int WindowPositioner::kMaximumWindowWidth = 1100;
29
30 namespace {
31
32 // When a window gets opened in default mode and the screen is less than or
33 // equal to this width, the window will get opened in maximized mode. This value
34 // can be reduced to a "tame" number if the feature is disabled.
35 const int kForceMaximizeWidthLimit = 1366;
36
37 // The time in milliseconds which should be used to visually move a window
38 // through an automatic "intelligent" window management option.
39 const int kWindowAutoMoveDurationMS = 125;
40
41 // If set to true all window repositioning actions will be ignored. Set through
42 // WindowPositioner::SetIgnoreActivations().
43 static bool disable_auto_positioning = false;
44
45 // If set to true, by default the first window in ASH will be maximized.
46 static bool maximize_first_window = false;
47
48 // Check if any management should be performed (with a given |window|).
49 bool UseAutoWindowManager(const wm::WmWindow* window) {
50 if (disable_auto_positioning)
51 return false;
52 const wm::WindowState* window_state = window->GetWindowState();
53 return !window_state->is_dragged() && window_state->window_position_managed();
54 }
55
56 // Check if a given |window| can be managed. This includes that its
57 // state is not minimized/maximized/fullscreen/the user has changed
58 // its size by hand already. It furthermore checks for the
59 // WindowIsManaged status.
60 bool WindowPositionCanBeManaged(const wm::WmWindow* window) {
61 if (disable_auto_positioning)
62 return false;
63 const wm::WindowState* window_state = window->GetWindowState();
64 return window_state->window_position_managed() &&
65 !window_state->IsMinimized() && !window_state->IsMaximized() &&
66 !window_state->IsFullscreen() &&
67 !window_state->bounds_changed_by_user();
68 }
69
70 // Move the given |bounds| on the available |work_area| in the direction
71 // indicated by |move_right|. If |move_right| is true, the rectangle gets moved
72 // to the right edge, otherwise to the left one.
73 bool MoveRectToOneSide(const gfx::Rect& work_area,
74 bool move_right,
75 gfx::Rect* bounds) {
76 if (move_right) {
77 if (work_area.right() > bounds->right()) {
78 bounds->set_x(work_area.right() - bounds->width());
79 return true;
80 }
81 } else {
82 if (work_area.x() < bounds->x()) {
83 bounds->set_x(work_area.x());
84 return true;
85 }
86 }
87 return false;
88 }
89
90 // Move a |window| to new |bounds|. Animate if desired by user.
91 // Moves the transient children of the |window| as well by the same |offset| as
92 // the parent |window|.
93 void SetBoundsAndOffsetTransientChildren(wm::WmWindow* window,
94 const gfx::Rect& bounds,
95 const gfx::Rect& work_area,
96 const gfx::Vector2d& offset) {
97 std::vector<wm::WmWindow*> transient_children =
98 window->GetTransientChildren();
99 for (wm::WmWindow* transient_child : transient_children) {
100 gfx::Rect child_bounds = transient_child->GetBounds();
101 gfx::Rect new_child_bounds = child_bounds + offset;
102 if ((child_bounds.x() <= work_area.x() &&
103 new_child_bounds.x() <= work_area.x()) ||
104 (child_bounds.right() >= work_area.right() &&
105 new_child_bounds.right() >= work_area.right())) {
106 continue;
107 }
108 if (new_child_bounds.right() > work_area.right())
109 new_child_bounds.set_x(work_area.right() - bounds.width());
110 else if (new_child_bounds.x() < work_area.x())
111 new_child_bounds.set_x(work_area.x());
112 SetBoundsAndOffsetTransientChildren(transient_child, new_child_bounds,
113 work_area, offset);
114 }
115
116 window->SetBoundsWithTransitionDelay(
117 bounds, base::TimeDelta::FromMilliseconds(kWindowAutoMoveDurationMS));
118 }
119
120 // Move a |window| to new |bounds|. Animate if desired by user.
121 // Note: The function will do nothing if the bounds did not change.
122 void SetBoundsAnimated(wm::WmWindow* window,
123 const gfx::Rect& bounds,
124 const gfx::Rect& work_area) {
125 gfx::Rect old_bounds = window->GetTargetBounds();
126 if (bounds == old_bounds)
127 return;
128 gfx::Vector2d offset(bounds.origin() - old_bounds.origin());
129 SetBoundsAndOffsetTransientChildren(window, bounds, work_area, offset);
130 }
131
132 // Move |window| into the center of the screen - or restore it to the previous
133 // position.
134 void AutoPlaceSingleWindow(wm::WmWindow* window, bool animated) {
135 gfx::Rect work_area = GetDisplayWorkAreaBoundsInParent(window);
136 gfx::Rect bounds = window->GetBounds();
137 const gfx::Rect* user_defined_area =
138 window->GetWindowState()->pre_auto_manage_window_bounds();
139 if (user_defined_area) {
140 bounds = *user_defined_area;
141 wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area, &bounds);
142 } else {
143 // Center the window (only in x).
144 bounds.set_x(work_area.x() + (work_area.width() - bounds.width()) / 2);
145 }
146
147 if (animated)
148 SetBoundsAnimated(window, bounds, work_area);
149 else
150 window->SetBounds(bounds);
151 }
152
153 // Get the first open (non minimized) window which is on the screen defined.
154 wm::WmWindow* GetReferenceWindow(const wm::WmWindow* root_window,
155 const wm::WmWindow* exclude,
156 bool* single_window) {
157 if (single_window)
158 *single_window = true;
159 // Get the active window.
160 wm::WmWindow* active = root_window->GetGlobals()->GetActiveWindow();
161 if (active && active->GetRootWindow() != root_window)
162 active = NULL;
163
164 // Get a list of all windows.
165 const std::vector<wm::WmWindow*> windows =
166 root_window->GetGlobals()->GetMruWindowListIgnoreModals();
167
168 if (windows.empty())
169 return nullptr;
170
171 int index = 0;
172 // Find the index of the current active window.
173 if (active)
174 index = std::find(windows.begin(), windows.end(), active) - windows.begin();
175
176 // Scan the cycle list backwards to see which is the second topmost window
177 // (and so on). Note that we might cycle a few indices twice if there is no
178 // suitable window. However - since the list is fairly small this should be
179 // very fast anyways.
180 wm::WmWindow* found = nullptr;
181 for (int i = index + windows.size(); i >= 0; i--) {
182 wm::WmWindow* window = windows[i % windows.size()];
183 while (window->GetTransientParent())
184 window = window->GetTransientParent();
185 if (window != exclude && window->GetType() == ui::wm::WINDOW_TYPE_NORMAL &&
186 window->GetRootWindow() == root_window &&
187 window->GetTargetVisibility() &&
188 window->GetWindowState()->window_position_managed()) {
189 if (found && found != window) {
190 // no need to check !single_window because the function must have
191 // been already returned in the "if (!single_window)" below.
192 *single_window = false;
193 return found;
194 }
195 found = window;
196 // If there is no need to check single window, return now.
197 if (!single_window)
198 return found;
199 }
200 }
201 return found;
202 }
203
204 } // namespace
205
206 // static
207 int WindowPositioner::GetForceMaximizedWidthLimit() {
208 return kForceMaximizeWidthLimit;
209 }
210
211 // static
212 void WindowPositioner::GetBoundsAndShowStateForNewWindow(
213 const wm::WmWindow* new_window,
214 bool is_saved_bounds,
215 ui::WindowShowState show_state_in,
216 gfx::Rect* bounds_in_out,
217 ui::WindowShowState* show_state_out) {
218 // Always open new window in the target display.
219 wm::WmWindow* target = wm::WmGlobals::Get()->GetRootWindowForNewWindows();
220
221 wm::WmWindow* top_window = GetReferenceWindow(target, nullptr, nullptr);
222 // Our window should not have any impact if we are already on top.
223 if (top_window == new_window)
224 top_window = nullptr;
225
226 // If there is no valid other window we take and adjust the passed coordinates
227 // and show state.
228 if (!top_window) {
229 gfx::Rect work_area = target->GetDisplayNearestWindow().work_area();
230
231 bounds_in_out->AdjustToFit(work_area);
232 // Use adjusted saved bounds, if there is one.
233 if (is_saved_bounds)
234 return;
235
236 if (show_state_in == ui::SHOW_STATE_DEFAULT) {
237 const bool maximize_first_window_on_first_run =
238 target->GetGlobals()->IsForceMaximizeOnFirstRun();
239 // We want to always open maximized on "small screens" or when policy
240 // tells us to.
241 const bool set_maximized =
242 maximize_first_window ||
243 ((work_area.width() <= GetForceMaximizedWidthLimit() ||
244 maximize_first_window_on_first_run) &&
245 (!new_window || !new_window->GetWindowState()->IsFullscreen()));
246
247 if (set_maximized)
248 *show_state_out = ui::SHOW_STATE_MAXIMIZED;
249 }
250 return;
251 }
252
253 wm::WindowState* top_window_state = top_window->GetWindowState();
254 bool maximized = top_window_state->IsMaximized();
255 // We ignore the saved show state, but look instead for the top level
256 // window's show state.
257 if (show_state_in == ui::SHOW_STATE_DEFAULT) {
258 *show_state_out =
259 maximized ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_DEFAULT;
260 }
261
262 if (maximized || top_window_state->IsFullscreen()) {
263 bool has_restore_bounds = top_window_state->HasRestoreBounds();
264 if (has_restore_bounds) {
265 // For a maximized/fullscreen window ignore the real bounds of
266 // the top level window and use its restore bounds
267 // instead. Offset the bounds to prevent the windows from
268 // overlapping exactly when restored.
269 *bounds_in_out =
270 top_window_state->GetRestoreBoundsInScreen() +
271 gfx::Vector2d(kMinimumWindowOffset, kMinimumWindowOffset);
272 }
273 if (is_saved_bounds || has_restore_bounds) {
274 gfx::Rect work_area = target->GetDisplayNearestWindow().work_area();
275 bounds_in_out->AdjustToFit(work_area);
276 // Use adjusted saved bounds or restore bounds, if there is one.
277 return;
278 }
279 }
280
281 // Use the size of the other window. The window's bound will be rearranged
282 // in ash::WorkspaceLayoutManager using this location.
283 *bounds_in_out = top_window->GetBoundsInScreen();
284 }
285
286 // static
287 void WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(
288 const wm::WmWindow* removed_window) {
289 if (!UseAutoWindowManager(removed_window))
290 return;
291 // Find a single open browser window.
292 bool single_window;
293 wm::WmWindow* other_shown_window = GetReferenceWindow(
294 removed_window->GetRootWindow(), removed_window, &single_window);
295 if (!other_shown_window || !single_window ||
296 !WindowPositionCanBeManaged(other_shown_window))
297 return;
298 AutoPlaceSingleWindow(other_shown_window, true);
299 }
300
301 // static
302 bool WindowPositioner::DisableAutoPositioning(bool ignore) {
303 bool old_state = disable_auto_positioning;
304 disable_auto_positioning = ignore;
305 return old_state;
306 }
307
308 // static
309 void WindowPositioner::RearrangeVisibleWindowOnShow(
310 wm::WmWindow* added_window) {
311 wm::WindowState* added_window_state = added_window->GetWindowState();
312 if (!added_window->GetTargetVisibility())
313 return;
314
315 if (!UseAutoWindowManager(added_window) ||
316 added_window_state->bounds_changed_by_user()) {
317 if (added_window_state->minimum_visibility()) {
318 // Guarantee minimum visibility within the work area.
319 gfx::Rect work_area = GetDisplayWorkAreaBoundsInParent(added_window);
320 gfx::Rect bounds = added_window->GetBounds();
321 gfx::Rect new_bounds = bounds;
322 wm::AdjustBoundsToEnsureMinimumWindowVisibility(work_area, &new_bounds);
323 if (new_bounds != bounds)
324 added_window->SetBounds(new_bounds);
325 }
326 return;
327 }
328 // Find a single open managed window.
329 bool single_window;
330 wm::WmWindow* other_shown_window = GetReferenceWindow(
331 added_window->GetRootWindow(), added_window, &single_window);
332
333 if (!other_shown_window) {
334 // It could be that this window is the first window joining the workspace.
335 if (!WindowPositionCanBeManaged(added_window) || other_shown_window)
336 return;
337 // Since we might be going from 0 to 1 window, we have to arrange the new
338 // window to a good default.
339 AutoPlaceSingleWindow(added_window, false);
340 return;
341 }
342
343 gfx::Rect other_bounds = other_shown_window->GetBounds();
344 gfx::Rect work_area = GetDisplayWorkAreaBoundsInParent(added_window);
345 bool move_other_right =
346 other_bounds.CenterPoint().x() > work_area.x() + work_area.width() / 2;
347
348 // Push the other window to the size only if there are two windows left.
349 if (single_window) {
350 // When going from one to two windows both windows loose their
351 // "positioned by user" flags.
352 added_window_state->set_bounds_changed_by_user(false);
353 wm::WindowState* other_window_state = other_shown_window->GetWindowState();
354 other_window_state->set_bounds_changed_by_user(false);
355
356 if (WindowPositionCanBeManaged(other_shown_window)) {
357 // Don't override pre auto managed bounds as the current bounds
358 // may not be original.
359 if (!other_window_state->pre_auto_manage_window_bounds())
360 other_window_state->SetPreAutoManageWindowBounds(other_bounds);
361
362 // Push away the other window after remembering its current position.
363 if (MoveRectToOneSide(work_area, move_other_right, &other_bounds))
364 SetBoundsAnimated(other_shown_window, other_bounds, work_area);
365 }
366 }
367
368 // Remember the current location of the window if it's new and push
369 // it also to the opposite location if needed. Since it is just
370 // being shown, we do not need to animate it.
371 gfx::Rect added_bounds = added_window->GetBounds();
372 if (!added_window_state->pre_auto_manage_window_bounds())
373 added_window_state->SetPreAutoManageWindowBounds(added_bounds);
374 if (MoveRectToOneSide(work_area, !move_other_right, &added_bounds))
375 added_window->SetBounds(added_bounds);
376 }
377
378 WindowPositioner::WindowPositioner(wm::WmGlobals* globals)
379 : globals_(globals),
380 pop_position_offset_increment_x(0),
381 pop_position_offset_increment_y(0),
382 popup_position_offset_from_screen_corner_x(0),
383 popup_position_offset_from_screen_corner_y(0),
384 last_popup_position_x_(0),
385 last_popup_position_y_(0) {}
386
387 WindowPositioner::~WindowPositioner() {}
388
389 gfx::Rect WindowPositioner::GetDefaultWindowBounds(
390 const display::Display& display) {
391 const gfx::Rect work_area = display.work_area();
392 // There should be a 'desktop' border around the window at the left and right
393 // side.
394 int default_width = work_area.width() - 2 * kDesktopBorderSize;
395 // There should also be a 'desktop' border around the window at the top.
396 // Since the workspace excludes the tray area we only need one border size.
397 int default_height = work_area.height() - kDesktopBorderSize;
398 int offset_x = kDesktopBorderSize;
399 if (default_width > kMaximumWindowWidth) {
400 // The window should get centered on the screen and not follow the grid.
401 offset_x = (work_area.width() - kMaximumWindowWidth) / 2;
402 default_width = kMaximumWindowWidth;
403 }
404 return gfx::Rect(work_area.x() + offset_x, work_area.y() + kDesktopBorderSize,
405 default_width, default_height);
406 }
407
408 gfx::Rect WindowPositioner::GetPopupPosition(const gfx::Rect& old_pos) {
409 int grid = kMinimumWindowOffset;
410 popup_position_offset_from_screen_corner_x = grid;
411 popup_position_offset_from_screen_corner_y = grid;
412 if (!pop_position_offset_increment_x) {
413 // When the popup position increment is 0, the last popup position
414 // was not yet initialized.
415 last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
416 last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
417 }
418 pop_position_offset_increment_x = grid;
419 pop_position_offset_increment_y = grid;
420 // We handle the Multi monitor support by retrieving the active window's
421 // work area.
422 wm::WmWindow* window = globals_->GetActiveWindow();
423 const gfx::Rect work_area =
424 window && window->IsVisible()
425 ? window->GetDisplayNearestWindow().work_area()
426 : display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
427 // Only try to reposition the popup when it is not spanning the entire
428 // screen.
429 if ((old_pos.width() + popup_position_offset_from_screen_corner_x >=
430 work_area.width()) ||
431 (old_pos.height() + popup_position_offset_from_screen_corner_y >=
432 work_area.height()))
433 return AlignPopupPosition(old_pos, work_area, grid);
434 const gfx::Rect result = SmartPopupPosition(old_pos, work_area, grid);
435 if (!result.IsEmpty())
436 return AlignPopupPosition(result, work_area, grid);
437 return NormalPopupPosition(old_pos, work_area);
438 }
439
440 // static
441 void WindowPositioner::SetMaximizeFirstWindow(bool maximize) {
442 maximize_first_window = maximize;
443 }
444
445 gfx::Rect WindowPositioner::NormalPopupPosition(const gfx::Rect& old_pos,
446 const gfx::Rect& work_area) {
447 int w = old_pos.width();
448 int h = old_pos.height();
449 // Note: The 'last_popup_position' is checked and kept relative to the
450 // screen size. The offsetting will be done in the last step when the
451 // target rectangle gets returned.
452 bool reset = false;
453 if (last_popup_position_y_ + h > work_area.height() ||
454 last_popup_position_x_ + w > work_area.width()) {
455 // Popup does not fit on screen. Reset to next diagonal row.
456 last_popup_position_x_ -= last_popup_position_y_ -
457 popup_position_offset_from_screen_corner_x -
458 pop_position_offset_increment_x;
459 last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
460 reset = true;
461 }
462 if (last_popup_position_x_ + w > work_area.width()) {
463 // Start again over.
464 last_popup_position_x_ = popup_position_offset_from_screen_corner_x;
465 last_popup_position_y_ = popup_position_offset_from_screen_corner_y;
466 reset = true;
467 }
468 int x = last_popup_position_x_;
469 int y = last_popup_position_y_;
470 if (!reset) {
471 last_popup_position_x_ += pop_position_offset_increment_x;
472 last_popup_position_y_ += pop_position_offset_increment_y;
473 }
474 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
475 }
476
477 gfx::Rect WindowPositioner::SmartPopupPosition(const gfx::Rect& old_pos,
478 const gfx::Rect& work_area,
479 int grid) {
480 const std::vector<wm::WmWindow*> windows =
481 globals_->GetMruWindowListIgnoreModals();
482
483 std::vector<const gfx::Rect*> regions;
484 // Process the window list and check if we can bail immediately.
485 for (size_t i = 0; i < windows.size(); i++) {
486 // We only include opaque and visible windows.
487 if (windows[i] && windows[i]->IsVisible() && windows[i]->GetLayer() &&
488 (windows[i]->GetLayer()->fills_bounds_opaquely() ||
489 windows[i]->GetLayer()->GetTargetOpacity() == 1.0)) {
490 wm::WindowState* window_state = windows[i]->GetWindowState();
491 // When any window is maximized we cannot find any free space.
492 if (window_state->IsMaximizedOrFullscreen())
493 return gfx::Rect(0, 0, 0, 0);
494 if (window_state->IsNormalOrSnapped())
495 regions.push_back(&windows[i]->GetBounds());
496 }
497 }
498
499 if (regions.empty())
500 return gfx::Rect(0, 0, 0, 0);
501
502 int w = old_pos.width();
503 int h = old_pos.height();
504 int x_end = work_area.width() / 2;
505 int x, x_increment;
506 // We parse for a proper location on the screen. We do this in two runs:
507 // The first run will start from the left, parsing down, skipping any
508 // overlapping windows it will encounter until the popup's height can not
509 // be served anymore. Then the next grid position to the right will be
510 // taken, and the same cycle starts again. This will be repeated until we
511 // hit the middle of the screen (or we find a suitable location).
512 // In the second run we parse beginning from the right corner downwards and
513 // then to the left.
514 // When no location was found, an empty rectangle will be returned.
515 for (int run = 0; run < 2; run++) {
516 if (run == 0) { // First run: Start left, parse right till mid screen.
517 x = 0;
518 x_increment = pop_position_offset_increment_x;
519 } else { // Second run: Start right, parse left till mid screen.
520 x = work_area.width() - w;
521 x_increment = -pop_position_offset_increment_x;
522 }
523 // Note: The passing (x,y,w,h) window is always relative to the work area's
524 // origin.
525 for (; x_increment > 0 ? (x < x_end) : (x > x_end); x += x_increment) {
526 int y = 0;
527 while (y + h <= work_area.height()) {
528 size_t i;
529 for (i = 0; i < regions.size(); i++) {
530 if (regions[i]->Intersects(
531 gfx::Rect(x + work_area.x(), y + work_area.y(), w, h))) {
532 y = regions[i]->bottom() - work_area.y();
533 break;
534 }
535 }
536 if (i >= regions.size())
537 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h);
538 }
539 }
540 }
541 return gfx::Rect(0, 0, 0, 0);
542 }
543
544 gfx::Rect WindowPositioner::AlignPopupPosition(const gfx::Rect& pos,
545 const gfx::Rect& work_area,
546 int grid) {
547 if (grid <= 1)
548 return pos;
549
550 int x = pos.x() - (pos.x() - work_area.x()) % grid;
551 int y = pos.y() - (pos.y() - work_area.y()) % grid;
552 int w = pos.width();
553 int h = pos.height();
554
555 // If the alignment was pushing the window out of the screen, we ignore the
556 // alignment for that call.
557 if (abs(pos.right() - work_area.right()) < grid)
558 x = work_area.right() - w;
559 if (abs(pos.bottom() - work_area.bottom()) < grid)
560 y = work_area.bottom() - h;
561 return gfx::Rect(x, y, w, h);
562 }
563
564 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/common/window_positioner.h ('k') | ash/wm/common/window_positioning_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698