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

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

Issue 9113045: Reworks the workspace code. Here's the new heuristics: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged and all that good stuff. Created 8 years, 11 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
« no previous file with comments | « ash/wm/workspace/workspace_manager.h ('k') | ash/wm/workspace/workspace_manager_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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/wm/workspace/workspace_manager.h" 5 #include "ash/wm/workspace/workspace_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ash/wm/property_util.h"
10 #include "ash/wm/window_util.h"
9 #include "ash/wm/workspace/workspace.h" 11 #include "ash/wm/workspace/workspace.h"
10 #include "ash/wm/workspace/workspace_observer.h"
11 #include "base/auto_reset.h" 12 #include "base/auto_reset.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "ui/aura/client/aura_constants.h"
14 #include "ui/aura/root_window.h" 16 #include "ui/aura/root_window.h"
15 #include "ui/aura/screen_aura.h" 17 #include "ui/aura/screen_aura.h"
16 #include "ui/aura/window.h" 18 #include "ui/aura/window.h"
19 #include "ui/base/ui_base_types.h"
17 #include "ui/gfx/compositor/layer.h" 20 #include "ui/gfx/compositor/layer.h"
18 #include "ui/gfx/compositor/layer_animator.h" 21 #include "ui/gfx/compositor/layer_animator.h"
19 #include "ui/gfx/compositor/scoped_layer_animation_settings.h" 22 #include "ui/gfx/compositor/scoped_layer_animation_settings.h"
20 #include "ui/gfx/screen.h" 23 #include "ui/gfx/screen.h"
21 #include "ui/gfx/transform.h" 24 #include "ui/gfx/transform.h"
22 25
23 namespace { 26 namespace {
24 27
25 // The horizontal margein between workspaces in pixels. 28 // The horizontal margein between workspaces in pixels.
26 const int kWorkspaceHorizontalMargin = 50; 29 const int kWorkspaceHorizontalMargin = 50;
27 30
28 // Minimum/maximum scale for overview mode. 31 // Minimum/maximum scale for overview mode.
29 const float kMaxOverviewScale = 0.9f; 32 const float kMaxOverviewScale = 0.9f;
30 const float kMinOverviewScale = 0.3f; 33 const float kMinOverviewScale = 0.3f;
31 34
35 // Sets the visibility of the layer of each window in |windows| to |value|. We
36 // set the visibility of the layer rather than the Window so that views doesn't
37 // see the visibility change.
38 // TODO: revisit this. Ideally the Window would think it's still visibile after
39 // this. May be possible after Ben lands his changes.
40 void SetWindowLayerVisibility(const std::vector<aura::Window*>& windows,
41 bool value) {
42 for (size_t i = 0; i < windows.size(); ++i){
43 if (windows[i]->layer())
44 windows[i]->layer()->SetVisible(value);
45 SetWindowLayerVisibility(windows[i]->transient_children(), value);
46 }
47 }
48
32 } 49 }
33 50
34 namespace ash { 51 namespace ash {
35 namespace internal { 52 namespace internal {
36 53
37 //////////////////////////////////////////////////////////////////////////////// 54 ////////////////////////////////////////////////////////////////////////////////
38 // WindowManager, public: 55 // WindowManager, public:
39 56
40 WorkspaceManager::WorkspaceManager(aura::Window* contents_view) 57 WorkspaceManager::WorkspaceManager(aura::Window* contents_view)
41 : contents_view_(contents_view), 58 : contents_view_(contents_view),
42 active_workspace_(NULL), 59 active_workspace_(NULL),
43 workspace_size_( 60 workspace_size_(
44 gfx::Screen::GetMonitorAreaNearestWindow(contents_view_).size()), 61 gfx::Screen::GetMonitorAreaNearestWindow(contents_view_).size()),
45 is_overview_(false), 62 is_overview_(false),
46 layout_in_progress_(false),
47 ignored_window_(NULL) { 63 ignored_window_(NULL) {
48 DCHECK(contents_view); 64 DCHECK(contents_view);
49 } 65 }
50 66
51 WorkspaceManager::~WorkspaceManager() { 67 WorkspaceManager::~WorkspaceManager() {
68 for (size_t i = 0; i < workspaces_.size(); ++i) {
69 Workspace* workspace = workspaces_[i];
70 for (size_t j = 0; j < workspace->windows().size(); ++j)
71 workspace->windows()[j]->RemoveObserver(this);
72 }
52 std::vector<Workspace*> copy_to_delete(workspaces_); 73 std::vector<Workspace*> copy_to_delete(workspaces_);
53 STLDeleteElements(&copy_to_delete); 74 STLDeleteElements(&copy_to_delete);
54 } 75 }
55 76
56 Workspace* WorkspaceManager::CreateWorkspace() { 77 bool WorkspaceManager::IsManagedWindow(aura::Window* window) const {
57 Workspace* workspace = new Workspace(this); 78 return window->type() == aura::client::WINDOW_TYPE_NORMAL &&
58 LayoutWorkspaces(); 79 !window->transient_parent();
59 return workspace;
60 } 80 }
61 81
62 Workspace* WorkspaceManager::GetActiveWorkspace() const { 82 void WorkspaceManager::AddWindow(aura::Window* window) {
63 return active_workspace_; 83 DCHECK(IsManagedWindow(window));
84
85 if (FindBy(window))
86 return; // Already know about this window.
87
88 window->AddObserver(this);
89
90 if (!window->GetProperty(aura::client::kShowStateKey)) {
91 // TODO: set maximized if width < x.
92 window->SetIntProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
93 }
94
95 // TODO: handle fullscreen.
96 if (window_util::IsWindowMaximized(window)) {
97 if (!GetRestoreBounds(window))
98 SetRestoreBounds(window, window->GetTargetBounds());
99 else
100 SetWindowBounds(window, GetWorkAreaBounds());
101 }
102
103 Workspace* workspace = NULL;
104 Workspace::Type type_for_window = Workspace::TypeForWindow(window);
105 switch (type_for_window) {
106 case Workspace::TYPE_SPLIT:
107 // Splits either go in current workspace (if maximized or split). If the
108 // current workspace isn't split/maximized, then create a maximized
109 // workspace.
110 workspace = GetActiveWorkspace();
111 if (workspace &&
112 (workspace->type() == Workspace::TYPE_SPLIT ||
113 workspace->type() == Workspace::TYPE_MAXIMIZED)) {
114 // TODO: this needs to reset bounds of any existing windows in
115 // workspace.
116 workspace->SetType(Workspace::TYPE_SPLIT);
117 } else {
118 type_for_window = Workspace::TYPE_MAXIMIZED;
119 workspace = NULL;
120 }
121 break;
122
123 case Workspace::TYPE_NORMAL:
124 // All normal windows go in the same workspace.
125 workspace = GetNormalWorkspace();
126 break;
127
128 case Workspace::TYPE_MAXIMIZED:
129 // All maximized windows go in their own workspace.
130 break;
131
132 default:
133 NOTREACHED();
134 break;
135 }
136
137 if (!workspace) {
138 workspace = new Workspace(this);
139 workspace->SetType(type_for_window);
140 }
141 workspace->AddWindowAfter(window, NULL);
142 workspace->Activate();
64 } 143 }
65 144
66 Workspace* WorkspaceManager::FindBy(aura::Window* window) const { 145 void WorkspaceManager::RemoveWindow(aura::Window* window) {
67 int index = GetWorkspaceIndexContaining(window); 146 Workspace* workspace = FindBy(window);
68 return index < 0 ? NULL : workspaces_[index]; 147 if (!workspace)
148 return;
149 window->RemoveObserver(this);
150 workspace->RemoveWindow(window);
151 if (workspace->is_empty())
152 delete workspace;
69 } 153 }
70 154
71 aura::Window* WorkspaceManager::FindRotateWindowForLocation( 155 void WorkspaceManager::SetActiveWorkspaceByWindow(aura::Window* window) {
72 const gfx::Point& point) { 156 Workspace* workspace = FindBy(window);
73 for (Workspaces::const_iterator i = workspaces_.begin(); 157 if (workspace)
74 i != workspaces_.end(); 158 workspace->Activate();
75 ++i) {
76 aura::Window* window = (*i)->FindRotateWindowForLocation(point);
77 if (window)
78 return window;
79 }
80 return NULL;
81 }
82
83 void WorkspaceManager::LayoutWorkspaces() {
84 UpdateContentsView();
85
86 gfx::Rect bounds(workspace_size_);
87 int x = 0;
88 for (Workspaces::const_iterator i = workspaces_.begin();
89 i != workspaces_.end();
90 ++i) {
91 Workspace* workspace = *i;
92 bounds.set_x(x);
93 workspace->SetBounds(bounds);
94 x += bounds.width() + kWorkspaceHorizontalMargin;
95 }
96 } 159 }
97 160
98 gfx::Rect WorkspaceManager::GetDragAreaBounds() { 161 gfx::Rect WorkspaceManager::GetDragAreaBounds() {
99 return GetWorkAreaBounds(gfx::Rect(contents_view_->bounds().size())); 162 return GetWorkAreaBounds();
100 } 163 }
101 164
102 void WorkspaceManager::SetOverview(bool overview) { 165 void WorkspaceManager::SetOverview(bool overview) {
103 if (is_overview_ == overview) 166 if (is_overview_ == overview)
104 return; 167 return;
105 is_overview_ = overview; 168 NOTIMPLEMENTED();
106
107 ui::Transform transform;
108 if (is_overview_) {
109 // TODO(oshima|sky): We limit the how small windows can be shrinked
110 // in overview mode, thus part of the contents_view may not be visible.
111 // We need to add capability to scroll/move contents_view in overview mode.
112 float scale = std::min(
113 kMaxOverviewScale,
114 workspace_size_.width() /
115 static_cast<float>(contents_view_->bounds().width()));
116 scale = std::max(kMinOverviewScale, scale);
117
118 transform.SetScale(scale, scale);
119
120 int overview_width = contents_view_->bounds().width() * scale;
121 int dx = 0;
122 if (overview_width < workspace_size_.width()) {
123 dx = (workspace_size_.width() - overview_width) / 2;
124 } else if (active_workspace_) {
125 // Center the active workspace.
126 int active_workspace_mid_x = (active_workspace_->bounds().x() +
127 active_workspace_->bounds().width() / 2) * scale;
128 dx = workspace_size_.width() / 2 - active_workspace_mid_x;
129 dx = std::min(0, std::max(dx, workspace_size_.width() - overview_width));
130 }
131
132 transform.SetTranslateX(dx);
133 transform.SetTranslateY(workspace_size_.height() * (1.0f - scale) / 2);
134 } else if (active_workspace_) {
135 transform.SetTranslateX(-active_workspace_->bounds().x());
136 }
137
138 ui::ScopedLayerAnimationSettings settings(
139 contents_view_->layer()->GetAnimator());
140 contents_view_->layer()->SetTransform(transform);
141 }
142
143 void WorkspaceManager::RotateWindows(aura::Window* source,
144 aura::Window* target) {
145 DCHECK(source);
146 DCHECK(target);
147 int source_ws_index = GetWorkspaceIndexContaining(source);
148 int target_ws_index = GetWorkspaceIndexContaining(target);
149 DCHECK(source_ws_index >= 0);
150 DCHECK(target_ws_index >= 0);
151 if (source_ws_index == target_ws_index) {
152 workspaces_[source_ws_index]->RotateWindows(source, target);
153 } else {
154 aura::Window* insert = source;
155 aura::Window* target_to_insert = target;
156 if (source_ws_index < target_ws_index) {
157 for (int i = target_ws_index; i >= source_ws_index; --i) {
158 insert = workspaces_[i]->ShiftWindows(
159 insert, source, target_to_insert, Workspace::SHIFT_TO_LEFT);
160 // |target| can only be in the 1st workspace.
161 target_to_insert = NULL;
162 }
163 } else {
164 for (int i = target_ws_index; i <= source_ws_index; ++i) {
165 insert = workspaces_[i]->ShiftWindows(
166 insert, source, target_to_insert, Workspace::SHIFT_TO_RIGHT);
167 // |target| can only be in the 1st workspace.
168 target_to_insert = NULL;
169 }
170 }
171 }
172 FOR_EACH_OBSERVER(WorkspaceObserver, observers_,
173 WindowMoved(this, source, target));
174 workspaces_[target_ws_index]->Activate();
175 } 169 }
176 170
177 void WorkspaceManager::SetWorkspaceSize(const gfx::Size& workspace_size) { 171 void WorkspaceManager::SetWorkspaceSize(const gfx::Size& workspace_size) {
178 if (workspace_size == workspace_size_) 172 if (workspace_size == workspace_size_)
179 return; 173 return;
180 workspace_size_ = workspace_size; 174 workspace_size_ = workspace_size;
181 LayoutWorkspaces(); 175 for (Workspaces::const_iterator i = workspaces_.begin();
176 i != workspaces_.end(); ++i) {
177 (*i)->WorkspaceSizeChanged();
178 }
182 } 179 }
183 180
184 void WorkspaceManager::AddObserver(WorkspaceObserver* observer) { 181 void WorkspaceManager::OnWindowPropertyChanged(aura::Window* window,
185 observers_.AddObserver(observer); 182 const char* name,
186 } 183 void* old) {
184 if (!IsManagedWindow(window))
185 return;
187 186
188 void WorkspaceManager::RemoveObserver(WorkspaceObserver* observer) { 187 if (name != aura::client::kShowStateKey)
189 observers_.RemoveObserver(observer); 188 return;
189 // TODO: handle fullscreen.
190 bool is_maximized = window->GetIntProperty(name) == ui::SHOW_STATE_MAXIMIZED;
191 bool was_maximized =
192 (old == reinterpret_cast<void*>(ui::SHOW_STATE_MAXIMIZED));
193 if (is_maximized == was_maximized)
194 return;
195
196 MaximizedStateChanged(window);
190 } 197 }
191 198
192 //////////////////////////////////////////////////////////////////////////////// 199 ////////////////////////////////////////////////////////////////////////////////
193 // WorkspaceManager, private: 200 // WorkspaceManager, private:
194 201
195 void WorkspaceManager::AddWorkspace(Workspace* workspace) { 202 void WorkspaceManager::AddWorkspace(Workspace* workspace) {
196 Workspaces::iterator i = std::find(workspaces_.begin(), 203 DCHECK(std::find(workspaces_.begin(), workspaces_.end(),
197 workspaces_.end(), 204 workspace) == workspaces_.end());
198 workspace); 205 if (active_workspace_) {
199 DCHECK(i == workspaces_.end()); 206 // New workspaces go right after current workspace.
200 workspaces_.push_back(workspace); 207 Workspaces::iterator i = std::find(workspaces_.begin(), workspaces_.end(),
208 active_workspace_);
209 workspaces_.insert(++i, workspace);
210 } else {
211 workspaces_.push_back(workspace);
212 }
201 } 213 }
202 214
203 void WorkspaceManager::RemoveWorkspace(Workspace* workspace) { 215 void WorkspaceManager::RemoveWorkspace(Workspace* workspace) {
204 Workspaces::iterator i = std::find(workspaces_.begin(), 216 Workspaces::iterator i = std::find(workspaces_.begin(),
205 workspaces_.end(), 217 workspaces_.end(),
206 workspace); 218 workspace);
207 DCHECK(i != workspaces_.end()); 219 DCHECK(i != workspaces_.end());
208 Workspace* old = NULL; 220 i = workspaces_.erase(i);
209 221 if (active_workspace_ == workspace) {
210 if (workspace == active_workspace_) { 222 // TODO: need mru order.
211 old = active_workspace_; 223 if (i != workspaces_.end())
212 active_workspace_ = NULL; 224 SetActiveWorkspace(*i);
213 } 225 else if (!workspaces_.empty())
214 workspaces_.erase(i); 226 SetActiveWorkspace(workspaces_.back());
215 LayoutWorkspaces(); 227 else
216 228 active_workspace_ = NULL;
217 if (old) {
218 FOR_EACH_OBSERVER(WorkspaceObserver, observers_,
219 ActiveWorkspaceChanged(this, old));
220 } 229 }
221 } 230 }
222 231
232 Workspace* WorkspaceManager::GetActiveWorkspace() const {
233 return active_workspace_;
234 }
235
236 Workspace* WorkspaceManager::FindBy(aura::Window* window) const {
237 int index = GetWorkspaceIndexContaining(window);
238 return index < 0 ? NULL : workspaces_[index];
239 }
240
223 void WorkspaceManager::SetActiveWorkspace(Workspace* workspace) { 241 void WorkspaceManager::SetActiveWorkspace(Workspace* workspace) {
224 if (active_workspace_ == workspace) 242 if (active_workspace_ == workspace)
225 return; 243 return;
226 DCHECK(std::find(workspaces_.begin(), workspaces_.end(), 244 DCHECK(std::find(workspaces_.begin(), workspaces_.end(),
227 workspace) != workspaces_.end()); 245 workspace) != workspaces_.end());
228 Workspace* old = active_workspace_; 246 if (active_workspace_)
247 SetWindowLayerVisibility(active_workspace_->windows(), false);
229 active_workspace_ = workspace; 248 active_workspace_ = workspace;
249 if (active_workspace_)
250 SetWindowLayerVisibility(active_workspace_->windows(), true);
230 251
231 is_overview_ = false; 252 is_overview_ = false;
232 UpdateContentsView();
233
234 FOR_EACH_OBSERVER(WorkspaceObserver, observers_,
235 ActiveWorkspaceChanged(this, old));
236 } 253 }
237 254
238 gfx::Rect WorkspaceManager::GetWorkAreaBounds( 255 gfx::Rect WorkspaceManager::GetWorkAreaBounds() {
239 const gfx::Rect& workspace_bounds) { 256 gfx::Rect bounds(workspace_size_);
240 gfx::Rect bounds = workspace_bounds;
241 bounds.Inset( 257 bounds.Inset(
242 aura::RootWindow::GetInstance()->screen()->work_area_insets()); 258 aura::RootWindow::GetInstance()->screen()->work_area_insets());
243 return bounds; 259 return bounds;
244 } 260 }
245 261
246 // Returns the index of the workspace that contains the |window|. 262 // Returns the index of the workspace that contains the |window|.
247 int WorkspaceManager::GetWorkspaceIndexContaining(aura::Window* window) const { 263 int WorkspaceManager::GetWorkspaceIndexContaining(aura::Window* window) const {
248 for (Workspaces::const_iterator i = workspaces_.begin(); 264 for (Workspaces::const_iterator i = workspaces_.begin();
249 i != workspaces_.end(); 265 i != workspaces_.end();
250 ++i) { 266 ++i) {
251 if ((*i)->Contains(window)) 267 if ((*i)->Contains(window))
252 return i - workspaces_.begin(); 268 return i - workspaces_.begin();
253 } 269 }
254 return -1; 270 return -1;
255 } 271 }
256 272
257 void WorkspaceManager::UpdateContentsView() { 273 void WorkspaceManager::SetWindowBounds(aura::Window* window,
258 int num_workspaces = std::max(1, static_cast<int>(workspaces_.size())); 274 const gfx::Rect& bounds) {
259 int total_width = workspace_size_.width() * num_workspaces + 275 // TODO: I suspect it's possible for this to be invoked when ignored_window_
260 kWorkspaceHorizontalMargin * (num_workspaces - 1); 276 // is non-NULL.
261 gfx::Rect bounds(0, 0, total_width, workspace_size_.height()); 277 ignored_window_ = window;
278 window->SetBounds(bounds);
279 ignored_window_ = NULL;
280 }
262 281
263 if (contents_view_->GetTargetBounds() != bounds) 282 void WorkspaceManager::SetWindowBoundsFromRestoreBounds(aura::Window* window) {
264 contents_view_->SetBounds(bounds); 283 Workspace* workspace = FindBy(window);
284 DCHECK(workspace);
285 const gfx::Rect* restore = GetRestoreBounds(window);
286 if (restore) {
287 SetWindowBounds(window,
288 restore->AdjustToFit(workspace->GetWorkAreaBounds()));
289 } else {
290 SetWindowBounds(window, window->bounds().AdjustToFit(
291 workspace->GetWorkAreaBounds()));
292 }
293 ash::ClearRestoreBounds(window);
294 }
265 295
266 // Move to active workspace. 296 void WorkspaceManager::MaximizedStateChanged(aura::Window* window) {
267 if (active_workspace_) { 297 DCHECK(IsManagedWindow(window));
268 ui::Transform transform; 298 bool is_maximized = window_util::IsWindowMaximized(window);
269 transform.SetTranslateX(-active_workspace_->bounds().x()); 299 Workspace* current_workspace = FindBy(window);
270 ui::ScopedLayerAnimationSettings settings( 300 DCHECK(current_workspace);
271 contents_view_->layer()->GetAnimator()); 301 if (is_maximized) {
272 contents_view_->SetTransform(transform); 302 // Unmaximized -> maximized; create a new workspace (unless current only has
303 // one window).
304 SetRestoreBounds(window, window->GetTargetBounds());
305 if (current_workspace->num_windows() != 1) {
306 current_workspace->RemoveWindow(window);
307 Workspace* workspace = new Workspace(this);
308 workspace->SetType(Workspace::TYPE_MAXIMIZED);
309 workspace->AddWindowAfter(window, NULL);
310 current_workspace = workspace;
311 } else {
312 current_workspace->SetType(Workspace::TYPE_MAXIMIZED);
313 }
314 SetWindowBounds(window, GetWorkAreaBounds());
315 } else {
316 // Maximized -> unmaximized; move window to unmaximized workspace (or reuse
317 // current if there isn't one).
318 window_util::SetOpenWindowSplit(window, false);
319 Workspace* workspace = GetNormalWorkspace();
320 if (workspace) {
321 current_workspace->RemoveWindow(window);
322 DCHECK(current_workspace->is_empty());
323 workspace->AddWindowAfter(window, NULL);
324 delete current_workspace;
325 current_workspace = workspace;
326 } else {
327 current_workspace->SetType(Workspace::TYPE_NORMAL);
328 }
329
330 SetWindowBoundsFromRestoreBounds(window);
273 } 331 }
332
333 SetActiveWorkspace(current_workspace);
334 }
335
336 Workspace* WorkspaceManager::GetNormalWorkspace() {
337 for (size_t i = 0; i < workspaces_.size(); ++i) {
338 if (workspaces_[i]->type() == Workspace::TYPE_NORMAL)
339 return workspaces_[i];
340 }
341 return NULL;
274 } 342 }
275 343
276 } // namespace internal 344 } // namespace internal
277 } // namespace ash 345 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/workspace/workspace_manager.h ('k') | ash/wm/workspace/workspace_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698