infobar.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2014 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_INFOBARS_CORE_INFOBAR_H_
  5. #define COMPONENTS_INFOBARS_CORE_INFOBAR_H_
  6. #include <memory>
  7. #include <utility>
  8. #include "base/memory/raw_ptr.h"
  9. #include "components/infobars/core/infobar_delegate.h"
  10. #include "ui/gfx/animation/animation_delegate_notifier.h"
  11. #include "ui/gfx/animation/slide_animation.h"
  12. #include "ui/gfx/geometry/size.h"
  13. namespace infobars {
  14. class InfoBarContainer;
  15. class InfoBarManager;
  16. // InfoBar is a cross-platform base class for an infobar "view" (in the MVC
  17. // sense), which owns a corresponding InfoBarDelegate "model". Typically,
  18. // a caller will call XYZInfoBarDelegate::Create() and pass in the
  19. // InfoBarManager for the relevant tab. This will create an XYZInfoBarDelegate,
  20. // create a platform-specific subclass of InfoBar to own it, and then call
  21. // InfoBarManager::AddInfoBar() to give it ownership of the infobar.
  22. // During its life, the InfoBar may be shown and hidden as the owning tab is
  23. // switched between the foreground and background. Eventually, InfoBarManager
  24. // will instruct the InfoBar to close itself. At this point, the InfoBar will
  25. // optionally animate closed; once it's no longer visible, it deletes itself,
  26. // destroying the InfoBarDelegate in the process.
  27. //
  28. // Thus, InfoBarDelegate and InfoBar implementations can assume they share
  29. // lifetimes, and not NULL-check each other; but if one needs to reach back into
  30. // the owning InfoBarManager, it must check whether that's still possible.
  31. class InfoBar : public gfx::AnimationDelegate {
  32. public:
  33. explicit InfoBar(std::unique_ptr<InfoBarDelegate> delegate);
  34. InfoBar(const InfoBar&) = delete;
  35. InfoBar& operator=(const InfoBar&) = delete;
  36. ~InfoBar() override;
  37. InfoBarManager* owner() { return owner_; }
  38. InfoBarDelegate* delegate() const { return delegate_.get(); }
  39. void set_container(InfoBarContainer* container) { container_ = container; }
  40. // Sets |owner_|. This also sets the nav entry ID on |delegate_|. This must
  41. // only be called once as there's no way to extract an infobar from its owner
  42. // without deleting it, for reparenting in another tab.
  43. void SetOwner(InfoBarManager* owner);
  44. void SetNotifier(std::unique_ptr<gfx::AnimationDelegate> notifier);
  45. // Makes the infobar visible. If |animate| is true, the infobar is then
  46. // animated to full size.
  47. void Show(bool animate);
  48. // Makes the infobar hidden. If |animate| is false, the infobar is
  49. // immediately removed from the container, and, if now unowned, deleted. If
  50. // |animate| is true, the infobar is animated to zero size, ultimately
  51. // triggering a call to AnimationEnded().
  52. void Hide(bool animate);
  53. // Notifies the infobar that it is no longer owned and should delete itself
  54. // once it is invisible.
  55. void CloseSoon();
  56. // Forwards a close request to our owner. This is a no-op if we're already
  57. // unowned.
  58. void RemoveSelf();
  59. // Changes the target height of the infobar.
  60. void SetTargetHeight(int height);
  61. const gfx::SlideAnimation& animation() const { return animation_; }
  62. int computed_height() const { return height_; }
  63. protected:
  64. // gfx::AnimationDelegate:
  65. void AnimationProgressed(const gfx::Animation* animation) override;
  66. const InfoBarContainer* container() const { return container_; }
  67. InfoBarContainer* container() { return container_; }
  68. gfx::SlideAnimation* animation() { return &animation_; }
  69. int target_height() const { return target_height_; }
  70. // Platforms may optionally override these if they need to do work during
  71. // processing of the given calls.
  72. virtual void PlatformSpecificSetOwner() {}
  73. virtual void PlatformSpecificShow(bool animate) {}
  74. virtual void PlatformSpecificHide(bool animate) {}
  75. virtual void PlatformSpecificOnCloseSoon() {}
  76. virtual void PlatformSpecificOnHeightRecalculated() {}
  77. private:
  78. // gfx::AnimationDelegate:
  79. void AnimationEnded(const gfx::Animation* animation) override;
  80. // Finds the new desired height, and if it differs from the current height,
  81. // calls PlatformSpecificOnHeightRecalculated(). Informs our container our
  82. // state has changed if either the height has changed or |force_notify| is
  83. // set.
  84. void RecalculateHeight(bool force_notify);
  85. // Checks whether the infobar is unowned and done with all animations. If so,
  86. // notifies the container that it should remove this infobar, and deletes
  87. // itself.
  88. void MaybeDelete();
  89. raw_ptr<InfoBarManager> owner_;
  90. std::unique_ptr<InfoBarDelegate> delegate_;
  91. raw_ptr<InfoBarContainer> container_;
  92. std::unique_ptr<gfx::AnimationDelegate> notifier_;
  93. gfx::SlideAnimation animation_;
  94. // The current and target heights.
  95. int height_; // Includes both fill and bottom separator.
  96. int target_height_;
  97. };
  98. } // namespace infobars
  99. #endif // COMPONENTS_INFOBARS_CORE_INFOBAR_H_