OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/infobars/one_click_signin_infobar.h" |
| 6 |
| 7 #include "chrome/browser/api/infobars/one_click_signin_infobar_delegate.h" |
| 8 #include "chrome/browser/defaults.h" |
| 9 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 10 #include "chrome/browser/ui/views/infobars/infobar_background.h" |
| 11 #include "third_party/skia/include/core/SkColor.h" |
| 12 #include "third_party/skia/include/core/SkPaint.h" |
| 13 #include "third_party/skia/include/core/SkRect.h" |
| 14 #include "third_party/skia/include/core/SkScalar.h" |
| 15 #include "ui/gfx/canvas.h" |
| 16 #include "ui/gfx/skia_util.h" |
| 17 #include "ui/views/border.h" |
| 18 #include "ui/views/controls/button/custom_button.h" |
| 19 #include "ui/views/controls/button/text_button.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Preferred padding between text and edge. |
| 24 const int kPreferredPaddingHorizontal = 6; |
| 25 |
| 26 // A border used for infobar buttons using a custom colour. |
| 27 class InfoBarColoredButtonBorder : public views::Border { |
| 28 public: |
| 29 InfoBarColoredButtonBorder(SkColor background_color, |
| 30 SkColor border_color); |
| 31 |
| 32 private: |
| 33 // A helper function to easily perform colour darkening from the |
| 34 // constructor initializer list. |
| 35 static SkColor DarkenColor(SkColor color); |
| 36 |
| 37 // Border overrides: |
| 38 virtual void Paint(const views::View& view, |
| 39 gfx::Canvas* canvas) const OVERRIDE; |
| 40 virtual void GetInsets(gfx::Insets* insets) const OVERRIDE; |
| 41 |
| 42 virtual ~InfoBarColoredButtonBorder(); |
| 43 |
| 44 const SkColor background_color_; |
| 45 const SkColor border_color_; |
| 46 const SkColor border_color_hot_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(InfoBarColoredButtonBorder); |
| 49 }; |
| 50 |
| 51 InfoBarColoredButtonBorder::InfoBarColoredButtonBorder( |
| 52 SkColor background_color, |
| 53 SkColor border_color) |
| 54 : background_color_(background_color), |
| 55 border_color_(border_color), |
| 56 border_color_hot_(DarkenColor(border_color_)) { |
| 57 } |
| 58 |
| 59 // static |
| 60 SkColor InfoBarColoredButtonBorder::DarkenColor(SkColor color) { |
| 61 SkScalar hsv[3]; |
| 62 SkColorToHSV(color, hsv); |
| 63 hsv[2] *= 0.8f; |
| 64 return SkHSVToColor(255, hsv); |
| 65 } |
| 66 |
| 67 void InfoBarColoredButtonBorder::Paint(const views::View& view, |
| 68 gfx::Canvas* canvas) const { |
| 69 const views::CustomButton* button = |
| 70 static_cast<const views::CustomButton*>(&view); |
| 71 const views::CustomButton::ButtonState state = button->state(); |
| 72 |
| 73 const SkScalar kRadius = 2.0; |
| 74 |
| 75 SkRect bounds(gfx::RectToSkRect(view.GetLocalBounds())); |
| 76 bounds.inset(0.5, 0.5); |
| 77 |
| 78 SkPaint paint; |
| 79 paint.setAntiAlias(true); |
| 80 |
| 81 paint.setStyle(SkPaint::kFill_Style); |
| 82 paint.setColor(background_color_); |
| 83 canvas->sk_canvas()->drawRoundRect(bounds, kRadius, kRadius, paint); |
| 84 |
| 85 paint.setStyle(SkPaint::kStroke_Style); |
| 86 paint.setColor(state == views::CustomButton::BS_NORMAL ? |
| 87 border_color_ : border_color_hot_); |
| 88 canvas->sk_canvas()->drawRoundRect(bounds, kRadius, kRadius, paint); |
| 89 } |
| 90 |
| 91 void InfoBarColoredButtonBorder::GetInsets(gfx::Insets* insets) const { |
| 92 insets->Set(browser_defaults::kInfoBarBorderPaddingVertical, |
| 93 kPreferredPaddingHorizontal, |
| 94 browser_defaults::kInfoBarBorderPaddingVertical, |
| 95 kPreferredPaddingHorizontal); |
| 96 } |
| 97 |
| 98 InfoBarColoredButtonBorder::~InfoBarColoredButtonBorder() { |
| 99 } |
| 100 |
| 101 } // namespace |
| 102 |
| 103 |
| 104 // OneClickSigninInfoBarDelegate ---------------------------------------------- |
| 105 |
| 106 InfoBar* OneClickSigninInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { |
| 107 return new OneClickSigninInfoBar(static_cast<InfoBarTabHelper*>(owner), this); |
| 108 } |
| 109 |
| 110 |
| 111 // OneClickLoginInfoBar ------------------------------------------------------- |
| 112 |
| 113 OneClickSigninInfoBar::OneClickSigninInfoBar( |
| 114 InfoBarTabHelper* owner, |
| 115 OneClickSigninInfoBarDelegate* delegate) |
| 116 : ConfirmInfoBar(owner, delegate), |
| 117 one_click_delegate_(delegate) { |
| 118 CHECK(one_click_delegate_); |
| 119 } |
| 120 |
| 121 OneClickSigninInfoBar::~OneClickSigninInfoBar() { |
| 122 } |
| 123 |
| 124 void OneClickSigninInfoBar::ViewHierarchyChanged(bool is_add, |
| 125 views::View* parent, |
| 126 views::View* child) { |
| 127 const bool fix_color = is_add && child == this && ok_button() == NULL; |
| 128 |
| 129 ConfirmInfoBar::ViewHierarchyChanged(is_add, parent, child); |
| 130 |
| 131 if (!fix_color || ok_button() == NULL) |
| 132 return; |
| 133 |
| 134 OneClickSigninInfoBarDelegate::AlternateColors alt_colors; |
| 135 one_click_delegate_->GetAlternateColors(&alt_colors); |
| 136 if (!alt_colors.enabled) |
| 137 return; |
| 138 |
| 139 set_background(new InfoBarBackground(alt_colors.infobar_top_color, |
| 140 alt_colors.infobar_bottom_color)); |
| 141 ok_button()->set_border(new InfoBarColoredButtonBorder( |
| 142 alt_colors.button_background_color, |
| 143 alt_colors.button_border_color)); |
| 144 ok_button()->SetEnabledColor(alt_colors.button_text_color); |
| 145 ok_button()->SetHighlightColor(alt_colors.button_text_color); |
| 146 ok_button()->SetHoverColor(alt_colors.button_text_color); |
| 147 } |
OLD | NEW |