[go: nahoru, domu]

blob: 2ebdd48231ca87d6062dc35859699f7676aa7bad [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"
Andre Leed1afef2020-09-11 01:06:5511#include "ui/base/l10n/l10n_util.h"
12#include "ui/gfx/geometry/insets.h"
Meilin Wang24f7cf82020-10-26 23:13:1213#include "ui/gfx/text_constants.h"
Andre Leed1afef2020-09-11 01:06:5514#include "ui/views/controls/label.h"
15#include "ui/views/layout/box_layout.h"
Andre Leed1afef2020-09-11 01:06:5516
17namespace ash {
18
Andre Le63ca8fe2020-09-21 21:35:3419using BrowserTabsModel = chromeos::phonehub::BrowserTabsModel;
20
Andre Leed1afef2020-09-11 01:06:5521namespace {
22
Meilin Wang24f7cf82020-10-26 23:13:1223// Appearance constants in dip.
Meilin Wang24f7cf82020-10-26 23:13:1224constexpr gfx::Size kTaskContinuationChipSize(176, 96);
Andre Leed1afef2020-09-11 01:06:5525constexpr int kTaskContinuationChipsInRow = 2;
26constexpr int kTaskContinuationChipSpacing = 8;
Meilin Wang24f7cf82020-10-26 23:13:1227constexpr int kTaskContinuationChipHorizontalPadding = 4;
28constexpr int kTaskContinuationChipVerticalPadding = 4;
29constexpr int kHeaderLabelLineHeight = 48;
Andre Leed1afef2020-09-11 01:06:5530
Meilin Wang24f7cf82020-10-26 23:13:1231// Typography.
32constexpr int kHeaderTextFontSizeDip = 15;
33
34class HeaderView : public views::Label {
Andre Leed1afef2020-09-11 01:06:5535 public:
36 HeaderView() {
Meilin Wang24f7cf82020-10-26 23:13:1237 SetText(
38 l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TASK_CONTINUATION_TITLE));
39 SetLineHeight(kHeaderLabelLineHeight);
40 SetFontList(font_list()
41 .DeriveWithSizeDelta(kHeaderTextFontSizeDip -
42 font_list().GetFontSize())
43 .DeriveWithWeight(gfx::Font::Weight::MEDIUM));
44 SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
45 SetVerticalAlignment(gfx::VerticalAlignment::ALIGN_MIDDLE);
46 SetAutoColorReadabilityEnabled(false);
47 SetSubpixelRenderingEnabled(false);
48 SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
Andre Leed1afef2020-09-11 01:06:5549 AshColorProvider::ContentLayerType::kTextColorPrimary));
50 }
51
52 ~HeaderView() override = default;
53 HeaderView(HeaderView&) = delete;
54 HeaderView operator=(HeaderView&) = delete;
55
56 // views::View:
57 const char* GetClassName() const override { return "HeaderView"; }
58};
59
Andre Leed1afef2020-09-11 01:06:5560} // namespace
61
Andre Le63ca8fe2020-09-21 21:35:3462TaskContinuationView::TaskContinuationView(
63 chromeos::phonehub::PhoneModel* phone_model)
64 : phone_model_(phone_model) {
Meilin Wange95457a82020-09-24 02:51:5565 SetID(PhoneHubViewID::kTaskContinuationView);
66
Andre Le63ca8fe2020-09-21 21:35:3467 phone_model_->AddObserver(this);
68
Andre Leed1afef2020-09-11 01:06:5569 auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
Meilin Wang5ab16ba2020-10-27 19:36:5170 views::BoxLayout::Orientation::kVertical));
Andre Leed1afef2020-09-11 01:06:5571 layout->set_cross_axis_alignment(
72 views::BoxLayout::CrossAxisAlignment::kStart);
73
74 AddChildView(std::make_unique<HeaderView>());
Andre Le63ca8fe2020-09-21 21:35:3475 chips_view_ = AddChildView(std::make_unique<TaskChipsView>());
76
77 Update();
Andre Leed1afef2020-09-11 01:06:5578}
79
Andre Le63ca8fe2020-09-21 21:35:3480TaskContinuationView::~TaskContinuationView() {
81 phone_model_->RemoveObserver(this);
82}
83
84void TaskContinuationView::OnModelChanged() {
85 Update();
86}
Andre Leed1afef2020-09-11 01:06:5587
88const char* TaskContinuationView::GetClassName() const {
89 return "TaskContinuationView";
90}
91
Andre Le63ca8fe2020-09-21 21:35:3492TaskContinuationView::TaskChipsView::TaskChipsView() = default;
93
94TaskContinuationView::TaskChipsView::~TaskChipsView() = default;
95
96void TaskContinuationView::TaskChipsView::AddTaskChip(views::View* task_chip) {
97 int view_size = task_chips_.view_size();
98 task_chips_.Add(task_chip, view_size);
99 AddChildView(task_chip);
100}
101
102// views::View:
103gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const {
104 int width = kTaskContinuationChipSize.width() * kTaskContinuationChipsInRow +
Meilin Wang24f7cf82020-10-26 23:13:12105 kTaskContinuationChipSpacing +
106 2 * kTaskContinuationChipHorizontalPadding;
Andre Le63ca8fe2020-09-21 21:35:34107 int rows_num =
108 std::ceil((double)task_chips_.view_size() / kTaskContinuationChipsInRow);
109 int height = (kTaskContinuationChipSize.height() +
110 kTaskContinuationChipVerticalPadding) *
111 std::max(0, rows_num - 1) +
Meilin Wang24f7cf82020-10-26 23:13:12112 kTaskContinuationChipSize.height() +
113 2 * kTaskContinuationChipHorizontalPadding;
Andre Le63ca8fe2020-09-21 21:35:34114 return gfx::Size(width, height);
115}
116
117void TaskContinuationView::TaskChipsView::Layout() {
118 views::View::Layout();
119 CalculateIdealBounds();
120 for (int i = 0; i < task_chips_.view_size(); ++i) {
121 auto* button = task_chips_.view_at(i);
122 button->SetBoundsRect(task_chips_.ideal_bounds(i));
123 }
124}
125
126const char* TaskContinuationView::TaskChipsView::GetClassName() const {
127 return "TaskChipsView";
128}
129
130void TaskContinuationView::TaskChipsView::Reset() {
131 task_chips_.Clear();
132 RemoveAllChildViews(true /* delete_children */);
133}
134
135gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) {
136 int row = index / kTaskContinuationChipsInRow;
137 int column = index % kTaskContinuationChipsInRow;
138 int x = (kTaskContinuationChipSize.width() + kTaskContinuationChipSpacing) *
Meilin Wang24f7cf82020-10-26 23:13:12139 column +
140 kTaskContinuationChipHorizontalPadding;
Andre Le63ca8fe2020-09-21 21:35:34141 int y = (kTaskContinuationChipSize.height() +
142 kTaskContinuationChipVerticalPadding) *
Meilin Wang24f7cf82020-10-26 23:13:12143 row +
144 kTaskContinuationChipVerticalPadding;
Andre Le63ca8fe2020-09-21 21:35:34145 return gfx::Point(x, y);
146}
147
148void TaskContinuationView::TaskChipsView::CalculateIdealBounds() {
149 for (int i = 0; i < task_chips_.view_size(); ++i) {
150 gfx::Rect tile_bounds =
151 gfx::Rect(GetButtonPosition(i), kTaskContinuationChipSize);
152 task_chips_.set_ideal_bounds(i, tile_bounds);
153 }
154}
155
156void TaskContinuationView::Update() {
Andre Lebbc3cf32020-09-30 18:18:46157 chips_view_->Reset();
158
Andre Le63ca8fe2020-09-21 21:35:34159 if (!phone_model_->browser_tabs_model()) {
160 SetVisible(false);
161 return;
162 }
163
164 const BrowserTabsModel& browser_tabs =
165 phone_model_->browser_tabs_model().value();
166
167 if (!browser_tabs.is_tab_sync_enabled() ||
168 browser_tabs.most_recent_tabs().empty()) {
169 SetVisible(false);
170 return;
171 }
172
Tim Song978248932020-10-27 19:47:41173 int index = 0;
Andre Le63ca8fe2020-09-21 21:35:34174 for (const BrowserTabsModel::BrowserTabMetadata& metadata :
175 browser_tabs.most_recent_tabs()) {
Tim Song978248932020-10-27 19:47:41176 chips_view_->AddTaskChip(new ContinueBrowsingChip(metadata, index));
177 index++;
Andre Le63ca8fe2020-09-21 21:35:34178 }
179
Andre Lebbc3cf32020-09-30 18:18:46180 PreferredSizeChanged();
Andre Le63ca8fe2020-09-21 21:35:34181 SetVisible(true);
182}
183
Andre Leed1afef2020-09-11 01:06:55184} // namespace ash