fullscreen_control_popup.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "components/fullscreen_control/fullscreen_control_popup.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "components/fullscreen_control/fullscreen_control_view.h"
  8. #include "ui/compositor/scoped_layer_animation_settings.h"
  9. #include "ui/views/widget/widget.h"
  10. namespace {
  11. // Offsets with respect to the top y coordinate of the parent widget.
  12. constexpr int kFinalOffset = 45;
  13. constexpr float kInitialOpacity = 0.1f;
  14. constexpr float kFinalOpacity = 1.f;
  15. // Creates a Widget containing an FullscreenControlView.
  16. std::unique_ptr<views::Widget> CreatePopupWidget(
  17. gfx::NativeView parent_view,
  18. std::unique_ptr<FullscreenControlView> view) {
  19. // Initialize the popup.
  20. std::unique_ptr<views::Widget> popup = std::make_unique<views::Widget>();
  21. views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
  22. params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
  23. params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  24. params.parent = parent_view;
  25. popup->Init(std::move(params));
  26. popup->SetContentsView(std::move(view));
  27. return popup;
  28. }
  29. } // namespace
  30. FullscreenControlPopup::FullscreenControlPopup(
  31. gfx::NativeView parent_view,
  32. const base::RepeatingClosure& on_button_pressed,
  33. const base::RepeatingClosure& on_visibility_changed)
  34. : FullscreenControlPopup(
  35. CreatePopupWidget(
  36. parent_view,
  37. std::make_unique<FullscreenControlView>(on_button_pressed)),
  38. on_visibility_changed) {}
  39. FullscreenControlPopup::~FullscreenControlPopup() {}
  40. // static
  41. int FullscreenControlPopup::GetButtonBottomOffset() {
  42. return kFinalOffset + FullscreenControlView::kCircleButtonDiameter;
  43. }
  44. void FullscreenControlPopup::Show(const gfx::Rect& parent_bounds_in_screen) {
  45. if (IsVisible())
  46. return;
  47. parent_bounds_in_screen_ = parent_bounds_in_screen;
  48. animation_->SetSlideDuration(base::Milliseconds(300));
  49. animation_->Show();
  50. // The default animation progress is 0. Call it once here then show the popup
  51. // to prevent potential flickering.
  52. AnimationProgressed(animation_.get());
  53. popup_->Show();
  54. }
  55. void FullscreenControlPopup::Hide(bool animated) {
  56. if (!IsVisible())
  57. return;
  58. if (animated) {
  59. animation_->SetSlideDuration(base::Milliseconds(150));
  60. animation_->Hide();
  61. return;
  62. }
  63. animation_->Reset(0);
  64. AnimationEnded(animation_.get());
  65. }
  66. views::Widget* FullscreenControlPopup::GetPopupWidget() {
  67. return popup_.get();
  68. }
  69. gfx::SlideAnimation* FullscreenControlPopup::GetAnimationForTesting() {
  70. return animation_.get();
  71. }
  72. bool FullscreenControlPopup::IsAnimating() const {
  73. return animation_->is_animating();
  74. }
  75. bool FullscreenControlPopup::IsVisible() const {
  76. return popup_->IsVisible();
  77. }
  78. FullscreenControlPopup::FullscreenControlPopup(
  79. std::unique_ptr<views::Widget> popup,
  80. const base::RepeatingClosure& on_visibility_changed)
  81. : AnimationDelegateViews(popup->GetRootView()),
  82. control_view_(
  83. static_cast<FullscreenControlView*>(popup->GetContentsView())),
  84. popup_(std::move(popup)),
  85. animation_(std::make_unique<gfx::SlideAnimation>(this)),
  86. on_visibility_changed_(on_visibility_changed) {
  87. DCHECK(on_visibility_changed_);
  88. animation_->Reset(0);
  89. }
  90. void FullscreenControlPopup::AnimationProgressed(
  91. const gfx::Animation* animation) {
  92. float opacity = static_cast<float>(
  93. animation_->CurrentValueBetween(kInitialOpacity, kFinalOpacity));
  94. popup_->SetOpacity(opacity);
  95. int initial_offset = -control_view_->GetPreferredSize().height();
  96. popup_->SetBounds(CalculateBounds(
  97. animation_->CurrentValueBetween(initial_offset, kFinalOffset)));
  98. }
  99. void FullscreenControlPopup::AnimationEnded(const gfx::Animation* animation) {
  100. if (animation_->GetCurrentValue() == 0.0) {
  101. // It's the end of the reversed animation. Just hide the popup in this case.
  102. parent_bounds_in_screen_ = gfx::Rect();
  103. popup_->Hide();
  104. } else {
  105. AnimationProgressed(animation);
  106. }
  107. OnVisibilityChanged();
  108. }
  109. gfx::Rect FullscreenControlPopup::CalculateBounds(int y_offset) const {
  110. if (parent_bounds_in_screen_.IsEmpty())
  111. return gfx::Rect();
  112. gfx::Point origin(parent_bounds_in_screen_.CenterPoint().x() -
  113. control_view_->GetPreferredSize().width() / 2,
  114. parent_bounds_in_screen_.y() + y_offset);
  115. return {origin, control_view_->GetPreferredSize()};
  116. }
  117. void FullscreenControlPopup::OnVisibilityChanged() {
  118. on_visibility_changed_.Run();
  119. }