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( |
Kyle Horimoto | ae02ab3 | 2020-12-11 00:35:47 | [diff] [blame^] | 74 | chromeos::phonehub::PhoneModel* phone_model, |
| 75 | chromeos::phonehub::UserActionRecorder* user_action_recorder) |
| 76 | : phone_model_(phone_model), user_action_recorder_(user_action_recorder) { |
Meilin Wang | e95457a8 | 2020-09-24 02:51:55 | [diff] [blame] | 77 | SetID(PhoneHubViewID::kTaskContinuationView); |
| 78 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 79 | phone_model_->AddObserver(this); |
| 80 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 81 | auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>( |
Meilin Wang | 5ab16ba | 2020-10-27 19:36:51 | [diff] [blame] | 82 | views::BoxLayout::Orientation::kVertical)); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 83 | layout->set_cross_axis_alignment( |
| 84 | views::BoxLayout::CrossAxisAlignment::kStart); |
| 85 | |
| 86 | AddChildView(std::make_unique<HeaderView>()); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 87 | chips_view_ = AddChildView(std::make_unique<TaskChipsView>()); |
| 88 | |
| 89 | Update(); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 90 | } |
| 91 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 92 | TaskContinuationView::~TaskContinuationView() { |
| 93 | phone_model_->RemoveObserver(this); |
| 94 | } |
| 95 | |
| 96 | void TaskContinuationView::OnModelChanged() { |
| 97 | Update(); |
| 98 | } |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 99 | |
| 100 | const char* TaskContinuationView::GetClassName() const { |
| 101 | return "TaskContinuationView"; |
| 102 | } |
| 103 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 104 | TaskContinuationView::TaskChipsView::TaskChipsView() = default; |
| 105 | |
| 106 | TaskContinuationView::TaskChipsView::~TaskChipsView() = default; |
| 107 | |
| 108 | void TaskContinuationView::TaskChipsView::AddTaskChip(views::View* task_chip) { |
| 109 | int view_size = task_chips_.view_size(); |
| 110 | task_chips_.Add(task_chip, view_size); |
| 111 | AddChildView(task_chip); |
| 112 | } |
| 113 | |
| 114 | // views::View: |
| 115 | gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const { |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 116 | auto chip_size = GetTaskContinuationChipSize(); |
| 117 | int width = chip_size.width() * kTaskContinuationChipsInRow + |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 118 | kTaskContinuationChipSpacing + |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 119 | 2 * kTaskContinuationChipHorizontalSidePadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 120 | int rows_num = |
| 121 | std::ceil((double)task_chips_.view_size() / kTaskContinuationChipsInRow); |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 122 | int height = (chip_size.height() + kTaskContinuationChipVerticalPadding) * |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 123 | std::max(0, rows_num - 1) + |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 124 | chip_size.height() + |
| 125 | 2 * kTaskContinuationChipHorizontalSidePadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 126 | return gfx::Size(width, height); |
| 127 | } |
| 128 | |
| 129 | void TaskContinuationView::TaskChipsView::Layout() { |
| 130 | views::View::Layout(); |
| 131 | CalculateIdealBounds(); |
| 132 | for (int i = 0; i < task_chips_.view_size(); ++i) { |
| 133 | auto* button = task_chips_.view_at(i); |
| 134 | button->SetBoundsRect(task_chips_.ideal_bounds(i)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | const char* TaskContinuationView::TaskChipsView::GetClassName() const { |
| 139 | return "TaskChipsView"; |
| 140 | } |
| 141 | |
| 142 | void TaskContinuationView::TaskChipsView::Reset() { |
| 143 | task_chips_.Clear(); |
| 144 | RemoveAllChildViews(true /* delete_children */); |
| 145 | } |
| 146 | |
| 147 | gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) { |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 148 | auto chip_size = GetTaskContinuationChipSize(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 149 | int row = index / kTaskContinuationChipsInRow; |
| 150 | int column = index % kTaskContinuationChipsInRow; |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 151 | int x = (chip_size.width() + kTaskContinuationChipSpacing) * column + |
| 152 | kTaskContinuationChipHorizontalSidePadding; |
| 153 | int y = (chip_size.height() + kTaskContinuationChipVerticalPadding) * row + |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 154 | kTaskContinuationChipVerticalPadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 155 | return gfx::Point(x, y); |
| 156 | } |
| 157 | |
| 158 | void TaskContinuationView::TaskChipsView::CalculateIdealBounds() { |
| 159 | for (int i = 0; i < task_chips_.view_size(); ++i) { |
| 160 | gfx::Rect tile_bounds = |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 161 | gfx::Rect(GetButtonPosition(i), GetTaskContinuationChipSize()); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 162 | task_chips_.set_ideal_bounds(i, tile_bounds); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void TaskContinuationView::Update() { |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame] | 167 | chips_view_->Reset(); |
| 168 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 169 | if (!phone_model_->browser_tabs_model()) { |
| 170 | SetVisible(false); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | const BrowserTabsModel& browser_tabs = |
| 175 | phone_model_->browser_tabs_model().value(); |
| 176 | |
| 177 | if (!browser_tabs.is_tab_sync_enabled() || |
| 178 | browser_tabs.most_recent_tabs().empty()) { |
| 179 | SetVisible(false); |
| 180 | return; |
| 181 | } |
| 182 | |
Tim Song | 97824893 | 2020-10-27 19:47:41 | [diff] [blame] | 183 | int index = 0; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 184 | for (const BrowserTabsModel::BrowserTabMetadata& metadata : |
| 185 | browser_tabs.most_recent_tabs()) { |
Jimmy Gong | 776ab1f | 2020-12-03 06:16:25 | [diff] [blame] | 186 | chips_view_->AddTaskChip(new ContinueBrowsingChip( |
Kyle Horimoto | ae02ab3 | 2020-12-11 00:35:47 | [diff] [blame^] | 187 | metadata, index, browser_tabs.most_recent_tabs().size(), |
| 188 | user_action_recorder_)); |
Tim Song | 97824893 | 2020-10-27 19:47:41 | [diff] [blame] | 189 | index++; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 190 | } |
| 191 | |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame] | 192 | PreferredSizeChanged(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 193 | SetVisible(true); |
| 194 | } |
| 195 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 196 | } // namespace ash |