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.
5 #include "ash/wm/screen_dimmer.h"
9 #include "ui/aura/root_window.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"
20 // Opacity for |dimming_layer_| when it's dimming the screen.
21 const float kDimmingLayerOpacity
= 0.4f
;
23 // Duration for dimming animations, in milliseconds.
24 const int kDimmingTransitionMs
= 200;
28 ScreenDimmer::ScreenDimmer(aura::RootWindow
* root_window
)
29 : root_window_(root_window
),
30 currently_dimming_(false) {
31 root_window_
->AddRootWindowObserver(this);
34 ScreenDimmer::~ScreenDimmer() {
35 root_window_
->RemoveRootWindowObserver(this);
38 void ScreenDimmer::SetDimming(bool should_dim
) {
39 if (should_dim
== currently_dimming_
)
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());
52 currently_dimming_
= should_dim
;
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
);
61 void ScreenDimmer::OnRootWindowResized(const aura::RootWindow
* root
,
62 const gfx::Size
& old_size
) {
64 dimming_layer_
->SetBounds(gfx::Rect(root
->bounds().size()));
67 } // namespace internal