OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ui/views/controls/button/md_text_button.h" | 5 #include "ui/views/controls/button/md_text_button.h" |
6 | 6 |
7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
8 #include "ui/gfx/render_text.h" | 8 #include "ui/gfx/render_text.h" |
9 #include "ui/native_theme/native_theme.h" | 9 #include "ui/native_theme/native_theme.h" |
| 10 #include "ui/views/style/platform_style.h" |
10 | 11 |
11 namespace views { | 12 namespace views { |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // Inset between clickable region border and button contents (text). | 16 // Inset between clickable region border and button contents (text). |
16 const int kHorizontalPadding = 12; | 17 const int kHorizontalPadding = 12; |
17 const int kVerticalPadding = 6; | 18 const int kVerticalPadding = 6; |
18 | 19 |
19 // Minimum size to reserve for the button contents. | 20 // Minimum size to reserve for the button contents. |
20 const int kMinWidth = 48; | 21 const int kMinWidth = 48; |
21 | 22 |
22 const gfx::FontList& GetFontList() { | 23 const gfx::FontList& GetFontList() { |
23 static base::LazyInstance<gfx::FontList>::Leaky font_list = | 24 static base::LazyInstance<gfx::FontList>::Leaky font_list = |
24 LAZY_INSTANCE_INITIALIZER; | 25 LAZY_INSTANCE_INITIALIZER; |
25 return font_list.Get(); | 26 return font_list.Get(); |
26 } | 27 } |
27 | 28 |
28 } // namespace | 29 } // namespace |
29 | 30 |
30 MdTextButton::MdTextButton(ButtonListener* listener, const base::string16& text) | 31 MdTextButton::MdTextButton(ButtonListener* listener, const base::string16& text) |
31 : CustomButton(listener), | 32 : CustomButton(listener), |
32 render_text_(gfx::RenderText::CreateInstance()) { | 33 render_text_(gfx::RenderText::CreateInstance()) { |
33 render_text_->SetFontList(GetFontList()); | 34 render_text_->SetFontList(GetFontList()); |
34 render_text_->SetCursorEnabled(false); | 35 render_text_->SetCursorEnabled(false); |
35 render_text_->SetText(base::i18n::ToUpper(text)); | 36 render_text_->SetText(base::i18n::ToUpper(text)); |
36 | 37 |
37 SetFocusable(true); | 38 PlatformStyle::SetControlStyleFocus(this); |
38 } | 39 } |
39 | 40 |
40 MdTextButton::~MdTextButton() {} | 41 MdTextButton::~MdTextButton() {} |
41 | 42 |
42 void MdTextButton::OnPaint(gfx::Canvas* canvas) { | 43 void MdTextButton::OnPaint(gfx::Canvas* canvas) { |
43 UpdateColor(); | 44 UpdateColor(); |
44 gfx::Rect rect = GetLocalBounds(); | 45 gfx::Rect rect = GetLocalBounds(); |
45 rect.Inset(kHorizontalPadding, kVerticalPadding); | 46 rect.Inset(kHorizontalPadding, kVerticalPadding); |
46 render_text_->SetDisplayRect(rect); | 47 render_text_->SetDisplayRect(rect); |
47 render_text_->Draw(canvas); | 48 render_text_->Draw(canvas); |
48 } | 49 } |
49 | 50 |
50 gfx::Size MdTextButton::GetPreferredSize() const { | 51 gfx::Size MdTextButton::GetPreferredSize() const { |
51 gfx::Size size = render_text_->GetStringSize(); | 52 gfx::Size size = render_text_->GetStringSize(); |
52 size.SetToMax(gfx::Size(kMinWidth, 0)); | 53 size.SetToMax(gfx::Size(kMinWidth, 0)); |
53 size.Enlarge(kHorizontalPadding * 2, kVerticalPadding * 2); | 54 size.Enlarge(kHorizontalPadding * 2, kVerticalPadding * 2); |
54 return size; | 55 return size; |
55 } | 56 } |
56 | 57 |
57 void MdTextButton::UpdateColor() { | 58 void MdTextButton::UpdateColor() { |
58 // TODO(estade): handle call to action theming and other things that can | 59 // TODO(estade): handle call to action theming and other things that can |
59 // affect the text color. | 60 // affect the text color. |
60 render_text_->SetColor(GetNativeTheme()->GetSystemColor( | 61 render_text_->SetColor(GetNativeTheme()->GetSystemColor( |
61 enabled() ? ui::NativeTheme::kColorId_MdTextButtonEnabledColor | 62 enabled() ? ui::NativeTheme::kColorId_MdTextButtonEnabledColor |
62 : ui::NativeTheme::kColorId_MdTextButtonDisabledColor)); | 63 : ui::NativeTheme::kColorId_MdTextButtonDisabledColor)); |
63 } | 64 } |
64 | 65 |
65 } // namespace views | 66 } // namespace views |
OLD | NEW |