summaryrefslogtreecommitdiffstats
path: root/ash/wm/screen_dimmer.cc
blob: 90edc696716966d61cc7e77e978b88681e03339c (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
// Copyright (c) 2012 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/screen_dimmer.h"

#include "ash/shell.h"
#include "base/time/time.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"

namespace ash {
namespace {

// Opacity for |dimming_layer_| when it's dimming the screen.
const float kDimmingLayerOpacity = 0.4f;

// Duration for dimming animations, in milliseconds.
const int kDimmingTransitionMs = 200;

}  // namespace

ScreenDimmer::ScreenDimmer(aura::Window* root_window)
    : root_window_(root_window),
      currently_dimming_(false) {
  root_window_->AddObserver(this);
}

ScreenDimmer::~ScreenDimmer() {
  root_window_->RemoveObserver(this);
}

void ScreenDimmer::SetDimming(bool should_dim) {
  if (should_dim == currently_dimming_)
    return;

  if (!dimming_layer_) {
    dimming_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR));
    dimming_layer_->SetColor(SK_ColorBLACK);
    dimming_layer_->SetOpacity(0.0f);
    ui::Layer* root_layer = root_window_->layer();
    dimming_layer_->SetBounds(root_layer->bounds());
    root_layer->Add(dimming_layer_.get());
    root_layer->StackAtTop(dimming_layer_.get());
  }

  currently_dimming_ = should_dim;

  ui::ScopedLayerAnimationSettings scoped_settings(
      dimming_layer_->GetAnimator());
  scoped_settings.SetTransitionDuration(
      base::TimeDelta::FromMilliseconds(kDimmingTransitionMs));
  dimming_layer_->SetOpacity(should_dim ? kDimmingLayerOpacity : 0.0f);
}

void ScreenDimmer::OnWindowBoundsChanged(aura::Window* root,
                                         const gfx::Rect& old_bounds,
                                         const gfx::Rect& new_bounds) {
  if (dimming_layer_)
    dimming_layer_->SetBounds(gfx::Rect(root->bounds().size()));
}

}  // namespace ash