This source file includes following definitions.
- bar_target_height_
- GetTopColor
- GetBottomColor
- SetOwner
- Show
- Hide
- SetArrowTargetHeight
- CloseSoon
- RemoveSelf
- SetBarTargetHeight
- AnimationProgressed
- AnimationEnded
- RecalculateHeights
- MaybeDelete
#include "chrome/browser/infobars/infobar.h"
#include <cmath>
#include "base/logging.h"
#include "build/build_config.h"
#include "chrome/browser/infobars/infobar_container.h"
#include "chrome/browser/infobars/infobar_manager.h"
#include "ui/gfx/animation/slide_animation.h"
InfoBar::InfoBar(scoped_ptr<InfoBarDelegate> delegate)
: owner_(NULL),
delegate_(delegate.Pass()),
container_(NULL),
animation_(this),
arrow_height_(0),
arrow_target_height_(kDefaultArrowTargetHeight),
arrow_half_width_(0),
bar_height_(0),
bar_target_height_(kDefaultBarTargetHeight) {
DCHECK(delegate_ != NULL);
animation_.SetTweenType(gfx::Tween::LINEAR);
delegate_->set_infobar(this);
}
InfoBar::~InfoBar() {
DCHECK(!owner_);
}
SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) {
static const SkColor kWarningBackgroundColorTop =
SkColorSetRGB(255, 242, 183);
static const SkColor kPageActionBackgroundColorTop =
SkColorSetRGB(237, 237, 237);
return (infobar_type == InfoBarDelegate::WARNING_TYPE) ?
kWarningBackgroundColorTop : kPageActionBackgroundColorTop;
}
SkColor InfoBar::GetBottomColor(InfoBarDelegate::Type infobar_type) {
static const SkColor kWarningBackgroundColorBottom =
SkColorSetRGB(250, 230, 145);
static const SkColor kPageActionBackgroundColorBottom =
SkColorSetRGB(217, 217, 217);
return (infobar_type == InfoBarDelegate::WARNING_TYPE) ?
kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom;
}
void InfoBar::SetOwner(InfoBarManager* owner) {
DCHECK(!owner_);
owner_ = owner;
delegate_->StoreActiveEntryUniqueID();
PlatformSpecificSetOwner();
}
void InfoBar::Show(bool animate) {
PlatformSpecificShow(animate);
if (animate) {
animation_.Show();
} else {
animation_.Reset(1.0);
RecalculateHeights(true);
}
}
void InfoBar::Hide(bool animate) {
PlatformSpecificHide(animate);
if (animate) {
animation_.Hide();
} else {
animation_.Reset(0.0);
DCHECK(container_);
container_->RemoveInfoBar(this);
MaybeDelete();
}
}
void InfoBar::SetArrowTargetHeight(int height) {
DCHECK_LE(height, kMaximumArrowTargetHeight);
if ((arrow_target_height_ != height) && !animation_.IsClosing()) {
arrow_target_height_ = height;
RecalculateHeights(false);
}
}
void InfoBar::CloseSoon() {
owner_ = NULL;
PlatformSpecificOnCloseSoon();
MaybeDelete();
}
void InfoBar::RemoveSelf() {
if (owner_)
owner_->RemoveInfoBar(this);
}
void InfoBar::SetBarTargetHeight(int height) {
if (bar_target_height_ != height) {
bar_target_height_ = height;
RecalculateHeights(false);
}
}
void InfoBar::AnimationProgressed(const gfx::Animation* animation) {
RecalculateHeights(false);
}
void InfoBar::AnimationEnded(const gfx::Animation* animation) {
RecalculateHeights(true);
MaybeDelete();
}
void InfoBar::RecalculateHeights(bool force_notify) {
int old_arrow_height = arrow_height_;
int old_bar_height = bar_height_;
double scale_factor = sqrt(animation_.GetCurrentValue());
arrow_height_ = static_cast<int>(arrow_target_height_ * scale_factor);
if (animation_.is_animating()) {
arrow_half_width_ = static_cast<int>(std::min(arrow_target_height_,
kMaximumArrowTargetHalfWidth) * scale_factor);
} else {
arrow_half_width_ = kDefaultArrowTargetHalfWidth +
((kMaximumArrowTargetHalfWidth - kDefaultArrowTargetHalfWidth) *
((arrow_height_ - kDefaultArrowTargetHeight) /
(kMaximumArrowTargetHeight - kDefaultArrowTargetHeight)));
}
if (arrow_height_)
arrow_height_ += kSeparatorLineHeight;
bar_height_ = animation_.CurrentValueBetween(0, bar_target_height_);
bool heights_differ =
(old_arrow_height != arrow_height_) || (old_bar_height != bar_height_);
if (heights_differ)
PlatformSpecificOnHeightsRecalculated();
if (container_ && (heights_differ || force_notify))
container_->OnInfoBarStateChanged(animation_.is_animating());
}
void InfoBar::MaybeDelete() {
if (!owner_ && (animation_.GetCurrentValue() == 0.0)) {
if (container_)
container_->RemoveInfoBar(this);
delete this;
}
}