| 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/ui/infobar_container_delegate.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, NULL), |
| 72 icon_(NULL), | 61 icon_(NULL), |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(); |
| 152 const infobars::InfoBarContainer::Delegate* delegate = container_delegate(); | 141 const infobars::InfoBarContainer::Delegate* delegate = container_delegate(); |
| 153 if (delegate) { | 142 if (delegate) { |
| 154 static_cast<InfoBarBackground*>(background())->set_separator_color( | 143 static_cast<InfoBarBackground*>(background())->set_separator_color( |
| 155 delegate->GetInfoBarSeparatorColor()); | 144 delegate->GetInfoBarSeparatorColor()); |
| 156 int arrow_x; | 145 int arrow_x; |
| 157 SkScalar arrow_fill_height = | 146 SkScalar arrow_fill_height = SkIntToScalar(std::max( |
| 158 SkIntToScalar(std::max(arrow_height() - kSeparatorLineHeight, 0)); | 147 arrow_height() - InfoBarContainerDelegate::kSeparatorLineHeight, 0)); |
| 159 SkScalar arrow_fill_half_width = SkIntToScalar(arrow_half_width()); | 148 SkScalar arrow_fill_half_width = SkIntToScalar(arrow_half_width()); |
| 160 SkScalar separator_height = SkIntToScalar(kSeparatorLineHeight); | 149 SkScalar separator_height = |
| 150 SkIntToScalar(InfoBarContainerDelegate::kSeparatorLineHeight); |
| 161 if (delegate->DrawInfoBarArrows(&arrow_x) && arrow_fill_height) { | 151 if (delegate->DrawInfoBarArrows(&arrow_x) && arrow_fill_height) { |
| 162 // Skia pixel centers are at the half-values, so the arrow is horizontally | 152 // 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 | 153 // 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 | 154 // 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 | 155 // through the entire height of the separator and down to the bottom of |
| 166 // the arrow where it joins the bar. | 156 // the arrow where it joins the bar. |
| 167 stroke_path_.moveTo( | 157 stroke_path_.moveTo( |
| 168 SkIntToScalar(arrow_x) + SK_ScalarHalf - arrow_fill_half_width, | 158 SkIntToScalar(arrow_x) + SK_ScalarHalf - arrow_fill_half_width, |
| 169 SkIntToScalar(arrow_height()) - (separator_height * SK_ScalarHalf)); | 159 SkIntToScalar(arrow_height()) - (separator_height * SK_ScalarHalf)); |
| 170 stroke_path_.rLineTo(arrow_fill_half_width, -arrow_fill_height); | 160 stroke_path_.rLineTo(arrow_fill_half_width, -arrow_fill_height); |
| 171 stroke_path_.rLineTo(arrow_fill_half_width, arrow_fill_height); | 161 stroke_path_.rLineTo(arrow_fill_half_width, arrow_fill_height); |
| 172 | 162 |
| 173 fill_path_ = stroke_path_; | 163 fill_path_ = stroke_path_; |
| 174 // Move the top of the fill path up to the top of the separator and then | 164 // Move the top of the fill path up to the top of the separator and then |
| 175 // extend it down all the way through. | 165 // extend it down all the way through. |
| 176 fill_path_.offset(0, -separator_height * SK_ScalarHalf); | 166 fill_path_.offset(0, -separator_height * SK_ScalarHalf); |
| 177 // This 0.01 hack prevents the fill from filling more pixels on the right | 167 // This 0.01 hack prevents the fill from filling more pixels on the right |
| 178 // edge of the arrow than on the left. | 168 // edge of the arrow than on the left. |
| 179 const SkScalar epsilon = 0.01f; | 169 const SkScalar epsilon = 0.01f; |
| 180 fill_path_.rLineTo(-epsilon, 0); | 170 fill_path_.rLineTo(-epsilon, 0); |
| 181 fill_path_.rLineTo(0, separator_height); | 171 fill_path_.rLineTo(0, separator_height); |
| 182 fill_path_.rLineTo(epsilon - (arrow_fill_half_width * 2), 0); | 172 fill_path_.rLineTo(epsilon - (arrow_fill_half_width * 2), 0); |
| 183 fill_path_.close(); | 173 fill_path_.close(); |
| 184 } | 174 } |
| 185 } | 175 } |
| 186 if (bar_height()) { | 176 if (bar_height()) { |
| 187 fill_path_.addRect(0.0, SkIntToScalar(arrow_height()), | 177 fill_path_.addRect( |
| 188 SkIntToScalar(width()), SkIntToScalar(height() - kSeparatorLineHeight)); | 178 0.0, SkIntToScalar(arrow_height()), SkIntToScalar(width()), |
| 179 SkIntToScalar( |
| 180 height() - InfoBarContainerDelegate::kSeparatorLineHeight)); |
| 189 } | 181 } |
| 190 | 182 |
| 191 int start_x = kEdgeItemPadding; | 183 int start_x = kEdgeItemPadding; |
| 192 if (icon_ != NULL) { | 184 if (icon_ != NULL) { |
| 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( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } else if ((close_button_ != NULL) && (details.parent == this) && | 224 } else if ((close_button_ != NULL) && (details.parent == this) && |
| 233 (details.child != close_button_) && (close_button_->parent() == this) && | 225 (details.child != close_button_) && (close_button_->parent() == this) && |
| 234 (child_at(child_count() - 1) != close_button_)) { | 226 (child_at(child_count() - 1) != close_button_)) { |
| 235 // For accessibility, ensure the close button is the last child view. | 227 // For accessibility, ensure the close button is the last child view. |
| 236 RemoveChildView(close_button_); | 228 RemoveChildView(close_button_); |
| 237 AddChildView(close_button_); | 229 AddChildView(close_button_); |
| 238 } | 230 } |
| 239 | 231 |
| 240 // Ensure the infobar is tall enough to display its contents. | 232 // Ensure the infobar is tall enough to display its contents. |
| 241 const int kMinimumVerticalPadding = 6; | 233 const int kMinimumVerticalPadding = 6; |
| 242 int height = kDefaultBarTargetHeight; | 234 int height = InfoBarContainerDelegate::kDefaultBarTargetHeight; |
| 243 for (int i = 0; i < child_count(); ++i) { | 235 for (int i = 0; i < child_count(); ++i) { |
| 244 const int child_height = child_at(i)->height(); | 236 const int child_height = child_at(i)->height(); |
| 245 height = std::max(height, child_height + kMinimumVerticalPadding); | 237 height = std::max(height, child_height + kMinimumVerticalPadding); |
| 246 } | 238 } |
| 247 SetBarTargetHeight(height); | 239 SetBarTargetHeight(height); |
| 248 } | 240 } |
| 249 | 241 |
| 250 void InfoBarView::PaintChildren(gfx::Canvas* canvas, | 242 void InfoBarView::PaintChildren(gfx::Canvas* canvas, |
| 251 const views::CullSet& cull_set) { | 243 const views::CullSet& cull_set) { |
| 252 canvas->Save(); | 244 canvas->Save(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 void InfoBarView::OnWillChangeFocus(View* focused_before, View* focused_now) { | 378 void InfoBarView::OnWillChangeFocus(View* focused_before, View* focused_now) { |
| 387 views::ExternalFocusTracker::OnWillChangeFocus(focused_before, focused_now); | 379 views::ExternalFocusTracker::OnWillChangeFocus(focused_before, focused_now); |
| 388 | 380 |
| 389 // This will trigger some screen readers to read the entire contents of this | 381 // This will trigger some screen readers to read the entire contents of this |
| 390 // infobar. | 382 // infobar. |
| 391 if (focused_before && focused_now && !Contains(focused_before) && | 383 if (focused_before && focused_now && !Contains(focused_before) && |
| 392 Contains(focused_now)) { | 384 Contains(focused_now)) { |
| 393 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); | 385 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); |
| 394 } | 386 } |
| 395 } | 387 } |
| OLD | NEW |