File: pin_textfield.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; 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 (259 lines) | stat: -rw-r--r-- 7,888 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 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/pin_textfield.h"

#include <algorithm>
#include <memory>
#include <string>
#include <utility>

#include "base/check_op.h"
#include "base/i18n/rtl.h"
#include "base/strings/strcat.h"
#include "cc/paint/paint_flags.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/base/ime/text_input_type.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/render_text.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/view.h"

namespace {

// Size specs of a pin cell.
constexpr int kCellWidth = 30;
constexpr int kCellHeight = 36;
constexpr int kCellSpacing = 8;
constexpr float kCellRadius = 8.0;

// Creates obscured render text for displaying a glyph in a specific pin cell.
std::unique_ptr<gfx::RenderText> CreatePinDigitRenderText(
    const gfx::FontList& font_list) {
  std::unique_ptr<gfx::RenderText> render_text =
      gfx::RenderText::CreateRenderText();
  render_text->SetCursorEnabled(false);
  render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER);
  render_text->SetObscured(true);
  render_text->SetFontList(font_list);
  return render_text;
}

}  // namespace

PinTextfield::PinTextfield(int pin_digits_amount)
    : pin_digits_count_(pin_digits_amount) {
  CHECK_GE(pin_digits_count_, 0);

  SetCursorEnabled(false);
  SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
  // Custom border handling is implemented in `OnPaint`.
  SetBorder(views::CreateEmptyBorder(0));

  const gfx::FontList& font_list = views::TypographyProvider::Get().GetFont(
      views::style::CONTEXT_TEXTFIELD,
      views::style::TextStyle::STYLE_BODY_1_BOLD);
  for (int i = 0; i < pin_digits_count_; i++) {
    render_texts_.push_back(CreatePinDigitRenderText(font_list));
  }

  UpdatePinAccessibleValue();
}

PinTextfield::~PinTextfield() = default;

bool PinTextfield::AppendDigit(std::u16string digit) {
  if (disabled_) {
    return false;
  }

  if (digits_typed_count_ >= pin_digits_count_) {
    return false;
  }

  render_texts_[digits_typed_count_++]->SetText(std::move(digit));
  SchedulePaint();
  UpdateAccessibilityAfterPinChange();
  return true;
}

bool PinTextfield::RemoveDigit() {
  if (disabled_) {
    return false;
  }

  if (digits_typed_count_ <= 0) {
    return false;
  }

  render_texts_[--digits_typed_count_]->SetText(u"");
  SchedulePaint();
  UpdateAccessibilityAfterPinChange();
  return true;
}

std::u16string PinTextfield::GetPin() {
  std::u16string pin;
  for (int i = 0; i < digits_typed_count_; i++) {
    base::StrAppend(&pin, {render_texts_[i]->text()});
  }
  return pin;
}

void PinTextfield::SetPin(const std::u16string& pin) {
  int pin_length = std::min(static_cast<int>(pin.length()), pin_digits_count_);
  for (int i = 0; i < pin_length; i++) {
    render_texts_[i]->SetText(std::u16string(1, pin[i]));
  }
  digits_typed_count_ = pin_length;
  UpdatePinAccessibleValue();
  SchedulePaint();
}

void PinTextfield::SetObscured(bool obscured) {
  if (obscured_ == obscured) {
    return;
  }

  obscured_ = obscured;
  GetViewAccessibility().SetIsProtected(obscured);
  for (int i = 0; i < pin_digits_count_; i++) {
    render_texts_[i]->SetObscured(obscured);
  }
  UpdateAccessibilityAfterPinChange();
  SchedulePaint();
}

void PinTextfield::SetDisabled(bool disabled) {
  if (disabled_ == disabled) {
    return;
  }

  disabled_ = disabled;
  UpdatePinAccessibleValue();
  SetTextInputType(disabled ? ui::TEXT_INPUT_TYPE_NONE
                            : ui::TEXT_INPUT_TYPE_PASSWORD);
  UpdateTextColor();
  SchedulePaint();
}

