| 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/base/material_design/material_design_controller.h" | 8 #include "ui/base/material_design/material_design_controller.h" |
| 9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/color_palette.h" | 10 #include "ui/gfx/color_palette.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 static base::LazyInstance<gfx::FontList>::Leaky font_list = | 47 static base::LazyInstance<gfx::FontList>::Leaky font_list = |
| 48 LAZY_INSTANCE_INITIALIZER; | 48 LAZY_INSTANCE_INITIALIZER; |
| 49 const gfx::Font::Weight min_weight = gfx::Font::Weight::MEDIUM; | 49 const gfx::Font::Weight min_weight = gfx::Font::Weight::MEDIUM; |
| 50 if (font_list.Get().GetFontWeight() < min_weight) | 50 if (font_list.Get().GetFontWeight() < min_weight) |
| 51 font_list.Get() = font_list.Get().DeriveWithWeight(min_weight); | 51 font_list.Get() = font_list.Get().DeriveWithWeight(min_weight); |
| 52 return font_list.Get(); | 52 return font_list.Get(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 } // namespace | 55 } // namespace |
| 56 | 56 |
| 57 namespace internal { | |
| 58 | |
| 59 class MdFocusRing : public View { | |
| 60 public: | |
| 61 MdFocusRing() { | |
| 62 SetPaintToLayer(true); | |
| 63 layer()->SetFillsBoundsOpaquely(false); | |
| 64 } | |
| 65 ~MdFocusRing() override {} | |
| 66 | |
| 67 // View: | |
| 68 bool CanProcessEventsWithinSubtree() const override { return false; } | |
| 69 | |
| 70 void OnPaint(gfx::Canvas* canvas) override { | |
| 71 SkPaint paint; | |
| 72 paint.setAntiAlias(true); | |
| 73 paint.setColor( | |
| 74 SkColorSetA(GetNativeTheme()->GetSystemColor( | |
| 75 ui::NativeTheme::kColorId_FocusedBorderColor), | |
| 76 0x66)); | |
| 77 paint.setStyle(SkPaint::kStroke_Style); | |
| 78 paint.setStrokeWidth(kFocusBorderThickness); | |
| 79 gfx::RectF rect(GetLocalBounds()); | |
| 80 rect.Inset(gfx::InsetsF(kFocusBorderThickness / 2.f)); | |
| 81 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 DISALLOW_COPY_AND_ASSIGN(MdFocusRing); | |
| 86 }; | |
| 87 | |
| 88 } // namespace internal | |
| 89 | |
| 90 // static | 57 // static |
| 91 LabelButton* MdTextButton::CreateStandardButton(ButtonListener* listener, | 58 LabelButton* MdTextButton::CreateStandardButton(ButtonListener* listener, |
| 92 const base::string16& text) { | 59 const base::string16& text) { |
| 93 return CreateButton(listener, text, | 60 return CreateButton(listener, text, |
| 94 ui::MaterialDesignController::IsModeMaterial()); | 61 ui::MaterialDesignController::IsModeMaterial()); |
| 95 } | 62 } |
| 96 | 63 |
| 97 // static | 64 // static |
| 98 LabelButton* MdTextButton::CreateSecondaryUiButton(ButtonListener* listener, | 65 LabelButton* MdTextButton::CreateSecondaryUiButton(ButtonListener* listener, |
| 99 const base::string16& text) { | 66 const base::string16& text) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 124 } | 91 } |
| 125 | 92 |
| 126 void MdTextButton::SetProminent(bool is_prominent) { | 93 void MdTextButton::SetProminent(bool is_prominent) { |
| 127 if (is_prominent_ == is_prominent) | 94 if (is_prominent_ == is_prominent) |
| 128 return; | 95 return; |
| 129 | 96 |
| 130 is_prominent_ = is_prominent; | 97 is_prominent_ = is_prominent; |
| 131 UpdateColors(); | 98 UpdateColors(); |
| 132 } | 99 } |
| 133 | 100 |
| 134 void MdTextButton::Layout() { | |
| 135 LabelButton::Layout(); | |
| 136 gfx::Rect focus_bounds = GetLocalBounds(); | |
| 137 focus_bounds.Inset(gfx::Insets(-kFocusBorderThickness)); | |
| 138 focus_ring_->SetBoundsRect(focus_bounds); | |
| 139 } | |
| 140 | |
| 141 void MdTextButton::OnFocus() { | |
| 142 LabelButton::OnFocus(); | |
| 143 focus_ring_->SetVisible(true); | |
| 144 } | |
| 145 | |
| 146 void MdTextButton::OnBlur() { | |
| 147 LabelButton::OnBlur(); | |
| 148 focus_ring_->SetVisible(false); | |
| 149 } | |
| 150 | |
| 151 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 101 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 152 LabelButton::OnNativeThemeChanged(theme); | 102 LabelButton::OnNativeThemeChanged(theme); |
| 153 UpdateColors(); | 103 UpdateColors(); |
| 154 } | 104 } |
| 155 | 105 |
| 156 SkColor MdTextButton::GetInkDropBaseColor() const { | 106 SkColor MdTextButton::GetInkDropBaseColor() const { |
| 157 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); | 107 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); |
| 158 } | 108 } |
| 159 | 109 |
| 160 std::unique_ptr<views::InkDropRipple> MdTextButton::CreateInkDropRipple() | 110 std::unique_ptr<views::InkDropRipple> MdTextButton::CreateInkDropRipple() |
| (...skipping 27 matching lines...) Expand all Loading... |
| 188 shadows.push_back(gfx::ShadowValue(gfx::Vector2d(0, kYOffset), | 138 shadows.push_back(gfx::ShadowValue(gfx::Vector2d(0, kYOffset), |
| 189 2 * kSkiaBlurRadius, | 139 2 * kSkiaBlurRadius, |
| 190 SkColorSetA(SK_ColorBLACK, 0x3D))); | 140 SkColorSetA(SK_ColorBLACK, 0x3D))); |
| 191 return base::MakeUnique<InkDropHighlight>( | 141 return base::MakeUnique<InkDropHighlight>( |
| 192 gfx::RectF(GetLocalBounds()).CenterPoint(), | 142 gfx::RectF(GetLocalBounds()).CenterPoint(), |
| 193 base::WrapUnique(new BorderShadowLayerDelegate( | 143 base::WrapUnique(new BorderShadowLayerDelegate( |
| 194 shadows, GetLocalBounds(), kInkDropSmallCornerRadius))); | 144 shadows, GetLocalBounds(), kInkDropSmallCornerRadius))); |
| 195 } | 145 } |
| 196 | 146 |
| 197 bool MdTextButton::ShouldShowInkDropForFocus() const { | 147 bool MdTextButton::ShouldShowInkDropForFocus() const { |
| 198 // These types of button use |focus_ring_|. | 148 // These types of button use an MdFocusRing. |
| 199 return false; | 149 return false; |
| 200 } | 150 } |
| 201 | 151 |
| 202 void MdTextButton::SetEnabledTextColors(SkColor color) { | 152 void MdTextButton::SetEnabledTextColors(SkColor color) { |
| 203 LabelButton::SetEnabledTextColors(color); | 153 LabelButton::SetEnabledTextColors(color); |
| 204 UpdateColors(); | 154 UpdateColors(); |
| 205 } | 155 } |
| 206 | 156 |
| 207 void MdTextButton::SetText(const base::string16& text) { | 157 void MdTextButton::SetText(const base::string16& text) { |
| 208 LabelButton::SetText(text); | 158 LabelButton::SetText(text); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 219 UpdateColors(); | 169 UpdateColors(); |
| 220 } | 170 } |
| 221 | 171 |
| 222 void MdTextButton::SetFontList(const gfx::FontList& font_list) { | 172 void MdTextButton::SetFontList(const gfx::FontList& font_list) { |
| 223 NOTREACHED() | 173 NOTREACHED() |
| 224 << "Don't call MdTextButton::SetFontList (it will soon be protected)"; | 174 << "Don't call MdTextButton::SetFontList (it will soon be protected)"; |
| 225 } | 175 } |
| 226 | 176 |
| 227 MdTextButton::MdTextButton(ButtonListener* listener) | 177 MdTextButton::MdTextButton(ButtonListener* listener) |
| 228 : LabelButton(listener, base::string16()), | 178 : LabelButton(listener, base::string16()), |
| 229 focus_ring_(new internal::MdFocusRing()), | |
| 230 is_prominent_(false) { | 179 is_prominent_(false) { |
| 231 SetInkDropMode(PlatformStyle::kUseRipples ? InkDropMode::ON | 180 SetInkDropMode(PlatformStyle::kUseRipples ? InkDropMode::ON |
| 232 : InkDropMode::OFF); | 181 : InkDropMode::OFF); |
| 233 set_has_ink_drop_action_on_click(true); | 182 set_has_ink_drop_action_on_click(true); |
| 234 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 183 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 235 SetFocusForPlatform(); | 184 SetFocusForPlatform(); |
| 236 SetMinSize(gfx::Size(kMinWidth, 0)); | 185 SetMinSize(gfx::Size(kMinWidth, 0)); |
| 237 SetFocusPainter(nullptr); | 186 SetFocusPainter(nullptr); |
| 238 label()->SetAutoColorReadabilityEnabled(false); | 187 label()->SetAutoColorReadabilityEnabled(false); |
| 239 AddChildView(focus_ring_); | 188 set_should_use_md_focus_ring(true); |
| 240 focus_ring_->SetVisible(false); | |
| 241 set_request_focus_on_press(false); | 189 set_request_focus_on_press(false); |
| 242 LabelButton::SetFontList(GetMdFontList()); | 190 LabelButton::SetFontList(GetMdFontList()); |
| 243 | 191 |
| 244 // Paint to a layer so that the canvas is snapped to pixel boundaries (useful | 192 // Paint to a layer so that the canvas is snapped to pixel boundaries (useful |
| 245 // for fractional DSF). | 193 // for fractional DSF). |
| 246 SetPaintToLayer(true); | 194 SetPaintToLayer(true); |
| 247 layer()->SetFillsBoundsOpaquely(false); | 195 layer()->SetFillsBoundsOpaquely(false); |
| 248 } | 196 } |
| 249 | 197 |
| 250 MdTextButton::~MdTextButton() {} | 198 MdTextButton::~MdTextButton() {} |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) | 272 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) |
| 325 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); | 273 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); |
| 326 | 274 |
| 327 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color))); | 275 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color))); |
| 328 set_background(Background::CreateBackgroundPainter( | 276 set_background(Background::CreateBackgroundPainter( |
| 329 true, Painter::CreateRoundRectWith1PxBorderPainter( | 277 true, Painter::CreateRoundRectWith1PxBorderPainter( |
| 330 bg_color, stroke_color, kInkDropSmallCornerRadius))); | 278 bg_color, stroke_color, kInkDropSmallCornerRadius))); |
| 331 } | 279 } |
| 332 | 280 |
| 333 } // namespace views | 281 } // namespace views |
| OLD | NEW |