OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "build/build_config.h" | |
6 | |
7 #include "chrome/browser/infobars/infobar_container.h" | |
8 | |
9 #include <algorithm> | |
10 | |
11 #include "base/logging.h" | |
12 #include "chrome/browser/infobars/infobar.h" | |
13 #include "chrome/browser/infobars/infobar_delegate.h" | |
14 #include "ui/gfx/animation/slide_animation.h" | |
15 | |
16 InfoBarContainer::Delegate::~Delegate() { | |
17 } | |
18 | |
19 InfoBarContainer::InfoBarContainer(Delegate* delegate) | |
20 : delegate_(delegate), | |
21 infobar_manager_(NULL), | |
22 top_arrow_target_height_(InfoBar::kDefaultArrowTargetHeight) { | |
23 } | |
24 | |
25 InfoBarContainer::~InfoBarContainer() { | |
26 // RemoveAllInfoBarsForDestruction() should have already cleared our infobars. | |
27 DCHECK(infobars_.empty()); | |
28 if (infobar_manager_) | |
29 infobar_manager_->RemoveObserver(this); | |
30 } | |
31 | |
32 void InfoBarContainer::ChangeInfoBarManager(InfoBarManager* infobar_manager) { | |
33 if (infobar_manager_) | |
34 infobar_manager_->RemoveObserver(this); | |
35 | |
36 // Hides all infobars in this container without animation. | |
37 while (!infobars_.empty()) { | |
38 InfoBar* infobar = infobars_.front(); | |
39 // Inform the infobar that it's hidden. If it was already closing, this | |
40 // deletes it. Otherwise, this ensures the infobar will be deleted if it's | |
41 // closed while it's not in an InfoBarContainer. | |
42 infobar->Hide(false); | |
43 } | |
44 | |
45 infobar_manager_ = infobar_manager; | |
46 if (infobar_manager_) { | |
47 infobar_manager_->AddObserver(this); | |
48 | |
49 for (size_t i = 0; i < infobar_manager_->infobar_count(); ++i) { | |
50 // As when we removed the infobars above, we prevent callbacks to | |
51 // OnInfoBarStateChanged() for each infobar. | |
52 AddInfoBar(infobar_manager_->infobar_at(i), i, false, NO_CALLBACK); | |
53 } | |
54 } | |
55 | |
56 // Now that everything is up to date, signal the delegate to re-layout. | |
57 OnInfoBarStateChanged(false); | |
58 } | |
59 | |
60 int InfoBarContainer::GetVerticalOverlap(int* total_height) { | |
61 // Our |total_height| is the sum of the preferred heights of the InfoBars | |
62 // contained within us plus the |vertical_overlap|. | |
63 int vertical_overlap = 0; | |
64 int next_infobar_y = 0; | |
65 | |
66 for (InfoBars::iterator i(infobars_.begin()); i != infobars_.end(); ++i) { | |
67 InfoBar* infobar = *i; | |
68 next_infobar_y -= infobar->arrow_height(); | |
69 vertical_overlap = std::max(vertical_overlap, -next_infobar_y); | |
70 next_infobar_y += infobar->total_height(); | |
71 } | |
72 | |
73 if (total_height) | |
74 *total_height = next_infobar_y + vertical_overlap; | |
75 return vertical_overlap; | |
76 } | |
77 | |
78 void InfoBarContainer::SetMaxTopArrowHeight(int height) { | |
79 // Decrease the height by the arrow stroke thickness, which is the separator | |
80 // line height, because the infobar arrow target heights are without-stroke. | |
81 top_arrow_target_height_ = std::min( | |
82 std::max(height - InfoBar::kSeparatorLineHeight, 0), | |
83 InfoBar::kMaximumArrowTargetHeight); | |
84 UpdateInfoBarArrowTargetHeights(); | |
85 } | |
86 | |
87 void InfoBarContainer::OnInfoBarStateChanged(bool is_animating) { | |
88 if (delegate_) | |
89 delegate_->InfoBarContainerStateChanged(is_animating); | |
90 UpdateInfoBarArrowTargetHeights(); | |
91 PlatformSpecificInfoBarStateChanged(is_animating); | |
92 } | |
93 | |
94 void InfoBarContainer::RemoveInfoBar(InfoBar* infobar) { | |
95 infobar->set_container(NULL); | |
96 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); | |
97 DCHECK(i != infobars_.end()); | |
98 PlatformSpecificRemoveInfoBar(infobar); | |
99 infobars_.erase(i); | |
100 } | |
101 | |
102 void InfoBarContainer::RemoveAllInfoBarsForDestruction() { | |
103 // Before we remove any children, we reset |delegate_|, so that no removals | |
104 // will result in us trying to call | |
105 // delegate_->InfoBarContainerStateChanged(). This is important because at | |
106 // this point |delegate_| may be shutting down, and it's at best unimportant | |
107 // and at worst disastrous to call that. | |
108 delegate_ = NULL; | |
109 ChangeInfoBarManager(NULL); | |
110 } | |
111 | |
112 void InfoBarContainer::OnInfoBarAdded(InfoBar* infobar) { | |
113 AddInfoBar(infobar, infobars_.size(), true, WANT_CALLBACK); | |
114 } | |
115 | |
116 void InfoBarContainer::OnInfoBarRemoved(InfoBar* infobar, bool animate) { | |
117 infobar->Hide(animate); | |
118 UpdateInfoBarArrowTargetHeights(); | |
119 } | |
120 | |
121 void InfoBarContainer::OnInfoBarReplaced(InfoBar* old_infobar, | |
122 InfoBar* new_infobar) { | |
123 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); | |
124 InfoBars::const_iterator i(std::find(infobars_.begin(), infobars_.end(), | |
125 old_infobar)); | |
126 DCHECK(i != infobars_.end()); | |
127 size_t position = i - infobars_.begin(); | |
128 old_infobar->Hide(false); | |
129 AddInfoBar(new_infobar, position, false, WANT_CALLBACK); | |
130 } | |
131 | |
132 void InfoBarContainer::OnManagerShuttingDown(InfoBarManager* manager) { | |
133 DCHECK_EQ(infobar_manager_, manager); | |
134 infobar_manager_->RemoveObserver(this); | |
135 infobar_manager_ = NULL; | |
136 } | |
137 | |
138 void InfoBarContainer::AddInfoBar(InfoBar* infobar, | |
139 size_t position, | |
140 bool animate, | |
141 CallbackStatus callback_status) { | |
142 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == | |
143 infobars_.end()); | |
144 DCHECK_LE(position, infobars_.size()); | |
145 infobars_.insert(infobars_.begin() + position, infobar); | |
146 UpdateInfoBarArrowTargetHeights(); | |
147 PlatformSpecificAddInfoBar(infobar, position); | |
148 if (callback_status == WANT_CALLBACK) | |
149 infobar->set_container(this); | |
150 infobar->Show(animate); | |
151 if (callback_status == NO_CALLBACK) | |
152 infobar->set_container(this); | |
153 } | |
154 | |
155 void InfoBarContainer::UpdateInfoBarArrowTargetHeights() { | |
156 for (size_t i = 0; i < infobars_.size(); ++i) | |
157 infobars_[i]->SetArrowTargetHeight(ArrowTargetHeightForInfoBar(i)); | |
158 } | |
159 | |
160 int InfoBarContainer::ArrowTargetHeightForInfoBar(size_t infobar_index) const { | |
161 if (!delegate_ || !delegate_->DrawInfoBarArrows(NULL)) | |
162 return 0; | |
163 if (infobar_index == 0) | |
164 return top_arrow_target_height_; | |
165 const gfx::SlideAnimation& first_infobar_animation = | |
166 const_cast<const InfoBar*>(infobars_.front())->animation(); | |
167 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) | |
168 return InfoBar::kDefaultArrowTargetHeight; | |
169 // When the first infobar is animating closed, we animate the second infobar's | |
170 // arrow target height from the default to the top target height. Note that | |
171 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. | |
172 return top_arrow_target_height_ + static_cast<int>( | |
173 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * | |
174 first_infobar_animation.GetCurrentValue()); | |
175 } | |
OLD | NEW |