system_shadow.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2022 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 ASH_STYLE_SYSTEM_SHADOW_H_
  5. #define ASH_STYLE_SYSTEM_SHADOW_H_
  6. #include "ash/ash_export.h"
  7. #include "ui/gfx/geometry/rect.h"
  8. namespace aura {
  9. class Window;
  10. } // namespace aura
  11. namespace ui {
  12. class Layer;
  13. } // namespace ui
  14. namespace views {
  15. class View;
  16. } // namespace views
  17. namespace ash {
  18. // SystemShadow is an interface to generate shadow with system shadow style for
  19. // different types of UI surfaces.
  20. class ASH_EXPORT SystemShadow {
  21. public:
  22. // Shadow types of system UI components. The shadows with different elevations
  23. // have different appearance.
  24. enum class Type {
  25. kElevation4,
  26. kElevation8,
  27. kElevation12,
  28. kElevation16,
  29. kElevation24,
  30. };
  31. virtual ~SystemShadow();
  32. // Create a system shadow based on `ui::Shadow` which paints shadow on a nine
  33. // patch layer. This shadow can be used for any UI surfaces. Usually, when
  34. // creating the shadow for a window, attach the shadow's layer at the bottom
  35. // of the window's layer; when creating the shadow for a view, attach the
  36. // shadow's layer at the bottom of the view's parent layer. The layer's
  37. // content bounds should be manually updated.
  38. static std::unique_ptr<SystemShadow> CreateShadowOnNinePatchLayer(
  39. Type shadow_type);
  40. // Create a system shadow based on `ash::ViewShadow`. This shadow is used for
  41. // views. The shadow's layer is added to the `layers_beneath_` of the view and
  42. // its content bounds are adjusted with the bounds of view's layer. The shadow
  43. // does not need to manually update the content bounds but cannot be used when
  44. // the shadow's content bounds do not equal to the view bounds. For example,
  45. // `AppListFolderView` has a clip rect whose bounds should be the content
  46. // bounds of the shadow. In this case, please use
  47. // `CreateShadowOnNinePatchLayer` instead.
  48. static std::unique_ptr<SystemShadow> CreateShadowOnNinePatchLayerForView(
  49. views::View* view,
  50. Type shadow_type);
  51. // Create a system shadow based on `ui::Shadow`. The shadow's layer is added
  52. // to the bottom of the window's layer and its contents bounds are adjusted
  53. // with the window bounds. The shadow does not need to manually update the
  54. // content bounds but cannot be used when the shadow's contents bounds do not
  55. // equal to the window bounds. For example, the content bounds of
  56. // `OverviewItem` for wide and tall windows do not equal to the item bounds.
  57. // In this case, please use `CreateShadowOnNinePatchLayer` instead.
  58. static std::unique_ptr<SystemShadow> CreateShadowOnNinePatchLayerForWindow(
  59. aura::Window* window,
  60. Type shadow_type);
  61. // Create a system shadow painted on a texture layer. Painting shadow on a
  62. // texture layer is expensive so only use it when necessary. See
  63. // `SystemShadowOnTextureLayer` for more details.
  64. static std::unique_ptr<SystemShadow> CreateShadowOnTextureLayer(
  65. Type shadow_type);
  66. // Get shadow elevation according to the given type.
  67. static int GetElevationFromType(Type type);
  68. // Change shadow type and update shadow elevation and appearance. Note that to
  69. // avoid inconsistency of shadow type and elevation. Always change system
  70. // shadow elevation with `SetType`.
  71. virtual void SetType(Type type) = 0;
  72. virtual void SetContentBounds(const gfx::Rect& bounds) = 0;
  73. virtual void SetRoundedCornerRadius(int corner_radius) = 0;
  74. virtual const gfx::Rect& GetContentBounds() = 0;
  75. // Return the layer of the shadow. This function can be used by any types of
  76. // shadow. The layer is commonly used for setting layer hierarchy, visibility,
  77. // and transformation.
  78. virtual ui::Layer* GetLayer() = 0;
  79. // Return the nine patch layer of the shadow. This function is only used by
  80. // ui::Shadow based implementations. The nine patch layer is a child layer of
  81. // the shadow's layer painted with the shadow image. Normally, set the
  82. // hierarchy, visibility and transformation on the shadow's layer instead of
  83. // the nine patch layer.
  84. virtual ui::Layer* GetNinePatchLayer() = 0;
  85. };
  86. } // namespace ash
  87. #endif // ASH_STYLE_SYSTEM_SHADOW_H_