Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Joel Hockey | 2de97cc2 | 2021-04-14 01:07:03 | [diff] [blame] | 5 | #include "components/fullscreen_control/fullscreen_control_popup.h" |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 6 | |
Sangwoo Ko | 5f2bd96b | 2019-11-22 15:44:11 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 9 | #include "base/functional/bind.h" |
Joel Hockey | 2de97cc2 | 2021-04-14 01:07:03 | [diff] [blame] | 10 | #include "components/fullscreen_control/fullscreen_control_view.h" |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 11 | #include "ui/compositor/scoped_layer_animation_settings.h" |
| 12 | #include "ui/views/widget/widget.h" |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | // Offsets with respect to the top y coordinate of the parent widget. |
| 17 | constexpr int kFinalOffset = 45; |
| 18 | |
| 19 | constexpr float kInitialOpacity = 0.1f; |
| 20 | constexpr float kFinalOpacity = 1.f; |
| 21 | |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 22 | // Creates a Widget containing an FullscreenControlView. |
Sangwoo Ko | 5f2bd96b | 2019-11-22 15:44:11 | [diff] [blame] | 23 | std::unique_ptr<views::Widget> CreatePopupWidget( |
| 24 | gfx::NativeView parent_view, |
| 25 | std::unique_ptr<FullscreenControlView> view) { |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 26 | // Initialize the popup. |
| 27 | std::unique_ptr<views::Widget> popup = std::make_unique<views::Widget>(); |
Dirk Pranke | a4e0301 | 2024-05-22 16:47:02 | [diff] [blame] | 28 | views::Widget::InitParams params( |
| 29 | views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET, |
| 30 | views::Widget::InitParams::TYPE_POPUP); |
Hwanseung Lee | 787d0aa | 2019-11-22 00:31:08 | [diff] [blame] | 31 | params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent; |
Timothy Loh | 61041707 | 2023-03-02 23:49:40 | [diff] [blame] | 32 | params.z_order = ui::ZOrderLevel::kSecuritySurface; |
Di Wu | 26533ae | 2023-05-05 01:25:46 | [diff] [blame] | 33 | params.shadow_type = views::Widget::InitParams::ShadowType::kNone; |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 34 | params.parent = parent_view; |
Ahmed Fakhry | 32f3c45 | 2019-08-01 16:36:34 | [diff] [blame] | 35 | popup->Init(std::move(params)); |
Wei Li | e409ae5 | 2020-05-21 22:32:12 | [diff] [blame] | 36 | popup->SetContentsView(std::move(view)); |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 37 | |
| 38 | return popup; |
| 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
| 43 | FullscreenControlPopup::FullscreenControlPopup( |
| 44 | gfx::NativeView parent_view, |
Yuwei Huang | 2bbad6b | 2017-11-17 02:44:55 | [diff] [blame] | 45 | const base::RepeatingClosure& on_button_pressed, |
| 46 | const base::RepeatingClosure& on_visibility_changed) |
Sangwoo Ko | 5f2bd96b | 2019-11-22 15:44:11 | [diff] [blame] | 47 | : FullscreenControlPopup( |
| 48 | CreatePopupWidget( |
| 49 | parent_view, |
| 50 | std::make_unique<FullscreenControlView>(on_button_pressed)), |
| 51 | on_visibility_changed) {} |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 52 | |
| 53 | FullscreenControlPopup::~FullscreenControlPopup() {} |
| 54 | |
Yuwei Huang | faa9754c | 2018-05-02 00:15:12 | [diff] [blame] | 55 | // static |
| 56 | int FullscreenControlPopup::GetButtonBottomOffset() { |
| 57 | return kFinalOffset + FullscreenControlView::kCircleButtonDiameter; |
| 58 | } |
| 59 | |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 60 | void FullscreenControlPopup::Show(const gfx::Rect& parent_bounds_in_screen) { |
| 61 | if (IsVisible()) |
| 62 | return; |
| 63 | |
| 64 | parent_bounds_in_screen_ = parent_bounds_in_screen; |
| 65 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 66 | animation_->SetSlideDuration(base::Milliseconds(300)); |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 67 | animation_->Show(); |
| 68 | |
| 69 | // The default animation progress is 0. Call it once here then show the popup |
| 70 | // to prevent potential flickering. |
| 71 | AnimationProgressed(animation_.get()); |
| 72 | popup_->Show(); |
| 73 | } |
| 74 | |
| 75 | void FullscreenControlPopup::Hide(bool animated) { |
| 76 | if (!IsVisible()) |
| 77 | return; |
| 78 | |
| 79 | if (animated) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 80 | animation_->SetSlideDuration(base::Milliseconds(150)); |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 81 | animation_->Hide(); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | animation_->Reset(0); |
| 86 | AnimationEnded(animation_.get()); |
| 87 | } |
| 88 | |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 89 | views::Widget* FullscreenControlPopup::GetPopupWidget() { |
| 90 | return popup_.get(); |
| 91 | } |
| 92 | |
| 93 | gfx::SlideAnimation* FullscreenControlPopup::GetAnimationForTesting() { |
| 94 | return animation_.get(); |
| 95 | } |
| 96 | |
| 97 | bool FullscreenControlPopup::IsAnimating() const { |
| 98 | return animation_->is_animating(); |
| 99 | } |
| 100 | |
| 101 | bool FullscreenControlPopup::IsVisible() const { |
| 102 | return popup_->IsVisible(); |
| 103 | } |
| 104 | |
Sangwoo Ko | 5f2bd96b | 2019-11-22 15:44:11 | [diff] [blame] | 105 | FullscreenControlPopup::FullscreenControlPopup( |
| 106 | std::unique_ptr<views::Widget> popup, |
| 107 | const base::RepeatingClosure& on_visibility_changed) |
| 108 | : AnimationDelegateViews(popup->GetRootView()), |
Sangwoo Ko | 5f2bd96b | 2019-11-22 15:44:11 | [diff] [blame] | 109 | popup_(std::move(popup)), |
Tom Sepez | 0fd8a6e | 2023-04-27 17:33:54 | [diff] [blame] | 110 | control_view_( |
| 111 | static_cast<FullscreenControlView*>(popup_->GetContentsView())), |
Sangwoo Ko | 5f2bd96b | 2019-11-22 15:44:11 | [diff] [blame] | 112 | animation_(std::make_unique<gfx::SlideAnimation>(this)), |
| 113 | on_visibility_changed_(on_visibility_changed) { |
| 114 | DCHECK(on_visibility_changed_); |
| 115 | animation_->Reset(0); |
| 116 | } |
| 117 | |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 118 | void FullscreenControlPopup::AnimationProgressed( |
| 119 | const gfx::Animation* animation) { |
| 120 | float opacity = static_cast<float>( |
| 121 | animation_->CurrentValueBetween(kInitialOpacity, kFinalOpacity)); |
| 122 | popup_->SetOpacity(opacity); |
| 123 | |
weidongliu | 87ec061e | 2024-04-05 07:54:30 | [diff] [blame] | 124 | int initial_offset = -control_view_->GetPreferredSize({}).height(); |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 125 | popup_->SetBounds(CalculateBounds( |
| 126 | animation_->CurrentValueBetween(initial_offset, kFinalOffset))); |
| 127 | } |
| 128 | |
| 129 | void FullscreenControlPopup::AnimationEnded(const gfx::Animation* animation) { |
| 130 | if (animation_->GetCurrentValue() == 0.0) { |
| 131 | // It's the end of the reversed animation. Just hide the popup in this case. |
| 132 | parent_bounds_in_screen_ = gfx::Rect(); |
| 133 | popup_->Hide(); |
Yuwei Huang | faa9754c | 2018-05-02 00:15:12 | [diff] [blame] | 134 | } else { |
| 135 | AnimationProgressed(animation); |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 136 | } |
Yuwei Huang | faa9754c | 2018-05-02 00:15:12 | [diff] [blame] | 137 | OnVisibilityChanged(); |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | gfx::Rect FullscreenControlPopup::CalculateBounds(int y_offset) const { |
| 141 | if (parent_bounds_in_screen_.IsEmpty()) |
| 142 | return gfx::Rect(); |
| 143 | |
| 144 | gfx::Point origin(parent_bounds_in_screen_.CenterPoint().x() - |
weidongliu | 87ec061e | 2024-04-05 07:54:30 | [diff] [blame] | 145 | control_view_->GetPreferredSize({}).width() / 2, |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 146 | parent_bounds_in_screen_.y() + y_offset); |
weidongliu | 87ec061e | 2024-04-05 07:54:30 | [diff] [blame] | 147 | return {origin, control_view_->GetPreferredSize({})}; |
Yuwei Huang | 6179c3d | 2017-09-28 00:36:49 | [diff] [blame] | 148 | } |
Yuwei Huang | 2bbad6b | 2017-11-17 02:44:55 | [diff] [blame] | 149 | |
| 150 | void FullscreenControlPopup::OnVisibilityChanged() { |
| 151 | on_visibility_changed_.Run(); |
| 152 | } |