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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
// 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 "ui/wm/core/shadow.h"
#include "grit/ui_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
namespace {
// Shadow opacity for different styles.
const float kActiveShadowOpacity = 1.0f;
const float kInactiveShadowOpacity = 0.2f;
const float kSmallShadowOpacity = 1.0f;
// Shadow aperture for different styles.
// Note that this may be greater than interior inset to allow shadows with
// curved corners that extend inwards beyond a window's borders.
const int kActiveInteriorAperture = 134;
const int kInactiveInteriorAperture = 134;
const int kSmallInteriorAperture = 9;
// Interior inset for different styles.
const int kActiveInteriorInset = 64;
const int kInactiveInteriorInset = 64;
const int kSmallInteriorInset = 4;
// Duration for opacity animation in milliseconds.
const int kShadowAnimationDurationMs = 100;
float GetOpacityForStyle(wm::Shadow::Style style) {
switch (style) {
case wm::Shadow::STYLE_ACTIVE:
return kActiveShadowOpacity;
case wm::Shadow::STYLE_INACTIVE:
return kInactiveShadowOpacity;
case wm::Shadow::STYLE_SMALL:
return kSmallShadowOpacity;
}
return 1.0f;
}
int GetShadowApertureForStyle(wm::Shadow::Style style) {
switch (style) {
case wm::Shadow::STYLE_ACTIVE:
return kActiveInteriorAperture;
case wm::Shadow::STYLE_INACTIVE:
return kInactiveInteriorAperture;
case wm::Shadow::STYLE_SMALL:
return kSmallInteriorAperture;
}
return 0;
}
int GetInteriorInsetForStyle(wm::Shadow::Style style) {
switch (style) {
case wm::Shadow::STYLE_ACTIVE:
return kActiveInteriorInset;
case wm::Shadow::STYLE_INACTIVE:
return kInactiveInteriorInset;
case wm::Shadow::STYLE_SMALL:
return kSmallInteriorInset;
}
return 0;
}
} // namespace
namespace wm {
Shadow::Shadow() : style_(STYLE_ACTIVE), interior_inset_(0) {
}
Shadow::~Shadow() {
}
void Shadow::Init(Style style) {
style_ = style;
layer_.reset(new ui::Layer(ui::LAYER_NOT_DRAWN));
shadow_layer_.reset(new ui::Layer(ui::LAYER_NINE_PATCH));
layer()->Add(shadow_layer_.get());
UpdateImagesForStyle();
shadow_layer_->set_name("Shadow");
shadow_layer_->SetVisible(true);
shadow_layer_->SetFillsBoundsOpaquely(false);
shadow_layer_->SetOpacity(GetOpacityForStyle(style_));
}
void Shadow::SetContentBounds(const gfx::Rect& content_bounds) {
content_bounds_ = content_bounds;
UpdateLayerBounds();
}
void Shadow::SetStyle(Style style) {
if (style_ == style)
return;
Style old_style = style_;
style_ = style;
// Stop waiting for any as yet unfinished implicit animations.
StopObservingImplicitAnimations();
// If we're switching to or from the small style, don't bother with
// animations.
if (style == STYLE_SMALL || old_style == STYLE_SMALL) {
UpdateImagesForStyle();
shadow_layer_->SetOpacity(GetOpacityForStyle(style));
return;
}
// If we're becoming active, switch images now. Because the inactive image
// has a very low opacity the switch isn't noticeable and this approach
// allows us to use only a single set of shadow images at a time.
if (style == STYLE_ACTIVE) {
UpdateImagesForStyle();
// Opacity was baked into inactive image, start opacity low to match.
shadow_layer_->SetOpacity(kInactiveShadowOpacity);
}
{
// Property sets within this scope will be implicitly animated.
ui::ScopedLayerAnimationSettings settings(shadow_layer_->GetAnimator());
settings.AddObserver(this);
settings.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kShadowAnimationDurationMs));
switch (style_) {
case STYLE_ACTIVE:
shadow_layer_->SetOpacity(kActiveShadowOpacity);
break;
case STYLE_INACTIVE:
shadow_layer_->SetOpacity(kInactiveShadowOpacity);
break;
default:
NOTREACHED() << "Unhandled style " << style_;
break;
}
}
}
void Shadow::OnImplicitAnimationsCompleted() {
// If we just finished going inactive, switch images. This doesn't cause
// a visual pop because the inactive image opacity is so low.
if (style_ == STYLE_INACTIVE) {
UpdateImagesForStyle();
// Opacity is baked into inactive image, so set fully opaque.
shadow_layer_->SetOpacity(1.0f);
}
}
void Shadow::UpdateImagesForStyle() {
ResourceBundle& res = ResourceBundle::GetSharedInstance();
gfx::Image image;
switch (style_) {
case STYLE_ACTIVE:
image = res.GetImageNamed(IDR_AURA_SHADOW_ACTIVE);
break;
case STYLE_INACTIVE:
image = res.GetImageNamed(IDR_AURA_SHADOW_INACTIVE);
break;
case STYLE_SMALL:
image = res.GetImageNamed(IDR_WINDOW_BUBBLE_SHADOW_SMALL);
break;
default:
NOTREACHED() << "Unhandled style " << style_;
break;
}
// Calculate shadow aperture for style.
int shadow_aperture = GetShadowApertureForStyle(style_);
gfx::Rect aperture(shadow_aperture,
shadow_aperture,
image.Width() - shadow_aperture * 2,
image.Height() - shadow_aperture * 2);
// Update nine-patch layer with new bitmap and aperture.
shadow_layer_->UpdateNinePatchLayerBitmap(image.AsBitmap(), aperture);
// Update interior inset for style.
interior_inset_ = GetInteriorInsetForStyle(style_);
// Image sizes may have changed.
UpdateLayerBounds();
}
void Shadow::UpdateLayerBounds() {
// Update bounds based on content bounds and interior inset.
gfx::Rect layer_bounds = content_bounds_;
layer_bounds.Inset(-interior_inset_, -interior_inset_);
layer()->SetBounds(layer_bounds);
shadow_layer_->SetBounds(gfx::Rect(layer_bounds.size()));
// Calculate shadow border for style. Note that border is in layer space
// and it cannot exceed the bounds of the layer.
int shadow_aperture = GetShadowApertureForStyle(style_);
gfx::Rect border(shadow_aperture, shadow_aperture,
shadow_aperture * 2, shadow_aperture * 2);
if (layer_bounds.width() < border.width() ||
layer_bounds.height() < border.height()) {
shadow_layer_->SetVisible(false);
} else {
shadow_layer_->SetVisible(true);
shadow_layer_->UpdateNinePatchLayerBorder(border);
}
}
} // namespace wm
|