| 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/location_bar/icon_label_bubble_view.h" | 5 #include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 8 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 9 #include "grit/theme_resources.h" | 9 #include "grit/theme_resources.h" |
| 10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
| 11 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
| 12 #include "ui/views/controls/image_view.h" | 12 #include "ui/views/controls/image_view.h" |
| 13 #include "ui/views/controls/label.h" | 13 #include "ui/views/controls/label.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Amount of padding at the edges of the bubble. | 17 // Amount of padding at the edges of the bubble. |
| 18 // | 18 // |
| 19 // This can't be statically initialized because | 19 // This can't be statically initialized because |
| 20 // LocationBarView::GetEdgeItemPadding() depends on whether we are | 20 // LocationBarView::GetEdgeItemPadding() depends on whether we are |
| 21 // using desktop or touch layout, and this in turn depends on the | 21 // using desktop or touch layout, and this in turn depends on the |
| 22 // command line. | 22 // command line. |
| 23 int GetBubbleOuterPadding() { | 23 int GetBubbleOuterPadding() { |
| 24 return LocationBarView::GetEdgeItemPadding() - | 24 return LocationBarView::GetEdgeItemPadding() - |
| 25 LocationBarView::kBubbleHorizontalPadding; | 25 LocationBarView::kBubbleHorizontalPadding; |
| 26 } | 26 } |
| 27 | 27 |
| 28 // Amount of padding after the label. | |
| 29 const int kLabelPadding = 5; | |
| 30 | |
| 31 } // namespace | 28 } // namespace |
| 32 | 29 |
| 33 IconLabelBubbleView::IconLabelBubbleView(const int background_images[], | 30 IconLabelBubbleView::IconLabelBubbleView(const int background_images[], |
| 34 int contained_image, | 31 int contained_image, |
| 35 SkColor color) | 32 SkColor color) |
| 36 : background_painter_(background_images), | 33 : background_painter_(background_images), |
| 37 is_extension_icon_(false) { | 34 is_extension_icon_(false) { |
| 38 image_ = new views::ImageView(); | 35 image_ = new views::ImageView(); |
| 39 image_->SetImage( | 36 if (contained_image > -1) { |
| 40 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 37 image_->SetImage( |
| 41 contained_image)); | 38 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 39 contained_image)); |
| 40 image_->SetVisible(true); |
| 41 } else { |
| 42 image_->SetVisible(false); |
| 43 } |
| 42 AddChildView(image_); | 44 AddChildView(image_); |
| 43 | 45 |
| 44 label_ = new views::Label(); | 46 label_ = new views::Label(); |
| 45 label_->SetAutoColorReadabilityEnabled(false); | 47 label_->SetAutoColorReadabilityEnabled(false); |
| 46 label_->SetEnabledColor(color); | 48 label_->SetEnabledColor(color); |
| 47 AddChildView(label_); | 49 AddChildView(label_); |
| 48 } | 50 } |
| 49 | 51 |
| 50 IconLabelBubbleView::~IconLabelBubbleView() { | 52 IconLabelBubbleView::~IconLabelBubbleView() { |
| 51 } | 53 } |
| 52 | 54 |
| 53 void IconLabelBubbleView::SetFont(const gfx::Font& font) { | 55 void IconLabelBubbleView::SetFont(const gfx::Font& font) { |
| 54 label_->SetFont(font); | 56 label_->SetFont(font); |
| 55 } | 57 } |
| 56 | 58 |
| 57 void IconLabelBubbleView::SetLabel(const string16& label) { | 59 void IconLabelBubbleView::SetLabel(const string16& label) { |
| 58 label_->SetText(label); | 60 label_->SetText(label); |
| 59 } | 61 } |
| 60 | 62 |
| 61 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { | 63 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { |
| 62 image_->SetImage(image_skia); | 64 image_->SetImage(image_skia); |
| 65 image_->SetVisible(true); |
| 63 } | 66 } |
| 64 | 67 |
| 65 void IconLabelBubbleView::SetLabelBackgroundColor(SkColor color) { | 68 void IconLabelBubbleView::SetLabelBackgroundColor(SkColor color) { |
| 66 label_->SetBackgroundColor(color); | 69 label_->SetBackgroundColor(color); |
| 67 } | 70 } |
| 68 | 71 |
| 69 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 72 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 70 background_painter_.Paint(canvas, size()); | 73 background_painter_.Paint(canvas, size()); |
| 71 } | 74 } |
| 72 | 75 |
| 73 gfx::Size IconLabelBubbleView::GetPreferredSize() { | 76 gfx::Size IconLabelBubbleView::GetPreferredSize() { |
| 74 gfx::Size size(GetNonLabelSize()); | 77 gfx::Size size(GetNonLabelSize()); |
| 75 size.Enlarge(label_->GetPreferredSize().width(), 0); | 78 size.Enlarge(label_->GetPreferredSize().width(), 0); |
| 76 return size; | 79 return size; |
| 77 } | 80 } |
| 78 | 81 |
| 79 void IconLabelBubbleView::Layout() { | 82 void IconLabelBubbleView::Layout() { |
| 80 image_->SetBounds(GetBubbleOuterPadding() + | 83 if (image_->visible()) { |
| 81 (is_extension_icon_ ? LocationBarView::kIconInternalPadding : 0), 0, | 84 image_->SetBounds(GetBubbleOuterPadding() + |
| 82 image_->GetPreferredSize().width(), height()); | 85 (is_extension_icon_ ? LocationBarView::kIconInternalPadding : 0), 0, |
| 86 image_->GetPreferredSize().width(), height()); |
| 87 } |
| 83 const int label_height = label_->GetPreferredSize().height(); | 88 const int label_height = label_->GetPreferredSize().height(); |
| 84 label_->SetBounds(GetPreLabelWidth(), (height() - label_height) / 2, | 89 label_->SetBounds(GetPreLabelWidth(), (height() - label_height) / 2, |
| 85 width() - GetNonLabelWidth(), label_height); | 90 width() - GetNonLabelWidth(), label_height); |
| 86 } | 91 } |
| 87 | 92 |
| 88 void IconLabelBubbleView::SetElideInMiddle(bool elide_in_middle) { | 93 void IconLabelBubbleView::SetElideInMiddle(bool elide_in_middle) { |
| 89 label_->SetElideBehavior( | 94 label_->SetElideBehavior( |
| 90 elide_in_middle ? views::Label::ELIDE_IN_MIDDLE : views::Label::NO_ELIDE); | 95 elide_in_middle ? views::Label::ELIDE_IN_MIDDLE : views::Label::NO_ELIDE); |
| 91 } | 96 } |
| 92 | 97 |
| 93 gfx::Size IconLabelBubbleView::GetNonLabelSize() const { | 98 gfx::Size IconLabelBubbleView::GetNonLabelSize() const { |
| 94 return gfx::Size(GetNonLabelWidth(), background_painter_.height()); | 99 return gfx::Size(GetNonLabelWidth(), background_painter_.height()); |
| 95 } | 100 } |
| 96 | 101 |
| 97 int IconLabelBubbleView::GetPreLabelWidth() const { | 102 int IconLabelBubbleView::GetPreLabelWidth() const { |
| 103 if (!image_->visible()) |
| 104 return GetBubbleOuterPadding(); |
| 98 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 105 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 99 return GetBubbleOuterPadding() + | 106 return GetBubbleOuterPadding() + |
| 100 rb.GetImageSkiaNamed(IDR_OMNIBOX_SEARCH)->width() + | 107 rb.GetImageSkiaNamed(IDR_OMNIBOX_SEARCH)->width() + |
| 101 LocationBarView::GetItemPadding(); | 108 LocationBarView::GetItemPadding(); |
| 102 } | 109 } |
| 103 | 110 |
| 104 int IconLabelBubbleView::GetNonLabelWidth() const { | 111 int IconLabelBubbleView::GetNonLabelWidth() const { |
| 105 return GetPreLabelWidth() + GetBubbleOuterPadding(); | 112 return GetPreLabelWidth() + GetBubbleOuterPadding(); |
| 106 } | 113 } |
| OLD | NEW |