lock_screen_action_background_view.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2017 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/lock_screen_action/lock_screen_action_background_view.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/i18n/rtl.h"
  10. #include "ui/gfx/geometry/point.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. #include "ui/gfx/geometry/size.h"
  13. #include "ui/views/animation/flood_fill_ink_drop_ripple.h"
  14. #include "ui/views/animation/ink_drop.h"
  15. #include "ui/views/animation/ink_drop_host_view.h"
  16. #include "ui/views/animation/ink_drop_impl.h"
  17. #include "ui/views/animation/ink_drop_ripple.h"
  18. #include "ui/views/animation/ink_drop_state.h"
  19. #include "ui/views/animation/square_ink_drop_ripple.h"
  20. #include "ui/views/layout/box_layout.h"
  21. namespace ash {
  22. class LockScreenActionBackgroundView::NoteBackground : public views::View {
  23. public:
  24. explicit NoteBackground(views::InkDropObserver* observer)
  25. : observer_(observer) {
  26. DCHECK(observer);
  27. views::InkDrop::Install(this, std::make_unique<views::InkDropHost>(this));
  28. views::InkDrop::Get(this)->SetMode(
  29. views::InkDropHost::InkDropMode::ON_NO_GESTURE_HANDLER);
  30. views::InkDrop::Get(this)->SetCreateInkDropCallback(base::BindRepeating(
  31. [](NoteBackground* host) {
  32. std::unique_ptr<views::InkDrop> ink_drop =
  33. views::InkDrop::CreateInkDropWithoutAutoHighlight(
  34. views::InkDrop::Get(host), /*highlight_on_hover=*/false);
  35. ink_drop->AddObserver(host->observer_);
  36. return ink_drop;
  37. },
  38. this));
  39. views::InkDrop::Get(this)->SetCreateRippleCallback(base::BindRepeating(
  40. [](NoteBackground* host) -> std::unique_ptr<views::InkDropRipple> {
  41. const gfx::Point center = base::i18n::IsRTL()
  42. ? host->GetLocalBounds().origin()
  43. : host->GetLocalBounds().top_right();
  44. auto ink_drop_ripple =
  45. std::make_unique<views::FloodFillInkDropRipple>(
  46. host->size(), gfx::Insets(), center,
  47. views::InkDrop::Get(host)->GetBaseColor(), 1);
  48. ink_drop_ripple->set_use_hide_transform_duration_for_hide_fade_out(
  49. true);
  50. ink_drop_ripple->set_duration_factor(1.5);
  51. return ink_drop_ripple;
  52. },
  53. this));
  54. views::InkDrop::Get(this)->SetBaseColor(SK_ColorBLACK);
  55. }
  56. NoteBackground(const NoteBackground&) = delete;
  57. NoteBackground& operator=(const NoteBackground&) = delete;
  58. ~NoteBackground() override = default;
  59. private:
  60. views::InkDropObserver* observer_;
  61. };
  62. LockScreenActionBackgroundView::LockScreenActionBackgroundView() {
  63. SetCanMaximize(true);
  64. auto layout_manager = std::make_unique<views::BoxLayout>(
  65. views::BoxLayout::Orientation::kVertical);
  66. layout_manager->set_cross_axis_alignment(
  67. views::BoxLayout::CrossAxisAlignment::kStretch);
  68. auto* layout_ptr = SetLayoutManager(std::move(layout_manager));
  69. background_ = new NoteBackground(this);
  70. AddChildView(background_);
  71. // Make background view flexible - the constant does not really matter given
  72. // that |background_| is the only child, as long as it's greater than 0.
  73. layout_ptr->SetFlexForView(background_, 1 /*flex_weight*/);
  74. }
  75. LockScreenActionBackgroundView::~LockScreenActionBackgroundView() = default;
  76. void LockScreenActionBackgroundView::AnimateShow(base::OnceClosure done) {
  77. animation_end_callback_ = std::move(done);
  78. animating_to_state_ = views::InkDropState::ACTIVATED;
  79. views::InkDrop::Get(background_)
  80. ->AnimateToState(views::InkDropState::ACTIVATED, nullptr);
  81. }
  82. void LockScreenActionBackgroundView::AnimateHide(base::OnceClosure done) {
  83. animation_end_callback_ = std::move(done);
  84. animating_to_state_ = views::InkDropState::HIDDEN;
  85. views::InkDrop::Get(background_)
  86. ->AnimateToState(views::InkDropState::HIDDEN, nullptr);
  87. }
  88. void LockScreenActionBackgroundView::InkDropAnimationStarted() {}
  89. void LockScreenActionBackgroundView::InkDropRippleAnimationEnded(
  90. views::InkDropState state) {
  91. // In case |AnimateShow| or |AnimateHide| is called before previous state
  92. // animation ends, this might get called with the previous target state
  93. // as the animation is aborted - ignore the event if the |state| does not
  94. // match the current target state.
  95. if (animation_end_callback_.is_null() || state != animating_to_state_)
  96. return;
  97. std::move(animation_end_callback_).Run();
  98. }
  99. bool LockScreenActionBackgroundView::CanActivate() const {
  100. return false;
  101. }
  102. views::View* LockScreenActionBackgroundView::GetBackgroundView() {
  103. return background_;
  104. }
  105. } // namespace ash