help_bubble_factory_registry.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_COMMON_HELP_BUBBLE_FACTORY_REGISTRY_H_
  5. #define COMPONENTS_USER_EDUCATION_COMMON_HELP_BUBBLE_FACTORY_REGISTRY_H_
  6. #include <map>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/callback_list.h"
  11. #include "components/user_education/common/help_bubble_factory.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/interaction/element_tracker.h"
  16. #include "ui/base/interaction/framework_specific_implementation.h"
  17. namespace user_education {
  18. // HelpBubbleFactoryRegistry provides access to framework specific bubble
  19. // factory implementations. HelpBubbleFactoryRegistry is not a strict singleton
  20. // that instances can be created multiple times in a test environment.
  21. class HelpBubbleFactoryRegistry {
  22. public:
  23. using ToggleFocusCallback = base::RepeatingCallback<void(HelpBubble*)>;
  24. HelpBubbleFactoryRegistry();
  25. ~HelpBubbleFactoryRegistry();
  26. HelpBubbleFactoryRegistry(const HelpBubbleFactoryRegistry&) = delete;
  27. HelpBubbleFactoryRegistry& operator=(const HelpBubbleFactoryRegistry&) =
  28. delete;
  29. // Finds the appropriate factory and creates a bubble, or returns null if the
  30. // bubble cannot be created.
  31. std::unique_ptr<HelpBubble> CreateHelpBubble(ui::TrackedElement* element,
  32. HelpBubbleParams params);
  33. // Returns true if any bubble is showing in any framework.
  34. bool is_any_bubble_showing() const { return !help_bubbles_.empty(); }
  35. // Notifies frameworks that the anchor bounds for a bubble in the given
  36. // context might have changed. For frameworks which automatically reposition
  37. // bubbles, this will be a no-op.
  38. void NotifyAnchorBoundsChanged(ui::ElementContext context);
  39. // Sets focus on a help bubble if there is one in the given context (or
  40. // toggles between help) bubble and its anchor; returns false if there is no
  41. // bubble or nothing can be focused.
  42. bool ToggleFocusForAccessibility(ui::ElementContext context);
  43. // Listens for ToggleFocusForAccessibility() calls for metrics purposes.
  44. base::CallbackListSubscription AddToggleFocusCallback(
  45. ToggleFocusCallback callback);
  46. // Gets the first visible help bubble in the given context, or null if none
  47. // exists.
  48. HelpBubble* GetHelpBubble(ui::ElementContext context);
  49. // Adds a bubble factory of type `T` to the list of bubble factories, if it
  50. // is not already present.
  51. template <class T, typename... Args>
  52. void MaybeRegister(Args&&... args) {
  53. factories_.MaybeRegister<T>(std::forward<Args>(args)...);
  54. }
  55. private:
  56. void OnHelpBubbleClosed(HelpBubble* help_bubble);
  57. // The list of known factories.
  58. ui::FrameworkSpecificRegistrationList<HelpBubbleFactory> factories_;
  59. // The list of known help bubbles.
  60. std::map<HelpBubble*, base::CallbackListSubscription> help_bubbles_;
  61. // For listening
  62. base::RepeatingCallbackList<typename ToggleFocusCallback::RunType>
  63. toggle_focus_callbacks_;
  64. };
  65. } // namespace user_education
  66. #endif // COMPONENTS_USER_EDUCATION_COMMON_HELP_BUBBLE_FACTORY_REGISTRY_H_