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 #ifndef COMPONENTS_INFOBARS_CORE_INFOBAR_H_
6 #define COMPONENTS_INFOBARS_CORE_INFOBAR_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "components/infobars/core/infobar_delegate.h"
12 #include "third_party/skia/include/core/SkColor.h"
13 #include "ui/gfx/animation/animation_delegate.h"
14 #include "ui/gfx/animation/slide_animation.h"
15 #include "ui/gfx/size.h"
19 class InfoBarContainer
;
22 // InfoBar is a cross-platform base class for an infobar "view" (in the MVC
23 // sense), which owns a corresponding InfoBarDelegate "model". Typically,
24 // a caller will call XYZInfoBarDelegate::Create() and pass in the
25 // InfoBarManager for the relevant tab. This will create an XYZInfoBarDelegate,
26 // create a platform-specific subclass of InfoBar to own it, and then call
27 // InfoBarManager::AddInfoBar() to give it ownership of the infobar.
28 // During its life, the InfoBar may be shown and hidden as the owning tab is
29 // switched between the foreground and background. Eventually, InfoBarManager
30 // will instruct the InfoBar to close itself. At this point, the InfoBar will
31 // optionally animate closed; once it's no longer visible, it deletes itself,
32 // destroying the InfoBarDelegate in the process.
34 // Thus, InfoBarDelegate and InfoBar implementations can assume they share
35 // lifetimes, and not NULL-check each other; but if one needs to reach back into
36 // the owning InfoBarManager, it must check whether that's still possible.
37 class InfoBar
: public gfx::AnimationDelegate
{
39 // These are the types passed as Details for infobar-related notifications.
40 typedef InfoBar AddedDetails
;
41 typedef std::pair
<InfoBar
*, bool> RemovedDetails
;
42 typedef std::pair
<InfoBar
*, InfoBar
*> ReplacedDetails
;
44 // Platforms must define these.
45 static const int kDefaultBarTargetHeight
;
46 static const int kSeparatorLineHeight
;
47 static const int kDefaultArrowTargetHeight
;
48 static const int kMaximumArrowTargetHeight
;
49 // The half-width (see comments on |arrow_half_width_| below) scales to its
50 // default and maximum values proportionally to how the height scales to its.
51 static const int kDefaultArrowTargetHalfWidth
;
52 static const int kMaximumArrowTargetHalfWidth
;
54 explicit InfoBar(scoped_ptr
<InfoBarDelegate
> delegate
);
57 static SkColor
GetTopColor(InfoBarDelegate::Type infobar_type
);
58 static SkColor
GetBottomColor(InfoBarDelegate::Type infobar_type
);
60 InfoBarManager
* owner() { return owner_
; }
61 InfoBarDelegate
* delegate() { return delegate_
.get(); }
62 const InfoBarDelegate
* delegate() const { return delegate_
.get(); }
63 void set_container(InfoBarContainer
* container
) { container_
= container
; }
65 // Sets |owner_|. This also calls StoreActiveEntryUniqueID() on |delegate_|.
66 // This must only be called once as there's no way to extract an infobar from
67 // its owner without deleting it, for reparenting in another tab.
68 void SetOwner(InfoBarManager
* owner
);
70 // Makes the infobar visible. If |animate| is true, the infobar is then
71 // animated to full size.
72 void Show(bool animate
);
74 // Makes the infobar hidden. If |animate| is false, the infobar is
75 // immediately removed from the container, and, if now unowned, deleted. If
76 // |animate| is true, the infobar is animated to zero size, ultimately
77 // triggering a call to AnimationEnded().
78 void Hide(bool animate
);
80 // Changes the target height of the arrow portion of the infobar. This has no
81 // effect once the infobar is animating closed.
82 void SetArrowTargetHeight(int height
);
84 // Notifies the infobar that it is no longer owned and should delete itself
85 // once it is invisible.
88 // Forwards a close request to our owner. This is a no-op if we're already
92 // Changes the target height of the main ("bar") portion of the infobar.
93 void SetBarTargetHeight(int height
);
95 const gfx::SlideAnimation
& animation() const { return animation_
; }
96 int arrow_height() const { return arrow_height_
; }
97 int arrow_target_height() const { return arrow_target_height_
; }
98 int arrow_half_width() const { return arrow_half_width_
; }
99 int total_height() const { return arrow_height_
+ bar_height_
; }
102 // gfx::AnimationDelegate:
103 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
;
105 const InfoBarContainer
* container() const { return container_
; }
106 InfoBarContainer
* container() { return container_
; }
107 gfx::SlideAnimation
* animation() { return &animation_
; }
108 int bar_height() const { return bar_height_
; }
109 int bar_target_height() const { return bar_target_height_
; }
111 // Platforms may optionally override these if they need to do work during
112 // processing of the given calls.
113 virtual void PlatformSpecificSetOwner() {}
114 virtual void PlatformSpecificShow(bool animate
) {}
115 virtual void PlatformSpecificHide(bool animate
) {}
116 virtual void PlatformSpecificOnCloseSoon() {}
117 virtual void PlatformSpecificOnHeightsRecalculated() {}
120 // gfx::AnimationDelegate:
121 virtual void AnimationEnded(const gfx::Animation
* animation
) OVERRIDE
;
123 // Finds the new desired arrow and bar heights, and if they differ from the
124 // current ones, calls PlatformSpecificOnHeightRecalculated(). Informs our
125 // container our state has changed if either the heights have changed or
126 // |force_notify| is set.
127 void RecalculateHeights(bool force_notify
);
129 // Checks whether the infobar is unowned and done with all animations. If so,
130 // notifies the container that it should remove this infobar, and deletes
134 InfoBarManager
* owner_
;
135 scoped_ptr
<InfoBarDelegate
> delegate_
;
136 InfoBarContainer
* container_
;
137 gfx::SlideAnimation animation_
;
139 // The current and target heights of the arrow and bar portions, and half the
140 // current arrow width. (It's easier to work in half-widths as we draw the
141 // arrow as two halves on either side of a center point.)
142 int arrow_height_
; // Includes both fill and top stroke.
143 int arrow_target_height_
;
144 int arrow_half_width_
; // Includes only fill.
145 int bar_height_
; // Includes both fill and bottom separator.
146 int bar_target_height_
;
148 DISALLOW_COPY_AND_ASSIGN(InfoBar
);
151 } // namespace infobars
153 #endif // COMPONENTS_INFOBARS_CORE_INFOBAR_H_