1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/picture_in_picture/auto_pip_setting_overlay_view.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/animation/tween.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/background.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/flex_layout_view.h"
namespace {
constexpr float kOverlayViewOpacity = 0.60f;
// The time duration for |background_| to fade in.
constexpr int kFadeInDurationMs = 500;
} // namespace
AutoPipSettingOverlayView::AutoPipSettingOverlayView(
ResultCb result_cb,
const GURL& origin,
views::View* anchor_view,
views::BubbleBorder::Arrow arrow)
: show_timer_(std::make_unique<base::OneShotTimer>()) {
CHECK(result_cb);
init_.auto_pip_setting_view_ = std::make_unique<AutoPipSettingView>(
std::move(result_cb),
base::BindOnce(&AutoPipSettingOverlayView::OnHideView,
weak_factory_.GetWeakPtr()),
origin, anchor_view, arrow);
// Create the content setting UI.
SetLayoutManager(std::make_unique<views::FillLayout>());
SetPaintToLayer(ui::LAYER_NOT_DRAWN);
SetEventTargeter(std::make_unique<views::ViewTargeter>(this));
// Add the semi-opaque background layer.
background_ = AddChildView(views::Builder<views::View>()
.SetPaintToLayer()
.SetBackground(views::CreateSolidBackground(
kColorPipWindowBackground))
.Build());
background_->layer()->SetOpacity(0.0f);
// TODO(crbug.com/356210387): Apply blur directly to `background_` layer.
//
// Add layer to blur the web contents.
blur_view_ =
AddChildView(views::Builder<views::View>().SetPaintToLayer().Build());
blur_view_->layer()->SetBackgroundBlur(4.0f);
FadeInLayer(background_->layer());
}
void AutoPipSettingOverlayView::ShowBubble(gfx::NativeView parent) {
DCHECK(parent);
init_.auto_pip_setting_view_->set_parent_window(parent);
auto_pip_setting_view_ = init_.auto_pip_setting_view_.get();
widget_ = views::BubbleDialogDelegate::CreateBubble(
std::move(init_.auto_pip_setting_view_));
// Delay showing the bubble, for both document and video pip, until after the
// scrim animation completes.
show_timer_->Start(
FROM_HERE, base::Milliseconds(kFadeInDurationMs),
base::BindOnce(
&views::Widget::ShowInactive,
// base::Unretained() is safe since the timer is cancelled if the
// WidgetObserver notices that the widget is being destroyed.
base::Unretained(widget_)));
bubble_size_ = widget_->GetWindowBoundsInScreen().size();
widget_->AddObserver(this);
}
void AutoPipSettingOverlayView::OnHideView() {
// Hide the overlay view.
SetVisible(false);
// No longer block input events, if we were doing that.
scoped_ignore_input_events_.reset();
if (delegate_) {
delegate_->OnAutoPipSettingOverlayViewHidden();
}
}
gfx::Size AutoPipSettingOverlayView::GetBubbleSize() const {
return bubble_size_;
}
void AutoPipSettingOverlayView::FadeInLayer(ui::Layer* layer) {
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.Once()
.SetDuration(base::Milliseconds(kFadeInDurationMs))
.SetOpacity(layer, kOverlayViewOpacity, gfx::Tween::LINEAR);
}
bool AutoPipSettingOverlayView::WantsEvent(const gfx::Point& point) {
if (!auto_pip_setting_view_) {
return false;
}
// If the show timer is running, we do not want the auto pip setting overlay
// to consume the event. This is done to allow dragging the video pip window
// and prevent interacting with controls while the overlay view is pending to
// be shown.
if (IsShowTimerRunning()) {
return false;
}
return auto_pip_setting_view_->WantsEvent(point);
}
bool AutoPipSettingOverlayView::IsShowTimerRunning() {
if (show_timer_ == nullptr) {
return false;
}
return show_timer_->IsRunning();
}
AutoPipSettingOverlayView::~AutoPipSettingOverlayView() {
if (widget_) {
// If we're being deleted and our widget still exists, then ensure that it
// closes.
widget_->RemoveObserver(this);
widget_.ExtractAsDangling()->CloseWithReason(
views::Widget::ClosedReason::kUnspecified);
}
background_ = nullptr;
blur_view_ = nullptr;
auto_pip_setting_view_ = nullptr;
}
void AutoPipSettingOverlayView::OnWidgetDestroying(views::Widget*) {
show_timer_.reset();
auto_pip_setting_view_ = nullptr;
widget_->RemoveObserver(this);
widget_ = nullptr;
}
void AutoPipSettingOverlayView::IgnoreInputEvents(
content::WebContents* web_contents) {
scoped_ignore_input_events_ = web_contents->IgnoreInputEvents(std::nullopt);
}
BEGIN_METADATA(AutoPipSettingOverlayView)
END_METADATA
|