shadow.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.
  4. #ifndef UI_COMPOSITOR_EXTRA_SHADOW_H_
  5. #define UI_COMPOSITOR_EXTRA_SHADOW_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "ui/compositor/layer_animation_observer.h"
  9. #include "ui/compositor/layer_owner.h"
  10. #include "ui/gfx/geometry/rect.h"
  11. #include "ui/gfx/shadow_util.h"
  12. namespace ui {
  13. class Layer;
  14. // Simple class that draws a drop shadow around content at given bounds.
  15. class Shadow : public ui::ImplicitAnimationObserver, public ui::LayerOwner {
  16. public:
  17. Shadow();
  18. Shadow(const Shadow&) = delete;
  19. Shadow& operator=(const Shadow&) = delete;
  20. ~Shadow() override;
  21. // Initialize for the the given shadow |elevation|. This is passed to
  22. // gfx::ShadowValue::MakeMdShadowValues() and controls the y-offset and blur
  23. // for the shadow style.
  24. void Init(int elevation);
  25. // Exposed to allow setting animation parameters for bounds and opacity
  26. // animations.
  27. ui::Layer* shadow_layer() { return shadow_layer_owner_.layer(); }
  28. ui::Layer* fading_layer() { return fading_layer_owner_.layer(); }
  29. const gfx::Rect& content_bounds() const { return content_bounds_; }
  30. int desired_elevation() const { return desired_elevation_; }
  31. // Moves and resizes the shadow layer to frame |content_bounds|.
  32. // This should be used to adjust the shadow's size and position (rather than
  33. // applying transformations to the `layer()` of this Shadow).
  34. void SetContentBounds(const gfx::Rect& content_bounds);
  35. // Sets the shadow's appearance, animating opacity as necessary.
  36. void SetElevation(int elevation);
  37. // Sets the radius for the rounded corners to take into account when
  38. // adjusting the shadow layer to frame |content_bounds|. 0 or greater.
  39. void SetRoundedCornerRadius(int rounded_corner_radius);
  40. // Set shadow style.
  41. void SetShadowStyle(gfx::ShadowStyle style);
  42. const gfx::ShadowDetails* details_for_testing() const { return details_; }
  43. // ui::ImplicitAnimationObserver overrides:
  44. void OnImplicitAnimationsCompleted() override;
  45. private:
  46. // A shadow layer owner that correctly updates the nine patch layer details
  47. // when it gets recreated.
  48. class ShadowLayerOwner : public ui::LayerOwner {
  49. public:
  50. explicit ShadowLayerOwner(Shadow* owner,
  51. std::unique_ptr<Layer> layer = nullptr);
  52. ShadowLayerOwner(const ShadowLayerOwner&) = delete;
  53. ShadowLayerOwner& operator=(const ShadowLayerOwner&) = delete;
  54. ~ShadowLayerOwner() override;
  55. // ui::LayerOwner:
  56. std::unique_ptr<Layer> RecreateLayer() override;
  57. private:
  58. const raw_ptr<Shadow> owner_shadow_;
  59. };
  60. // Updates the shadow layer and its image to reflect |desired_elevation_|.
  61. void RecreateShadowLayer();
  62. // Updates the shadow layer bounds based on the inteior inset and the current
  63. // |content_bounds_|.
  64. void UpdateLayerBounds();
  65. // The goal elevation, set when the transition animation starts. The elevation
  66. // dictates the shadow's display characteristics and is proportional to the
  67. // size of the blur and its offset. This may not match reality if the window
  68. // isn't big enough to support it.
  69. int desired_elevation_ = 0;
  70. // Rounded corners are drawn on top of the window's content layer,
  71. // we need to exclude them from the occlusion area.
  72. int rounded_corner_radius_ = 2;
  73. // The details of the shadow image that's currently set on |shadow_layer()|.
  74. // This will be null until a positive elevation has been set. Once set, it
  75. // will always point to a global ShadowDetails instance that is guaranteed
  76. // to outlive the Shadow instance. See ui/gfx/shadow_util.h for how these
  77. // ShadowDetails instances are created.
  78. raw_ptr<const gfx::ShadowDetails> details_ = nullptr;
  79. // The style of shadow. Use MD style by default.
  80. gfx::ShadowStyle style_ = gfx::ShadowStyle::kMaterialDesign;
  81. // The owner of the actual shadow layer corresponding to a cc::NinePatchLayer.
  82. ShadowLayerOwner shadow_layer_owner_;
  83. // When the elevation changes, the old shadow cross-fades with the new one.
  84. // When non-null, this owns an old |shadow_layer()| that's being animated out.
  85. ui::LayerOwner fading_layer_owner_;
  86. // Bounds of the content that the shadow encloses.
  87. gfx::Rect content_bounds_;
  88. };
  89. } // namespace ui
  90. #endif // UI_COMPOSITOR_EXTRA_SHADOW_H_