#include "ash/capture_mode/action_button_view.h"
#include <memory>
#include <string>
#include <utility>
#include "ash/capture_mode/capture_mode_session_focus_cycler.h"
#include "ash/capture_mode/capture_mode_types.h"
#include "ash/capture_mode/capture_mode_util.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/style_util.h"
#include "ash/style/system_shadow.h"
#include "ash/style/typography.h"
#include "base/time/time.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/gfx/animation/tween.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/highlight_border.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
constexpr auto kFullActionButtonInsets = gfx::Insets::TLBR(8, 12, 8, 16);
constexpr auto kTextOnlyActionButtonInsets = gfx::Insets::VH(8, 16);
constexpr auto kCollapsedActionButtonInsets = gfx::Insets(8);
constexpr int kActionButtonIconLabelSpacing = 8;
constexpr int kActionButtonRadius = 18;
constexpr int kActionButtonIconSize = 20;
}
ActionButtonView::ActionButtonView(views::Button::PressedCallback callback,
std::u16string text,
const gfx::VectorIcon* icon,
ActionButtonRank rank)
: views::Button(std::move(callback)),
rank_(rank),
shadow_(SystemShadow::CreateShadowOnTextureLayer(
SystemShadow::Type::kElevation12)) {
box_layout_ = SetLayoutManager(
icon ? std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal,
kFullActionButtonInsets, kActionButtonIconLabelSpacing)
: std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal,
kTextOnlyActionButtonInsets));
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
SetBackground(views::CreateRoundedRectBackground(
cros_tokens::kCrosSysSystemBaseElevated, kActionButtonRadius));
shadow_->SetRoundedCornerRadius(kActionButtonRadius);
capture_mode_util::SetHighlightBorder(
this, kActionButtonRadius,
views::HighlightBorder::Type::kHighlightBorderNoShadow);
views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
kActionButtonRadius);
StyleUtil::ConfigureInkDropAttributes(
this, StyleUtil::kBaseColor | StyleUtil::kInkDropOpacity);
StyleUtil::SetUpInkDropForButton(this);
ink_drop_container_ =
AddChildView(std::make_unique<views::InkDropContainerView>());
ink_drop_container_->SetAutoMatchParentBounds(true);
if (icon) {
image_view_ = AddChildView(
std::make_unique<views::ImageView>(ui::ImageModel::FromVectorIcon(
*icon, kColorAshButtonIconColor, kActionButtonIconSize)));
}
label_ = AddChildView(std::make_unique<views::Label>(text));
TypographyProvider::Get()->StyleLabel(TypographyToken::kCrosButton2, *label_);
CaptureModeSessionFocusCycler::HighlightHelper::Install(this);
SetAccessibleName(text);
}
ActionButtonView::~ActionButtonView() {
views::InkDrop::Remove(this);
}
void ActionButtonView::AddedToWidget() {
views::Button::AddedToWidget();
auto* parent = layer()->parent();
ui::Layer* shadow_layer = shadow_->GetLayer();
parent->Add(shadow_layer);
parent->StackAtBottom(shadow_layer);
shadow_->ObserveColorProviderSource(GetWidget());
}
void ActionButtonView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
shadow_->SetContentBounds(layer()->bounds());
}
void ActionButtonView::AddLayerToRegion(ui::Layer* layer,
views::LayerRegion region) {
ink_drop_container_->AddLayerToRegion(layer, region);
}
void ActionButtonView::RemoveLayerFromRegions(ui::Layer* layer) {
ink_drop_container_->RemoveLayerFromRegions(layer);
}
void ActionButtonView::CollapseToIconButton() {
if (!label_->GetVisible()) {
return;
}
label_->SetVisible(false);
const std::u16string label_text(label_->GetText());
label_->SetTooltipText(label_text);
box_layout_->set_inside_border_insets(kCollapsedActionButtonInsets);
}
void ActionButtonView::PerformFadeInAnimation(
base::TimeDelta fade_in_duration) {
CHECK(layer());
layer()->SetOpacity(0.0f);
shadow_->GetLayer()->SetOpacity(0.0f);
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.Once()
.SetDuration(fade_in_duration)
.SetOpacity(layer(), 1.0f, gfx::Tween::LINEAR)
.SetOpacity(shadow_->GetLayer(), 1.0f, gfx::Tween::LINEAR);
}
BEGIN_METADATA(ActionButtonView)
END_METADATA
}