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

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

Issue 2736573002: chromeos: Move files in //ash/common to //ash, part 2 (Closed)
Patch Set: Created 3 years, 9 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/common/wm/window_positioner.h ('k') | ash/common/wm/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/common/wm/window_positioner.h"
6
7 #include "ash/common/wm/mru_window_tracker.h"
8 #include "ash/common/wm/window_positioning_utils.h"
9 #include "ash/common/wm/window_state.h"
10 #include "ash/common/wm/wm_screen_util.h"
11 #include "ash/common/wm_shell.h"
12 #include "ash/common/wm_window.h"
13 #include "ui/compositor/layer.h"
14 #include "ui/display/display.h"
15 #include "ui/display/screen.h"
16 #include "ui/gfx/geometry/insets.h"
17
18 namespace ash {
19
20 const int WindowPositioner::kMinimumWindowOffset = 32;
21
22 // The number of pixels which are kept free top, left and right when a window
23 // gets positioned to its default location.
24 // static
25 const int WindowPositioner::kDesktopBorderSize = 16;
26
27 // Maximum width of a window even if there is more room on the desktop.
28 // static
29 const int WindowPositioner::kMaximumWindowWidth = 1100;
30
31 namespace {
32
33 // When a window gets opened in default mode and the screen is less than or
34 // equal to this width, the window will get opened in maximized mode. This value
35 // can be reduced to a "tame" number if the feature is disabled.
36 const int kForceMaximizeWidthLimit = 1366;
37
38 // The time in milliseconds which should be used to visually move a window
39 // through an automatic "intelligent" window management option.
40 const int kWindowAutoMoveDurationMS = 125;
41
42 // If set to true all window repositioning actions will be ignored. Set through
43 // WindowPositioner::SetIgnoreActivations().
44 static bool disable_auto_positioning = false;
45
46 // If set to true, by default the first window in ASH will be maximized.
47 static bool maximize_first_window = false;
48
49 // Check if any management should be performed (with a given |window|).
50 bool UseAutoWindowManager(const WmWindow* window) {
51 if (disable_auto_positioning)
52 return false;
53 const wm::WindowState* window_state = window->GetWindowState();
54 return !window_state->is_dragged() && window_state->window_position_managed();
55 }
56
57 // Check if a given |window| can be managed. This includes that its
58 // state is not minimized/maximized/fullscreen/the user has changed
59 // its size by hand already. It furthermore checks for the
60 // WindowIsManaged status.
61 bool WindowPositionCanBeManaged(const WmWindow* window) {
62 if (disable_auto_positioning)
63 return false;
64 const wm::WindowState* window_state = window->GetWindowState();
65 return window_state->window_position_managed() &&
66 !window_state->IsMinimized() && !window_state->IsMaximized() &&
67 !window_state->IsFullscreen() && !window_state->IsPinned() &&
68 !window_state->bounds_changed_by_user();
69 }
70
71 // Move the given |bounds| on the available |work_area| in the direction
72 // indicated by |move_right|. If |move_right| is true, the rectangle gets moved
73 // to the right edge, otherwise to the left one.
74 bool MoveRectToOneSide(const gfx::Rect& work_area,
75 bool move_right,
76 gfx::Rect* bounds) {
77 if (move_right) {
78 if (work_area.right() > bounds->right()) {
79 bounds->set_x(work_area.right() - bounds->width());
80 return true;
81 }
82 } else {
83 if (work_area.x() < bounds->x()) {
84 bounds->set_x(work_area.x());
85 return true;
86 }
87 }
88 return false;
89 }
90
91 // Move a |window| to new |bounds|. Animate if desired by user.
92 // Moves the transient children of the |window| as well by the same |offset| as
93 // the parent |window|.
94 void SetBoundsAndOffsetTransientChildren(WmWindow* window,
95 const gfx::Rect& bounds,
96 const gfx::Rect& work_area,
97 const gfx::Vector2d& offset) {
98 std::vector<WmWindow*> transient_children = window->GetTransientChildren();
99 for (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(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(WmWindow* window, bool animated) {
135 gfx::Rect work_area = wm::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 WmWindow* GetReferenceWindow(const WmWindow* root_window,
155 const WmWindow* exclude,
156 bool* single_window) {
157 if (single_window)
158 *single_window = true;
159 // Get the active window.
160 WmWindow* active = root_window->GetShell()->GetActiveWindow();
161 if (active && active->GetRootWindow() != root_window)
162 active = NULL;
163
164 // Get a list of all windows.
165 const std::vector<WmWindow*> windows = root_window->GetShell()
166 ->mru_window_tracker()
167 ->BuildWindowListIgnoreModal();
168
169 if (windows.empty())
170 return nullptr;
171
172 int index = 0;
173 // Find the index of the current active window.
174 if (active)
175 index = std::find(windows.begin(), windows.end(), active) - windows.begin();
176
177 // Scan the cycle list backwards to see which is the second topmost window
178 // (and so on). Note that we might cycle a few indices twice if there is no
179 // suitable window. However - since the list is fairly small this should be
180 // very fast anyways.
181 WmWindow* found = nullptr;
182 for (int i = index + windows.size(); i >= 0; i--) {
183 WmWindow* window = windows[i % windows.size()];
184 while (window->GetTransientParent())
185 window = window->GetTransientParent();
186 if (window != exclude && window->GetType() == ui::wm::WINDOW_TYPE_NORMAL &&
187 window->GetRootWindow() == root_window &&
188 window->GetTargetVisibility() &&
189 window->GetWindowState()->window_position_managed()) {
190 if (found && found != window) {
191 // no need to check !single_window because the function must have
192 // been already returned in the "if (!single_window)" below.
193 *single_window = false;
194 return found;
195 }
196 found = window;
197 // If there is no need to check single window, return now.
198 if (!single_window)
199 return found;
200 }
201 }
202 return found;
203 }
204
205 } // namespace
206
207 // static
208 int WindowPositioner::GetForceMaximizedWidthLimit() {
209 return kForceMaximizeWidthLimit;
210 }
211
212 // static
213 void WindowPositioner::GetBoundsAndShowStateForNewWindow(
214 const WmWindow* new_window,
215 bool is_saved_bounds,
216 ui::WindowShowState show_state_in,
217 gfx::Rect* bounds_in_out,
218 ui::WindowShowState* show_state_out) {
219 // Always open new window in the target display.
220 WmWindow* target = WmShell::Get()->GetRootWindowForNewWindows();
221
222 WmWindow* top_window = GetReferenceWindow(target, nullptr, nullptr);
223 // Our window should not have any impact if we are already on top.
224 if (top_window == new_window)
225 top_window = nullptr;
226
227 // If there is no valid other window we take and adjust the passed coordinates
228 // and show state.
229 if (!top_window) {
230 gfx::Rect work_area = target->GetDisplayNearestWindow().work_area();
231
232 bounds_in_out->AdjustToFit(work_area);
233 // Use adjusted saved bounds, if there is one.
234 if (is_saved_bounds)
235 return;
236
237 if (show_state_in == ui::SHOW_STATE_DEFAULT) {
238 const bool maximize_first_window_on_first_run =
239 target->GetShell()->IsForceMaximizeOnFirstRun();
240 // We want to always open maximized on "small screens" or when policy
241 // tells us to.
242 const bool set_maximized =
243 maximize_first_window ||
244 ((work_area.width() <= GetForceMaximizedWidthLimit() ||
245 maximize_first_window_on_first_run) &&
246 (!new_window || !new_window->GetWindowState()->IsFullscreen()));
247
248 if (set_maximized)
249 *show_state_out = ui::SHOW_STATE_MAXIMIZED;
250 }
251 return;
252 }
253
254 wm::WindowState* top_window_state = top_window->GetWindowState();
255 bool maximized = top_window_state->IsMaximized();
256 // We ignore the saved show state, but look instead for the top level
257 // window's show state.
258 if (show_state_in == ui::SHOW_STATE_DEFAULT) {
259 *show_state_out =
260 maximized ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_DEFAULT;
261 }
262
263 if (maximized || top_window_state->IsFullscreen()) {
264 bool has_restore_bounds = top_window_state->HasRestoreBounds();
265 if (has_restore_bounds) {
266 // For a maximized/fullscreen window ignore the real bounds of
267 // the top level window and use its restore bounds
268 // instead. Offset the bounds to prevent the windows from
269 // overlapping exactly when restored.
270 *bounds_in_out =
271 top_window_state->GetRestoreBoundsInScreen() +
272 gfx::Vector2d(kMinimumWindowOffset, kMinimumWindowOffset);
273 }
274 if (is_saved_bounds || has_restore_bounds) {
275 gfx::Rect work_area = target->GetDisplayNearestWindow().work_area();
276 bounds_in_out->AdjustToFit(work_area);
277 // Use adjusted saved bounds or restore bounds, if there is one.
278 return;
279 }
280 }
281
282 // Use the size of the other window. The window's bound will be rearranged
283 // in ash::WorkspaceLayoutManager using this location.
284 *bounds_in_out = top_window->GetBoundsInScreen();
285 }
286
287 // static
288 void WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(
289 const WmWindow* removed_window) {
290 if (!UseAutoWindowManager(removed_window))
291 return;
292 // Find a single open browser window.
293 bool single_window;
294 WmWindow* other_shown_window = GetReferenceWindow(
295 removed_window->GetRootWindow(), removed_window, &single_window);
296 if (!other_shown_window || !single_window ||
297 !WindowPositionCanBeManaged(other_shown_window))
298 return;
299 AutoPlaceSingleWindow(other_shown_window, true);
300 }
301
302 // static
303 bool WindowPositioner::DisableAutoPositioning(bool ignore) {
304 bool old_state = disable_auto_positioning;
305 disable_auto_positioning = ignore;
306 return old_state;
307 }
308
309 // static
310 void WindowPositioner::RearrangeVisibleWindowOnShow(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 = wm::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 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 = wm::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(WmShell* shell)
379 : shell_(shell),
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 WmWindow* window = shell_->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<WmWindow*> windows =
481 shell_->mru_window_tracker()->BuildWindowListIgnoreModal();
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->IsMaximizedOrFullscreenOrPinned())
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/common/wm/window_positioner.h ('k') | ash/common/wm/window_positioning_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698