help_bubble_factory_views.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2021 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_FACTORY_VIEWS_H_
  5. #define COMPONENTS_USER_EDUCATION_VIEWS_HELP_BUBBLE_FACTORY_VIEWS_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/scoped_observation.h"
  8. #include "components/user_education/common/help_bubble.h"
  9. #include "components/user_education/common/help_bubble_factory.h"
  10. #include "components/user_education/common/help_bubble_params.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. #include "ui/base/accelerators/accelerator.h"
  13. #include "ui/base/interaction/element_identifier.h"
  14. #include "ui/base/interaction/element_tracker.h"
  15. #include "ui/base/interaction/framework_specific_implementation.h"
  16. #include "ui/color/color_id.h"
  17. #include "ui/gfx/geometry/rect.h"
  18. #include "ui/views/widget/widget.h"
  19. #include "ui/views/widget/widget_observer.h"
  20. namespace user_education {
  21. class HelpBubbleView;
  22. class HelpBubbleDelegate;
  23. // Views-specific implementation of the help bubble.
  24. //
  25. // Because this is a FrameworkSpecificImplementation, you can use:
  26. // help_bubble->AsA<HelpBubbleViews>()->bubble_view()
  27. // to retrieve the underlying bubble view.
  28. class HelpBubbleViews : public HelpBubble,
  29. public views::WidgetObserver,
  30. public ui::AcceleratorTarget {
  31. public:
  32. ~HelpBubbleViews() override;
  33. DECLARE_FRAMEWORK_SPECIFIC_METADATA()
  34. // Retrieve the bubble view. If the bubble has been closed, this may return
  35. // null.
  36. HelpBubbleView* bubble_view() { return help_bubble_view_; }
  37. const HelpBubbleView* bubble_view() const { return help_bubble_view_; }
  38. // HelpBubble:
  39. bool ToggleFocusForAccessibility() override;
  40. void OnAnchorBoundsChanged() override;
  41. gfx::Rect GetBoundsInScreen() const override;
  42. ui::ElementContext GetContext() const override;
  43. // ui::AcceleratorTarget
  44. bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
  45. bool CanHandleAccelerators() const override;
  46. private:
  47. friend class HelpBubbleFactoryViews;
  48. friend class HelpBubbleFactoryMac;
  49. explicit HelpBubbleViews(HelpBubbleView* help_bubble_view);
  50. // Clean up properties on the anchor view, if applicable.
  51. void MaybeResetAnchorView();
  52. // HelpBubble:
  53. void CloseBubbleImpl() override;
  54. // views::WidgetObserver:
  55. void OnWidgetDestroying(views::Widget* widget) override;
  56. raw_ptr<HelpBubbleView> help_bubble_view_;
  57. base::ScopedObservation<views::Widget, views::WidgetObserver>
  58. scoped_observation_{this};
  59. };
  60. // Factory implementation for HelpBubbleViews.
  61. class HelpBubbleFactoryViews : public HelpBubbleFactory {
  62. public:
  63. explicit HelpBubbleFactoryViews(const HelpBubbleDelegate* delegate);
  64. ~HelpBubbleFactoryViews() override;
  65. DECLARE_FRAMEWORK_SPECIFIC_METADATA()
  66. // HelpBubbleFactory:
  67. std::unique_ptr<HelpBubble> CreateBubble(ui::TrackedElement* element,
  68. HelpBubbleParams params) override;
  69. bool CanBuildBubbleForTrackedElement(
  70. const ui::TrackedElement* element) const override;
  71. private:
  72. base::raw_ptr<const HelpBubbleDelegate> delegate_;
  73. };
  74. } // namespace user_education
  75. #endif // COMPONENTS_USER_EDUCATION_VIEWS_HELP_BUBBLE_FACTORY_VIEWS_H_