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