Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tommy C. Li | 092c628a | 2018-03-29 20:32:15 | [diff] [blame] | 5 | #ifndef UI_COMPOSITOR_EXTRA_SHADOW_H_ |
| 6 | #define UI_COMPOSITOR_EXTRA_SHADOW_H_ |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 7 | |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 10 | #include "base/memory/raw_ptr.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 11 | #include "ui/compositor/layer_animation_observer.h" |
Ahmed Fakhry | 34972974 | 2019-07-26 03:17:43 | [diff] [blame] | 12 | #include "ui/compositor/layer_owner.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 13 | #include "ui/gfx/geometry/rect.h" |
Xiaodan Zhu | c176db6 | 2022-01-25 01:47:19 | [diff] [blame] | 14 | #include "ui/gfx/shadow_util.h" |
David Reveman | 60c9b24 | 2017-11-28 02:24:50 | [diff] [blame] | 15 | |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 16 | namespace ui { |
| 17 | class Layer; |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 18 | |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 19 | // Simple class that draws a drop shadow around content at given bounds. |
Ahmed Fakhry | 34972974 | 2019-07-26 03:17:43 | [diff] [blame] | 20 | class Shadow : public ui::ImplicitAnimationObserver, public ui::LayerOwner { |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 21 | public: |
Xiaodan Zhu | e1930d4 | 2023-07-18 17:51:02 | [diff] [blame] | 22 | // Mapping from elevation to key and ambient shadow colors. The first color is |
| 23 | // the key shadow color and the second is the ambient shadow color. |
Xiaodan Zhu | 6622a40 | 2023-07-12 01:59:50 | [diff] [blame] | 24 | using ElevationToColorsMap = base::flat_map<int, std::pair<SkColor, SkColor>>; |
| 25 | |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 26 | Shadow(); |
Peter Boström | c8c1235 | 2021-09-21 23:37:15 | [diff] [blame] | 27 | |
| 28 | Shadow(const Shadow&) = delete; |
| 29 | Shadow& operator=(const Shadow&) = delete; |
| 30 | |
dcheng | a74e354 | 2014-10-27 21:15:05 | [diff] [blame] | 31 | ~Shadow() override; |
[email protected] | a8bd1cb | 2012-02-17 23:05:54 | [diff] [blame] | 32 | |
Trent Apted | 280f4ff | 2018-02-07 07:11:53 | [diff] [blame] | 33 | // Initialize for the the given shadow |elevation|. This is passed to |
| 34 | // gfx::ShadowValue::MakeMdShadowValues() and controls the y-offset and blur |
| 35 | // for the shadow style. |
| 36 | void Init(int elevation); |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 37 | |
varkha | 52d1403 | 2016-07-16 04:23:58 | [diff] [blame] | 38 | // Exposed to allow setting animation parameters for bounds and opacity |
| 39 | // animations. |
Ahmed Fakhry | 34972974 | 2019-07-26 03:17:43 | [diff] [blame] | 40 | ui::Layer* shadow_layer() { return shadow_layer_owner_.layer(); } |
| 41 | |
| 42 | ui::Layer* fading_layer() { return fading_layer_owner_.layer(); } |
varkha | 52d1403 | 2016-07-16 04:23:58 | [diff] [blame] | 43 | |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 44 | const gfx::Rect& content_bounds() const { return content_bounds_; } |
Trent Apted | 280f4ff | 2018-02-07 07:11:53 | [diff] [blame] | 45 | int desired_elevation() const { return desired_elevation_; } |
Xiaodan Zhu | 6622a40 | 2023-07-12 01:59:50 | [diff] [blame] | 46 | const ElevationToColorsMap& color_map() const { return color_map_; } |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 47 | |
[email protected] | 98a5913 | 2014-07-16 22:49:52 | [diff] [blame] | 48 | // Moves and resizes the shadow layer to frame |content_bounds|. |
Ahmed Fakhry | 34972974 | 2019-07-26 03:17:43 | [diff] [blame] | 49 | // This should be used to adjust the shadow's size and position (rather than |
| 50 | // applying transformations to the `layer()` of this Shadow). |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 51 | void SetContentBounds(const gfx::Rect& content_bounds); |
| 52 | |
estade | ba7b9d7 | 2017-01-04 20:06:06 | [diff] [blame] | 53 | // Sets the shadow's appearance, animating opacity as necessary. |
Trent Apted | 280f4ff | 2018-02-07 07:11:53 | [diff] [blame] | 54 | void SetElevation(int elevation); |
[email protected] | a8bd1cb | 2012-02-17 23:05:54 | [diff] [blame] | 55 | |
reveman | c2ac3cdd | 2017-01-26 02:50:51 | [diff] [blame] | 56 | // Sets the radius for the rounded corners to take into account when |
| 57 | // adjusting the shadow layer to frame |content_bounds|. 0 or greater. |
| 58 | void SetRoundedCornerRadius(int rounded_corner_radius); |
| 59 | |
Xiaodan Zhu | c176db6 | 2022-01-25 01:47:19 | [diff] [blame] | 60 | // Set shadow style. |
| 61 | void SetShadowStyle(gfx::ShadowStyle style); |
| 62 | |
Xiaodan Zhu | 6622a40 | 2023-07-12 01:59:50 | [diff] [blame] | 63 | // Set customized key and ambient shadows color map for certain elevations. |
| 64 | void SetElevationToColorsMap(const ElevationToColorsMap& color_map); |
| 65 | |
David Reveman | 60c9b24 | 2017-11-28 02:24:50 | [diff] [blame] | 66 | const gfx::ShadowDetails* details_for_testing() const { return details_; } |
Xiaodan Zhu | b691945 | 2023-09-11 17:59:26 | [diff] [blame] | 67 | int rounded_corner_radius_for_testing() const { |
| 68 | return rounded_corner_radius_; |
| 69 | } |
David Reveman | 60c9b24 | 2017-11-28 02:24:50 | [diff] [blame] | 70 | |
[email protected] | a8bd1cb | 2012-02-17 23:05:54 | [diff] [blame] | 71 | // ui::ImplicitAnimationObserver overrides: |
dcheng | a74e354 | 2014-10-27 21:15:05 | [diff] [blame] | 72 | void OnImplicitAnimationsCompleted() override; |
[email protected] | a8bd1cb | 2012-02-17 23:05:54 | [diff] [blame] | 73 | |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 74 | private: |
Ahmed Fakhry | fc28da5f | 2019-07-30 18:05:32 | [diff] [blame] | 75 | // A shadow layer owner that correctly updates the nine patch layer details |
| 76 | // when it gets recreated. |
| 77 | class ShadowLayerOwner : public ui::LayerOwner { |
| 78 | public: |
| 79 | explicit ShadowLayerOwner(Shadow* owner, |
| 80 | std::unique_ptr<Layer> layer = nullptr); |
Peter Boström | c8c1235 | 2021-09-21 23:37:15 | [diff] [blame] | 81 | |
| 82 | ShadowLayerOwner(const ShadowLayerOwner&) = delete; |
| 83 | ShadowLayerOwner& operator=(const ShadowLayerOwner&) = delete; |
| 84 | |
Ahmed Fakhry | fc28da5f | 2019-07-30 18:05:32 | [diff] [blame] | 85 | ~ShadowLayerOwner() override; |
| 86 | |
| 87 | // ui::LayerOwner: |
| 88 | std::unique_ptr<Layer> RecreateLayer() override; |
| 89 | |
| 90 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 91 | const raw_ptr<Shadow> owner_shadow_; |
Ahmed Fakhry | fc28da5f | 2019-07-30 18:05:32 | [diff] [blame] | 92 | }; |
| 93 | |
estade | ba7b9d7 | 2017-01-04 20:06:06 | [diff] [blame] | 94 | // Updates the shadow layer and its image to reflect |desired_elevation_|. |
estade | 7793237 | 2016-12-12 23:31:43 | [diff] [blame] | 95 | void RecreateShadowLayer(); |
[email protected] | a8bd1cb | 2012-02-17 23:05:54 | [diff] [blame] | 96 | |
Xiaodan Zhu | e1930d4 | 2023-07-18 17:51:02 | [diff] [blame] | 97 | // Updates the shadow appearance based on the inteior inset, the current |
| 98 | // |content_bounds_|, shadow style, and colors. |
| 99 | void UpdateShadowAppearance(); |
[email protected] | a8bd1cb | 2012-02-17 23:05:54 | [diff] [blame] | 100 | |
estade | ba7b9d7 | 2017-01-04 20:06:06 | [diff] [blame] | 101 | // The goal elevation, set when the transition animation starts. The elevation |
| 102 | // dictates the shadow's display characteristics and is proportional to the |
| 103 | // size of the blur and its offset. This may not match reality if the window |
| 104 | // isn't big enough to support it. |
Trent Apted | 280f4ff | 2018-02-07 07:11:53 | [diff] [blame] | 105 | int desired_elevation_ = 0; |
estade | d1acf85 | 2016-12-07 20:23:20 | [diff] [blame] | 106 | |
reveman | c2ac3cdd | 2017-01-26 02:50:51 | [diff] [blame] | 107 | // Rounded corners are drawn on top of the window's content layer, |
| 108 | // we need to exclude them from the occlusion area. |
Trent Apted | 280f4ff | 2018-02-07 07:11:53 | [diff] [blame] | 109 | int rounded_corner_radius_ = 2; |
reveman | c2ac3cdd | 2017-01-26 02:50:51 | [diff] [blame] | 110 | |
Ahmed Fakhry | 34972974 | 2019-07-26 03:17:43 | [diff] [blame] | 111 | // The details of the shadow image that's currently set on |shadow_layer()|. |
David Reveman | 60c9b24 | 2017-11-28 02:24:50 | [diff] [blame] | 112 | // This will be null until a positive elevation has been set. Once set, it |
| 113 | // will always point to a global ShadowDetails instance that is guaranteed |
| 114 | // to outlive the Shadow instance. See ui/gfx/shadow_util.h for how these |
| 115 | // ShadowDetails instances are created. |
Paul Semel | 89e1f63c | 2023-06-19 13:34:10 | [diff] [blame] | 116 | raw_ptr<const gfx::ShadowDetails, LeakedDanglingUntriaged> details_ = nullptr; |
David Reveman | 60c9b24 | 2017-11-28 02:24:50 | [diff] [blame] | 117 | |
Xiaodan Zhu | c176db6 | 2022-01-25 01:47:19 | [diff] [blame] | 118 | // The style of shadow. Use MD style by default. |
| 119 | gfx::ShadowStyle style_ = gfx::ShadowStyle::kMaterialDesign; |
| 120 | |
Xiaodan Zhu | 6622a40 | 2023-07-12 01:59:50 | [diff] [blame] | 121 | // The customized key and ambient shadows color map for certain elevations. |
| 122 | ElevationToColorsMap color_map_; |
| 123 | |
Ahmed Fakhry | 34972974 | 2019-07-26 03:17:43 | [diff] [blame] | 124 | // The owner of the actual shadow layer corresponding to a cc::NinePatchLayer. |
Ahmed Fakhry | fc28da5f | 2019-07-30 18:05:32 | [diff] [blame] | 125 | ShadowLayerOwner shadow_layer_owner_; |
[email protected] | 3ddb682 | 2014-07-23 16:38:10 | [diff] [blame] | 126 | |
estade | ba7b9d7 | 2017-01-04 20:06:06 | [diff] [blame] | 127 | // When the elevation changes, the old shadow cross-fades with the new one. |
Ahmed Fakhry | 34972974 | 2019-07-26 03:17:43 | [diff] [blame] | 128 | // When non-null, this owns an old |shadow_layer()| that's being animated out. |
| 129 | ui::LayerOwner fading_layer_owner_; |
estade | 7793237 | 2016-12-12 23:31:43 | [diff] [blame] | 130 | |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 131 | // Bounds of the content that the shadow encloses. |
| 132 | gfx::Rect content_bounds_; |
Xiaodan Zhu | 9ab84d3 | 2023-08-02 05:09:51 | [diff] [blame] | 133 | |
| 134 | // The layer bounds since content bounds were last set. |
| 135 | gfx::Rect last_layer_bounds_; |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 136 | }; |
| 137 | |
Tommy C. Li | 092c628a | 2018-03-29 20:32:15 | [diff] [blame] | 138 | } // namespace ui |
[email protected] | a54e65b | 2011-11-21 22:03:34 | [diff] [blame] | 139 | |
Tommy C. Li | 092c628a | 2018-03-29 20:32:15 | [diff] [blame] | 140 | #endif // UI_COMPOSITOR_EXTRA_SHADOW_H_ |