system_shadow_on_texture_layer.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "ash/style/system_shadow_on_texture_layer.h"
  5. #include "ui/compositor/paint_recorder.h"
  6. #include "ui/gfx/skia_paint_util.h"
  7. namespace ash {
  8. SystemShadowOnTextureLayer::SystemShadowOnTextureLayer(SystemShadow::Type type)
  9. : shadow_values_(gfx::ShadowValue::MakeChromeOSSystemUIShadowValues(
  10. SystemShadow::GetElevationFromType(type))) {
  11. layer_.SetFillsBoundsOpaquely(false);
  12. layer_.set_delegate(this);
  13. layer_.SetBounds(GetLayerBounds());
  14. }
  15. SystemShadowOnTextureLayer::~SystemShadowOnTextureLayer() = default;
  16. void SystemShadowOnTextureLayer::SetType(SystemShadow::Type type) {
  17. shadow_values_ = gfx::ShadowValue::MakeChromeOSSystemUIShadowValues(
  18. SystemShadow::GetElevationFromType(type));
  19. UpdateLayer();
  20. }
  21. void SystemShadowOnTextureLayer::SetContentBounds(const gfx::Rect& bounds) {
  22. content_bounds_ = bounds;
  23. layer_.SetBounds(GetLayerBounds());
  24. UpdateLayer();
  25. }
  26. void SystemShadowOnTextureLayer::SetRoundedCornerRadius(int corner_radius) {
  27. corner_radius_ = corner_radius;
  28. UpdateLayer();
  29. }
  30. const gfx::Rect& SystemShadowOnTextureLayer::GetContentBounds() {
  31. return content_bounds_;
  32. }
  33. ui::Layer* SystemShadowOnTextureLayer::GetLayer() {
  34. return &layer_;
  35. }
  36. ui::Layer* SystemShadowOnTextureLayer::GetNinePatchLayer() {
  37. return nullptr;
  38. }
  39. gfx::Rect SystemShadowOnTextureLayer::GetLayerBounds() const {
  40. gfx::Rect layer_bounds = content_bounds_;
  41. layer_bounds.Inset(gfx::ShadowValue::GetMargin(shadow_values_));
  42. return layer_bounds;
  43. }
  44. void SystemShadowOnTextureLayer::UpdateLayer() {
  45. layer_.SchedulePaint(GetLayerBounds());
  46. }
  47. void SystemShadowOnTextureLayer::OnPaintLayer(const ui::PaintContext& context) {
  48. // Create a rounded rect of content area.
  49. const gfx::Rect r_rect_bounds =
  50. content_bounds_ - GetLayerBounds().OffsetFromOrigin();
  51. const SkRRect r_rect = SkRRect::MakeRectXY(gfx::RectToSkRect(r_rect_bounds),
  52. corner_radius_, corner_radius_);
  53. // Clip out the center.
  54. ui::PaintRecorder recorder(context, content_bounds_.size());
  55. recorder.canvas()->sk_canvas()->clipRRect(r_rect, SkClipOp::kDifference,
  56. true);
  57. // Paint shadow.
  58. cc::PaintFlags shadow_flags;
  59. shadow_flags.setAntiAlias(true);
  60. shadow_flags.setLooper(gfx::CreateShadowDrawLooper(shadow_values_));
  61. // Due to anti alias, we should fill transparent color to the rounded corner
  62. // area.
  63. shadow_flags.setColor(SK_ColorTRANSPARENT);
  64. recorder.canvas()->sk_canvas()->drawRRect(r_rect, shadow_flags);
  65. }
  66. void SystemShadowOnTextureLayer::OnDeviceScaleFactorChanged(
  67. float old_device_scale_factor,
  68. float new_device_scale_factor) {}
  69. } // namespace ash