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