| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/avatar_label.h" | 5 #include "chrome/browser/ui/views/avatar_label.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/themes/theme_properties.h" | 8 #include "chrome/browser/themes/theme_properties.h" |
| 9 #include "chrome/browser/ui/views/avatar_menu_bubble_view.h" | |
| 10 #include "chrome/browser/ui/views/frame/browser_view.h" | 9 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 11 #include "grit/generated_resources.h" | 10 #include "grit/generated_resources.h" |
| 12 #include "grit/theme_resources.h" | 11 #include "grit/theme_resources.h" |
| 13 #include "ui/base/l10n/l10n_util.h" | 12 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/theme_provider.h" | 13 #include "ui/base/theme_provider.h" |
| 15 #include "ui/events/event.h" | |
| 16 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/color_utils.h" | 15 #include "ui/gfx/color_utils.h" |
| 18 #include "ui/gfx/font_list.h" | |
| 19 #include "ui/views/painter.h" | 16 #include "ui/views/painter.h" |
| 20 | 17 |
| 21 namespace { | 18 namespace { |
| 22 | 19 |
| 23 // A special text button border for the managed user avatar label. | 20 // A custom border for the managed user avatar label. |
| 24 class AvatarLabelBorder: public views::TextButtonBorder { | 21 class AvatarLabelBorder : public views::Border { |
| 25 public: | 22 public: |
| 26 explicit AvatarLabelBorder(bool label_on_right); | 23 explicit AvatarLabelBorder(bool label_on_right); |
| 27 | 24 |
| 28 // views::TextButtonBorder: | 25 // views::Border: |
| 29 virtual void Paint(const views::View& view, gfx::Canvas* canvas) OVERRIDE; | 26 virtual void Paint(const views::View& view, gfx::Canvas* canvas) OVERRIDE; |
| 27 virtual gfx::Insets GetInsets() const OVERRIDE; |
| 30 virtual gfx::Size GetMinimumSize() const OVERRIDE; | 28 virtual gfx::Size GetMinimumSize() const OVERRIDE; |
| 31 | 29 |
| 32 private: | 30 private: |
| 33 scoped_ptr<views::Painter> painter_; | 31 scoped_ptr<views::Painter> painter_; |
| 32 gfx::Insets insets_; |
| 34 | 33 |
| 35 DISALLOW_COPY_AND_ASSIGN(AvatarLabelBorder); | 34 DISALLOW_COPY_AND_ASSIGN(AvatarLabelBorder); |
| 36 }; | 35 }; |
| 37 | 36 |
| 38 AvatarLabelBorder::AvatarLabelBorder(bool label_on_right) { | 37 AvatarLabelBorder::AvatarLabelBorder(bool label_on_right) { |
| 39 const int kHorizontalInsetRight = label_on_right ? 43 : 10; | 38 const int kHorizontalInsetRight = label_on_right ? 43 : 10; |
| 40 const int kHorizontalInsetLeft = label_on_right ? 10 : 43; | 39 const int kHorizontalInsetLeft = label_on_right ? 10 : 43; |
| 41 const int kVerticalInsetTop = 2; | 40 const int kVerticalInsetTop = 2; |
| 42 const int kVerticalInsetBottom = 3; | 41 const int kVerticalInsetBottom = 3; |
| 43 // We want to align with the top of the tab. This works if the default font | 42 // We want to align with the top of the tab. This works if the default font |
| 44 // size is 13. If it is smaller, we need to increase the TopInset accordingly. | 43 // size is 13. If it is smaller, we need to increase the TopInset accordingly. |
| 45 const gfx::FontList font_list; | 44 const int difference = std::max<int>(0, 13 - gfx::FontList().GetFontSize()); |
| 46 int difference = | 45 const int addToTop = difference / 2; |
| 47 (font_list.GetFontSize() < 13) ? 13 - font_list.GetFontSize() : 0; | 46 const int addToBottom = difference - addToTop; |
| 48 int addToTop = difference / 2; | 47 insets_ = gfx::Insets(kVerticalInsetTop + addToTop, |
| 49 int addToBottom = difference - addToTop; | |
| 50 SetInsets(gfx::Insets(kVerticalInsetTop + addToTop, | |
| 51 kHorizontalInsetLeft, | 48 kHorizontalInsetLeft, |
| 52 kVerticalInsetBottom + addToBottom, | 49 kVerticalInsetBottom + addToBottom, |
| 53 kHorizontalInsetRight)); | 50 kHorizontalInsetRight); |
| 54 const int kImages[] = IMAGE_GRID(IDR_MANAGED_USER_LABEL); | 51 const int kImages[] = IMAGE_GRID(IDR_MANAGED_USER_LABEL); |
| 55 painter_.reset(views::Painter::CreateImageGridPainter(kImages)); | 52 painter_.reset(views::Painter::CreateImageGridPainter(kImages)); |
| 56 } | 53 } |
| 57 | 54 |
| 58 void AvatarLabelBorder::Paint(const views::View& view, gfx::Canvas* canvas) { | 55 void AvatarLabelBorder::Paint(const views::View& view, gfx::Canvas* canvas) { |
| 59 // Paint the default background using the image assets provided by UI. This | 56 // Paint the default background using the image assets provided by UI. This |
| 60 // includes a border with almost transparent white color. | 57 // includes a border with almost transparent white color. |
| 61 painter_->Paint(canvas, view.size()); | 58 painter_->Paint(canvas, view.size()); |
| 62 | 59 |
| 63 // Now repaint the inner part of the background in order to be able to change | 60 // Repaint the inner part of the background in order to be able to change |
| 64 // the colors according to the currently installed theme. | 61 // the colors according to the currently installed theme. |
| 65 gfx::Rect rect(1, 1, view.size().width() - 2, view.size().height() - 2); | 62 gfx::Rect rect(1, 1, view.size().width() - 2, view.size().height() - 2); |
| 66 SkPaint paint; | 63 SkPaint paint; |
| 67 int kRadius = 2; | 64 int kRadius = 2; |
| 68 SkColor background_color = view.GetThemeProvider()->GetColor( | 65 SkColor background_color = view.GetThemeProvider()->GetColor( |
| 69 ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND); | 66 ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND); |
| 70 paint.setStyle(SkPaint::kFill_Style); | 67 paint.setStyle(SkPaint::kFill_Style); |
| 71 | 68 |
| 72 // For the inner border, use a color which is slightly darker than the | 69 // Paint the inner border with a color slightly darker than the background. |
| 73 // background color. | |
| 74 SkAlpha kAlphaForBlending = 230; | 70 SkAlpha kAlphaForBlending = 230; |
| 75 paint.setColor(color_utils::AlphaBlend( | 71 paint.setColor(color_utils::AlphaBlend( |
| 76 background_color, SK_ColorBLACK, kAlphaForBlending)); | 72 background_color, SK_ColorBLACK, kAlphaForBlending)); |
| 77 canvas->DrawRoundRect(rect, kRadius, paint); | 73 canvas->DrawRoundRect(rect, kRadius, paint); |
| 78 | 74 |
| 79 // Now paint the inner background using the color provided by the | 75 // Paint the inner background using the color provided by the ThemeProvider. |
| 80 // ThemeProvider. | |
| 81 paint.setColor(background_color); | 76 paint.setColor(background_color); |
| 82 rect = gfx::Rect(2, 2, view.size().width() - 4, view.size().height() - 4); | 77 rect = gfx::Rect(2, 2, view.size().width() - 4, view.size().height() - 4); |
| 83 canvas->DrawRoundRect(rect, kRadius, paint); | 78 canvas->DrawRoundRect(rect, kRadius, paint); |
| 84 } | 79 } |
| 85 | 80 |
| 81 gfx::Insets AvatarLabelBorder::GetInsets() const { |
| 82 return insets_; |
| 83 } |
| 84 |
| 86 gfx::Size AvatarLabelBorder::GetMinimumSize() const { | 85 gfx::Size AvatarLabelBorder::GetMinimumSize() const { |
| 87 gfx::Size size(4, 4); | 86 gfx::Size size(4, 4); |
| 88 size.SetToMax(painter_->GetMinimumSize()); | 87 size.SetToMax(painter_->GetMinimumSize()); |
| 89 return size; | 88 return size; |
| 90 } | 89 } |
| 91 | 90 |
| 92 } // namespace | 91 } // namespace |
| 93 | 92 |
| 94 AvatarLabel::AvatarLabel(BrowserView* browser_view) | 93 AvatarLabel::AvatarLabel(BrowserView* browser_view) |
| 95 : TextButton(NULL, | 94 : LabelButton(NULL, |
| 96 l10n_util::GetStringUTF16(IDS_MANAGED_USER_AVATAR_LABEL)), | 95 l10n_util::GetStringUTF16(IDS_MANAGED_USER_AVATAR_LABEL)), |
| 97 browser_view_(browser_view) { | 96 browser_view_(browser_view) { |
| 98 ClearMaxTextSize(); | |
| 99 SetLabelOnRight(false); | 97 SetLabelOnRight(false); |
| 100 UpdateLabelStyle(); | 98 UpdateLabelStyle(); |
| 101 } | 99 } |
| 102 | 100 |
| 103 AvatarLabel::~AvatarLabel() {} | 101 AvatarLabel::~AvatarLabel() {} |
| 104 | 102 |
| 105 bool AvatarLabel::OnMousePressed(const ui::MouseEvent& event) { | 103 bool AvatarLabel::OnMousePressed(const ui::MouseEvent& event) { |
| 106 if (!TextButton::OnMousePressed(event)) | 104 if (!LabelButton::OnMousePressed(event)) |
| 107 return false; | 105 return false; |
| 108 | 106 |
| 109 browser_view_->ShowAvatarBubbleFromAvatarButton(); | 107 browser_view_->ShowAvatarBubbleFromAvatarButton(); |
| 110 return true; | 108 return true; |
| 111 } | 109 } |
| 112 | 110 |
| 113 void AvatarLabel::UpdateLabelStyle() { | 111 void AvatarLabel::UpdateLabelStyle() { |
| 114 // |browser_view_| can be NULL in unit tests. | 112 // |browser_view_| can be NULL in unit tests. |
| 115 if (!browser_view_) | 113 if (!browser_view_) |
| 116 return; | 114 return; |
| 117 | 115 |
| 118 SkColor color_label = browser_view_->frame()->GetThemeProvider()->GetColor( | 116 SkColor color_label = browser_view_->frame()->GetThemeProvider()->GetColor( |
| 119 ThemeProperties::COLOR_MANAGED_USER_LABEL); | 117 ThemeProperties::COLOR_MANAGED_USER_LABEL); |
| 120 SetEnabledColor(color_label); | 118 for (size_t state = 0; state < STATE_COUNT; ++state) |
| 121 SetHighlightColor(color_label); | 119 SetTextColor(static_cast<ButtonState>(state), color_label); |
| 122 SetHoverColor(color_label); | |
| 123 SetDisabledColor(color_label); | |
| 124 SchedulePaint(); | 120 SchedulePaint(); |
| 125 } | 121 } |
| 126 | 122 |
| 127 void AvatarLabel::SetLabelOnRight(bool label_on_right) { | 123 void AvatarLabel::SetLabelOnRight(bool label_on_right) { |
| 128 SetBorder(scoped_ptr<views::Border>(new AvatarLabelBorder(label_on_right))); | 124 SetBorder(scoped_ptr<views::Border>(new AvatarLabelBorder(label_on_right))); |
| 129 } | 125 } |
| OLD | NEW |