File: combined_selector_views.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (285 lines) | stat: -rw-r--r-- 10,736 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
// 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/webauthn/combined_selector_views.h"

#include <algorithm>
#include <cstddef>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/strings/string_util.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/webauthn/sheet_models.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/events/event.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/radio_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/separator.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/layout/table_layout_view.h"
#include "ui/views/style/typography.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"

namespace {

// This value is used to group all `CombinedSelectorRadioButton` which are under
// the same `CombinedSelectorRowView`. The grouping is used for traversal and
// selection. The value is selected voluntarily in order not to conflict
// grouping from any parent views.
constexpr static int kGroupId = 1327;

}  // namespace

CombinedSelectorRadioButton::CombinedSelectorRadioButton(Delegate* delegate,
                                                         int index)
    : views::RadioButton(u"", kGroupId), delegate_(delegate), index_(index) {}

views::View* CombinedSelectorRadioButton::GetSelectedViewForGroup(int group) {
  Views views;
  GetRadioButtonsInList(group, &views);

  const auto i = std::ranges::find_if(views, [](const views::View* view) {
    return static_cast<const CombinedSelectorRadioButton*>(view)->GetChecked();
  });
  return (i == views.cend()) ? nullptr : *i;
}

void CombinedSelectorRadioButton::SetChecked(bool checked) {
  if (checked == RadioButton::GetChecked()) {
    return;
  }
  if (checked) {
    Views other;
    GetRadioButtonsInList(GetGroup(), &other);
    for (views::View* peer : other) {
      if (peer != this && IsViewClass<CombinedSelectorRadioButton>(peer)) {
        static_cast<CombinedSelectorRadioButton*>(peer)->SetChecked(false);
      }
    }
    delegate_->OnRadioButtonChecked(index_);
    RequestFocus();
  }
  Checkbox::SetChecked(checked);
}

bool CombinedSelectorRadioButton::IsGroupFocusTraversable() const {
  return true;
}

void CombinedSelectorRadioButton::GetRadioButtonsInList(int group,
                                                        Views* views) {
  auto* row_view = parent();
  if (!row_view) {
    return;
  }
  auto* list_view = row_view->parent();
  if (!list_view) {
    return;
  }
  list_view->GetViewsInGroup(group, views);
}

bool CombinedSelectorRadioButton::SkipDefaultKeyEventProcessing(
    const ui::KeyEvent& event) {
  // The radio button would show the ink drop on return key press. Since the
  // radio buttons in the combined selector are tab focusable
  // (IsGroupFocusTraversable), this is not required. The return key should not
  // be handled by the radio button.
  return event.key_code() == ui::VKEY_RETURN
             ? false
             : RadioButton::SkipDefaultKeyEventProcessing(event);
}

BEGIN_METADATA(CombinedSelectorRadioButton)
END_METADATA

CombinedSelectorTextColumnView::CombinedSelectorTextColumnView(
    const std::vector<std::u16string_view> texts) {
  AddColumn(views::LayoutAlignment::kStart, views::LayoutAlignment::kCenter,
            1.0f, views::TableLayout::ColumnSize::kFixed, 0, 0);
  AddRows(texts.size(), views::TableLayout::kFixedSize);
  for (size_t i = 0; i < texts.size(); i++) {
    auto* label_view = AddChildView(std::make_unique<views::Label>(
        texts.at(i), views::style::CONTEXT_LABEL,
        i == 0 ? views::style::STYLE_BODY_3_MEDIUM
               : views::style::STYLE_BODY_4));
    label_view->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  }
}

BEGIN_METADATA(CombinedSelectorTextColumnView)
END_METADATA

CombinedSelectorRowView::CombinedSelectorRowView(
    const ui::ImageModel& icon,
    const std::vector<std::u16string_view> texts,
    RadioStatus radio_status,
    bool enabled,
    CombinedSelectorRadioButton::Delegate* radio_delegate,
    int index)
    : radio_status_(radio_status), enabled_(enabled) {
  SetEnabled(enabled);

  GetViewAccessibility().SetRole(radio_status != RadioStatus::kNone
                                     ? ax::mojom::Role::kRadioButton
                                     : ax::mojom::Role::kButton);
  GetViewAccessibility().SetName(base::JoinString(texts, u"\n"));
  const int horizontal_padding = radio_status == RadioStatus::kNone ? 0 : 16;
  SetBorder(views::CreateEmptyBorder(gfx::Insets::VH(8, horizontal_padding)));

  const int icon_padding = ChromeLayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_RELATED_LABEL_HORIZONTAL);
  AddColumn(views::LayoutAlignment::kCenter, views::LayoutAlignment::kCenter,
            views::TableLayout::kFixedSize,
            views::TableLayout::ColumnSize::kUsePreferred,
            /*fixed_width=*/0,
            /*min_width=*/0)
      .AddPaddingColumn(views::TableLayout::kFixedSize, icon_padding);
  AddColumn(views::LayoutAlignment::kStretch, views::LayoutAlignment::kStretch,
            /*horizontal_resize=*/1.0,
            views::TableLayout::ColumnSize::kUsePreferred,
            /*fixed_width=*/0,
            /*min_width=*/0);
  if (radio_status != RadioStatus::kNone) {
    AddPaddingColumn(views::TableLayout::kFixedSize, icon_padding)
        .AddColumn(views::LayoutAlignment::kCenter,
                   views::LayoutAlignment::kCenter,
                   views::TableLayout::kFixedSize,
                   views::TableLayout::ColumnSize::kUsePreferred,
                   /*fixed_width=*/0,
                   /*min_width=*/0);
  }
  AddRows(1, views::TableLayout::kFixedSize);

  AddChildView(std::make_unique<views::ImageView>(icon));
  AddChildView(std::make_unique<CombinedSelectorTextColumnView>(texts));
  MaybeAddRadioButton(radio_delegate, index);
}

