File: subtle_notification_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (298 lines) | stat: -rw-r--r-- 11,402 bytes parent folder | download | duplicates (3)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/fullscreen_control/subtle_notification_view.h"

#include <memory>

#include "base/feature_list.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/color/color_id.h"
#include "components/fullscreen_control/fullscreen_features.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/style/typography.h"
#include "ui/views/widget/widget.h"
namespace {

// Space between the site info label.
const int kMiddlePaddingPx = 30;

const int kOuterPaddingHorizPx = 40;
const int kOuterPaddingVertPx = 8;

// Spacing around the key name.
const int kKeyNameMarginHorizPx = 7;
const int kKeyNameBorderPx = 1;
const int kKeyNameCornerRadius = 2;
const int kKeyNamePaddingPx = 5;

// Spacing between the key name and image, if any.
const int kKeyNameImageSpacingPx = 3;

// The context used to obtain typography for the instruction text. It's not
// really a dialog, but a dialog title is a good fit.
constexpr int kInstructionTextContext = views::style::CONTEXT_DIALOG_TITLE;

// Delimiter indicating there should be a segment displayed as a keyboard key.
constexpr char16_t kKeyNameDelimiter[] = u"|";

// Returns the background color to use for the notification.
ui::ColorId GetSubtleNotificationBackgroundColor() {
  return base::FeatureList::IsEnabled(features::kFullscreenBubbleShowOpaque)
             ? color::kFullscreenNotificationOpaqueBackgroundColor
             : color::kFullscreenNotificationTransparentBackgroundColor;
}

}  // namespace

// Class containing the instruction text. Contains fancy styling on the keyboard
// key (not just a simple label).
class SubtleNotificationView::InstructionView : public views::View {
  METADATA_HEADER(InstructionView, views::View)

 public:
  // Creates an InstructionView with specific text. |text| may contain one or
  // more segments delimited by a pair of pipes ('|'); each of these segments
  // will be displayed as a keyboard key. e.g., "Press |Alt|+|Q| to exit" will
  // have "Alt" and "Q" rendered as keys.
  explicit InstructionView(const std::u16string& text);
  InstructionView(const InstructionView&) = delete;
  InstructionView& operator=(const InstructionView&) = delete;

  std::u16string GetText() const;
  void SetText(const std::u16string& text);
  void SetTextAndImages(const std::u16string& text,
                        std::vector<std::unique_ptr<views::View>> key_images);

  base::CallbackListSubscription AddTextChangedCallback(
      views::PropertyChangedCallback callback);

 private:
  // Adds a label to the end of the notification text. If |format_as_key|,
  // surrounds the label in a rounded-rect border to indicate that it is a
  // keyboard key.
  void AddTextSegment(const std::u16string& text,
                      bool format_as_key,
                      std::unique_ptr<views::View> key_image);

  std::u16string text_;
};

SubtleNotificationView::InstructionView::InstructionView(
    const std::u16string& text) {
  // The |between_child_spacing| is the horizontal margin of the key name.
  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
      kKeyNameMarginHorizPx));

  SetText(text);
}

std::u16string SubtleNotificationView::InstructionView::GetText() const {
  return text_;
}

void SubtleNotificationView::InstructionView::SetText(
    const std::u16string& text) {
  SetTextAndImages(text, std::vector<std::unique_ptr<views::View>>());
}

void SubtleNotificationView::InstructionView::SetTextAndImages(
    const std::u16string& text,
    std::vector<std::unique_ptr<views::View>> key_images) {
  // Avoid replacing the contents with the same text.
  if (text == text_ && key_images.empty())
    return;

  RemoveAllChildViews();

  // Parse |text|, looking for pipe-delimited segment.
  std::vector<std::u16string> segments = base::SplitString(
      text, kKeyNameDelimiter, base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  // SplitString() returns empty strings for zero-length segments, so given an
  // even number of pipes, there should always be an odd number of segments.
  // The exception is if |text| is entirely empty, in which case the returned
  // list is also empty (rather than containing a single empty string).
  DCHECK(segments.empty() || segments.size() % 2 == 1);

  // Every second segment is formatted as a key, so should have an image
  // specified for it (even if that image is an empty unique_ptr).
  // Or, we can have no key images at all.
  //
  // There should always be one non-key segment preceding each key segment
  // (even if empty, as above) and one segment at the end, so the total number
  // of segments should be double the number of key segments, plus one.
  DCHECK(key_images.empty() || key_images.size() * 2 + 1 == segments.size());

  // Add text segment, alternating between non-key (no border) and key (border)
  // formatting.
  bool format_as_key = false;
  int idx = 0;
  for (const auto& segment : segments) {
    std::unique_ptr<views::View> key_image;
    if (!key_images.empty() && format_as_key) {
      key_image = std::move(key_images[idx]);
      idx++;
    }
    AddTextSegment(segment, format_as_key, std::move(key_image));
    format_as_key = !format_as_key;
  }

  text_ = text;
  OnPropertyChanged(&text_, views::kPropertyEffectsPaint);
}

void SubtleNotificationView::InstructionView::AddTextSegment(
    const std::u16string& text,
    bool format_as_key,
    std::unique_ptr<views::View> key_image) {
  constexpr SkColor kForegroundColor = SK_ColorWHITE;

  views::Label* label = new views::Label(text, kInstructionTextContext);
  label->SetEnabledColor(kForegroundColor);
  label->SetBackgroundColor(GetSubtleNotificationBackgroundColor());

  if (!format_as_key) {
    DCHECK(!key_image);
    AddChildViewRaw(label);
    return;
  }

  views::View* key = new views::View;
  auto key_name_layout = std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal,
      gfx::Insets::VH(0, kKeyNamePaddingPx), kKeyNameImageSpacingPx);
  key_name_layout->set_minimum_cross_axis_size(
      label->GetPreferredSize({}).height() + kKeyNamePaddingPx * 2);
  key->SetLayoutManager(std::move(key_name_layout));
  if (key_image)
    key->AddChildView(std::move(key_image));
  key->AddChildViewRaw(label);
  // The key name has a border around it.
  std::unique_ptr<views::Border> border(views::CreateRoundedRectBorder(
      kKeyNameBorderPx, kKeyNameCornerRadius, kForegroundColor));
  key->SetBorder(std::move(border));
  AddChildViewRaw(key);
}

