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

ash / auth / views / auth_textfield.cc [blame]

// 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 "ash/auth/views/auth_textfield.h"

#include <string>

#include "ash/auth/views/auth_textfield_timer.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/system_textfield.h"
#include "ash/style/system_textfield_controller.h"
#include "ash/style/typography.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/observer_list_types.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/font_list.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/view.h"

namespace ash {

namespace {

// Spacing between glyphs, used when password is in hidden state.
constexpr int kPasswordGlyphSpacing = 6;

// Size (width/height) of the different icons belonging to the password row
// (the display password icon and the caps lock icon).
constexpr int kIconSizeDp = 20;

// The max width of the AuthTextfield.
constexpr int kAuthTextfieldMaxWidthDp = 216;

// Font type for text.
constexpr TypographyToken kTextFont = TypographyToken::kCrosBody2;

// Font type for hidden text.
constexpr TypographyToken kHiddenTextFont = TypographyToken::kCrosDisplay5;
}

AuthTextfield::AuthTextfield(AuthType auth_type)
    : SystemTextfield(Type::kMedium),
      SystemTextfieldController(this),
      auth_type_(auth_type) {

  SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
  SetFocusBehavior(FocusBehavior::ALWAYS);
  set_placeholder_font_list(
      ash::TypographyProvider::Get()->ResolveTypographyToken(kTextFont));
  SetFontList(
      ash::TypographyProvider::Get()->ResolveTypographyToken(kHiddenTextFont));
  SetObscuredGlyphSpacing(kPasswordGlyphSpacing);
  // Remove focus ring to remain consistent with other implementations of
  // login input fields.
  views::FocusRing::Remove(this);

  // Don't show background.
  SetShowBackground(false);
  SetBackgroundEnabled(false);
  SetBackground(nullptr);

  // Remove the border.
  SetBorder(nullptr);

  // Set the text colors.
  SetTextColorId(cros_tokens::kCrosSysOnSurface);
  SetPlaceholderTextColorId(cros_tokens::kCrosSysDisabled);

  // Set Accessible name
  if (auth_type_ == AuthType::kPassword) {
    GetViewAccessibility().SetName(l10n_util::GetStringUTF16(
        IDS_ASH_AUTH_TEXTFIELD_PASSWORD_ACCESSIBLE_NAME));
    SetPlaceholderText(l10n_util::GetStringUTF16(
        IDS_ASH_IN_SESSION_AUTH_PASSWORD_PLACEHOLDER));
  } else {
    CHECK_EQ(auth_type_, AuthType::kPin);
    GetViewAccessibility().SetName(
        l10n_util::GetStringUTF16(IDS_ASH_AUTH_TEXTFIELD_PIN_ACCESSIBLE_NAME));
    SetPlaceholderText(
        l10n_util::GetStringUTF16(IDS_ASH_IN_SESSION_AUTH_PIN_PLACEHOLDER));
  }
}

AuthTextfield::~AuthTextfield() = default;

void AuthTextfield::AboutToRequestFocusFromTabTraversal(bool reverse) {
  if (!GetText().empty()) {
    SelectAll(/*reversed=*/false);
  }
}

void AuthTextfield::OnBlur() {
  SystemTextfield::OnBlur();
  for (auto& observer : observers_) {
    observer.OnTextfieldBlur();
  }
}

void AuthTextfield::OnFocus() {
  SystemTextfield::OnFocus();
  for (auto& observer : observers_) {
    observer.OnTextfieldFocus();
  }
}

ui::TextInputMode AuthTextfield::GetTextInputMode() const {
  if (auth_type_ == AuthType::kPin) {
    return ui::TextInputMode::TEXT_INPUT_MODE_NUMERIC;
  }
  return ui::TextInputMode::TEXT_INPUT_MODE_DEFAULT;
}

bool AuthTextfield::ShouldDoLearning() {
  return false;
}

bool AuthTextfield::HandleKeyEvent(views::Textfield* sender,
                                    const ui::KeyEvent& key_event) {
  CHECK_EQ(sender, this);

  if (GetReadOnly()) {
    return false;
  }

  if (key_event.type() != ui::EventType::kKeyPressed) {
    return false;
  }

  const ui::KeyboardCode key_code = key_event.key_code();

  if (key_code == ui::VKEY_RETURN) {
    if (!GetText().empty()) {
      for (auto& observer : observers_) {
        observer.OnSubmit();
      }
    }
    return true;
  }

  if (key_code == ui::VKEY_ESCAPE) {
    for (auto& observer : observers_) {
      observer.OnEscape();
    }
    return true;
  }

  if (auth_type_ == AuthType::kPassword) {
    return SystemTextfieldController::HandleKeyEvent(sender, key_event);
  }

  CHECK_EQ(auth_type_, AuthType::kPin);

  // Default handling for events with Alt modifier like spoken feedback.
  if (key_event.IsAltDown()) {
    return false;
  }

  // Default handling for events with Control modifier like sign out.
  if (key_event.IsControlDown()) {
    return false;
  }

  // All key pressed events not handled below are ignored.
  if (key_code == ui::VKEY_TAB || key_code == ui::VKEY_BACKTAB) {
    // Allow using tab for keyboard navigation.
    return false;
  } else if (key_code == ui::VKEY_PROCESSKEY) {
    // Default handling for keyboard events that are not generated by physical
    // key press. This can happen, for example, when virtual keyboard button
    // is pressed.
    return false;
  } else if (key_code >= ui::VKEY_0 && key_code <= ui::VKEY_9) {
    InsertDigit(key_code - ui::VKEY_0);
  } else if (key_code >= ui::VKEY_NUMPAD0 && key_code <= ui::VKEY_NUMPAD9) {
    InsertDigit(key_code - ui::VKEY_NUMPAD0);
  } else if (key_code == ui::VKEY_BACK) {
    return false;
  } else if (key_code == ui::VKEY_DELETE) {
    return false;
  } else if (key_code == ui::VKEY_LEFT) {
    return false;
  } else if (key_code == ui::VKEY_RIGHT) {
    return false;
  }

  return true;
}

void AuthTextfield::ContentsChanged(Textfield* sender,
                                     const std::u16string& new_contents) {
  for (auto& observer : observers_) {
    observer.OnContentsChanged(new_contents);
  }
}

gfx::Size AuthTextfield::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  return gfx::Size(kAuthTextfieldMaxWidthDp, kIconSizeDp);
}

