[go: nahoru, domu]

blob: 8984d6f857cd4f67b6017d8f003187f8a4705e83 [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2020 The Chromium Authors
Andre Leed1afef2020-09-11 01:06:552// 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/task_continuation_view.h"
6
7#include "ash/strings/grit/ash_strings.h"
8#include "ash/style/ash_color_provider.h"
Crisrael Lucerof1392ae2023-05-12 23:17:459#include "ash/style/typography.h"
Andre Leed1afef2020-09-11 01:06:5510#include "ash/system/phonehub/continue_browsing_chip.h"
Meilin Wange95457a82020-09-24 02:51:5511#include "ash/system/phonehub/phone_hub_view_ids.h"
Meilin Wangc14a36f2020-10-31 00:17:5112#include "ash/system/phonehub/ui_constants.h"
13#include "ash/system/tray/tray_constants.h"
Crisrael Lucerof1392ae2023-05-12 23:17:4514#include "chromeos/constants/chromeos_features.h"
Andre Leed1afef2020-09-11 01:06:5515#include "ui/base/l10n/l10n_util.h"
Allen Bauer912e8ff32023-11-08 21:13:1116#include "ui/base/metadata/metadata_header_macros.h"
17#include "ui/base/metadata/metadata_impl_macros.h"
Andre Leed1afef2020-09-11 01:06:5518#include "ui/gfx/geometry/insets.h"
Meilin Wang24f7cf82020-10-26 23:13:1219#include "ui/gfx/text_constants.h"
Andre Leed1afef2020-09-11 01:06:5520#include "ui/views/controls/label.h"
21#include "ui/views/layout/box_layout.h"
Andre Leed1afef2020-09-11 01:06:5522
23namespace ash {
24
25namespace {
26
Meilin Wang24f7cf82020-10-26 23:13:1227// Appearance constants in dip.
Meilin Wangc14a36f2020-10-31 00:17:5128constexpr int kTaskContinuationChipHeight = 96;
Andre Leed1afef2020-09-11 01:06:5529constexpr int kTaskContinuationChipsInRow = 2;
30constexpr int kTaskContinuationChipSpacing = 8;
Meilin Wangc14a36f2020-10-31 00:17:5131constexpr int kTaskContinuationChipHorizontalSidePadding = 4;
Meilin Wang24f7cf82020-10-26 23:13:1232constexpr int kTaskContinuationChipVerticalPadding = 4;
33constexpr int kHeaderLabelLineHeight = 48;
Andre Leed1afef2020-09-11 01:06:5534
Meilin Wang24f7cf82020-10-26 23:13:1235// Typography.
36constexpr int kHeaderTextFontSizeDip = 15;
37
Meilin Wangc14a36f2020-10-31 00:17:5138gfx::Size GetTaskContinuationChipSize() {
39 int width =
40 (kTrayMenuWidth - kBubbleHorizontalSidePaddingDip * 2 -
41 kTaskContinuationChipHorizontalSidePadding * 2 -
42 kTaskContinuationChipSpacing * (kTaskContinuationChipsInRow - 1)) /
43 kTaskContinuationChipsInRow;
44 return gfx::Size(width, kTaskContinuationChipHeight);
45}
46
Meilin Wang24f7cf82020-10-26 23:13:1247class HeaderView : public views::Label {
Allen Bauer912e8ff32023-11-08 21:13:1148 METADATA_HEADER(HeaderView, views::Label)
49
Andre Leed1afef2020-09-11 01:06:5550 public:
51 HeaderView() {
Meilin Wang24f7cf82020-10-26 23:13:1252 SetText(
53 l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TASK_CONTINUATION_TITLE));
Meilin Wang24f7cf82020-10-26 23:13:1254 SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
55 SetVerticalAlignment(gfx::VerticalAlignment::ALIGN_MIDDLE);
56 SetAutoColorReadabilityEnabled(false);
57 SetSubpixelRenderingEnabled(false);
58 SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
Andre Leed1afef2020-09-11 01:06:5559 AshColorProvider::ContentLayerType::kTextColorPrimary));
Crisrael Lucerof1392ae2023-05-12 23:17:4560
61 if (chromeos::features::IsJellyrollEnabled()) {
62 TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosButton1,
63 *this);
64 } else {
65 SetFontList(font_list()
66 .DeriveWithSizeDelta(kHeaderTextFontSizeDip -
67 font_list().GetFontSize())
68 .DeriveWithWeight(gfx::Font::Weight::MEDIUM));
69 }
70 SetLineHeight(kHeaderLabelLineHeight);
Andre Leed1afef2020-09-11 01:06:5571 }
72
73 ~HeaderView() override = default;
74 HeaderView(HeaderView&) = delete;
75 HeaderView operator=(HeaderView&) = delete;
Andre Leed1afef2020-09-11 01:06:5576};
77
Allen Bauer912e8ff32023-11-08 21:13:1178BEGIN_METADATA(HeaderView)
79END_METADATA
80
Andre Leed1afef2020-09-11 01:06:5581} // namespace
82
Andre Le63ca8fe2020-09-21 21:35:3483TaskContinuationView::TaskContinuationView(
Henrique Ferreiro91fb61df2021-11-12 15:51:0384 phonehub::PhoneModel* phone_model,
85 phonehub::UserActionRecorder* user_action_recorder)
Kyle Horimotoae02ab32020-12-11 00:35:4786 : phone_model_(phone_model), user_action_recorder_(user_action_recorder) {
Meilin Wange95457a82020-09-24 02:51:5587 SetID(PhoneHubViewID::kTaskContinuationView);
88
Andre Le63ca8fe2020-09-21 21:35:3489 phone_model_->AddObserver(this);
90
Andre Leed1afef2020-09-11 01:06:5591 auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
Meilin Wang5ab16ba2020-10-27 19:36:5192 views::BoxLayout::Orientation::kVertical));
Andre Leed1afef2020-09-11 01:06:5593 layout->set_cross_axis_alignment(
94 views::BoxLayout::CrossAxisAlignment::kStart);
95
96 AddChildView(std::make_unique<HeaderView>());
Andre Le63ca8fe2020-09-21 21:35:3497 chips_view_ = AddChildView(std::make_unique<TaskChipsView>());
98
99 Update();
Andre Leed1afef2020-09-11 01:06:55100}
101
Andre Le63ca8fe2020-09-21 21:35:34102TaskContinuationView::~TaskContinuationView() {
103 phone_model_->RemoveObserver(this);
104}
105
106void TaskContinuationView::OnModelChanged() {
107 Update();
108}
Andre Leed1afef2020-09-11 01:06:55109
Andre Le63ca8fe2020-09-21 21:35:34110TaskContinuationView::TaskChipsView::TaskChipsView() = default;
111
112TaskContinuationView::TaskChipsView::~TaskChipsView() = default;
113
114void TaskContinuationView::TaskChipsView::AddTaskChip(views::View* task_chip) {
Peter Kasting991d3fc22022-06-24 19:06:02115 size_t view_size = task_chips_.view_size();
Andre Le63ca8fe2020-09-21 21:35:34116 task_chips_.Add(task_chip, view_size);
117 AddChildView(task_chip);
118}
119
120// views::View:
121gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const {
Meilin Wangc14a36f2020-10-31 00:17:51122 auto chip_size = GetTaskContinuationChipSize();
123 int width = chip_size.width() * kTaskContinuationChipsInRow +
Meilin Wang24f7cf82020-10-26 23:13:12124 kTaskContinuationChipSpacing +
Meilin Wangc14a36f2020-10-31 00:17:51125 2 * kTaskContinuationChipHorizontalSidePadding;
Andre Le63ca8fe2020-09-21 21:35:34126 int rows_num =
127 std::ceil((double)task_chips_.view_size() / kTaskContinuationChipsInRow);
Meilin Wangc14a36f2020-10-31 00:17:51128 int height = (chip_size.height() + kTaskContinuationChipVerticalPadding) *
Andre Le63ca8fe2020-09-21 21:35:34129 std::max(0, rows_num - 1) +
Meilin Wangc14a36f2020-10-31 00:17:51130 chip_size.height() +
131 2 * kTaskContinuationChipHorizontalSidePadding;
Andre Le63ca8fe2020-09-21 21:35:34132 return gfx::Size(width, height);
133}
134
135void TaskContinuationView::TaskChipsView::Layout() {
136 views::View::Layout();
137 CalculateIdealBounds();
Peter Kasting991d3fc22022-06-24 19:06:02138 for (size_t i = 0; i < task_chips_.view_size(); ++i) {
Andre Le63ca8fe2020-09-21 21:35:34139 auto* button = task_chips_.view_at(i);
140 button->SetBoundsRect(task_chips_.ideal_bounds(i));
141 }
142}
143
144const char* TaskContinuationView::TaskChipsView::GetClassName() const {
145 return "TaskChipsView";
146}
147
148void TaskContinuationView::TaskChipsView::Reset() {
149 task_chips_.Clear();
Peter Boström91c5c8332021-08-05 15:09:19150 RemoveAllChildViews();
Andre Le63ca8fe2020-09-21 21:35:34151}
152
153gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) {
Meilin Wangc14a36f2020-10-31 00:17:51154 auto chip_size = GetTaskContinuationChipSize();
Andre Le63ca8fe2020-09-21 21:35:34155 int row = index / kTaskContinuationChipsInRow;
156 int column = index % kTaskContinuationChipsInRow;
Meilin Wangc14a36f2020-10-31 00:17:51157 int x = (chip_size.width() + kTaskContinuationChipSpacing) * column +
158 kTaskContinuationChipHorizontalSidePadding;
159 int y = (chip_size.height() + kTaskContinuationChipVerticalPadding) * row +
Meilin Wang24f7cf82020-10-26 23:13:12160 kTaskContinuationChipVerticalPadding;
Andre Le63ca8fe2020-09-21 21:35:34161 return gfx::Point(x, y);
162}
163
164void TaskContinuationView::TaskChipsView::CalculateIdealBounds() {
Peter Kasting991d3fc22022-06-24 19:06:02165 for (size_t i = 0; i < task_chips_.view_size(); ++i) {
Andre Le63ca8fe2020-09-21 21:35:34166 gfx::Rect tile_bounds =
Meilin Wangc14a36f2020-10-31 00:17:51167 gfx::Rect(GetButtonPosition(i), GetTaskContinuationChipSize());
Andre Le63ca8fe2020-09-21 21:35:34168 task_chips_.set_ideal_bounds(i, tile_bounds);
169 }
170}
171
Allen Bauer912e8ff32023-11-08 21:13:11172BEGIN_METADATA(TaskContinuationView, TaskChipsView, views::View)
173END_METADATA
174
Andre Le63ca8fe2020-09-21 21:35:34175void TaskContinuationView::Update() {
Andre Lebbc3cf32020-09-30 18:18:46176 chips_view_->Reset();
177
Andre Le63ca8fe2020-09-21 21:35:34178 if (!phone_model_->browser_tabs_model()) {
179 SetVisible(false);
180 return;
181 }
182
Henrique Ferreiro91fb61df2021-11-12 15:51:03183 const phonehub::BrowserTabsModel& browser_tabs =
Andre Le63ca8fe2020-09-21 21:35:34184 phone_model_->browser_tabs_model().value();
185
186 if (!browser_tabs.is_tab_sync_enabled() ||
187 browser_tabs.most_recent_tabs().empty()) {
188 SetVisible(false);
189 return;
190 }
191
Tim Song978248932020-10-27 19:47:41192 int index = 0;
Henrique Ferreiro91fb61df2021-11-12 15:51:03193 for (const phonehub::BrowserTabsModel::BrowserTabMetadata& metadata :
Andre Le63ca8fe2020-09-21 21:35:34194 browser_tabs.most_recent_tabs()) {
Jimmy Gong776ab1f2020-12-03 06:16:25195 chips_view_->AddTaskChip(new ContinueBrowsingChip(
Kyle Horimotoae02ab32020-12-11 00:35:47196 metadata, index, browser_tabs.most_recent_tabs().size(),
197 user_action_recorder_));
Tim Song978248932020-10-27 19:47:41198 index++;
Andre Le63ca8fe2020-09-21 21:35:34199 }
200
Andre Lebbc3cf32020-09-30 18:18:46201 PreferredSizeChanged();
Andre Le63ca8fe2020-09-21 21:35:34202 SetVisible(true);
203}
204
Allen Bauer912e8ff32023-11-08 21:13:11205BEGIN_METADATA(TaskContinuationView)
206END_METADATA
207
Andre Leed1afef2020-09-11 01:06:55208} // namespace ash