File: fade_footer_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (372 lines) | stat: -rw-r--r-- 14,101 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/tabs/fade_footer_view.h"

#include "base/check.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/tabs/alert/tab_alert.h"
#include "chrome/browser/ui/tabs/alert/tab_alert_icon.h"
#include "chrome/browser/ui/views/tabs/alert_indicator_button.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/base/text/bytes_formatting.h"
#include "ui/base/ui_base_features.h"
#include "ui/color/color_id.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/color_palette.h"
#include "ui/views/border.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"

namespace {
// Spacing to separate the icon from its corresponding label.
constexpr int kIconLabelSpacing = 8;
// Margins used to surround the entire footer contents.
constexpr auto kFooterMargins = gfx::Insets::VH(12, 12);
// Spacing used to separate two footer rows.
constexpr int kFooterRowSpacing = 8;

ui::ColorId GetTabAlertColor(tabs::TabAlert alert_state) {
  // Hover card background color isn't affected by third party themes so icons
  // need to use a different color id from those used in the
  // AlertIndicatorButton.
  ui::ColorId icon_color = gfx::kPlaceholderColor;
  switch (alert_state) {
    case tabs::TabAlert::MEDIA_RECORDING:
    case tabs::TabAlert::AUDIO_RECORDING:
    case tabs::TabAlert::VIDEO_RECORDING:
    case tabs::TabAlert::DESKTOP_CAPTURING:
      icon_color = kColorHoverCardTabAlertMediaRecordingIcon;
      break;
    case tabs::TabAlert::TAB_CAPTURING:
    case tabs::TabAlert::PIP_PLAYING:
    case tabs::TabAlert::GLIC_ACCESSING:
    case tabs::TabAlert::GLIC_SHARING:
      icon_color = kColorHoverCardTabAlertPipPlayingIcon;
      break;
    case tabs::TabAlert::AUDIO_PLAYING:
    case tabs::TabAlert::AUDIO_MUTING:
    case tabs::TabAlert::BLUETOOTH_CONNECTED:
    case tabs::TabAlert::BLUETOOTH_SCAN_ACTIVE:
    case tabs::TabAlert::USB_CONNECTED:
    case tabs::TabAlert::HID_CONNECTED:
    case tabs::TabAlert::SERIAL_CONNECTED:
    case tabs::TabAlert::VR_PRESENTING_IN_HEADSET:
      icon_color = kColorHoverCardTabAlertAudioPlayingIcon;
      break;
  }

  return icon_color;
}
}  // namespace

template <typename T>
FooterRow<T>::FooterRow(bool is_fade_out_view)
    : is_fade_out_view_(is_fade_out_view) {
  views::FlexLayout* flex_layout =
      views::View::SetLayoutManager(std::make_unique<views::FlexLayout>());
  flex_layout->SetOrientation(views::LayoutOrientation::kHorizontal)
      .SetCrossAxisAlignment(views::LayoutAlignment::kStart);

  icon_ = views::View::AddChildView(std::make_unique<views::ImageView>());

  if (is_fade_out_view) {
    icon_->SetPaintToLayer();
    icon_->layer()->SetOpacity(0.0f);
  }

  icon_->SetVerticalAlignment(views::ImageView::Alignment::kLeading);
  footer_label_ = views::View::AddChildView(std::make_unique<views::Label>(
      std::u16string(), views::style::CONTEXT_DIALOG_BODY_TEXT));
  icon_->SetBackground(
      views::CreateSolidBackground(ui::kColorBubbleFooterBackground));
  footer_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  footer_label_->SetMultiLine(true);
  footer_label_->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::LayoutOrientation::kHorizontal,
                               views::MinimumFlexSizeRule::kScaleToZero,
                               views::MaximumFlexSizeRule::kUnbounded, true));

  footer_label_->SetEnabledColor(kColorTabHoverCardSecondaryText);
  footer_label_->SetTextStyle(views::style::STYLE_BODY_4);

  // Vertically align the icon to the top line of the label
  const int offset = (footer_label_->GetLineHeight() -
                      GetLayoutConstant(TAB_ALERT_INDICATOR_ICON_WIDTH)) /
                     2;
  icon_->SetProperty(views::kMarginsKey,
                     gfx::Insets::TLBR(offset, 0, 0, kIconLabelSpacing));
}

template <typename T>
void FooterRow<T>::SetContent(const ui::ImageModel& icon_image_model,
                              std::u16string label_text) {
  footer_label_->SetText(label_text);
  icon_->SetImage(icon_image_model);
}

template <typename T>
gfx::Size FooterRow<T>::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  return footer_label_->GetText().empty()
             ? gfx::Size()
             : views::View::CalculatePreferredSize(available_size);
}

template <typename T>
gfx::Size FooterRow<T>::GetMinimumSize() const {
  return gfx::Size();
}

