Roll src/third_party/skia d32087a:1052f51
[chromium-blink-merge.git] / ui / wm / core / shadow.cc
bloba5f736ae08057ec362c72b4cc0c7f4d13d1be1f7
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 "third_party/skia/include/core/SkBitmap.h"
8 #include "ui/base/resource/resource_bundle.h"
9 #include "ui/compositor/layer.h"
10 #include "ui/compositor/scoped_layer_animation_settings.h"
11 #include "ui/resources/grit/ui_resources.h"
13 namespace {
15 // The opacity used for active shadow when animating between
16 // inactive/active shadow.
17 const float kInactiveShadowAnimationOpacity = 0.2f;
19 // Shadow aperture for different styles.
20 // Note that this may be greater than interior inset to allow shadows with
21 // curved corners that extend inwards beyond a window's borders.
22 const int kActiveInteriorAperture = 134;
23 const int kInactiveInteriorAperture = 134;
24 const int kSmallInteriorAperture = 9;
26 // Interior inset for different styles.
27 const int kActiveInteriorInset = 64;
28 const int kInactiveInteriorInset = 64;
29 const int kSmallInteriorInset = 4;
31 // Duration for opacity animation in milliseconds.
32 const int kShadowAnimationDurationMs = 100;
34 int GetShadowApertureForStyle(wm::Shadow::Style style) {
35 switch (style) {
36 case wm::Shadow::STYLE_ACTIVE:
37 return kActiveInteriorAperture;
38 case wm::Shadow::STYLE_INACTIVE:
39 return kInactiveInteriorAperture;
40 case wm::Shadow::STYLE_SMALL:
41 return kSmallInteriorAperture;
43 return 0;
46 int GetInteriorInsetForStyle(wm::Shadow::Style style) {
47 switch (style) {
48 case wm::Shadow::STYLE_ACTIVE:
49 return kActiveInteriorInset;
50 case wm::Shadow::STYLE_INACTIVE:
51 return kInactiveInteriorInset;
52 case wm::Shadow::STYLE_SMALL:
53 return kSmallInteriorInset;
55 return 0;
58 } // namespace
60 namespace wm {
62 Shadow::Shadow() : style_(STYLE_ACTIVE), interior_inset_(0) {
65 Shadow::~Shadow() {
68 void Shadow::Init(Style style) {
69 style_ = style;
71 layer_.reset(new ui::Layer(ui::LAYER_NOT_DRAWN));
72 shadow_layer_.reset(new ui::Layer(ui::LAYER_NINE_PATCH));
73 layer()->Add(shadow_layer_.get());
75 UpdateImagesForStyle();
76 shadow_layer_->set_name("Shadow");
77 shadow_layer_->SetVisible(true);
78 shadow_layer_->SetFillsBoundsOpaquely(false);
81 void Shadow::SetContentBounds(const gfx::Rect& content_bounds) {
82 content_bounds_ = content_bounds;
83 UpdateLayerBounds();
86 void Shadow::SetStyle(Style style) {
87 if (style_ == style)
88 return;
90 Style old_style = style_;
91 style_ = style;
93 // Stop waiting for any as yet unfinished implicit animations.
94 StopObservingImplicitAnimations();
96 // If we're switching to or from the small style, don't bother with
97 // animations.
98 if (style == STYLE_SMALL || old_style == STYLE_SMALL) {
99 UpdateImagesForStyle();
100 // Make sure the shadow is fully opaque.
101 shadow_layer_->SetOpacity(1.0f);
102 return;
105 // If we're becoming active, switch images now. Because the inactive image
106 // has a very low opacity the switch isn't noticeable and this approach
107 // allows us to use only a single set of shadow images at a time.
108 if (style == STYLE_ACTIVE) {
109 UpdateImagesForStyle();
110 // Opacity was baked into inactive image, start opacity low to match.
111 shadow_layer_->SetOpacity(kInactiveShadowAnimationOpacity);
115 // Property sets within this scope will be implicitly animated.
116 ui::ScopedLayerAnimationSettings settings(shadow_layer_->GetAnimator());
117 settings.AddObserver(this);
118 settings.SetTransitionDuration(
119 base::TimeDelta::FromMilliseconds(kShadowAnimationDurationMs));
120 switch (style_) {
121 case STYLE_ACTIVE:
122 // Animate the active shadow from kInactiveShadowAnimationOpacity to
123 // 1.0f.
124 shadow_layer_->SetOpacity(1.0f);
125 break;
126 case STYLE_INACTIVE:
127 // The opacity will be reset to 1.0f when animation is completed.
128 shadow_layer_->SetOpacity(kInactiveShadowAnimationOpacity);
129 break;
130 default:
131 NOTREACHED() << "Unhandled style " << style_;
132 break;
137 void Shadow::OnImplicitAnimationsCompleted() {
138 // If we just finished going inactive, switch images. This doesn't cause
139 // a visual pop because the inactive image opacity is so low.
140 if (style_ == STYLE_INACTIVE) {
141 UpdateImagesForStyle();
142 // Opacity is baked into inactive image, so set fully opaque.
143 shadow_layer_->SetOpacity(1.0f);
147 void Shadow::UpdateImagesForStyle() {
148 ResourceBundle& res = ResourceBundle::GetSharedInstance();
149 gfx::Image image;
150 switch (style_) {
151 case STYLE_ACTIVE:
152 image = res.GetImageNamed(IDR_AURA_SHADOW_ACTIVE);
153 break;
154 case STYLE_INACTIVE:
155 image = res.GetImageNamed(IDR_AURA_SHADOW_INACTIVE);
156 break;
157 case STYLE_SMALL:
158 image = res.GetImageNamed(IDR_WINDOW_BUBBLE_SHADOW_SMALL);
159 break;
160 default:
161 NOTREACHED() << "Unhandled style " << style_;
162 break;
165 shadow_layer_->UpdateNinePatchLayerImage(image.AsImageSkia());
166 image_size_ = image.Size();
167 interior_inset_ = GetInteriorInsetForStyle(style_);
169 // Image sizes may have changed.
170 UpdateLayerBounds();
173 void Shadow::UpdateLayerBounds() {
174 // Update bounds based on content bounds and interior inset.
175 gfx::Rect layer_bounds = content_bounds_;
176 layer_bounds.Inset(-interior_inset_, -interior_inset_);
177 layer()->SetBounds(layer_bounds);
178 shadow_layer_->SetBounds(gfx::Rect(layer_bounds.size()));
180 // Update the shadow aperture and border for style. Note that border is in
181 // layer space and it cannot exceed the bounds of the layer.
182 int aperture = GetShadowApertureForStyle(style_);
183 int aperture_x = std::min(aperture, layer_bounds.width() / 2);
184 int aperture_y = std::min(aperture, layer_bounds.height() / 2);
185 shadow_layer_->UpdateNinePatchLayerAperture(
186 gfx::Rect(aperture_x, aperture_y,
187 image_size_.width() - aperture_x * 2,
188 image_size_.height() - aperture_y * 2));
189 shadow_layer_->UpdateNinePatchLayerBorder(
190 gfx::Rect(aperture_x, aperture_y, aperture_x * 2, aperture_y * 2));
193 } // namespace wm