File: payments_view_util.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (370 lines) | stat: -rw-r--r-- 15,673 bytes parent folder | download | duplicates (2)
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
// Copyright 2019 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/autofill/payments/payments_view_util.h"

#include <algorithm>
#include <memory>
#include <optional>

#include "base/functional/bind.h"
#include "build/branding_buildflags.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/views/autofill/payments/dialog_view_ids.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "components/grit/components_scaled_resources.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/text_constants.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/border.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/separator.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/theme_tracking_image_view.h"
#include "ui/views/controls/throbber.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/style/typography.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace autofill {

namespace {

// Width of the Google Pay logo if used, as it is not square.
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
constexpr int kGooglePayLogoWidth = 40;
#endif
constexpr int kIconHeight = 16;
// The size of the icons in the view created by `CreateTextWithIconView()`.
constexpr int kTextWithIconViewIconSize = 24;

std::unique_ptr<views::ImageView> CreateIconView(
    TitleWithIconAfterLabelView::Icon icon_to_show,
    const base::RepeatingCallback<ui::ColorVariant()>&
        get_background_color_callback) {
  ui::ImageModel model;
  std::optional<ui::ImageModel> model_dark;
  switch (icon_to_show) {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
    case TitleWithIconAfterLabelView::Icon::GOOGLE_PAY:
      model = ui::ImageModel::FromImageGenerator(
          base::BindRepeating(&CreateTiledGooglePayLogo, kGooglePayLogoWidth,
                              kIconHeight),
          gfx::Size(kGooglePayLogoWidth, kIconHeight));
      break;
    case TitleWithIconAfterLabelView::Icon::GOOGLE_PAY_AND_AFFIRM:
      model = ui::ImageModel::FromImageSkia(
          *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
              IDR_AUTOFILL_GOOGLE_PAY_AFFIRM));
      model_dark = ui::ImageModel::FromImageSkia(
          *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
              IDR_AUTOFILL_GOOGLE_PAY_AFFIRM_DARK));
      break;
    case TitleWithIconAfterLabelView::Icon::GOOGLE_PAY_AND_AFTERPAY:
      model = ui::ImageModel::FromImageSkia(
          *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
              IDR_AUTOFILL_GOOGLE_PAY_AFTERPAY));
      model_dark = ui::ImageModel::FromImageSkia(
          *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
              IDR_AUTOFILL_GOOGLE_PAY_AFTERPAY_DARK));
      break;
    case TitleWithIconAfterLabelView::Icon::GOOGLE_PAY_AND_ZIP:
      model = ui::ImageModel::FromImageSkia(
          *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
              IDR_AUTOFILL_GOOGLE_PAY_ZIP));
      model_dark = ui::ImageModel::FromImageSkia(
          *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
              IDR_AUTOFILL_GOOGLE_PAY_ZIP_DARK));
      break;
    case TitleWithIconAfterLabelView::Icon::GOOGLE_G: {
      const gfx::VectorIcon& icon = vector_icons::kGoogleGLogoIcon;
#else
    case TitleWithIconAfterLabelView::Icon::GOOGLE_PAY:
    case TitleWithIconAfterLabelView::Icon::GOOGLE_G:
    case TitleWithIconAfterLabelView::Icon::GOOGLE_PAY_AND_AFFIRM:
    case TitleWithIconAfterLabelView::Icon::GOOGLE_PAY_AND_AFTERPAY:
    case TitleWithIconAfterLabelView::Icon::GOOGLE_PAY_AND_ZIP: {
      const gfx::VectorIcon& icon = kCreditCardIcon;
#endif
      model = ui::ImageModel::FromVectorIcon(icon, ui::kColorIcon, kIconHeight);
      break;
    }
  }
  return std::make_unique<views::ThemeTrackingImageView>(
      model, model_dark.value_or(model), get_background_color_callback);
}

}  // namespace

TextLinkInfo::TextLinkInfo() = default;

TextLinkInfo::TextLinkInfo(const TextLinkInfo& other) = default;
TextLinkInfo& TextLinkInfo::operator=(const TextLinkInfo& other) = default;
TextLinkInfo::TextLinkInfo(TextLinkInfo&& other) = default;
TextLinkInfo& TextLinkInfo::operator=(TextLinkInfo&& other) = default;

TextLinkInfo::~TextLinkInfo() = default;

ui::ImageModel GetProfileAvatar(const AccountInfo& account_info) {
  // Get the user avatar icon.
  gfx::Image account_avatar = account_info.account_image;

  // Check if the avatar is empty, and if so, replace it with a placeholder.
  if (account_avatar.IsEmpty()) {
    account_avatar = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
        profiles::GetPlaceholderAvatarIconResourceID());
  }

  int avatar_size = views::TypographyProvider::Get().GetLineHeight(
      views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_SECONDARY);

  return ui::ImageModel::FromImage(profiles::GetSizedAvatarIcon(
      account_avatar, avatar_size, avatar_size, profiles::SHAPE_CIRCLE));
}

