help_bubble.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_H_
  5. #define COMPONENTS_USER_EDUCATION_COMMON_HELP_BUBBLE_H_
  6. #include "base/callback.h"
  7. #include "base/callback_list.h"
  8. #include "base/compiler_specific.h"
  9. #include "ui/base/interaction/element_identifier.h"
  10. #include "ui/base/interaction/framework_specific_implementation.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. namespace user_education {
  13. // HelpBubble is an interface for the lifecycle of an IPH or tutorial bubble.
  14. // it is implemented by a framework's bubble. It is returned as the result of
  15. // HelpBubbleFactory's CreateBubble...() method.
  16. class HelpBubble : public ui::FrameworkSpecificImplementation {
  17. public:
  18. // Callback to be notified when the help bubble is closed. Note that the
  19. // pointer passed in is entirely for reference and should not be dereferenced
  20. // as another callback may have deleted the bubble itself.
  21. using ClosedCallback = base::OnceCallback<void(HelpBubble*)>;
  22. HelpBubble();
  23. ~HelpBubble() override;
  24. // Sets input focus on the bubble or on the bubble's anchor.
  25. virtual bool ToggleFocusForAccessibility() = 0;
  26. // Closes the bubble if it is not already closed. Returns whether the bubble
  27. // was open.
  28. bool Close();
  29. // Notify that the element the help bubble is anchored to may have moved.
  30. // Default is no-op.
  31. virtual void OnAnchorBoundsChanged();
  32. // Get the bounds of the bubble in the screen. Default is gfx::Rect(), which
  33. // indicates that the bubble's screen position is not identifiable, or that
  34. // the bubble is not visible.
  35. virtual gfx::Rect GetBoundsInScreen() const;
  36. // Returns the context of this help bubble (if there is one).
  37. virtual ui::ElementContext GetContext() const = 0;
  38. // Add a callback to know when a bubble is going away.
  39. [[nodiscard]] base::CallbackListSubscription AddOnCloseCallback(
  40. ClosedCallback callback);
  41. bool is_open() const { return !is_closed() && !closing_; }
  42. protected:
  43. // Actually close the bubble.
  44. virtual void CloseBubbleImpl() = 0;
  45. // Updates internal state to indicate that the bubble has been closed.
  46. // Called by Close(), but can also be called if the bubble is closed by user
  47. // action, etc.
  48. void NotifyBubbleClosed();
  49. private:
  50. // Closed callbacks are cleared out on close, so this keeps us from having to
  51. // store extra data about closed status that could become out of sync.
  52. bool is_closed() const { return !on_close_callbacks_; }
  53. using CallbackList = base::OnceCallbackList<ClosedCallback::RunType>;
  54. std::unique_ptr<CallbackList> on_close_callbacks_;
  55. bool closing_ = false;
  56. };
  57. } // namespace user_education
  58. #endif // COMPONENTS_USER_EDUCATION_COMMON_HELP_BUBBLE_H_