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

Side by Side Diff: ui/aura_shell/workspace/workspace_manager.cc

Issue 8381015: Add workspace to desktop (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: temporarily exlucde tests failing on win Created 9 years, 1 month 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "ui/aura_shell/workspace/workspace_manager.h" 4 #include "ui/aura_shell/workspace/workspace_manager.h"
5 5
6 #include <algorithm> 6 #include <algorithm>
7 7
8 #include "base/auto_reset.h"
9 #include "ui/aura/desktop.h"
10 #include "ui/aura/screen_aura.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura_shell/workspace/workspace.h"
13 #include "ui/aura_shell/workspace/workspace_manager.h"
14 #include "ui/gfx/screen.h"
15 #include "ui/gfx/compositor/layer.h"
16 #include "ui/gfx/compositor/layer_animator.h"
17 #include "ui/gfx/transform.h"
8 #include "base/logging.h" 18 #include "base/logging.h"
9 #include "base/stl_util.h" 19 #include "base/stl_util.h"
10 #include "ui/aura_shell/workspace/workspace.h"
11 20
12 namespace { 21 namespace {
13 22
14 // The horizontal margein between workspaces in pixels. 23 // The horizontal margein between workspaces in pixels.
15 const int kWorkspaceHorizontalMargin = 50; 24 const int kWorkspaceHorizontalMargin = 50;
25
26 // Minimum/maximum scale for overview mode.
27 const float kMaxOverviewScale = 0.9f;
28 const float kMinOverviewScale = 0.3f;
29
16 } 30 }
17 31
18 namespace aura_shell { 32 namespace aura_shell {
19 33
20 //////////////////////////////////////////////////////////////////////////////// 34 ////////////////////////////////////////////////////////////////////////////////
21 // WindowManager, public: 35 // WindowManager, public:
22 36
23 WorkspaceManager::WorkspaceManager() 37 WorkspaceManager::WorkspaceManager(aura::Window* viewport)
24 : active_workspace_(NULL) { 38 : viewport_(viewport),
39 active_workspace_(NULL),
40 workspace_size_(
41 gfx::Screen::GetMonitorAreaNearestWindow(viewport_).size()),
42 is_overview_(false) {
43 aura::Desktop::GetInstance()->AddObserver(this);
25 } 44 }
26 45
27 WorkspaceManager::~WorkspaceManager() { 46 WorkspaceManager::~WorkspaceManager() {
47 aura::Desktop::GetInstance()->RemoveObserver(this);
28 std::vector<Workspace*> copy_to_delete(workspaces_); 48 std::vector<Workspace*> copy_to_delete(workspaces_);
29 STLDeleteElements(&copy_to_delete); 49 STLDeleteElements(&copy_to_delete);
30 } 50 }
31 51
32 Workspace* WorkspaceManager::CreateWorkspace() { 52 Workspace* WorkspaceManager::CreateWorkspace() {
33 return new Workspace(this); 53 Workspace* workspace = new Workspace(this);
54 LayoutWorkspaces();
55 return workspace;
34 } 56 }
35 57
36 Workspace* WorkspaceManager::GetActiveWorkspace() const { 58 Workspace* WorkspaceManager::GetActiveWorkspace() const {
37 return active_workspace_; 59 return active_workspace_;
38 } 60 }
39 61
40 Workspace* WorkspaceManager::FindBy(aura::Window* window) const { 62 Workspace* WorkspaceManager::FindBy(aura::Window* window) const {
41 for (Workspaces::const_iterator i = workspaces_.begin(); 63 for (Workspaces::const_iterator i = workspaces_.begin();
42 i != workspaces_.end(); 64 i != workspaces_.end();
43 i++) { 65 ++i) {
44 if ((*i)->Contains(window)) 66 if ((*i)->Contains(window))
45 return *i; 67 return *i;
46 } 68 }
47 return NULL; 69 return NULL;
48 } 70 }
49 71
50 void WorkspaceManager::Layout() { 72 void WorkspaceManager::LayoutWorkspaces() {
73 UpdateViewport();
74
51 gfx::Rect bounds(workspace_size_); 75 gfx::Rect bounds(workspace_size_);
52 int x = 0; 76 int x = 0;
53 for (Workspaces::const_iterator i = workspaces_.begin(); 77 for (Workspaces::const_iterator i = workspaces_.begin();
54 i != workspaces_.end(); 78 i != workspaces_.end();
55 i++) { 79 ++i) {
56 Workspace* workspace = *i; 80 Workspace* workspace = *i;
57 bounds.set_x(x); 81 bounds.set_x(x);
58 workspace->SetBounds(bounds); 82 workspace->SetBounds(bounds);
59 x += workspace_size_.width() + kWorkspaceHorizontalMargin; 83 x += bounds.width() + kWorkspaceHorizontalMargin;
60 } 84 }
61 } 85 }
62 86
63 int WorkspaceManager::GetTotalWidth() const { 87 gfx::Rect WorkspaceManager::GetDragAreaBounds() {
64 return !workspaces_.size() ? 0 : 88 return GetWorkAreaBounds(gfx::Rect(viewport_->bounds().size()));
65 workspace_size().width() * workspaces_.size() + 89 }
66 kWorkspaceHorizontalMargin * (workspaces_.size() - 1); 90
91 void WorkspaceManager::SetOverview(bool overview) {
92 if (is_overview_ == overview)
93 return;
94 is_overview_ = overview;
95 gfx::Rect bounds = viewport_->GetTargetBounds();
96
97 ui::Transform transform;
98 if (is_overview_) {
99 // TODO(oshima|sky): We limit the how small windows can be shrinked
100 // in overview mode, thus part of the viewport may not be visible.
101 // We need to add capability to scroll/move viewport in overview mode.
102 float scale = std::min(
103 kMaxOverviewScale,
104 workspace_size_.width() /
105 static_cast<float>(viewport_->bounds().width()));
106 scale = std::max(kMinOverviewScale, scale);
107
108 transform.SetScale(scale, scale);
109
110 int overview_width = viewport_->bounds().width() * scale;
111 int dx = overview_width < workspace_size_.width() ?
112 (workspace_size_.width() - overview_width) / 2 : 0;
113
114 transform.SetTranslateX(-viewport_->GetTargetBounds().x() + dx);
115 transform.SetTranslateY(workspace_size_.height() * (1.0f - scale) / 2);
116 } else {
117 transform.SetTranslateX(-active_workspace_->bounds().x());
118 }
119
120 viewport_->layer()->SetAnimation(aura::Window::CreateDefaultAnimation());
121 viewport_->layer()->SetTransform(transform);
67 } 122 }
68 123
69 //////////////////////////////////////////////////////////////////////////////// 124 ////////////////////////////////////////////////////////////////////////////////
70 // WindowManager, private: 125 // WorkspaceManager, Overridden from aura::DesktopObserver:
126
127 void WorkspaceManager::OnDesktopResized(const gfx::Size& new_size) {
128 workspace_size_ =
129 gfx::Screen::GetMonitorAreaNearestWindow(viewport_).size();
130 LayoutWorkspaces();
131 }
132
133 void WorkspaceManager::OnActiveWindowChanged(aura::Window* active) {
134 Workspace* workspace = FindBy(active);
135 if (workspace)
136 SetActiveWorkspace(workspace);
137 }
138
139 ////////////////////////////////////////////////////////////////////////////////
140 // WorkspaceManager, private:
71 141
72 void WorkspaceManager::AddWorkspace(Workspace* workspace) { 142 void WorkspaceManager::AddWorkspace(Workspace* workspace) {
73 Workspaces::iterator i = std::find(workspaces_.begin(), 143 Workspaces::iterator i = std::find(workspaces_.begin(),
74 workspaces_.end(), 144 workspaces_.end(),
75 workspace); 145 workspace);
76 DCHECK(i == workspaces_.end()); 146 DCHECK(i == workspaces_.end());
77 workspaces_.push_back(workspace); 147 workspaces_.push_back(workspace);
78 } 148 }
79 149
80 void WorkspaceManager::RemoveWorkspace(Workspace* workspace) { 150 void WorkspaceManager::RemoveWorkspace(Workspace* workspace) {
81 Workspaces::iterator i = std::find(workspaces_.begin(), 151 Workspaces::iterator i = std::find(workspaces_.begin(),
82 workspaces_.end(), 152 workspaces_.end(),
83 workspace); 153 workspace);
84 DCHECK(i != workspaces_.end()); 154 DCHECK(i != workspaces_.end());
85 if (workspace == active_workspace_) 155 if (workspace == active_workspace_)
86 active_workspace_ = NULL; 156 active_workspace_ = NULL;
87 workspaces_.erase(i); 157 workspaces_.erase(i);
158 LayoutWorkspaces();
88 } 159 }
89 160
90 void WorkspaceManager::SetActiveWorkspace(Workspace* workspace) { 161 void WorkspaceManager::SetActiveWorkspace(Workspace* workspace) {
91 DCHECK(std::find(workspaces_.begin(), 162 DCHECK(std::find(workspaces_.begin(),
92 workspaces_.end(), 163 workspaces_.end(),
93 workspace) 164 workspace)
94 != workspaces_.end()); 165 != workspaces_.end());
95 active_workspace_ = workspace; 166 active_workspace_ = workspace;
167
168 DCHECK(!workspaces_.empty());
169
170 is_overview_ = false;
171 UpdateViewport();
172 }
173
174 gfx::Rect WorkspaceManager::GetWorkAreaBounds(
175 const gfx::Rect& workspace_bounds) {
176 gfx::Rect bounds = workspace_bounds;
177 bounds.Inset(
178 aura::Desktop::GetInstance()->screen()->work_area_insets());
179 return bounds;
180 }
181
182 void WorkspaceManager::UpdateViewport() {
183 int num_workspaces = std::max(1, static_cast<int>(workspaces_.size()));
184 int total_width = workspace_size_.width() * num_workspaces +
185 kWorkspaceHorizontalMargin * (num_workspaces - 1);
186 gfx::Rect bounds(0, 0, total_width, workspace_size_.height());
187
188 if (viewport_->GetTargetBounds() != bounds)
189 viewport_->SetBounds(bounds);
190
191 // Move to active workspace.
192 if (active_workspace_) {
193 ui::Transform transform;
194 transform.SetTranslateX(-active_workspace_->bounds().x());
195 viewport_->layer()->SetAnimation(aura::Window::CreateDefaultAnimation());
196 viewport_->SetTransform(transform);
197 }
96 } 198 }
97 199
98 } // namespace aura_shell 200 } // namespace aura_shell
OLDNEW
« no previous file with comments | « ui/aura_shell/workspace/workspace_manager.h ('k') | ui/aura_shell/workspace/workspace_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698