void CombinedSelectorRowView::MaybeAddRadioButton(
    CombinedSelectorRadioButton::Delegate* delegate,
    int index) {
  if (radio_status_ == RadioStatus::kNone) {
    return;
  }
  auto radio_button =
      std::make_unique<CombinedSelectorRadioButton>(delegate, index);
  radio_button->SetChecked(radio_status_ == RadioStatus::kSelected);
  radio_button->SetEnabled(enabled_);
  radio_button->GetViewAccessibility().SetName(*this);
  radio_button_ = AddChildView(std::move(radio_button));
}

void CombinedSelectorRowView::RequestFocus() {
  if (radio_button_) {
    radio_button_->RequestFocus();
  }
}

bool CombinedSelectorRowView::OnMousePressed(const ui::MouseEvent& event) {
  if (radio_button_ && event.IsOnlyLeftMouseButton()) {
    const gfx::Point center = radio_button_->GetLocalBounds().CenterPoint();
    ui::MouseEvent synthetic_press_event(
        ui::EventType::kMousePressed, center, center, event.time_stamp(),
        event.flags(), event.changed_button_flags());
    radio_button_->OnMousePressed(synthetic_press_event);
    return true;
  }
  return views::TableLayoutView::OnMousePressed(event);
}

void CombinedSelectorRowView::OnMouseReleased(const ui::MouseEvent& event) {
  if (radio_button_ && event.IsOnlyLeftMouseButton()) {
    const gfx::Point center = radio_button_->GetLocalBounds().CenterPoint();
    ui::MouseEvent synthetic_release_event(
        ui::EventType::kMouseReleased, center, center, event.time_stamp(),
        event.flags(), event.changed_button_flags());
    radio_button_->OnMouseReleased(synthetic_release_event);
    RequestFocus();
    return;
  }
  views::TableLayoutView::OnMouseReleased(event);  // Default handling.
}

BEGIN_METADATA(CombinedSelectorRowView)
END_METADATA

CombinedSelectorListView::CombinedSelectorListView(
    CombinedSelectorSheetModel* model,
    CombinedSelectorRadioButton::Delegate* delegate) {
  SetLayoutManager(std::make_unique<views::FillLayout>());
  auto* scroll_view = AddChildView(std::make_unique<views::ScrollView>());

  auto wrapper = std::make_unique<views::View>();
  wrapper->SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical, gfx::Insets(),
      /*between_child_spacing=*/kRowGap));
  size_t num_mechanisms = model->dialog_model()->mechanisms.size();

  if (num_mechanisms > 1) {
    wrapper->AddChildView(std::make_unique<views::Separator>());
  }

  for (size_t i = 0; i < num_mechanisms; i++) {
    if (i > 0) {
      wrapper->AddChildView(std::make_unique<views::Separator>());
    }
    const auto& mechanism = model->dialog_model()->mechanisms[i];
    auto image_model =
        ui::ImageModel::FromVectorIcon(*mechanism.icon, ui::kColorIcon, 20);
    auto texts = mechanism.display_name.empty() ||
                         (mechanism.display_name == mechanism.name)
                     ? std::vector<std::u16string_view>{mechanism.name,
                                                        mechanism.description}
                     : std::vector<std::u16string_view>{mechanism.display_name,
                                                        mechanism.name,
                                                        mechanism.description};
    auto* row = wrapper->AddChildView(std::make_unique<CombinedSelectorRowView>(
        image_model, std::move(texts), model->GetSelectionStatus(i),
        !model->dialog_model()->ui_disabled_, delegate, i));
    if (model->GetSelectionStatus(i) ==
        CombinedSelectorSheetModel::SelectionStatus::kSelected) {
      selected_view_ = row;
    }
  }

  if (num_mechanisms > 1) {
    wrapper->AddChildView(std::make_unique<views::Separator>());
  }

  scroll_view->SetContents(std::move(wrapper));
  scroll_view->ClipHeightTo(kMaxRowHeight, 3 * kMaxRowHeight + 2 * kRowGap);
}

CombinedSelectorListView::~CombinedSelectorListView() = default;

void CombinedSelectorListView::RequestFocus() {
  if (selected_view_) {
    selected_view_->RequestFocus();
  }
}

BEGIN_METADATA(CombinedSelectorListView)
END_METADATA