system_shadow_on_nine_patch_layer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_ON_NINE_PATCH_LAYER_H_
  5. #define ASH_STYLE_SYSTEM_SHADOW_ON_NINE_PATCH_LAYER_H_
  6. #include "ash/public/cpp/view_shadow.h"
  7. #include "ash/style/system_shadow.h"
  8. #include "base/scoped_observation.h"
  9. #include "ui/aura/window.h"
  10. #include "ui/aura/window_observer.h"
  11. #include "ui/compositor_extra/shadow.h"
  12. namespace ui {
  13. class Layer;
  14. } // namespace ui
  15. namespace ash {
  16. // SystemShadowOnNinePatchLayer is an interface for the shadows based on
  17. // ui::Shadow which paints shadow on a nine patch layer. The shadow attributes
  18. // are set and get from ui::Shadow's functions. The child classes need to expose
  19. // their ui::Shadow pointer in `shadow()`.
  20. class SystemShadowOnNinePatchLayer : public SystemShadow {
  21. public:
  22. ~SystemShadowOnNinePatchLayer() override;
  23. // SystemShadow:
  24. void SetType(SystemShadow::Type type) override;
  25. void SetContentBounds(const gfx::Rect& bounds) override;
  26. void SetRoundedCornerRadius(int corner_radius) override;
  27. const gfx::Rect& GetContentBounds() override;
  28. ui::Layer* GetLayer() override;
  29. ui::Layer* GetNinePatchLayer() override;
  30. protected:
  31. virtual ui::Shadow* shadow() = 0;
  32. };
  33. // An implementation of `SystemShadowOnNinePatchLayer`. It is directly based on
  34. // the ui::Shadow.
  35. class SystemShadowOnNinePatchLayerImpl : public SystemShadowOnNinePatchLayer {
  36. public:
  37. explicit SystemShadowOnNinePatchLayerImpl(SystemShadow::Type type);
  38. SystemShadowOnNinePatchLayerImpl(const SystemShadowOnNinePatchLayerImpl&) =
  39. delete;
  40. SystemShadowOnNinePatchLayerImpl& operator=(
  41. const SystemShadowOnNinePatchLayerImpl&) = delete;
  42. ~SystemShadowOnNinePatchLayerImpl() override;
  43. private:
  44. // SystemShadowOnNinePatchLayer:
  45. ui::Shadow* shadow() override;
  46. ui::Shadow shadow_;
  47. };
  48. // An implementation of `SystemShadowOnNinePatchLayer`. It is based on
  49. // ViewShadow. The ViewShadow is added in the layers beneath the view and
  50. // adjusts its content bounds with the view's bounds. Do not manually set the
  51. // content bounds.
  52. class SystemViewShadowOnNinePatchLayer : public SystemShadowOnNinePatchLayer {
  53. public:
  54. SystemViewShadowOnNinePatchLayer(views::View* view, SystemShadow::Type type);
  55. SystemViewShadowOnNinePatchLayer(const SystemViewShadowOnNinePatchLayer&) =
  56. delete;
  57. SystemViewShadowOnNinePatchLayer& operator=(
  58. const SystemViewShadowOnNinePatchLayer&) = delete;
  59. ~SystemViewShadowOnNinePatchLayer() override;
  60. // SystemShadow:
  61. void SetRoundedCornerRadius(int corner_radius) override;
  62. private:
  63. // SystemShadowOnNinePatchLayer:
  64. void SetContentBounds(const gfx::Rect& content_bounds) override;
  65. ui::Shadow* shadow() override;
  66. ViewShadow view_shadow_;
  67. };
  68. // An extension of SystemShadowOnNinePatchLayerImpl. The shadow is added at the
  69. // bottom of a window's layer and adjusts its content bounds with the window's
  70. // bounds. Do not manually set the content bounds.
  71. class SystemWindowShadowOnNinePatchLayer
  72. : public SystemShadowOnNinePatchLayerImpl,
  73. public aura::WindowObserver {
  74. public:
  75. SystemWindowShadowOnNinePatchLayer(aura::Window* window,
  76. SystemShadow::Type type);
  77. SystemWindowShadowOnNinePatchLayer(
  78. const SystemWindowShadowOnNinePatchLayer&) = delete;
  79. SystemWindowShadowOnNinePatchLayer& operator=(
  80. const SystemWindowShadowOnNinePatchLayer&) = delete;
  81. ~SystemWindowShadowOnNinePatchLayer() override;
  82. // aura::WindowObserver:
  83. void OnWindowBoundsChanged(aura::Window* window,
  84. const gfx::Rect& old_bounds,
  85. const gfx::Rect& new_bounds,
  86. ui::PropertyChangeReason reason) override;
  87. void OnWindowDestroyed(aura::Window* window) override;
  88. private:
  89. // SystemShadowOnNinePatchLayerImpl:
  90. void SetContentBounds(const gfx::Rect& content_bounds) override;
  91. base::ScopedObservation<aura::Window, aura::WindowObserver>
  92. window_observation_{this};
  93. };
  94. } // namespace ash
  95. #endif // ASH_STYLE_SYSTEM_SHADOW_ON_NINE_PATCH_LAYER_H_