Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 1 | // 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 Wang | e95457a8 | 2020-09-24 02:51:55 | [diff] [blame] | 10 | #include "ash/system/phonehub/phone_hub_view_ids.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 11 | #include "ui/base/l10n/l10n_util.h" |
| 12 | #include "ui/gfx/geometry/insets.h" |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 13 | #include "ui/gfx/text_constants.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 14 | #include "ui/views/controls/label.h" |
| 15 | #include "ui/views/layout/box_layout.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 16 | |
| 17 | namespace ash { |
| 18 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 19 | using BrowserTabsModel = chromeos::phonehub::BrowserTabsModel; |
| 20 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 21 | namespace { |
| 22 | |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 23 | // Appearance constants in dip. |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 24 | constexpr gfx::Size kTaskContinuationChipSize(176, 96); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 25 | constexpr int kTaskContinuationChipsInRow = 2; |
| 26 | constexpr int kTaskContinuationChipSpacing = 8; |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 27 | constexpr int kTaskContinuationChipHorizontalPadding = 4; |
| 28 | constexpr int kTaskContinuationChipVerticalPadding = 4; |
| 29 | constexpr int kHeaderLabelLineHeight = 48; |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 30 | |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 31 | // Typography. |
| 32 | constexpr int kHeaderTextFontSizeDip = 15; |
| 33 | |
| 34 | class HeaderView : public views::Label { |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 35 | public: |
| 36 | HeaderView() { |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 37 | 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 Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 49 | 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 Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 60 | } // namespace |
| 61 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 62 | TaskContinuationView::TaskContinuationView( |
| 63 | chromeos::phonehub::PhoneModel* phone_model) |
| 64 | : phone_model_(phone_model) { |
Meilin Wang | e95457a8 | 2020-09-24 02:51:55 | [diff] [blame] | 65 | SetID(PhoneHubViewID::kTaskContinuationView); |
| 66 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 67 | phone_model_->AddObserver(this); |
| 68 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 69 | auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>( |
Meilin Wang | 5ab16ba | 2020-10-27 19:36:51 | [diff] [blame] | 70 | views::BoxLayout::Orientation::kVertical)); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 71 | layout->set_cross_axis_alignment( |
| 72 | views::BoxLayout::CrossAxisAlignment::kStart); |
| 73 | |
| 74 | AddChildView(std::make_unique<HeaderView>()); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 75 | chips_view_ = AddChildView(std::make_unique<TaskChipsView>()); |
| 76 | |
| 77 | Update(); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 78 | } |
| 79 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 80 | TaskContinuationView::~TaskContinuationView() { |
| 81 | phone_model_->RemoveObserver(this); |
| 82 | } |
| 83 | |
| 84 | void TaskContinuationView::OnModelChanged() { |
| 85 | Update(); |
| 86 | } |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 87 | |
| 88 | const char* TaskContinuationView::GetClassName() const { |
| 89 | return "TaskContinuationView"; |
| 90 | } |
| 91 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 92 | TaskContinuationView::TaskChipsView::TaskChipsView() = default; |
| 93 | |
| 94 | TaskContinuationView::TaskChipsView::~TaskChipsView() = default; |
| 95 | |
| 96 | void 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: |
| 103 | gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const { |
| 104 | int width = kTaskContinuationChipSize.width() * kTaskContinuationChipsInRow + |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 105 | kTaskContinuationChipSpacing + |
| 106 | 2 * kTaskContinuationChipHorizontalPadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 107 | 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 Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 112 | kTaskContinuationChipSize.height() + |
| 113 | 2 * kTaskContinuationChipHorizontalPadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 114 | return gfx::Size(width, height); |
| 115 | } |
| 116 | |
| 117 | void 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 | |
| 126 | const char* TaskContinuationView::TaskChipsView::GetClassName() const { |
| 127 | return "TaskChipsView"; |
| 128 | } |
| 129 | |
| 130 | void TaskContinuationView::TaskChipsView::Reset() { |
| 131 | task_chips_.Clear(); |
| 132 | RemoveAllChildViews(true /* delete_children */); |
| 133 | } |
| 134 | |
| 135 | gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) { |
| 136 | int row = index / kTaskContinuationChipsInRow; |
| 137 | int column = index % kTaskContinuationChipsInRow; |
| 138 | int x = (kTaskContinuationChipSize.width() + kTaskContinuationChipSpacing) * |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 139 | column + |
| 140 | kTaskContinuationChipHorizontalPadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 141 | int y = (kTaskContinuationChipSize.height() + |
| 142 | kTaskContinuationChipVerticalPadding) * |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 143 | row + |
| 144 | kTaskContinuationChipVerticalPadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 145 | return gfx::Point(x, y); |
| 146 | } |
| 147 | |
| 148 | void 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 | |
| 156 | void TaskContinuationView::Update() { |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame] | 157 | chips_view_->Reset(); |
| 158 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 159 | 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 Song | 97824893 | 2020-10-27 19:47:41 | [diff] [blame^] | 173 | int index = 0; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 174 | for (const BrowserTabsModel::BrowserTabMetadata& metadata : |
| 175 | browser_tabs.most_recent_tabs()) { |
Tim Song | 97824893 | 2020-10-27 19:47:41 | [diff] [blame^] | 176 | chips_view_->AddTaskChip(new ContinueBrowsingChip(metadata, index)); |
| 177 | index++; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 178 | } |
| 179 | |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame] | 180 | PreferredSizeChanged(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 181 | SetVisible(true); |
| 182 | } |
| 183 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 184 | } // namespace ash |