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" |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 11 | #include "ash/system/phonehub/ui_constants.h" |
| 12 | #include "ash/system/tray/tray_constants.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 13 | #include "ui/base/l10n/l10n_util.h" |
| 14 | #include "ui/gfx/geometry/insets.h" |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 15 | #include "ui/gfx/text_constants.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 16 | #include "ui/views/controls/label.h" |
| 17 | #include "ui/views/layout/box_layout.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 18 | |
| 19 | namespace ash { |
| 20 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 21 | using BrowserTabsModel = chromeos::phonehub::BrowserTabsModel; |
| 22 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 23 | namespace { |
| 24 | |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 25 | // Appearance constants in dip. |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 26 | constexpr int kTaskContinuationChipHeight = 96; |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 27 | constexpr int kTaskContinuationChipsInRow = 2; |
| 28 | constexpr int kTaskContinuationChipSpacing = 8; |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 29 | constexpr int kTaskContinuationChipHorizontalSidePadding = 4; |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 30 | constexpr int kTaskContinuationChipVerticalPadding = 4; |
| 31 | constexpr int kHeaderLabelLineHeight = 48; |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 32 | |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 33 | // Typography. |
| 34 | constexpr int kHeaderTextFontSizeDip = 15; |
| 35 | |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 36 | gfx::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 Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 45 | class HeaderView : public views::Label { |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 46 | public: |
| 47 | HeaderView() { |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 48 | 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 Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 60 | 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 Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 71 | } // namespace |
| 72 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 73 | TaskContinuationView::TaskContinuationView( |
| 74 | chromeos::phonehub::PhoneModel* phone_model) |
| 75 | : phone_model_(phone_model) { |
Meilin Wang | e95457a8 | 2020-09-24 02:51:55 | [diff] [blame] | 76 | SetID(PhoneHubViewID::kTaskContinuationView); |
| 77 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 78 | phone_model_->AddObserver(this); |
| 79 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 80 | auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>( |
Meilin Wang | 5ab16ba | 2020-10-27 19:36:51 | [diff] [blame] | 81 | views::BoxLayout::Orientation::kVertical)); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 82 | layout->set_cross_axis_alignment( |
| 83 | views::BoxLayout::CrossAxisAlignment::kStart); |
| 84 | |
| 85 | AddChildView(std::make_unique<HeaderView>()); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 86 | chips_view_ = AddChildView(std::make_unique<TaskChipsView>()); |
| 87 | |
| 88 | Update(); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 89 | } |
| 90 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 91 | TaskContinuationView::~TaskContinuationView() { |
| 92 | phone_model_->RemoveObserver(this); |
| 93 | } |
| 94 | |
| 95 | void TaskContinuationView::OnModelChanged() { |
| 96 | Update(); |
| 97 | } |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 98 | |
| 99 | const char* TaskContinuationView::GetClassName() const { |
| 100 | return "TaskContinuationView"; |
| 101 | } |
| 102 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 103 | TaskContinuationView::TaskChipsView::TaskChipsView() = default; |
| 104 | |
| 105 | TaskContinuationView::TaskChipsView::~TaskChipsView() = default; |
| 106 | |
| 107 | void 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: |
| 114 | gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const { |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 115 | auto chip_size = GetTaskContinuationChipSize(); |
| 116 | int width = chip_size.width() * kTaskContinuationChipsInRow + |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 117 | kTaskContinuationChipSpacing + |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 118 | 2 * kTaskContinuationChipHorizontalSidePadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 119 | int rows_num = |
| 120 | std::ceil((double)task_chips_.view_size() / kTaskContinuationChipsInRow); |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 121 | int height = (chip_size.height() + kTaskContinuationChipVerticalPadding) * |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 122 | std::max(0, rows_num - 1) + |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 123 | chip_size.height() + |
| 124 | 2 * kTaskContinuationChipHorizontalSidePadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 125 | return gfx::Size(width, height); |
| 126 | } |
| 127 | |
| 128 | void 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 | |
| 137 | const char* TaskContinuationView::TaskChipsView::GetClassName() const { |
| 138 | return "TaskChipsView"; |
| 139 | } |
| 140 | |
| 141 | void TaskContinuationView::TaskChipsView::Reset() { |
| 142 | task_chips_.Clear(); |
| 143 | RemoveAllChildViews(true /* delete_children */); |
| 144 | } |
| 145 | |
| 146 | gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) { |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 147 | auto chip_size = GetTaskContinuationChipSize(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 148 | int row = index / kTaskContinuationChipsInRow; |
| 149 | int column = index % kTaskContinuationChipsInRow; |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 150 | int x = (chip_size.width() + kTaskContinuationChipSpacing) * column + |
| 151 | kTaskContinuationChipHorizontalSidePadding; |
| 152 | int y = (chip_size.height() + kTaskContinuationChipVerticalPadding) * row + |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 153 | kTaskContinuationChipVerticalPadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 154 | return gfx::Point(x, y); |
| 155 | } |
| 156 | |
| 157 | void TaskContinuationView::TaskChipsView::CalculateIdealBounds() { |
| 158 | for (int i = 0; i < task_chips_.view_size(); ++i) { |
| 159 | gfx::Rect tile_bounds = |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 160 | gfx::Rect(GetButtonPosition(i), GetTaskContinuationChipSize()); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 161 | task_chips_.set_ideal_bounds(i, tile_bounds); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void TaskContinuationView::Update() { |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame] | 166 | chips_view_->Reset(); |
| 167 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 168 | 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 Song | 97824893 | 2020-10-27 19:47:41 | [diff] [blame] | 182 | int index = 0; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 183 | for (const BrowserTabsModel::BrowserTabMetadata& metadata : |
| 184 | browser_tabs.most_recent_tabs()) { |
Jimmy Gong | 776ab1f | 2020-12-03 06:16:25 | [diff] [blame^] | 185 | chips_view_->AddTaskChip(new ContinueBrowsingChip( |
| 186 | metadata, index, browser_tabs.most_recent_tabs().size())); |
Tim Song | 97824893 | 2020-10-27 19:47:41 | [diff] [blame] | 187 | index++; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 188 | } |
| 189 | |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame] | 190 | PreferredSizeChanged(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 191 | SetVisible(true); |
| 192 | } |
| 193 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 194 | } // namespace ash |