workspace_backdrop_delegate.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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_->GetBoundsInScreen();
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.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
39  background_->Init(params);
40  // Do not use the animation system. We don't want the bounds animation and
41  // opacity needs to get set to |kBackdropOpacity|.
42  ::wm::SetWindowVisibilityAnimationTransition(
43      background_->GetNativeView(),
44      ::wm::ANIMATE_NONE);
45  background_->GetNativeView()->SetName("WorkspaceBackdropDelegate");
46  background_->GetNativeView()->layer()->SetColor(SK_ColorBLACK);
47  // Make sure that the layer covers visibly everything - including the shelf.
48  background_->GetNativeView()->layer()->SetBounds(params.bounds);
49  Show();
50  RestackBackdrop();
51  container_->AddObserver(this);
52}
53
54WorkspaceBackdropDelegate::~WorkspaceBackdropDelegate() {
55  container_->RemoveObserver(this);
56  ::wm::ScopedHidingAnimationSettings hiding_settings(
57      background_->GetNativeView());
58  background_->Close();
59  background_->GetNativeView()->layer()->SetOpacity(0.0f);
60}
61
62void WorkspaceBackdropDelegate::OnWindowBoundsChanged(
63    aura::Window* window,
64    const gfx::Rect& old_bounds,
65    const gfx::Rect& new_bounds) {
66  // The container size has changed and the layer needs to be adapt to it.
67  AdjustToContainerBounds();
68}
69
70void WorkspaceBackdropDelegate::OnWindowAddedToLayout(aura::Window* child) {
71  RestackBackdrop();
72}
73
74void WorkspaceBackdropDelegate::OnWindowRemovedFromLayout(aura::Window* child) {
75  RestackBackdrop();
76}
77
78void WorkspaceBackdropDelegate::OnChildWindowVisibilityChanged(
79    aura::Window* child,
80    bool visible) {
81  RestackBackdrop();
82}
83
84void WorkspaceBackdropDelegate::OnWindowStackingChanged(aura::Window* window) {
85  RestackBackdrop();
86}
87
88void WorkspaceBackdropDelegate::OnPostWindowStateTypeChange(
89    wm::WindowState* window_state,
90    wm::WindowStateType old_type) {
91  RestackBackdrop();
92}
93
94void WorkspaceBackdropDelegate::OnDisplayWorkAreaInsetsChanged() {
95  AdjustToContainerBounds();
96}
97
98void WorkspaceBackdropDelegate::RestackBackdrop() {
99  // Avoid recursive calls.
100  if (in_restacking_)
101    return;
102
103  aura::Window* window = GetCurrentTopWindow();
104  if (!window) {
105    // Hide backdrop since no suitable window was found.
106    background_->Hide();
107    return;
108  }
109  if (window == background_->GetNativeWindow() &&
110      background_->IsVisible()) {
111    return;
112  }
113  // We are changing the order of windows which will cause recursion.
114  base::AutoReset<bool> lock(&in_restacking_, true);
115  if (!background_->IsVisible())
116    Show();
117  // Since the backdrop needs to be immediately behind the window and the
118  // stacking functions only guarantee a "it's above or below", we need
119  // to re-arrange the two windows twice.
120  container_->StackChildAbove(background_->GetNativeView(), window);
121  container_->StackChildAbove(window, background_->GetNativeView());
122}
123
124aura::Window* WorkspaceBackdropDelegate::GetCurrentTopWindow() {
125  const aura::Window::Windows& windows = container_->children();
126  for (aura::Window::Windows::const_reverse_iterator window_iter =
127           windows.rbegin();
128       window_iter != windows.rend(); ++window_iter) {
129    aura::Window* window = *window_iter;
130    if (window->TargetVisibility() &&
131        window->type() == ui::wm::WINDOW_TYPE_NORMAL &&
132        ash::wm::CanActivateWindow(window))
133      return window;
134  }
135  return NULL;
136}
137
138void WorkspaceBackdropDelegate::AdjustToContainerBounds() {
139  // Cover the entire container window.
140  gfx::Rect target_rect(gfx::Point(0, 0), container_->bounds().size());
141  if (target_rect != background_->GetNativeWindow()->bounds()) {
142    // This needs to be instant.
143    ui::ScopedLayerAnimationSettings settings(
144        background_->GetNativeView()->layer()->GetAnimator());
145    settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(0));
146    background_->GetNativeWindow()->SetBounds(target_rect);
147    if (!background_->IsVisible())
148      background_->GetNativeView()->layer()->SetOpacity(kBackdropOpacity);
149  }
150}
151
152void WorkspaceBackdropDelegate::Show() {
153  background_->GetNativeView()->layer()->SetOpacity(0.0f);
154  background_->Show();
155  ui::ScopedLayerAnimationSettings settings(
156      background_->GetNativeView()->layer()->GetAnimator());
157  background_->GetNativeView()->layer()->SetOpacity(kBackdropOpacity);
158}
159
160}  // namespace ash
161