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

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

#ifndef ASH_AUTH_VIEWS_AUTH_TEXTFIELD_H_
#define ASH_AUTH_VIEWS_AUTH_TEXTFIELD_H_

#include <memory>
#include <string>

#include "ash/ash_export.h"
#include "ash/style/system_textfield.h"
#include "ash/style/system_textfield_controller.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list_types.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/gfx/geometry/size.h"

namespace ash {

class AuthTextfieldTimer;

// A textfield that selects all text on focus and allows to switch between
// show/hide password modes.
class ASH_EXPORT AuthTextfield : public SystemTextfield,
                                  public SystemTextfieldController {
  METADATA_HEADER(AuthTextfield, SystemTextfield)
 public:
  enum class AuthType {
    kPassword,
    kPin,
  };

  class Observer : public base::CheckedObserver {
   public:
    virtual void OnTextfieldBlur() {}
    virtual void OnTextfieldFocus() {}
    virtual void OnContentsChanged(const std::u16string& new_contents) {}
    virtual void OnTextVisibleChanged(bool visible) {}
    virtual void OnSubmit() {}
    virtual void OnEscape() {}
  };

  AuthTextfield(AuthType auth_type);
  AuthTextfield(const AuthTextfield&) = delete;
  AuthTextfield& operator=(const AuthTextfield&) = delete;
  ~AuthTextfield() override;

  // views::Textfield:
  void AboutToRequestFocusFromTabTraversal(bool reverse) override;
  void OnBlur() override;
  void OnFocus() override;
  ui::TextInputMode GetTextInputMode() const override;
  bool ShouldDoLearning() override;

  // SystemTextfieldController:
  bool HandleKeyEvent(views::Textfield* sender,
                      const ui::KeyEvent& key_event) override;
  void ContentsChanged(Textfield* sender,
                       const std::u16string& new_contents) override;

  // This is useful when the display password button is not shown. In such a
  // case, the login text field needs to define its size.
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override;

  void Reset();

  void InsertDigit(int digit);
  void Backspace();

  void SetTextVisible(bool visible);
  bool IsTextVisible() const;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  void ApplyTimerLogic();
  void ResetTimerLogic();

 private:
  void ShowText();
  void HideText();

  const AuthType auth_type_;
  base::ObserverList<Observer> observers_;

  std::unique_ptr<AuthTextfieldTimer> timer_logic_;
};

}  // namespace ash

#endif  // ASH_AUTH_VIEWS_AUTH_TEXTFIELD_H_