help_bubble_view.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_USER_EDUCATION_VIEWS_HELP_BUBBLE_VIEW_H_
  5. #define COMPONENTS_USER_EDUCATION_VIEWS_HELP_BUBBLE_VIEW_H_
  6. #include <cstddef>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/time/time.h"
  11. #include "base/timer/timer.h"
  12. #include "components/user_education/common/help_bubble_params.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "ui/base/interaction/element_identifier.h"
  15. #include "ui/base/metadata/metadata_header_macros.h"
  16. #include "ui/gfx/geometry/rect.h"
  17. #include "ui/views/bubble/bubble_border.h"
  18. #include "ui/views/bubble/bubble_dialog_delegate_view.h"
  19. #include "ui/views/controls/button/label_button.h"
  20. namespace ui {
  21. class MouseEvent;
  22. } // namespace ui
  23. namespace views {
  24. class ImageView;
  25. class Label;
  26. class MdTextButton;
  27. } // namespace views
  28. namespace user_education {
  29. class HelpBubbleDelegate;
  30. // The HelpBubbleView is a special BubbleDialogDelegateView for
  31. // in-product help which educates users about certain Chrome features in
  32. // a deferred context.
  33. class HelpBubbleView : public views::BubbleDialogDelegateView {
  34. public:
  35. METADATA_HEADER(HelpBubbleView);
  36. DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kHelpBubbleElementIdForTesting);
  37. HelpBubbleView(const HelpBubbleDelegate* delegate,
  38. views::View* anchor_view,
  39. HelpBubbleParams params,
  40. absl::optional<gfx::Rect> anchor_rect = absl::nullopt);
  41. HelpBubbleView(const HelpBubbleView&) = delete;
  42. HelpBubbleView& operator=(const HelpBubbleView&) = delete;
  43. ~HelpBubbleView() override;
  44. // Returns whether the given dialog is a help bubble.
  45. static bool IsHelpBubble(views::DialogDelegate* dialog);
  46. bool IsFocusInHelpBubble() const;
  47. views::LabelButton* GetDefaultButtonForTesting() const;
  48. views::LabelButton* GetNonDefaultButtonForTesting(int index) const;
  49. protected:
  50. // BubbleDialogDelegateView:
  51. bool OnMousePressed(const ui::MouseEvent& event) override;
  52. std::u16string GetAccessibleWindowTitle() const override;
  53. void OnWidgetActivationChanged(views::Widget* widget, bool active) override;
  54. void OnThemeChanged() override;
  55. gfx::Size CalculatePreferredSize() const override;
  56. gfx::Rect GetAnchorRect() const override;
  57. private:
  58. FRIEND_TEST_ALL_PREFIXES(HelpBubbleViewTimeoutTest,
  59. RespectsProvidedTimeoutAfterActivate);
  60. void MaybeStartAutoCloseTimer();
  61. void OnTimeout();
  62. const base::raw_ptr<const HelpBubbleDelegate> delegate_;
  63. // Forces the anchor rect to the specified rectangle (in screen coordinates).
  64. // If an artificial anchor rect is used, we assume the exact target cannot be
  65. // localized, and a visible arrow is not shown.
  66. absl::optional<gfx::Rect> force_anchor_rect_;
  67. base::raw_ptr<views::ImageView> icon_view_ = nullptr;
  68. std::vector<views::Label*> labels_;
  69. // If the bubble has buttons, it must be focusable.
  70. std::vector<views::MdTextButton*> non_default_buttons_;
  71. base::raw_ptr<views::MdTextButton> default_button_ = nullptr;
  72. base::raw_ptr<views::Button> close_button_ = nullptr;
  73. // This is the base accessible name of the window.
  74. std::u16string accessible_name_;
  75. // This is any additional hint text to read.
  76. std::u16string screenreader_hint_text_;
  77. // Track the number of times the widget has been activated; if it's greater
  78. // than 1 we won't re-read the screenreader hint again.
  79. int activate_count_ = 0;
  80. // Prevents the widget we're anchored to from disappearing when it loses
  81. // focus, even if it's marked as close_on_deactivate.
  82. std::unique_ptr<CloseOnDeactivatePin> anchor_pin_;
  83. // Auto close timeout. If the value is 0 (default), the bubble never times
  84. // out.
  85. base::TimeDelta timeout_;
  86. base::OneShotTimer auto_close_timer_;
  87. base::OnceClosure timeout_callback_;
  88. };
  89. } // namespace user_education
  90. #endif // COMPONENTS_USER_EDUCATION_VIEWS_HELP_BUBBLE_VIEW_H_