workspace_backdrop_delegate.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 3// found in the LICENSE file. 4 5#include "ash/wm/maximize_mode/workspace_backdrop_delegate.h" 6 7#include "ash/wm/window_animations.h" 8#include "ash/wm/window_util.h" 9#include "base/auto_reset.h" 10#include "ui/aura/window.h" 11#include "ui/compositor/layer.h" 12#include "ui/compositor/scoped_layer_animation_settings.h" 13#include "ui/views/background.h" 14#include "ui/views/widget/widget.h" 15#include "ui/wm/core/window_animations.h" 16#include "ui/wm/core/window_util.h" 17 18namespace ash { 19namespace { 20 21// The opacity of the backdrop. 22const float kBackdropOpacity = 0.5f; 23 24} // namespace 25 26WorkspaceBackdropDelegate::WorkspaceBackdropDelegate(aura::Window* container) 27 : background_(NULL), 28 container_(container), 29 in_restacking_(false) { 30 background_ = new views::Widget; 31 views::Widget::InitParams params( 32 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); 33 params.parent = container_; 34 params.bounds = container_->bounds(); 35 params.layer_type = aura::WINDOW_LAYER_SOLID_COLOR; 36 // To disallow the MRU list from picking this window up it should not be 37 // activateable. 38 params.can_activate = false; 39 background_->Init(params); 40 background_->GetNativeView()->SetName("WorkspaceBackdropDelegate"); 41 background_->GetNativeView()->layer()->SetColor(SK_ColorBLACK); 42 Show(); 43 RestackBackdrop(); 44 container_->AddObserver(this); 45} 46 47WorkspaceBackdropDelegate::~WorkspaceBackdropDelegate() { 48 container_->RemoveObserver(this); 49 ::wm::ScopedHidingAnimationSettings hiding_settings( 50 background_->GetNativeView()); 51 background_->Close(); 52 background_->GetNativeView()->layer()->SetOpacity(0.0f); 53} 54 55void WorkspaceBackdropDelegate::OnWindowBoundsChanged( 56 aura::Window* window, 57 const gfx::Rect& old_bounds, 58 const gfx::Rect& new_bounds) { 59 // The container size has changed and the layer needs to be adapt to it. 60 AdjustToContainerBounds(); 61} 62 63void WorkspaceBackdropDelegate::OnWindowAddedToLayout(aura::Window* child) { 64 RestackBackdrop(); 65} 66 67void WorkspaceBackdropDelegate::OnWindowRemovedFromLayout(aura::Window* child) { 68 RestackBackdrop(); 69} 70 71void WorkspaceBackdropDelegate::OnChildWindowVisibilityChanged( 72 aura::Window* child, 73 bool visible) { 74 RestackBackdrop(); 75} 76 77void WorkspaceBackdropDelegate::OnWindowStackingChanged(aura::Window* window) { 78 RestackBackdrop(); 79} 80 81void WorkspaceBackdropDelegate::OnPostWindowStateTypeChange( 82 wm::WindowState* window_state, 83 wm::WindowStateType old_type) { 84 RestackBackdrop(); 85} 86 87void WorkspaceBackdropDelegate::RestackBackdrop() { 88 // Avoid recursive calls. 89 if (in_restacking_) 90 return; 91 92 aura::Window* window = GetCurrentTopWindow(); 93 if (!window) { 94 // Hide backdrop since no suitable window was found. 95 background_->Hide(); 96 return; 97 } 98 if (window == background_->GetNativeWindow() && 99 background_->IsVisible()) { 100 return; 101 } 102 // We are changing the order of windows which will cause recursion. 103 base::AutoReset<bool> lock(&in_restacking_, true); 104 if (!background_->IsVisible()) 105 Show(); 106 // Since the backdrop needs to be immediately behind the window and the 107 // stacking functions only guarantee a "it's above or below", we need 108 // to re-arrange the two windows twice. 109 container_->StackChildAbove(background_->GetNativeView(), window); 110 container_->StackChildAbove(window, background_->GetNativeView()); 111} 112 113aura::Window* WorkspaceBackdropDelegate::GetCurrentTopWindow() { 114 const aura::Window::Windows& windows = container_->children(); 115 for (aura::Window::Windows::const_reverse_iterator window_iter = 116 windows.rbegin(); 117 window_iter != windows.rend(); ++window_iter) { 118 aura::Window* window = *window_iter; 119 if (window->TargetVisibility() && 120 window->type() == ui::wm::WINDOW_TYPE_NORMAL && 121 ash::wm::CanActivateWindow(window)) 122 return window; 123 } 124 return NULL; 125} 126 127void WorkspaceBackdropDelegate::AdjustToContainerBounds() { 128 // Cover the entire container window. 129 gfx::Rect target_rect(gfx::Point(0, 0), container_->bounds().size()); 130 if (target_rect != background_->GetNativeWindow()->bounds()) { 131 // This needs to be instant. 132 ui::ScopedLayerAnimationSettings settings( 133 background_->GetNativeView()->layer()->GetAnimator()); 134 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(0)); 135 background_->GetNativeWindow()->SetBounds(target_rect); 136 if (!background_->IsVisible()) 137 background_->GetNativeView()->layer()->SetOpacity(kBackdropOpacity); 138 } 139} 140 141void WorkspaceBackdropDelegate::Show() { 142 background_->GetNativeView()->layer()->SetOpacity(0.0f); 143 background_->Show(); 144 ui::ScopedLayerAnimationSettings settings( 145 background_->GetNativeView()->layer()->GetAnimator()); 146 background_->GetNativeView()->layer()->SetOpacity(kBackdropOpacity); 147} 148 149} // namespace ash 150