TitleWithIconAfterLabelView::TitleWithIconAfterLabelView(
    const std::u16string& window_title,
    TitleWithIconAfterLabelView::Icon icon_to_show) {
  SetBetweenChildSpacing(ChromeLayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_RELATED_LABEL_HORIZONTAL));
  // Align to the top instead of center in vertical direction so that we
  // can adjust the icon location to align with the first line of title label
  SetCrossAxisAlignment(views::BoxLayout::CrossAxisAlignment::kStart);

  auto* title_label = AddChildView(std::make_unique<views::Label>(
      window_title, views::style::CONTEXT_DIALOG_TITLE));
  title_label->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
  title_label->SetMultiLine(true);
  auto* icon_view = AddChildView(CreateIconView(
      icon_to_show,
      base::BindRepeating(
          [](views::View* view) {
            return ui::ColorVariant(
                view->GetColorProvider()->GetColor(ui::kColorDialogBackground));
          },
          base::Unretained(this))));

  // Center the icon against the first line of the title label. This needs to be
  // done after we create the title label, so that we can use its preferred
  // size. Depending on which height is larger, the inset will be applied to the
  // view with the smaller height.
  const int title_label_height =
      title_label->GetPreferredSize(views::SizeBounds(title_label->width(), {}))
          .height();
  const int icon_image_height = icon_view->GetImageModel().Size().height();
  if (title_label_height > icon_image_height) {
    icon_view->SetBorder(views::CreateEmptyBorder(
        gfx::Insets().set_top((title_label_height - icon_image_height) / 2)));
  } else {
    title_label->SetBorder(views::CreateEmptyBorder(
        gfx::Insets().set_top((icon_image_height - title_label_height) / 2)));
  }

  // Flex |title_label| to fill up remaining space and tail align the GPay icon.
  SetFlexForView(title_label, 1);
}

TitleWithIconAfterLabelView::~TitleWithIconAfterLabelView() = default;

// TODO(crbug.com/40914021): Replace GetMinimumSize() may generate views
// narrower than expected. The ideal solution should be limit the width of
// multi-line text views.
gfx::Size TitleWithIconAfterLabelView::GetMinimumSize() const {
  // Default View::GetMinimumSize() will make dialogs wider than it should.
  // To avoid that, just return 0x0.
  return gfx::Size(0, 0);
}

BEGIN_METADATA(TitleWithIconAfterLabelView)
END_METADATA

PaymentsUiClosedReason GetPaymentsUiClosedReasonFromWidget(
    const views::Widget* widget) {
  DCHECK(widget);
  if (!widget->IsClosed()) {
    return PaymentsUiClosedReason::kUnknown;
  }

  switch (widget->closed_reason()) {
    case views::Widget::ClosedReason::kUnspecified:
      return PaymentsUiClosedReason::kNotInteracted;
    case views::Widget::ClosedReason::kEscKeyPressed:
    case views::Widget::ClosedReason::kCloseButtonClicked:
      return PaymentsUiClosedReason::kClosed;
    case views::Widget::ClosedReason::kLostFocus:
      return PaymentsUiClosedReason::kLostFocus;
    case views::Widget::ClosedReason::kAcceptButtonClicked:
      return PaymentsUiClosedReason::kAccepted;
    case views::Widget::ClosedReason::kCancelButtonClicked:
      return PaymentsUiClosedReason::kCancelled;
  }
}

std::unique_ptr<views::View> CreateLegalMessageView(
    const LegalMessageLines& legal_message_lines,
    const std::u16string& user_email,
    const ui::ImageModel& user_avatar,
    base::RepeatingCallback<void(const GURL&)> callback) {
  auto result = views::Builder<views::BoxLayoutView>()
                    .SetOrientation(views::BoxLayout::Orientation::kVertical)
                    .SetBetweenChildSpacing(
                        ChromeLayoutProvider::Get()->GetDistanceMetric(
                            DISTANCE_RELATED_CONTROL_VERTICAL_SMALL))
                    .Build();
  for (const LegalMessageLine& line : legal_message_lines) {
    auto label = views::Builder<views::StyledLabel>()
                     .SetText(line.text())
                     .SetTextContext(CONTEXT_DIALOG_BODY_TEXT_SMALL)
                     .SetDefaultTextStyle(views::style::STYLE_SECONDARY)
                     .Build();
    for (const LegalMessageLine::Link& link : line.links()) {
      label->AddStyleRange(link.range,
                           views::StyledLabel::RangeStyleInfo::CreateForLink(
                               base::BindRepeating(callback, link.url)));
    }
    result->AddChildView(std::move(label));
  }

  if (user_email.empty() || user_avatar.IsEmpty()) {
    return result;
  }

  // Extra child view for user identity information including the avatar and
  // the email.
  result->AddChildView(
      views::Builder<views::BoxLayoutView>()
          .SetOrientation(views::BoxLayout::Orientation::kHorizontal)
          .SetBetweenChildSpacing(
              ChromeLayoutProvider::Get()->GetDistanceMetric(
                  DISTANCE_RELATED_CONTROL_HORIZONTAL_SMALL))
          .SetID(DialogViewId::USER_INFORMATION_VIEW)
          .AddChildren(views::Builder<views::ImageView>().SetImage(user_avatar),
                       views::Builder<views::Label>()
                           .SetText(user_email)
                           .SetTextContext(CONTEXT_DIALOG_BODY_TEXT_SMALL)
                           .SetTextStyle(views::style::STYLE_SECONDARY))
          .Build());
  return result;
}

