| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/wm/core/shadow.h" | 5 #include "ui/wm/core/shadow.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "ui/base/resource/resource_bundle.h" | 8 #include "ui/base/resource/resource_bundle.h" |
| 9 #include "ui/compositor/layer.h" | 9 #include "ui/compositor/layer.h" |
| 10 #include "ui/compositor/scoped_layer_animation_settings.h" | 10 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 11 #include "ui/gfx/geometry/insets.h" | 11 #include "ui/gfx/geometry/insets.h" |
| 12 #include "ui/gfx/image/image_skia_operations.h" | 12 #include "ui/gfx/image/image_skia_operations.h" |
| 13 #include "ui/wm/core/shadow_types.h" |
| 13 | 14 |
| 14 namespace wm { | 15 namespace wm { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Rounded corners are overdrawn on top of the window's content layer, | 19 // Rounded corners are overdrawn on top of the window's content layer, |
| 19 // we need to exclude them from the occlusion area. | 20 // we need to exclude them from the occlusion area. |
| 20 const int kRoundedCornerRadius = 2; | 21 const int kRoundedCornerRadius = 2; |
| 21 | 22 |
| 22 // Duration for opacity animation in milliseconds. | 23 // Duration for opacity animation in milliseconds. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 // To see what this looks like for elevation 24, try this CSS: | 57 // To see what this looks like for elevation 24, try this CSS: |
| 57 // box-shadow: 0 24px 48px rgba(0, 0, 0, .24), | 58 // box-shadow: 0 24px 48px rgba(0, 0, 0, .24), |
| 58 // 0 0 24px rgba(0, 0, 0, .12); | 59 // 0 0 24px rgba(0, 0, 0, .12); |
| 59 shadow->ninebox_image = gfx::ImageSkiaOperations::CreateShadowNinebox( | 60 shadow->ninebox_image = gfx::ImageSkiaOperations::CreateShadowNinebox( |
| 60 shadow->values, kRoundedCornerRadius); | 61 shadow->values, kRoundedCornerRadius); |
| 61 return *shadow; | 62 return *shadow; |
| 62 } | 63 } |
| 63 | 64 |
| 64 } // namespace | 65 } // namespace |
| 65 | 66 |
| 66 Shadow::Shadow() {} | 67 Shadow::Shadow() : desired_elevation_(ShadowElevation::NONE) {} |
| 67 | 68 |
| 68 Shadow::~Shadow() {} | 69 Shadow::~Shadow() {} |
| 69 | 70 |
| 70 void Shadow::Init(Style style) { | 71 void Shadow::Init(ShadowElevation elevation) { |
| 71 style_ = style; | 72 desired_elevation_ = elevation; |
| 72 layer_.reset(new ui::Layer(ui::LAYER_NOT_DRAWN)); | 73 layer_.reset(new ui::Layer(ui::LAYER_NOT_DRAWN)); |
| 73 RecreateShadowLayer(); | 74 RecreateShadowLayer(); |
| 74 } | 75 } |
| 75 | 76 |
| 76 void Shadow::SetContentBounds(const gfx::Rect& content_bounds) { | 77 void Shadow::SetContentBounds(const gfx::Rect& content_bounds) { |
| 77 // When the window moves but doesn't change size, this is a no-op. (The | 78 // When the window moves but doesn't change size, this is a no-op. (The |
| 78 // origin stays the same in this case.) | 79 // origin stays the same in this case.) |
| 79 if (content_bounds == content_bounds_) | 80 if (content_bounds == content_bounds_) |
| 80 return; | 81 return; |
| 81 | 82 |
| 82 content_bounds_ = content_bounds; | 83 content_bounds_ = content_bounds; |
| 83 UpdateLayerBounds(); | 84 UpdateLayerBounds(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 void Shadow::SetStyle(Style style) { | 87 void Shadow::SetElevation(ShadowElevation elevation) { |
| 87 if (style_ == style) | 88 if (desired_elevation_ == elevation) |
| 88 return; | 89 return; |
| 89 | 90 |
| 90 style_ = style; | 91 desired_elevation_ = elevation; |
| 91 | 92 |
| 92 // Stop waiting for any as yet unfinished implicit animations. | 93 // Stop waiting for any as yet unfinished implicit animations. |
| 93 StopObservingImplicitAnimations(); | 94 StopObservingImplicitAnimations(); |
| 94 | 95 |
| 95 // The old shadow layer is the new fading out layer. | 96 // The old shadow layer is the new fading out layer. |
| 96 DCHECK(shadow_layer_); | 97 DCHECK(shadow_layer_); |
| 97 fading_layer_ = std::move(shadow_layer_); | 98 fading_layer_ = std::move(shadow_layer_); |
| 98 RecreateShadowLayer(); | 99 RecreateShadowLayer(); |
| 99 shadow_layer_->SetOpacity(0.f); | 100 shadow_layer_->SetOpacity(0.f); |
| 100 | 101 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 130 shadow_layer_->SetFillsBoundsOpaquely(false); | 131 shadow_layer_->SetFillsBoundsOpaquely(false); |
| 131 layer()->Add(shadow_layer_.get()); | 132 layer()->Add(shadow_layer_.get()); |
| 132 | 133 |
| 133 UpdateLayerBounds(); | 134 UpdateLayerBounds(); |
| 134 } | 135 } |
| 135 | 136 |
| 136 void Shadow::UpdateLayerBounds() { | 137 void Shadow::UpdateLayerBounds() { |
| 137 if (content_bounds_.IsEmpty()) | 138 if (content_bounds_.IsEmpty()) |
| 138 return; | 139 return; |
| 139 | 140 |
| 140 // The elevation depends on the style, but the ninebox assumption breaks down | 141 // The ninebox assumption breaks down when the window is too small for the |
| 141 // when the window is too small. The height/width of |blur_region| will be | 142 // desired elevation. The height/width of |blur_region| will be 4 * elevation |
| 142 // 4 * elevation (see GetDetailsForElevation), so cap elevation at the most we | 143 // (see GetDetailsForElevation), so cap elevation at the most we can handle. |
| 143 // can handle. | |
| 144 const int smaller_dimension = | 144 const int smaller_dimension = |
| 145 std::min(content_bounds_.width(), content_bounds_.height()); | 145 std::min(content_bounds_.width(), content_bounds_.height()); |
| 146 const int size_adjusted_elevation = std::min( | 146 const int size_adjusted_elevation = |
| 147 (smaller_dimension - 2 * kRoundedCornerRadius) / 4, ElevationForStyle()); | 147 std::min((smaller_dimension - 2 * kRoundedCornerRadius) / 4, |
| 148 static_cast<int>(desired_elevation_)); |
| 148 const ShadowDetails& details = | 149 const ShadowDetails& details = |
| 149 GetDetailsForElevation(size_adjusted_elevation); | 150 GetDetailsForElevation(size_adjusted_elevation); |
| 150 gfx::Insets blur_region = gfx::ShadowValue::GetBlurRegion(details.values) + | 151 gfx::Insets blur_region = gfx::ShadowValue::GetBlurRegion(details.values) + |
| 151 gfx::Insets(kRoundedCornerRadius); | 152 gfx::Insets(kRoundedCornerRadius); |
| 152 if (size_adjusted_elevation != effective_elevation_) { | 153 if (size_adjusted_elevation != effective_elevation_) { |
| 153 shadow_layer_->UpdateNinePatchLayerImage(details.ninebox_image); | 154 shadow_layer_->UpdateNinePatchLayerImage(details.ninebox_image); |
| 154 // The ninebox grid is defined in terms of the image size. The shadow blurs | 155 // The ninebox grid is defined in terms of the image size. The shadow blurs |
| 155 // in both inward and outward directions from the edge of the contents, so | 156 // in both inward and outward directions from the edge of the contents, so |
| 156 // the aperture goes further inside the image than the shadow margins (which | 157 // the aperture goes further inside the image than the shadow margins (which |
| 157 // represent exterior blur). | 158 // represent exterior blur). |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 gfx::Rect occlusion_bounds(shadow_layer_bounds.size()); | 198 gfx::Rect occlusion_bounds(shadow_layer_bounds.size()); |
| 198 occlusion_bounds.Inset(-margins + gfx::Insets(kRoundedCornerRadius)); | 199 occlusion_bounds.Inset(-margins + gfx::Insets(kRoundedCornerRadius)); |
| 199 shadow_layer_->UpdateNinePatchOcclusion(occlusion_bounds); | 200 shadow_layer_->UpdateNinePatchOcclusion(occlusion_bounds); |
| 200 | 201 |
| 201 // The border is the same inset as the aperture. | 202 // The border is the same inset as the aperture. |
| 202 shadow_layer_->UpdateNinePatchLayerBorder( | 203 shadow_layer_->UpdateNinePatchLayerBorder( |
| 203 gfx::Rect(blur_region.left(), blur_region.top(), blur_region.width(), | 204 gfx::Rect(blur_region.left(), blur_region.top(), blur_region.width(), |
| 204 blur_region.height())); | 205 blur_region.height())); |
| 205 } | 206 } |
| 206 | 207 |
| 207 int Shadow::ElevationForStyle() { | |
| 208 switch (style_) { | |
| 209 case STYLE_ACTIVE: | |
| 210 return 24; | |
| 211 case STYLE_INACTIVE: | |
| 212 return 8; | |
| 213 case STYLE_SMALL: | |
| 214 return 6; | |
| 215 } | |
| 216 NOTREACHED(); | |
| 217 return 0; | |
| 218 } | |
| 219 | |
| 220 } // namespace wm | 208 } // namespace wm |
| OLD | NEW |