[go: nahoru, domu]

blob: 8b3ea72c22e1c196b9d4dcb5ee66b96a707e6eb8 [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"
13#include "ui/views/controls/label.h"
14#include "ui/views/layout/box_layout.h"
Andre Leed1afef2020-09-11 01:06:5515
16namespace ash {
17
Andre Le63ca8fe2020-09-21 21:35:3418using BrowserTabsModel = chromeos::phonehub::BrowserTabsModel;
19
Andre Leed1afef2020-09-11 01:06:5520namespace {
21
22constexpr int kTaskContinuationHeaderSpacing = 8;
23constexpr gfx::Insets kTaskContinuationViewPadding(12, 4);
24constexpr gfx::Insets kPhoneHubSubHeaderPadding(4, 32);
Andre Le63ca8fe2020-09-21 21:35:3425constexpr gfx::Size kTaskContinuationChipSize(170, 80);
Andre Leed1afef2020-09-11 01:06:5526constexpr int kTaskContinuationChipsInRow = 2;
27constexpr int kTaskContinuationChipSpacing = 8;
28constexpr int kTaskContinuationChipVerticalPadding = 5;
29
30class HeaderView : public views::View {
31 public:
32 HeaderView() {
33 SetLayoutManager(std::make_unique<views::BoxLayout>(
34 views::BoxLayout::Orientation::kVertical, kPhoneHubSubHeaderPadding));
35 auto* header_label = AddChildView(std::make_unique<views::Label>(
36 l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TASK_CONTINUATION_TITLE)));
37 header_label->SetAutoColorReadabilityEnabled(false);
38 header_label->SetSubpixelRenderingEnabled(false);
39 header_label->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
40 AshColorProvider::ContentLayerType::kTextColorPrimary));
41 }
42
43 ~HeaderView() override = default;
44 HeaderView(HeaderView&) = delete;
45 HeaderView operator=(HeaderView&) = delete;
46
47 // views::View:
48 const char* GetClassName() const override { return "HeaderView"; }
49};
50
Andre Leed1afef2020-09-11 01:06:5551} // namespace
52
Andre Le63ca8fe2020-09-21 21:35:3453TaskContinuationView::TaskContinuationView(
54 chromeos::phonehub::PhoneModel* phone_model)
55 : phone_model_(phone_model) {
Meilin Wange95457a82020-09-24 02:51:5556 SetID(PhoneHubViewID::kTaskContinuationView);
57
Andre Le63ca8fe2020-09-21 21:35:3458 phone_model_->AddObserver(this);
59
Andre Leed1afef2020-09-11 01:06:5560 auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
61 views::BoxLayout::Orientation::kVertical, kTaskContinuationViewPadding,
62 kTaskContinuationHeaderSpacing));
63 layout->set_cross_axis_alignment(
64 views::BoxLayout::CrossAxisAlignment::kStart);
65
66 AddChildView(std::make_unique<HeaderView>());
Andre Le63ca8fe2020-09-21 21:35:3467 chips_view_ = AddChildView(std::make_unique<TaskChipsView>());
68
69 Update();
Andre Leed1afef2020-09-11 01:06:5570}
71
Andre Le63ca8fe2020-09-21 21:35:3472TaskContinuationView::~TaskContinuationView() {
73 phone_model_->RemoveObserver(this);
74}
75
76void TaskContinuationView::OnModelChanged() {
77 Update();
78}
Andre Leed1afef2020-09-11 01:06:5579
80const char* TaskContinuationView::GetClassName() const {
81 return "TaskContinuationView";
82}
83
Andre Le63ca8fe2020-09-21 21:35:3484TaskContinuationView::TaskChipsView::TaskChipsView() = default;
85
86TaskContinuationView::TaskChipsView::~TaskChipsView() = default;
87
88void TaskContinuationView::TaskChipsView::AddTaskChip(views::View* task_chip) {
89 int view_size = task_chips_.view_size();
90 task_chips_.Add(task_chip, view_size);
91 AddChildView(task_chip);
92}
93
94// views::View:
95gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const {
96 int width = kTaskContinuationChipSize.width() * kTaskContinuationChipsInRow +
97 kTaskContinuationChipSpacing;
98 int rows_num =
99 std::ceil((double)task_chips_.view_size() / kTaskContinuationChipsInRow);
100 int height = (kTaskContinuationChipSize.height() +
101 kTaskContinuationChipVerticalPadding) *
102 std::max(0, rows_num - 1) +
103 kTaskContinuationChipSize.height();
104 return gfx::Size(width, height);
105}
106
107void TaskContinuationView::TaskChipsView::Layout() {
108 views::View::Layout();
109 CalculateIdealBounds();
110 for (int i = 0; i < task_chips_.view_size(); ++i) {
111 auto* button = task_chips_.view_at(i);
112 button->SetBoundsRect(task_chips_.ideal_bounds(i));
113 }
114}
115
116const char* TaskContinuationView::TaskChipsView::GetClassName() const {
117 return "TaskChipsView";
118}
119
120void TaskContinuationView::TaskChipsView::Reset() {
121 task_chips_.Clear();
122 RemoveAllChildViews(true /* delete_children */);
123}
124
125gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) {
126 int row = index / kTaskContinuationChipsInRow;
127 int column = index % kTaskContinuationChipsInRow;
128 int x = (kTaskContinuationChipSize.width() + kTaskContinuationChipSpacing) *
129 column;
130 int y = (kTaskContinuationChipSize.height() +
131 kTaskContinuationChipVerticalPadding) *
132 row;
133 return gfx::Point(x, y);
134}
135
136void TaskContinuationView::TaskChipsView::CalculateIdealBounds() {
137 for (int i = 0; i < task_chips_.view_size(); ++i) {
138 gfx::Rect tile_bounds =
139 gfx::Rect(GetButtonPosition(i), kTaskContinuationChipSize);
140 task_chips_.set_ideal_bounds(i, tile_bounds);
141 }
142}
143
144void TaskContinuationView::Update() {
Andre Lebbc3cf32020-09-30 18:18:46145 chips_view_->Reset();
146
Andre Le63ca8fe2020-09-21 21:35:34147 if (!phone_model_->browser_tabs_model()) {
148 SetVisible(false);
149 return;
150 }
151
152 const BrowserTabsModel& browser_tabs =
153 phone_model_->browser_tabs_model().value();
154
155 if (!browser_tabs.is_tab_sync_enabled() ||
156 browser_tabs.most_recent_tabs().empty()) {
157 SetVisible(false);
158 return;
159 }
160
Andre Le63ca8fe2020-09-21 21:35:34161 for (const BrowserTabsModel::BrowserTabMetadata& metadata :
162 browser_tabs.most_recent_tabs()) {
163 chips_view_->AddTaskChip(new ContinueBrowsingChip(metadata));
164 }
165
Andre Lebbc3cf32020-09-30 18:18:46166 PreferredSizeChanged();
Andre Le63ca8fe2020-09-21 21:35:34167 SetVisible(true);
168}
169
Andre Leed1afef2020-09-11 01:06:55170} // namespace ash