base::CallbackListSubscription
SubtleNotificationView::InstructionView::AddTextChangedCallback(
    views::PropertyChangedCallback callback) {
  return AddPropertyChangedCallback(&text_, std::move(callback));
}

BEGIN_METADATA(SubtleNotificationView, InstructionView)
ADD_PROPERTY_METADATA(std::u16string, Text)
END_METADATA

SubtleNotificationView::SubtleNotificationView() : instruction_view_(nullptr) {
  auto bubble_border = std::make_unique<views::BubbleBorder>(
      views::BubbleBorder::NONE, views::BubbleBorder::NO_SHADOW);
  bubble_border->SetColor(GetSubtleNotificationBackgroundColor());
  SetBackground(std::make_unique<views::BubbleBackground>(bubble_border.get()));
  SetBorder(std::move(bubble_border));

  instruction_view_ = new InstructionView(std::u16string());

  int outer_padding_horiz = kOuterPaddingHorizPx;
  int outer_padding_vert = kOuterPaddingVertPx;
  AddChildViewRaw(instruction_view_.get());

  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal,
      gfx::Insets::VH(outer_padding_vert, outer_padding_horiz),
      kMiddlePaddingPx));

  GetViewAccessibility().SetRole(ax::mojom::Role::kAlert);
  UpdateAccessibleName();
  text_changed_callback_ = instruction_view_->AddTextChangedCallback(
      base::BindRepeating(&SubtleNotificationView::OnInstructionViewTextChanged,
                          base::Unretained(this)));
}

SubtleNotificationView::~SubtleNotificationView() = default;

void SubtleNotificationView::UpdateContent(
    const std::u16string& instruction_text) {
  instruction_view_->SetText(instruction_text);
  instruction_view_->SetVisible(!instruction_text.empty());
  DeprecatedLayoutImmediately();
}

void SubtleNotificationView::UpdateContent(
    const std::u16string& instruction_text,
    std::vector<std::unique_ptr<views::View>> key_images) {
  instruction_view_->SetTextAndImages(instruction_text, std::move(key_images));
  instruction_view_->SetVisible(!instruction_text.empty());
  DeprecatedLayoutImmediately();
}

// static
views::Widget* SubtleNotificationView::CreatePopupWidget(
    gfx::NativeView parent_view,
    std::unique_ptr<SubtleNotificationView> view) {
  // Initialize the popup.
  views::Widget* popup = new views::Widget;
  views::Widget::InitParams params(
      views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
      views::Widget::InitParams::TYPE_POPUP);
#if BUILDFLAG(IS_WIN)
  // On Windows, this widget isn't parented on purpose to avoid it being
  // obscured by other topmost widgets. See crbug.com/1431043. Setting
  // `parent_view` as the context instead of the parent to meet Aura's
  // requirement for widgets to have either a parent_view or a context.
  // TODO(crbug.com/40066609): Aura should respect the fine-grained levels of
  // topmost windows defined in ZOrderLevel.
  params.context = parent_view;
#else
  params.parent = parent_view;
#endif

  params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
  params.z_order = ui::ZOrderLevel::kSecuritySurface;
  params.accept_events = false;
  popup->Init(std::move(params));
  popup->SetContentsView(std::move(view));
  // We set layout manager to nullptr to prevent the widget from sizing its
  // contents to the same size as itself. This prevents the widget contents from
  // shrinking while we animate the height of the popup to give the impression
  // that it is sliding off the top of the screen.
  // TODO(mgiuca): This probably isn't necessary now that there is no slide
  // animation. Remove it.
  popup->GetRootView()->SetLayoutManager(nullptr);

  return popup;
}

void SubtleNotificationView::OnInstructionViewTextChanged() {
  UpdateAccessibleName();
}

void SubtleNotificationView::UpdateAccessibleName() {
  std::u16string accessible_name;
  base::RemoveChars(instruction_view_->GetText(), kKeyNameDelimiter,
                    &accessible_name);
  GetViewAccessibility().SetName(accessible_name);
}

std::u16string SubtleNotificationView::GetInstructionTextForTest() const {
  return instruction_view_->GetText();
}

BEGIN_METADATA(SubtleNotificationView)
END_METADATA