| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "components/infobars/core/infobar_container.h" | 5 #include "components/infobars/core/infobar_container.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 11 #include "components/infobars/core/infobar.h" | 12 #include "components/infobars/core/infobar.h" |
| 12 #include "components/infobars/core/infobar_delegate.h" | 13 #include "components/infobars/core/infobar_delegate.h" |
| 13 #include "ui/gfx/animation/slide_animation.h" | |
| 14 | 14 |
| 15 namespace infobars { | 15 namespace infobars { |
| 16 | 16 |
| 17 InfoBarContainer::Delegate::~Delegate() { | 17 InfoBarContainer::Delegate::~Delegate() { |
| 18 } | 18 } |
| 19 | 19 |
| 20 InfoBarContainer::InfoBarContainer(Delegate* delegate) | 20 InfoBarContainer::InfoBarContainer(Delegate* delegate) |
| 21 : delegate_(delegate), | 21 : delegate_(delegate), |
| 22 infobar_manager_(NULL), | 22 infobar_manager_(NULL), |
| 23 top_arrow_target_height_(InfoBar::kDefaultArrowTargetHeight) { | 23 ignore_infobar_state_changed_(false) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 InfoBarContainer::~InfoBarContainer() { | 26 InfoBarContainer::~InfoBarContainer() { |
| 27 // RemoveAllInfoBarsForDestruction() should have already cleared our infobars. | 27 // RemoveAllInfoBarsForDestruction() should have already cleared our infobars. |
| 28 DCHECK(infobars_.empty()); | 28 DCHECK(infobars_.empty()); |
| 29 if (infobar_manager_) | 29 if (infobar_manager_) |
| 30 infobar_manager_->RemoveObserver(this); | 30 infobar_manager_->RemoveObserver(this); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void InfoBarContainer::ChangeInfoBarManager(InfoBarManager* infobar_manager) { | 33 void InfoBarContainer::ChangeInfoBarManager(InfoBarManager* infobar_manager) { |
| 34 if (infobar_manager_) | 34 if (infobar_manager_) |
| 35 infobar_manager_->RemoveObserver(this); | 35 infobar_manager_->RemoveObserver(this); |
| 36 | 36 |
| 37 // Hides all infobars in this container without animation. | 37 { |
| 38 while (!infobars_.empty()) { | 38 // Ignore intermediate state changes. We'll manually trigger processing |
| 39 InfoBar* infobar = infobars_.front(); | 39 // once we're finished. |
| 40 // Inform the infobar that it's hidden. If it was already closing, this | 40 base::AutoReset<bool> ignore_changes(&ignore_infobar_state_changed_, true); |
| 41 // deletes it. Otherwise, this ensures the infobar will be deleted if it's | |
| 42 // closed while it's not in an InfoBarContainer. | |
| 43 infobar->Hide(false); | |
| 44 } | |
| 45 | 41 |
| 46 infobar_manager_ = infobar_manager; | 42 // Hides all infobars in this container without animation. |
| 47 if (infobar_manager_) { | 43 while (!infobars_.empty()) { |
| 48 infobar_manager_->AddObserver(this); | 44 InfoBar* infobar = infobars_.front(); |
| 45 // Inform the infobar that it's hidden. If it was already closing, this |
| 46 // deletes it. Otherwise, this ensures the infobar will be deleted if |
| 47 // it's closed while it's not in an InfoBarContainer. |
| 48 infobar->Hide(false); |
| 49 } |
| 49 | 50 |
| 50 for (size_t i = 0; i < infobar_manager_->infobar_count(); ++i) { | 51 infobar_manager_ = infobar_manager; |
| 51 // As when we removed the infobars above, we prevent callbacks to | 52 if (infobar_manager_) { |
| 52 // OnInfoBarStateChanged() for each infobar. | 53 infobar_manager_->AddObserver(this); |
| 53 AddInfoBar(infobar_manager_->infobar_at(i), i, false, NO_CALLBACK); | 54 |
| 55 for (size_t i = 0; i < infobar_manager_->infobar_count(); ++i) |
| 56 AddInfoBar(infobar_manager_->infobar_at(i), i, false); |
| 54 } | 57 } |
| 55 } | 58 } |
| 56 | 59 |
| 57 // Now that everything is up to date, signal the delegate to re-layout. | 60 // Now that everything is up to date, signal the delegate to re-layout. |
| 58 OnInfoBarStateChanged(false); | 61 OnInfoBarStateChanged(false); |
| 59 } | 62 } |
| 60 | 63 |
| 61 int InfoBarContainer::GetVerticalOverlap(int* total_height) const { | 64 int InfoBarContainer::GetVerticalOverlap(int* total_height) const { |
| 62 // Our |total_height| is the sum of the preferred heights of the InfoBars | 65 // Our |total_height| is the sum of the preferred heights of the InfoBars |
| 63 // contained within us plus the |vertical_overlap|. | 66 // contained within us plus the |vertical_overlap|. |
| 64 int vertical_overlap = 0; | 67 int vertical_overlap = 0; |
| 65 int next_infobar_y = 0; | 68 int next_infobar_y = 0; |
| 66 | 69 |
| 67 for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); | 70 for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); |
| 68 ++i) { | 71 ++i) { |
| 69 InfoBar* infobar = *i; | 72 InfoBar* infobar = *i; |
| 70 next_infobar_y -= infobar->arrow_height(); | 73 next_infobar_y -= infobar->arrow_height(); |
| 71 vertical_overlap = std::max(vertical_overlap, -next_infobar_y); | 74 vertical_overlap = std::max(vertical_overlap, -next_infobar_y); |
| 72 next_infobar_y += infobar->total_height(); | 75 next_infobar_y += infobar->total_height(); |
| 73 } | 76 } |
| 74 | 77 |
| 75 if (total_height) | 78 if (total_height) |
| 76 *total_height = next_infobar_y + vertical_overlap; | 79 *total_height = next_infobar_y + vertical_overlap; |
| 77 return vertical_overlap; | 80 return vertical_overlap; |
| 78 } | 81 } |
| 79 | 82 |
| 80 void InfoBarContainer::SetMaxTopArrowHeight(int height) { | 83 void InfoBarContainer::UpdateInfoBarArrowTargetHeights() { |
| 81 // Decrease the height by the arrow stroke thickness, which is the separator | 84 for (size_t i = 0; i < infobars_.size(); ++i) { |
| 82 // line height, because the infobar arrow target heights are without-stroke. | 85 infobars_[i]->SetArrowTargetHeight(delegate_ ? |
| 83 top_arrow_target_height_ = std::min( | 86 delegate_->ArrowTargetHeightForInfoBar( |
| 84 std::max(height - InfoBar::kSeparatorLineHeight, 0), | 87 i, const_cast<const InfoBar*>(infobars_[i])->animation()) : 0); |
| 85 InfoBar::kMaximumArrowTargetHeight); | 88 } |
| 86 UpdateInfoBarArrowTargetHeights(); | |
| 87 } | 89 } |
| 88 | 90 |
| 89 void InfoBarContainer::OnInfoBarStateChanged(bool is_animating) { | 91 void InfoBarContainer::OnInfoBarStateChanged(bool is_animating) { |
| 92 if (ignore_infobar_state_changed_) |
| 93 return; |
| 90 if (delegate_) | 94 if (delegate_) |
| 91 delegate_->InfoBarContainerStateChanged(is_animating); | 95 delegate_->InfoBarContainerStateChanged(is_animating); |
| 92 UpdateInfoBarArrowTargetHeights(); | 96 UpdateInfoBarArrowTargetHeights(); |
| 93 PlatformSpecificInfoBarStateChanged(is_animating); | 97 PlatformSpecificInfoBarStateChanged(is_animating); |
| 94 } | 98 } |
| 95 | 99 |
| 96 void InfoBarContainer::RemoveInfoBar(InfoBar* infobar) { | 100 void InfoBarContainer::RemoveInfoBar(InfoBar* infobar) { |
| 97 infobar->set_container(NULL); | 101 infobar->set_container(NULL); |
| 98 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); | 102 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); |
| 99 DCHECK(i != infobars_.end()); | 103 DCHECK(i != infobars_.end()); |
| 100 PlatformSpecificRemoveInfoBar(infobar); | 104 PlatformSpecificRemoveInfoBar(infobar); |
| 101 infobars_.erase(i); | 105 infobars_.erase(i); |
| 102 } | 106 } |
| 103 | 107 |
| 104 void InfoBarContainer::RemoveAllInfoBarsForDestruction() { | 108 void InfoBarContainer::RemoveAllInfoBarsForDestruction() { |
| 105 // Before we remove any children, we reset |delegate_|, so that no removals | 109 // Before we remove any children, we reset |delegate_|, so that no removals |
| 106 // will result in us trying to call | 110 // will result in us trying to call |
| 107 // delegate_->InfoBarContainerStateChanged(). This is important because at | 111 // delegate_->InfoBarContainerStateChanged(). This is important because at |
| 108 // this point |delegate_| may be shutting down, and it's at best unimportant | 112 // this point |delegate_| may be shutting down, and it's at best unimportant |
| 109 // and at worst disastrous to call that. | 113 // and at worst disastrous to call that. |
| 110 delegate_ = NULL; | 114 delegate_ = NULL; |
| 111 ChangeInfoBarManager(NULL); | 115 ChangeInfoBarManager(NULL); |
| 112 } | 116 } |
| 113 | 117 |
| 114 void InfoBarContainer::OnInfoBarAdded(InfoBar* infobar) { | 118 void InfoBarContainer::OnInfoBarAdded(InfoBar* infobar) { |
| 115 AddInfoBar(infobar, infobars_.size(), true, WANT_CALLBACK); | 119 AddInfoBar(infobar, infobars_.size(), true); |
| 116 } | 120 } |
| 117 | 121 |
| 118 void InfoBarContainer::OnInfoBarRemoved(InfoBar* infobar, bool animate) { | 122 void InfoBarContainer::OnInfoBarRemoved(InfoBar* infobar, bool animate) { |
| 119 infobar->Hide(animate); | 123 infobar->Hide(animate); |
| 120 UpdateInfoBarArrowTargetHeights(); | 124 UpdateInfoBarArrowTargetHeights(); |
| 121 } | 125 } |
| 122 | 126 |
| 123 void InfoBarContainer::OnInfoBarReplaced(InfoBar* old_infobar, | 127 void InfoBarContainer::OnInfoBarReplaced(InfoBar* old_infobar, |
| 124 InfoBar* new_infobar) { | 128 InfoBar* new_infobar) { |
| 125 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); | 129 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); |
| 126 InfoBars::const_iterator i(std::find(infobars_.begin(), infobars_.end(), | 130 InfoBars::const_iterator i(std::find(infobars_.begin(), infobars_.end(), |
| 127 old_infobar)); | 131 old_infobar)); |
| 128 DCHECK(i != infobars_.end()); | 132 DCHECK(i != infobars_.end()); |
| 129 size_t position = i - infobars_.begin(); | 133 size_t position = i - infobars_.begin(); |
| 130 old_infobar->Hide(false); | 134 old_infobar->Hide(false); |
| 131 AddInfoBar(new_infobar, position, false, WANT_CALLBACK); | 135 AddInfoBar(new_infobar, position, false); |
| 132 } | 136 } |
| 133 | 137 |
| 134 void InfoBarContainer::OnManagerShuttingDown(InfoBarManager* manager) { | 138 void InfoBarContainer::OnManagerShuttingDown(InfoBarManager* manager) { |
| 135 DCHECK_EQ(infobar_manager_, manager); | 139 DCHECK_EQ(infobar_manager_, manager); |
| 136 infobar_manager_->RemoveObserver(this); | 140 infobar_manager_->RemoveObserver(this); |
| 137 infobar_manager_ = NULL; | 141 infobar_manager_ = NULL; |
| 138 } | 142 } |
| 139 | 143 |
| 140 void InfoBarContainer::AddInfoBar(InfoBar* infobar, | 144 void InfoBarContainer::AddInfoBar(InfoBar* infobar, |
| 141 size_t position, | 145 size_t position, |
| 142 bool animate, | 146 bool animate) { |
| 143 CallbackStatus callback_status) { | |
| 144 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == | 147 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == |
| 145 infobars_.end()); | 148 infobars_.end()); |
| 146 DCHECK_LE(position, infobars_.size()); | 149 DCHECK_LE(position, infobars_.size()); |
| 147 infobars_.insert(infobars_.begin() + position, infobar); | 150 infobars_.insert(infobars_.begin() + position, infobar); |
| 148 UpdateInfoBarArrowTargetHeights(); | 151 UpdateInfoBarArrowTargetHeights(); |
| 149 PlatformSpecificAddInfoBar(infobar, position); | 152 PlatformSpecificAddInfoBar(infobar, position); |
| 150 if (callback_status == WANT_CALLBACK) | 153 infobar->set_container(this); |
| 151 infobar->set_container(this); | |
| 152 infobar->Show(animate); | 154 infobar->Show(animate); |
| 153 if (callback_status == NO_CALLBACK) | |
| 154 infobar->set_container(this); | |
| 155 } | |
| 156 | |
| 157 void InfoBarContainer::UpdateInfoBarArrowTargetHeights() { | |
| 158 for (size_t i = 0; i < infobars_.size(); ++i) | |
| 159 infobars_[i]->SetArrowTargetHeight(ArrowTargetHeightForInfoBar(i)); | |
| 160 } | |
| 161 | |
| 162 int InfoBarContainer::ArrowTargetHeightForInfoBar(size_t infobar_index) const { | |
| 163 if (!delegate_ || !delegate_->DrawInfoBarArrows(NULL)) | |
| 164 return 0; | |
| 165 if (infobar_index == 0) | |
| 166 return top_arrow_target_height_; | |
| 167 const gfx::SlideAnimation& first_infobar_animation = | |
| 168 const_cast<const InfoBar*>(infobars_.front())->animation(); | |
| 169 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) | |
| 170 return InfoBar::kDefaultArrowTargetHeight; | |
| 171 // When the first infobar is animating closed, we animate the second infobar's | |
| 172 // arrow target height from the default to the top target height. Note that | |
| 173 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. | |
| 174 return top_arrow_target_height_ + static_cast<int>( | |
| 175 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * | |
| 176 first_infobar_animation.GetCurrentValue()); | |
| 177 } | 155 } |
| 178 | 156 |
| 179 } // namespace infobars | 157 } // namespace infobars |
| OLD | NEW |