OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_view.h" | 5 #include "chrome/browser/ui/views/infobars/infobar_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/infobars/infobar_service.h" |
11 #include "chrome/browser/ui/views/infobars/infobar_background.h" | 12 #include "chrome/browser/ui/views/infobars/infobar_background.h" |
12 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
13 #include "components/infobars/core/infobar_delegate.h" | 14 #include "components/infobars/core/infobar_delegate.h" |
14 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
15 #include "third_party/skia/include/effects/SkGradientShader.h" | 16 #include "third_party/skia/include/effects/SkGradientShader.h" |
16 #include "ui/accessibility/ax_view_state.h" | 17 #include "ui/accessibility/ax_view_state.h" |
17 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
18 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
19 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
20 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
(...skipping 20 matching lines...) Expand all Loading... |
41 const int kBeforeCloseButtonSpacing = views::kUnrelatedControlHorizontalSpacing; | 42 const int kBeforeCloseButtonSpacing = views::kUnrelatedControlHorizontalSpacing; |
42 | 43 |
43 bool SortLabelsByDecreasingWidth(views::Label* label_1, views::Label* label_2) { | 44 bool SortLabelsByDecreasingWidth(views::Label* label_1, views::Label* label_2) { |
44 return label_1->GetPreferredSize().width() > | 45 return label_1->GetPreferredSize().width() > |
45 label_2->GetPreferredSize().width(); | 46 label_2->GetPreferredSize().width(); |
46 } | 47 } |
47 | 48 |
48 } // namespace | 49 } // namespace |
49 | 50 |
50 | 51 |
51 // InfoBar -------------------------------------------------------------------- | |
52 | |
53 // static | |
54 const int infobars::InfoBar::kSeparatorLineHeight = | |
55 views::NonClientFrameView::kClientEdgeThickness; | |
56 const int infobars::InfoBar::kDefaultArrowTargetHeight = 9; | |
57 const int infobars::InfoBar::kMaximumArrowTargetHeight = 24; | |
58 const int infobars::InfoBar::kDefaultArrowTargetHalfWidth = | |
59 kDefaultArrowTargetHeight; | |
60 const int infobars::InfoBar::kMaximumArrowTargetHalfWidth = 14; | |
61 const int infobars::InfoBar::kDefaultBarTargetHeight = 36; | |
62 | |
63 // InfoBarView ---------------------------------------------------------------- | 52 // InfoBarView ---------------------------------------------------------------- |
64 | 53 |
65 // static | 54 // static |
66 const int InfoBarView::kButtonButtonSpacing = views::kRelatedButtonHSpacing; | 55 const int InfoBarView::kButtonButtonSpacing = views::kRelatedButtonHSpacing; |
67 const int InfoBarView::kEndOfLabelSpacing = views::kItemLabelSpacing; | 56 const int InfoBarView::kEndOfLabelSpacing = views::kItemLabelSpacing; |
68 | 57 |
69 InfoBarView::InfoBarView(scoped_ptr<infobars::InfoBarDelegate> delegate) | 58 InfoBarView::InfoBarView(scoped_ptr<infobars::InfoBarDelegate> delegate) |
70 : infobars::InfoBar(delegate.Pass()), | 59 : infobars::InfoBar(delegate.Pass()), |
71 views::ExternalFocusTracker(this, NULL), | 60 views::ExternalFocusTracker(this, nullptr), |
72 icon_(NULL), | 61 icon_(nullptr), |
73 close_button_(NULL) { | 62 close_button_(nullptr) { |
74 set_owned_by_client(); // InfoBar deletes itself at the appropriate time. | 63 set_owned_by_client(); // InfoBar deletes itself at the appropriate time. |
75 set_background( | 64 set_background( |
76 new InfoBarBackground(infobars::InfoBar::delegate()->GetInfoBarType())); | 65 new InfoBarBackground(infobars::InfoBar::delegate()->GetInfoBarType())); |
77 } | 66 } |
78 | 67 |
79 InfoBarView::~InfoBarView() { | 68 InfoBarView::~InfoBarView() { |
80 // We should have closed any open menus in PlatformSpecificHide(), then | 69 // We should have closed any open menus in PlatformSpecificHide(), then |
81 // subclasses' RunMenu() functions should have prevented opening any new ones | 70 // subclasses' RunMenu() functions should have prevented opening any new ones |
82 // once we became unowned. | 71 // once we became unowned. |
83 DCHECK(!menu_runner_.get()); | 72 DCHECK(!menu_runner_.get()); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 std::sort(labels->begin(), labels->end(), SortLabelsByDecreasingWidth); | 131 std::sort(labels->begin(), labels->end(), SortLabelsByDecreasingWidth); |
143 AssignWidthsSorted(labels, available_width); | 132 AssignWidthsSorted(labels, available_width); |
144 } | 133 } |
145 | 134 |
146 void InfoBarView::Layout() { | 135 void InfoBarView::Layout() { |
147 // Calculate the fill and stroke paths. We do this here, rather than in | 136 // Calculate the fill and stroke paths. We do this here, rather than in |
148 // PlatformSpecificRecalculateHeight(), because this is also reached when our | 137 // PlatformSpecificRecalculateHeight(), because this is also reached when our |
149 // width is changed, which affects both paths. | 138 // width is changed, which affects both paths. |
150 stroke_path_.rewind(); | 139 stroke_path_.rewind(); |
151 fill_path_.rewind(); | 140 fill_path_.rewind(); |
| 141 DCHECK(owner()); |
| 142 const infobars::InfoBarConstants& constants = owner()->GetInfoBarConstants(); |
152 const infobars::InfoBarContainer::Delegate* delegate = container_delegate(); | 143 const infobars::InfoBarContainer::Delegate* delegate = container_delegate(); |
153 if (delegate) { | 144 if (delegate) { |
154 static_cast<InfoBarBackground*>(background())->set_separator_color( | 145 static_cast<InfoBarBackground*>(background())->set_separator_color( |
155 delegate->GetInfoBarSeparatorColor()); | 146 delegate->GetInfoBarSeparatorColor()); |
156 int arrow_x; | 147 int arrow_x; |
157 SkScalar arrow_fill_height = | 148 SkScalar arrow_fill_height = SkIntToScalar( |
158 SkIntToScalar(std::max(arrow_height() - kSeparatorLineHeight, 0)); | 149 std::max(arrow_height() - constants.separator_line_height, 0)); |
159 SkScalar arrow_fill_half_width = SkIntToScalar(arrow_half_width()); | 150 SkScalar arrow_fill_half_width = SkIntToScalar(arrow_half_width()); |
160 SkScalar separator_height = SkIntToScalar(kSeparatorLineHeight); | 151 SkScalar separator_height = SkIntToScalar(constants.separator_line_height); |
161 if (delegate->DrawInfoBarArrows(&arrow_x) && arrow_fill_height) { | 152 if (delegate->DrawInfoBarArrows(&arrow_x) && arrow_fill_height) { |
162 // Skia pixel centers are at the half-values, so the arrow is horizontally | 153 // Skia pixel centers are at the half-values, so the arrow is horizontally |
163 // centered at |arrow_x| + 0.5. Vertically, the stroke path is the center | 154 // centered at |arrow_x| + 0.5. Vertically, the stroke path is the center |
164 // of the separator, while the fill path is a closed path that extends up | 155 // of the separator, while the fill path is a closed path that extends up |
165 // through the entire height of the separator and down to the bottom of | 156 // through the entire height of the separator and down to the bottom of |
166 // the arrow where it joins the bar. | 157 // the arrow where it joins the bar. |
167 stroke_path_.moveTo( | 158 stroke_path_.moveTo( |
168 SkIntToScalar(arrow_x) + SK_ScalarHalf - arrow_fill_half_width, | 159 SkIntToScalar(arrow_x) + SK_ScalarHalf - arrow_fill_half_width, |
169 SkIntToScalar(arrow_height()) - (separator_height * SK_ScalarHalf)); | 160 SkIntToScalar(arrow_height()) - (separator_height * SK_ScalarHalf)); |
170 stroke_path_.rLineTo(arrow_fill_half_width, -arrow_fill_height); | 161 stroke_path_.rLineTo(arrow_fill_half_width, -arrow_fill_height); |
171 stroke_path_.rLineTo(arrow_fill_half_width, arrow_fill_height); | 162 stroke_path_.rLineTo(arrow_fill_half_width, arrow_fill_height); |
172 | 163 |
173 fill_path_ = stroke_path_; | 164 fill_path_ = stroke_path_; |
174 // Move the top of the fill path up to the top of the separator and then | 165 // Move the top of the fill path up to the top of the separator and then |
175 // extend it down all the way through. | 166 // extend it down all the way through. |
176 fill_path_.offset(0, -separator_height * SK_ScalarHalf); | 167 fill_path_.offset(0, -separator_height * SK_ScalarHalf); |
177 // This 0.01 hack prevents the fill from filling more pixels on the right | 168 // This 0.01 hack prevents the fill from filling more pixels on the right |
178 // edge of the arrow than on the left. | 169 // edge of the arrow than on the left. |
179 const SkScalar epsilon = 0.01f; | 170 const SkScalar epsilon = 0.01f; |
180 fill_path_.rLineTo(-epsilon, 0); | 171 fill_path_.rLineTo(-epsilon, 0); |
181 fill_path_.rLineTo(0, separator_height); | 172 fill_path_.rLineTo(0, separator_height); |
182 fill_path_.rLineTo(epsilon - (arrow_fill_half_width * 2), 0); | 173 fill_path_.rLineTo(epsilon - (arrow_fill_half_width * 2), 0); |
183 fill_path_.close(); | 174 fill_path_.close(); |
184 } | 175 } |
185 } | 176 } |
186 if (bar_height()) { | 177 if (bar_height()) { |
187 fill_path_.addRect(0.0, SkIntToScalar(arrow_height()), | 178 fill_path_.addRect( |
188 SkIntToScalar(width()), SkIntToScalar(height() - kSeparatorLineHeight)); | 179 0.0, SkIntToScalar(arrow_height()), SkIntToScalar(width()), |
| 180 SkIntToScalar(height() - constants.separator_line_height)); |
189 } | 181 } |
190 | 182 |
191 int start_x = kEdgeItemPadding; | 183 int start_x = kEdgeItemPadding; |
192 if (icon_ != NULL) { | 184 if (icon_) { |
193 icon_->SetPosition(gfx::Point(start_x, OffsetY(icon_))); | 185 icon_->SetPosition(gfx::Point(start_x, OffsetY(icon_))); |
194 start_x = icon_->bounds().right() + kIconToLabelSpacing; | 186 start_x = icon_->bounds().right() + kIconToLabelSpacing; |
195 } | 187 } |
196 | 188 |
197 int content_minimum_width = ContentMinimumWidth(); | 189 int content_minimum_width = ContentMinimumWidth(); |
198 close_button_->SetPosition(gfx::Point( | 190 close_button_->SetPosition(gfx::Point( |
199 std::max( | 191 std::max( |
200 start_x + content_minimum_width + | 192 start_x + content_minimum_width + |
201 ((content_minimum_width > 0) ? kBeforeCloseButtonSpacing : 0), | 193 ((content_minimum_width > 0) ? kBeforeCloseButtonSpacing : 0), |
202 width() - kEdgeItemPadding - close_button_->width()), | 194 width() - kEdgeItemPadding - close_button_->width()), |
203 OffsetY(close_button_))); | 195 OffsetY(close_button_))); |
204 } | 196 } |
205 | 197 |
206 void InfoBarView::ViewHierarchyChanged( | 198 void InfoBarView::ViewHierarchyChanged( |
207 const ViewHierarchyChangedDetails& details) { | 199 const ViewHierarchyChangedDetails& details) { |
208 View::ViewHierarchyChanged(details); | 200 View::ViewHierarchyChanged(details); |
209 | 201 |
210 if (details.is_add && (details.child == this) && (close_button_ == NULL)) { | 202 if (details.is_add && (details.child == this) && !close_button_) { |
211 gfx::Image image = delegate()->GetIcon(); | 203 gfx::Image image = delegate()->GetIcon(); |
212 if (!image.IsEmpty()) { | 204 if (!image.IsEmpty()) { |
213 icon_ = new views::ImageView; | 205 icon_ = new views::ImageView; |
214 icon_->SetImage(image.ToImageSkia()); | 206 icon_->SetImage(image.ToImageSkia()); |
215 icon_->SizeToPreferredSize(); | 207 icon_->SizeToPreferredSize(); |
216 AddChildView(icon_); | 208 AddChildView(icon_); |
217 } | 209 } |
218 | 210 |
219 close_button_ = new views::ImageButton(this); | 211 close_button_ = new views::ImageButton(this); |
220 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 212 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
221 close_button_->SetImage(views::CustomButton::STATE_NORMAL, | 213 close_button_->SetImage(views::CustomButton::STATE_NORMAL, |
222 rb.GetImageNamed(IDR_CLOSE_1).ToImageSkia()); | 214 rb.GetImageNamed(IDR_CLOSE_1).ToImageSkia()); |
223 close_button_->SetImage(views::CustomButton::STATE_HOVERED, | 215 close_button_->SetImage(views::CustomButton::STATE_HOVERED, |
224 rb.GetImageNamed(IDR_CLOSE_1_H).ToImageSkia()); | 216 rb.GetImageNamed(IDR_CLOSE_1_H).ToImageSkia()); |
225 close_button_->SetImage(views::CustomButton::STATE_PRESSED, | 217 close_button_->SetImage(views::CustomButton::STATE_PRESSED, |
226 rb.GetImageNamed(IDR_CLOSE_1_P).ToImageSkia()); | 218 rb.GetImageNamed(IDR_CLOSE_1_P).ToImageSkia()); |
227 close_button_->SizeToPreferredSize(); | 219 close_button_->SizeToPreferredSize(); |
228 close_button_->SetAccessibleName( | 220 close_button_->SetAccessibleName( |
229 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); | 221 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); |
230 close_button_->SetFocusable(true); | 222 close_button_->SetFocusable(true); |
231 AddChildView(close_button_); | 223 AddChildView(close_button_); |
232 } else if ((close_button_ != NULL) && (details.parent == this) && | 224 } else if (close_button_ && (details.parent == this) && |
233 (details.child != close_button_) && (close_button_->parent() == this) && | 225 (details.child != close_button_) && |
234 (child_at(child_count() - 1) != close_button_)) { | 226 (close_button_->parent() == this) && |
| 227 (child_at(child_count() - 1) != close_button_)) { |
235 // For accessibility, ensure the close button is the last child view. | 228 // For accessibility, ensure the close button is the last child view. |
236 RemoveChildView(close_button_); | 229 RemoveChildView(close_button_); |
237 AddChildView(close_button_); | 230 AddChildView(close_button_); |
238 } | 231 } |
239 | 232 |
| 233 DCHECK(owner()); |
| 234 const infobars::InfoBarConstants& constants = owner()->GetInfoBarConstants(); |
240 // Ensure the infobar is tall enough to display its contents. | 235 // Ensure the infobar is tall enough to display its contents. |
241 const int kMinimumVerticalPadding = 6; | 236 const int kMinimumVerticalPadding = 6; |
242 int height = kDefaultBarTargetHeight; | 237 int height = constants.default_bar_target_height; |
243 for (int i = 0; i < child_count(); ++i) { | 238 for (int i = 0; i < child_count(); ++i) { |
244 const int child_height = child_at(i)->height(); | 239 const int child_height = child_at(i)->height(); |
245 height = std::max(height, child_height + kMinimumVerticalPadding); | 240 height = std::max(height, child_height + kMinimumVerticalPadding); |
246 } | 241 } |
247 SetBarTargetHeight(height); | 242 SetBarTargetHeight(height); |
248 } | 243 } |
249 | 244 |
250 void InfoBarView::PaintChildren(gfx::Canvas* canvas, | 245 void InfoBarView::PaintChildren(gfx::Canvas* canvas, |
251 const views::CullSet& cull_set) { | 246 const views::CullSet& cull_set) { |
252 canvas->Save(); | 247 canvas->Save(); |
(...skipping 21 matching lines...) Expand all Loading... |
274 } | 269 } |
275 | 270 |
276 int InfoBarView::ContentMinimumWidth() const { | 271 int InfoBarView::ContentMinimumWidth() const { |
277 return 0; | 272 return 0; |
278 } | 273 } |
279 | 274 |
280 int InfoBarView::StartX() const { | 275 int InfoBarView::StartX() const { |
281 // Ensure we don't return a value greater than EndX(), so children can safely | 276 // Ensure we don't return a value greater than EndX(), so children can safely |
282 // set something's width to "EndX() - StartX()" without risking that being | 277 // set something's width to "EndX() - StartX()" without risking that being |
283 // negative. | 278 // negative. |
284 return std::min(EndX(), (icon_ != NULL) ? | 279 return std::min(EndX(), icon_ |
285 (icon_->bounds().right() + kIconToLabelSpacing) : kEdgeItemPadding); | 280 ? (icon_->bounds().right() + kIconToLabelSpacing) |
| 281 : kEdgeItemPadding); |
286 } | 282 } |
287 | 283 |
288 int InfoBarView::EndX() const { | 284 int InfoBarView::EndX() const { |
289 return close_button_->x() - kBeforeCloseButtonSpacing; | 285 return close_button_->x() - kBeforeCloseButtonSpacing; |
290 } | 286 } |
291 | 287 |
292 int InfoBarView::OffsetY(views::View* view) const { | 288 int InfoBarView::OffsetY(views::View* view) const { |
293 return arrow_height() + | 289 return arrow_height() + |
294 std::max((bar_target_height() - view->height()) / 2, 0) - | 290 std::max((bar_target_height() - view->height()) / 2, 0) - |
295 (bar_target_height() - bar_height()); | 291 (bar_target_height() - bar_height()); |
296 } | 292 } |
297 | 293 |
298 const infobars::InfoBarContainer::Delegate* InfoBarView::container_delegate() | 294 const infobars::InfoBarContainer::Delegate* InfoBarView::container_delegate() |
299 const { | 295 const { |
300 const infobars::InfoBarContainer* infobar_container = container(); | 296 const infobars::InfoBarContainer* infobar_container = container(); |
301 return infobar_container ? infobar_container->delegate() : NULL; | 297 return infobar_container ? infobar_container->delegate() : nullptr; |
302 } | 298 } |
303 | 299 |
304 void InfoBarView::RunMenuAt(ui::MenuModel* menu_model, | 300 void InfoBarView::RunMenuAt(ui::MenuModel* menu_model, |
305 views::MenuButton* button, | 301 views::MenuButton* button, |
306 views::MenuAnchorPosition anchor) { | 302 views::MenuAnchorPosition anchor) { |
307 DCHECK(owner()); // We'd better not open any menus while we're closing. | 303 DCHECK(owner()); // We'd better not open any menus while we're closing. |
308 gfx::Point screen_point; | 304 gfx::Point screen_point; |
309 views::View::ConvertPointToScreen(button, &screen_point); | 305 views::View::ConvertPointToScreen(button, &screen_point); |
310 menu_runner_.reset( | 306 menu_runner_.reset( |
311 new views::MenuRunner(menu_model, views::MenuRunner::HAS_MNEMONICS)); | 307 new views::MenuRunner(menu_model, views::MenuRunner::HAS_MNEMONICS)); |
(...skipping 29 matching lines...) Expand all Loading... |
341 | 337 |
342 void InfoBarView::PlatformSpecificHide(bool animate) { | 338 void InfoBarView::PlatformSpecificHide(bool animate) { |
343 // Cancel any menus we may have open. It doesn't make sense to leave them | 339 // Cancel any menus we may have open. It doesn't make sense to leave them |
344 // open while we're hidden, and if we're going to become unowned, we can't | 340 // open while we're hidden, and if we're going to become unowned, we can't |
345 // allow the user to choose any options and potentially call functions that | 341 // allow the user to choose any options and potentially call functions that |
346 // try to access the owner. | 342 // try to access the owner. |
347 menu_runner_.reset(); | 343 menu_runner_.reset(); |
348 | 344 |
349 // It's possible to be called twice (once with |animate| true and once with it | 345 // It's possible to be called twice (once with |animate| true and once with it |
350 // false); in this case the second SetFocusManager() call will silently no-op. | 346 // false); in this case the second SetFocusManager() call will silently no-op. |
351 SetFocusManager(NULL); | 347 SetFocusManager(nullptr); |
352 | 348 |
353 if (!animate) | 349 if (!animate) |
354 return; | 350 return; |
355 | 351 |
356 // Do not restore focus (and active state with it) if some other top-level | 352 // Do not restore focus (and active state with it) if some other top-level |
357 // window became active. | 353 // window became active. |
358 views::Widget* widget = GetWidget(); | 354 views::Widget* widget = GetWidget(); |
359 if (!widget || widget->IsActive()) | 355 if (!widget || widget->IsActive()) |
360 FocusLastFocusedExternalView(); | 356 FocusLastFocusedExternalView(); |
361 } | 357 } |
(...skipping 24 matching lines...) Expand all Loading... |
386 void InfoBarView::OnWillChangeFocus(View* focused_before, View* focused_now) { | 382 void InfoBarView::OnWillChangeFocus(View* focused_before, View* focused_now) { |
387 views::ExternalFocusTracker::OnWillChangeFocus(focused_before, focused_now); | 383 views::ExternalFocusTracker::OnWillChangeFocus(focused_before, focused_now); |
388 | 384 |
389 // This will trigger some screen readers to read the entire contents of this | 385 // This will trigger some screen readers to read the entire contents of this |
390 // infobar. | 386 // infobar. |
391 if (focused_before && focused_now && !Contains(focused_before) && | 387 if (focused_before && focused_now && !Contains(focused_before) && |
392 Contains(focused_now)) { | 388 Contains(focused_now)) { |
393 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); | 389 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); |
394 } | 390 } |
395 } | 391 } |
OLD | NEW |