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