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" |
| 13 | #include "ui/views/controls/label.h" |
| 14 | #include "ui/views/layout/box_layout.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 15 | |
| 16 | namespace ash { |
| 17 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 18 | using BrowserTabsModel = chromeos::phonehub::BrowserTabsModel; |
| 19 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | constexpr int kTaskContinuationHeaderSpacing = 8; |
| 23 | constexpr gfx::Insets kTaskContinuationViewPadding(12, 4); |
| 24 | constexpr gfx::Insets kPhoneHubSubHeaderPadding(4, 32); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 25 | constexpr gfx::Size kTaskContinuationChipSize(170, 80); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 26 | constexpr int kTaskContinuationChipsInRow = 2; |
| 27 | constexpr int kTaskContinuationChipSpacing = 8; |
| 28 | constexpr int kTaskContinuationChipVerticalPadding = 5; |
| 29 | |
| 30 | class 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 Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 51 | } // namespace |
| 52 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 53 | TaskContinuationView::TaskContinuationView( |
| 54 | chromeos::phonehub::PhoneModel* phone_model) |
| 55 | : phone_model_(phone_model) { |
Meilin Wang | e95457a8 | 2020-09-24 02:51:55 | [diff] [blame] | 56 | SetID(PhoneHubViewID::kTaskContinuationView); |
| 57 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 58 | phone_model_->AddObserver(this); |
| 59 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 60 | 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 Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 67 | chips_view_ = AddChildView(std::make_unique<TaskChipsView>()); |
| 68 | |
| 69 | Update(); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 70 | } |
| 71 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 72 | TaskContinuationView::~TaskContinuationView() { |
| 73 | phone_model_->RemoveObserver(this); |
| 74 | } |
| 75 | |
| 76 | void TaskContinuationView::OnModelChanged() { |
| 77 | Update(); |
| 78 | } |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 79 | |
| 80 | const char* TaskContinuationView::GetClassName() const { |
| 81 | return "TaskContinuationView"; |
| 82 | } |
| 83 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 84 | TaskContinuationView::TaskChipsView::TaskChipsView() = default; |
| 85 | |
| 86 | TaskContinuationView::TaskChipsView::~TaskChipsView() = default; |
| 87 | |
| 88 | void 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: |
| 95 | gfx::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 | |
| 107 | void 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 | |
| 116 | const char* TaskContinuationView::TaskChipsView::GetClassName() const { |
| 117 | return "TaskChipsView"; |
| 118 | } |
| 119 | |
| 120 | void TaskContinuationView::TaskChipsView::Reset() { |
| 121 | task_chips_.Clear(); |
| 122 | RemoveAllChildViews(true /* delete_children */); |
| 123 | } |
| 124 | |
| 125 | gfx::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 | |
| 136 | void 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 | |
| 144 | void TaskContinuationView::Update() { |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame^] | 145 | chips_view_->Reset(); |
| 146 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 147 | 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 Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 161 | for (const BrowserTabsModel::BrowserTabMetadata& metadata : |
| 162 | browser_tabs.most_recent_tabs()) { |
| 163 | chips_view_->AddTaskChip(new ContinueBrowsingChip(metadata)); |
| 164 | } |
| 165 | |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame^] | 166 | PreferredSizeChanged(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 167 | SetVisible(true); |
| 168 | } |
| 169 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 170 | } // namespace ash |