view_shadow.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2019 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/public/cpp/view_shadow.h"
  5. #include "ui/compositor/layer.h"
  6. #include "ui/compositor_extra/shadow.h"
  7. #include "ui/views/view.h"
  8. namespace ash {
  9. ViewShadow::ViewShadow(views::View* view, int elevation)
  10. : view_(view), shadow_(std::make_unique<ui::Shadow>()) {
  11. if (!view_->layer())
  12. view_->SetPaintToLayer();
  13. shadow_->Init(elevation);
  14. view_->AddLayerBeneathView(shadow_->layer());
  15. shadow_->SetContentBounds(view_->layer()->bounds());
  16. view_->AddObserver(this);
  17. shadow_->AddObserver(this);
  18. }
  19. ViewShadow::~ViewShadow() {
  20. if (view_)
  21. OnViewIsDeleting(view_);
  22. }
  23. void ViewShadow::SetRoundedCornerRadius(int corner_radius) {
  24. if (!view_)
  25. return;
  26. view_->layer()->SetRoundedCornerRadius(gfx::RoundedCornersF(corner_radius));
  27. shadow_->SetRoundedCornerRadius(corner_radius);
  28. }
  29. void ViewShadow::OnLayerRecreated(ui::Layer* old_layer) {
  30. if (!view_)
  31. return;
  32. view_->RemoveLayerBeneathViewKeepInLayerTree(old_layer);
  33. view_->AddLayerBeneathView(shadow_->layer());
  34. }
  35. void ViewShadow::OnLayerTargetBoundsChanged(views::View* view) {
  36. shadow_->SetContentBounds(view->layer()->bounds());
  37. }
  38. void ViewShadow::OnViewIsDeleting(views::View* view) {
  39. shadow_->RemoveObserver(this);
  40. shadow_.reset();
  41. view_->RemoveObserver(this);
  42. view_ = nullptr;
  43. }
  44. } // namespace ash