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

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

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

Powered by Google App Engine
This is Rietveld 408576698