OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #if defined(TOOLKIT_VIEWS) // TODO(pkasting): Port non-views to use this. | 5 #if defined(TOOLKIT_VIEWS) // TODO(pkasting): Port non-views to use this. |
6 | 6 |
7 #include "chrome/browser/tab_contents/infobar_container.h" | 7 #include "chrome/browser/tab_contents/infobar_container.h" |
8 | 8 |
9 #include "chrome/browser/tab_contents/infobar.h" | 9 #include "chrome/browser/tab_contents/infobar.h" |
10 #include "chrome/browser/tab_contents/infobar_delegate.h" | 10 #include "chrome/browser/tab_contents/infobar_delegate.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, | 49 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, |
50 tc_source); | 50 tc_source); |
51 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REPLACED, | 51 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REPLACED, |
52 tc_source); | 52 tc_source); |
53 | 53 |
54 for (size_t i = 0; i < tab_contents_->infobar_count(); ++i) { | 54 for (size_t i = 0; i < tab_contents_->infobar_count(); ++i) { |
55 // As when we removed the infobars above, we prevent callbacks to | 55 // As when we removed the infobars above, we prevent callbacks to |
56 // OnInfoBarAnimated() for each infobar. | 56 // OnInfoBarAnimated() for each infobar. |
57 AddInfoBar( | 57 AddInfoBar( |
58 tab_contents_->GetInfoBarDelegateAt(i)->CreateInfoBar(tab_contents_), | 58 tab_contents_->GetInfoBarDelegateAt(i)->CreateInfoBar(tab_contents_), |
59 false, NO_CALLBACK); | 59 i, false, NO_CALLBACK); |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 // Now that everything is up to date, signal the delegate to re-layout. | 63 // Now that everything is up to date, signal the delegate to re-layout. |
64 OnInfoBarStateChanged(false); | 64 OnInfoBarStateChanged(false); |
65 } | 65 } |
66 | 66 |
67 int InfoBarContainer::GetVerticalOverlap(int* total_height) { | 67 int InfoBarContainer::GetVerticalOverlap(int* total_height) { |
68 // Our |total_height| is the sum of the preferred heights of the InfoBars | 68 // Our |total_height| is the sum of the preferred heights of the InfoBars |
69 // contained within us plus the |vertical_overlap|. | 69 // contained within us plus the |vertical_overlap|. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 delegate_ = NULL; | 117 delegate_ = NULL; |
118 ChangeTabContents(NULL); | 118 ChangeTabContents(NULL); |
119 } | 119 } |
120 | 120 |
121 void InfoBarContainer::Observe(NotificationType type, | 121 void InfoBarContainer::Observe(NotificationType type, |
122 const NotificationSource& source, | 122 const NotificationSource& source, |
123 const NotificationDetails& details) { | 123 const NotificationDetails& details) { |
124 switch (type.value) { | 124 switch (type.value) { |
125 case NotificationType::TAB_CONTENTS_INFOBAR_ADDED: | 125 case NotificationType::TAB_CONTENTS_INFOBAR_ADDED: |
126 AddInfoBar( | 126 AddInfoBar( |
127 Details<InfoBarDelegate>(details)->CreateInfoBar(tab_contents_), true, | 127 Details<InfoBarDelegate>(details)->CreateInfoBar(tab_contents_), |
128 WANT_CALLBACK); | 128 infobars_.size(), true, WANT_CALLBACK); |
129 break; | 129 break; |
130 | 130 |
131 case NotificationType::TAB_CONTENTS_INFOBAR_REMOVED: { | 131 case NotificationType::TAB_CONTENTS_INFOBAR_REMOVED: { |
132 typedef std::pair<InfoBarDelegate*, bool> RemoveDetails; | 132 typedef std::pair<InfoBarDelegate*, bool> RemoveDetails; |
133 RemoveDetails* remove_details = Details<RemoveDetails>(details).ptr(); | 133 RemoveDetails* remove_details = Details<RemoveDetails>(details).ptr(); |
134 RemoveInfoBar(remove_details->first, remove_details->second); | 134 RemoveInfoBar(remove_details->first, remove_details->second); |
135 break; | 135 break; |
136 } | 136 } |
137 | 137 |
138 case NotificationType::TAB_CONTENTS_INFOBAR_REPLACED: { | 138 case NotificationType::TAB_CONTENTS_INFOBAR_REPLACED: { |
139 typedef std::pair<InfoBarDelegate*, InfoBarDelegate*> ReplaceDetails; | 139 typedef std::pair<InfoBarDelegate*, InfoBarDelegate*> ReplaceDetails; |
140 ReplaceDetails* replace_details = Details<ReplaceDetails>(details).ptr(); | 140 ReplaceDetails* replace_details = Details<ReplaceDetails>(details).ptr(); |
141 RemoveInfoBar(replace_details->first, false); | 141 AddInfoBar(replace_details->second->CreateInfoBar(tab_contents_), |
142 AddInfoBar(replace_details->second->CreateInfoBar(tab_contents_), false, | 142 RemoveInfoBar(replace_details->first, false), false, WANT_CALLBACK); |
143 WANT_CALLBACK); | |
144 break; | 143 break; |
145 } | 144 } |
146 | 145 |
147 default: | 146 default: |
148 NOTREACHED(); | 147 NOTREACHED(); |
149 break; | 148 break; |
150 } | 149 } |
151 } | 150 } |
152 | 151 |
153 void InfoBarContainer::RemoveInfoBar(InfoBarDelegate* delegate, | 152 size_t InfoBarContainer::RemoveInfoBar(InfoBarDelegate* delegate, |
154 bool use_animation) { | 153 bool use_animation) { |
155 // Search for the infobar associated with |delegate|. We cannot search for | 154 // Search for the infobar associated with |delegate|. We cannot search for |
156 // |delegate| in |tab_contents_|, because an InfoBar remains alive until its | 155 // |delegate| in |tab_contents_|, because an InfoBar remains alive until its |
157 // close animation completes, while the delegate is removed from the tab | 156 // close animation completes, while the delegate is removed from the tab |
158 // immediately. | 157 // immediately. |
159 for (InfoBars::iterator i(infobars_.begin()); i != infobars_.end(); ++i) { | 158 for (InfoBars::iterator i(infobars_.begin()); i != infobars_.end(); ++i) { |
160 InfoBar* infobar = *i; | 159 InfoBar* infobar = *i; |
161 if (infobar->delegate() == delegate) { | 160 if (infobar->delegate() == delegate) { |
| 161 size_t position = i - infobars_.begin(); |
162 // We merely need hide the infobar; it will call back to RemoveInfoBar() | 162 // We merely need hide the infobar; it will call back to RemoveInfoBar() |
163 // itself once it's hidden. | 163 // itself once it's hidden. |
164 infobar->Hide(use_animation); | 164 infobar->Hide(use_animation); |
165 UpdateInfoBarArrowTargetHeights(); | 165 UpdateInfoBarArrowTargetHeights(); |
166 break; | 166 return position; |
167 } | 167 } |
168 } | 168 } |
| 169 NOTREACHED(); |
| 170 return infobars_.size(); |
169 } | 171 } |
170 | 172 |
171 void InfoBarContainer::AddInfoBar(InfoBar* infobar, | 173 void InfoBarContainer::AddInfoBar(InfoBar* infobar, |
| 174 size_t position, |
172 bool animate, | 175 bool animate, |
173 CallbackStatus callback_status) { | 176 CallbackStatus callback_status) { |
174 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == | 177 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == |
175 infobars_.end()); | 178 infobars_.end()); |
176 infobars_.push_back(infobar); | 179 DCHECK_LE(position, infobars_.size()); |
| 180 infobars_.insert(infobars_.begin() + position, infobar); |
177 UpdateInfoBarArrowTargetHeights(); | 181 UpdateInfoBarArrowTargetHeights(); |
178 PlatformSpecificAddInfoBar(infobar); | 182 PlatformSpecificAddInfoBar(infobar, position); |
179 if (callback_status == WANT_CALLBACK) | 183 if (callback_status == WANT_CALLBACK) |
180 infobar->set_container(this); | 184 infobar->set_container(this); |
181 infobar->Show(animate); | 185 infobar->Show(animate); |
182 if (callback_status == NO_CALLBACK) | 186 if (callback_status == NO_CALLBACK) |
183 infobar->set_container(this); | 187 infobar->set_container(this); |
184 } | 188 } |
185 | 189 |
186 void InfoBarContainer::UpdateInfoBarArrowTargetHeights() { | 190 void InfoBarContainer::UpdateInfoBarArrowTargetHeights() { |
187 for (size_t i = 0; i < infobars_.size(); ++i) | 191 for (size_t i = 0; i < infobars_.size(); ++i) |
188 infobars_[i]->SetArrowTargetHeight(ArrowTargetHeightForInfoBar(i)); | 192 infobars_[i]->SetArrowTargetHeight(ArrowTargetHeightForInfoBar(i)); |
(...skipping 10 matching lines...) Expand all Loading... |
199 return InfoBar::kDefaultArrowTargetHeight; | 203 return InfoBar::kDefaultArrowTargetHeight; |
200 // When the first infobar is animating closed, we animate the second infobar's | 204 // When the first infobar is animating closed, we animate the second infobar's |
201 // arrow target height from the default to the top target height. Note that | 205 // arrow target height from the default to the top target height. Note that |
202 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. | 206 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. |
203 return top_arrow_target_height_ + static_cast<int>( | 207 return top_arrow_target_height_ + static_cast<int>( |
204 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * | 208 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * |
205 first_infobar_animation->GetCurrentValue()); | 209 first_infobar_animation->GetCurrentValue()); |
206 } | 210 } |
207 | 211 |
208 #endif // TOOLKIT_VIEWS | 212 #endif // TOOLKIT_VIEWS |
OLD | NEW |