Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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()); | |
|
sadrul
2016/07/15 14:41:53
Where do we restore the opacity/visibility?
Evan Stade
2016/07/15 17:37:15
we don't. The mirror layer is owned by |this| (Rec
| |
| 109 } | |
| 110 } | |
| 111 | |
| 112 // views::View: | |
| 113 gfx::Size GetPreferredSize() const override { | |
| 114 const int kMaxWidth = 800; | |
| 115 const int kMaxHeight = 600; | |
|
oshima
2016/07/15 18:28:04
This looked pretty big. Is this temporary, or I'm
Evan Stade
2016/07/15 18:44:08
the visual details like this one are temporary. I
| |
| 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 views::View* destroying_view = window_view_map_[destroying_window]; | |
|
oshima
2016/07/15 18:28:04
don't you have to (or want to) erase?
Evan Stade
2016/07/15 18:44:08
technically I don't think it's doing any harm by l
| |
| 220 destroying_view->parent()->RemoveChildView(destroying_view); | |
| 221 SetTargetWindow(new_target); | |
| 222 } | |
| 223 | |
| 224 // views::View overrides: | |
| 225 gfx::Size GetPreferredSize() const override { | |
| 226 return mirror_container_->GetPreferredSize(); | |
| 227 } | |
| 228 | |
| 229 void Layout() override { | |
| 230 views::View* target_view = window_view_map_[target_window_]; | |
| 231 gfx::RectF target_bounds(target_view->GetLocalBounds()); | |
| 232 views::View::ConvertRectToTarget(target_view, this, &target_bounds); | |
| 233 target_bounds.Inset(gfx::InsetsF(-15)); | |
| 234 selector_view_->SetBoundsRect(gfx::ToEnclosingRect(target_bounds)); | |
| 235 | |
| 236 mirror_container_->SetBoundsRect(GetLocalBounds()); | |
| 237 } | |
| 238 | |
| 239 WmWindow* target_window() { return target_window_; } | |
| 240 | |
| 241 private: | |
| 242 std::map<WmWindow*, WindowMirrorView*> window_view_map_; | |
| 243 views::View* mirror_container_; | |
| 244 views::View* selector_view_; | |
| 245 WmWindow* target_window_; | |
| 246 | |
| 247 DISALLOW_COPY_AND_ASSIGN(WindowCycleView); | |
| 248 }; | |
| 249 | |
| 58 ScopedShowWindow::ScopedShowWindow() | 250 ScopedShowWindow::ScopedShowWindow() |
| 59 : window_(nullptr), stack_window_above_(nullptr), minimized_(false) {} | 251 : window_(nullptr), stack_window_above_(nullptr), minimized_(false) {} |
| 60 | 252 |
| 61 ScopedShowWindow::~ScopedShowWindow() { | 253 ScopedShowWindow::~ScopedShowWindow() { |
| 62 if (window_) { | 254 if (window_) { |
| 63 window_->GetParent()->RemoveObserver(this); | 255 window_->GetParent()->RemoveObserver(this); |
| 64 | 256 |
| 65 // Restore window's stacking position. | 257 // Restore window's stacking position. |
| 66 if (stack_window_above_) | 258 if (stack_window_above_) |
| 67 window_->GetParent()->StackChildAbove(window_, stack_window_above_); | 259 window_->GetParent()->StackChildAbove(window_, stack_window_above_); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 if (params.target == window_) { | 292 if (params.target == window_) { |
| 101 CancelRestore(); | 293 CancelRestore(); |
| 102 } else if (params.target == stack_window_above_) { | 294 } else if (params.target == stack_window_above_) { |
| 103 // If the window this window was above is removed, use the next window down | 295 // If the window this window was above is removed, use the next window down |
| 104 // as the restore marker. | 296 // as the restore marker. |
| 105 stack_window_above_ = GetWindowBelow(stack_window_above_); | 297 stack_window_above_ = GetWindowBelow(stack_window_above_); |
| 106 } | 298 } |
| 107 } | 299 } |
| 108 | 300 |
| 109 WindowCycleList::WindowCycleList(const WindowList& windows) | 301 WindowCycleList::WindowCycleList(const WindowList& windows) |
| 110 : windows_(windows), current_index_(0) { | 302 : windows_(windows), current_index_(0), cycle_view_(nullptr) { |
| 111 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(true); | 303 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(true); |
| 112 | 304 |
| 113 for (WmWindow* window : windows_) | 305 for (WmWindow* window : windows_) |
| 114 window->AddObserver(this); | 306 window->AddObserver(this); |
| 307 | |
| 308 if (ShouldShowUi()) { | |
| 309 WmWindow* root_window = WmShell::Get()->GetPrimaryRootWindow(); | |
| 310 views::Widget* widget = new views::Widget; | |
| 311 views::Widget::InitParams params; | |
| 312 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; | |
| 313 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 314 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
| 315 params.accept_events = true; | |
| 316 root_window->GetRootWindowController() | |
| 317 ->ConfigureWidgetInitParamsForContainer( | |
| 318 widget, kShellWindowId_OverlayContainer, ¶ms); | |
|
oshima
2016/07/15 19:47:52
Overlay container is above lock screen and system
Evan Stade
2016/07/15 22:49:02
Done.
| |
| 319 widget->Init(params); | |
| 320 | |
| 321 cycle_view_ = new WindowCycleView(windows_); | |
| 322 | |
| 323 widget->SetContentsView(cycle_view_); | |
| 324 // TODO(estade): right now this just extends past the edge of the screen if | |
| 325 // there are too many windows. Handle this more gracefully. | |
| 326 gfx::Rect widget_rect = widget->GetWorkAreaBoundsInScreen(); | |
| 327 gfx::Size widget_size = cycle_view_->GetPreferredSize(); | |
| 328 widget_rect.ClampToCenteredSize(widget_size); | |
| 329 widget_rect.set_width(widget_size.width()); | |
| 330 widget->SetBounds(widget_rect); | |
|
oshima
2016/07/15 19:47:52
you should also handle the case when display metri
Evan Stade
2016/07/15 22:49:02
I don't know what would currently happen with mult
| |
| 331 widget->Show(); | |
| 332 cycle_ui_widget_.reset(widget); | |
| 333 } | |
| 115 } | 334 } |
| 116 | 335 |
| 117 WindowCycleList::~WindowCycleList() { | 336 WindowCycleList::~WindowCycleList() { |
| 118 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(false); | 337 WmShell::Get()->mru_window_tracker()->SetIgnoreActivations(false); |
| 119 for (WmWindow* window : windows_) { | 338 for (WmWindow* window : windows_) { |
| 120 // TODO(oshima): Remove this once crbug.com/483491 is fixed. | 339 // TODO(oshima): Remove this once crbug.com/483491 is fixed. |
| 121 CHECK(window); | 340 CHECK(window); |
| 122 window->RemoveObserver(this); | 341 window->RemoveObserver(this); |
| 123 } | 342 } |
| 124 if (showing_window_) | 343 if (showing_window_) |
| 125 showing_window_->CancelRestore(); | 344 showing_window_->CancelRestore(); |
| 345 | |
| 346 if (cycle_view_) { | |
| 347 cycle_view_->target_window()->Show(); | |
| 348 cycle_view_->target_window()->GetWindowState()->Activate(); | |
| 349 } | |
| 126 } | 350 } |
| 127 | 351 |
| 128 void WindowCycleList::Step(WindowCycleController::Direction direction) { | 352 void WindowCycleList::Step(WindowCycleController::Direction direction) { |
| 129 if (windows_.empty()) | 353 if (windows_.empty()) |
| 130 return; | 354 return; |
| 131 | 355 |
| 132 // When there is only one window, we should give feedback to the user. If the | 356 // When there is only one window, we should give feedback to the user. If the |
| 133 // window is minimized, we should also show it. | 357 // window is minimized, we should also show it. |
| 134 if (windows_.size() == 1) { | 358 if (windows_.size() == 1) { |
| 135 windows_[0]->Animate(::wm::WINDOW_ANIMATION_TYPE_BOUNCE); | 359 windows_[0]->Animate(::wm::WINDOW_ANIMATION_TYPE_BOUNCE); |
| 136 windows_[0]->Show(); | 360 windows_[0]->Show(); |
| 137 windows_[0]->GetWindowState()->Activate(); | 361 windows_[0]->GetWindowState()->Activate(); |
| 138 return; | 362 return; |
| 139 } | 363 } |
| 140 | 364 |
| 141 DCHECK(static_cast<size_t>(current_index_) < windows_.size()); | 365 DCHECK(static_cast<size_t>(current_index_) < windows_.size()); |
| 142 | 366 |
| 143 // We're in a valid cycle, so step forward or backward. | 367 // We're in a valid cycle, so step forward or backward. |
| 144 current_index_ += direction == WindowCycleController::FORWARD ? 1 : -1; | 368 current_index_ += direction == WindowCycleController::FORWARD ? 1 : -1; |
| 145 | 369 |
| 146 // Wrap to window list size. | 370 // Wrap to window list size. |
| 147 current_index_ = (current_index_ + windows_.size()) % windows_.size(); | 371 current_index_ = (current_index_ + windows_.size()) % windows_.size(); |
| 148 DCHECK(windows_[current_index_]); | 372 DCHECK(windows_[current_index_]); |
| 149 | 373 |
| 374 if (cycle_view_) { | |
| 375 cycle_view_->SetTargetWindow(windows_[current_index_]); | |
| 376 return; | |
| 377 } | |
| 378 | |
| 150 // Make sure the next window is visible. | 379 // Make sure the next window is visible. |
| 151 showing_window_.reset(new ScopedShowWindow); | 380 showing_window_.reset(new ScopedShowWindow); |
| 152 showing_window_->Show(windows_[current_index_]); | 381 showing_window_->Show(windows_[current_index_]); |
| 153 } | 382 } |
| 154 | 383 |
| 155 void WindowCycleList::OnWindowDestroying(WmWindow* window) { | 384 void WindowCycleList::OnWindowDestroying(WmWindow* window) { |
| 156 window->RemoveObserver(this); | 385 window->RemoveObserver(this); |
| 157 | 386 |
| 158 WindowList::iterator i = std::find(windows_.begin(), windows_.end(), window); | 387 WindowList::iterator i = std::find(windows_.begin(), windows_.end(), window); |
| 159 // TODO(oshima): Change this back to DCHECK once crbug.com/483491 is fixed. | 388 // TODO(oshima): Change this back to DCHECK once crbug.com/483491 is fixed. |
| 160 CHECK(i != windows_.end()); | 389 CHECK(i != windows_.end()); |
| 161 int removed_index = static_cast<int>(i - windows_.begin()); | 390 int removed_index = static_cast<int>(i - windows_.begin()); |
| 162 windows_.erase(i); | 391 windows_.erase(i); |
| 163 if (current_index_ > removed_index || | 392 if (current_index_ > removed_index || |
| 164 current_index_ == static_cast<int>(windows_.size())) { | 393 current_index_ == static_cast<int>(windows_.size())) { |
| 165 current_index_--; | 394 current_index_--; |
|
oshima
2016/07/15 18:28:04
what if the 1st window is destroyed?
Evan Stade
2016/07/15 18:44:08
I don't think that would be a problem? If the 1st
oshima
2016/07/15 19:47:52
Ah yes, that's what I was wondering. If current_in
Evan Stade
2016/07/15 22:49:02
I just thought this was a good stopping point beca
| |
| 166 } | 395 } |
| 396 | |
| 397 // TODO(estade): what if this is the last (or second from last) window? | |
| 398 if (cycle_view_) | |
| 399 cycle_view_->HandleWindowDestruction(window, windows_[current_index_]); | |
| 400 } | |
| 401 | |
| 402 bool WindowCycleList::ShouldShowUi() { | |
| 403 return windows_.size() > 1 && | |
| 404 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 405 switches::kAshEnableWindowCycleUi); | |
| 167 } | 406 } |
| 168 | 407 |
| 169 } // namespace ash | 408 } // namespace ash |
| OLD | NEW |