File: payment_request_row_view.cc

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 (195 lines) | stat: -rw-r--r-- 6,292 bytes parent folder | download | duplicates (4)
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
// Copyright 2017 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/payments/payment_request_row_view.h"

#include <string>

#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/views/payments/payment_request_views_util.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.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/controls/label.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"

namespace {

// TODO(pbos): Reconsider how to construct accessible names from these nodes.
// Right now this concatenates (with newlines) every Label inside the row to
// ensure that no data is inaccessible.
std::u16string GetAccessibleNameFromTree(views::View* view) {
  if (views::IsViewClass<views::Label>(view)) {
    return static_cast<views::Label*>(view)
        ->GetViewAccessibility()
        .GetCachedName();
  }

  std::u16string accessible_name;
  for (views::View* child : view->children()) {
    // Skip buttons they will be announced independently. This is used for
    // "more" items.
    if (views::IsViewClass<views::Button>(child)) {
      continue;
    }
    std::u16string child_accessible_name = GetAccessibleNameFromTree(child);
    if (child_accessible_name.empty()) {
      continue;
    }
    if (!accessible_name.empty()) {
      accessible_name += '\n';
    }
    accessible_name += child_accessible_name;
  }
  return accessible_name;
}

}  // namespace

namespace payments {

PaymentRequestRowView::PaymentRequestRowView()
    : PaymentRequestRowView(PressedCallback(),
                            /*clickable=*/true,
                            gfx::Insets()) {}

PaymentRequestRowView::PaymentRequestRowView(PressedCallback callback,
                                             bool clickable,
                                             const gfx::Insets& insets)
    : views::Button(std::move(callback)),
      clickable_(clickable),
      row_insets_(insets) {
  UpdateButtonState();
  SetBottomSeparatorVisible(true);
  SetFocusBehavior(views::View::FocusBehavior::ALWAYS);
}

PaymentRequestRowView::~PaymentRequestRowView() = default;

bool PaymentRequestRowView::GetClickable() const {
  return clickable_;
}
void PaymentRequestRowView::SetClickable(bool clickable) {
  if (clickable == clickable_) {
    return;
  }
  clickable_ = clickable;
  UpdateButtonState();
  OnPropertyChanged(&clickable_, views::PropertyEffects::kPropertyEffectsPaint);
}

base::WeakPtr<PaymentRequestRowView> PaymentRequestRowView::AsWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

gfx::Insets PaymentRequestRowView::GetRowInsets() const {
  return row_insets_;
}

void PaymentRequestRowView::SetRowInsets(const gfx::Insets& row_insets) {
  if (row_insets == row_insets_) {
    return;
  }
  row_insets_ = row_insets;
  UpdateBottomSeparatorVisualState();
  OnPropertyChanged(&row_insets_,
                    views::PropertyEffects::kPropertyEffectsPaint);
}

void PaymentRequestRowView::SetBottomSeparatorVisible(bool visible) {
  bottom_separator_visible_ = visible;
  UpdateBottomSeparatorVisualState();
}

void PaymentRequestRowView::UpdateBottomSeparatorVisualState() {
  // Create an empty border even when not present in a Widget hierarchy as the
  // border is needed to correctly compute the bounds of the ScrollView in the
  // PaymentRequestSheetController which is done before this is added to its
  // Widget.
  // TODO(crbug.com/40768647): Update PaymentRequestSheetController to recompute
  // the bounds of its ScrollView in response to changes in preferred size.
  SetBorder(
      bottom_separator_visible_ && GetWidget()
          ? payments::CreatePaymentRequestRowBorder(
                GetColorProvider()->GetColor(ui::kColorSeparator), row_insets_)
          : views::CreateEmptyBorder(row_insets_));
}

void PaymentRequestRowView::SetHighlighted(bool highlighted) {
  if (highlighted) {
    SetBackground(views::CreateSolidBackground(
        kColorPaymentsRequestRowBackgroundHighlighted));
    SetBottomSeparatorVisible(false);
    if (previous_row_) {
      previous_row_->SetBottomSeparatorVisible(false);
    }
  } else {
    SetBackground(nullptr);
    SetBottomSeparatorVisible(true);
    if (previous_row_) {
      previous_row_->SetBottomSeparatorVisible(true);
    }
  }
}

void PaymentRequestRowView::UpdateButtonState() {
  // When not clickable, use Button's STATE_DISABLED but don't set our
  // View state to disabled. The former ensures we aren't clickable, the
  // latter also disables us and our children for event handling.
  views::Button::SetState(clickable_ ? views::Button::STATE_NORMAL
                                     : views::Button::STATE_DISABLED);
}

void PaymentRequestRowView::StateChanged(ButtonState old_state) {
  Button::StateChanged(old_state);
  if (!GetClickable()) {
    return;
  }

  SetHighlighted(GetState() == views::Button::STATE_HOVERED ||
                 GetState() == views::Button::STATE_PRESSED);
}

void PaymentRequestRowView::OnThemeChanged() {
  Button::OnThemeChanged();
  UpdateBottomSeparatorVisualState();
}

void PaymentRequestRowView::ViewHierarchyChanged(
    const views::ViewHierarchyChangedDetails& details) {
  views::Button::ViewHierarchyChanged(details);
  GetViewAccessibility().SetName(GetAccessibleNameFromTree(this));
}

void PaymentRequestRowView::OnFocus() {
  if (GetClickable()) {
    SetHighlighted(true);
  }
  View::OnFocus();
  if (views::FocusRing* focus_ring = views::FocusRing::Get(this)) {
    focus_ring->SetProperty(views::kViewIgnoredByLayoutKey, true);
  }
}

void PaymentRequestRowView::OnBlur() {
  if (GetClickable()) {
    SetHighlighted(false);
  }
}

BEGIN_METADATA(PaymentRequestRowView)
ADD_PROPERTY_METADATA(bool, Clickable)
ADD_PROPERTY_METADATA(gfx::Insets, RowInsets)
END_METADATA

}  // namespace payments