blob: 0cda8a9cbc14ac69b95bd8be45c1101f471915af [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2017 The Chromium Authors
Yuwei Huang6179c3d2017-09-28 00:36:492// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Joel Hockey2de97cc22021-04-14 01:07:035#include "components/fullscreen_control/fullscreen_control_popup.h"
Yuwei Huang6179c3d2017-09-28 00:36:496
Sangwoo Ko5f2bd96b2019-11-22 15:44:117#include <memory>
8
Avi Drissman12be0312023-01-11 09:16:099#include "base/functional/bind.h"
Joel Hockey2de97cc22021-04-14 01:07:0310#include "components/fullscreen_control/fullscreen_control_view.h"
Yuwei Huang6179c3d2017-09-28 00:36:4911#include "ui/compositor/scoped_layer_animation_settings.h"
12#include "ui/views/widget/widget.h"
13
14namespace {
15
16// Offsets with respect to the top y coordinate of the parent widget.
17constexpr int kFinalOffset = 45;
18
19constexpr float kInitialOpacity = 0.1f;
20constexpr float kFinalOpacity = 1.f;
21
Yuwei Huang6179c3d2017-09-28 00:36:4922// Creates a Widget containing an FullscreenControlView.
Sangwoo Ko5f2bd96b2019-11-22 15:44:1123std::unique_ptr<views::Widget> CreatePopupWidget(
24 gfx::NativeView parent_view,
25 std::unique_ptr<FullscreenControlView> view) {
Yuwei Huang6179c3d2017-09-28 00:36:4926 // Initialize the popup.
27 std::unique_ptr<views::Widget> popup = std::make_unique<views::Widget>();
Dirk Prankea4e03012024-05-22 16:47:0228 views::Widget::InitParams params(
29 views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
30 views::Widget::InitParams::TYPE_POPUP);
Hwanseung Lee787d0aa2019-11-22 00:31:0831 params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
Timothy Loh610417072023-03-02 23:49:4032 params.z_order = ui::ZOrderLevel::kSecuritySurface;
Di Wu26533ae2023-05-05 01:25:4633 params.shadow_type = views::Widget::InitParams::ShadowType::kNone;
Yuwei Huang6179c3d2017-09-28 00:36:4934 params.parent = parent_view;
Ahmed Fakhry32f3c452019-08-01 16:36:3435 popup->Init(std::move(params));
Wei Lie409ae52020-05-21 22:32:1236 popup->SetContentsView(std::move(view));
Yuwei Huang6179c3d2017-09-28 00:36:4937
38 return popup;
39}
40
41} // namespace
42
43FullscreenControlPopup::FullscreenControlPopup(
44 gfx::NativeView parent_view,
Yuwei Huang2bbad6b2017-11-17 02:44:5545 const base::RepeatingClosure& on_button_pressed,
46 const base::RepeatingClosure& on_visibility_changed)
Sangwoo Ko5f2bd96b2019-11-22 15:44:1147 : FullscreenControlPopup(
48 CreatePopupWidget(
49 parent_view,
50 std::make_unique<FullscreenControlView>(on_button_pressed)),
51 on_visibility_changed) {}
Yuwei Huang6179c3d2017-09-28 00:36:4952
53FullscreenControlPopup::~FullscreenControlPopup() {}
54
Yuwei Huangfaa9754c2018-05-02 00:15:1255// static
56int FullscreenControlPopup::GetButtonBottomOffset() {
57 return kFinalOffset + FullscreenControlView::kCircleButtonDiameter;
58}
59
Yuwei Huang6179c3d2017-09-28 00:36:4960void 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 Kastinge5a38ed2021-10-02 03:06:3566 animation_->SetSlideDuration(base::Milliseconds(300));
Yuwei Huang6179c3d2017-09-28 00:36:4967 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
75void FullscreenControlPopup::Hide(bool animated) {
76 if (!IsVisible())
77 return;
78
79 if (animated) {
Peter Kastinge5a38ed2021-10-02 03:06:3580 animation_->SetSlideDuration(base::Milliseconds(150));
Yuwei Huang6179c3d2017-09-28 00:36:4981 animation_->Hide();
82 return;
83 }
84
85 animation_->Reset(0);
86 AnimationEnded(animation_.get());
87}
88
Yuwei Huang6179c3d2017-09-28 00:36:4989views::Widget* FullscreenControlPopup::GetPopupWidget() {
90 return popup_.get();
91}
92
93gfx::SlideAnimation* FullscreenControlPopup::GetAnimationForTesting() {
94 return animation_.get();
95}
96
97bool FullscreenControlPopup::IsAnimating() const {
98 return animation_->is_animating();
99}
100
101bool FullscreenControlPopup::IsVisible() const {
102 return popup_->IsVisible();
103}
104
Sangwoo Ko5f2bd96b2019-11-22 15:44:11105FullscreenControlPopup::FullscreenControlPopup(
106 std::unique_ptr<views::Widget> popup,
107 const base::RepeatingClosure& on_visibility_changed)
108 : AnimationDelegateViews(popup->GetRootView()),
Sangwoo Ko5f2bd96b2019-11-22 15:44:11109 popup_(std::move(popup)),
Tom Sepez0fd8a6e2023-04-27 17:33:54110 control_view_(
111 static_cast<FullscreenControlView*>(popup_->GetContentsView())),
Sangwoo Ko5f2bd96b2019-11-22 15:44:11112 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 Huang6179c3d2017-09-28 00:36:49118void FullscreenControlPopup::AnimationProgressed(
119 const gfx::Animation* animation) {
120 float opacity = static_cast<float>(
121 animation_->CurrentValueBetween(kInitialOpacity, kFinalOpacity));
122 popup_->SetOpacity(opacity);
123
weidongliu87ec061e2024-04-05 07:54:30124 int initial_offset = -control_view_->GetPreferredSize({}).height();
Yuwei Huang6179c3d2017-09-28 00:36:49125 popup_->SetBounds(CalculateBounds(
126 animation_->CurrentValueBetween(initial_offset, kFinalOffset)));
127}
128
129void 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 Huangfaa9754c2018-05-02 00:15:12134 } else {
135 AnimationProgressed(animation);
Yuwei Huang6179c3d2017-09-28 00:36:49136 }
Yuwei Huangfaa9754c2018-05-02 00:15:12137 OnVisibilityChanged();
Yuwei Huang6179c3d2017-09-28 00:36:49138}
139
140gfx::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() -
weidongliu87ec061e2024-04-05 07:54:30145 control_view_->GetPreferredSize({}).width() / 2,
Yuwei Huang6179c3d2017-09-28 00:36:49146 parent_bounds_in_screen_.y() + y_offset);
weidongliu87ec061e2024-04-05 07:54:30147 return {origin, control_view_->GetPreferredSize({})};
Yuwei Huang6179c3d2017-09-28 00:36:49148}
Yuwei Huang2bbad6b2017-11-17 02:44:55149
150void FullscreenControlPopup::OnVisibilityChanged() {
151 on_visibility_changed_.Run();
152}