File: address_editor_view.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 4,858 bytes parent folder | download | duplicates (5)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_VIEWS_AUTOFILL_ADDRESS_EDITOR_VIEW_H_
#define CHROME_BROWSER_UI_VIEWS_AUTOFILL_ADDRESS_EDITOR_VIEW_H_

#include <memory>
#include <string_view>
#include <unordered_map>

#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/autofill/address_editor_controller.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/view.h"

namespace views {
class Combobox;
class Textfield;
class View;
class Label;
}  // namespace views

namespace autofill {

class AddressEditorView : public views::View {
  METADATA_HEADER(AddressEditorView, views::View)

 public:
  explicit AddressEditorView(
      std::unique_ptr<AddressEditorController> controller);
  AddressEditorView(const AddressEditorView&) = delete;
  AddressEditorView& operator=(const AddressEditorView&) = delete;
  ~AddressEditorView() override;

  // The view to be focused first when the editor shows up. It points to
  // the first input (normally the country combobox) and important from
  // the usability perspective as the most reasonable place to start editing
  // the address from.
  // Returns `nullprt` only when `AddressEditorController::editor_fields()`
  // returns no fields.
  views::View* initial_focus_view() { return initial_focus_view_; }

  // views::View
  void PreferredSizeChanged() override;

  // Stores the current state of the address profile in the controller, and
  // returns it.
  // If the editor is validatable (`AddressEditorController::is_validatable()`),
  // make sure the address is valid (`AddressEditorController::is_valid()`)
  // before calling this method.
  const autofill::AutofillProfile& GetAddressProfile();

  // Checks all fields and updates their visual status accordingly. Returns
  // `false` if at least one field is invalid and `true` otherwise or if
  // the form is not validatable.
  bool ValidateAllFields();

  void SelectCountryForTesting(const std::u16string& code);
  void SetTextInputFieldValueForTesting(autofill::FieldType type,
                                        const std::u16string& value);
  std::u16string_view GetValidationErrorForTesting() const;

 private:
  // Creates the whole editor view to go within the editor dialog. It
  // encompasses all the input fields created by CreateInputField().
  void CreateEditorView();

  // Adds some views to |layout|, to represent an input field and its labels.
  // |field| is the field definition, which contains the label and the hint
  // about the length of the input field. Returns the input view for this field.
  views::View* CreateInputField(const EditorField& field);

  // Will create a country combobox with |label|. Will also keep track of this
  // field to populate the edited model on save.
  std::unique_ptr<views::Combobox> CreateCountryCombobox(
      const std::u16string& label);

  // Update the editor view by removing all it's child views and recreating
  // the input fields using |editor_fields_|. Note that
  // CreateEditorView MUST have been called at least once before calling
  // UpdateEditorView.
  void UpdateEditorView();

  void SaveFieldsToProfile();

  // Combobox callback. Called when data changes need to force a view update.
  // The view is updated synchronously.
  void OnSelectedCountryChanged(views::Combobox* combobox);

  // Checks the field and updates its visual status accordingly.
  void ValidateField(views::Textfield* textfield);

  std::unique_ptr<AddressEditorController> controller_;

  // Map from TextField to the object that describes it.
  std::unordered_map<views::Textfield*, const EditorField> text_fields_;
  const std::string locale_;
  raw_ptr<views::Label> validation_error_ = nullptr;

  // The first input field, depends on the `controller_->editor_fields()`, but
  // normally is the country selection combobox.
  raw_ptr<views::View> initial_focus_view_ = nullptr;

  // The property is set to `true` after the first call of
  // `ValidateAllFields()`. It affects the validation logic upon on a single
  // field change: if it is `false`,only the edited erroneous fields are
  // highlighted, otherwise, the whole form validity is reconsidred. This
  // ensures a user-friendly experience by initially displaying the form as
  // error-free (for new profiles).
  bool all_address_fields_have_been_validated_ = false;

  // 1 subscription to text changes per field.
  std::vector<base::CallbackListSubscription> field_change_callbacks_;

  base::WeakPtrFactory<AddressEditorView> weak_ptr_factory_{this};
};

}  // namespace autofill

#endif  // CHROME_BROWSER_UI_VIEWS_AUTOFILL_ADDRESS_EDITOR_VIEW_H_