| 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.h" | 5 #include "components/infobars/core/infobar.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 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_container.h" | 11 #include "components/infobars/core/infobar_container.h" |
| 12 #include "components/infobars/core/infobar_manager.h" | 12 #include "components/infobars/core/infobar_manager.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 InfoBar::InfoBar(scoped_ptr<InfoBarDelegate> delegate) | 17 InfoBar::InfoBar(scoped_ptr<InfoBarDelegate> delegate) |
| 18 : owner_(NULL), | 18 : owner_(nullptr), |
| 19 delegate_(delegate.Pass()), | 19 delegate_(delegate.Pass()), |
| 20 container_(NULL), | 20 container_(nullptr), |
| 21 animation_(this), | 21 animation_(this), |
| 22 arrow_height_(0), | 22 arrow_height_(0), |
| 23 arrow_target_height_(kDefaultArrowTargetHeight), | 23 arrow_target_height_(0), |
| 24 arrow_half_width_(0), | 24 arrow_half_width_(0), |
| 25 bar_height_(0), | 25 bar_height_(0), |
| 26 bar_target_height_(kDefaultBarTargetHeight) { | 26 bar_target_height_(0) { |
| 27 DCHECK(delegate_ != NULL); | 27 DCHECK(delegate_ != nullptr); |
| 28 animation_.SetTweenType(gfx::Tween::LINEAR); | 28 animation_.SetTweenType(gfx::Tween::LINEAR); |
| 29 delegate_->set_infobar(this); | 29 delegate_->set_infobar(this); |
| 30 } | 30 } |
| 31 | 31 |
| 32 InfoBar::~InfoBar() { | 32 InfoBar::~InfoBar() { |
| 33 DCHECK(!owner_); | 33 DCHECK(!owner_); |
| 34 } | 34 } |
| 35 | 35 |
| 36 // static | 36 // static |
| 37 SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) { | 37 SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 49 SkColorSetRGB(250, 230, 145); // Yellow | 49 SkColorSetRGB(250, 230, 145); // Yellow |
| 50 static const SkColor kPageActionBackgroundColorBottom = | 50 static const SkColor kPageActionBackgroundColorBottom = |
| 51 SkColorSetRGB(217, 217, 217); // Gray | 51 SkColorSetRGB(217, 217, 217); // Gray |
| 52 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? | 52 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? |
| 53 kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom; | 53 kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom; |
| 54 } | 54 } |
| 55 | 55 |
| 56 void InfoBar::SetOwner(InfoBarManager* owner) { | 56 void InfoBar::SetOwner(InfoBarManager* owner) { |
| 57 DCHECK(!owner_); | 57 DCHECK(!owner_); |
| 58 owner_ = owner; | 58 owner_ = owner; |
| 59 const InfoBarConstants& constants = owner_->GetInfoBarConstants(); |
| 60 arrow_target_height_ = constants.default_arrow_target_height; |
| 61 bar_target_height_ = constants.default_bar_target_height; |
| 59 delegate_->StoreActiveEntryUniqueID(); | 62 delegate_->StoreActiveEntryUniqueID(); |
| 60 PlatformSpecificSetOwner(); | 63 PlatformSpecificSetOwner(); |
| 61 } | 64 } |
| 62 | 65 |
| 63 void InfoBar::Show(bool animate) { | 66 void InfoBar::Show(bool animate) { |
| 64 PlatformSpecificShow(animate); | 67 PlatformSpecificShow(animate); |
| 65 if (animate) { | 68 if (animate) { |
| 66 animation_.Show(); | 69 animation_.Show(); |
| 67 } else { | 70 } else { |
| 68 animation_.Reset(1.0); | 71 animation_.Reset(1.0); |
| 69 RecalculateHeights(true); | 72 RecalculateHeights(true); |
| 70 } | 73 } |
| 71 } | 74 } |
| 72 | 75 |
| 73 void InfoBar::Hide(bool animate) { | 76 void InfoBar::Hide(bool animate) { |
| 74 PlatformSpecificHide(animate); | 77 PlatformSpecificHide(animate); |
| 75 if (animate) { | 78 if (animate) { |
| 76 animation_.Hide(); | 79 animation_.Hide(); |
| 77 } else { | 80 } else { |
| 78 animation_.Reset(0.0); | 81 animation_.Reset(0.0); |
| 79 // We want to remove ourselves from the container immediately even if we | 82 // We want to remove ourselves from the container immediately even if we |
| 80 // still have an owner, which MaybeDelete() won't do. | 83 // still have an owner, which MaybeDelete() won't do. |
| 81 DCHECK(container_); | 84 DCHECK(container_); |
| 82 container_->RemoveInfoBar(this); | 85 container_->RemoveInfoBar(this); |
| 83 MaybeDelete(); // Necessary if the infobar was already closing. | 86 MaybeDelete(); // Necessary if the infobar was already closing. |
| 84 } | 87 } |
| 85 } | 88 } |
| 86 | 89 |
| 87 void InfoBar::SetArrowTargetHeight(int height) { | 90 void InfoBar::SetArrowTargetHeight(int height) { |
| 88 DCHECK_LE(height, kMaximumArrowTargetHeight); | 91 DCHECK(owner_); |
| 92 DCHECK_LE(height, owner_->GetInfoBarConstants().maximum_arrow_target_height); |
| 89 // Once the closing animation starts, we ignore further requests to change the | 93 // Once the closing animation starts, we ignore further requests to change the |
| 90 // target height. | 94 // target height. |
| 91 if ((arrow_target_height_ != height) && !animation_.IsClosing()) { | 95 if ((arrow_target_height_ != height) && !animation_.IsClosing()) { |
| 92 arrow_target_height_ = height; | 96 arrow_target_height_ = height; |
| 93 RecalculateHeights(false); | 97 RecalculateHeights(false); |
| 94 } | 98 } |
| 95 } | 99 } |
| 96 | 100 |
| 97 void InfoBar::CloseSoon() { | 101 void InfoBar::CloseSoon() { |
| 98 owner_ = NULL; | 102 owner_ = nullptr; |
| 99 PlatformSpecificOnCloseSoon(); | 103 PlatformSpecificOnCloseSoon(); |
| 100 MaybeDelete(); | 104 MaybeDelete(); |
| 101 } | 105 } |
| 102 | 106 |
| 103 void InfoBar::RemoveSelf() { | 107 void InfoBar::RemoveSelf() { |
| 104 if (owner_) | 108 if (owner_) |
| 105 owner_->RemoveInfoBar(this); | 109 owner_->RemoveInfoBar(this); |
| 106 } | 110 } |
| 107 | 111 |
| 108 void InfoBar::SetBarTargetHeight(int height) { | 112 void InfoBar::SetBarTargetHeight(int height) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 119 void InfoBar::AnimationEnded(const gfx::Animation* animation) { | 123 void InfoBar::AnimationEnded(const gfx::Animation* animation) { |
| 120 // When the animation ends, we must ensure the container is notified even if | 124 // When the animation ends, we must ensure the container is notified even if |
| 121 // the heights haven't changed, lest it never get an "animation finished" | 125 // the heights haven't changed, lest it never get an "animation finished" |
| 122 // notification. (If the browser doesn't get this notification, it will not | 126 // notification. (If the browser doesn't get this notification, it will not |
| 123 // bother to re-layout the content area for the new infobar size.) | 127 // bother to re-layout the content area for the new infobar size.) |
| 124 RecalculateHeights(true); | 128 RecalculateHeights(true); |
| 125 MaybeDelete(); | 129 MaybeDelete(); |
| 126 } | 130 } |
| 127 | 131 |
| 128 void InfoBar::RecalculateHeights(bool force_notify) { | 132 void InfoBar::RecalculateHeights(bool force_notify) { |
| 133 DCHECK(owner_); |
| 134 const InfoBarConstants& constants = owner_->GetInfoBarConstants(); |
| 129 int old_arrow_height = arrow_height_; | 135 int old_arrow_height = arrow_height_; |
| 130 int old_bar_height = bar_height_; | 136 int old_bar_height = bar_height_; |
| 131 | 137 |
| 132 // Find the desired arrow height/half-width. The arrow area is | 138 // Find the desired arrow height/half-width. The arrow area is |
| 133 // |arrow_height_| * |arrow_half_width_|. When the bar is opening or closing, | 139 // |arrow_height_| * |arrow_half_width_|. When the bar is opening or closing, |
| 134 // scaling each of these with the square root of the animation value causes a | 140 // scaling each of these with the square root of the animation value causes a |
| 135 // linear animation of the area, which matches the perception of the animation | 141 // linear animation of the area, which matches the perception of the animation |
| 136 // of the bar portion. | 142 // of the bar portion. |
| 137 double scale_factor = sqrt(animation_.GetCurrentValue()); | 143 double scale_factor = sqrt(animation_.GetCurrentValue()); |
| 138 arrow_height_ = static_cast<int>(arrow_target_height_ * scale_factor); | 144 arrow_height_ = static_cast<int>(arrow_target_height_ * scale_factor); |
| 139 if (animation_.is_animating()) { | 145 if (animation_.is_animating()) { |
| 140 arrow_half_width_ = static_cast<int>(std::min(arrow_target_height_, | 146 arrow_half_width_ = |
| 141 kMaximumArrowTargetHalfWidth) * scale_factor); | 147 static_cast<int>(std::min(arrow_target_height_, |
| 148 constants.maximum_arrow_target_half_width) * |
| 149 scale_factor); |
| 142 } else { | 150 } else { |
| 143 // When the infobar is not animating (i.e. fully open), we set the | 151 // When the infobar is not animating (i.e. fully open), we set the |
| 144 // half-width to be proportionally the same distance between its default and | 152 // half-width to be proportionally the same distance between its default and |
| 145 // maximum values as the height is between its. | 153 // maximum values as the height is between its. |
| 146 arrow_half_width_ = kDefaultArrowTargetHalfWidth + | 154 arrow_half_width_ = |
| 147 ((kMaximumArrowTargetHalfWidth - kDefaultArrowTargetHalfWidth) * | 155 constants.default_arrow_target_half_width + |
| 148 ((arrow_height_ - kDefaultArrowTargetHeight) / | 156 ((constants.maximum_arrow_target_half_width - |
| 149 (kMaximumArrowTargetHeight - kDefaultArrowTargetHeight))); | 157 constants.default_arrow_target_half_width) * |
| 158 ((arrow_height_ - constants.default_arrow_target_height) / |
| 159 (constants.maximum_arrow_target_height - |
| 160 constants.default_arrow_target_height))); |
| 150 } | 161 } |
| 151 // Add pixels for the stroke, if the arrow is to be visible at all. Without | 162 // Add pixels for the stroke, if the arrow is to be visible at all. Without |
| 152 // this, changing the arrow height from 0 to kSeparatorLineHeight would | 163 // this, changing the arrow height from 0 to constants.separator_line_height |
| 153 // produce no visible effect, because the stroke would paint atop the divider | 164 // would produce no visible effect, because the stroke would paint atop the |
| 154 // line above the infobar. | 165 // divider line above the infobar. |
| 155 if (arrow_height_) | 166 if (arrow_height_) |
| 156 arrow_height_ += kSeparatorLineHeight; | 167 arrow_height_ += constants.separator_line_height; |
| 157 | 168 |
| 158 bar_height_ = animation_.CurrentValueBetween(0, bar_target_height_); | 169 bar_height_ = animation_.CurrentValueBetween(0, bar_target_height_); |
| 159 | 170 |
| 160 // Don't re-layout if nothing has changed, e.g. because the animation step was | 171 // Don't re-layout if nothing has changed, e.g. because the animation step was |
| 161 // not large enough to actually change the heights by at least a pixel. | 172 // not large enough to actually change the heights by at least a pixel. |
| 162 bool heights_differ = | 173 bool heights_differ = |
| 163 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_); | 174 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_); |
| 164 if (heights_differ) | 175 if (heights_differ) |
| 165 PlatformSpecificOnHeightsRecalculated(); | 176 PlatformSpecificOnHeightsRecalculated(); |
| 166 | 177 |
| 167 if (container_ && (heights_differ || force_notify)) | 178 if (container_ && (heights_differ || force_notify)) |
| 168 container_->OnInfoBarStateChanged(animation_.is_animating()); | 179 container_->OnInfoBarStateChanged(animation_.is_animating()); |
| 169 } | 180 } |
| 170 | 181 |
| 171 void InfoBar::MaybeDelete() { | 182 void InfoBar::MaybeDelete() { |
| 172 if (!owner_ && (animation_.GetCurrentValue() == 0.0)) { | 183 if (!owner_ && (animation_.GetCurrentValue() == 0.0)) { |
| 173 if (container_) | 184 if (container_) |
| 174 container_->RemoveInfoBar(this); | 185 container_->RemoveInfoBar(this); |
| 175 delete this; | 186 delete this; |
| 176 } | 187 } |
| 177 } | 188 } |
| 178 | 189 |
| 179 } // namespace infobars | 190 } // namespace infobars |
| OLD | NEW |