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

Side by Side Diff: ash/wm/workspace/auto_window_management.cc

Issue 11085053: Improving window auto management between workspaces (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed Created 8 years, 2 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ash/wm/workspace/auto_window_management.h"
6
7 #include "ash/ash_switches.h"
8 #include "ash/shell.h"
9 #include "ash/wm/window_animations.h"
10 #include "ash/wm/window_util.h"
11 #include "base/command_line.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/client/aura_constants.h"
14 #include "ui/compositor/layer_animator.h"
15 #include "ui/compositor/scoped_layer_animation_settings.h"
16 #include "ui/gfx/screen.h"
17
18 namespace ash {
19 namespace internal {
20
21 namespace {
22
23 // The time in milliseconds which should be used to visually move a window
24 // through an automatic "intelligent" window management option.
25 const int kWindowAutoMoveDurationMS = 125;
26
27 // Check if the given |window| is marked to get managed.
28 bool WindowIsManaged(const aura::Window* window) {
29 return (!window->bounds().IsEmpty() && wm::IsWindowPositionManaged(window));
30 }
31
32 // Check if a given |window| can be managed. This includes that it's state is
33 // not minimized/maximized/the user has changed it's size by hand already.
34 // It furthermore checks for the WindowIsManaged status.
35 bool WindowPositionCanBeManaged(const aura::Window* window) {
36 return (WindowIsManaged(window) &&
37 !wm::IsWindowMinimized(window) &&
38 !wm::IsWindowMaximized(window) &&
39 !wm::HasUserChangedWindowPositionOrSize(window));
40 }
41
42 // Given a |window|, return the |work_area| of the desktop on which it is
43 // located as well as the only |other_window| which has an impact on the
44 // automated window location management. If there is more then one windows,
45 // false is returned, but the |other_window| will be set to the first one
46 // found.
47 // If the return value is true a single window was found.
48 // Note: |work_area| is undefined if false is returned and |other_window| is
49 // NULL.
50 bool GetOtherVisibleAndManageableWindow(const aura::Window* window,
51 gfx::Rect* work_area,
52 aura::Window** other_window) {
53 *other_window = NULL;
54 // If the given window was not manageable, it cannot have caused a management
55 // operation earlier.
56 if (!WindowIsManaged(window))
57 return false;
58
59 // Get our work area.
60 *work_area = gfx::Rect(window->parent()->bounds().size());
61 work_area->Inset(Shell::GetScreen()->GetDisplayMatching(
62 *work_area).GetWorkAreaInsets());
63
64 const aura::Window::Windows& windows = window->parent()->children();
65 // Find a single open managed window.
66 for (size_t i = 0; i < windows.size(); i++) {
67 aura::Window* iterated_window = windows[i];
68 if (window != iterated_window &&
69 iterated_window->type() == aura::client::WINDOW_TYPE_NORMAL &&
70 iterated_window->TargetVisibility() &&
71 WindowIsManaged(iterated_window)) {
72 // Bail if we find a second usable window.
73 if (*other_window)
74 return false;
75 *other_window = iterated_window;
76 }
77 }
78 return *other_window != NULL;
79 }
80
81 // Move the given |bounds| on the available |parent_width| to the
82 // direction. If |move_right| is true, the rectangle gets moved to the right
83 // corner, otherwise to the left one.
84 bool MoveRectToOneSide(int parent_width, bool move_right, gfx::Rect* bounds) {
85 if (move_right) {
86 if (parent_width > bounds->right()) {
87 bounds->set_x(parent_width - bounds->width());
88 return true;
89 }
90 } else {
91 if (0 < bounds->x()) {
92 bounds->set_x(0);
93 return true;
94 }
95 }
96 return false;
97 }
98
99 } // namespace
100
101 void RearrangeVisibleWindowOnHide(const aura::Window* removed_window) {
102 // Find a single open browser window.
103 aura::Window* shown_window = NULL;
104 gfx::Rect work_area;
105 if (!GetOtherVisibleAndManageableWindow(removed_window,
106 &work_area,
107 &shown_window))
108 return;
109
110 if (WindowPositionCanBeManaged(shown_window)) {
111 // Center the window (only in x).
112 gfx::Rect bounds = shown_window->bounds();
113 bounds.set_x((work_area.width() - bounds.width()) / 2);
114 if (bounds != shown_window->bounds()) {
115 if (!shown_window->GetProperty(aura::client::kAnimationsDisabledKey) &&
116 !CommandLine::ForCurrentProcess()->HasSwitch(
117 ash::switches::kAshWindowAnimationsDisabled)) {
118 ui::LayerAnimator* animator = shown_window->layer()->GetAnimator();
119 ui::ScopedLayerAnimationSettings animation_setter(animator);
120 animation_setter.SetTransitionDuration(
121 base::TimeDelta::FromMilliseconds(kWindowAutoMoveDurationMS));
122 }
123 shown_window->SetBounds(bounds);
124 }
125 }
126 }
127
128 void RearrangeVisibleWindowOnShow(aura::Window* added_window) {
129 // Find a single open managed window.
sky 2012/10/17 15:59:38 This code would be a lot easier to reason about if
Mr4D (OOO till 08-26) 2012/10/17 18:43:56 Done.
130 aura::Window* shown_window = NULL;
131 gfx::Rect work_area;
132 if (!GetOtherVisibleAndManageableWindow(added_window,
133 &work_area,
134 &shown_window)) {
sky 2012/10/17 15:59:38 shown_window is particularly confusing name to use
Mr4D (OOO till 08-26) 2012/10/17 18:43:56 Done.
135 // It could be that this window is the first window joining the workspace.
136 if (!WindowPositionCanBeManaged(added_window) || shown_window)
137 return;
138 // If so we have to make sure it is centered.
139 gfx::Rect bounds = added_window->bounds();
140 bounds.set_x((work_area.width() - bounds.width()) / 2);
141 added_window->SetBounds(bounds);
142 return;
143 }
144
145 if (WindowPositionCanBeManaged(shown_window)) {
146 // Push away the other window.
147 gfx::Rect other_bounds = shown_window->bounds();
148 bool move_right = other_bounds.CenterPoint().x() < work_area.width() / 2;
149 if (MoveRectToOneSide(work_area.width(), move_right, &other_bounds)) {
150 if (other_bounds != shown_window->bounds()) {
151 if (!shown_window->GetProperty(aura::client::kAnimationsDisabledKey) &&
152 !CommandLine::ForCurrentProcess()->HasSwitch(
153 ash::switches::kAshWindowAnimationsDisabled)) {
154 ui::LayerAnimator* animator = shown_window->layer()->GetAnimator();
sky 2012/10/17 15:59:38 This is why nothing is animating. As soon as Scope
Mr4D (OOO till 08-26) 2012/10/17 18:43:56 Aha! So that was the problem... Well - as stated e
155 ui::ScopedLayerAnimationSettings animation_setter(animator);
156 animation_setter.SetTransitionDuration(
157 base::TimeDelta::FromMilliseconds(kWindowAutoMoveDurationMS));
158 }
159 shown_window->SetBounds(other_bounds);
160 }
161 }
162 // Push the new window also to the opposite location (if needed).
163 // Since it is just coming into view, we do not need to animate it.
164 gfx::Rect added_bounds = added_window->bounds();
165 if (MoveRectToOneSide(work_area.width(), !move_right, &added_bounds))
166 added_window->SetBounds(added_bounds);
167 }
168 }
169
170 } // namespace internal
171 } // namespace ash
172
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698