[go: nahoru, domu]

blob: b6c67bced9f257e8fbc7c3d8d7bf23778c6a7731 [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(
74 chromeos::phonehub::PhoneModel* phone_model)
75 : phone_model_(phone_model) {
Meilin Wange95457a82020-09-24 02:51:5576 SetID(PhoneHubViewID::kTaskContinuationView);
77
Andre Le63ca8fe2020-09-21 21:35:3478 phone_model_->AddObserver(this);
79
Andre Leed1afef2020-09-11 01:06:5580 auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
Meilin Wang5ab16ba2020-10-27 19:36:5181 views::BoxLayout::Orientation::kVertical));
Andre Leed1afef2020-09-11 01:06:5582 layout->set_cross_axis_alignment(
83 views::BoxLayout::CrossAxisAlignment::kStart);
84
85 AddChildView(std::make_unique<HeaderView>());
Andre Le63ca8fe2020-09-21 21:35:3486 chips_view_ = AddChildView(std::make_unique<TaskChipsView>());
87
88 Update();
Andre Leed1afef2020-09-11 01:06:5589}
90
Andre Le63ca8fe2020-09-21 21:35:3491TaskContinuationView::~TaskContinuationView() {
92 phone_model_->RemoveObserver(this);
93}
94
95void TaskContinuationView::OnModelChanged() {
96 Update();
97}
Andre Leed1afef2020-09-11 01:06:5598
99const char* TaskContinuationView::GetClassName() const {
100 return "TaskContinuationView";
101}
102
Andre Le63ca8fe2020-09-21 21:35:34103TaskContinuationView::TaskChipsView::TaskChipsView() = default;
104
105TaskContinuationView::TaskChipsView::~TaskChipsView() = default;
106
107void TaskContinuationView::TaskChipsView::AddTaskChip(views::View* task_chip) {
108 int view_size = task_chips_.view_size();
109 task_chips_.Add(task_chip, view_size);
110 AddChildView(task_chip);
111}
112
113// views::View:
114gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const {
Meilin Wangc14a36f2020-10-31 00:17:51115 auto chip_size = GetTaskContinuationChipSize();
116 int width = chip_size.width() * kTaskContinuationChipsInRow +
Meilin Wang24f7cf82020-10-26 23:13:12117 kTaskContinuationChipSpacing +
Meilin Wangc14a36f2020-10-31 00:17:51118 2 * kTaskContinuationChipHorizontalSidePadding;
Andre Le63ca8fe2020-09-21 21:35:34119 int rows_num =
120 std::ceil((double)task_chips_.view_size() / kTaskContinuationChipsInRow);
Meilin Wangc14a36f2020-10-31 00:17:51121 int height = (chip_size.height() + kTaskContinuationChipVerticalPadding) *
Andre Le63ca8fe2020-09-21 21:35:34122 std::max(0, rows_num - 1) +
Meilin Wangc14a36f2020-10-31 00:17:51123 chip_size.height() +
124 2 * kTaskContinuationChipHorizontalSidePadding;
Andre Le63ca8fe2020-09-21 21:35:34125 return gfx::Size(width, height);
126}
127
128void TaskContinuationView::TaskChipsView::Layout() {
129 views::View::Layout();
130 CalculateIdealBounds();
131 for (int i = 0; i < task_chips_.view_size(); ++i) {
132 auto* button = task_chips_.view_at(i);
133 button->SetBoundsRect(task_chips_.ideal_bounds(i));
134 }
135}
136
137const char* TaskContinuationView::TaskChipsView::GetClassName() const {
138 return "TaskChipsView";
139}
140
141void TaskContinuationView::TaskChipsView::Reset() {
142 task_chips_.Clear();
143 RemoveAllChildViews(true /* delete_children */);
144}
145
146gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) {
Meilin Wangc14a36f2020-10-31 00:17:51147 auto chip_size = GetTaskContinuationChipSize();
Andre Le63ca8fe2020-09-21 21:35:34148 int row = index / kTaskContinuationChipsInRow;
149 int column = index % kTaskContinuationChipsInRow;
Meilin Wangc14a36f2020-10-31 00:17:51150 int x = (chip_size.width() + kTaskContinuationChipSpacing) * column +
151 kTaskContinuationChipHorizontalSidePadding;
152 int y = (chip_size.height() + kTaskContinuationChipVerticalPadding) * row +
Meilin Wang24f7cf82020-10-26 23:13:12153 kTaskContinuationChipVerticalPadding;
Andre Le63ca8fe2020-09-21 21:35:34154 return gfx::Point(x, y);
155}
156
157void TaskContinuationView::TaskChipsView::CalculateIdealBounds() {
158 for (int i = 0; i < task_chips_.view_size(); ++i) {
159 gfx::Rect tile_bounds =
Meilin Wangc14a36f2020-10-31 00:17:51160 gfx::Rect(GetButtonPosition(i), GetTaskContinuationChipSize());
Andre Le63ca8fe2020-09-21 21:35:34161 task_chips_.set_ideal_bounds(i, tile_bounds);
162 }
163}
164
165void TaskContinuationView::Update() {
Andre Lebbc3cf32020-09-30 18:18:46166 chips_view_->Reset();
167
Andre Le63ca8fe2020-09-21 21:35:34168 if (!phone_model_->browser_tabs_model()) {
169 SetVisible(false);
170 return;
171 }
172
173 const BrowserTabsModel& browser_tabs =
174 phone_model_->browser_tabs_model().value();
175
176 if (!browser_tabs.is_tab_sync_enabled() ||
177 browser_tabs.most_recent_tabs().empty()) {
178 SetVisible(false);
179 return;
180 }
181
Tim Song978248932020-10-27 19:47:41182 int index = 0;
Andre Le63ca8fe2020-09-21 21:35:34183 for (const BrowserTabsModel::BrowserTabMetadata& metadata :
184 browser_tabs.most_recent_tabs()) {
Jimmy Gong776ab1f2020-12-03 06:16:25185 chips_view_->AddTaskChip(new ContinueBrowsingChip(
186 metadata, index, browser_tabs.most_recent_tabs().size()));
Tim Song978248932020-10-27 19:47:41187 index++;
Andre Le63ca8fe2020-09-21 21:35:34188 }
189
Andre Lebbc3cf32020-09-30 18:18:46190 PreferredSizeChanged();
Andre Le63ca8fe2020-09-21 21:35:34191 SetVisible(true);
192}
193
Andre Leed1afef2020-09-11 01:06:55194} // namespace ash