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

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

Issue 2157393002: Moves WindowCycleList/Controller to ash/common (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Feedback and std::move 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/common/wm/window_cycle_list.h ('k') | ash/common/wm_shell.h » ('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 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/common/wm/window_cycle_list.h"
6 6
7 #include <list> 7 #include <list>
8 #include <map> 8 #include <map>
9 9
10 #include "ash/common/ash_switches.h" 10 #include "ash/common/ash_switches.h"
11 #include "ash/common/shell_window_ids.h" 11 #include "ash/common/shell_window_ids.h"
12 #include "ash/common/wm/forwarding_layer_delegate.h" 12 #include "ash/common/wm/forwarding_layer_delegate.h"
13 #include "ash/common/wm/mru_window_tracker.h" 13 #include "ash/common/wm/mru_window_tracker.h"
14 #include "ash/common/wm/window_state.h" 14 #include "ash/common/wm/window_state.h"
15 #include "ash/common/wm_root_window_controller.h" 15 #include "ash/common/wm_root_window_controller.h"
16 #include "ash/common/wm_shell.h" 16 #include "ash/common/wm_shell.h"
17 #include "ash/common/wm_window.h" 17 #include "ash/common/wm_window.h"
18 #include "ash/shell.h"
19 #include "base/command_line.h" 18 #include "base/command_line.h"
20 #include "ui/compositor/layer_tree_owner.h"
21 #include "ui/views/background.h" 19 #include "ui/views/background.h"
22 #include "ui/views/layout/box_layout.h" 20 #include "ui/views/layout/box_layout.h"
23 #include "ui/views/painter.h" 21 #include "ui/views/painter.h"
24 #include "ui/views/view.h" 22 #include "ui/views/view.h"
25 #include "ui/views/widget/widget.h" 23 #include "ui/views/widget/widget.h"
26 #include "ui/wm/core/visibility_controller.h" 24 #include "ui/wm/core/visibility_controller.h"
27 #include "ui/wm/core/window_util.h"
28 25
29 namespace ash { 26 namespace ash {
30 27
31 void EnsureAllChildrenAreVisible(ui::Layer* layer) {
32 std::list<ui::Layer*> layers;
33 layers.push_back(layer);
34 while (!layers.empty()) {
35 for (auto child : layers.front()->children())
36 layers.push_back(child);
37 layers.front()->SetVisible(true);
38 layers.pop_front();
39 }
40 }
41
42 // Returns the window immediately below |window| in the current container. 28 // Returns the window immediately below |window| in the current container.
43 WmWindow* GetWindowBelow(WmWindow* window) { 29 WmWindow* GetWindowBelow(WmWindow* window) {
44 WmWindow* parent = window->GetParent(); 30 WmWindow* parent = window->GetParent();
45 if (!parent) 31 if (!parent)
46 return nullptr; 32 return nullptr;
47 const WmWindow::Windows children = parent->GetChildren(); 33 const WmWindow::Windows children = parent->GetChildren();
48 auto iter = std::find(children.begin(), children.end(), window); 34 auto iter = std::find(children.begin(), children.end(), window);
49 CHECK(*iter == window); 35 CHECK(*iter == window);
50 return (iter != children.begin()) ? *(iter - 1) : nullptr; 36 return (iter != children.begin()) ? *(iter - 1) : nullptr;
51 } 37 }
(...skipping 21 matching lines...) Expand all
73 59
74 // The window immediately below where window_ belongs. 60 // The window immediately below where window_ belongs.
75 WmWindow* stack_window_above_; 61 WmWindow* stack_window_above_;
76 62
77 // If true, minimize window_ on going out of scope. 63 // If true, minimize window_ on going out of scope.
78 bool minimized_; 64 bool minimized_;
79 65
80 DISALLOW_COPY_AND_ASSIGN(ScopedShowWindow); 66 DISALLOW_COPY_AND_ASSIGN(ScopedShowWindow);
81 }; 67 };
82 68
83 // A view that mirrors a single window. Layers are lifted from the underlying
84 // window (which gets new ones in their place). New paint calls, if any, are
85 // forwarded to the underlying window.
86 class WindowMirrorView : public views::View, public ::wm::LayerDelegateFactory {
87 public:
88 explicit WindowMirrorView(WmWindow* window) : target_(window) {
89 DCHECK(window);
90 }
91 ~WindowMirrorView() override {}
92
93 void Init() {
94 SetPaintToLayer(true);
95
96 layer_owner_ = ::wm::RecreateLayers(
97 target_->GetInternalWidget()->GetNativeView(), this);
98
99 GetMirrorLayer()->parent()->Remove(GetMirrorLayer());
100 layer()->Add(GetMirrorLayer());
101
102 // Some extra work is needed when the target window is minimized.
103 if (target_->GetWindowState()->IsMinimized()) {
104 GetMirrorLayer()->SetVisible(true);
105 GetMirrorLayer()->SetOpacity(1);
106 EnsureAllChildrenAreVisible(GetMirrorLayer());
107 }
108 }
109
110 // views::View:
111 gfx::Size GetPreferredSize() const override {
112 const int kMaxWidth = 512;
113 const int kMaxHeight = 256;
114
115 gfx::Size target_size = target_->GetBounds().size();
116 if (target_size.width() <= kMaxWidth &&
117 target_size.height() <= kMaxHeight) {
118 return target_size;
119 }
120
121 float scale =
122 std::min(kMaxWidth / static_cast<float>(target_size.width()),
123 kMaxHeight / static_cast<float>(target_size.height()));
124 return gfx::ScaleToCeiledSize(target_size, scale, scale);
125 }
126
127 void Layout() override {
128 // Position at 0, 0.
129 GetMirrorLayer()->SetBounds(gfx::Rect(GetMirrorLayer()->bounds().size()));
130
131 // Scale down if necessary.
132 gfx::Transform mirror_transform;
133 if (size() != target_->GetBounds().size()) {
134 const float scale =
135 width() / static_cast<float>(target_->GetBounds().width());
136 mirror_transform.Scale(scale, scale);
137 }
138 GetMirrorLayer()->SetTransform(mirror_transform);
139 }
140
141 // ::wm::LayerDelegateFactory:
142 ui::LayerDelegate* CreateDelegate(ui::LayerDelegate* delegate) override {
143 if (!delegate)
144 return nullptr;
145 delegates_.push_back(
146 base::WrapUnique(new wm::ForwardingLayerDelegate(target_, delegate)));
147
148 return delegates_.back().get();
149 }
150
151 private:
152 // Gets the root of the layer tree that was lifted from |target_| (and is now
153 // a child of |this->layer()|).
154 ui::Layer* GetMirrorLayer() { return layer_owner_->root(); }
155
156 // The original window that is being represented by |this|.
157 WmWindow* target_;
158
159 // Retains ownership of the mirror layer tree.
160 std::unique_ptr<ui::LayerTreeOwner> layer_owner_;
161
162 std::vector<std::unique_ptr<wm::ForwardingLayerDelegate>> delegates_;
163
164 DISALLOW_COPY_AND_ASSIGN(WindowMirrorView);
165 };
166
167 // A view that shows a collection of windows the user can tab through. 69 // A view that shows a collection of windows the user can tab through.
168 class WindowCycleView : public views::View { 70 class WindowCycleView : public views::View {
169 public: 71 public:
170 explicit WindowCycleView(const WindowCycleList::WindowList& windows) 72 explicit WindowCycleView(const WindowCycleList::WindowList& windows)
171 : mirror_container_(new views::View()), 73 : mirror_container_(new views::View()),
172 highlight_view_(new views::View()), 74 highlight_view_(new views::View()),
173 target_window_(nullptr) { 75 target_window_(nullptr) {
174 DCHECK(!windows.empty()); 76 DCHECK(!windows.empty());
175 SetPaintToLayer(true); 77 SetPaintToLayer(true);
176 layer()->SetFillsBoundsOpaquely(false); 78 layer()->SetFillsBoundsOpaquely(false);
(...skipping 10 matching lines...) Expand all
187 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); 89 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START);
188 mirror_container_->SetLayoutManager(layout); 90 mirror_container_->SetLayoutManager(layout);
189 mirror_container_->SetPaintToLayer(true); 91 mirror_container_->SetPaintToLayer(true);
190 mirror_container_->layer()->SetFillsBoundsOpaquely(false); 92 mirror_container_->layer()->SetFillsBoundsOpaquely(false);
191 // The preview list animates bounds changes (other animatable properties 93 // The preview list animates bounds changes (other animatable properties
192 // never change). 94 // never change).
193 mirror_container_->layer()->SetAnimator( 95 mirror_container_->layer()->SetAnimator(
194 ui::LayerAnimator::CreateImplicitAnimator()); 96 ui::LayerAnimator::CreateImplicitAnimator());
195 97
196 for (WmWindow* window : windows) { 98 for (WmWindow* window : windows) {
197 WindowMirrorView* view = new WindowMirrorView(window); 99 // |mirror_container_| owns |view|.
198 view->Init(); 100 views::View* view = window->CreateViewWithRecreatedLayers().release();
199 window_view_map_[window] = view; 101 window_view_map_[window] = view;
200 mirror_container_->AddChildView(view); 102 mirror_container_->AddChildView(view);
201 } 103 }
202 104
203 const float kHighlightCornerRadius = 4; 105 const float kHighlightCornerRadius = 4;
204 highlight_view_->set_background(views::Background::CreateBackgroundPainter( 106 highlight_view_->set_background(views::Background::CreateBackgroundPainter(
205 true, views::Painter::CreateRoundRectWith1PxBorderPainter( 107 true, views::Painter::CreateRoundRectWith1PxBorderPainter(
206 SkColorSetA(SK_ColorWHITE, 0x4D), 108 SkColorSetA(SK_ColorWHITE, 0x4D),
207 SkColorSetA(SK_ColorWHITE, 0x33), kHighlightCornerRadius))); 109 SkColorSetA(SK_ColorWHITE, 0x33), kHighlightCornerRadius)));
208 highlight_view_->SetPaintToLayer(true); 110 highlight_view_->SetPaintToLayer(true);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 // Calculate the target preview's bounds relative to |this|. 162 // Calculate the target preview's bounds relative to |this|.
261 views::View::ConvertRectToTarget(mirror_container_, this, &target_bounds); 163 views::View::ConvertRectToTarget(mirror_container_, this, &target_bounds);
262 const int kHighlightPaddingDip = 5; 164 const int kHighlightPaddingDip = 5;
263 target_bounds.Inset(gfx::InsetsF(-kHighlightPaddingDip)); 165 target_bounds.Inset(gfx::InsetsF(-kHighlightPaddingDip));
264 highlight_view_->SetBoundsRect(gfx::ToEnclosingRect(target_bounds)); 166 highlight_view_->SetBoundsRect(gfx::ToEnclosingRect(target_bounds));
265 } 167 }
266 168
267 WmWindow* target_window() { return target_window_; } 169 WmWindow* target_window() { return target_window_; }
268 170
269 private: 171 private:
270 std::map<WmWindow*, WindowMirrorView*> window_view_map_; 172 std::map<WmWindow*, views::View*> window_view_map_;
271 views::View* mirror_container_; 173 views::View* mirror_container_;
272 views::View* highlight_view_; 174 views::View* highlight_view_;
273 WmWindow* target_window_; 175 WmWindow* target_window_;
274 176
275 DISALLOW_COPY_AND_ASSIGN(WindowCycleView); 177 DISALLOW_COPY_AND_ASSIGN(WindowCycleView);
276 }; 178 };
277 179
278 ScopedShowWindow::ScopedShowWindow() 180 ScopedShowWindow::ScopedShowWindow()
279 : window_(nullptr), stack_window_above_(nullptr), minimized_(false) {} 181 : window_(nullptr), stack_window_above_(nullptr), minimized_(false) {}
280 182
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 current_index_ == static_cast<int>(windows_.size())) { 324 current_index_ == static_cast<int>(windows_.size())) {
423 current_index_--; 325 current_index_--;
424 } 326 }
425 327
426 if (cycle_view_) { 328 if (cycle_view_) {
427 WmWindow* new_target_window = 329 WmWindow* new_target_window =
428 windows_.empty() ? nullptr : windows_[current_index_]; 330 windows_.empty() ? nullptr : windows_[current_index_];
429 cycle_view_->HandleWindowDestruction(window, new_target_window); 331 cycle_view_->HandleWindowDestruction(window, new_target_window);
430 if (windows_.empty()) { 332 if (windows_.empty()) {
431 // This deletes us. 333 // This deletes us.
432 Shell::GetInstance()->window_cycle_controller()->StopCycling(); 334 WmShell::Get()->window_cycle_controller()->StopCycling();
433 return; 335 return;
434 } 336 }
435 } 337 }
436 } 338 }
437 339
438 bool WindowCycleList::ShouldShowUi() { 340 bool WindowCycleList::ShouldShowUi() {
439 return windows_.size() > 1 && 341 return windows_.size() > 1 &&
440 base::CommandLine::ForCurrentProcess()->HasSwitch( 342 base::CommandLine::ForCurrentProcess()->HasSwitch(
441 switches::kAshEnableWindowCycleUi); 343 switches::kAshEnableWindowCycleUi);
442 } 344 }
443 345
444 } // namespace ash 346 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/wm/window_cycle_list.h ('k') | ash/common/wm_shell.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698