File: help_bubble_views.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (320 lines) | stat: -rw-r--r-- 11,559 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2025 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/user_education/views/help_bubble_views.h"

#include "base/functional/bind.h"
#include "components/user_education/common/help_bubble/custom_help_bubble.h"
#include "components/user_education/common/user_education_class_properties.h"
#include "components/user_education/common/user_education_events.h"
#include "components/user_education/views/help_bubble_view.h"
#include "components/user_education/views/toggle_tracked_element_attention_utils.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/framework_specific_implementation.h"
#include "ui/views/accessible_pane_view.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/interaction/element_tracker_views.h"

namespace user_education {

namespace {

bool IsFocusInHelpBubble(const views::BubbleDialogDelegateView* bubble) {
#if BUILDFLAG(IS_MAC)
  auto* const focused = bubble->GetFocusManager()->GetFocusedView();
  return focused && focused->GetWidget() == bubble->GetWidget();
#else
  return bubble->GetWidget()->IsActive();
#endif
}

}  // namespace

DEFINE_FRAMEWORK_SPECIFIC_METADATA(HelpBubbleViews)

HelpBubbleViews::HelpBubbleViews(
    views::BubbleDialogDelegateView* help_bubble_view,
    ui::TrackedElement* anchor_element)
    : help_bubble_view_(help_bubble_view), anchor_element_(anchor_element) {
  CHECK(help_bubble_view);
  CHECK(help_bubble_view->GetWidget());
  CHECK(anchor_element);
  scoped_observation_.Observe(help_bubble_view->GetWidget());

  anchor_hidden_subscription_ =
      ui::ElementTracker::GetElementTracker()->AddElementHiddenCallback(
          anchor_element->identifier(), anchor_element->context(),
          base::BindRepeating(&HelpBubbleViews::OnElementHidden,
                              base::Unretained(this)));
  anchor_bounds_changed_subscription_ =
      ui::ElementTracker::GetElementTracker()->AddCustomEventCallback(
          kHelpBubbleAnchorBoundsChangedEvent, anchor_element->context(),
          base::BindRepeating(&HelpBubbleViews::OnElementBoundsChanged,
                              base::Unretained(this)));
}

HelpBubbleViews::~HelpBubbleViews() {
  // Needs to be called here while we still have access to HelpBubbleViews-
  // specific logic.
  Close(CloseReason::kBubbleDestroyed);
}

// static
views::BubbleBorder::Arrow HelpBubbleViews::TranslateArrow(
    HelpBubbleArrow arrow) {
  switch (arrow) {
    case HelpBubbleArrow::kNone:
      return views::BubbleBorder::NONE;
    case HelpBubbleArrow::kTopLeft:
      return views::BubbleBorder::TOP_LEFT;
    case HelpBubbleArrow::kTopRight:
      return views::BubbleBorder::TOP_RIGHT;
    case HelpBubbleArrow::kBottomLeft:
      return views::BubbleBorder::BOTTOM_LEFT;
    case HelpBubbleArrow::kBottomRight:
      return views::BubbleBorder::BOTTOM_RIGHT;
    case HelpBubbleArrow::kLeftTop:
      return views::BubbleBorder::LEFT_TOP;
    case HelpBubbleArrow::kRightTop:
      return views::BubbleBorder::RIGHT_TOP;
    case HelpBubbleArrow::kLeftBottom:
      return views::BubbleBorder::LEFT_BOTTOM;
    case HelpBubbleArrow::kRightBottom:
      return views::BubbleBorder::RIGHT_BOTTOM;
    case HelpBubbleArrow::kTopCenter:
      return views::BubbleBorder::TOP_CENTER;
    case HelpBubbleArrow::kBottomCenter:
      return views::BubbleBorder::BOTTOM_CENTER;
    case HelpBubbleArrow::kLeftCenter:
      return views::BubbleBorder::LEFT_CENTER;
    case HelpBubbleArrow::kRightCenter:
      return views::BubbleBorder::RIGHT_CENTER;
  }
}

bool HelpBubbleViews::ToggleFocusForAccessibility() {
  // // If the bubble isn't present or can't be meaningfully focused, stop.
  if (!help_bubble_view_) {
    return false;
  }

  // If the focus isn't in the help bubble, focus the help bubble.
  // Note that if is_focus_in_ancestor_widget is true, then anchor both exists
  // and has a widget, so anchor->GetWidget() will always be valid.
  if (!IsFocusInHelpBubble(help_bubble_view_)) {
    help_bubble_view_->GetWidget()->Activate();
    help_bubble_view_->RequestFocus();
    return true;
  }

  auto* const anchor = help_bubble_view_->GetAnchorView();
  if (!anchor) {
    return false;
  }

  bool set_focus = false;
  if (anchor->GetViewAccessibility().IsAccessibilityFocusable()) {
#if BUILDFLAG(IS_MAC)
    // Mac does not automatically pass activation on focus, so we have to do it
    // manually.
    anchor->GetWidget()->Activate();
#else
    // Focus the anchor. We can't request focus for an accessibility-only view
    // until we turn on keyboard accessibility for its focus manager.
    anchor->GetFocusManager()->SetKeyboardAccessible(true);
#endif
    anchor->RequestFocus();
    set_focus = true;
  } else if (views::IsViewClass<views::AccessiblePaneView>(anchor)) {
    // An AccessiblePaneView can receive focus, but is not necessarily itself
    // accessibility focusable. Use the built-in functionality for focusing
    // elements of AccessiblePaneView instead.
#if BUILDFLAG(IS_MAC)
    // Mac does not automatically pass activation on focus, so we have to do it
    // manually.
    anchor->GetWidget()->Activate();
#else
    // You can't focus an accessible pane if it's already in accessibility
    // mode, so avoid doing that; the SetPaneFocus() call will go back into
    // accessibility navigation mode.
    anchor->GetFocusManager()->SetKeyboardAccessible(false);
#endif
    set_focus =
        static_cast<views::AccessiblePaneView*>(anchor)->SetPaneFocus(nullptr);
  }

  return set_focus;
}

void HelpBubbleViews::OnAnchorBoundsChanged() {
  if (help_bubble_view_) {
    help_bubble_view_->OnAnchorBoundsChanged();
  }
}

gfx::Rect HelpBubbleViews::GetBoundsInScreen() const {
  return help_bubble_view_
             ? help_bubble_view_->GetWidget()->GetWindowBoundsInScreen()
             : gfx::Rect();
}

ui::ElementContext HelpBubbleViews::GetContext() const {
  return help_bubble_view_
             ? views::ElementTrackerViews::GetContextForView(help_bubble_view_)
             : ui::ElementContext();
}

bool HelpBubbleViews::AcceleratorPressed(const ui::Accelerator& accelerator) {
  if (CanHandleAccelerators()) {
    ToggleFocusForAccessibility();
    return true;
  }

  return false;
}

bool HelpBubbleViews::CanHandleAccelerators() const {
  return help_bubble_view_ && help_bubble_view_->GetWidget() &&
         help_bubble_view_->GetWidget()->IsActive();
}

void HelpBubbleViews::MaybeResetAnchorView() {
  if (!help_bubble_view_) {
    return;
  }
  auto* const anchor_view = help_bubble_view_->GetAnchorView();
  if (!anchor_view) {
    return;
  }
  anchor_view->SetProperty(kHasInProductHelpPromoKey, false);
  MaybeRemoveAttentionStateFromTrackedElement(anchor_view);
}

void HelpBubbleViews::CloseBubbleImpl() {
  anchor_hidden_subscription_ = base::CallbackListSubscription();
  anchor_bounds_changed_subscription_ = base::CallbackListSubscription();
  scoped_observation_.Reset();
  MaybeResetAnchorView();

  // Reset the anchor view. Closing the widget could cause callbacks which could
  // theoretically destroy `this`, so
  auto* const help_bubble_view = help_bubble_view_.get();
  help_bubble_view_ = nullptr;
  anchor_element_ = nullptr;
  if (help_bubble_view && help_bubble_view->GetWidget()) {
    help_bubble_view->GetWidget()->Close();
  }
}

void HelpBubbleViews::OnWidgetDestroying(views::Widget* widget) {
  Close(CloseReason::kBubbleElementDestroyed);
}

void HelpBubbleViews::OnElementHidden(ui::TrackedElement* element) {
  // There could be other elements with the same identifier as the anchor
  // element, so don't close the bubble unless it is actually the anchor.
  if (element != anchor_element_) {
    return;
  }

  anchor_hidden_subscription_ = base::CallbackListSubscription();
  anchor_bounds_changed_subscription_ = base::CallbackListSubscription();
  anchor_element_ = nullptr;
  Close(CloseReason::kAnchorHidden);
}

void HelpBubbleViews::OnElementBoundsChanged(ui::TrackedElement* element) {
  if (help_bubble_view_ && element == anchor_element_) {
    // TODO(dfried): Support arbitrary anchor regions more generally in
    // BubbleDialogDelegateViews so that non-help bubble dialogs can be used
    // as help bubbles when attached to e.g. WebUI elements.
    if (HelpBubbleView::IsHelpBubble(help_bubble_view_)) {
      static_cast<HelpBubbleView*>(help_bubble_view_.get())
          ->SetForceAnchorRect(element->GetScreenBounds());
    }
    OnAnchorBoundsChanged();
  }
}

CustomHelpBubbleViews::CustomHelpBubbleViews(
    std::unique_ptr<views::Widget> widget,
    views::BubbleDialogDelegateView* bubble,
    CustomHelpBubbleUi& ui,
    ui::TrackedElement* anchor_element,
    std::optional<UserAction> accept_button_action,
    std::optional<UserAction> cancel_button_action)
    : HelpBubbleViews(bubble, anchor_element),
      CustomHelpBubble(ui),
      help_bubble_widget_(std::move(widget)),
      accept_button_action_(accept_button_action),
      cancel_button_action_(cancel_button_action) {
  CHECK(help_bubble_widget_);

  // Help bubbles should not close on deactivate.
  bubble->set_close_on_deactivate(false);

  // Help bubbles should always send "ESC Pressed" on escape key, not cancel.
  bubble->set_esc_should_cancel_dialog_override(false);

  bubble->GetWidget()->MakeCloseSynchronous(base::BindOnce(
      &CustomHelpBubbleViews::OnHelpBubbleClosing, base::Unretained(this)));
}

CustomHelpBubbleViews::~CustomHelpBubbleViews() {
  // Ensure that all closing of help bubbles goes through the same logic path.
  //
  // Due to upstream logic in HelpBubbleViews, `OnHelpBubbleClosing()` ends up
  // getting called in a state where the widget cannot correctly be destroyed,
  // leading to a CHECK().
  //
  // This will be unnecessary when HelpBubbleViews is migrated to ownership mode
  // CLIENT_OWNS_WIDGET.
  if (help_bubble_widget_) {
    help_bubble_widget_->CloseWithReason(
        views::Widget::ClosedReason::kUnspecified);
  }
}

void CustomHelpBubbleViews::OnHelpBubbleClosing(
    views::Widget::ClosedReason reason) {
  // The calls below could also destroy `this`, so save off widget in a local.
  // This both guarantees that the widget will get properly destroyed at the end
  // of this method (as is required by `MakeCloseSynchronous()`) and also
  // prevents re-entrancy in the destructor as `help_bubble_widget_` will be
  // null.
  std::unique_ptr<views::Widget> widget = std::move(help_bubble_widget_);

  if (auto* const ui = custom_bubble_ui()) {
    switch (reason) {
      case views::Widget::ClosedReason::kAcceptButtonClicked:
        if (accept_button_action_) {
          ui->NotifyUserAction(*accept_button_action_);
        }
        break;

      case views::Widget::ClosedReason::kCancelButtonClicked:
        if (cancel_button_action_) {
          ui->NotifyUserAction(*cancel_button_action_);
        }
        break;

      case views::Widget::ClosedReason::kCloseButtonClicked:
      case views::Widget::ClosedReason::kEscKeyPressed:
        ui->NotifyUserAction(UserAction::kCancel);
        break;

      case views::Widget::ClosedReason::kLostFocus:
      case views::Widget::ClosedReason::kUnspecified:
        // Do nothing.
        break;
    }
  }

  // This is required when responding to `OnHelpBubbleClosing()`; the widget
  // must be destroyed before this method returns.
  widget.reset();
}

}  // namespace user_education