template <typename T>
void FooterRow<T>::SetFade(double percent) {
  CHECK(is_fade_out_view_);
  percent = std::min(1.0, percent);
  icon_->layer()->SetOpacity(1.0 - percent);
  const SkAlpha alpha = base::saturated_cast<SkAlpha>(
      std::numeric_limits<SkAlpha>::max() * (1.0 - percent));
  footer_label_->SetBackgroundColor(
      SkColorSetA(footer_label_->GetBackgroundColor(), alpha));
  footer_label_->SetEnabledColor(
      SkColorSetA(footer_label_->GetEnabledColor(), alpha));
}

using FooterRow_AlertFooterRowData = FooterRow<AlertFooterRowData>;
BEGIN_TEMPLATE_METADATA(FooterRow_AlertFooterRowData, FooterRow)
END_METADATA

using FooterRow_PerformanceRowData = FooterRow<PerformanceRowData>;
BEGIN_TEMPLATE_METADATA(FooterRow_PerformanceRowData, FooterRow)
END_METADATA

using FooterRow_CollaborationMessagingRowData =
    FooterRow<CollaborationMessagingRowData>;
BEGIN_TEMPLATE_METADATA(FooterRow_CollaborationMessagingRowData, FooterRow)
END_METADATA

template class FooterRow<AlertFooterRowData>;
template class FooterRow<PerformanceRowData>;
template class FooterRow<CollaborationMessagingRowData>;

// FadeAlertFooterRow
// -----------------------------------------------------------------------

void FadeAlertFooterRow::SetData(const AlertFooterRowData& data) {
  std::optional<tabs::TabAlert> alert_state = data.alert_state;
  if (data.should_show_discard_status) {
    std::u16string row_text;
    if (data.memory_savings_in_bytes > 0) {
      const std::u16string formatted_memory_usage =
          ui::FormatBytes(data.memory_savings_in_bytes);
      row_text = l10n_util::GetStringFUTF16(
          IDS_HOVERCARD_INACTIVE_TAB_MEMORY_SAVINGS, formatted_memory_usage);
    } else {
      row_text = l10n_util::GetStringUTF16(IDS_HOVERCARD_INACTIVE_TAB);
    }
    SetContent(ui::ImageModel::FromVectorIcon(
                   kPerformanceSpeedometerIcon,
                   kColorHoverCardTabAlertAudioPlayingIcon,
                   GetLayoutConstant(TAB_ALERT_INDICATOR_ICON_WIDTH)),
               row_text);
  } else if (alert_state.has_value()) {
    const tabs::TabAlert alert = alert_state.value();
    SetContent(tabs::GetAlertImageModel(alert, GetTabAlertColor(alert)),
               GetTabAlertStateText(alert));
  } else {
    SetContent(ui::ImageModel(), std::u16string());
  }
  data_ = data;
}

BEGIN_METADATA(FadeAlertFooterRow)
END_METADATA

// FadePerformanceFooterRow
// -----------------------------------------------------------------------

void FadePerformanceFooterRow::SetData(const PerformanceRowData& data) {
  if (data.show_memory_usage) {
    const std::u16string formatted_memory_usage =
        ui::FormatBytes(data.memory_usage_in_bytes);
    const std::u16string row_text = l10n_util::GetStringFUTF16(
        data.is_high_memory_usage ? IDS_HOVERCARD_TAB_HIGH_MEMORY_USAGE
                                  : IDS_HOVERCARD_TAB_MEMORY_USAGE,
        formatted_memory_usage);

    const ui::ImageModel icon_image_model = ui::ImageModel::FromVectorIcon(
        kPerformanceSpeedometerIcon, kColorHoverCardTabAlertAudioPlayingIcon,
        GetLayoutConstant(TAB_ALERT_INDICATOR_ICON_WIDTH));
    SetContent(icon_image_model, row_text);
  } else {
    SetContent(ui::ImageModel(), std::u16string());
  }

  data_ = data;
}

BEGIN_METADATA(FadePerformanceFooterRow)
END_METADATA

// FadeCollaborationMessagingFooterRow
// -----------------------------------------------------------------------

void FadeCollaborationMessagingFooterRow::SetData(
    const CollaborationMessagingRowData& data) {
  data_ = data;

  if (!data_.should_show_collaboration_messaging) {
    // Empty section if collaboration messaging should be hidden.
    SetContent(ui::ImageModel(), std::u16string());
    return;
  }

  SetContent(data_.avatar, data_.text);
}

CollaborationMessagingRowData::CollaborationMessagingRowData() = default;
CollaborationMessagingRowData::~CollaborationMessagingRowData() = default;
CollaborationMessagingRowData::CollaborationMessagingRowData(
    const CollaborationMessagingRowData& other) = default;
CollaborationMessagingRowData& CollaborationMessagingRowData::operator=(
    const CollaborationMessagingRowData& other) = default;

BEGIN_METADATA(FadeCollaborationMessagingFooterRow)
END_METADATA

// FooterView
// -----------------------------------------------------------------------

DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(FooterView, kHoverCardFooterElementId);

