| 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" | 9 #include "chrome/browser/ui/views/avatar_menu_bubble_view.h" |
| 10 #include "chrome/browser/ui/views/frame/browser_view.h" | 10 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 11 #include "grit/generated_resources.h" | 11 #include "grit/generated_resources.h" |
| 12 #include "grit/theme_resources.h" |
| 12 #include "ui/base/events/event.h" | 13 #include "ui/base/events/event.h" |
| 13 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 15 #include "ui/base/theme_provider.h" | 16 #include "ui/base/theme_provider.h" |
| 16 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/color_utils.h" | 18 #include "ui/gfx/color_utils.h" |
| 18 #include "ui/views/painter.h" | 19 #include "ui/views/painter.h" |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // A special text button border for the managed user avatar label. | 23 // A special text button border for the managed user avatar label. |
| 23 class AvatarLabelBorder: public views::TextButtonBorder { | 24 class AvatarLabelBorder: public views::TextButtonBorder { |
| 24 public: | 25 public: |
| 25 explicit AvatarLabelBorder(ui::ThemeProvider* theme_provider); | 26 explicit AvatarLabelBorder(ui::ThemeProvider* theme_provider); |
| 26 | 27 |
| 27 virtual void Paint(const views::View& view, gfx::Canvas* canvas) OVERRIDE; | 28 virtual void Paint(const views::View& view, gfx::Canvas* canvas) OVERRIDE; |
| 28 | 29 |
| 29 private: | 30 private: |
| 30 scoped_ptr<views::Painter> hot_painter_; | |
| 31 scoped_ptr<views::Painter> painter_; | 31 scoped_ptr<views::Painter> painter_; |
| 32 | 32 |
| 33 DISALLOW_COPY_AND_ASSIGN(AvatarLabelBorder); | 33 DISALLOW_COPY_AND_ASSIGN(AvatarLabelBorder); |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 AvatarLabelBorder::AvatarLabelBorder(ui::ThemeProvider* theme_provider) { | 36 AvatarLabelBorder::AvatarLabelBorder(ui::ThemeProvider* theme_provider) { |
| 37 const int kHorizontalInset = 10; | 37 const int kHorizontalInsetRight = 10; |
| 38 const int kVerticalInset = 2; | 38 const int kHorizontalInsetLeft = 43; |
| 39 SetInsets(gfx::Insets( | 39 const int kVerticalInsetTop = 3; |
| 40 kVerticalInset, kHorizontalInset, kVerticalInset, kHorizontalInset)); | 40 const int kVerticalInsetBottom = 3; |
| 41 SkColor color = theme_provider->GetColor( | 41 // We want to align with the top of the tab. This works if the BaseFont size |
| 42 ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND); | 42 // is 13. If it is smaller, we need to increase the TopInset accordingly. |
| 43 SkColor color2 = color_utils::BlendTowardOppositeLuminance(color, 0x20); | 43 gfx::Font font = ui::ResourceBundle::GetSharedInstance().GetFont( |
| 44 painter_.reset(views::Painter::CreateVerticalGradient(color, color2)); | 44 ui::ResourceBundle::BaseFont); |
| 45 hot_painter_.reset(views::Painter::CreateVerticalGradient(color2, color)); | 45 int difference = (font.GetFontSize() < 13) ? 13 - font.GetFontSize() : 0; |
| 46 int addToTop = difference / 2; |
| 47 int addToBottom = difference - addToTop; |
| 48 SetInsets(gfx::Insets(kVerticalInsetTop + addToTop, |
| 49 kHorizontalInsetLeft, |
| 50 kVerticalInsetBottom + addToBottom, |
| 51 kHorizontalInsetRight)); |
| 52 const int kImages[] = IMAGE_GRID(IDR_MANAGED_USER_LABEL); |
| 53 painter_.reset(views::Painter::CreateImageGridPainter(kImages)); |
| 46 } | 54 } |
| 47 | 55 |
| 48 void AvatarLabelBorder::Paint(const views::View& view, gfx::Canvas* canvas) { | 56 void AvatarLabelBorder::Paint(const views::View& view, gfx::Canvas* canvas) { |
| 49 const views::TextButton* button = | 57 painter_->Paint(canvas, view.size()); |
| 50 static_cast<const views::TextButton*>(&view); | |
| 51 if (button->state() == views::TextButton::STATE_HOVERED || | |
| 52 button->state() == views::TextButton::STATE_PRESSED) | |
| 53 hot_painter_->Paint(canvas, view.size()); | |
| 54 else | |
| 55 painter_->Paint(canvas, view.size()); | |
| 56 } | 58 } |
| 57 | 59 |
| 58 } // namespace | 60 } // namespace |
| 59 | 61 |
| 60 AvatarLabel::AvatarLabel(BrowserView* browser_view, | 62 AvatarLabel::AvatarLabel(BrowserView* browser_view, |
| 61 ui::ThemeProvider* theme_provider) | 63 ui::ThemeProvider* theme_provider) |
| 62 : TextButton(NULL, | 64 : TextButton(NULL, |
| 63 l10n_util::GetStringUTF16(IDS_MANAGED_USER_AVATAR_LABEL)), | 65 l10n_util::GetStringUTF16(IDS_MANAGED_USER_AVATAR_LABEL)), |
| 64 browser_view_(browser_view), | 66 browser_view_(browser_view), |
| 65 theme_provider_(theme_provider) { | 67 theme_provider_(theme_provider) { |
| 66 SetFont(ui::ResourceBundle::GetSharedInstance().GetFont( | 68 SetFont(ui::ResourceBundle::GetSharedInstance().GetFont( |
| 67 ui::ResourceBundle::SmallFont)); | 69 ui::ResourceBundle::BaseFont)); |
| 68 ClearMaxTextSize(); | 70 ClearMaxTextSize(); |
| 69 set_border(new AvatarLabelBorder(theme_provider)); | 71 set_border(new AvatarLabelBorder(theme_provider)); |
| 70 UpdateLabelStyle(); | 72 UpdateLabelStyle(); |
| 71 } | 73 } |
| 72 | 74 |
| 73 AvatarLabel::~AvatarLabel() {} | 75 AvatarLabel::~AvatarLabel() {} |
| 74 | 76 |
| 75 bool AvatarLabel::OnMousePressed(const ui::MouseEvent& event) { | 77 bool AvatarLabel::OnMousePressed(const ui::MouseEvent& event) { |
| 76 if (!TextButton::OnMousePressed(event)) | 78 if (!TextButton::OnMousePressed(event)) |
| 77 return false; | 79 return false; |
| 78 | 80 |
| 79 browser_view_->ShowAvatarBubbleFromAvatarButton(); | 81 browser_view_->ShowAvatarBubbleFromAvatarButton(); |
| 80 return true; | 82 return true; |
| 81 } | 83 } |
| 82 | 84 |
| 83 void AvatarLabel::UpdateLabelStyle() { | 85 void AvatarLabel::UpdateLabelStyle() { |
| 84 SkColor color_label = | 86 SkColor color_label = |
| 85 theme_provider_->GetColor(ThemeProperties::COLOR_MANAGED_USER_LABEL); | 87 theme_provider_->GetColor(ThemeProperties::COLOR_MANAGED_USER_LABEL); |
| 86 SetEnabledColor(color_label); | 88 SetEnabledColor(color_label); |
| 87 SetHighlightColor(color_label); | 89 SetHighlightColor(color_label); |
| 88 SetHoverColor(color_label); | 90 SetHoverColor(color_label); |
| 89 SetDisabledColor(color_label); | 91 SetDisabledColor(color_label); |
| 90 SchedulePaint(); | 92 SchedulePaint(); |
| 91 } | 93 } |
| OLD | NEW |