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 #include "chrome/browser/ui/views/infobars/infobar.h" | 5 #if defined(TOOLKIT_VIEWS) // TODO(pkasting): Port non-views to use this. |
| 6 |
| 7 #include "chrome/browser/tab_contents/infobar.h" |
6 | 8 |
7 #include <cmath> | 9 #include <cmath> |
8 | 10 |
| 11 #include "chrome/browser/tab_contents/infobar_container.h" |
| 12 #include "chrome/browser/tab_contents/infobar_delegate.h" |
9 #include "ui/base/animation/slide_animation.h" | 13 #include "ui/base/animation/slide_animation.h" |
10 #include "chrome/browser/tab_contents/infobar_delegate.h" | |
11 #include "chrome/browser/ui/views/infobars/infobar_container.h" | |
12 | 14 |
13 InfoBar::InfoBar(InfoBarDelegate* delegate) | 15 InfoBar::InfoBar(TabContentsWrapper* owner, InfoBarDelegate* delegate) |
14 : delegate_(delegate), | 16 : owner_(owner), |
| 17 delegate_(delegate), |
15 container_(NULL), | 18 container_(NULL), |
16 ALLOW_THIS_IN_INITIALIZER_LIST(animation_(new ui::SlideAnimation(this))), | 19 ALLOW_THIS_IN_INITIALIZER_LIST(animation_(new ui::SlideAnimation(this))), |
17 arrow_height_(0), | 20 arrow_height_(0), |
18 arrow_target_height_(kDefaultArrowTargetHeight), | 21 arrow_target_height_(kDefaultArrowTargetHeight), |
19 arrow_half_width_(0), | 22 arrow_half_width_(0), |
20 bar_height_(0), | 23 bar_height_(0), |
21 bar_target_height_(kDefaultBarTargetHeight) { | 24 bar_target_height_(kDefaultBarTargetHeight) { |
| 25 DCHECK(owner != NULL); |
22 DCHECK(delegate != NULL); | 26 DCHECK(delegate != NULL); |
23 animation_->SetTweenType(ui::Tween::LINEAR); | 27 animation_->SetTweenType(ui::Tween::LINEAR); |
24 } | 28 } |
25 | 29 |
26 InfoBar::~InfoBar() { | 30 InfoBar::~InfoBar() { |
27 } | 31 } |
28 | 32 |
29 void InfoBar::Show(bool animate) { | 33 void InfoBar::Show(bool animate) { |
30 if (animate) { | 34 if (animate) { |
31 animation_->Show(); | 35 animation_->Show(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 92 |
89 void InfoBar::RecalculateHeights(bool force_notify) { | 93 void InfoBar::RecalculateHeights(bool force_notify) { |
90 int old_arrow_height = arrow_height_; | 94 int old_arrow_height = arrow_height_; |
91 int old_bar_height = bar_height_; | 95 int old_bar_height = bar_height_; |
92 | 96 |
93 // Find the desired arrow height/half-width. The arrow area is | 97 // Find the desired arrow height/half-width. The arrow area is |
94 // |arrow_height_| * |arrow_half_width_|. When the bar is opening or closing, | 98 // |arrow_height_| * |arrow_half_width_|. When the bar is opening or closing, |
95 // scaling each of these with the square root of the animation value causes a | 99 // scaling each of these with the square root of the animation value causes a |
96 // linear animation of the area, which matches the perception of the animation | 100 // linear animation of the area, which matches the perception of the animation |
97 // of the bar portion. | 101 // of the bar portion. |
98 double scale_factor = sqrt(animation()->GetCurrentValue()); | 102 double scale_factor = sqrt(animation_->GetCurrentValue()); |
99 arrow_height_ = static_cast<int>(arrow_target_height_ * scale_factor); | 103 arrow_height_ = static_cast<int>(arrow_target_height_ * scale_factor); |
100 if (animation_->is_animating()) { | 104 if (animation_->is_animating()) { |
101 arrow_half_width_ = static_cast<int>(std::min(arrow_target_height_, | 105 arrow_half_width_ = static_cast<int>(std::min(arrow_target_height_, |
102 kMaximumArrowTargetHalfWidth) * scale_factor); | 106 kMaximumArrowTargetHalfWidth) * scale_factor); |
103 } else { | 107 } else { |
104 // When the infobar is not animating (i.e. fully open), we set the | 108 // When the infobar is not animating (i.e. fully open), we set the |
105 // half-width to be proportionally the same distance between its default and | 109 // half-width to be proportionally the same distance between its default and |
106 // maximum values as the height is between its. | 110 // maximum values as the height is between its. |
107 arrow_half_width_ = kDefaultArrowTargetHalfWidth + | 111 arrow_half_width_ = kDefaultArrowTargetHalfWidth + |
108 ((kMaximumArrowTargetHalfWidth - kDefaultArrowTargetHalfWidth) * | 112 ((kMaximumArrowTargetHalfWidth - kDefaultArrowTargetHalfWidth) * |
109 ((arrow_height_ - kDefaultArrowTargetHeight) / | 113 ((arrow_height_ - kDefaultArrowTargetHeight) / |
110 (kMaximumArrowTargetHeight - kDefaultArrowTargetHeight))); | 114 (kMaximumArrowTargetHeight - kDefaultArrowTargetHeight))); |
111 } | 115 } |
112 // Add pixels for the stroke, if the arrow is to be visible at all. Without | 116 // Add pixels for the stroke, if the arrow is to be visible at all. Without |
113 // this, changing the arrow height from 0 to kSeparatorLineHeight would | 117 // this, changing the arrow height from 0 to kSeparatorLineHeight would |
114 // produce no visible effect, because the stroke would paint atop the divider | 118 // produce no visible effect, because the stroke would paint atop the divider |
115 // line above the infobar. | 119 // line above the infobar. |
116 if (arrow_height_) | 120 if (arrow_height_) |
117 arrow_height_ += kSeparatorLineHeight; | 121 arrow_height_ += kSeparatorLineHeight; |
118 | 122 |
119 bar_height_ = | 123 bar_height_ = animation_->CurrentValueBetween(0, bar_target_height_); |
120 static_cast<int>(bar_target_height_ * animation()->GetCurrentValue()); | |
121 | 124 |
122 // Don't re-layout if nothing has changed, e.g. because the animation step was | 125 // Don't re-layout if nothing has changed, e.g. because the animation step was |
123 // not large enough to actually change the heights by at least a pixel. | 126 // not large enough to actually change the heights by at least a pixel. |
124 bool heights_differ = | 127 bool heights_differ = |
125 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_); | 128 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_); |
126 if (heights_differ) | 129 if (heights_differ) |
127 PlatformSpecificOnHeightsRecalculated(); | 130 PlatformSpecificOnHeightsRecalculated(); |
128 | 131 |
129 if (container_ && (heights_differ || force_notify)) | 132 if (container_ && (heights_differ || force_notify)) |
130 container_->OnInfoBarStateChanged(animation_->is_animating()); | 133 container_->OnInfoBarStateChanged(animation_->is_animating()); |
131 } | 134 } |
132 | 135 |
133 void InfoBar::MaybeDelete() { | 136 void InfoBar::MaybeDelete() { |
134 if (delegate_ && (animation_->GetCurrentValue() == 0.0)) { | 137 if (delegate_ && (animation_->GetCurrentValue() == 0.0)) { |
135 if (container_) | 138 if (container_) |
136 container_->RemoveInfoBar(this); | 139 container_->RemoveInfoBar(this); |
137 // Note that we only tell the delegate we're closed here, and not when we're | 140 // Note that we only tell the delegate we're closed here, and not when we're |
138 // simply destroyed (by virtue of a tab switch or being moved from window to | 141 // simply destroyed (by virtue of a tab switch or being moved from window to |
139 // window), since this action can cause the delegate to destroy itself. | 142 // window), since this action can cause the delegate to destroy itself. |
140 delegate_->InfoBarClosed(); | 143 delegate_->InfoBarClosed(); |
141 delegate_ = NULL; | 144 delegate_ = NULL; |
142 } | 145 } |
143 } | 146 } |
| 147 |
| 148 #endif // TOOLKIT_VIEWS |
OLD | NEW |