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