File: account_chooser_view.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (307 lines) | stat: -rw-r--r-- 13,221 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 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/save_to_drive/account_chooser_view.h"

#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/controls/hover_button.h"
#include "chrome/browser/ui/views/save_to_drive/account_chooser_radio_group_view.h"
#include "chrome/browser/ui/views/save_to_drive/account_chooser_util.h"
#include "chrome/grit/generated_resources.h"
#include "components/omnibox/browser/vector_icons.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/views/controls/button/md_text_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/separator.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget.h"

namespace save_to_drive {
AccountChooserView::AccountChooserView(
    AccountChooserControllerDelegate* account_chooser_controller_delegate,
    AccountChooserViewDelegate* parent_dialog,
    const std::vector<AccountInfo>& accounts,
    std::optional<CoreAccountId> primary_account_id)
    : account_chooser_controller_delegate_(account_chooser_controller_delegate),
      parent_dialog_(parent_dialog) {
  SetOrientation(views::LayoutOrientation::kVertical);
  header_view_ = AddChildView(CreateHeaderView(accounts));
  body_view_ = AddChildView(CreateBodyView(accounts, primary_account_id));
  footer_view_ = AddChildView(CreateFooterView());
}
AccountChooserView::~AccountChooserView() = default;

void AccountChooserView::UpdateView(
    const std::vector<AccountInfo>& accounts,
    std::optional<CoreAccountId> primary_account_id) {
  UpdateHeaderView(accounts);
  UpdateBodyView(accounts, primary_account_id);
}

std::unique_ptr<views::View> AccountChooserView::CreateBodyMultiAccount(
    const std::vector<AccountInfo>& accounts,
    std::optional<CoreAccountId> primary_account_id) {
  auto scroll_view = std::make_unique<views::ScrollView>();
  scroll_view->SetHorizontalScrollBarMode(
      views::ScrollView::ScrollBarMode::kDisabled);
  views::View* const content =
      scroll_view->SetContents(std::make_unique<AccountChooserRadioGroupView>(
          *parent_dialog_, accounts, primary_account_id));

  int per_account_size = content->GetPreferredSize().height() / accounts.size();
  scroll_view->ClipHeightTo(
      0, static_cast<int>(per_account_size * kMaxAccountsToShow));
  return scroll_view;
}

std::unique_ptr<views::View> AccountChooserView::CreateBodySingleAccount(
    const AccountInfo& account) {
  return views::Builder<views::FlexLayoutView>()
      .SetProperty(
          views::kFlexBehaviorKey,
          views::FlexSpecification(views::LayoutOrientation::kHorizontal,
                                   views::MinimumFlexSizeRule::kPreferred,
                                   views::MaximumFlexSizeRule::kUnbounded))
      .SetOrientation(views::LayoutOrientation::kVertical)
      .AddChildren(
          views::Builder<views::Separator>(),
          views::Builder<views::FlexLayoutView>()
              .SetOrientation(views::LayoutOrientation::kVertical)
              .SetInteriorMargin(gfx::Insets::VH(
                  /*vertical=*/ChromeLayoutProvider::Get()->GetDistanceMetric(
                      DISTANCE_EXTENSIONS_MENU_BUTTON_MARGIN),
                  /*horizontal=*/0))
              .AddChildren(
                  views::Builder<views::View>(CreateAccountRow(account))),
          views::Builder<views::Separator>())
      .Build();
}

std::unique_ptr<views::View> AccountChooserView::CreateBodyView(
    const std::vector<AccountInfo>& accounts,
    std::optional<CoreAccountId> primary_account_id) {
  CHECK(IsSingleAccount(accounts) || IsMultiAccount(accounts))
      << "Account chooser view should "
         "only be used if there are one or more accounts.";
  if (IsSingleAccount(accounts)) {
    parent_dialog_->OnAccountSelected(accounts.front());
    return CreateBodySingleAccount(accounts.front());
  } else {
    return CreateBodyMultiAccount(accounts, primary_account_id);
  }
}

std::unique_ptr<views::View> AccountChooserView::CreateDriveLogoView() {
  auto drive_logo_view = std::make_unique<views::BoxLayoutView>();
  drive_logo_view->SetCrossAxisAlignment(
      views::BoxLayout::CrossAxisAlignment::kCenter);
  drive_logo_view->SetBetweenChildSpacing(
      ChromeLayoutProvider::Get()->GetDistanceMetric(
          DISTANCE_EXTENSIONS_MENU_LABEL_ICON_SPACING));
  drive_logo_view->AddChildView(
      std::make_unique<views::ImageView>(ui::ImageModel::FromVectorIcon(
          omnibox::kDriveLogoIcon, ui::kColorIcon,
          ChromeLayoutProvider::Get()->GetDistanceMetric(
              DISTANCE_TOAST_BUBBLE_ICON_SIZE))));
  drive_logo_view->AddChildView(std::make_unique<views::Label>(
      l10n_util::GetStringUTF16(IDS_ACCOUNT_CHOOSER_DRIVE),
      views::style::CONTEXT_DIALOG_BODY_TEXT,
      views::style::STYLE_BODY_3_MEDIUM));
  return drive_logo_view;
}

std::unique_ptr<views::View> AccountChooserView::CreateFooterView() {
  const views::LayoutProvider* layout_provider = views::LayoutProvider::Get();
  auto footer = std::make_unique<views::FlexLayoutView>();
  footer->SetMainAxisAlignment(views::LayoutAlignment::kEnd);
  footer->SetIgnoreDefaultMainAxisMargins(true);
  footer->SetDefault(views::kMarginsKey,
                     gfx::Insets::TLBR(
                         /*top=*/0, /*left=*/
                         layout_provider->GetDistanceMetric(
                             views::DISTANCE_RELATED_BUTTON_HORIZONTAL),
                         /*bottom=*/0, /*right=*/0));
  footer->SetInteriorMargin(gfx::Insets::TLBR(
      /*top=*/ChromeLayoutProvider::Get()->GetDistanceMetric(
          DISTANCE_HORIZONTAL_SEPARATOR_PADDING_PAGE_INFO_VIEW),
      /*left=*/0,
      /*bottom=*/0,
      /*right=*/0));

  // Add the "Use a different account" button.
  auto add_account_button_container = std::make_unique<views::FlexLayoutView>();
  auto use_other_account_button = std::make_unique<views::MdTextButton>(
      base::BindRepeating(
          &AccountChooserControllerDelegate::ShowAddAccountDialog,
          base::Unretained(account_chooser_controller_delegate_)),
      l10n_util::GetStringUTF16(IDS_ACCOUNT_CHOOSER_ADD_ACCOUNT));
  use_other_account_button->SetStyle(ui::ButtonStyle::kDefault);
  use_other_account_button->SetAppearDisabledInInactiveWidget(true);
  add_account_button_container->AddChildView(
      std::move(use_other_account_button));
  // Ensure the button is left-aligned.
  add_account_button_container->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred,
                               views::MaximumFlexSizeRule::kUnbounded));
  footer->AddChildView(std::move(add_account_button_container));

  // Add the "Cancel" button.
  auto cancel_button = std::make_unique<views::MdTextButton>(
      base::BindRepeating(
          &AccountChooserViewDelegate::OnUserClosedDialog,
          base::Unretained(parent_dialog_),
          static_cast<int32_t>(
              views::Widget::ClosedReason::kCancelButtonClicked)),
      l10n_util::GetStringUTF16(IDS_CANCEL));
  cancel_button->SetStyle(ui::ButtonStyle::kTonal);
  cancel_button->SetAppearDisabledInInactiveWidget(true);
  footer->AddChildView(std::move(cancel_button));

  // Add the "Save" button.
  auto save_button = std::make_unique<views::MdTextButton>(
      // Save button eventually calls a OnceCallback
      base::BindOnce(&AccountChooserViewDelegate::OnSaveButtonClicked,
                     base::Unretained(parent_dialog_)),
      l10n_util::GetStringUTF16(IDS_SAVE));
  save_button->SetStyle(ui::ButtonStyle::kProminent);
  save_button->SetAppearDisabledInInactiveWidget(true);
  footer->AddChildView(std::move(save_button));

  return footer;
}

