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

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

Issue 2129773002: [CrOS] Initial rough cut of alt-tab window cycling UI. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: function rename Created 4 years, 5 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_cycle_list.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/window_cycle_list.h" 5 #include "ash/wm/window_cycle_list.h"
6 6
7 #include <list>
8 #include <map>
9
10 #include "ash/common/ash_switches.h"
11 #include "ash/common/shell_window_ids.h"
12 #include "ash/common/wm/forwarding_layer_delegate.h"
7 #include "ash/common/wm/mru_window_tracker.h" 13 #include "ash/common/wm/mru_window_tracker.h"
8 #include "ash/common/wm/window_state.h" 14 #include "ash/common/wm/window_state.h"
15 #include "ash/common/wm_root_window_controller.h"
9 #include "ash/common/wm_shell.h" 16 #include "ash/common/wm_shell.h"
10 #include "ash/common/wm_window.h" 17 #include "ash/common/wm_window.h"
11 #include "ash/shell.h" 18 #include "ash/shell.h"
12 #include "ash/wm/window_animations.h" 19 #include "ash/wm/window_animations.h"
13 #include "ash/wm/window_util.h" 20 #include "ash/wm/window_util.h"
21 #include "base/command_line.h"
22 #include "ui/compositor/layer_tree_owner.h"
23 #include "ui/views/background.h"
24 #include "ui/views/layout/box_layout.h"
25 #include "ui/views/painter.h"
26 #include "ui/views/view.h"
27 #include "ui/views/widget/widget.h"
28 #include "ui/wm/core/visibility_controller.h"
29 #include "ui/wm/core/window_util.h"
14 30
15 namespace ash { 31 namespace ash {
16 32
33 void EnsureAllChildrenAreVisible(ui::Layer* layer) {
34 std::list<ui::Layer*> layers;
35 layers.push_back(layer);
36 while (!layers.empty()) {
37 for (auto child : layers.front()->children())
38 layers.push_back(child);
39 layers.front()->SetVisible(true);
40 layers.pop_front();
41 }
42 }
43
17 // Returns the window immediately below |window| in the current container. 44 // Returns the window immediately below |window| in the current container.
18 WmWindow* GetWindowBelow(WmWindow* window) { 45 WmWindow* GetWindowBelow(WmWindow* window) {
19 WmWindow* parent = window->GetParent(); 46 WmWindow* parent = window->GetParent();
20 if (!parent) 47 if (!parent)
21 return nullptr; 48 return nullptr;
22 const WmWindow::Windows children = parent->GetChildren(); 49 const WmWindow::Windows children = parent->GetChildren();
23 auto iter = std::find(children.begin(), children.end(), window); 50 auto iter = std::find(children.begin(), children.end(), window);
24 CHECK(*iter == window); 51 CHECK(*iter == window);
25 return (iter != children.begin()) ? *(iter - 1) : nullptr; 52 return (iter != children.begin()) ? *(iter - 1) : nullptr;
26 } 53 }
(...skipping 21 matching lines...) Expand all
48 75
49 // The window immediately below where window_ belongs. 76 // The window immediately below where window_ belongs.
50 WmWindow* stack_window_above_; 77 WmWindow* stack_window_above_;
51 78
52 // If true, minimize window_ on going out of scope. 79 // If true, minimize window_ on going out of scope.
53 bool minimized_; 80 bool minimized_;
54 81
55 DISALLOW_COPY_AND_ASSIGN(ScopedShowWindow); 82 DISALLOW_COPY_AND_ASSIGN(ScopedShowWindow);
56 }; 83 };
57 84
85 // A view that mirrors a single window. Layers are lifted from the underlying
86 // window (which gets new ones in their place). New paint calls, if any, are
87 // forwarded to the underlying window.
88 class WindowMirrorView : public views::View, public ::wm::LayerDelegateFactory {
89 public:
90 explicit WindowMirrorView(WmWindow* window) : target_(window) {
91 DCHECK(window);
92 }
93 ~WindowMirrorView() override {}
94
95 void Init() {
96 SetPaintToLayer(true);
97
98 layer_owner_ = ::wm::RecreateLayers(
99 target_->GetInternalWidget()->GetNativeView(), this);
100
101 GetMirrorLayer()->parent()->Remove(GetMirrorLayer());
102 layer()->Add(GetMirrorLayer());
103
104 // Some extra work is needed when the target window is minimized.
105 if (target_->GetWindowState()->IsMinimized()) {
106 GetMirrorLayer()->SetVisible(true);
107 GetMirrorLayer()->SetOpacity(1);
108 EnsureAllChildrenAreVisible(GetMirrorLayer());
109 }
110 }
111
112 // views::View:
113 gfx::Size GetPreferredSize() const override {
114 const int kMaxWidth = 800;
115 const int kMaxHeight = 600;
116
117 gfx::Size target_size = target_->GetBounds().size();
118 if (target_size.width() <= kMaxWidth &&
119 target_size.height() <= kMaxHeight) {
120 return target_size;
121 }
122
123 float scale =
124 std::min(kMaxWidth / static_cast<float>(target_size.width()),
125 kMaxHeight / static_cast<float>(target_size.height()));
126 return gfx::ScaleToCeiledSize(target_size, scale, scale);
127 }
128
129 void Layout() override {
130 // Position at 0, 0.
131 GetMirrorLayer()->SetBounds(gfx::Rect(GetMirrorLayer()->bounds().size()));
132
133 // Scale down if necessary.
134 gfx::Transform mirror_transform;
135 if (size() != target_->GetBounds().size()) {
136 const float scale =
137 width() / static_cast<float>(target_->GetBounds().width());
138 mirror_transform.Scale(scale, scale);
139 }
140 GetMirrorLayer()->SetTransform(mirror_transform);
141 }
142
143 // ::wm::LayerDelegateFactory:
144 ui::LayerDelegate* CreateDelegate(ui::LayerDelegate* delegate) override {
145 if (!delegate)
146 return nullptr;
147 delegates_.push_back(
148 base::WrapUnique(new wm::ForwardingLayerDelegate(target_, delegate)));
149
150 return delegates_.back().get();
151 }
152
153 private:
154 // Gets the root of the layer tree that was lifted from |target_| (and is now
155 // a child of |this->layer()|).
156 ui::Layer* GetMirrorLayer() { return layer_owner_->root(); }
157
158 // The original window that is being represented by |this|.
159 WmWindow* target_;
160
161 // Retains ownership of the mirror layer tree.
162 std::unique_ptr<ui::LayerTreeOwner> layer_owner_;
163
164 std::vector<std::unique_ptr<wm::ForwardingLayerDelegate>> delegates_;
165
166 DISALLOW_COPY_AND_ASSIGN(WindowMirrorView);
167 };
168
169 // A view that shows a collection of windows the user can tab through.
170 class WindowCycleView : public views::View {
171 public:
172 explicit WindowCycleView(const WindowCycleList::WindowList& windows)
173 : mirror_container_(new views::View()),
174 selector_view_(new views::View()),
175 target_window_(nullptr) {
176 DCHECK(!windows.empty());
177 SetPaintToLayer(true);
178 layer()->SetFillsBoundsOpaquely(false);
179
180 // TODO(estade): adjust constants in this function (colors, spacing, corner
181 // radius) as per mocks.
182 const float kCornerRadius = 5;
183 set_background(views::Background::CreateBackgroundPainter(
184 true, views::Painter::CreateSolidRoundRectPainter(
185 SkColorSetA(SK_ColorBLACK, 0xA5), kCornerRadius)));
186
187 views::BoxLayout* layout =
188 new views::BoxLayout(views::BoxLayout::kHorizontal, 25, 25, 20);
189 layout->set_cross_axis_alignment(
190 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START);
191 mirror_container_->SetLayoutManager(layout);
192
193 for (WmWindow* window : windows) {
194 WindowMirrorView* view = new WindowMirrorView(window);
195 view->Init();
196 window_view_map_[window] = view;
197 mirror_container_->AddChildView(view);
198 }
199
200 selector_view_->set_background(views::Background::CreateBackgroundPainter(
201 true, views::Painter::CreateSolidRoundRectPainter(SK_ColorBLUE,
202 kCornerRadius)));
203
204 AddChildView(selector_view_);
205 AddChildView(mirror_container_);
206 SetTargetWindow(windows.front());
207 }
208
209 ~WindowCycleView() override {}
210
211 void SetTargetWindow(WmWindow* target) {
212 target_window_ = target;
213 if (GetWidget())
214 Layout();
215 }
216
217 void HandleWindowDestruction(WmWindow* destroying_window,
218 WmWindow* new_target) {
219 auto view_iter = window_view_map_.find(destroying_window);
220 view_iter->second->parent()->RemoveChildView(view_iter->second);
221 window_view_map_.erase(view_iter);
222 SetTargetWindow(new_target);
223 }
224
225 // views::View overrides:
226 gfx::Size GetPreferredSize() const override {
227 return mirror_container_->GetPreferredSize();
228 }
229
230 void Layout() override {
231 // Possible if the last window is deleted.
232 if (!target_window_)
233 return;
234
235 views::View* target_view = window_view_map_[target_window_];
236 gfx::RectF target_bounds(target_view->GetLocalBounds());
237 views::View::ConvertRectToTarget(target_view, this, &target_bounds);
238 target_bounds.Inset(gfx::InsetsF(-15));
239 selector_view_->SetBoundsRect(gfx::ToEnclosingRect(target_bounds));
240
241 mirror_container_->SetBoundsRect(GetLocalBounds());
242 }
243
244 WmWindow* target_window() { return target_window_; }
245
246 private:
247 std::map<WmWindow*, WindowMirrorView*> window_view_map_;
248 views::View* mirror_container_;
249 views::View* selector_view_;
250 WmWindow* target_window_;
251
252 DISALLOW_COPY_AND_ASSIGN(WindowCycleView);
253 };
254
58 ScopedShowWindow::ScopedShowWindow() 255 ScopedShowWindow::ScopedShowWindow()
59 : window_(nullptr), stack_window_above_(nullptr), minimized_(false) {} 256 : window_(nullptr), stack_window_above_(nullptr), minimized_(false) {}
60 257
61 ScopedShowWindow::~ScopedShowWindow() { 258 ScopedShowWindow::~ScopedShowWindow() {
62 if (window_) { 259 if (window_) {
63 window_->GetParent()->RemoveObserver(this); 260 window_->GetParent()->RemoveObserver(this);
64 261
65 // Restore window's stacking position. 262 // Restore window's stacking position.
66 if (stack_window_above_) 263 if (stack_window_above_)
67 window_->GetParent()->StackChildAbove(window_, stack_window_above_); 264 window_->GetParent()->StackChildAbove(window_, stack_window_above_);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 if (params.target == window_) { 297 if (params.target == window_) {
101 CancelRestore(); 298 CancelRestore();
102 } else if (params.target == stack_window_above_) { 299 } else if (params.target == stack_window_above_) {
103 // If the window this window was above is removed, use the next window down 300 // If the window this window was above is removed, use the next window down
104 // as the restore marker. 301 // as the restore marker.
105 stack_window_above_ = GetWindowBelow(stack_window_above_); 302 stack_window_above_ = GetWindowBelow(stack_window_above_);
106 } 303 }
107 } 304 }
108 305
109 WindowCycleList::WindowCycleList(const WindowList& windows) 306 WindowCycleList::WindowCycleList(const WindowList& windows)
110 : windows_(windows), current_index_(0) { 307 : windows_(windows), current_index_(0), cycle_view_(nullptr) {
111 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(true); 308 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(true);
112 309
113 for (WmWindow* window : windows_) 310 for (WmWindow* window : windows_)
114 window->AddObserver(this); 311 window->AddObserver(this);
312
313 if (ShouldShowUi()) {
314 WmWindow* root_window = WmShell::Get()->GetRootWindowForNewWindows();
315 views::Widget* widget = new views::Widget;
316 views::Widget::InitParams params;
317 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
318 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
319 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
320 params.accept_events = true;
321 // TODO(estade): make sure nothing untoward happens when the lock screen
322 // or a system modal dialog is shown.
323 root_window->GetRootWindowController()
324 ->ConfigureWidgetInitParamsForContainer(
325 widget, kShellWindowId_OverlayContainer, &params);
326 widget->Init(params);
327
328 cycle_view_ = new WindowCycleView(windows_);
329
330 widget->SetContentsView(cycle_view_);
331 // TODO(estade): right now this just extends past the edge of the screen if
332 // there are too many windows. Handle this more gracefully. Also, if
333 // the display metrics change, cancel the UI.
334 gfx::Rect widget_rect = widget->GetWorkAreaBoundsInScreen();
335 gfx::Size widget_size = cycle_view_->GetPreferredSize();
336 widget_rect.ClampToCenteredSize(widget_size);
337 widget_rect.set_width(widget_size.width());
338 widget->SetBounds(widget_rect);
339 widget->Show();
340 cycle_ui_widget_.reset(widget);
341 }
115 } 342 }
116 343
117 WindowCycleList::~WindowCycleList() { 344 WindowCycleList::~WindowCycleList() {
118 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(false); 345 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(false);
119 for (WmWindow* window : windows_) { 346 for (WmWindow* window : windows_)
120 // TODO(oshima): Remove this once crbug.com/483491 is fixed.
121 CHECK(window);
122 window->RemoveObserver(this); 347 window->RemoveObserver(this);
123 } 348
124 if (showing_window_) 349 if (showing_window_)
125 showing_window_->CancelRestore(); 350 showing_window_->CancelRestore();
351
352 if (cycle_view_ && cycle_view_->target_window()) {
353 cycle_view_->target_window()->Show();
354 cycle_view_->target_window()->GetWindowState()->Activate();
355 }
126 } 356 }
127 357
128 void WindowCycleList::Step(WindowCycleController::Direction direction) { 358 void WindowCycleList::Step(WindowCycleController::Direction direction) {
129 if (windows_.empty()) 359 if (windows_.empty())
130 return; 360 return;
131 361
132 // When there is only one window, we should give feedback to the user. If the 362 // When there is only one window, we should give feedback to the user. If the
133 // window is minimized, we should also show it. 363 // window is minimized, we should also show it.
134 if (windows_.size() == 1) { 364 if (windows_.size() == 1) {
135 windows_[0]->Animate(::wm::WINDOW_ANIMATION_TYPE_BOUNCE); 365 windows_[0]->Animate(::wm::WINDOW_ANIMATION_TYPE_BOUNCE);
136 windows_[0]->Show(); 366 windows_[0]->Show();
137 windows_[0]->GetWindowState()->Activate(); 367 windows_[0]->GetWindowState()->Activate();
138 return; 368 return;
139 } 369 }
140 370
141 DCHECK(static_cast<size_t>(current_index_) < windows_.size()); 371 DCHECK(static_cast<size_t>(current_index_) < windows_.size());
142 372
143 // We're in a valid cycle, so step forward or backward. 373 // We're in a valid cycle, so step forward or backward.
144 current_index_ += direction == WindowCycleController::FORWARD ? 1 : -1; 374 current_index_ += direction == WindowCycleController::FORWARD ? 1 : -1;
145 375
146 // Wrap to window list size. 376 // Wrap to window list size.
147 current_index_ = (current_index_ + windows_.size()) % windows_.size(); 377 current_index_ = (current_index_ + windows_.size()) % windows_.size();
148 DCHECK(windows_[current_index_]); 378 DCHECK(windows_[current_index_]);
149 379
380 if (cycle_view_) {
381 cycle_view_->SetTargetWindow(windows_[current_index_]);
382 return;
383 }
384
150 // Make sure the next window is visible. 385 // Make sure the next window is visible.
151 showing_window_.reset(new ScopedShowWindow); 386 showing_window_.reset(new ScopedShowWindow);
152 showing_window_->Show(windows_[current_index_]); 387 showing_window_->Show(windows_[current_index_]);
153 } 388 }
154 389
155 void WindowCycleList::OnWindowDestroying(WmWindow* window) { 390 void WindowCycleList::OnWindowDestroying(WmWindow* window) {
156 window->RemoveObserver(this); 391 window->RemoveObserver(this);
157 392
158 WindowList::iterator i = std::find(windows_.begin(), windows_.end(), window); 393 WindowList::iterator i = std::find(windows_.begin(), windows_.end(), window);
159 // TODO(oshima): Change this back to DCHECK once crbug.com/483491 is fixed. 394 // TODO(oshima): Change this back to DCHECK once crbug.com/483491 is fixed.
160 CHECK(i != windows_.end()); 395 CHECK(i != windows_.end());
161 int removed_index = static_cast<int>(i - windows_.begin()); 396 int removed_index = static_cast<int>(i - windows_.begin());
162 windows_.erase(i); 397 windows_.erase(i);
163 if (current_index_ > removed_index || 398 if (current_index_ > removed_index ||
164 current_index_ == static_cast<int>(windows_.size())) { 399 current_index_ == static_cast<int>(windows_.size())) {
165 current_index_--; 400 current_index_--;
166 } 401 }
402
403 if (cycle_view_) {
404 WmWindow* new_target_window =
405 windows_.empty() ? nullptr : windows_[current_index_];
406 cycle_view_->HandleWindowDestruction(window, new_target_window);
407 if (windows_.empty()) {
408 // This deletes us.
409 Shell::GetInstance()->window_cycle_controller()->StopCycling();
410 return;
411 }
412 }
413 }
414
415 bool WindowCycleList::ShouldShowUi() {
416 return windows_.size() > 1 &&
417 base::CommandLine::ForCurrentProcess()->HasSwitch(
418 switches::kAshEnableWindowCycleUi);
167 } 419 }
168 420
169 } // namespace ash 421 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/window_cycle_list.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698