[go: nahoru, domu]

blob: 70105e506ecee396546c152d44f34ab04acf5dd2 [file] [log] [blame]
Andre Leed1afef2020-09-11 01:06:551// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/system/phonehub/continue_browsing_chip.h"
Andre Le63ca8fe2020-09-21 21:35:346
7#include "ash/public/cpp/new_window_delegate.h"
Tim Songbaea0592020-11-12 19:55:338#include "ash/resources/vector_icons/vector_icons.h"
Andre Le63ca8fe2020-09-21 21:35:349#include "ash/root_window_controller.h"
10#include "ash/shell.h"
Jimmy Gong776ab1f2020-12-03 06:16:2511#include "ash/strings/grit/ash_strings.h"
Andre Leed1afef2020-09-11 01:06:5512#include "ash/style/ash_color_provider.h"
Tim Song978248932020-10-27 19:47:4113#include "ash/system/phonehub/phone_hub_metrics.h"
Andre Le63ca8fe2020-09-21 21:35:3414#include "ash/system/phonehub/phone_hub_tray.h"
15#include "ash/system/status_area_widget.h"
Jimmy Gong776ab1f2020-12-03 06:16:2516#include "base/strings/string_number_conversions.h"
Andre Le63ca8fe2020-09-21 21:35:3417#include "base/strings/utf_string_conversions.h"
18#include "chromeos/components/multidevice/logging/logging.h"
Kyle Horimotoae02ab32020-12-11 00:35:4719#include "chromeos/components/phonehub/user_action_recorder.h"
Jimmy Gong776ab1f2020-12-03 06:16:2520#include "ui/base/l10n/l10n_util.h"
Tim Songbaea0592020-11-12 19:55:3321#include "ui/gfx/paint_vector_icon.h"
Meilin Wang9fc2c042020-10-06 22:26:5322#include "ui/views/controls/highlight_path_generator.h"
Andre Le63ca8fe2020-09-21 21:35:3423#include "ui/views/controls/image_view.h"
Andre Leed1afef2020-09-11 01:06:5524#include "ui/views/controls/label.h"
25#include "ui/views/layout/box_layout.h"
26
27namespace ash {
28
29namespace {
30
Meilin Wang24f7cf82020-10-26 23:13:1231// Appearance in dip.
32constexpr gfx::Insets kContinueBrowsingChipInsets(8, 8);
33constexpr int kContinueBrowsingChipSpacing = 8;
34constexpr int kContinueBrowsingChipFaviconSpacing = 8;
35constexpr gfx::Size kContinueBrowsingChipFaviconSize(16, 16);
36constexpr int kTaskContinuationChipRadius = 8;
Andre Le63ca8fe2020-09-21 21:35:3437constexpr int kTitleMaxLines = 2;
Andre Leed1afef2020-09-11 01:06:5538
39} // namespace
40
Andre Le63ca8fe2020-09-21 21:35:3441ContinueBrowsingChip::ContinueBrowsingChip(
Tim Song978248932020-10-27 19:47:4142 const chromeos::phonehub::BrowserTabsModel::BrowserTabMetadata& metadata,
Jimmy Gong776ab1f2020-12-03 06:16:2543 int index,
Kyle Horimotoae02ab32020-12-11 00:35:4744 size_t total_count,
45 chromeos::phonehub::UserActionRecorder* user_action_recorder)
Peter Kasting5aacfd492020-10-31 03:33:1446 : views::Button(base::BindRepeating(&ContinueBrowsingChip::ButtonPressed,
47 base::Unretained(this))),
48 url_(metadata.url),
Jimmy Gong776ab1f2020-12-03 06:16:2549 index_(index),
Kyle Horimotoae02ab32020-12-11 00:35:4750 total_count_(total_count),
51 user_action_recorder_(user_action_recorder) {
minch63a73ef2020-10-22 17:19:4552 auto* color_provider = AshColorProvider::Get();
Meilin Wang9fc2c042020-10-06 22:26:5353 SetFocusBehavior(FocusBehavior::ALWAYS);
minch63a73ef2020-10-22 17:19:4554 focus_ring()->SetColor(color_provider->GetControlsLayerColor(
55 AshColorProvider::ControlsLayerType::kFocusRingColor));
Meilin Wang9fc2c042020-10-06 22:26:5356
57 // Install this highlight path generator to set the desired shape for
58 // our focus ring.
59 views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
60 kTaskContinuationChipRadius);
61
Andre Leed1afef2020-09-11 01:06:5562 auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
Meilin Wang24f7cf82020-10-26 23:13:1263 views::BoxLayout::Orientation::kVertical, kContinueBrowsingChipInsets,
Andre Le63ca8fe2020-09-21 21:35:3464 kContinueBrowsingChipSpacing));
Meilin Wang24f7cf82020-10-26 23:13:1265 layout->set_main_axis_alignment(views::BoxLayout::MainAxisAlignment::kStart);
Andre Leed1afef2020-09-11 01:06:5566 layout->set_cross_axis_alignment(
67 views::BoxLayout::CrossAxisAlignment::kStart);
68
Meilin Wang24f7cf82020-10-26 23:13:1269 // Inits the header view which consists of the favicon image and the url.
Andre Le63ca8fe2020-09-21 21:35:3470 auto* header_view = AddChildView(std::make_unique<views::View>());
71 auto* header_layout =
72 header_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
73 views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
74 kContinueBrowsingChipFaviconSpacing));
75 header_layout->set_main_axis_alignment(
Meilin Wang24f7cf82020-10-26 23:13:1276 views::BoxLayout::MainAxisAlignment::kStart);
Andre Le63ca8fe2020-09-21 21:35:3477 header_layout->set_cross_axis_alignment(
Meilin Wang24f7cf82020-10-26 23:13:1278 views::BoxLayout::CrossAxisAlignment::kCenter);
Andre Leed1afef2020-09-11 01:06:5579
Andre Le63ca8fe2020-09-21 21:35:3480 auto* favicon =
81 header_view->AddChildView(std::make_unique<views::ImageView>());
82 favicon->SetImageSize(kContinueBrowsingChipFaviconSize);
Tim Songbaea0592020-11-12 19:55:3383
84 if (metadata.favicon.IsEmpty()) {
85 favicon->SetImage(CreateVectorIcon(
86 kPhoneHubDefaultFaviconIcon,
87 AshColorProvider::Get()->GetContentLayerColor(
88 AshColorProvider::ContentLayerType::kIconColorPrimary)));
89 } else {
90 favicon->SetImage(metadata.favicon.AsImageSkia());
91 }
Andre Le63ca8fe2020-09-21 21:35:3492
Meilin Wang24f7cf82020-10-26 23:13:1293 auto* url_label = header_view->AddChildView(
94 std::make_unique<views::Label>(base::UTF8ToUTF16(metadata.url.host())));
95 url_label->SetAutoColorReadabilityEnabled(false);
96 url_label->SetSubpixelRenderingEnabled(false);
97 url_label->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
98 AshColorProvider::ContentLayerType::kTextColorPrimary));
99 url_label->SetElideBehavior(gfx::ElideBehavior::ELIDE_TAIL);
100
Andre Le63ca8fe2020-09-21 21:35:34101 auto* title_label =
Meilin Wang24f7cf82020-10-26 23:13:12102 AddChildView(std::make_unique<views::Label>(metadata.title));
Andre Le63ca8fe2020-09-21 21:35:34103 title_label->SetAutoColorReadabilityEnabled(false);
104 title_label->SetSubpixelRenderingEnabled(false);
minch63a73ef2020-10-22 17:19:45105 title_label->SetEnabledColor(color_provider->GetContentLayerColor(
Andre Le63ca8fe2020-09-21 21:35:34106 AshColorProvider::ContentLayerType::kTextColorPrimary));
107 title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
108 title_label->SetMultiLine(true);
109 title_label->SetMaxLines(kTitleMaxLines);
Andre Le63ca8fe2020-09-21 21:35:34110 title_label->SetFontList(
111 title_label->font_list().DeriveWithWeight(gfx::Font::Weight::BOLD));
Andre Le11a9ee52020-11-11 22:42:59112
Jan Wilken Dörrie85285b02021-03-11 23:38:47113 const std::u16string card_label = l10n_util::GetStringFUTF16(
Jimmy Gong776ab1f2020-12-03 06:16:25114 IDS_ASH_PHONE_HUB_CONTINUE_BROWSING_TAB_LABEL,
115 base::NumberToString16(index_ + 1), base::NumberToString16(total_count_),
116 metadata.title, base::UTF8ToUTF16(url_.spec()));
117 SetTooltipText(card_label);
118 SetAccessibleName(card_label);
Andre Leed1afef2020-09-11 01:06:55119}
120
121void ContinueBrowsingChip::OnPaintBackground(gfx::Canvas* canvas) {
122 cc::PaintFlags flags;
123 flags.setAntiAlias(true);
124 flags.setColor(AshColorProvider::Get()->GetControlsLayerColor(
125 AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive));
126 gfx::Rect bounds = GetContentsBounds();
127 canvas->DrawRoundRect(bounds, kTaskContinuationChipRadius, flags);
128 views::View::OnPaintBackground(canvas);
129}
130
Peter Kasting5aacfd492020-10-31 03:33:14131ContinueBrowsingChip::~ContinueBrowsingChip() = default;
132
133const char* ContinueBrowsingChip::GetClassName() const {
134 return "ContinueBrowsingChip";
135}
136
137void ContinueBrowsingChip::ButtonPressed() {
Andre Le63ca8fe2020-09-21 21:35:34138 PA_LOG(INFO) << "Opening browser tab: " << url_;
Tim Song978248932020-10-27 19:47:41139 phone_hub_metrics::LogTabContinuationChipClicked(index_);
Kyle Horimotoae02ab32020-12-11 00:35:47140 user_action_recorder_->RecordBrowserTabOpened();
Tim Song978248932020-10-27 19:47:41141
Andre Le63ca8fe2020-09-21 21:35:34142 NewWindowDelegate::GetInstance()->NewTabWithUrl(
143 url_, /*from_user_interaction=*/true);
Tim Song978248932020-10-27 19:47:41144
Andre Le4a5d5cd2020-12-08 20:04:15145 // Close Phone Hub bubble in current display.
146 views::Widget* const widget = GetWidget();
147 // |widget| is null when this function is called before the view is added to a
148 // widget (in unit tests).
149 if (!widget)
150 return;
151 int64_t current_display_id =
152 display::Screen::GetScreen()
153 ->GetDisplayNearestWindow(widget->GetNativeWindow())
154 .id();
155 Shell::GetRootWindowControllerWithDisplayId(current_display_id)
Andre Le63ca8fe2020-09-21 21:35:34156 ->GetStatusAreaWidget()
157 ->phone_hub_tray()
158 ->CloseBubble();
Andre Leed1afef2020-09-11 01:06:55159}
160
Andre Leed1afef2020-09-11 01:06:55161} // namespace ash