std::unique_ptr<views::View> AccountChooserView::CreateHeaderView(
    const std::vector<AccountInfo>& accounts) {
  auto header = std::make_unique<views::BoxLayoutView>();
  header->SetOrientation(views::BoxLayout::Orientation::kVertical);
  header->SetInsideBorderInsets(
      gfx::Insets::TLBR(0, 0,
                        ChromeLayoutProvider::Get()->GetDistanceMetric(
                            DISTANCE_UNRELATED_CONTROL_VERTICAL_LARGE),
                        0));
  header->SetMainAxisAlignment(views::BoxLayout::MainAxisAlignment::kCenter);
  views::FlexSpecification flex_spec(views::LayoutOrientation::kHorizontal,
                                     views::MinimumFlexSizeRule::kScaleToZero,
                                     views::MaximumFlexSizeRule::kUnbounded);
  header->SetProperty(views::kFlexBehaviorKey, flex_spec);

  // Add the title.
  header->AddChildView(CreateTitleView(accounts));

  // Add the subtitle.
  header->AddChildView(CreateSubtitleLabel());

  return header;
}

std::unique_ptr<views::Label> AccountChooserView::CreateTitleLabel(
    const std::vector<AccountInfo>& accounts) {
  auto title_label = std::make_unique<views::Label>(
      GetTitle(accounts), views::style::CONTEXT_DIALOG_TITLE,
      views::style::STYLE_HEADLINE_4_BOLD);
  SetLabelProperties(title_label.get());
  return title_label;
}

