summaryrefslogtreecommitdiffstats
path: root/ash/wm/dim_window.cc
blob: 552789f54321b4322931d7db6c4700dd027c457d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wm/dim_window.h"
#include "base/time/time.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window_property.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/wm/core/visibility_controller.h"
#include "ui/wm/core/window_animations.h"

DECLARE_WINDOW_PROPERTY_TYPE(ash::DimWindow*);

namespace ash {
namespace {

DEFINE_LOCAL_WINDOW_PROPERTY_KEY(DimWindow*, kDimWindowKey, nullptr);

const int kDefaultDimAnimationDurationMs = 200;

const float kDefaultDimOpacity = 0.5f;

}  // namespace

// static
DimWindow* DimWindow::Get(aura::Window* container) {
  return container->GetProperty(kDimWindowKey);
}

DimWindow::DimWindow(aura::Window* parent)
    : aura::Window(nullptr), parent_(parent) {
  SetType(ui::wm::WINDOW_TYPE_NORMAL);
  Init(ui::LAYER_SOLID_COLOR);
  wm::SetWindowVisibilityChangesAnimated(this);
  wm::SetWindowVisibilityAnimationType(
      this, wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
  wm::SetWindowVisibilityAnimationDuration(
      this, base::TimeDelta::FromMilliseconds(kDefaultDimAnimationDurationMs));

  SetDimOpacity(kDefaultDimOpacity);

  parent->AddChild(this);
  parent->AddObserver(this);
  parent->SetProperty(kDimWindowKey, this);
  parent->StackChildAtTop(this);

  SetBounds(parent->bounds());
}

DimWindow::~DimWindow() {
  if (parent_) {
    parent_->ClearProperty(kDimWindowKey);
    parent_->RemoveObserver(this);
    parent_ = nullptr;
  }
}

void DimWindow::SetDimOpacity(float target_opacity) {
  layer()->SetColor(SkColorSetA(SK_ColorBLACK, 255 * target_opacity));
}

void DimWindow::OnWindowBoundsChanged(aura::Window* window,
                                      const gfx::Rect& old_bounds,
                                      const gfx::Rect& new_bounds) {
  if (window == parent_)
    SetBounds(new_bounds);
}

void DimWindow::OnWindowDestroying(Window* window) {
  if (window == parent_) {
    window->ClearProperty(kDimWindowKey);
    window->RemoveObserver(this);
    parent_ = nullptr;
  }
}

}  // namespace ash