File: facegaze_bubble_view.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (200 lines) | stat: -rw-r--r-- 7,204 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
196
197
198
199
200
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/accessibility/facegaze_bubble_view.h"

#include <memory>
#include <string_view>

#include "ash/ash_element_identifiers.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/events/event.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view_class_properties.h"

namespace ash {

namespace {

constexpr ui::ColorId kBackgroundColorId =
    cros_tokens::kCrosSysSystemBaseElevatedOpaque;
constexpr ui::ColorId kWarningBackgroundColor =
    cros_tokens::kCrosSysWarningContainer;
constexpr ui::ColorId kWarningForegroundColor =
    cros_tokens::kCrosSysOnWarningContainer;
constexpr int kIconSizeDip = 24;
constexpr int kLeftRightMarginDip = 20;
constexpr int kRoundedCornerRadius = 24.f;
constexpr int kSpaceBetweenIconAndTextDip = 16;
constexpr int kTopBottomMarginDip = 12;
const ui::ResourceBundle::FontStyle kKeyLabelFontStyle =
    ui::ResourceBundle::MediumFont;

std::unique_ptr<views::Label> CreateLabelView(
    raw_ptr<views::Label>* destination_view,
    const std::u16string& text,
    ui::ColorId enabled_color_id) {
  ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();

  return views::Builder<views::Label>()
      .CopyAddressTo(destination_view)
      .SetText(text)
      .SetEnabledColor(enabled_color_id)
      .SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_CENTER)
      .SetMultiLine(false)
      .SetFontList(rb->GetFontList(kKeyLabelFontStyle))
      .Build();
}

std::unique_ptr<views::ImageView> CreateImageView(
    raw_ptr<views::ImageView>* destination_view,
    const gfx::VectorIcon& icon) {
  return views::Builder<views::ImageView>()
      .CopyAddressTo(destination_view)
      .SetImage(ui::ImageModel::FromVectorIcon(icon, kColorAshTextColorPrimary,
                                               kIconSizeDip))
      .Build();
}

}  // namespace

FaceGazeBubbleView::FaceGazeBubbleView(
    const base::RepeatingCallback<void()>& on_mouse_entered,
    const base::RepeatingCallback<void(const ui::Event& event)>&
        on_close_button_clicked) {
  SetBackgroundColor(kBackgroundColorId);
  set_parent_window(
      Shell::GetContainer(Shell::GetPrimaryRootWindow(),
                          kShellWindowId_AccessibilityBubbleContainer));

  // Create layout and specify other properties.
  auto layout = std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal);
  layout->set_between_child_spacing(kSpaceBetweenIconAndTextDip);
  SetLayoutManager(std::move(layout));
  set_margins(gfx::Insets()
                  .set_top(kTopBottomMarginDip)
                  .set_bottom(kTopBottomMarginDip)
                  .set_left(kLeftRightMarginDip)
                  .set_right(kLeftRightMarginDip));
  set_corner_radius(kRoundedCornerRadius);
  set_highlight_button_when_shown(false);
  SetCanActivate(false);
  SetNotifyEnterExitOnChild(true);
  GetViewAccessibility().SetRole(ax::mojom::Role::kGenericContainer);
  SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
  SetProperty(views::kElementIdentifierKey, kFaceGazeBubbleElementId);

  // Add main content and close button to this view.
  main_content_view_ =
      AddChildView(std::make_unique<FaceGazeBubbleMainContentView>(
          std::move(on_mouse_entered)));
  close_view_ = AddChildView(std::make_unique<FaceGazeBubbleCloseView>(
      std::move(on_close_button_clicked)));
}

FaceGazeBubbleView::~FaceGazeBubbleView() = default;

void FaceGazeBubbleView::Update(const std::u16string& text, bool is_warning) {
  main_content_view_->Update(text, is_warning);
  UpdateColor(is_warning);
  SizeToContents();
}

void FaceGazeBubbleView::UpdateColor(bool is_warning) {
  SetBackgroundColor(is_warning ? kWarningBackgroundColor : kBackgroundColorId);
}

std::u16string_view FaceGazeBubbleView::GetTextForTesting() const {
  return main_content_view_->label()->GetText();
}

BEGIN_METADATA(FaceGazeBubbleView)
END_METADATA

FaceGazeBubbleMainContentView::FaceGazeBubbleMainContentView(
    const base::RepeatingCallback<void()>& on_mouse_entered)
    : on_mouse_entered_(std::move(on_mouse_entered)) {
  // Create layout and ensure we receive mouse events.
  auto layout = std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal);
  layout->set_between_child_spacing(kSpaceBetweenIconAndTextDip);
  SetLayoutManager(std::move(layout));
  SetNotifyEnterExitOnChild(true);
  GetViewAccessibility().SetRole(ax::mojom::Role::kGenericContainer);

  // Add child views to the view structure.
  AddChildView(CreateImageView(&image_, kFacegazeIcon));
  AddChildView(
      CreateLabelView(&label_, std::u16string(), kColorAshTextColorPrimary));
}

FaceGazeBubbleMainContentView::~FaceGazeBubbleMainContentView() = default;

void FaceGazeBubbleMainContentView::OnMouseEntered(
    const ui::MouseEvent& event) {
  on_mouse_entered_.Run();
}

void FaceGazeBubbleMainContentView::Update(const std::u16string& text,
                                           bool is_warning) {
  UpdateColor(is_warning);
  label_->SetVisible(text != u"");
  label_->SetText(text);
}

void FaceGazeBubbleMainContentView::UpdateColor(bool is_warning) {
  ui::ColorId foreground_color =
      is_warning ? kWarningForegroundColor : kColorAshTextColorPrimary;
  image_->SetImage(ui::ImageModel::FromVectorIcon(
      kFacegazeIcon, foreground_color, kIconSizeDip));
  label_->SetEnabledColor(foreground_color);
}

BEGIN_METADATA(FaceGazeBubbleMainContentView)
END_METADATA

FaceGazeBubbleCloseView::FaceGazeBubbleCloseView(
    const base::RepeatingCallback<void(const ui::Event& event)>&
        on_close_button_clicked)
    : on_close_button_clicked_(std::move(on_close_button_clicked)) {
  // Create layout.
  auto layout = std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal);
  SetLayoutManager(std::move(layout));

  // Add close button to view structure.
  AddChildView(CreateImageView(&close_button_, kMediumOrLargeCloseButtonIcon));
  close_button_->SetTooltipText(
      l10n_util::GetStringUTF16(IDS_ASH_FACEGAZE_CLOSE_BUTTON_TEXT));
}

FaceGazeBubbleCloseView::~FaceGazeBubbleCloseView() = default;

bool FaceGazeBubbleCloseView::OnMousePressed(const ui::MouseEvent& event) {
  on_close_button_clicked_.Run(event);
  return false;
}

BEGIN_METADATA(FaceGazeBubbleCloseView)
END_METADATA

}  // namespace ash