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

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: one more todo 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
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 {
sadrul 2016/07/13 18:38:07 Can you move this into a separate file? We should
Evan Stade 2016/07/13 19:31:56 are you suggesting a separate file for every class
sadrul 2016/07/15 14:41:53 I am suggesting moving this WindowMirrorView class
Evan Stade 2016/07/15 17:37:15 OK. If you don't mind, I'll leave this here for no
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 SetPaintToLayer(true);
177 layer()->SetFillsBoundsOpaquely(false);
178
179 // TODO(estade): adjust constants in this function (colors, spacing, corner
180 // radius) as per mocks.
181 const float kCornerRadius = 5;
182 set_background(views::Background::CreateBackgroundPainter(
183 true, views::Painter::CreateSolidRoundRectPainter(
184 SkColorSetA(SK_ColorBLACK, 0xA5), kCornerRadius)));
185
186 views::BoxLayout* layout =
187 new views::BoxLayout(views::BoxLayout::kHorizontal, 25, 25, 20);
188 layout->set_cross_axis_alignment(
189 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START);
190 mirror_container_->SetLayoutManager(layout);
191
192 for (WmWindow* window : windows) {
193 WindowMirrorView* view = new WindowMirrorView(window);
194 view->Init();
195 window_view_map_[window] = view;
196 mirror_container_->AddChildView(view);
197 }
198
199 selector_view_->set_background(views::Background::CreateBackgroundPainter(
200 true, views::Painter::CreateSolidRoundRectPainter(SK_ColorBLUE,
201 kCornerRadius)));
202
203 AddChildView(selector_view_);
204 AddChildView(mirror_container_);
205 SetTargetWindow(windows.front());
206 }
207
208 ~WindowCycleView() override {}
209
210 void SetTargetWindow(WmWindow* target) {
211 target_window_ = target;
212 if (GetWidget())
213 Layout();
214 }
215
216 void HandleWindowDestruction(WmWindow* destroying_window,
217 WmWindow* new_target) {
218 views::View* destroying_view = window_view_map_[destroying_window];
219 destroying_view->parent()->RemoveChildView(destroying_view);
220 SetTargetWindow(new_target);
221 }
222
223 // views::View overrides:
224 gfx::Size GetPreferredSize() const override {
225 return mirror_container_->GetPreferredSize();
226 }
227
228 void Layout() override {
229 views::View* target_view = window_view_map_[target_window_];
sadrul 2016/07/13 18:38:07 What happens if target_window_ is null?
Evan Stade 2016/07/13 19:31:56 It should not be possible. Added a dcheck on |wind
230 gfx::RectF target_bounds(target_view->GetLocalBounds());
231 views::View::ConvertRectToTarget(target_view, this, &target_bounds);
232 target_bounds.Inset(gfx::InsetsF(-15));
233 selector_view_->SetBoundsRect(gfx::ToEnclosingRect(target_bounds));
234
235 mirror_container_->SetBoundsRect(GetLocalBounds());
236 }
237
238 WmWindow* target_window() { return target_window_; }
239
240 private:
241 std::map<WmWindow*, WindowMirrorView*> window_view_map_;
242 views::View* mirror_container_;
243 views::View* selector_view_;
244 WmWindow* target_window_;
245
246 DISALLOW_COPY_AND_ASSIGN(WindowCycleView);
247 };
248
58 ScopedShowWindow::ScopedShowWindow() 249 ScopedShowWindow::ScopedShowWindow()
59 : window_(nullptr), stack_window_above_(nullptr), minimized_(false) {} 250 : window_(nullptr), stack_window_above_(nullptr), minimized_(false) {}
60 251
61 ScopedShowWindow::~ScopedShowWindow() { 252 ScopedShowWindow::~ScopedShowWindow() {
62 if (window_) { 253 if (window_) {
63 window_->GetParent()->RemoveObserver(this); 254 window_->GetParent()->RemoveObserver(this);
64 255
65 // Restore window's stacking position. 256 // Restore window's stacking position.
66 if (stack_window_above_) 257 if (stack_window_above_)
67 window_->GetParent()->StackChildAbove(window_, stack_window_above_); 258 window_->GetParent()->StackChildAbove(window_, stack_window_above_);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 if (params.target == window_) { 291 if (params.target == window_) {
101 CancelRestore(); 292 CancelRestore();
102 } else if (params.target == stack_window_above_) { 293 } else if (params.target == stack_window_above_) {
103 // If the window this window was above is removed, use the next window down 294 // If the window this window was above is removed, use the next window down
104 // as the restore marker. 295 // as the restore marker.
105 stack_window_above_ = GetWindowBelow(stack_window_above_); 296 stack_window_above_ = GetWindowBelow(stack_window_above_);
106 } 297 }
107 } 298 }
108 299
109 WindowCycleList::WindowCycleList(const WindowList& windows) 300 WindowCycleList::WindowCycleList(const WindowList& windows)
110 : windows_(windows), current_index_(0) { 301 : windows_(windows), current_index_(0), cycle_view_(nullptr) {
111 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(true); 302 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(true);
112 303
113 for (WmWindow* window : windows_) 304 for (WmWindow* window : windows_)
114 window->AddObserver(this); 305 window->AddObserver(this);
306
307 if (ShouldShowUi()) {
308 WmWindow* root_window = WmShell::Get()->GetPrimaryRootWindow();
309 views::Widget* widget = new views::Widget;
310 views::Widget::InitParams params;
311 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
312 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
313 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
314 params.accept_events = true;
315 root_window->GetRootWindowController()
316 ->ConfigureWidgetInitParamsForContainer(
317 widget, kShellWindowId_OverlayContainer, &params);
318 widget->Init(params);
319
320 cycle_view_ = new WindowCycleView(windows_);
321
322 widget->SetContentsView(cycle_view_);
323 // TODO(estade): right now this just extends past the edge of the screen if
324 // there are too many windows. Handle this more gracefully.
325 gfx::Rect widget_rect = widget->GetWorkAreaBoundsInScreen();
326 gfx::Size widget_size = cycle_view_->GetPreferredSize();
327 widget_rect.ClampToCenteredSize(widget_size);
328 widget_rect.set_width(widget_size.width());
329 widget->SetBounds(widget_rect);
330 widget->Show();
331 cycle_ui_widget_.reset(widget);
332 }
115 } 333 }
116 334
117 WindowCycleList::~WindowCycleList() { 335 WindowCycleList::~WindowCycleList() {
118 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(false); 336 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(false);
119 for (WmWindow* window : windows_) { 337 for (WmWindow* window : windows_) {
120 // TODO(oshima): Remove this once crbug.com/483491 is fixed. 338 // TODO(oshima): Remove this once crbug.com/483491 is fixed.
121 CHECK(window); 339 CHECK(window);
122 window->RemoveObserver(this); 340 window->RemoveObserver(this);
123 } 341 }
124 if (showing_window_) 342 if (showing_window_)
125 showing_window_->CancelRestore(); 343 showing_window_->CancelRestore();
344
345 if (cycle_view_) {
346 cycle_view_->target_window()->Show();
347 cycle_view_->target_window()->GetWindowState()->Activate();
348 }
126 } 349 }
127 350
128 void WindowCycleList::Step(WindowCycleController::Direction direction) { 351 void WindowCycleList::Step(WindowCycleController::Direction direction) {
129 if (windows_.empty()) 352 if (windows_.empty())
130 return; 353 return;
131 354
132 // When there is only one window, we should give feedback to the user. If the 355 // When there is only one window, we should give feedback to the user. If the
133 // window is minimized, we should also show it. 356 // window is minimized, we should also show it.
134 if (windows_.size() == 1) { 357 if (windows_.size() == 1) {
135 windows_[0]->Animate(::wm::WINDOW_ANIMATION_TYPE_BOUNCE); 358 windows_[0]->Animate(::wm::WINDOW_ANIMATION_TYPE_BOUNCE);
136 windows_[0]->Show(); 359 windows_[0]->Show();
137 windows_[0]->GetWindowState()->Activate(); 360 windows_[0]->GetWindowState()->Activate();
138 return; 361 return;
139 } 362 }
140 363
141 DCHECK(static_cast<size_t>(current_index_) < windows_.size()); 364 DCHECK(static_cast<size_t>(current_index_) < windows_.size());
142 365
143 // We're in a valid cycle, so step forward or backward. 366 // We're in a valid cycle, so step forward or backward.
144 current_index_ += direction == WindowCycleController::FORWARD ? 1 : -1; 367 current_index_ += direction == WindowCycleController::FORWARD ? 1 : -1;
145 368
146 // Wrap to window list size. 369 // Wrap to window list size.
147 current_index_ = (current_index_ + windows_.size()) % windows_.size(); 370 current_index_ = (current_index_ + windows_.size()) % windows_.size();
148 DCHECK(windows_[current_index_]); 371 DCHECK(windows_[current_index_]);
149 372
373 if (cycle_view_) {
374 cycle_view_->SetTargetWindow(windows_[current_index_]);
375 return;
376 }
377
150 // Make sure the next window is visible. 378 // Make sure the next window is visible.
151 showing_window_.reset(new ScopedShowWindow); 379 showing_window_.reset(new ScopedShowWindow);
152 showing_window_->Show(windows_[current_index_]); 380 showing_window_->Show(windows_[current_index_]);
153 } 381 }
154 382
155 void WindowCycleList::OnWindowDestroying(WmWindow* window) { 383 void WindowCycleList::OnWindowDestroying(WmWindow* window) {
156 window->RemoveObserver(this); 384 window->RemoveObserver(this);
157 385
158 WindowList::iterator i = std::find(windows_.begin(), windows_.end(), window); 386 WindowList::iterator i = std::find(windows_.begin(), windows_.end(), window);
159 // TODO(oshima): Change this back to DCHECK once crbug.com/483491 is fixed. 387 // TODO(oshima): Change this back to DCHECK once crbug.com/483491 is fixed.
160 CHECK(i != windows_.end()); 388 CHECK(i != windows_.end());
161 int removed_index = static_cast<int>(i - windows_.begin()); 389 int removed_index = static_cast<int>(i - windows_.begin());
162 windows_.erase(i); 390 windows_.erase(i);
163 if (current_index_ > removed_index || 391 if (current_index_ > removed_index ||
164 current_index_ == static_cast<int>(windows_.size())) { 392 current_index_ == static_cast<int>(windows_.size())) {
165 current_index_--; 393 current_index_--;
166 } 394 }
395
396 // TODO(estade): what if this is the last (or second from last) window?
397 if (cycle_view_)
398 cycle_view_->HandleWindowDestruction(window, windows_[current_index_]);
sadrul 2016/07/13 18:38:07 windows_.empty() ? nullptr : windows_[current_inde
Evan Stade 2016/07/13 19:31:56 I thought about it but that alone is not a satisfy
sadrul 2016/07/15 14:41:53 Acknowledged.
399 }
400
401 bool WindowCycleList::ShouldShowUi() {
402 return windows_.size() > 1 &&
403 base::CommandLine::ForCurrentProcess()->HasSwitch(
404 switches::kAshEnableWindowCycleUi);
167 } 405 }
168 406
169 } // namespace ash 407 } // namespace ash
OLDNEW
« ash/common/wm/forwarding_layer_delegate.h ('K') | « ash/wm/window_cycle_list.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698