OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_INFOBARS_INFOBAR_H_ | 5 #ifndef CHROME_BROWSER_INFOBARS_INFOBAR_H_ |
6 #define CHROME_BROWSER_INFOBARS_INFOBAR_H_ | 6 #define CHROME_BROWSER_INFOBARS_INFOBAR_H_ |
7 | 7 |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/compiler_specific.h" |
| 12 #include "build/build_config.h" |
11 #include "chrome/browser/infobars/infobar_delegate.h" | 13 #include "chrome/browser/infobars/infobar_delegate.h" |
12 #include "third_party/skia/include/core/SkColor.h" | 14 #include "third_party/skia/include/core/SkColor.h" |
13 #include "ui/gfx/animation/animation_delegate.h" | 15 #include "ui/gfx/animation/animation_delegate.h" |
14 #include "ui/gfx/animation/slide_animation.h" | 16 #include "ui/gfx/animation/slide_animation.h" |
15 #include "ui/gfx/size.h" | 17 #include "ui/gfx/size.h" |
16 | 18 |
17 class InfoBarContainer; | 19 class InfoBarContainer; |
18 class InfoBarService; | 20 class InfoBarService; |
19 | 21 |
20 // InfoBar is a cross-platform base class for an infobar "view" (in the MVC | |
21 // sense), which owns a corresponding InfoBarDelegate "model". Typically, | |
22 // a caller will call XYZInfoBarDelegate::Create() and pass in the | |
23 // InfoBarService for the relevant tab. This will create an XYZInfoBarDelegate, | |
24 // create a platform-specific subclass of InfoBar to own it, and then call | |
25 // InfoBarService::AddInfoBar() to give it ownership of the infobar. | |
26 // During its life, the InfoBar may be shown and hidden as the owning tab is | |
27 // switched between the foreground and background. Eventually, InfoBarService | |
28 // will instruct the InfoBar to close itself. At this point, the InfoBar will | |
29 // optionally animate closed; once it's no longer visible, it deletes itself, | |
30 // destroying the InfoBarDelegate in the process. | |
31 // | |
32 // Thus, InfoBarDelegate and InfoBar implementations can assume they share | |
33 // lifetimes, and not NULL-check each other; but if one needs to reach back into | |
34 // the owning InfoBarService, it must check whether that's still possible. | |
35 class InfoBar : public gfx::AnimationDelegate { | 22 class InfoBar : public gfx::AnimationDelegate { |
36 public: | 23 public: |
37 // These are the types passed as Details for infobar-related notifications. | 24 // These are the types passed as Details for infobar-related notifications. |
38 typedef InfoBar AddedDetails; | 25 typedef InfoBarDelegate AddedDetails; |
39 typedef std::pair<InfoBar*, bool> RemovedDetails; | 26 typedef std::pair<InfoBarDelegate*, bool> RemovedDetails; |
40 typedef std::pair<InfoBar*, InfoBar*> ReplacedDetails; | 27 typedef std::pair<InfoBarDelegate*, InfoBarDelegate*> ReplacedDetails; |
41 | 28 |
42 // Platforms must define these. | 29 // Platforms must define these. |
43 static const int kDefaultBarTargetHeight; | 30 static const int kDefaultBarTargetHeight; |
44 static const int kSeparatorLineHeight; | 31 static const int kSeparatorLineHeight; |
45 static const int kDefaultArrowTargetHeight; | 32 static const int kDefaultArrowTargetHeight; |
46 static const int kMaximumArrowTargetHeight; | 33 static const int kMaximumArrowTargetHeight; |
47 // The half-width (see comments on |arrow_half_width_| below) scales to its | 34 // The half-width (see comments on |arrow_half_width_| below) scales to its |
48 // default and maximum values proportionally to how the height scales to its. | 35 // default and maximum values proportionally to how the height scales to its. |
49 static const int kDefaultArrowTargetHalfWidth; | 36 static const int kDefaultArrowTargetHalfWidth; |
50 static const int kMaximumArrowTargetHalfWidth; | 37 static const int kMaximumArrowTargetHalfWidth; |
51 | 38 |
52 explicit InfoBar(scoped_ptr<InfoBarDelegate> delegate); | 39 InfoBar(InfoBarService* owner, InfoBarDelegate* delegate); |
53 virtual ~InfoBar(); | 40 virtual ~InfoBar(); |
54 | 41 |
55 static SkColor GetTopColor(InfoBarDelegate::Type infobar_type); | 42 static SkColor GetTopColor(InfoBarDelegate::Type infobar_type); |
56 static SkColor GetBottomColor(InfoBarDelegate::Type infobar_type); | 43 static SkColor GetBottomColor(InfoBarDelegate::Type infobar_type); |
57 | 44 |
58 InfoBarService* owner() { return owner_; } | 45 InfoBarDelegate* delegate() { return delegate_; } |
59 InfoBarDelegate* delegate() { return delegate_.get(); } | |
60 const InfoBarDelegate* delegate() const { return delegate_.get(); } | |
61 void set_container(InfoBarContainer* container) { container_ = container; } | 46 void set_container(InfoBarContainer* container) { container_ = container; } |
62 | 47 |
63 // Sets |owner_|. This also calls StoreActiveEntryUniqueID() on |delegate_|. | |
64 // This must only be called once as there's no way to extract an infobar from | |
65 // its owner without deleting it, for reparenting in another tab. | |
66 void SetOwner(InfoBarService* owner); | |
67 | |
68 // Makes the infobar visible. If |animate| is true, the infobar is then | 48 // Makes the infobar visible. If |animate| is true, the infobar is then |
69 // animated to full size. | 49 // animated to full size. |
70 void Show(bool animate); | 50 void Show(bool animate); |
71 | 51 |
72 // Makes the infobar hidden. If |animate| is false, the infobar is | 52 // Makes the infobar hidden. If |animate| is true, the infobar is first |
73 // immediately removed from the container, and, if now unowned, deleted. If | 53 // animated to zero size. Once the infobar is hidden, it is removed from its |
74 // |animate| is true, the infobar is animated to zero size, ultimately | 54 // container (triggering its deletion), and its delegate is closed. |
75 // triggering a call to AnimationEnded(). | |
76 void Hide(bool animate); | 55 void Hide(bool animate); |
77 | 56 |
78 // Changes the target height of the arrow portion of the infobar. This has no | 57 // Changes the target height of the arrow portion of the infobar. This has no |
79 // effect once the infobar is animating closed. | 58 // effect once the infobar is animating closed. |
80 void SetArrowTargetHeight(int height); | 59 void SetArrowTargetHeight(int height); |
81 | 60 |
82 // Notifies the infobar that it is no longer owned and should delete itself | 61 // Notifies the infobar that it is no longer owned and should close its |
83 // once it is invisible. | 62 // delegate once it is invisible. |
84 void CloseSoon(); | 63 void CloseSoon(); |
85 | 64 |
86 // Forwards a close request to our owner. This is a no-op if we're already | |
87 // unowned. | |
88 void RemoveSelf(); | |
89 | |
90 // Changes the target height of the main ("bar") portion of the infobar. | 65 // Changes the target height of the main ("bar") portion of the infobar. |
91 void SetBarTargetHeight(int height); | 66 void SetBarTargetHeight(int height); |
92 | 67 |
93 const gfx::SlideAnimation& animation() const { return animation_; } | 68 const gfx::SlideAnimation& animation() const { return animation_; } |
94 int arrow_height() const { return arrow_height_; } | 69 int arrow_height() const { return arrow_height_; } |
95 int arrow_target_height() const { return arrow_target_height_; } | 70 int arrow_target_height() const { return arrow_target_height_; } |
96 int arrow_half_width() const { return arrow_half_width_; } | 71 int arrow_half_width() const { return arrow_half_width_; } |
97 int total_height() const { return arrow_height_ + bar_height_; } | 72 int total_height() const { return arrow_height_ + bar_height_; } |
98 | 73 |
99 protected: | 74 protected: |
100 // gfx::AnimationDelegate: | 75 // gfx::AnimationDelegate: |
101 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE; | 76 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE; |
102 | 77 |
| 78 // Forwards a close request to our owner. |
| 79 // NOTE: Subclasses should not call this if we're already unowned. |
| 80 void RemoveSelf(); |
| 81 |
103 // Given a control with size |prefsize|, returns the centered y position | 82 // Given a control with size |prefsize|, returns the centered y position |
104 // within us, taking into account animation so the control "slides in" (or | 83 // within us, taking into account animation so the control "slides in" (or |
105 // out) as we animate open and closed. | 84 // out) as we animate open and closed. |
106 int OffsetY(const gfx::Size& prefsize) const; | 85 int OffsetY(const gfx::Size& prefsize) const; |
107 | 86 |
| 87 InfoBarService* owner() const { return owner_; } |
108 const InfoBarContainer* container() const { return container_; } | 88 const InfoBarContainer* container() const { return container_; } |
109 InfoBarContainer* container() { return container_; } | 89 InfoBarContainer* container() { return container_; } |
110 gfx::SlideAnimation* animation() { return &animation_; } | 90 gfx::SlideAnimation* animation() { return &animation_; } |
111 int bar_height() const { return bar_height_; } | 91 int bar_height() const { return bar_height_; } |
112 int bar_target_height() const { return bar_target_height_; } | 92 int bar_target_height() const { return bar_target_height_; } |
113 | 93 |
114 // Platforms may optionally override these if they need to do work during | 94 // Platforms may optionally override these if they need to do work during |
115 // processing of the given calls. | 95 // processing of the given calls. |
116 virtual void PlatformSpecificSetOwner() {} | |
117 virtual void PlatformSpecificShow(bool animate) {} | 96 virtual void PlatformSpecificShow(bool animate) {} |
118 virtual void PlatformSpecificHide(bool animate) {} | 97 virtual void PlatformSpecificHide(bool animate) {} |
119 virtual void PlatformSpecificOnCloseSoon() {} | 98 virtual void PlatformSpecificOnCloseSoon() {} |
120 virtual void PlatformSpecificOnHeightsRecalculated() {} | 99 virtual void PlatformSpecificOnHeightsRecalculated() {} |
121 | 100 |
122 private: | 101 private: |
123 // gfx::AnimationDelegate: | 102 // gfx::AnimationDelegate: |
124 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE; | 103 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE; |
125 | 104 |
126 // Finds the new desired arrow and bar heights, and if they differ from the | 105 // Finds the new desired arrow and bar heights, and if they differ from the |
127 // current ones, calls PlatformSpecificOnHeightRecalculated(). Informs our | 106 // current ones, calls PlatformSpecificOnHeightRecalculated(). Informs our |
128 // container our state has changed if either the heights have changed or | 107 // container our state has changed if either the heights have changed or |
129 // |force_notify| is set. | 108 // |force_notify| is set. |
130 void RecalculateHeights(bool force_notify); | 109 void RecalculateHeights(bool force_notify); |
131 | 110 |
132 // Checks whether the infobar is unowned and done with all animations. If so, | 111 // Checks whether we're closed. If so, notifies the container that it should |
133 // notifies the container that it should remove this infobar, and deletes | 112 // remove us (which will cause the platform-specific code to asynchronously |
134 // itself. | 113 // delete us) and closes the delegate. |
135 void MaybeDelete(); | 114 void MaybeDelete(); |
136 | 115 |
137 InfoBarService* owner_; | 116 InfoBarService* owner_; |
138 scoped_ptr<InfoBarDelegate> delegate_; | 117 InfoBarDelegate* delegate_; |
139 InfoBarContainer* container_; | 118 InfoBarContainer* container_; |
140 gfx::SlideAnimation animation_; | 119 gfx::SlideAnimation animation_; |
141 | 120 |
142 // The current and target heights of the arrow and bar portions, and half the | 121 // The current and target heights of the arrow and bar portions, and half the |
143 // current arrow width. (It's easier to work in half-widths as we draw the | 122 // current arrow width. (It's easier to work in half-widths as we draw the |
144 // arrow as two halves on either side of a center point.) | 123 // arrow as two halves on either side of a center point.) |
145 int arrow_height_; // Includes both fill and top stroke. | 124 int arrow_height_; // Includes both fill and top stroke. |
146 int arrow_target_height_; | 125 int arrow_target_height_; |
147 int arrow_half_width_; // Includes only fill. | 126 int arrow_half_width_; // Includes only fill. |
148 int bar_height_; // Includes both fill and bottom separator. | 127 int bar_height_; // Includes both fill and bottom separator. |
149 int bar_target_height_; | 128 int bar_target_height_; |
150 | 129 |
151 DISALLOW_COPY_AND_ASSIGN(InfoBar); | 130 DISALLOW_COPY_AND_ASSIGN(InfoBar); |
152 }; | 131 }; |
153 | 132 |
154 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_H_ | 133 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_H_ |
OLD | NEW |