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
3 // found in the LICENSE file.
5 #include "components/infobars/core/infobar.h"
9 #include "base/logging.h"
10 #include "build/build_config.h"
11 #include "components/infobars/core/infobar_container.h"
12 #include "components/infobars/core/infobar_manager.h"
13 #include "ui/base/resource/material_design/material_design_controller.h"
14 #include "ui/gfx/animation/slide_animation.h"
18 InfoBar::InfoBar(scoped_ptr
<InfoBarDelegate
> delegate
)
20 delegate_(delegate
.Pass()),
24 arrow_target_height_(0),
27 bar_target_height_(-1) {
28 DCHECK(delegate_
!= NULL
);
29 animation_
.SetTweenType(gfx::Tween::LINEAR
);
30 delegate_
->set_infobar(this);
38 SkColor
InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type
) {
39 if (ui::MaterialDesignController::IsModeMaterial()) {
40 static const SkColor kWarningBackgroundColorMd
=
41 SkColorSetRGB(0xFF, 0xEC, 0xB3); // Yellow
42 static const SkColor kPageActionBackgroundColorMd
= SK_ColorWHITE
;
44 return infobar_type
== InfoBarDelegate::WARNING_TYPE
?
45 kWarningBackgroundColorMd
: kPageActionBackgroundColorMd
;
48 static const SkColor kWarningBackgroundColorTop
=
49 SkColorSetRGB(255, 242, 183); // Yellow
50 static const SkColor kPageActionBackgroundColorTop
=
51 SkColorSetRGB(237, 237, 237); // Gray
52 return (infobar_type
== InfoBarDelegate::WARNING_TYPE
) ?
53 kWarningBackgroundColorTop
: kPageActionBackgroundColorTop
;
57 SkColor
InfoBar::GetBottomColor(InfoBarDelegate::Type infobar_type
) {
59 if (ui::MaterialDesignController::IsModeMaterial())
60 return GetTopColor(infobar_type
);
62 static const SkColor kWarningBackgroundColorBottom
=
63 SkColorSetRGB(250, 230, 145); // Yellow
64 static const SkColor kPageActionBackgroundColorBottom
=
65 SkColorSetRGB(217, 217, 217); // Gray
66 return (infobar_type
== InfoBarDelegate::WARNING_TYPE
) ?
67 kWarningBackgroundColorBottom
: kPageActionBackgroundColorBottom
;
70 void InfoBar::SetOwner(InfoBarManager
* owner
) {
73 delegate_
->set_nav_entry_id(owner
->GetActiveEntryID());
74 PlatformSpecificSetOwner();
77 void InfoBar::Show(bool animate
) {
78 PlatformSpecificShow(animate
);
82 animation_
.Reset(1.0);
83 RecalculateHeights(true);
87 void InfoBar::Hide(bool animate
) {
88 PlatformSpecificHide(animate
);
92 animation_
.Reset(0.0);
93 // We want to remove ourselves from the container immediately even if we
94 // still have an owner, which MaybeDelete() won't do.
96 container_
->RemoveInfoBar(this);
97 MaybeDelete(); // Necessary if the infobar was already closing.
101 void InfoBar::SetArrowTargetHeight(int height
) {
102 // Once the closing animation starts, we ignore further requests to change the
104 if ((arrow_target_height_
!= height
) && !animation_
.IsClosing()) {
105 arrow_target_height_
= height
;
106 RecalculateHeights(false);
110 void InfoBar::CloseSoon() {
112 PlatformSpecificOnCloseSoon();
116 void InfoBar::RemoveSelf() {
118 owner_
->RemoveInfoBar(this);
121 void InfoBar::SetBarTargetHeight(int height
) {
122 if (bar_target_height_
!= height
) {
123 bar_target_height_
= height
;
124 RecalculateHeights(false);
128 void InfoBar::AnimationProgressed(const gfx::Animation
* animation
) {
129 RecalculateHeights(false);
132 void InfoBar::AnimationEnded(const gfx::Animation
* animation
) {
133 // When the animation ends, we must ensure the container is notified even if
134 // the heights haven't changed, lest it never get an "animation finished"
135 // notification. (If the browser doesn't get this notification, it will not
136 // bother to re-layout the content area for the new infobar size.)
137 RecalculateHeights(true);
141 void InfoBar::RecalculateHeights(bool force_notify
) {
142 // If there's no container delegate, there's no way to compute new element
143 // sizes, so return immediately. We don't need to worry that this might leave
144 // us with bogus sizes, because if we're ever re-added to a container, it will
145 // call Show(false) while re-adding us, which will compute a correct set of
147 if (!container_
|| !container_
->delegate())
150 int old_arrow_height
= arrow_height_
;
151 int old_bar_height
= bar_height_
;
153 container_
->delegate()->ComputeInfoBarElementSizes(
154 animation_
, arrow_target_height_
, bar_target_height_
, &arrow_height_
,
155 &arrow_half_width_
, &bar_height_
);
157 // Don't re-layout if nothing has changed, e.g. because the animation step was
158 // not large enough to actually change the heights by at least a pixel.
159 bool heights_differ
=
160 (old_arrow_height
!= arrow_height_
) || (old_bar_height
!= bar_height_
);
162 PlatformSpecificOnHeightsRecalculated();
164 if (heights_differ
|| force_notify
)
165 container_
->OnInfoBarStateChanged(animation_
.is_animating());
168 void InfoBar::MaybeDelete() {
169 if (!owner_
&& (animation_
.GetCurrentValue() == 0.0)) {
171 container_
->RemoveInfoBar(this);
176 } // namespace infobars