void PinTextfield::OnPaint(gfx::Canvas* canvas) {
  View::OnPaintBackground(canvas);

  cc::PaintFlags paint_flags;
  paint_flags.setStyle(cc::PaintFlags::kStroke_Style);
  paint_flags.setAntiAlias(true);

  SkColor background_color = GetColorProvider()->GetColor(
      disabled_ ? ui::kColorTextfieldBackgroundDisabled
                : ui::kColorTextfieldBackground);
  for (int i = 0; i < pin_digits_count_; i++) {
    paint_flags.setColor(GetColorProvider()->GetColor(
        disabled_ ? ui::kColorTextfieldOutlineDisabled
                  : (HasCellFocus(i) ? ui::kColorFocusableBorderFocused
                                     : ui::kColorTextfieldOutline)));
    float stroke_width = HasCellFocus(i) ? 2.f : 1.f;
    paint_flags.setStrokeWidth(stroke_width);

    // Drawing is adjusted in RTL so that the first cell is drawn rightmost.
    int index_rtl_adjusted =
        base::i18n::IsRTL() ? pin_digits_count_ - i - 1 : i;
    gfx::Rect cell_rect(index_rtl_adjusted * (kCellWidth + kCellSpacing), 0,
                        kCellWidth, kCellHeight);

    // Make sure background is not drawn outside of the rounded cell.
    SkPath path;
    path.addRoundRect(gfx::RectToSkRect(cell_rect), kCellRadius, kCellRadius);
    canvas->Save();
    canvas->ClipPath(path, /*do_anti_alias=*/true);

    // Draw cell background.
    canvas->FillRect(cell_rect, background_color);
    // Draw cell border.
    gfx::RectF cell_rect_f(cell_rect);
    cell_rect_f.Inset(stroke_width / 2.f);
    canvas->DrawRoundRect(cell_rect_f, kCellRadius, paint_flags);
    // Draw cell text.
    render_texts_[i]->SetDisplayRect(cell_rect);
    render_texts_[i]->Draw(canvas);

    canvas->Restore();
  }
}

gfx::Size PinTextfield::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  return gfx::Size(
      pin_digits_count_ * kCellWidth + (pin_digits_count_ - 1) * kCellSpacing,
      kCellHeight);
}

void PinTextfield::OnThemeChanged() {
  views::View::OnThemeChanged();
  UpdateTextColor();
}

void PinTextfield::UpdateAccessibleTextSelection() {
  // Pin textfield does not support selecting characters, set it to an empty
  // selection at the end of the currently typed pin.
  GetViewAccessibility().SetTextSelStart(digits_typed_count_);
  GetViewAccessibility().SetTextSelEnd(digits_typed_count_);
}

bool PinTextfield::HasCellFocus(int cell) const {
  int cell_with_focus = digits_typed_count_ == pin_digits_count_
                            ? pin_digits_count_ - 1
                            : digits_typed_count_;
  return HasFocus() && cell == cell_with_focus;
}

void PinTextfield::UpdateAccessibilityAfterPinChange() {
  UpdateAccessibleTextSelection();
  UpdatePinAccessibleValue();

  // Don't announce the selected text (last typed digit) in `obscured_` mode.
  if (!obscured_) {
    NotifyAccessibilityEventDeprecated(ax::mojom::Event::kTextSelectionChanged,
                                       /*send_native_event=*/true);
  }
}

void PinTextfield::UpdatePinAccessibleValue() {
  std::u16string pin = GetPin();
  GetViewAccessibility().SetValue(
      (obscured_ || disabled_)
          ? std::u16string(pin.size(),
                           gfx::RenderText::kPasswordReplacementChar)
          : pin);
}

void PinTextfield::UpdateTextColor() {
  if (!GetWidget()) {
    return;
  }

  int text_style =
      disabled_ ? views::style::STYLE_DISABLED : views::style::STYLE_PRIMARY;
  SkColor text_color =
      GetColorProvider()->GetColor(views::TypographyProvider::Get().GetColorId(
          views::style::CONTEXT_TEXTFIELD, text_style));
  for (int i = 0; i < pin_digits_count_; i++) {
    render_texts_[i]->SetColor(text_color);
  }
}

BEGIN_METADATA(PinTextfield)
END_METADATA