[go: nahoru, domu]

blob: f0d1676838fa3123831e1def79f7c7bd7df30b43 [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/task_continuation_view.h"
6
7#include "ash/strings/grit/ash_strings.h"
8#include "ash/style/ash_color_provider.h"
9#include "ash/system/phonehub/continue_browsing_chip.h"
Meilin Wange95457a82020-09-24 02:51:5510#include "ash/system/phonehub/phone_hub_view_ids.h"
Meilin Wangc14a36f2020-10-31 00:17:5111#include "ash/system/phonehub/ui_constants.h"
12#include "ash/system/tray/tray_constants.h"
Andre Leed1afef2020-09-11 01:06:5513#include "ui/base/l10n/l10n_util.h"
14#include "ui/gfx/geometry/insets.h"
Meilin Wang24f7cf82020-10-26 23:13:1215#include "ui/gfx/text_constants.h"
Andre Leed1afef2020-09-11 01:06:5516#include "ui/views/controls/label.h"
17#include "ui/views/layout/box_layout.h"
Andre Leed1afef2020-09-11 01:06:5518
19namespace ash {
20
Andre Le63ca8fe2020-09-21 21:35:3421using BrowserTabsModel = chromeos::phonehub::BrowserTabsModel;
22
Andre Leed1afef2020-09-11 01:06:5523namespace {
24
Meilin Wang24f7cf82020-10-26 23:13:1225// Appearance constants in dip.
Meilin Wangc14a36f2020-10-31 00:17:5126constexpr int kTaskContinuationChipHeight = 96;
Andre Leed1afef2020-09-11 01:06:5527constexpr int kTaskContinuationChipsInRow = 2;
28constexpr int kTaskContinuationChipSpacing = 8;
Meilin Wangc14a36f2020-10-31 00:17:5129constexpr int kTaskContinuationChipHorizontalSidePadding = 4;
Meilin Wang24f7cf82020-10-26 23:13:1230constexpr int kTaskContinuationChipVerticalPadding = 4;
31constexpr int kHeaderLabelLineHeight = 48;
Andre Leed1afef2020-09-11 01:06:5532
Meilin Wang24f7cf82020-10-26 23:13:1233// Typography.
34constexpr int kHeaderTextFontSizeDip = 15;
35
Meilin Wangc14a36f2020-10-31 00:17:5136gfx::Size GetTaskContinuationChipSize() {
37 int width =
38 (kTrayMenuWidth - kBubbleHorizontalSidePaddingDip * 2 -
39 kTaskContinuationChipHorizontalSidePadding * 2 -
40 kTaskContinuationChipSpacing * (kTaskContinuationChipsInRow - 1)) /
41 kTaskContinuationChipsInRow;
42 return gfx::Size(width, kTaskContinuationChipHeight);
43}
44
Meilin Wang24f7cf82020-10-26 23:13:1245class HeaderView : public views::Label {
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));
50 SetLineHeight(kHeaderLabelLineHeight);
51 SetFontList(font_list()
52 .DeriveWithSizeDelta(kHeaderTextFontSizeDip -
53 font_list().GetFontSize())
54 .DeriveWithWeight(gfx::Font::Weight::MEDIUM));
55 SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
56 SetVerticalAlignment(gfx::VerticalAlignment::ALIGN_MIDDLE);
57 SetAutoColorReadabilityEnabled(false);
58 SetSubpixelRenderingEnabled(false);
59 SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
Andre Leed1afef2020-09-11 01:06:5560 AshColorProvider::ContentLayerType::kTextColorPrimary));
61 }
62
63 ~HeaderView() override = default;
64 HeaderView(HeaderView&) = delete;
65 HeaderView operator=(HeaderView&) = delete;
66
67 // views::View:
68 const char* GetClassName() const override { return "HeaderView"; }
69};
70
Andre Leed1afef2020-09-11 01:06:5571} // namespace
72
Andre Le63ca8fe2020-09-21 21:35:3473TaskContinuationView::TaskContinuationView(
Kyle Horimotoae02ab32020-12-11 00:35:4774 chromeos::phonehub::PhoneModel* phone_model,
75 chromeos::phonehub::UserActionRecorder* user_action_recorder)
76 : 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
100const char* TaskContinuationView::GetClassName() const {
101 return "TaskContinuationView";
102}
103
Andre Le63ca8fe2020-09-21 21:35:34104TaskContinuationView::TaskChipsView::TaskChipsView() = default;
105
106TaskContinuationView::TaskChipsView::~TaskChipsView() = default;
107
108void TaskContinuationView::TaskChipsView::AddTaskChip(views::View* task_chip) {
109 int view_size = task_chips_.view_size();
110 task_chips_.Add(task_chip, view_size);
111 AddChildView(task_chip);
112}
113
114// views::View:
115gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const {
Meilin Wangc14a36f2020-10-31 00:17:51116 auto chip_size = GetTaskContinuationChipSize();
117 int width = chip_size.width() * kTaskContinuationChipsInRow +
Meilin Wang24f7cf82020-10-26 23:13:12118 kTaskContinuationChipSpacing +
Meilin Wangc14a36f2020-10-31 00:17:51119 2 * kTaskContinuationChipHorizontalSidePadding;
Andre Le63ca8fe2020-09-21 21:35:34120 int rows_num =
121 std::ceil((double)task_chips_.view_size() / kTaskContinuationChipsInRow);
Meilin Wangc14a36f2020-10-31 00:17:51122 int height = (chip_size.height() + kTaskContinuationChipVerticalPadding) *
Andre Le63ca8fe2020-09-21 21:35:34123 std::max(0, rows_num - 1) +
Meilin Wangc14a36f2020-10-31 00:17:51124 chip_size.height() +
125 2 * kTaskContinuationChipHorizontalSidePadding;
Andre Le63ca8fe2020-09-21 21:35:34126 return gfx::Size(width, height);
127}
128
129void TaskContinuationView::TaskChipsView::Layout() {
130 views::View::Layout();
131 CalculateIdealBounds();
132 for (int i = 0; i < task_chips_.view_size(); ++i) {
133 auto* button = task_chips_.view_at(i);
134 button->SetBoundsRect(task_chips_.ideal_bounds(i));
135 }
136}
137
138const char* TaskContinuationView::TaskChipsView::GetClassName() const {
139 return "TaskChipsView";
140}
141
142void TaskContinuationView::TaskChipsView::Reset() {
143 task_chips_.Clear();
Peter Boström91c5c8332021-08-05 15:09:19144 RemoveAllChildViews();
Andre Le63ca8fe2020-09-21 21:35:34145}
146
147gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) {
Meilin Wangc14a36f2020-10-31 00:17:51148 auto chip_size = GetTaskContinuationChipSize();
Andre Le63ca8fe2020-09-21 21:35:34149 int row = index / kTaskContinuationChipsInRow;
150 int column = index % kTaskContinuationChipsInRow;
Meilin Wangc14a36f2020-10-31 00:17:51151 int x = (chip_size.width() + kTaskContinuationChipSpacing) * column +
152 kTaskContinuationChipHorizontalSidePadding;
153 int y = (chip_size.height() + kTaskContinuationChipVerticalPadding) * row +
Meilin Wang24f7cf82020-10-26 23:13:12154 kTaskContinuationChipVerticalPadding;
Andre Le63ca8fe2020-09-21 21:35:34155 return gfx::Point(x, y);
156}
157
158void TaskContinuationView::TaskChipsView::CalculateIdealBounds() {
159 for (int i = 0; i < task_chips_.view_size(); ++i) {
160 gfx::Rect tile_bounds =
Meilin Wangc14a36f2020-10-31 00:17:51161 gfx::Rect(GetButtonPosition(i), GetTaskContinuationChipSize());
Andre Le63ca8fe2020-09-21 21:35:34162 task_chips_.set_ideal_bounds(i, tile_bounds);
163 }
164}
165
166void TaskContinuationView::Update() {
Andre Lebbc3cf32020-09-30 18:18:46167 chips_view_->Reset();
168
Andre Le63ca8fe2020-09-21 21:35:34169 if (!phone_model_->browser_tabs_model()) {
170 SetVisible(false);
171 return;
172 }
173
174 const BrowserTabsModel& browser_tabs =
175 phone_model_->browser_tabs_model().value();
176
177 if (!browser_tabs.is_tab_sync_enabled() ||
178 browser_tabs.most_recent_tabs().empty()) {
179 SetVisible(false);
180 return;
181 }
182
Tim Song978248932020-10-27 19:47:41183 int index = 0;
Andre Le63ca8fe2020-09-21 21:35:34184 for (const BrowserTabsModel::BrowserTabMetadata& metadata :
185 browser_tabs.most_recent_tabs()) {
Jimmy Gong776ab1f2020-12-03 06:16:25186 chips_view_->AddTaskChip(new ContinueBrowsingChip(
Kyle Horimotoae02ab32020-12-11 00:35:47187 metadata, index, browser_tabs.most_recent_tabs().size(),
188 user_action_recorder_));
Tim Song978248932020-10-27 19:47:41189 index++;
Andre Le63ca8fe2020-09-21 21:35:34190 }
191
Andre Lebbc3cf32020-09-30 18:18:46192 PreferredSizeChanged();
Andre Le63ca8fe2020-09-21 21:35:34193 SetVisible(true);
194}
195
Andre Leed1afef2020-09-11 01:06:55196} // namespace ash