void AuthTextfield::Reset() {
  if (!GetText().empty()) {
    SetText(std::u16string());
    for (auto& observer : observers_) {
      observer.OnContentsChanged(GetText());
    }
  }
  HideText();
  ClearEditHistory();
}

void AuthTextfield::InsertDigit(int digit) {
  CHECK(auth_type_ == AuthType::kPin);
  CHECK(0 <= digit && digit <= 9);
  if (GetReadOnly()) {
    return;
  }

  if (!HasFocus()) {
    // RequestFocus on textfield to activate cursor.
    RequestFocus();
  }
  InsertOrReplaceText(base::NumberToString16(digit));
}

void AuthTextfield::Backspace() {
  // Instead of just adjusting textfield_ text directly, fire a backspace key
  // event as this handles the various edge cases (ie, selected text).

  // views::Textfield::OnKeyPressed is private, so we call it via views::View.
  if (GetReadOnly()) {
    return;
  }

  if (!HasFocus()) {
    // RequestFocus on textfield to activate cursor.
    RequestFocus();
  }

  auto* view = static_cast<views::View*>(this);
  view->OnKeyPressed(ui::KeyEvent(ui::EventType::kKeyPressed, ui::VKEY_BACK,
                                  ui::DomCode::BACKSPACE, ui::EF_NONE));
  view->OnKeyPressed(ui::KeyEvent(ui::EventType::kKeyReleased, ui::VKEY_BACK,
                                  ui::DomCode::BACKSPACE, ui::EF_NONE));
}

void AuthTextfield::SetTextVisible(bool visible) {
  if (visible) {
    ShowText();
  } else {
    HideText();
  }
}

void AuthTextfield::ShowText() {
  if (IsTextVisible()) {
    return;
  }
  SetFontList(
      ash::TypographyProvider::Get()->ResolveTypographyToken(kTextFont));
  switch (auth_type_) {
    case AuthType::kPassword:
      SetTextInputType(ui::TEXT_INPUT_TYPE_NULL);
      break;
    case AuthType::kPin:
      SetTextInputType(ui::TEXT_INPUT_TYPE_NUMBER);
      break;
  }
  for (auto& observer : observers_) {
    observer.OnTextVisibleChanged(true);
  }
}

void AuthTextfield::HideText() {
  if (!IsTextVisible()) {
    return;
  }
  SetFontList(
      ash::TypographyProvider::Get()->ResolveTypographyToken(kHiddenTextFont));
  SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
  for (auto& observer : observers_) {
    observer.OnTextVisibleChanged(false);
  }
}

bool AuthTextfield::IsTextVisible() const {
  return GetTextInputType() != ui::TEXT_INPUT_TYPE_PASSWORD;
}

void AuthTextfield::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void AuthTextfield::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void AuthTextfield::ApplyTimerLogic() {
  CHECK_EQ(timer_logic_.get(), nullptr);
  timer_logic_ = std::make_unique<AuthTextfieldTimer>(this);
}

void AuthTextfield::ResetTimerLogic() {
  CHECK(timer_logic_.get());
  timer_logic_.reset();
}

BEGIN_METADATA(AuthTextfield)
END_METADATA

}  // namespace ash