4b4335e2ae1486e6f5be4603c2004c444fd83147
[platform/framework/web/crosswalk.git] / src / ash / wm / screen_dimmer.cc
1 // Copyright (c) 2012 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/screen_dimmer.h"
6
7 #include "ash/shell.h"
8 #include "base/time/time.h"
9 #include "ui/aura/window_event_dispatcher.h"
10 #include "ui/compositor/layer.h"
11 #include "ui/compositor/scoped_layer_animation_settings.h"
12 #include "ui/gfx/rect.h"
13 #include "ui/gfx/size.h"
14
15 namespace ash {
16 namespace internal {
17
18 namespace {
19
20 // Opacity for |dimming_layer_| when it's dimming the screen.
21 const float kDimmingLayerOpacity = 0.4f;
22
23 // Duration for dimming animations, in milliseconds.
24 const int kDimmingTransitionMs = 200;
25
26 }  // namespace
27
28 ScreenDimmer::ScreenDimmer(aura::Window* root_window)
29     : root_window_(root_window),
30       currently_dimming_(false) {
31   root_window_->AddObserver(this);
32 }
33
34 ScreenDimmer::~ScreenDimmer() {
35   root_window_->RemoveObserver(this);
36 }
37
38 void ScreenDimmer::SetDimming(bool should_dim) {
39   if (should_dim == currently_dimming_)
40     return;
41
42   if (!dimming_layer_) {
43     dimming_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR));
44     dimming_layer_->SetColor(SK_ColorBLACK);
45     dimming_layer_->SetOpacity(0.0f);
46     ui::Layer* root_layer = root_window_->layer();
47     dimming_layer_->SetBounds(root_layer->bounds());
48     root_layer->Add(dimming_layer_.get());
49     root_layer->StackAtTop(dimming_layer_.get());
50   }
51
52   currently_dimming_ = should_dim;
53
54   ui::ScopedLayerAnimationSettings scoped_settings(
55       dimming_layer_->GetAnimator());
56   scoped_settings.SetTransitionDuration(
57       base::TimeDelta::FromMilliseconds(kDimmingTransitionMs));
58   dimming_layer_->SetOpacity(should_dim ? kDimmingLayerOpacity : 0.0f);
59 }
60
61 void ScreenDimmer::OnWindowBoundsChanged(aura::Window* root,
62                                          const gfx::Rect& old_bounds,
63                                          const gfx::Rect& new_bounds) {
64   if (dimming_layer_)
65     dimming_layer_->SetBounds(gfx::Rect(root->bounds().size()));
66 }
67
68 }  // namespace internal
69 }  // namespace ash