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
|
// Copyright 2021 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/update_address_profile_view.h"
#include <algorithm>
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/ui/views/autofill/autofill_bubble_utils.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/grit/theme_resources.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/ui/addresses/autofill_address_util.h"
#include "components/autofill/core/common/autofill_features.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_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/color/color_id.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/table_layout_view.h"
#include "ui/views/style/typography.h"
namespace autofill {
namespace {
constexpr int kIconSize = 16;
constexpr int kValuesLabelWidth = 190;
base::optional_ref<const gfx::VectorIcon> GetVectorIconForType(FieldType type) {
switch (GetAddressUIComponentIconTypeForFieldType(type)) {
case AddressUIComponentIconType::kNoIcon:
return std::nullopt;
case AddressUIComponentIconType::kName:
return kAccountCircleIcon;
case AddressUIComponentIconType::kAddress:
return vector_icons::kLocationOnIcon;
case AddressUIComponentIconType::kEmail:
return vector_icons::kEmailIcon;
case AddressUIComponentIconType::kPhone:
return vector_icons::kCallIcon;
}
}
// Creates a view that displays all values in `diff`. `are_new_values`
// decides which set of values from `diff` are displayed.
std::unique_ptr<views::View> CreateValuesView(
const std::vector<ProfileValueDifference>& diff,
bool are_new_values,
ui::ColorId icon_color) {
auto view = std::make_unique<views::View>();
view->SetLayoutManager(std::make_unique<views::FlexLayout>())
->SetOrientation(views::LayoutOrientation::kVertical)
.SetCrossAxisAlignment(views::LayoutAlignment::kStretch)
.SetIgnoreDefaultMainAxisMargins(true)
.SetCollapseMargins(true)
.SetDefault(
views::kMarginsKey,
gfx::Insets::VH(ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_CONTROL_LIST_VERTICAL),
0));
for (const ProfileValueDifference& diff_entry : diff) {
const std::u16string& value =
are_new_values ? diff_entry.first_value : diff_entry.second_value;
// Don't add rows for empty original values.
if (value.empty()) {
continue;
}
views::View* value_row =
view->AddChildView(std::make_unique<views::View>());
value_row->SetLayoutManager(std::make_unique<views::FlexLayout>())
->SetOrientation(views::LayoutOrientation::kHorizontal)
.SetCrossAxisAlignment(views::LayoutAlignment::kStart)
.SetIgnoreDefaultMainAxisMargins(true)
.SetCollapseMargins(true)
.SetDefault(
views::kMarginsKey,
gfx::Insets::VH(0, ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_LABEL_HORIZONTAL)));
auto label_view =
std::make_unique<views::Label>(value, views::style::CONTEXT_LABEL);
label_view->SetMultiLine(true);
label_view->SizeToFit(kValuesLabelWidth);
label_view->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
auto icon_view = std::make_unique<views::ImageView>();
base::optional_ref<const gfx::VectorIcon> icon_ref =
GetVectorIconForType(diff_entry.type);
if (icon_ref.has_value()) {
icon_view->SetImage(
ui::ImageModel::FromVectorIcon(*icon_ref, icon_color, kIconSize));
}
// The container aligns the icon vertically in the middle of the first label
// line, the icon size is expected to be smaller than the label height.
auto icon_container =
views::Builder<views::BoxLayoutView>()
.SetPreferredSize(gfx::Size(kIconSize, label_view->GetLineHeight()))
.SetCrossAxisAlignment(
views::BoxLayout::CrossAxisAlignment::kCenter)
.Build();
icon_container->AddChildView(std::move(icon_view));
value_row->AddChildView(std::move(icon_container));
value_row->AddChildView(std::move(label_view));
}
return view;
}
// Add a row in `layout` that contains a label and a view displays all the
// values in `values`. Labels are added only if `show_row_label` is true.
void AddValuesRow(views::TableLayoutView* layout_view,
const std::vector<ProfileValueDifference>& diff,
bool show_row_label,
views::Button::PressedCallback edit_button_callback) {
bool are_new_values = !!edit_button_callback;
layout_view->AddRows(1, /*vertical_resize=*/views::TableLayout::kFixedSize);
if (show_row_label) {
auto label = std::make_unique<views::Label>(
l10n_util::GetStringUTF16(
are_new_values
? IDS_AUTOFILL_UPDATE_ADDRESS_PROMPT_NEW_VALUES_SECTION_LABEL
: IDS_AUTOFILL_UPDATE_ADDRESS_PROMPT_OLD_VALUES_SECTION_LABEL),
views::style::CONTEXT_LABEL, views::style::STYLE_SECONDARY);
layout_view->AddChildView(std::move(label));
}
ui::ColorId icon_color = are_new_values ? ui::kColorButtonBackgroundProminent
: ui::kColorIconSecondary;
layout_view->AddChildView(CreateValuesView(diff, are_new_values, icon_color));
if (are_new_values) {
std::unique_ptr<views::ImageButton> edit_button =
CreateEditButton(std::move(edit_button_callback));
edit_button->SetProperty(views::kElementIdentifierKey,
UpdateAddressProfileView::kEditButtonViewId);
layout_view->AddChildView(std::move(edit_button));
}
}
// Returns true if there is there is at least one entry in `diff` with
// non-empty second value.
bool HasNonEmptySecondValues(const std::vector<ProfileValueDifference>& diff) {
return std::ranges::any_of(diff, [](const ProfileValueDifference& entry) {
return !entry.second_value.empty();
});
}
// Returns true if there is an entry coressponding to type ADDRESS_HOME_ADDRESS.
bool HasAddressEntry(const std::vector<ProfileValueDifference>& diff) {
return std::ranges::any_of(diff, [](const ProfileValueDifference& entry) {
return entry.type == ADDRESS_HOME_ADDRESS;
});
}
} // namespace
UpdateAddressProfileView::UpdateAddressProfileView(
views::View* anchor_view,
std::unique_ptr<UpdateAddressBubbleController> controller,
content::WebContents* web_contents)
: AddressBubbleBaseView(anchor_view, web_contents),
controller_(std::move(controller)) {
auto* layout_provider = views::LayoutProvider::Get();
SetAcceptCallback(
base::BindOnce(&UpdateAddressBubbleController::OnUserDecision,
base::Unretained(controller_.get()),
AutofillClient::AddressPromptUserDecision::kAccepted,
controller_->GetProfileToSave()));
SetCancelCallback(base::BindOnce(
&UpdateAddressBubbleController::OnUserDecision,
base::Unretained(controller_.get()),
AutofillClient::AddressPromptUserDecision::kDeclined, std::nullopt));
SetProperty(views::kElementIdentifierKey, kTopViewId);
SetTitle(controller_->GetWindowTitle());
SetButtonLabel(ui::mojom::DialogButton::kOk,
l10n_util::GetStringUTF16(
IDS_AUTOFILL_UPDATE_ADDRESS_PROMPT_OK_BUTTON_LABEL));
SetButtonLabel(ui::mojom::DialogButton::kCancel,
l10n_util::GetStringUTF16(
IDS_AUTOFILL_UPDATE_ADDRESS_PROMPT_CANCEL_BUTTON_LABEL));
SetLayoutManager(std::make_unique<views::FlexLayout>())
->SetOrientation(views::LayoutOrientation::kVertical)
.SetCrossAxisAlignment(views::LayoutAlignment::kStretch)
.SetIgnoreDefaultMainAxisMargins(true)
.SetCollapseMargins(true)
.SetDefault(views::kMarginsKey,
gfx::Insets::VH(layout_provider->GetDistanceMetric(
views::DISTANCE_CONTROL_LIST_VERTICAL),
0));
std::vector<ProfileValueDifference> profile_diff = GetProfileDifferenceForUi(
controller_->GetProfileToSave(), controller_->GetOriginalProfile(),
g_browser_process->GetApplicationLocale());
std::u16string subtitle = GetProfileDescription(
controller_->GetOriginalProfile(),
g_browser_process->GetApplicationLocale(),
/*include_address_and_contacts=*/!HasAddressEntry(profile_diff));
if (!subtitle.empty()) {
views::Label* subtitle_label = AddChildView(std::make_unique<views::Label>(
subtitle, views::style::CONTEXT_LABEL, views::style::STYLE_SECONDARY));
subtitle_label->SetHorizontalAlignment(
gfx::HorizontalAlignment::ALIGN_LEFT);
}
auto* main_content_view =
AddChildView(std::make_unique<views::TableLayoutView>());
bool has_non_empty_original_values = HasNonEmptySecondValues(profile_diff);
// Build the TableLayoutView columns.
const int column_divider = layout_provider->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_HORIZONTAL);
if (has_non_empty_original_values) {
// Label column exists only if there is a section for original values.
main_content_view
->AddColumn(
/*h_align=*/views::LayoutAlignment::kStart,
/*v_align=*/views::LayoutAlignment::kStart,
/*horizontal_resize=*/views::TableLayout::kFixedSize,
/*size_type=*/views::TableLayout::ColumnSize::kUsePreferred,
/*fixed_width=*/0, /*min_width=*/0)
.AddPaddingColumn(views::TableLayout::kFixedSize, column_divider);
}
main_content_view
->AddColumn(
/*h_align=*/views::LayoutAlignment::kStretch,
/*v_align=*/views::LayoutAlignment::kStretch,
/*horizontal_resize=*/1.0,
/*size_type=*/views::TableLayout::ColumnSize::kUsePreferred,
/*fixed_width=*/0, /*min_width=*/0)
.AddColumn(
/*h_align=*/views::LayoutAlignment::kStart,
/*v_align=*/views::LayoutAlignment::kStart,
/*horizontal_resize=*/views::TableLayout::kFixedSize,
/*size_type=*/views::TableLayout::ColumnSize::kUsePreferred,
/*fixed_width=*/0, /*min_width=*/0);
AddValuesRow(
main_content_view, profile_diff,
/*show_row_label=*/has_non_empty_original_values,
/*edit_button_callback=*/
base::BindRepeating(&UpdateAddressBubbleController::OnEditButtonClicked,
base::Unretained(controller_.get())));
if (has_non_empty_original_values) {
main_content_view->AddPaddingRow(
views::TableLayout::kFixedSize,
layout_provider->GetDistanceMetric(
DISTANCE_UNRELATED_CONTROL_VERTICAL_LARGE));
AddValuesRow(main_content_view, profile_diff, /*show_row_label=*/true,
/*edit_button_callback=*/{});
}
std::u16string footer_message = controller_->GetFooterMessage();
if (!footer_message.empty()) {
SetFootnoteView(
views::Builder<views::Label>()
.SetText(footer_message)
.SetTextContext(views::style::CONTEXT_BUBBLE_FOOTER)
.SetTextStyle(views::style::STYLE_SECONDARY)
.SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT)
.SetMultiLine(true)
.Build());
}
set_fixed_width(std::max(
main_content_view->GetPreferredSize().width() + margins().width(),
layout_provider->GetDistanceMetric(
views::DISTANCE_BUBBLE_PREFERRED_WIDTH)));
}
UpdateAddressProfileView::~UpdateAddressProfileView() = default;
bool UpdateAddressProfileView::ShouldShowCloseButton() const {
return true;
}
std::u16string UpdateAddressProfileView::GetWindowTitle() const {
return controller_->GetWindowTitle();
}
void UpdateAddressProfileView::WindowClosing() {
if (controller_) {
controller_->OnBubbleClosed();
controller_ = nullptr;
}
}
void UpdateAddressProfileView::Show(DisplayReason reason) {
ShowForReason(reason);
}
void UpdateAddressProfileView::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();
}
controller_ = nullptr;
}
BEGIN_METADATA(UpdateAddressProfileView)
END_METADATA
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(UpdateAddressProfileView, kTopViewId);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(UpdateAddressProfileView,
kEditButtonViewId);
} // namespace autofill
|