OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/chromeos/login/username_view.h" | 5 #include "chrome/browser/chromeos/login/username_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/chromeos/login/rounded_view.h" | 9 #include "chrome/browser/chromeos/login/rounded_view.h" |
10 #include "grit/generated_resources.h" | 10 #include "grit/generated_resources.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 namespace chromeos { | 21 namespace chromeos { |
22 | 22 |
23 namespace { | 23 namespace { |
24 // Username label background color. | 24 // Username label background color. |
25 const SkColor kLabelBackgoundColor = 0x55000000; | 25 const SkColor kLabelBackgoundColor = 0x55000000; |
26 // Holds margin to height ratio. | 26 // Holds margin to height ratio. |
27 const double kMarginRatio = 1.0 / 3.0; | 27 const double kMarginRatio = 1.0 / 3.0; |
28 // Holds the frame width for the small shaped username view. | 28 // Holds the frame width for the small shaped username view. |
29 const SkScalar kSmallShapeFrameWidth = SkIntToScalar(1); | 29 const SkScalar kSmallShapeFrameWidth = SkIntToScalar(1); |
30 | 30 |
31 // Class that sets up half rounded rectangle (only the bottom corners are | |
32 // rounded) as a clip region of the view. | |
33 // For more info see the file "chrome/browser/chromeos/login/rounded_view.h". | |
34 template<typename C> | |
35 class HalfRoundedView : public RoundedView<C> { | |
36 public: | |
37 HalfRoundedView(const std::wstring &text, bool use_small_shape) | |
38 : RoundedView<C>(text, use_small_shape) { | |
39 } | |
40 | |
41 protected: | |
42 // Overrides ViewFilter. | |
43 virtual SkPath GetClipPath() const { | |
44 if (!C::use_small_shape()) { | |
45 return RoundedView<C>::GetClipPath(); | |
46 } else { | |
47 SkPath path; | |
48 gfx::Rect frame_bounds = this->bounds(); | |
49 frame_bounds.Inset(kSmallShapeFrameWidth, kSmallShapeFrameWidth, | |
50 kSmallShapeFrameWidth, kSmallShapeFrameWidth); | |
51 path.addRect(SkIntToScalar(frame_bounds.x()), | |
52 SkIntToScalar(frame_bounds.y()), | |
53 SkIntToScalar(frame_bounds.x() + frame_bounds.width()), | |
54 SkIntToScalar(frame_bounds.y() + frame_bounds.height())); | |
55 return path; | |
56 } | |
57 } | |
58 | |
59 virtual void DrawFrame(gfx::Canvas* canvas) { | |
60 // No frame is needed. | |
61 } | |
62 | |
63 virtual SkRect GetViewRect() const { | |
64 SkRect view_rect; | |
65 // The rectangle will be intersected with the bounds, so the correct half | |
66 // of the round rectangle will be obtained. | |
67 view_rect.iset(this->x(), | |
68 this->y() - this->height(), | |
69 this->x() + this->width(), | |
70 this->y() + this->height()); | |
71 return view_rect; | |
72 } | |
73 }; | |
74 | |
75 } // namespace | 31 } // namespace |
76 | 32 |
77 UsernameView::UsernameView(const std::wstring& username, bool use_small_shape) | 33 UsernameView::UsernameView(const std::wstring& username, bool use_small_shape) |
78 : views::Label(username.empty() | 34 : views::Label(username.empty() |
79 ? UTF16ToWide(l10n_util::GetStringUTF16(IDS_GUEST)) : username), | 35 ? UTF16ToWide(l10n_util::GetStringUTF16(IDS_GUEST)) : username), |
80 use_small_shape_(use_small_shape), | 36 use_small_shape_(use_small_shape), |
81 is_guest_(username.empty()) { | 37 is_guest_(username.empty()) { |
82 } | 38 } |
83 | 39 |
84 UsernameView::~UsernameView() {} | 40 UsernameView::~UsernameView() {} |
85 | 41 |
86 void UsernameView::OnPaint(gfx::Canvas* canvas) { | 42 void UsernameView::OnPaint(gfx::Canvas* canvas) { |
87 gfx::Rect bounds = GetContentsBounds(); | 43 gfx::Rect bounds = GetContentsBounds(); |
88 if (text_image_ == NULL) | 44 if (text_image_ == NULL) |
89 PaintUsername(bounds); | 45 PaintUsername(bounds); |
90 DCHECK(text_image_ != NULL); | 46 DCHECK(text_image_ != NULL); |
91 DCHECK(bounds.size() == | 47 DCHECK(bounds.size() == |
92 gfx::Size(text_image_->width(), text_image_->height())); | 48 gfx::Size(text_image_->width(), text_image_->height())); |
93 canvas->DrawBitmapInt(*text_image_, bounds.x(), bounds.y()); | 49 canvas->DrawBitmapInt(*text_image_, bounds.x(), bounds.y()); |
94 } | 50 } |
95 | 51 |
96 // static | 52 // static |
97 UsernameView* UsernameView::CreateShapedUsernameView( | 53 UsernameView* UsernameView::CreateShapedUsernameView( |
98 const std::wstring& username, bool use_small_shape) { | 54 const std::wstring& username, bool use_small_shape) { |
99 return new HalfRoundedView<UsernameView>(username, use_small_shape); | 55 return new UsernameView(username, use_small_shape); |
100 } | 56 } |
101 | 57 |
102 gfx::NativeCursor UsernameView::GetCursor(const views::MouseEvent& event) { | 58 gfx::NativeCursor UsernameView::GetCursor(const views::MouseEvent& event) { |
103 return use_small_shape_ ? gfx::GetCursor(GDK_HAND2) : NULL; | 59 return use_small_shape_ ? gfx::GetCursor(GDK_HAND2) : NULL; |
104 } | 60 } |
105 | 61 |
106 void UsernameView::PaintUsername(const gfx::Rect& bounds) { | 62 void UsernameView::PaintUsername(const gfx::Rect& bounds) { |
107 margin_width_ = bounds.height() * kMarginRatio; | 63 margin_width_ = bounds.height() * kMarginRatio; |
108 gfx::CanvasSkia canvas(bounds.width(), bounds.height(), false); | 64 gfx::CanvasSkia canvas(bounds.width(), bounds.height(), false); |
109 // Draw transparent background. | 65 // Draw transparent background. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 void UsernameView::OnLocaleChanged() { | 145 void UsernameView::OnLocaleChanged() { |
190 if (is_guest_) { | 146 if (is_guest_) { |
191 SetText(UTF16ToWide(l10n_util::GetStringUTF16(IDS_GUEST))); | 147 SetText(UTF16ToWide(l10n_util::GetStringUTF16(IDS_GUEST))); |
192 } | 148 } |
193 // Repaint because the font may have changed. | 149 // Repaint because the font may have changed. |
194 text_image_.reset(); | 150 text_image_.reset(); |
195 SchedulePaint(); | 151 SchedulePaint(); |
196 } | 152 } |
197 | 153 |
198 } // namespace chromeos | 154 } // namespace chromeos |
OLD | NEW |