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

ash / style / system_textfield.h [blame]

// Copyright 2022 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_STYLE_SYSTEM_TEXTFIELD_H_
#define ASH_STYLE_SYSTEM_TEXTFIELD_H_

#include <optional>

#include "ash/ash_export.h"
#include "base/memory/raw_ptr.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/color/color_id.h"
#include "ui/views/controls/textfield/textfield.h"

namespace ash {

// SystemTextfield is an extension of `Views::Textfield` used for system UIs. It
// has specific small, medium, and large types and applies dynamic colors.
class ASH_EXPORT SystemTextfield : public views::Textfield {
  METADATA_HEADER(SystemTextfield, views::Textfield)

 public:
  enum class Type {
    kSmall,
    kMedium,
    kLarge,
  };

  explicit SystemTextfield(Type type);
  SystemTextfield(const SystemTextfield&) = delete;
  SystemTextfield& operator=(const SystemTextfield&) = delete;
  ~SystemTextfield() override;

  // Set custom colors of text, selected text, selection background, and
  // textfield background color.
  void SetTextColorId(ui::ColorId color_id);
  void SetSelectedTextColorId(ui::ColorId color_id);
  void SetSelectionBackgroundColorId(ui::ColorId color_id);
  void SetBackgroundColorId(ui::ColorId color_id);
  void SetPlaceholderTextColorId(ui::ColorId color_id);
  void SetActiveStateChangedCallback(base::RepeatingClosure callback);
  void SetCornerRadius(int corner_radius);

  // Activates or deactivates the textfield. The textfield can only be edited if
  // it is active.
  void SetActive(bool active);
  bool IsActive() const;
  // Sets if the focus ring should be shown despite the active state.
  void SetShowFocusRing(bool show);
  // Sets if the themed rounded rect background should be shown.
  // TODO(zxdan): Move this to be determined by textfield controller.
  void SetShowBackground(bool show);
  // Restores to previous text when the changes are discarded.
  void RestoreText();
  // Creates themed or transparent background according to the textfield states.
  void UpdateBackground();

  // views::Textfield:
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override;
  void SetBorder(std::unique_ptr<views::Border> b) override;
  void OnMouseEntered(const ui::MouseEvent& event) override;
  void OnMouseExited(const ui::MouseEvent& event) override;
  void OnThemeChanged() override;
  void OnFocus() override;
  void OnBlur() override;

 private:
  // An event handler to handle the events outside the textfield.
  class EventHandler;

  // Called when the enabled state is changed.
  void OnEnabledStateChanged();
  // Update custom color ID.
  void UpdateColorId(std::optional<ui::ColorId>& src,
                     ui::ColorId dst,
                     bool is_background_color);
  // Updates text and selection text colors.
  void UpdateTextColor();

  Type type_;
  std::unique_ptr<EventHandler> event_handler_;
  int corner_radius_;

  // Text content to restore when changes are discarded.
  std::u16string restored_text_content_;
  // Indicates if the textfield should show focus ring.
  bool show_focus_ring_ = false;
  // Indicates if the textfield should show background.
  bool show_background_ = false;

  // custom color IDs for text, selected text, selection background, and
  // textfield background.
  std::optional<ui::ColorId> text_color_id_;
  std::optional<ui::ColorId> selected_text_color_id_;
  std::optional<ui::ColorId> selection_background_color_id_;
  std::optional<ui::ColorId> background_color_id_;
  std::optional<ui::ColorId> placeholder_text_color_id_;

  // Active state changed callback.
  base::RepeatingClosure active_state_changed_callback_;

  // Enabled state changed callback.
  base::CallbackListSubscription enabled_changed_subscription_;
};

}  // namespace ash

#endif  // ASH_STYLE_SYSTEM_TEXTFIELD_H_