#include "ash/capture_mode/capture_label_view.h"
#include "ash/capture_mode/capture_button_view.h"
#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/capture_mode/capture_mode_session.h"
#include "ash/capture_mode/capture_mode_util.h"
#include "ash/capture_mode/stop_recording_button_tray.h"
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/style/color_provider.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/number_formatting.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/task_runner.h"
#include "base/time/time.h"
#include "chromeos/constants/chromeos_features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/label.h"
#include "ui/views/highlight_border.h"
namespace ash {
namespace {
constexpr int kCaptureLabelRadius = 18;
constexpr int kCountDownStartSeconds = 3;
constexpr base::TimeDelta kCaptureLabelOpacityFadeoutDuration =
base::Milliseconds(33);
constexpr base::TimeDelta kStartCountDownDelay = base::Milliseconds(233);
constexpr base::TimeDelta kCounterFadeInDuration = base::Milliseconds(250);
constexpr base::TimeDelta kCounterFadeOutDuration = base::Milliseconds(150);
constexpr base::TimeDelta kStartCounterFadeOutDelay = base::Milliseconds(900);
constexpr base::TimeDelta kAllCountersFadeOutDelay = base::Milliseconds(850);
constexpr base::TimeDelta kWidgetFadeOutDuration = base::Milliseconds(333);
constexpr base::TimeDelta kDropToStopRecordingButtonDuration =
base::Milliseconds(500);
constexpr float kCounterInitialFadeInScale = 0.8f;
constexpr float kCounterFinalFadeOutScale = 1.2f;
constexpr float kWidgetFinalFadeOutScale = 0.8f;
}
class DropToStopRecordingButtonAnimation : public gfx::LinearAnimation {
public:
DropToStopRecordingButtonAnimation(gfx::AnimationDelegate* delegate,
const gfx::Point& start_position,
const gfx::Point& target_position)
: LinearAnimation(kDropToStopRecordingButtonDuration,
gfx::LinearAnimation::kDefaultFrameRate,
delegate),
start_position_(start_position),
target_position_(target_position) {}
DropToStopRecordingButtonAnimation(
const DropToStopRecordingButtonAnimation&) = delete;
DropToStopRecordingButtonAnimation& operator=(
const DropToStopRecordingButtonAnimation&) = delete;
~DropToStopRecordingButtonAnimation() override = default;
const gfx::Transform& current_transform() const { return current_transform_; }
void AnimateToState(double state) override {
const int new_x = gfx::Tween::IntValueBetween(
gfx::Tween::CalculateValue(gfx::Tween::FAST_OUT_LINEAR_IN, state),
start_position_.x(), target_position_.x());
const int new_y = gfx::Tween::IntValueBetween(
gfx::Tween::CalculateValue(gfx::Tween::ACCEL_30_DECEL_20_85, state),
start_position_.y(), target_position_.y());
current_transform_.MakeIdentity();
current_transform_.Translate(gfx::Point(new_x, new_y) - start_position_);
}
private:
const gfx::Point start_position_;
const gfx::Point target_position_;
gfx::Transform current_transform_;
};
CaptureLabelView::CaptureLabelView(
CaptureModeSession* capture_mode_session,
views::Button::PressedCallback on_capture_button_pressed,
views::Button::PressedCallback on_drop_down_button_pressed)
: capture_mode_session_(capture_mode_session),
shadow_(SystemShadow::CreateShadowOnTextureLayer(
SystemShadow::Type::kElevation12)) {
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
SetBackground(views::CreateThemedSolidBackground(kColorAshShieldAndBase80));
layer()->SetRoundedCornerRadius(gfx::RoundedCornersF(kCaptureLabelRadius));
layer()->SetBackgroundBlur(ColorProvider::kBackgroundBlurSigma);
layer()->SetBackdropFilterQuality(ColorProvider::kBackgroundBlurQuality);
capture_button_container_ = AddChildView(std::make_unique<CaptureButtonView>(
std::move(on_capture_button_pressed),
std::move(on_drop_down_button_pressed),
capture_mode_session_->is_in_projector_mode()));
capture_button_container_->SetPaintToLayer();
capture_button_container_->layer()->SetFillsBoundsOpaquely(false);
capture_button_container_->SetNotifyEnterExitOnChild(true);
label_ = AddChildView(std::make_unique<views::Label>(std::u16string()));
label_->SetPaintToLayer();
label_->layer()->SetFillsBoundsOpaquely(false);
label_->SetEnabledColorId(kColorAshTextColorPrimary);
label_->SetBackgroundColor(SK_ColorTRANSPARENT);
capture_mode_util::SetHighlightBorder(
this, kCaptureLabelRadius,
chromeos::features::IsJellyrollEnabled()
? views::HighlightBorder::Type::kHighlightBorderNoShadow
: views::HighlightBorder::Type::kHighlightBorder2);
shadow_->SetRoundedCornerRadius(kCaptureLabelRadius);
}
CaptureLabelView::~CaptureLabelView() = default;
bool CaptureLabelView::IsViewInteractable() const {
return capture_button_container_->GetVisible();
}
bool CaptureLabelView::IsPointOnRecordingTypeDropDownButton(
const gfx::Point& screen_location) const {
auto* drop_down_button = capture_button_container_->drop_down_button();
return drop_down_button &&
drop_down_button->GetBoundsInScreen().Contains(screen_location);
}
bool CaptureLabelView::IsRecordingTypeDropDownButtonVisible() const {
auto* drop_down_button = capture_button_container_->drop_down_button();
return capture_button_container_->GetVisible() && drop_down_button &&
drop_down_button->GetVisible();
}
void CaptureLabelView::UpdateIconAndText() {
CaptureModeController* controller = CaptureModeController::Get();
const CaptureModeSource source = controller->source();
const bool is_capturing_image = controller->type() == CaptureModeType::kImage;
const bool in_tablet_mode = TabletModeController::Get()->InTabletMode();
bool capture_button_visibility = false;
std::u16string text;
switch (source) {
case CaptureModeSource::kFullscreen:
text = l10n_util::GetStringUTF16(
is_capturing_image
? (in_tablet_mode
? IDS_ASH_SCREEN_CAPTURE_LABEL_FULLSCREEN_IMAGE_CAPTURE_TABLET
: IDS_ASH_SCREEN_CAPTURE_LABEL_FULLSCREEN_IMAGE_CAPTURE_CLAMSHELL)
: (in_tablet_mode
? IDS_ASH_SCREEN_CAPTURE_LABEL_FULLSCREEN_VIDEO_RECORD_TABLET
: IDS_ASH_SCREEN_CAPTURE_LABEL_FULLSCREEN_VIDEO_RECORD_CLAMSHELL));
break;
case CaptureModeSource::kWindow: {
if (in_tablet_mode) {
text = l10n_util::GetStringUTF16(
is_capturing_image
? IDS_ASH_SCREEN_CAPTURE_LABEL_WINDOW_IMAGE_CAPTURE
: IDS_ASH_SCREEN_CAPTURE_LABEL_WINDOW_VIDEO_RECORD);
}
break;
}
case CaptureModeSource::kRegion: {
if (!capture_mode_session_->is_selecting_region()) {
if (CaptureModeController::Get()->user_capture_region().IsEmpty()) {
text = l10n_util::GetStringUTF16(
is_capturing_image
? IDS_ASH_SCREEN_CAPTURE_LABEL_REGION_IMAGE_CAPTURE
: IDS_ASH_SCREEN_CAPTURE_LABEL_REGION_VIDEO_RECORD);
} else {
capture_button_visibility = true;
}
}
break;
}
}
capture_button_container_->SetVisible(capture_button_visibility);
if (capture_button_visibility)
capture_button_container_->UpdateViewVisuals();
const bool label_visibility = !text.empty();
label_->SetVisible(label_visibility);
if (label_visibility)
label_->SetText(text);
}
bool CaptureLabelView::ShouldHandleEvent() {
return IsViewInteractable() && !IsInCountDownAnimation();
}
void CaptureLabelView::StartCountDown(
base::OnceClosure countdown_finished_callback) {
countdown_finished_callback_ = std::move(countdown_finished_callback);
ui::Layer* animation_layer = nullptr;
if (capture_button_container_->GetVisible())
animation_layer = capture_button_container_->layer();
else if (label_->GetVisible())
animation_layer = label_->layer();
if (animation_layer) {
animation_layer->SetOpacity(1.f);
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.Once()
.SetDuration(kCaptureLabelOpacityFadeoutDuration)
.SetOpacity(animation_layer, 0.f);
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&CaptureLabelView::FadeInAndOutCounter,
weak_factory_.GetWeakPtr(), kCountDownStartSeconds),
kStartCountDownDelay);
}
bool CaptureLabelView::IsInCountDownAnimation() const {
return !!countdown_finished_callback_;
}
void CaptureLabelView::AddedToWidget() {
auto* parent = layer()->parent();
parent->Add(shadow_->GetLayer());
parent->StackAtBottom(shadow_->GetLayer());
}
void CaptureLabelView::Layout() {
gfx::Rect label_bounds = GetLocalBounds();
capture_button_container_->SetBoundsRect(label_bounds);
label_bounds.ClampToCenteredSize(label_->GetPreferredSize());
label_->SetBoundsRect(label_bounds);
views::View::Layout();
shadow_->SetContentBounds(layer()->bounds());
}
gfx::Size CaptureLabelView::CalculatePreferredSize() const {
if (countdown_finished_callback_)
return gfx::Size(kCaptureLabelRadius * 2, kCaptureLabelRadius * 2);
const bool is_label_button_visible = capture_button_container_->GetVisible();
const bool is_label_visible = label_->GetVisible();
if (!is_label_button_visible && !is_label_visible)
return gfx::Size();
if (is_label_button_visible) {
DCHECK(!is_label_visible);
return gfx::Size(capture_button_container_->GetPreferredSize().width(),
kCaptureLabelRadius * 2);
}
DCHECK(is_label_visible && !is_label_button_visible);
return gfx::Size(label_->GetPreferredSize().width() + kCaptureLabelRadius * 2,
kCaptureLabelRadius * 2);
}
void CaptureLabelView::OnThemeChanged() {
views::View::OnThemeChanged();
UpdateIconAndText();
}
void CaptureLabelView::AnimationEnded(const gfx::Animation* animation) {
DCHECK_EQ(drop_to_stop_button_animation_.get(), animation);
OnCountDownAnimationFinished();
}
void CaptureLabelView::AnimationProgressed(const gfx::Animation* animation) {
DCHECK_EQ(drop_to_stop_button_animation_.get(), animation);
GetWidget()->GetLayer()->SetTransform(
drop_to_stop_button_animation_->current_transform());
}
void CaptureLabelView::FadeInAndOutCounter(int counter_value) {
if (counter_value == 0) {
DropWidgetToStopRecordingButton();
return;
}
label_->SetVisible(true);
label_->SetText(base::FormatNumber(counter_value));
Layout();
ui::Layer* layer = label_->layer();
layer->SetOpacity(0.f);
layer->SetTransform(capture_mode_util::GetScaleTransformAboutCenter(
layer, kCounterInitialFadeInScale));
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnEnded(base::BindOnce(&CaptureLabelView::FadeInAndOutCounter,
weak_factory_.GetWeakPtr(), counter_value - 1))
.Once()
.SetDuration(kCounterFadeInDuration)
.SetOpacity(layer, 1.f)
.SetTransform(layer, gfx::Transform(), gfx::Tween::LINEAR_OUT_SLOW_IN)
.At(counter_value == kCountDownStartSeconds ? kStartCounterFadeOutDelay
: kAllCountersFadeOutDelay)
.SetDuration(kCounterFadeOutDuration)
.SetOpacity(layer, 0.f)
.SetTransform(layer,
capture_mode_util::GetScaleTransformAboutCenter(
layer, kCounterFinalFadeOutScale),
gfx::Tween::FAST_OUT_LINEAR_IN);
}
void CaptureLabelView::DropWidgetToStopRecordingButton() {
auto* widget_window = GetWidget()->GetNativeWindow();
StopRecordingButtonTray* stop_recording_button =
capture_mode_util::GetStopRecordingButtonForRoot(
widget_window->GetRootWindow());
if (!stop_recording_button) {
FadeOutWidget();
return;
}
stop_recording_button->SetVisible(true);
stop_recording_button->UpdateLayout();
const auto target_position =
stop_recording_button->GetBoundsInScreen().origin();
stop_recording_button->SetVisible(false);
drop_to_stop_button_animation_ =
std::make_unique<DropToStopRecordingButtonAnimation>(
this, widget_window->GetBoundsInScreen().origin(), target_position);
drop_to_stop_button_animation_->Start();
}
void CaptureLabelView::FadeOutWidget() {
const auto tween = gfx::Tween::EASE_OUT_3;
auto* widget_layer = GetWidget()->GetLayer();
views::AnimationBuilder builder;
builder
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnEnded(base::BindOnce(&CaptureLabelView::OnCountDownAnimationFinished,
weak_factory_.GetWeakPtr()))
.Once()
.At(kCounterFadeInDuration + kAllCountersFadeOutDelay)
.SetDuration(kWidgetFadeOutDuration)
.SetOpacity(widget_layer, 0.f, tween)
.SetTransform(widget_layer,
capture_mode_util::GetScaleTransformAboutCenter(
widget_layer, kWidgetFinalFadeOutScale),
tween);
}
void CaptureLabelView::OnCountDownAnimationFinished() {
DCHECK(countdown_finished_callback_);
std::move(countdown_finished_callback_).Run();
}
BEGIN_METADATA(CaptureLabelView, views::View)
END_METADATA
}