File: tooltip_icon.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 (183 lines) | stat: -rw-r--r-- 5,364 bytes parent folder | download | duplicates (6)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/bubble/tooltip_icon.h"

#include "base/observer_list.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/bubble/info_bubble.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/mouse_watcher_view_host.h"
#include "ui/views/style/platform_style.h"

namespace views {

TooltipIcon::TooltipIcon(const std::u16string& tooltip, int tooltip_icon_size)
    : tooltip_(tooltip),
      tooltip_icon_size_(tooltip_icon_size),

      bubble_(nullptr) {
  SetFocusBehavior(PlatformStyle::kDefaultFocusBehavior);
  set_suppress_default_focus_handling();
  FocusRing::Install(this);
  SetBorder(CreateEmptyBorder(
      LayoutProvider::Get()->GetInsetsMetric(INSETS_VECTOR_IMAGE_BUTTON)));
  InstallCircleHighlightPathGenerator(this);

  // The tooltip icon, despite visually being an icon with no text, actually
  // opens a bubble whenever the user mouses over it or focuses it, so it's
  // essentially a text control that hides itself when not in view without
  // altering the bubble's layout when shown. As such, have it behave like
  // static text for screenreader users, since that's the role it serves here
  // anyway.
  GetViewAccessibility().SetRole(ax::mojom::Role::kStaticText);
  GetViewAccessibility().SetName(tooltip_);
}

TooltipIcon::~TooltipIcon() {
  observers_.Notify(&Observer::OnTooltipIconDestroying, this);
  HideBubble();
}

void TooltipIcon::SetBubbleWidth(int preferred_width) {
  preferred_width_ = preferred_width;
  OnPropertyChanged(&preferred_width_, kPropertyEffectsPreferredSizeChanged);
}

int TooltipIcon::GetBubbleWidth() const {
  return preferred_width_;
}

void TooltipIcon::SetAnchorPointArrow(BubbleBorder::Arrow arrow) {
  anchor_point_arrow_ = arrow;
  OnPropertyChanged(&anchor_point_arrow_, kPropertyEffectsPaint);
}

BubbleBorder::Arrow TooltipIcon::GetAnchorPointArrow() const {
  return anchor_point_arrow_;
}

void TooltipIcon::OnMouseEntered(const ui::MouseEvent& event) {
  mouse_inside_ = true;
  show_timer_.Start(FROM_HERE, base::Milliseconds(150), this,
                    &TooltipIcon::ShowBubble);
}

void TooltipIcon::OnMouseExited(const ui::MouseEvent& event) {
  show_timer_.Stop();
}

bool TooltipIcon::OnMousePressed(const ui::MouseEvent& event) {
  // Swallow the click so that the parent doesn't process it.
  return true;
}

void TooltipIcon::OnFocus() {
  ShowBubble();
#if BUILDFLAG(IS_WIN)
  // Tooltip text does not announce on Windows; crbug.com/1245470
  NotifyAccessibilityEventDeprecated(ax::mojom::Event::kFocus, true);
#endif
}

void TooltipIcon::OnBlur() {
  HideBubble();
}

void TooltipIcon::OnGestureEvent(ui::GestureEvent* event) {
  if (event->type() == ui::EventType::kGestureTap) {
    ShowBubble();
    event->SetHandled();
  }
}

void TooltipIcon::OnThemeChanged() {
  ImageView::OnThemeChanged();
  SetDrawAsHovered(false);
}

void TooltipIcon::MouseMovedOutOfHost() {
  if (IsMouseHovered()) {
    mouse_watcher_->Start(GetWidget()->GetNativeWindow());
    return;
  }

  mouse_inside_ = false;
  HideBubble();
}

void TooltipIcon::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void TooltipIcon::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void TooltipIcon::SetDrawAsHovered(bool hovered) {
  SetImage(ui::ImageModel::FromVectorIcon(
      vector_icons::kInfoOutlineIcon,
      GetColorProvider()->GetColor(hovered ? ui::kColorHelpIconActive
                                           : ui::kColorHelpIconInactive),
      tooltip_icon_size_));
}

void TooltipIcon::ShowBubble() {
  if (bubble_) {
    return;
  }

  SetDrawAsHovered(true);

  bubble_ = new InfoBubble(this, anchor_point_arrow_, tooltip_);
  bubble_->set_preferred_width(preferred_width_);
  // When shown due to a gesture event, close on deactivate (i.e. don't use
  // "focusless").
  bubble_->SetCanActivate(!mouse_inside_);

  bubble_->Show();
  observation_.Observe(bubble_->GetWidget());

  if (mouse_inside_) {
    View* frame = bubble_->GetWidget()->non_client_view()->frame_view();
    mouse_watcher_ = std::make_unique<MouseWatcher>(
        std::make_unique<MouseWatcherViewHost>(frame, gfx::Insets()), this);
    mouse_watcher_->Start(GetWidget()->GetNativeWindow());
  }

  observers_.Notify(&Observer::OnTooltipBubbleShown, this);
}

void TooltipIcon::HideBubble() {
  if (bubble_) {
    bubble_->Hide();
  }
}

void TooltipIcon::OnWidgetDestroyed(Widget* widget) {
  DCHECK(observation_.IsObservingSource(widget));
  observation_.Reset();

  SetDrawAsHovered(false);
  mouse_watcher_.reset();
  bubble_ = nullptr;
}

BEGIN_METADATA(TooltipIcon)
ADD_PROPERTY_METADATA(int, BubbleWidth)
ADD_PROPERTY_METADATA(BubbleBorder::Arrow, AnchorPointArrow)
END_METADATA

}  // namespace views