FooterView::FooterView() {
  SetProperty(views::kElementIdentifierKey, kHoverCardFooterElementId);
  flex_layout_ =
      views::View::SetLayoutManager(std::make_unique<views::FlexLayout>());
  flex_layout_->SetOrientation(views::LayoutOrientation::kVertical)
      .SetMainAxisAlignment(views::LayoutAlignment::kStart)
      .SetCollapseMargins(true)
      .SetInteriorMargin(kFooterMargins)
      .SetDefault(views::kMarginsKey, gfx::Insets::VH(kFooterRowSpacing, 0));
  alert_row_ = AddChildView(std::make_unique<AlertFadeView>(
      std::make_unique<FadeAlertFooterRow>(/* is_fade_out_view =*/false),
      std::make_unique<FadeAlertFooterRow>(/* is_fade_out_view =*/true)));

  performance_row_ = AddChildView(std::make_unique<PerformanceFadeView>(
      std::make_unique<FadePerformanceFooterRow>(/* is_fade_out_view =*/false),
      std::make_unique<FadePerformanceFooterRow>(/* is_fade_out_view =*/true)));

  collaboration_messaging_row_ =
      AddChildView(std::make_unique<CollaborationMessagingFadeView>(
          std::make_unique<FadeCollaborationMessagingFooterRow>(
              /* is_fade_out_view =*/false),
          std::make_unique<FadeCollaborationMessagingFooterRow>(
              /* is_fade_out_view =*/true)));

  alert_row_->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::LayoutOrientation::kHorizontal,
                               views::MinimumFlexSizeRule::kScaleToMinimum,
                               views::MaximumFlexSizeRule::kUnbounded, true));

  performance_row_->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::LayoutOrientation::kHorizontal,
                               views::MinimumFlexSizeRule::kScaleToMinimum,
                               views::MaximumFlexSizeRule::kUnbounded, true));

  collaboration_messaging_row_->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::LayoutOrientation::kHorizontal,
                               views::MinimumFlexSizeRule::kScaleToMinimum,
                               views::MaximumFlexSizeRule::kUnbounded, true));

  SetBackground(views::CreateSolidBackground(ui::kColorBubbleFooterBackground));
}

void FooterView::SetAlertData(const AlertFooterRowData& data) {
  alert_row_->SetData(data);
  UpdateVisibility();
}

void FooterView::SetPerformanceData(const PerformanceRowData& data) {
  performance_row_->SetData(data);
  UpdateVisibility();
}

void FooterView::SetCollaborationMessagingData(
    const CollaborationMessagingRowData& data) {
  collaboration_messaging_row_->SetData(data);
  UpdateVisibility();
}

void FooterView::SetFade(double percent) {
  alert_row_->SetFade(percent);
  performance_row_->SetFade(percent);
  collaboration_messaging_row_->SetFade(percent);
}

void FooterView::UpdateVisibility() {
  SetVisible(performance_row_->CalculatePreferredSize({}).height() > 0 ||
             alert_row_->CalculatePreferredSize({}).height() > 0 ||
             collaboration_messaging_row_->CalculatePreferredSize({}).height() >
                 0);
}

using FadeWrapper_View_PerformanceRowData =
    FadeWrapper<views::View, PerformanceRowData>;

BEGIN_TEMPLATE_METADATA(FadeWrapper_View_PerformanceRowData, FadeWrapper)
END_METADATA

using FadeWrapper_View_AlertFooterRowData =
    FadeWrapper<views::View, AlertFooterRowData>;

BEGIN_TEMPLATE_METADATA(FadeWrapper_View_AlertFooterRowData, FadeWrapper)
END_METADATA

using FadeWrapper_View_CollaborationMessagingRowData =
    FadeWrapper<views::View, CollaborationMessagingRowData>;

BEGIN_TEMPLATE_METADATA(FadeWrapper_View_CollaborationMessagingRowData,
                        FadeWrapper)
END_METADATA

using FadeView_FadeAlertFooterRow_FadeAlertFooterRow_AlertFooterRowData =
    FadeView<FadeAlertFooterRow, FadeAlertFooterRow, AlertFooterRowData>;

BEGIN_TEMPLATE_METADATA(
    FadeView_FadeAlertFooterRow_FadeAlertFooterRow_AlertFooterRowData,
    FadeView)
END_METADATA

using FadeView_FadePerformanceFooterRow_FadePerformanceFooterRow_PerformanceRowData =
    FadeView<FadePerformanceFooterRow,
             FadePerformanceFooterRow,
             PerformanceRowData>;

BEGIN_TEMPLATE_METADATA(
    FadeView_FadePerformanceFooterRow_FadePerformanceFooterRow_PerformanceRowData,
    FadeView)
END_METADATA

using FadeView_FadeCollaborationMessagingFooterRow_FadeCollaborationMessagingFooterRow_CollaborationMessagingRowData =
    FadeView<FadeCollaborationMessagingFooterRow,
             FadeCollaborationMessagingFooterRow,
             CollaborationMessagingRowData>;

BEGIN_TEMPLATE_METADATA(
    FadeView_FadeCollaborationMessagingFooterRow_FadeCollaborationMessagingFooterRow_CollaborationMessagingRowData,
    FadeView)
END_METADATA

BEGIN_METADATA(FooterView)
END_METADATA