blob: 1c008745cb4bf4d9cd8aea9ecb1b6af57d233a14 [file] [log] [blame]
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/phonehub/continue_browsing_chip.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/style/ash_color_provider.h"
#include "ash/system/phonehub/phone_hub_metrics.h"
#include "ash/system/phonehub/phone_hub_tray.h"
#include "ash/system/status_area_widget.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/components/multidevice/logging/logging.h"
#include "ui/gfx/paint_vector_icon.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/layout/box_layout.h"
namespace ash {
namespace {
// Appearance in dip.
constexpr gfx::Insets kContinueBrowsingChipInsets(8, 8);
constexpr int kContinueBrowsingChipSpacing = 8;
constexpr int kContinueBrowsingChipFaviconSpacing = 8;
constexpr gfx::Size kContinueBrowsingChipFaviconSize(16, 16);
constexpr int kTaskContinuationChipRadius = 8;
constexpr int kTitleMaxLines = 2;
} // namespace
ContinueBrowsingChip::ContinueBrowsingChip(
const chromeos::phonehub::BrowserTabsModel::BrowserTabMetadata& metadata,
int index)
: views::Button(base::BindRepeating(&ContinueBrowsingChip::ButtonPressed,
base::Unretained(this))),
url_(metadata.url),
index_(index) {
auto* color_provider = AshColorProvider::Get();
SetFocusBehavior(FocusBehavior::ALWAYS);
focus_ring()->SetColor(color_provider->GetControlsLayerColor(
AshColorProvider::ControlsLayerType::kFocusRingColor));
// Install this highlight path generator to set the desired shape for
// our focus ring.
views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
kTaskContinuationChipRadius);
auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical, kContinueBrowsingChipInsets,
kContinueBrowsingChipSpacing));
layout->set_main_axis_alignment(views::BoxLayout::MainAxisAlignment::kStart);
layout->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kStart);
// Inits the header view which consists of the favicon image and the url.
auto* header_view = AddChildView(std::make_unique<views::View>());
auto* header_layout =
header_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
kContinueBrowsingChipFaviconSpacing));
header_layout->set_main_axis_alignment(
views::BoxLayout::MainAxisAlignment::kStart);
header_layout->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kCenter);
auto* favicon =
header_view->AddChildView(std::make_unique<views::ImageView>());
favicon->SetImageSize(kContinueBrowsingChipFaviconSize);
if (metadata.favicon.IsEmpty()) {
favicon->SetImage(CreateVectorIcon(
kPhoneHubDefaultFaviconIcon,
AshColorProvider::Get()->GetContentLayerColor(
AshColorProvider::ContentLayerType::kIconColorPrimary)));
} else {
favicon->SetImage(metadata.favicon.AsImageSkia());
}
auto* url_label = header_view->AddChildView(
std::make_unique<views::Label>(base::UTF8ToUTF16(metadata.url.host())));
url_label->SetAutoColorReadabilityEnabled(false);
url_label->SetSubpixelRenderingEnabled(false);
url_label->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
AshColorProvider::ContentLayerType::kTextColorPrimary));
url_label->SetElideBehavior(gfx::ElideBehavior::ELIDE_TAIL);
auto* title_label =
AddChildView(std::make_unique<views::Label>(metadata.title));
title_label->SetAutoColorReadabilityEnabled(false);
title_label->SetSubpixelRenderingEnabled(false);
title_label->SetEnabledColor(color_provider->GetContentLayerColor(
AshColorProvider::ContentLayerType::kTextColorPrimary));
title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
title_label->SetMultiLine(true);
title_label->SetMaxLines(kTitleMaxLines);
title_label->SetFontList(
title_label->font_list().DeriveWithWeight(gfx::Font::Weight::BOLD));
SetTooltipText(metadata.title);
}
void ContinueBrowsingChip::OnPaintBackground(gfx::Canvas* canvas) {
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setColor(AshColorProvider::Get()->GetControlsLayerColor(
AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive));
gfx::Rect bounds = GetContentsBounds();
canvas->DrawRoundRect(bounds, kTaskContinuationChipRadius, flags);
views::View::OnPaintBackground(canvas);
}
ContinueBrowsingChip::~ContinueBrowsingChip() = default;
const char* ContinueBrowsingChip::GetClassName() const {
return "ContinueBrowsingChip";
}
void ContinueBrowsingChip::ButtonPressed() {
PA_LOG(INFO) << "Opening browser tab: " << url_;
phone_hub_metrics::LogTabContinuationChipClicked(index_);
NewWindowDelegate::GetInstance()->NewTabWithUrl(
url_, /*from_user_interaction=*/true);
Shell::GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->phone_hub_tray()
->CloseBubble();
}
} // namespace ash