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 // static |
| 18 int InfoBar::gSeparatorLineHeight = 1; |
| 19 int InfoBar::gDefaultArrowTargetHeight = 9; |
| 20 int InfoBar::gMaximumArrowTargetHeight = 24; |
| 21 int InfoBar::gDefaultArrowTargetHalfWidth = gDefaultArrowTargetHeight; |
| 22 int InfoBar::gMaximumArrowTargetHalfWidth = 14; |
| 23 int InfoBar::gDefaultBarTargetHeight = 36; |
| 24 |
17 InfoBar::InfoBar(scoped_ptr<InfoBarDelegate> delegate) | 25 InfoBar::InfoBar(scoped_ptr<InfoBarDelegate> delegate) |
18 : owner_(NULL), | 26 : owner_(NULL), |
19 delegate_(delegate.Pass()), | 27 delegate_(delegate.Pass()), |
20 container_(NULL), | 28 container_(NULL), |
21 animation_(this), | 29 animation_(this), |
22 arrow_height_(0), | 30 arrow_height_(0), |
23 arrow_target_height_(kDefaultArrowTargetHeight), | 31 arrow_target_height_(gDefaultArrowTargetHeight), |
24 arrow_half_width_(0), | 32 arrow_half_width_(0), |
25 bar_height_(0), | 33 bar_height_(0), |
26 bar_target_height_(kDefaultBarTargetHeight) { | 34 bar_target_height_(gDefaultBarTargetHeight) { |
27 DCHECK(delegate_ != NULL); | 35 DCHECK(delegate_ != NULL); |
28 animation_.SetTweenType(gfx::Tween::LINEAR); | 36 animation_.SetTweenType(gfx::Tween::LINEAR); |
29 delegate_->set_infobar(this); | 37 delegate_->set_infobar(this); |
30 } | 38 } |
31 | 39 |
32 InfoBar::~InfoBar() { | 40 InfoBar::~InfoBar() { |
33 DCHECK(!owner_); | 41 DCHECK(!owner_); |
34 } | 42 } |
35 | 43 |
36 // static | 44 // static |
| 45 void InfoBar::Initialize(int defaultBarTargetHeight, |
| 46 int separatorLineHeight, |
| 47 int defaultArrowTargetHeight, |
| 48 int maximumArrowTargetHeight, |
| 49 int defaultArrowTargetHalfWidth, |
| 50 int maximumArrowTargetHalfWidth) { |
| 51 gDefaultBarTargetHeight = defaultBarTargetHeight; |
| 52 gSeparatorLineHeight = separatorLineHeight; |
| 53 gDefaultArrowTargetHeight = defaultArrowTargetHeight; |
| 54 gMaximumArrowTargetHeight = maximumArrowTargetHeight; |
| 55 gDefaultArrowTargetHalfWidth = defaultArrowTargetHalfWidth; |
| 56 gMaximumArrowTargetHalfWidth = maximumArrowTargetHalfWidth; |
| 57 } |
| 58 |
| 59 // static |
37 SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) { | 60 SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) { |
38 static const SkColor kWarningBackgroundColorTop = | 61 static const SkColor kWarningBackgroundColorTop = |
39 SkColorSetRGB(255, 242, 183); // Yellow | 62 SkColorSetRGB(255, 242, 183); // Yellow |
40 static const SkColor kPageActionBackgroundColorTop = | 63 static const SkColor kPageActionBackgroundColorTop = |
41 SkColorSetRGB(237, 237, 237); // Gray | 64 SkColorSetRGB(237, 237, 237); // Gray |
42 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? | 65 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? |
43 kWarningBackgroundColorTop : kPageActionBackgroundColorTop; | 66 kWarningBackgroundColorTop : kPageActionBackgroundColorTop; |
44 } | 67 } |
45 | 68 |
46 // static | 69 // static |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 animation_.Reset(0.0); | 101 animation_.Reset(0.0); |
79 // We want to remove ourselves from the container immediately even if we | 102 // We want to remove ourselves from the container immediately even if we |
80 // still have an owner, which MaybeDelete() won't do. | 103 // still have an owner, which MaybeDelete() won't do. |
81 DCHECK(container_); | 104 DCHECK(container_); |
82 container_->RemoveInfoBar(this); | 105 container_->RemoveInfoBar(this); |
83 MaybeDelete(); // Necessary if the infobar was already closing. | 106 MaybeDelete(); // Necessary if the infobar was already closing. |
84 } | 107 } |
85 } | 108 } |
86 | 109 |
87 void InfoBar::SetArrowTargetHeight(int height) { | 110 void InfoBar::SetArrowTargetHeight(int height) { |
88 DCHECK_LE(height, kMaximumArrowTargetHeight); | 111 DCHECK_LE(height, gMaximumArrowTargetHeight); |
89 // Once the closing animation starts, we ignore further requests to change the | 112 // Once the closing animation starts, we ignore further requests to change the |
90 // target height. | 113 // target height. |
91 if ((arrow_target_height_ != height) && !animation_.IsClosing()) { | 114 if ((arrow_target_height_ != height) && !animation_.IsClosing()) { |
92 arrow_target_height_ = height; | 115 arrow_target_height_ = height; |
93 RecalculateHeights(false); | 116 RecalculateHeights(false); |
94 } | 117 } |
95 } | 118 } |
96 | 119 |
97 void InfoBar::CloseSoon() { | 120 void InfoBar::CloseSoon() { |
98 owner_ = NULL; | 121 owner_ = NULL; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 154 |
132 // Find the desired arrow height/half-width. The arrow area is | 155 // Find the desired arrow height/half-width. The arrow area is |
133 // |arrow_height_| * |arrow_half_width_|. When the bar is opening or closing, | 156 // |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 | 157 // 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 | 158 // linear animation of the area, which matches the perception of the animation |
136 // of the bar portion. | 159 // of the bar portion. |
137 double scale_factor = sqrt(animation_.GetCurrentValue()); | 160 double scale_factor = sqrt(animation_.GetCurrentValue()); |
138 arrow_height_ = static_cast<int>(arrow_target_height_ * scale_factor); | 161 arrow_height_ = static_cast<int>(arrow_target_height_ * scale_factor); |
139 if (animation_.is_animating()) { | 162 if (animation_.is_animating()) { |
140 arrow_half_width_ = static_cast<int>(std::min(arrow_target_height_, | 163 arrow_half_width_ = static_cast<int>(std::min(arrow_target_height_, |
141 kMaximumArrowTargetHalfWidth) * scale_factor); | 164 gMaximumArrowTargetHalfWidth) * scale_factor); |
142 } else { | 165 } else { |
143 // When the infobar is not animating (i.e. fully open), we set the | 166 // 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 | 167 // half-width to be proportionally the same distance between its default and |
145 // maximum values as the height is between its. | 168 // maximum values as the height is between its. |
146 arrow_half_width_ = kDefaultArrowTargetHalfWidth + | 169 arrow_half_width_ = gDefaultArrowTargetHalfWidth + |
147 ((kMaximumArrowTargetHalfWidth - kDefaultArrowTargetHalfWidth) * | 170 ((gMaximumArrowTargetHalfWidth - gDefaultArrowTargetHalfWidth) * |
148 ((arrow_height_ - kDefaultArrowTargetHeight) / | 171 ((arrow_height_ - gDefaultArrowTargetHeight) / |
149 (kMaximumArrowTargetHeight - kDefaultArrowTargetHeight))); | 172 (gMaximumArrowTargetHeight - gDefaultArrowTargetHeight))); |
150 } | 173 } |
151 // Add pixels for the stroke, if the arrow is to be visible at all. Without | 174 // 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 | 175 // this, changing the arrow height from 0 to gSeparatorLineHeight would |
153 // produce no visible effect, because the stroke would paint atop the divider | 176 // produce no visible effect, because the stroke would paint atop the divider |
154 // line above the infobar. | 177 // line above the infobar. |
155 if (arrow_height_) | 178 if (arrow_height_) |
156 arrow_height_ += kSeparatorLineHeight; | 179 arrow_height_ += gSeparatorLineHeight; |
157 | 180 |
158 bar_height_ = animation_.CurrentValueBetween(0, bar_target_height_); | 181 bar_height_ = animation_.CurrentValueBetween(0, bar_target_height_); |
159 | 182 |
160 // Don't re-layout if nothing has changed, e.g. because the animation step was | 183 // 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. | 184 // not large enough to actually change the heights by at least a pixel. |
162 bool heights_differ = | 185 bool heights_differ = |
163 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_); | 186 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_); |
164 if (heights_differ) | 187 if (heights_differ) |
165 PlatformSpecificOnHeightsRecalculated(); | 188 PlatformSpecificOnHeightsRecalculated(); |
166 | 189 |
167 if (container_ && (heights_differ || force_notify)) | 190 if (container_ && (heights_differ || force_notify)) |
168 container_->OnInfoBarStateChanged(animation_.is_animating()); | 191 container_->OnInfoBarStateChanged(animation_.is_animating()); |
169 } | 192 } |
170 | 193 |
171 void InfoBar::MaybeDelete() { | 194 void InfoBar::MaybeDelete() { |
172 if (!owner_ && (animation_.GetCurrentValue() == 0.0)) { | 195 if (!owner_ && (animation_.GetCurrentValue() == 0.0)) { |
173 if (container_) | 196 if (container_) |
174 container_->RemoveInfoBar(this); | 197 container_->RemoveInfoBar(this); |
175 delete this; | 198 delete this; |
176 } | 199 } |
177 } | 200 } |
178 | 201 |
179 } // namespace infobars | 202 } // namespace infobars |
OLD | NEW |