[go: nahoru, domu]

blob: 5d7100392bb8c62e6f3ffbf276e16b9c0ab85d5a [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"
Andre Leed1afef2020-09-11 01:06:5514#include "ui/base/l10n/l10n_util.h"
Allen Bauer912e8ff32023-11-08 21:13:1115#include "ui/base/metadata/metadata_header_macros.h"
16#include "ui/base/metadata/metadata_impl_macros.h"
Andre Leed1afef2020-09-11 01:06:5517#include "ui/gfx/geometry/insets.h"
Meilin Wang24f7cf82020-10-26 23:13:1218#include "ui/gfx/text_constants.h"
Andre Leed1afef2020-09-11 01:06:5519#include "ui/views/controls/label.h"
20#include "ui/views/layout/box_layout.h"
Andre Leed1afef2020-09-11 01:06:5521
22namespace ash {
23
24namespace {
25
Meilin Wang24f7cf82020-10-26 23:13:1226// Appearance constants in dip.
Meilin Wangc14a36f2020-10-31 00:17:5127constexpr int kTaskContinuationChipHeight = 96;
Andre Leed1afef2020-09-11 01:06:5528constexpr int kTaskContinuationChipsInRow = 2;
29constexpr int kTaskContinuationChipSpacing = 8;
Meilin Wangc14a36f2020-10-31 00:17:5130constexpr int kTaskContinuationChipHorizontalSidePadding = 4;
Meilin Wang24f7cf82020-10-26 23:13:1231constexpr int kTaskContinuationChipVerticalPadding = 4;
32constexpr int kHeaderLabelLineHeight = 48;
Andre Leed1afef2020-09-11 01:06:5533
Meilin Wangc14a36f2020-10-31 00:17:5134gfx::Size GetTaskContinuationChipSize() {
35 int width =
36 (kTrayMenuWidth - kBubbleHorizontalSidePaddingDip * 2 -
37 kTaskContinuationChipHorizontalSidePadding * 2 -
38 kTaskContinuationChipSpacing * (kTaskContinuationChipsInRow - 1)) /
39 kTaskContinuationChipsInRow;
40 return gfx::Size(width, kTaskContinuationChipHeight);
41}
42
Meilin Wang24f7cf82020-10-26 23:13:1243class HeaderView : public views::Label {
Allen Bauer912e8ff32023-11-08 21:13:1144 METADATA_HEADER(HeaderView, views::Label)
45
Andre Leed1afef2020-09-11 01:06:5546 public:
47 HeaderView() {
Meilin Wang24f7cf82020-10-26 23:13:1248 SetText(
49 l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TASK_CONTINUATION_TITLE));
Meilin Wang24f7cf82020-10-26 23:13:1250 SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
51 SetVerticalAlignment(gfx::VerticalAlignment::ALIGN_MIDDLE);
52 SetAutoColorReadabilityEnabled(false);
53 SetSubpixelRenderingEnabled(false);
Crisrael Luceroecff8b82024-01-29 21:20:3254 // TODO(b/322067753): Replace usage of |AshColorProvider| with
55 // |cros_tokens|.
Meilin Wang24f7cf82020-10-26 23:13:1256 SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
Andre Leed1afef2020-09-11 01:06:5557 AshColorProvider::ContentLayerType::kTextColorPrimary));
Crisrael Luceroecff8b82024-01-29 21:20:3258 TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosButton1,
59 *this);
Crisrael Lucerof1392ae2023-05-12 23:17:4560 SetLineHeight(kHeaderLabelLineHeight);
Andre Leed1afef2020-09-11 01:06:5561 }
62
63 ~HeaderView() override = default;
64 HeaderView(HeaderView&) = delete;
65 HeaderView operator=(HeaderView&) = delete;
Andre Leed1afef2020-09-11 01:06:5566};
67
Allen Bauer912e8ff32023-11-08 21:13:1168BEGIN_METADATA(HeaderView)
69END_METADATA
70
Andre Leed1afef2020-09-11 01:06:5571} // namespace
72
Andre Le63ca8fe2020-09-21 21:35:3473TaskContinuationView::TaskContinuationView(
Henrique Ferreiro91fb61df2021-11-12 15:51:0374 phonehub::PhoneModel* phone_model,
75 phonehub::UserActionRecorder* user_action_recorder)
Kyle Horimotoae02ab32020-12-11 00:35:4776 : phone_model_(phone_model), user_action_recorder_(user_action_recorder) {
Meilin Wange95457a82020-09-24 02:51:5577 SetID(PhoneHubViewID::kTaskContinuationView);
78
Andre Le63ca8fe2020-09-21 21:35:3479 phone_model_->AddObserver(this);
80
Andre Leed1afef2020-09-11 01:06:5581 auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
Meilin Wang5ab16ba2020-10-27 19:36:5182 views::BoxLayout::Orientation::kVertical));
Andre Leed1afef2020-09-11 01:06:5583 layout->set_cross_axis_alignment(
84 views::BoxLayout::CrossAxisAlignment::kStart);
85
86 AddChildView(std::make_unique<HeaderView>());
Andre Le63ca8fe2020-09-21 21:35:3487 chips_view_ = AddChildView(std::make_unique<TaskChipsView>());
88
89 Update();
Andre Leed1afef2020-09-11 01:06:5590}
91
Andre Le63ca8fe2020-09-21 21:35:3492TaskContinuationView::~TaskContinuationView() {
93 phone_model_->RemoveObserver(this);
94}
95
96void TaskContinuationView::OnModelChanged() {
97 Update();
98}
Andre Leed1afef2020-09-11 01:06:5599
Andre Le63ca8fe2020-09-21 21:35:34100TaskContinuationView::TaskChipsView::TaskChipsView() = default;
101
102TaskContinuationView::TaskChipsView::~TaskChipsView() = default;
103
104void TaskContinuationView::TaskChipsView::AddTaskChip(views::View* task_chip) {
Peter Kasting991d3fc22022-06-24 19:06:02105 size_t view_size = task_chips_.view_size();
Andre Le63ca8fe2020-09-21 21:35:34106 task_chips_.Add(task_chip, view_size);
107 AddChildView(task_chip);
108}
109
110// views::View:
111gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const {
Meilin Wangc14a36f2020-10-31 00:17:51112 auto chip_size = GetTaskContinuationChipSize();
113 int width = chip_size.width() * kTaskContinuationChipsInRow +
Meilin Wang24f7cf82020-10-26 23:13:12114 kTaskContinuationChipSpacing +
Meilin Wangc14a36f2020-10-31 00:17:51115 2 * kTaskContinuationChipHorizontalSidePadding;
Andre Le63ca8fe2020-09-21 21:35:34116 int rows_num =
117 std::ceil((double)task_chips_.view_size() / kTaskContinuationChipsInRow);
Meilin Wangc14a36f2020-10-31 00:17:51118 int height = (chip_size.height() + kTaskContinuationChipVerticalPadding) *
Andre Le63ca8fe2020-09-21 21:35:34119 std::max(0, rows_num - 1) +
Meilin Wangc14a36f2020-10-31 00:17:51120 chip_size.height() +
121 2 * kTaskContinuationChipHorizontalSidePadding;
Andre Le63ca8fe2020-09-21 21:35:34122 return gfx::Size(width, height);
123}
124
Peter Kasting1e3a1e992024-02-01 18:10:41125void TaskContinuationView::TaskChipsView::Layout(PassKey) {
Peter Kasting6a838b542024-01-29 23:37:39126 LayoutSuperclass<views::View>(this);
Andre Le63ca8fe2020-09-21 21:35:34127 CalculateIdealBounds();
Peter Kasting991d3fc22022-06-24 19:06:02128 for (size_t i = 0; i < task_chips_.view_size(); ++i) {
Andre Le63ca8fe2020-09-21 21:35:34129 auto* button = task_chips_.view_at(i);
130 button->SetBoundsRect(task_chips_.ideal_bounds(i));
131 }
132}
133
Andre Le63ca8fe2020-09-21 21:35:34134void TaskContinuationView::TaskChipsView::Reset() {
135 task_chips_.Clear();
Peter Boström91c5c8332021-08-05 15:09:19136 RemoveAllChildViews();
Andre Le63ca8fe2020-09-21 21:35:34137}
138
139gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) {
Meilin Wangc14a36f2020-10-31 00:17:51140 auto chip_size = GetTaskContinuationChipSize();
Andre Le63ca8fe2020-09-21 21:35:34141 int row = index / kTaskContinuationChipsInRow;
142 int column = index % kTaskContinuationChipsInRow;
Meilin Wangc14a36f2020-10-31 00:17:51143 int x = (chip_size.width() + kTaskContinuationChipSpacing) * column +
144 kTaskContinuationChipHorizontalSidePadding;
145 int y = (chip_size.height() + kTaskContinuationChipVerticalPadding) * row +
Meilin Wang24f7cf82020-10-26 23:13:12146 kTaskContinuationChipVerticalPadding;
Andre Le63ca8fe2020-09-21 21:35:34147 return gfx::Point(x, y);
148}
149
150void TaskContinuationView::TaskChipsView::CalculateIdealBounds() {
Peter Kasting991d3fc22022-06-24 19:06:02151 for (size_t i = 0; i < task_chips_.view_size(); ++i) {
Andre Le63ca8fe2020-09-21 21:35:34152 gfx::Rect tile_bounds =
Meilin Wangc14a36f2020-10-31 00:17:51153 gfx::Rect(GetButtonPosition(i), GetTaskContinuationChipSize());
Andre Le63ca8fe2020-09-21 21:35:34154 task_chips_.set_ideal_bounds(i, tile_bounds);
155 }
156}
157
Allen Bauer7a42dd92024-02-16 23:08:54158BEGIN_METADATA(TaskContinuationView, TaskChipsView)
Allen Bauer912e8ff32023-11-08 21:13:11159END_METADATA
160
Andre Le63ca8fe2020-09-21 21:35:34161void TaskContinuationView::Update() {
Andre Lebbc3cf32020-09-30 18:18:46162 chips_view_->Reset();
163
Andre Le63ca8fe2020-09-21 21:35:34164 if (!phone_model_->browser_tabs_model()) {
165 SetVisible(false);
166 return;
167 }
168
Henrique Ferreiro91fb61df2021-11-12 15:51:03169 const phonehub::BrowserTabsModel& browser_tabs =
Andre Le63ca8fe2020-09-21 21:35:34170 phone_model_->browser_tabs_model().value();
171
172 if (!browser_tabs.is_tab_sync_enabled() ||
173 browser_tabs.most_recent_tabs().empty()) {
174 SetVisible(false);
175 return;
176 }
177
Tim Song978248932020-10-27 19:47:41178 int index = 0;
Henrique Ferreiro91fb61df2021-11-12 15:51:03179 for (const phonehub::BrowserTabsModel::BrowserTabMetadata& metadata :
Andre Le63ca8fe2020-09-21 21:35:34180 browser_tabs.most_recent_tabs()) {
Jimmy Gong776ab1f2020-12-03 06:16:25181 chips_view_->AddTaskChip(new ContinueBrowsingChip(
Kyle Horimotoae02ab32020-12-11 00:35:47182 metadata, index, browser_tabs.most_recent_tabs().size(),
183 user_action_recorder_));
Tim Song978248932020-10-27 19:47:41184 index++;
Andre Le63ca8fe2020-09-21 21:35:34185 }
186
Andre Lebbc3cf32020-09-30 18:18:46187 PreferredSizeChanged();
Andre Le63ca8fe2020-09-21 21:35:34188 SetVisible(true);
189}
190
Allen Bauer912e8ff32023-11-08 21:13:11191BEGIN_METADATA(TaskContinuationView)
192END_METADATA
193
Andre Leed1afef2020-09-11 01:06:55194} // namespace ash