| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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/mus/shadow.h" | |
| 6 | |
| 7 #include "third_party/skia/include/core/SkBitmap.h" | |
| 8 #include "ui/aura/window.h" | |
| 9 #include "ui/base/class_property.h" | |
| 10 #include "ui/base/resource/resource_bundle.h" | |
| 11 #include "ui/compositor/layer.h" | |
| 12 #include "ui/compositor/scoped_layer_animation_settings.h" | |
| 13 #include "ui/resources/grit/ui_resources.h" | |
| 14 | |
| 15 DECLARE_UI_CLASS_PROPERTY_TYPE(ash::mus::Shadow*); | |
| 16 | |
| 17 namespace ash { | |
| 18 namespace mus { | |
| 19 namespace { | |
| 20 | |
| 21 DEFINE_UI_CLASS_PROPERTY_KEY(Shadow*, kShadowProperty, nullptr); | |
| 22 | |
| 23 // The opacity used for active shadow when animating between | |
| 24 // inactive/active shadow. | |
| 25 const float kInactiveShadowAnimationOpacity = 0.2f; | |
| 26 | |
| 27 // Shadow aperture for different styles. | |
| 28 // Note that this may be greater than interior inset to allow shadows with | |
| 29 // curved corners that extend inwards beyond a window's borders. | |
| 30 const int kActiveInteriorAperture = 134; | |
| 31 const int kInactiveInteriorAperture = 134; | |
| 32 const int kSmallInteriorAperture = 9; | |
| 33 | |
| 34 // Interior inset for different styles. | |
| 35 const int kActiveInteriorInset = 64; | |
| 36 const int kInactiveInteriorInset = 64; | |
| 37 const int kSmallInteriorInset = 4; | |
| 38 | |
| 39 // Rounded corners are overdrawn on top of the window's content layer, | |
| 40 // we need to exclude them from the occlusion area. | |
| 41 const int kRoundedCornerRadius = 2; | |
| 42 | |
| 43 // Duration for opacity animation in milliseconds. | |
| 44 const int kShadowAnimationDurationMs = 100; | |
| 45 | |
| 46 int GetShadowApertureForStyle(Shadow::Style style) { | |
| 47 switch (style) { | |
| 48 case Shadow::STYLE_ACTIVE: | |
| 49 return kActiveInteriorAperture; | |
| 50 case Shadow::STYLE_INACTIVE: | |
| 51 return kInactiveInteriorAperture; | |
| 52 case Shadow::STYLE_SMALL: | |
| 53 return kSmallInteriorAperture; | |
| 54 } | |
| 55 return 0; | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 Shadow::Shadow() : style_(STYLE_ACTIVE), interior_inset_(0), window_(nullptr) {} | |
| 61 | |
| 62 Shadow::~Shadow() { | |
| 63 if (window_) { | |
| 64 window_->ClearProperty(kShadowProperty); | |
| 65 window_->RemoveObserver(this); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void Shadow::Init(Style style) { | |
| 70 style_ = style; | |
| 71 | |
| 72 layer_.reset(new ui::Layer(ui::LAYER_NOT_DRAWN)); | |
| 73 shadow_layer_.reset(new ui::Layer(ui::LAYER_NINE_PATCH)); | |
| 74 layer()->Add(shadow_layer_.get()); | |
| 75 | |
| 76 UpdateImagesForStyle(); | |
| 77 shadow_layer_->set_name("Shadow"); | |
| 78 shadow_layer_->SetVisible(true); | |
| 79 shadow_layer_->SetFillsBoundsOpaquely(false); | |
| 80 } | |
| 81 | |
| 82 // static | |
| 83 Shadow* Shadow::Get(aura::Window* window) { | |
| 84 return window->GetProperty(kShadowProperty); | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 int Shadow::GetInteriorInsetForStyle(Shadow::Style style) { | |
| 89 switch (style) { | |
| 90 case Shadow::STYLE_ACTIVE: | |
| 91 return kActiveInteriorInset; | |
| 92 case Shadow::STYLE_INACTIVE: | |
| 93 return kInactiveInteriorInset; | |
| 94 case Shadow::STYLE_SMALL: | |
| 95 return kSmallInteriorInset; | |
| 96 } | |
| 97 return 0; | |
| 98 } | |
| 99 | |
| 100 void Shadow::SetContentBounds(const gfx::Rect& content_bounds) { | |
| 101 content_bounds_ = content_bounds; | |
| 102 UpdateLayerBounds(); | |
| 103 } | |
| 104 | |
| 105 void Shadow::SetStyle(Style style) { | |
| 106 if (style_ == style) | |
| 107 return; | |
| 108 | |
| 109 Style old_style = style_; | |
| 110 style_ = style; | |
| 111 | |
| 112 // Stop waiting for any as yet unfinished implicit animations. | |
| 113 StopObservingImplicitAnimations(); | |
| 114 | |
| 115 // If we're switching to or from the small style, don't bother with | |
| 116 // animations. | |
| 117 if (style == STYLE_SMALL || old_style == STYLE_SMALL) { | |
| 118 UpdateImagesForStyle(); | |
| 119 // Make sure the shadow is fully opaque. | |
| 120 shadow_layer_->SetOpacity(1.0f); | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 // If we're becoming active, switch images now. Because the inactive image | |
| 125 // has a very low opacity the switch isn't noticeable and this approach | |
| 126 // allows us to use only a single set of shadow images at a time. | |
| 127 if (style == STYLE_ACTIVE) { | |
| 128 UpdateImagesForStyle(); | |
| 129 // Opacity was baked into inactive image, start opacity low to match. | |
| 130 shadow_layer_->SetOpacity(kInactiveShadowAnimationOpacity); | |
| 131 } | |
| 132 | |
| 133 { | |
| 134 // Property sets within this scope will be implicitly animated. | |
| 135 ui::ScopedLayerAnimationSettings settings(shadow_layer_->GetAnimator()); | |
| 136 settings.AddObserver(this); | |
| 137 settings.SetTransitionDuration( | |
| 138 base::TimeDelta::FromMilliseconds(kShadowAnimationDurationMs)); | |
| 139 switch (style_) { | |
| 140 case STYLE_ACTIVE: | |
| 141 // Animate the active shadow from kInactiveShadowAnimationOpacity to | |
| 142 // 1.0f. | |
| 143 shadow_layer_->SetOpacity(1.0f); | |
| 144 break; | |
| 145 case STYLE_INACTIVE: | |
| 146 // The opacity will be reset to 1.0f when animation is completed. | |
| 147 shadow_layer_->SetOpacity(kInactiveShadowAnimationOpacity); | |
| 148 break; | |
| 149 default: | |
| 150 NOTREACHED() << "Unhandled style " << style_; | |
| 151 break; | |
| 152 } | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 void Shadow::Install(aura::Window* window) { | |
| 157 window->SetProperty(kShadowProperty, this); | |
| 158 window_ = window; | |
| 159 window_->AddObserver(this); | |
| 160 } | |
| 161 | |
| 162 void Shadow::OnImplicitAnimationsCompleted() { | |
| 163 // If we just finished going inactive, switch images. This doesn't cause | |
| 164 // a visual pop because the inactive image opacity is so low. | |
| 165 if (style_ == STYLE_INACTIVE) { | |
| 166 UpdateImagesForStyle(); | |
| 167 // Opacity is baked into inactive image, so set fully opaque. | |
| 168 shadow_layer_->SetOpacity(1.0f); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 void Shadow::UpdateImagesForStyle() { | |
| 173 ResourceBundle& res = ResourceBundle::GetSharedInstance(); | |
| 174 gfx::Image image; | |
| 175 switch (style_) { | |
| 176 case STYLE_ACTIVE: | |
| 177 image = res.GetImageNamed(IDR_AURA_SHADOW_ACTIVE); | |
| 178 break; | |
| 179 case STYLE_INACTIVE: | |
| 180 image = res.GetImageNamed(IDR_AURA_SHADOW_INACTIVE); | |
| 181 break; | |
| 182 case STYLE_SMALL: | |
| 183 image = res.GetImageNamed(IDR_WINDOW_BUBBLE_SHADOW_SMALL); | |
| 184 break; | |
| 185 default: | |
| 186 NOTREACHED() << "Unhandled style " << style_; | |
| 187 break; | |
| 188 } | |
| 189 | |
| 190 shadow_layer_->UpdateNinePatchLayerImage(image.AsImageSkia()); | |
| 191 image_size_ = image.Size(); | |
| 192 interior_inset_ = GetInteriorInsetForStyle(style_); | |
| 193 | |
| 194 // Image sizes may have changed. | |
| 195 UpdateLayerBounds(); | |
| 196 } | |
| 197 | |
| 198 void Shadow::UpdateLayerBounds() { | |
| 199 // Update bounds based on content bounds and interior inset. | |
| 200 gfx::Rect layer_bounds = content_bounds_; | |
| 201 layer_bounds.Inset(-interior_inset_, -interior_inset_); | |
| 202 layer()->SetBounds(layer_bounds); | |
| 203 shadow_layer_->SetBounds(gfx::Rect(layer_bounds.size())); | |
| 204 | |
| 205 // Update the shadow aperture and border for style. Note that border is in | |
| 206 // layer space and it cannot exceed the bounds of the layer. | |
| 207 int aperture = GetShadowApertureForStyle(style_); | |
| 208 int aperture_x = std::min(aperture, layer_bounds.width() / 2); | |
| 209 int aperture_y = std::min(aperture, layer_bounds.height() / 2); | |
| 210 gfx::Rect aperture_rect(aperture_x, aperture_y, | |
| 211 image_size_.width() - aperture_x * 2, | |
| 212 image_size_.height() - aperture_y * 2); | |
| 213 | |
| 214 shadow_layer_->UpdateNinePatchLayerAperture(aperture_rect); | |
| 215 shadow_layer_->UpdateNinePatchLayerBorder( | |
| 216 gfx::Rect(aperture_x, aperture_y, aperture_x * 2, aperture_y * 2)); | |
| 217 | |
| 218 // The content bounds in the shadow's layer space are offsetted by | |
| 219 // |interior_inset_|. The occlusion area also has to be shrunk to allow | |
| 220 // rounded corners overdrawing on top of the window's content. | |
| 221 gfx::Rect content_bounds(interior_inset_ + kRoundedCornerRadius, | |
| 222 interior_inset_ + kRoundedCornerRadius, | |
| 223 content_bounds_.width() - 2 * kRoundedCornerRadius, | |
| 224 content_bounds_.height() - 2 * kRoundedCornerRadius); | |
| 225 shadow_layer_->UpdateNinePatchOcclusion(content_bounds); | |
| 226 } | |
| 227 | |
| 228 void Shadow::OnWindowDestroyed(aura::Window* window) { | |
| 229 DCHECK_EQ(window_, window); | |
| 230 window_ = nullptr; | |
| 231 } | |
| 232 | |
| 233 } // namespace mus | |
| 234 } // namespace ash | |
| OLD | NEW |