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 "ui/wm/core/shadow.h"
7 #include "grit/ui_resources.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/compositor/layer.h"
11 #include "ui/compositor/scoped_layer_animation_settings.h"
15 // Shadow opacity for different styles.
16 const float kActiveShadowOpacity = 1.0f;
17 const float kInactiveShadowOpacity = 0.2f;
18 const float kSmallShadowOpacity = 1.0f;
20 // Shadow aperture for different styles.
21 // Note that this may be greater than interior inset to allow shadows with
22 // curved corners that extend inwards beyond a window's borders.
23 const int kActiveInteriorAperture = 134;
24 const int kInactiveInteriorAperture = 134;
25 const int kSmallInteriorAperture = 9;
27 // Interior inset for different styles.
28 const int kActiveInteriorInset = 64;
29 const int kInactiveInteriorInset = 64;
30 const int kSmallInteriorInset = 4;
32 // Duration for opacity animation in milliseconds.
33 const int kShadowAnimationDurationMs = 100;
35 float GetOpacityForStyle(wm::Shadow::Style style) {
37 case wm::Shadow::STYLE_ACTIVE:
38 return kActiveShadowOpacity;
39 case wm::Shadow::STYLE_INACTIVE:
40 return kInactiveShadowOpacity;
41 case wm::Shadow::STYLE_SMALL:
42 return kSmallShadowOpacity;
47 int GetShadowApertureForStyle(wm::Shadow::Style style) {
49 case wm::Shadow::STYLE_ACTIVE:
50 return kActiveInteriorAperture;
51 case wm::Shadow::STYLE_INACTIVE:
52 return kInactiveInteriorAperture;
53 case wm::Shadow::STYLE_SMALL:
54 return kSmallInteriorAperture;
59 int GetInteriorInsetForStyle(wm::Shadow::Style style) {
61 case wm::Shadow::STYLE_ACTIVE:
62 return kActiveInteriorInset;
63 case wm::Shadow::STYLE_INACTIVE:
64 return kInactiveInteriorInset;
65 case wm::Shadow::STYLE_SMALL:
66 return kSmallInteriorInset;
75 Shadow::Shadow() : style_(STYLE_ACTIVE), interior_inset_(0) {
81 void Shadow::Init(Style style) {
84 layer_.reset(new ui::Layer(ui::LAYER_NOT_DRAWN));
85 shadow_layer_.reset(new ui::Layer(ui::LAYER_NINE_PATCH));
86 layer()->Add(shadow_layer_.get());
88 UpdateImagesForStyle();
89 shadow_layer_->set_name("Shadow");
90 shadow_layer_->SetVisible(true);
91 shadow_layer_->SetFillsBoundsOpaquely(false);
92 shadow_layer_->SetOpacity(GetOpacityForStyle(style_));
95 void Shadow::SetContentBounds(const gfx::Rect& content_bounds) {
96 content_bounds_ = content_bounds;
100 void Shadow::SetStyle(Style style) {
104 Style old_style = style_;
107 // Stop waiting for any as yet unfinished implicit animations.
108 StopObservingImplicitAnimations();
110 // If we're switching to or from the small style, don't bother with
112 if (style == STYLE_SMALL || old_style == STYLE_SMALL) {
113 UpdateImagesForStyle();
114 shadow_layer_->SetOpacity(GetOpacityForStyle(style));
118 // If we're becoming active, switch images now. Because the inactive image
119 // has a very low opacity the switch isn't noticeable and this approach
120 // allows us to use only a single set of shadow images at a time.
121 if (style == STYLE_ACTIVE) {
122 UpdateImagesForStyle();
123 // Opacity was baked into inactive image, start opacity low to match.
124 shadow_layer_->SetOpacity(kInactiveShadowOpacity);
128 // Property sets within this scope will be implicitly animated.
129 ui::ScopedLayerAnimationSettings settings(shadow_layer_->GetAnimator());
130 settings.AddObserver(this);
131 settings.SetTransitionDuration(
132 base::TimeDelta::FromMilliseconds(kShadowAnimationDurationMs));
135 shadow_layer_->SetOpacity(kActiveShadowOpacity);
138 shadow_layer_->SetOpacity(kInactiveShadowOpacity);
141 NOTREACHED() << "Unhandled style " << style_;
147 void Shadow::OnImplicitAnimationsCompleted() {
148 // If we just finished going inactive, switch images. This doesn't cause
149 // a visual pop because the inactive image opacity is so low.
150 if (style_ == STYLE_INACTIVE) {
151 UpdateImagesForStyle();
152 // Opacity is baked into inactive image, so set fully opaque.
153 shadow_layer_->SetOpacity(1.0f);
157 void Shadow::UpdateImagesForStyle() {
158 ResourceBundle& res = ResourceBundle::GetSharedInstance();
162 image = res.GetImageNamed(IDR_AURA_SHADOW_ACTIVE);
165 image = res.GetImageNamed(IDR_AURA_SHADOW_INACTIVE);
168 image = res.GetImageNamed(IDR_WINDOW_BUBBLE_SHADOW_SMALL);
171 NOTREACHED() << "Unhandled style " << style_;
175 // Calculate shadow aperture for style.
176 int shadow_aperture = GetShadowApertureForStyle(style_);
177 gfx::Rect aperture(shadow_aperture,
179 image.Width() - shadow_aperture * 2,
180 image.Height() - shadow_aperture * 2);
182 // Update nine-patch layer with new bitmap and aperture.
183 shadow_layer_->UpdateNinePatchLayerBitmap(image.AsBitmap(), aperture);
185 // Update interior inset for style.
186 interior_inset_ = GetInteriorInsetForStyle(style_);
188 // Image sizes may have changed.
192 void Shadow::UpdateLayerBounds() {
193 // Update bounds based on content bounds and interior inset.
194 gfx::Rect layer_bounds = content_bounds_;
195 layer_bounds.Inset(-interior_inset_, -interior_inset_);
196 layer()->SetBounds(layer_bounds);
197 shadow_layer_->SetBounds(gfx::Rect(layer_bounds.size()));
199 // Calculate shadow border for style. Note that border is in layer space
200 // and it cannot exceed the bounds of the layer.
201 int shadow_aperture = GetShadowApertureForStyle(style_);
202 gfx::Rect border(shadow_aperture, shadow_aperture,
203 shadow_aperture * 2, shadow_aperture * 2);
204 if (layer_bounds.width() < border.width() ||
205 layer_bounds.height() < border.height()) {
206 shadow_layer_->SetVisible(false);
208 shadow_layer_->SetVisible(true);
209 shadow_layer_->UpdateNinePatchLayerBorder(border);