std::unique_ptr<views::View> CreateProgressBarWithTextView(
    const std::u16string& progress_bar_text) {
  views::Throbber* throbber = nullptr;
  auto result =
      views::Builder<views::BoxLayoutView>()
          .SetOrientation(views::BoxLayout::Orientation::kVertical)
          .SetBetweenChildSpacing(
              ChromeLayoutProvider::Get()->GetDistanceMetric(
                  views::DISTANCE_RELATED_CONTROL_VERTICAL))
          .SetCrossAxisAlignment(views::BoxLayout::CrossAxisAlignment::kCenter)
          .AddChildren(
              views::Builder<views::Throbber>().CopyAddressTo(&throbber),
              views::Builder<views::Label>()
                  .SetText(progress_bar_text)
                  .SetEnabledColor(ui::kColorThrobber))
          .Build();
  throbber->Start();
  return result;
}

std::unique_ptr<views::View> CreateTextWithIconView(
    const std::u16string& text,
    std::optional<TextLinkInfo> text_link_info,
    const gfx::VectorIcon& icon) {
  ChromeLayoutProvider* chrome_layout_provider = ChromeLayoutProvider::Get();

  views::Builder<views::BoxLayoutView> view_builder =
      views::Builder<views::BoxLayoutView>()
          .SetOrientation(views::BoxLayout::Orientation::kHorizontal)
          .SetBetweenChildSpacing(chrome_layout_provider->GetDistanceMetric(
              views::DISTANCE_RELATED_LABEL_HORIZONTAL))
          .AddChild(
              views::Builder<views::ImageView>()
                  .SetVerticalAlignment(views::ImageView::Alignment::kLeading)
                  .SetBorder(views::CreateEmptyBorder(
                      chrome_layout_provider->GetInsetsMetric(
                          views::INSETS_LABEL_BUTTON)))
                  .SetImage(ui::ImageModel::FromVectorIcon(
                      icon, ui::kColorIcon, kTextWithIconViewIconSize)));

  if (text_link_info.has_value()) {
    view_builder.AddChild(
        views::Builder<views::StyledLabel>()
            .SetTextContext(views::style::CONTEXT_DIALOG_BODY_TEXT)
            .SetDefaultTextStyle(views::style::STYLE_SECONDARY)
            .SetText(text)
            .AddStyleRange(text_link_info->offset,
                           views::StyledLabel::RangeStyleInfo::CreateForLink(
                               text_link_info->callback)));
  } else {
    view_builder.AddChild(
        views::Builder<views::Label>()
            .SetMultiLine(true)
            .SetHorizontalAlignment(gfx::ALIGN_TO_HEAD)
            .SetTextContext(views::style::CONTEXT_DIALOG_BODY_TEXT)
            .SetTextStyle(views::style::STYLE_SECONDARY)
            .SetText(text));
  }

  return std::move(view_builder).Build();
}

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
// kGooglePayLogoIcon is square overall, despite the drawn portion being a
// rectangular area at the top. CreateTiledImage() will correctly clip it
// whereas setting the icon size would rescale it incorrectly and keep the
// bottom empty portion.
gfx::ImageSkia CreateTiledGooglePayLogo(int width,
                                        int height,
                                        const ui::ColorProvider* provider) {
  return gfx::ImageSkiaOperations::CreateTiledImage(
      gfx::CreateVectorIcon(vector_icons::kGooglePayLogoIcon,
                            provider->GetColor(kColorPaymentsGooglePayLogo)),
      /*x=*/0, /*y=*/0, width, height);
}
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

std::unique_ptr<views::View> CreateLabelAndTextfieldView(
    const std::u16string& text) {
  auto view_builder =
      views::Builder<views::BoxLayoutView>()
          .SetOrientation(views::BoxLayout::Orientation::kVertical)
          .SetBetweenChildSpacing(
              ChromeLayoutProvider::Get()->GetDistanceMetric(
                  DISTANCE_RELATED_CONTROL_VERTICAL_SMALL));

  view_builder.AddChild(views::Builder<views::Label>()
                            .SetText(text)
                            .SetTextContext(views::style::CONTEXT_LABEL)
                            .SetTextStyle(views::style::STYLE_PRIMARY)
                            .SetHorizontalAlignment(gfx::ALIGN_TO_HEAD));

  view_builder.AddChild(
      views::Builder<views::Textfield>().SetAccessibleName(text));

  return std::move(view_builder).Build();
}

}  // namespace autofill