Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 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" |
Crisrael Lucero | f1392ae | 2023-05-12 23:17:45 | [diff] [blame] | 9 | #include "ash/style/typography.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 10 | #include "ash/system/phonehub/continue_browsing_chip.h" |
Meilin Wang | e95457a8 | 2020-09-24 02:51:55 | [diff] [blame] | 11 | #include "ash/system/phonehub/phone_hub_view_ids.h" |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 12 | #include "ash/system/phonehub/ui_constants.h" |
| 13 | #include "ash/system/tray/tray_constants.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 14 | #include "ui/base/l10n/l10n_util.h" |
Allen Bauer | 912e8ff3 | 2023-11-08 21:13:11 | [diff] [blame] | 15 | #include "ui/base/metadata/metadata_header_macros.h" |
| 16 | #include "ui/base/metadata/metadata_impl_macros.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 17 | #include "ui/gfx/geometry/insets.h" |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 18 | #include "ui/gfx/text_constants.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 19 | #include "ui/views/controls/label.h" |
| 20 | #include "ui/views/layout/box_layout.h" |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 21 | |
| 22 | namespace ash { |
| 23 | |
| 24 | namespace { |
| 25 | |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 26 | // Appearance constants in dip. |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 27 | constexpr int kTaskContinuationChipHeight = 96; |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 28 | constexpr int kTaskContinuationChipsInRow = 2; |
| 29 | constexpr int kTaskContinuationChipSpacing = 8; |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 30 | constexpr int kTaskContinuationChipHorizontalSidePadding = 4; |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 31 | constexpr int kTaskContinuationChipVerticalPadding = 4; |
| 32 | constexpr int kHeaderLabelLineHeight = 48; |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 33 | |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 34 | gfx::Size GetTaskContinuationChipSize() { |
| 35 | int width = |
| 36 | (kTrayMenuWidth - kBubbleHorizontalSidePaddingDip * 2 - |
| 37 | kTaskContinuationChipHorizontalSidePadding * 2 - |
| 38 | kTaskContinuationChipSpacing * (kTaskContinuationChipsInRow - 1)) / |
| 39 | kTaskContinuationChipsInRow; |
| 40 | return gfx::Size(width, kTaskContinuationChipHeight); |
| 41 | } |
| 42 | |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 43 | class HeaderView : public views::Label { |
Allen Bauer | 912e8ff3 | 2023-11-08 21:13:11 | [diff] [blame] | 44 | METADATA_HEADER(HeaderView, views::Label) |
| 45 | |
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)); |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 50 | SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT); |
| 51 | SetVerticalAlignment(gfx::VerticalAlignment::ALIGN_MIDDLE); |
| 52 | SetAutoColorReadabilityEnabled(false); |
| 53 | SetSubpixelRenderingEnabled(false); |
Crisrael Lucero | ecff8b8 | 2024-01-29 21:20:32 | [diff] [blame] | 54 | // TODO(b/322067753): Replace usage of |AshColorProvider| with |
| 55 | // |cros_tokens|. |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 56 | SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor( |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 57 | AshColorProvider::ContentLayerType::kTextColorPrimary)); |
Crisrael Lucero | ecff8b8 | 2024-01-29 21:20:32 | [diff] [blame] | 58 | TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosButton1, |
| 59 | *this); |
Crisrael Lucero | f1392ae | 2023-05-12 23:17:45 | [diff] [blame] | 60 | SetLineHeight(kHeaderLabelLineHeight); |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | ~HeaderView() override = default; |
| 64 | HeaderView(HeaderView&) = delete; |
| 65 | HeaderView operator=(HeaderView&) = delete; |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 66 | }; |
| 67 | |
Allen Bauer | 912e8ff3 | 2023-11-08 21:13:11 | [diff] [blame] | 68 | BEGIN_METADATA(HeaderView) |
| 69 | END_METADATA |
| 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( |
Henrique Ferreiro | 91fb61df | 2021-11-12 15:51:03 | [diff] [blame] | 74 | phonehub::PhoneModel* phone_model, |
| 75 | phonehub::UserActionRecorder* user_action_recorder) |
Kyle Horimoto | ae02ab3 | 2020-12-11 00:35:47 | [diff] [blame] | 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 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 100 | TaskContinuationView::TaskChipsView::TaskChipsView() = default; |
| 101 | |
| 102 | TaskContinuationView::TaskChipsView::~TaskChipsView() = default; |
| 103 | |
| 104 | void TaskContinuationView::TaskChipsView::AddTaskChip(views::View* task_chip) { |
Peter Kasting | 991d3fc2 | 2022-06-24 19:06:02 | [diff] [blame] | 105 | size_t view_size = task_chips_.view_size(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 106 | task_chips_.Add(task_chip, view_size); |
| 107 | AddChildView(task_chip); |
| 108 | } |
| 109 | |
| 110 | // views::View: |
| 111 | gfx::Size TaskContinuationView::TaskChipsView::CalculatePreferredSize() const { |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 112 | auto chip_size = GetTaskContinuationChipSize(); |
| 113 | int width = chip_size.width() * kTaskContinuationChipsInRow + |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 114 | kTaskContinuationChipSpacing + |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 115 | 2 * kTaskContinuationChipHorizontalSidePadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 116 | int rows_num = |
| 117 | std::ceil((double)task_chips_.view_size() / kTaskContinuationChipsInRow); |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 118 | int height = (chip_size.height() + kTaskContinuationChipVerticalPadding) * |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 119 | std::max(0, rows_num - 1) + |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 120 | chip_size.height() + |
| 121 | 2 * kTaskContinuationChipHorizontalSidePadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 122 | return gfx::Size(width, height); |
| 123 | } |
| 124 | |
Peter Kasting | 1e3a1e99 | 2024-02-01 18:10:41 | [diff] [blame] | 125 | void TaskContinuationView::TaskChipsView::Layout(PassKey) { |
Peter Kasting | 6a838b54 | 2024-01-29 23:37:39 | [diff] [blame] | 126 | LayoutSuperclass<views::View>(this); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 127 | CalculateIdealBounds(); |
Peter Kasting | 991d3fc2 | 2022-06-24 19:06:02 | [diff] [blame] | 128 | for (size_t i = 0; i < task_chips_.view_size(); ++i) { |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 129 | auto* button = task_chips_.view_at(i); |
| 130 | button->SetBoundsRect(task_chips_.ideal_bounds(i)); |
| 131 | } |
| 132 | } |
| 133 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 134 | void TaskContinuationView::TaskChipsView::Reset() { |
| 135 | task_chips_.Clear(); |
Peter Boström | 91c5c833 | 2021-08-05 15:09:19 | [diff] [blame] | 136 | RemoveAllChildViews(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | gfx::Point TaskContinuationView::TaskChipsView::GetButtonPosition(int index) { |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 140 | auto chip_size = GetTaskContinuationChipSize(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 141 | int row = index / kTaskContinuationChipsInRow; |
| 142 | int column = index % kTaskContinuationChipsInRow; |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 143 | int x = (chip_size.width() + kTaskContinuationChipSpacing) * column + |
| 144 | kTaskContinuationChipHorizontalSidePadding; |
| 145 | int y = (chip_size.height() + kTaskContinuationChipVerticalPadding) * row + |
Meilin Wang | 24f7cf8 | 2020-10-26 23:13:12 | [diff] [blame] | 146 | kTaskContinuationChipVerticalPadding; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 147 | return gfx::Point(x, y); |
| 148 | } |
| 149 | |
| 150 | void TaskContinuationView::TaskChipsView::CalculateIdealBounds() { |
Peter Kasting | 991d3fc2 | 2022-06-24 19:06:02 | [diff] [blame] | 151 | for (size_t i = 0; i < task_chips_.view_size(); ++i) { |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 152 | gfx::Rect tile_bounds = |
Meilin Wang | c14a36f | 2020-10-31 00:17:51 | [diff] [blame] | 153 | gfx::Rect(GetButtonPosition(i), GetTaskContinuationChipSize()); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 154 | task_chips_.set_ideal_bounds(i, tile_bounds); |
| 155 | } |
| 156 | } |
| 157 | |
Allen Bauer | 7a42dd9 | 2024-02-16 23:08:54 | [diff] [blame] | 158 | BEGIN_METADATA(TaskContinuationView, TaskChipsView) |
Allen Bauer | 912e8ff3 | 2023-11-08 21:13:11 | [diff] [blame] | 159 | END_METADATA |
| 160 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 161 | void TaskContinuationView::Update() { |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame] | 162 | chips_view_->Reset(); |
| 163 | |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 164 | if (!phone_model_->browser_tabs_model()) { |
| 165 | SetVisible(false); |
| 166 | return; |
| 167 | } |
| 168 | |
Henrique Ferreiro | 91fb61df | 2021-11-12 15:51:03 | [diff] [blame] | 169 | const phonehub::BrowserTabsModel& browser_tabs = |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 170 | phone_model_->browser_tabs_model().value(); |
| 171 | |
| 172 | if (!browser_tabs.is_tab_sync_enabled() || |
| 173 | browser_tabs.most_recent_tabs().empty()) { |
| 174 | SetVisible(false); |
| 175 | return; |
| 176 | } |
| 177 | |
Tim Song | 97824893 | 2020-10-27 19:47:41 | [diff] [blame] | 178 | int index = 0; |
Henrique Ferreiro | 91fb61df | 2021-11-12 15:51:03 | [diff] [blame] | 179 | for (const phonehub::BrowserTabsModel::BrowserTabMetadata& metadata : |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 180 | browser_tabs.most_recent_tabs()) { |
Jimmy Gong | 776ab1f | 2020-12-03 06:16:25 | [diff] [blame] | 181 | chips_view_->AddTaskChip(new ContinueBrowsingChip( |
Kyle Horimoto | ae02ab3 | 2020-12-11 00:35:47 | [diff] [blame] | 182 | metadata, index, browser_tabs.most_recent_tabs().size(), |
| 183 | user_action_recorder_)); |
Tim Song | 97824893 | 2020-10-27 19:47:41 | [diff] [blame] | 184 | index++; |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 185 | } |
| 186 | |
Andre Le | bbc3cf3 | 2020-09-30 18:18:46 | [diff] [blame] | 187 | PreferredSizeChanged(); |
Andre Le | 63ca8fe | 2020-09-21 21:35:34 | [diff] [blame] | 188 | SetVisible(true); |
| 189 | } |
| 190 | |
Allen Bauer | 912e8ff3 | 2023-11-08 21:13:11 | [diff] [blame] | 191 | BEGIN_METADATA(TaskContinuationView) |
| 192 | END_METADATA |
| 193 | |
Andre Le | ed1afef | 2020-09-11 01:06:55 | [diff] [blame] | 194 | } // namespace ash |