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 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/autofill/payments/save_iban_bubble_view.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/autofill/payments/save_iban_ui.h"
#include "chrome/browser/ui/views/accessibility/theme_tracking_non_accessible_image_view.h"
#include "chrome/browser/ui/views/autofill/payments/dialog_view_ids.h"
#include "chrome/browser/ui/views/autofill/payments/payments_view_util.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/autofill/core/browser/data_model/payments/iban.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/vector_icon_utils.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/throbber.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/view_class_properties.h"
namespace autofill {
namespace {
const int kMaxNicknameChars = 25;
} // namespace
SaveIbanBubbleView::SaveIbanBubbleView(views::View* anchor_view,
content::WebContents* web_contents,
IbanBubbleController* controller)
: AutofillLocationBarBubble(anchor_view, web_contents),
controller_(controller) {
DCHECK(controller);
SetButtonLabel(ui::mojom::DialogButton::kOk,
controller->GetAcceptButtonText());
SetButtonLabel(ui::mojom::DialogButton::kCancel,
controller->GetDeclineButtonText());
SetShowCloseButton(true);
set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_BUBBLE_PREFERRED_WIDTH));
}
void SaveIbanBubbleView::Show(DisplayReason reason) {
ShowForReason(reason);
AssignIdsToDialogButtonsForTesting(); // IN-TEST
}
void SaveIbanBubbleView::Hide() {
CloseBubble();
// If `controller_` is null, WindowClosing() won't invoke OnBubbleClosed(), so
// do that here. This will clear out `controller_`'s reference to `this`. Note
// that WindowClosing() happens only after the _asynchronous_ Close() task
// posted in CloseBubble() completes, but we need to fix references sooner.
if (controller_) {
controller_->OnBubbleClosed(
GetPaymentsUiClosedReasonFromWidget(GetWidget()));
}
controller_ = nullptr;
}
void SaveIbanBubbleView::AddedToWidget() {
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
GetBubbleFrameView()->SetHeaderView(
std::make_unique<ThemeTrackingNonAccessibleImageView>(
*bundle.GetImageSkiaNamed(IDR_SAVE_CARD),
*bundle.GetImageSkiaNamed(IDR_SAVE_CARD_DARK),
base::BindRepeating(&views::BubbleDialogDelegate::background_color,
base::Unretained(this))));
if (controller_->IsUploadSave()) {
GetBubbleFrameView()->SetTitleView(
std::make_unique<TitleWithIconAfterLabelView>(
GetWindowTitle(), TitleWithIconAfterLabelView::Icon::GOOGLE_PAY));
}
}
std::u16string SaveIbanBubbleView::GetWindowTitle() const {
return controller_ ? controller_->GetWindowTitle() : std::u16string();
}
void SaveIbanBubbleView::WindowClosing() {
if (controller_) {
controller_->OnBubbleClosed(
GetPaymentsUiClosedReasonFromWidget(GetWidget()));
controller_ = nullptr;
}
}
void SaveIbanBubbleView::ContentsChanged(views::Textfield* sender,
const std::u16string& new_contents) {
if (new_contents.length() > kMaxNicknameChars) {
nickname_textfield_->SetText(new_contents.substr(0, kMaxNicknameChars));
}
// Update the IBAN nickname count label to current_length/max_length,
// e.g. "6/25".
UpdateNicknameLengthLabel();
}
SaveIbanBubbleView::~SaveIbanBubbleView() = default;
void SaveIbanBubbleView::CreateMainContentView() {
const ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical, gfx::Insets(),
provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_VERTICAL)));
SetID(controller_->IsUploadSave() ? DialogViewId::MAIN_CONTENT_VIEW_UPLOAD
: DialogViewId::MAIN_CONTENT_VIEW_LOCAL);
SetProperty(views::kMarginsKey, gfx::Insets());
// If applicable, add the upload explanation label. Appears above the IBAN
// info.
std::u16string explanation = controller_->GetExplanatoryMessage();
if (!explanation.empty()) {
auto* explanation_label = AddChildView(std::make_unique<views::Label>(
explanation, views::style::CONTEXT_DIALOG_BODY_TEXT,
views::style::STYLE_SECONDARY));
explanation_label->SetMultiLine(true);
explanation_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
}
const int row_height = views::TypographyProvider::Get().GetLineHeight(
views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_PRIMARY);
auto* iban_content = AddChildView(std::make_unique<views::View>());
auto* iban_layout =
iban_content->SetLayoutManager(std::make_unique<views::TableLayout>());
iban_layout
->AddColumn(views::LayoutAlignment::kStart,
views::LayoutAlignment::kCenter,
views::TableLayout::kFixedSize,
views::TableLayout::ColumnSize::kUsePreferred, 0, 0)
.AddPaddingColumn(
views::TableLayout::kFixedSize,
provider->GetDistanceMetric(views::DISTANCE_RELATED_LABEL_HORIZONTAL))
.AddColumn(views::LayoutAlignment::kStretch,
views::LayoutAlignment::kStretch, 1.0,
views::TableLayout::ColumnSize::kFixed, 0, 0)
// Add a row for IBAN label and the value of IBAN. It might happen that
// the revealed IBAN value is too long to fit in a single line while the
// obscured IBAN value can fit in one line, so fix the height to fit both
// cases so toggling visibility does not change the bubble's overall
// height.
.AddRows(1, views::TableLayout::kFixedSize, row_height * 2)
.AddPaddingRow(views::TableLayout::kFixedSize,
ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL))
// Add a row for nickname label and the input text field.
.AddRows(1, views::TableLayout::kFixedSize);
iban_content->AddChildView(std::make_unique<views::Label>(
l10n_util::GetStringUTF16(IDS_AUTOFILL_SAVE_IBAN_LABEL),
views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_PRIMARY));
views::Label* iban_value =
iban_content->AddChildView(std::make_unique<views::Label>(
controller_->GetIban().GetIdentifierStringForAutofillDisplay(
/*is_value_masked=*/false),
views::style::CONTEXT_LABEL, views::style::STYLE_SECONDARY));
iban_value->SetProperty(
views::kFlexBehaviorKey,
views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred,
views::MaximumFlexSizeRule::kScaleToMaximum));
iban_value->SetHorizontalAlignment(gfx::ALIGN_LEFT);
iban_value->SetMultiLine(true);
iban_content->AddChildView(std::make_unique<views::Label>(
l10n_util::GetStringUTF16(IDS_AUTOFILL_SAVE_IBAN_PROMPT_NICKNAME),
views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_PRIMARY));
// Adds view that combines nickname textfield and nickname length count label.
auto* nickname_input_textfield_view =
iban_content->AddChildView(std::make_unique<views::BoxLayoutView>());
nickname_input_textfield_view->SetBorder(
views::CreateSolidBorder(1, SK_ColorLTGRAY));
nickname_input_textfield_view->SetInsideBorderInsets(
views::LayoutProvider::Get()->GetInsetsMetric(
views::InsetsMetric::INSETS_LABEL_BUTTON));
nickname_input_textfield_view->SetOrientation(
views::BoxLayout::Orientation::kHorizontal);
nickname_input_textfield_view->SetBetweenChildSpacing(
ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL));
// Adds nickname textfield.
nickname_textfield_ = nickname_input_textfield_view->AddChildView(
std::make_unique<views::Textfield>());
nickname_textfield_->GetViewAccessibility().SetName(
l10n_util::GetStringUTF16(IDS_AUTOFILL_SAVE_IBAN_PROMPT_NICKNAME));
nickname_textfield_->SetTextInputType(
ui::TextInputType::TEXT_INPUT_TYPE_TEXT);
nickname_textfield_->set_controller(this);
nickname_textfield_->SetPlaceholderText(
l10n_util::GetStringUTF16(IDS_AUTOFILL_SAVE_IBAN_PLACEHOLDER));
nickname_textfield_->SetProperty(views::kBoxLayoutFlexKey,
views::BoxLayoutFlexSpecification());
nickname_textfield_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
nickname_textfield_->SetBorder(views::NullBorder());
// Adds nickname length count label.
// Note: nickname is empty at the prompt.
nickname_length_label_ = nickname_input_textfield_view->AddChildView(
std::make_unique<views::Label>(/*text=*/u"", views::style::CONTEXT_LABEL,
views::style::STYLE_SECONDARY));
nickname_length_label_->SetHorizontalAlignment(
gfx::HorizontalAlignment::ALIGN_RIGHT);
UpdateNicknameLengthLabel();
if (std::unique_ptr<views::View> legal_message_view =
CreateLegalMessageView()) {
legal_message_view->SetID(DialogViewId::LEGAL_MESSAGE_VIEW);
AddChildView(std::move(legal_message_view));
}
}
void SaveIbanBubbleView::AssignIdsToDialogButtonsForTesting() {
auto* ok_button = GetOkButton();
if (ok_button) {
ok_button->SetID(DialogViewId::OK_BUTTON);
}
auto* cancel_button = GetCancelButton();
if (cancel_button) {
cancel_button->SetID(DialogViewId::CANCEL_BUTTON);
}
if (nickname_textfield_) {
nickname_textfield_->SetID(DialogViewId::NICKNAME_TEXTFIELD);
}
}
void SaveIbanBubbleView::LinkClicked(const GURL& url) {
if (controller_) {
controller()->OnLegalMessageLinkClicked(url);
}
}
void SaveIbanBubbleView::Init() {
// For server IBAN save, there is an explanation between the title and the
// controls; use DialogContentType::kText. For local IBAN save, since there is
// no explanation, use DialogContentType::kControl instead.
// For server IBANs, there are legal messages before the buttons, so use
// DialogContentType::kText. For local IBANs, since there is no legal message,
// use DialogContentType::kControl instead.
set_margins(ChromeLayoutProvider::Get()->GetDialogInsetsForContentType(
views::DialogContentType::kText,
!controller_->GetLegalMessageLines().empty()
? views::DialogContentType::kText
: views::DialogContentType::kControl));
CreateMainContentView();
if (controller_ &&
(controller_->GetBubbleType() == IbanBubbleType::kUploadSave ||
controller_->GetBubbleType() == IbanBubbleType::kUploadInProgress)) {
loading_row_ = AddChildView(CreateLoadingRow());
if (controller_->GetBubbleType() == IbanBubbleType::kUploadInProgress) {
ShowThrobber();
}
}
}
bool SaveIbanBubbleView::Accept() {
bool show_throbber = controller_ && controller_->GetBubbleType() ==
IbanBubbleType::kUploadSave;
if (show_throbber) {
ShowThrobber();
}
if (controller_) {
DCHECK(nickname_textfield_);
controller_->OnAcceptButton(nickname_textfield_->GetText());
}
// If a throbber is shown, don't automatically close the bubble view upon
// acceptance.
return !show_throbber;
}
std::unique_ptr<views::View> SaveIbanBubbleView::CreateLegalMessageView() {
const LegalMessageLines message_lines = controller()->GetLegalMessageLines();
if (message_lines.empty()) {
return nullptr;
}
auto legal_message_view = std::make_unique<views::BoxLayoutView>();
legal_message_view->SetOrientation(views::BoxLayout::Orientation::kVertical);
legal_message_view->SetBetweenChildSpacing(
ChromeLayoutProvider::Get()->GetDistanceMetric(
DISTANCE_RELATED_CONTROL_VERTICAL_SMALL));
legal_message_view->AddChildView(::autofill::CreateLegalMessageView(
message_lines, base::UTF8ToUTF16(controller()->GetAccountInfo().email),
GetProfileAvatar(controller()->GetAccountInfo()),
base::BindRepeating(&SaveIbanBubbleView::LinkClicked,
base::Unretained(this))));
legal_message_view->SetID(DialogViewId::LEGAL_MESSAGE_VIEW);
return legal_message_view;
}
std::unique_ptr<views::View> SaveIbanBubbleView::CreateLoadingRow() {
auto loading_row = std::make_unique<views::BoxLayoutView>();
// Initialize `loading_row` as hidden because it should only be visible after
// the user accepts uploading the card.
loading_row->SetVisible(false);
loading_row->SetMainAxisAlignment(views::BoxLayout::MainAxisAlignment::kEnd);
loading_row->SetInsideBorderInsets(gfx::Insets::TLBR(10, 0, 0, 40));
loading_throbber_ =
loading_row->AddChildView(std::make_unique<views::Throbber>());
loading_throbber_->SetID(DialogViewId::LOADING_THROBBER);
return loading_row;
}
void SaveIbanBubbleView::ShowThrobber() {
if (loading_row_ == nullptr) {
return;
}
SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
SetExtraView({nullptr});
CHECK(loading_throbber_);
loading_throbber_->Start();
loading_row_->SetVisible(true);
loading_throbber_->GetViewAccessibility().AnnounceText(
l10n_util::GetStringUTF16(
IDS_AUTOFILL_SAVE_IBAN_PROMPT_LOADING_THROBBER_ACCESSIBLE_NAME));
DialogModelChanged();
}
void SaveIbanBubbleView::UpdateNicknameLengthLabel() {
nickname_length_label_->SetText(l10n_util::GetStringFUTF16(
IDS_IBAN_NICKNAME_COUNT_BY,
base::NumberToString16(nickname_textfield_->GetText().length()),
base::NumberToString16(kMaxNicknameChars)));
}
BEGIN_METADATA(SaveIbanBubbleView)
END_METADATA
} // namespace autofill
|