OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/wm/workspace/workspace_layout_manager_backdrop.h" |
| 6 |
| 7 #include "ash/wm/window_animations.h" |
| 8 #include "ui/aura/window.h" |
| 9 #include "ui/compositor/layer.h" |
| 10 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 11 #include "ui/views/background.h" |
| 12 #include "ui/views/corewm/window_util.h" |
| 13 #include "ui/views/widget/widget.h" |
| 14 |
| 15 namespace ash { |
| 16 |
| 17 namespace internal { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // The opacity of the backdrop. |
| 22 const float kBackdropOpacity = 0.5f; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 WorkspaceLayoutManagerBackdrop::WorkspaceLayoutManagerBackdrop( |
| 27 aura::Window* container) |
| 28 : background_(NULL), |
| 29 container_(container), |
| 30 inside_(false) { |
| 31 background_ = new views::Widget; |
| 32 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
| 33 params.parent = container_; |
| 34 params.bounds = container_->bounds(); |
| 35 // To disallow the MRU list from picking this window up it should not be |
| 36 // activateable. |
| 37 params.can_activate = false; |
| 38 background_->Init(params); |
| 39 background_->GetNativeView()->SetName("WorkspaceLayoutManagerBackdrop"); |
| 40 views::View* contents_view = new views::View(); |
| 41 contents_view->set_background( |
| 42 views::Background::CreateSolidBackground(SK_ColorBLACK)); |
| 43 contents_view->SetEnabled(true); |
| 44 background_->SetContentsView(contents_view); |
| 45 Show(); |
| 46 RestackOrHideWindow(); |
| 47 container_->AddObserver(this); |
| 48 } |
| 49 |
| 50 WorkspaceLayoutManagerBackdrop::~WorkspaceLayoutManagerBackdrop() { |
| 51 container_->RemoveObserver(this); |
| 52 ui::ScopedLayerAnimationSettings settings( |
| 53 background_->GetNativeView()->layer()->GetAnimator()); |
| 54 background_->Close(); |
| 55 settings.AddObserver(views::corewm::CreateHidingWindowAnimationObserver( |
| 56 background_->GetNativeView())); |
| 57 background_->GetNativeView()->layer()->SetOpacity(0.0f); |
| 58 } |
| 59 |
| 60 void WorkspaceLayoutManagerBackdrop::RestackOrHideWindow() { |
| 61 // Avoid recursive calls. |
| 62 if (inside_) |
| 63 return; |
| 64 // We are stuffing the backdrop behind the first visible & qualifying |
| 65 // window in the given container. |
| 66 const aura::Window::Windows& windows = container_->children(); |
| 67 for (aura::Window::Windows::const_reverse_iterator window_iter = |
| 68 windows.rbegin(); |
| 69 window_iter != windows.rend(); ++window_iter) { |
| 70 aura::Window* window = *window_iter; |
| 71 if (window->IsVisible() && |
| 72 window->type() == ui::wm::WINDOW_TYPE_NORMAL) { |
| 73 // Check if the window has already the backdrop directly behind it. |
| 74 if (++window_iter != windows.rend()) { |
| 75 if (*window_iter == background_->GetNativeWindow() && |
| 76 background_->IsVisible()) { |
| 77 return; |
| 78 } |
| 79 } |
| 80 // We are changing the order of windows which will cause recursion. |
| 81 inside_ = true; |
| 82 if (!background_->IsVisible()) |
| 83 Show(); |
| 84 // Since the backdrop needs to be immediately behind the window and the |
| 85 // stacking functions only guarantee a "it's above or below", we need |
| 86 // to re-arrange the two windows twice. |
| 87 container_->StackChildAbove(background_->GetNativeView(), window); |
| 88 container_->StackChildAbove(window, background_->GetNativeView()); |
| 89 inside_ = false; |
| 90 return; |
| 91 } |
| 92 } |
| 93 // Hide backdrop since no suitable window was found. |
| 94 if (background_->IsVisible()) |
| 95 background_->Hide(); |
| 96 } |
| 97 |
| 98 void WorkspaceLayoutManagerBackdrop::OnWindowBoundsChanged(aura::Window* window, |
| 99 const gfx::Rect& old_bounds, |
| 100 const gfx::Rect& new_bounds) OVERRIDE { |
| 101 // The container size has changed and the layer needs to be adapt to it. |
| 102 AdjustToContainerBounds(); |
| 103 } |
| 104 |
| 105 void WorkspaceLayoutManagerBackdrop::AdjustToContainerBounds() { |
| 106 // Cover the entire container window. |
| 107 gfx::Rect target_rect(gfx::Point(0, 0), container_->bounds().size()); |
| 108 if (target_rect != background_->GetNativeWindow()->bounds()) { |
| 109 // This needs to be instantly. |
| 110 ui::ScopedLayerAnimationSettings settings( |
| 111 background_->GetNativeView()->layer()->GetAnimator()); |
| 112 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(0)); |
| 113 background_->GetNativeWindow()->SetBounds(target_rect); |
| 114 if (!background_->IsVisible()) |
| 115 background_->GetNativeView()->layer()->SetOpacity(kBackdropOpacity); |
| 116 } |
| 117 } |
| 118 |
| 119 void WorkspaceLayoutManagerBackdrop::Show() { |
| 120 background_->GetNativeView()->layer()->SetOpacity(0.0f); |
| 121 background_->Show(); |
| 122 ui::ScopedLayerAnimationSettings settings( |
| 123 background_->GetNativeView()->layer()->GetAnimator()); |
| 124 background_->Show(); |
| 125 background_->GetNativeView()->layer()->SetOpacity(kBackdropOpacity); |
| 126 } |
| 127 |
| 128 } // namespace internal |
| 129 } // namespace ash |
OLD | NEW |