std::unique_ptr<views::StyledLabel> AccountChooserView::CreateSubtitleLabel() {
  auto subtitle_label = std::make_unique<views::StyledLabel>();

  subtitle_label->SetDefaultTextStyle(views::style::STYLE_BODY_3);
  subtitle_label->SetTextContext(views::style::CONTEXT_DIALOG_TITLE);

  std::u16string saved_from_chrome =
      l10n_util::GetStringUTF16(IDS_ACCOUNT_CHOOSER_SAVED_FROM_CHROME);

  // Find the offsets of the text to style.
  std::vector<size_t> offsets;
  std::u16string text = base::ReplaceStringPlaceholders(
      l10n_util::GetStringUTF16(IDS_ACCOUNT_CHOOSER_SUBTITLE),
      {saved_from_chrome}, &offsets);
  subtitle_label->SetText(text);
  subtitle_label->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
  gfx::Range saved_from_chrome_range(offsets[0],
                                     offsets[0] + saved_from_chrome.length());
  views::StyledLabel::RangeStyleInfo style_info;
  style_info.text_style = views::style::STYLE_BODY_3_MEDIUM;
  subtitle_label->AddStyleRange(saved_from_chrome_range, style_info);
  return subtitle_label;
}

std::unique_ptr<views::View> AccountChooserView::CreateTitleView(
    const std::vector<AccountInfo>& accounts) {
  auto title_view = std::make_unique<views::FlexLayoutView>();
  title_view->SetCrossAxisAlignment(views::LayoutAlignment::kCenter);

  auto title_container = std::make_unique<views::FlexLayoutView>();
  title_container->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::LayoutOrientation::kHorizontal,
                               views::MinimumFlexSizeRule::kPreferred,
                               views::MaximumFlexSizeRule::kUnbounded));
  title_view->AddChildView(CreateTitleLabel(accounts));
  title_view->AddChildView(CreateDriveLogoView());
  return title_view;
}

std::u16string AccountChooserView::GetTitle(
    const std::vector<AccountInfo>& accounts) {
  if (IsSingleAccount(accounts)) {
    return l10n_util::GetStringUTF16(IDS_ACCOUNT_CHOOSER_SINGLE_ACCOUNT_TITLE);
  } else {
    return l10n_util::GetStringUTF16(IDS_ACCOUNT_CHOOSER_MULTI_ACCOUNT_TITLE);
  }
}

bool AccountChooserView::IsMultiAccount(
    const std::vector<AccountInfo>& accounts) {
  return accounts.size() > 1;
}

bool AccountChooserView::IsSingleAccount(
    const std::vector<AccountInfo>& accounts) {
  return accounts.size() == 1;
}

void AccountChooserView::SetLabelProperties(views::Label* label) {
  label->SetMultiLine(true);
  label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label->SetAllowCharacterBreak(true);
  label->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::LayoutOrientation::kHorizontal,
                               views::MinimumFlexSizeRule::kScaleToZero,
                               views::MaximumFlexSizeRule::kUnbounded));
}

void AccountChooserView::UpdateBodyView(
    const std::vector<AccountInfo>& accounts,
    std::optional<CoreAccountId> primary_account_id) {
  std::optional<size_t> index = GetIndexOf(body_view_);
  CHECK(index.has_value());
  RemoveChildViewT(body_view_.ExtractAsDangling());
  body_view_ = AddChildViewAt(CreateBodyView(accounts, primary_account_id),
                              index.value());
}

void AccountChooserView::UpdateHeaderView(
    const std::vector<AccountInfo>& accounts) {
  std::optional<size_t> index = GetIndexOf(header_view_);
  CHECK(index.has_value());
  RemoveChildViewT(header_view_.ExtractAsDangling());
  header_view_ = AddChildViewAt(CreateHeaderView(accounts), index.value());
}

BEGIN_METADATA(AccountChooserView)
END_METADATA
}  // namespace save_to_drive