Chromium Code Reviews| Index: ui/views/controls/button/toggle_button.cc |
| diff --git a/ui/views/controls/button/toggle_button.cc b/ui/views/controls/button/toggle_button.cc |
| index 8d02d0b838aa732697a6f3e5a7e9d1e0b87d4a26..ff209a128b850aa2b43433ee0ca1b6d8acdb759d 100644 |
| --- a/ui/views/controls/button/toggle_button.cc |
| +++ b/ui/views/controls/button/toggle_button.cc |
| @@ -10,9 +10,11 @@ |
| #include "ui/gfx/canvas.h" |
| #include "ui/gfx/color_palette.h" |
| #include "ui/gfx/color_utils.h" |
| +#include "ui/gfx/scoped_canvas.h" |
| #include "ui/views/animation/ink_drop_impl.h" |
| #include "ui/views/animation/ink_drop_ripple.h" |
| #include "ui/views/border.h" |
| +#include "ui/views/painter.h" |
| namespace views { |
| @@ -24,18 +26,15 @@ const int kTrackWidth = 28; |
| // Margins from edge of track to edge of view. |
| const int kTrackVerticalMargin = 5; |
| const int kTrackHorizontalMargin = 6; |
| -// Margin from edge of thumb to closest edge of view. Note that the thumb |
| -// margins must be sufficiently large to allow space for the shadow. |
| -const int kThumbHorizontalMargin = 4; |
| -// Margin from top/bottom edge of thumb to top/bottom edge of view. |
| -const int kThumbVerticalMargin = 3; |
| +// Inset from the rounded edge of the thumb to the rounded edge of the track. |
| +const int kThumbInset = 2; |
| } // namespace |
| // Class representing the thumb (the circle that slides horizontally). |
| class ToggleButton::ThumbView : public InkDropHostView { |
| public: |
| - ThumbView() : color_ratio_(0.) {} |
| + ThumbView(ToggleButton* parent) : color_ratio_(0.), parent_(parent) {} |
| ~ThumbView() override {} |
| void Update(const gfx::Rect& bounds, double color_ratio) { |
| @@ -51,6 +50,12 @@ class ToggleButton::ThumbView : public InkDropHostView { |
| .Offset(gfx::Vector2d(kShadowOffsetX, kShadowOffsetY)); |
| } |
| + // views::View: |
| + bool GetTooltipText(const gfx::Point& p, |
| + base::string16* tooltip) const override { |
| + return parent_->GetTooltipText(p, tooltip); |
|
Evan Stade
2016/11/17 21:11:26
can't we just make this view invisible in terms of
varkha
2016/11/17 22:24:57
Yes, TIL. Done.
|
| + } |
| + |
| private: |
| static const int kShadowOffsetX = 0; |
| static const int kShadowOffsetY = 1; |
| @@ -95,6 +100,9 @@ class ToggleButton::ThumbView : public InkDropHostView { |
| // Color ratio between 0 and 1 that controls the thumb color. |
| double color_ratio_; |
| + // Our parent. |
| + ToggleButton* parent_; |
|
Evan Stade
2016/11/17 21:11:26
I don't think you need this, just call parent() (a
varkha
2016/11/17 22:24:57
Done.
|
| + |
| DISALLOW_COPY_AND_ASSIGN(ThumbView); |
| }; |
| @@ -105,11 +113,10 @@ ToggleButton::ToggleButton(ButtonListener* listener) |
| : CustomButton(listener), |
| is_on_(false), |
| slide_animation_(this), |
| - thumb_view_(new ThumbView()) { |
| + thumb_view_(new ThumbView(this)), |
| + focus_painter_(Painter::CreateDashedFocusPainter()) { |
|
Evan Stade
2016/11/17 21:11:26
I don't think we'll ever want a dashed focus paint
varkha
2016/11/17 22:24:57
Done.
|
| slide_animation_.SetSlideDuration(80 /* ms */); |
| slide_animation_.SetTweenType(gfx::Tween::LINEAR); |
| - SetBorder(CreateEmptyBorder( |
| - gfx::Insets(kTrackVerticalMargin, kTrackHorizontalMargin))); |
| AddChildView(thumb_view_); |
| SetInkDropMode(InkDropMode::ON); |
| set_has_ink_drop_action_on_click(true); |
| @@ -136,9 +143,27 @@ void ToggleButton::SetIsOn(bool is_on, bool animate) { |
| } |
| } |
| +void ToggleButton::SetFocusPainter(std::unique_ptr<Painter> focus_painter) { |
|
Evan Stade
2016/11/17 21:11:26
set_focus_painter
varkha
2016/11/17 22:24:57
Done.
|
| + focus_painter_ = std::move(focus_painter); |
| +} |
| + |
| +void ToggleButton::OnFocus() { |
| + CustomButton::OnFocus(); |
| + if (focus_painter_.get()) |
| + SchedulePaint(); |
| +} |
| + |
| +void ToggleButton::OnBlur() { |
| + CustomButton::OnBlur(); |
| + if (focus_painter_.get()) |
|
Evan Stade
2016/11/17 21:11:26
.get() shouldn't be necessary
varkha
2016/11/17 22:24:57
Done.
|
| + SchedulePaint(); |
| +} |
| + |
| gfx::Rect ToggleButton::GetThumbBounds() const { |
| - gfx::Rect thumb_bounds = GetLocalBounds(); |
| - thumb_bounds.Inset(gfx::Insets(kThumbVerticalMargin, kThumbHorizontalMargin)); |
| + gfx::Rect thumb_bounds((width() - kTrackWidth) / 2, |
| + (height() - kTrackHeight) / 2, kTrackWidth, |
|
Evan Stade
2016/11/17 21:11:26
please share the track rect calculations
varkha
2016/11/17 22:24:57
Done.
|
| + kTrackHeight); |
| + thumb_bounds.Inset(gfx::Insets(-kThumbInset)); |
| thumb_bounds.set_x(thumb_bounds.x() + |
| slide_animation_.GetCurrentValue() * |
| (thumb_bounds.width() - thumb_bounds.height())); |
| @@ -165,6 +190,7 @@ SkColor ToggleButton::GetTrackColor(bool is_on) const { |
| gfx::Size ToggleButton::GetPreferredSize() const { |
| gfx::Rect rect(0, 0, kTrackWidth, kTrackHeight); |
| + rect.Inset(gfx::Insets(-kTrackVerticalMargin, -kTrackHorizontalMargin)); |
| if (border()) |
| rect.Inset(-border()->GetInsets()); |
| return rect.size(); |
| @@ -177,17 +203,23 @@ const char* ToggleButton::GetClassName() const { |
| void ToggleButton::OnPaint(gfx::Canvas* canvas) { |
| // Paint the toggle track. To look sharp even at fractional scale factors, |
| // round up to pixel boundaries. |
| - float dsf = canvas->UndoDeviceScaleFactor(); |
| - gfx::RectF track_rect(GetContentsBounds()); |
| - track_rect.Scale(dsf); |
| - track_rect = gfx::RectF(gfx::ToEnclosingRect(track_rect)); |
| - SkPaint track_paint; |
| - track_paint.setAntiAlias(true); |
| - const double color_ratio = slide_animation_.GetCurrentValue(); |
| - track_paint.setColor(color_utils::AlphaBlend( |
| - GetTrackColor(true), GetTrackColor(false), |
| - static_cast<SkAlpha>(SK_AlphaOPAQUE * color_ratio))); |
| - canvas->DrawRoundRect(track_rect, track_rect.height() / 2, track_paint); |
| + { |
| + gfx::ScopedCanvas scoped_canvas(canvas); |
| + float dsf = canvas->UndoDeviceScaleFactor(); |
| + gfx::RectF track_rect((width() - kTrackWidth) / 2, |
| + (height() - kTrackHeight) / 2, kTrackWidth, |
| + kTrackHeight); |
| + track_rect.Scale(dsf); |
| + track_rect = gfx::RectF(gfx::ToEnclosingRect(track_rect)); |
| + SkPaint track_paint; |
| + track_paint.setAntiAlias(true); |
| + const double color_ratio = slide_animation_.GetCurrentValue(); |
| + track_paint.setColor(color_utils::AlphaBlend( |
| + GetTrackColor(true), GetTrackColor(false), |
| + static_cast<SkAlpha>(SK_AlphaOPAQUE * color_ratio))); |
| + canvas->DrawRoundRect(track_rect, track_rect.height() / 2, track_paint); |
| + } |
| + views::Painter::PaintFocusPainter(this, canvas, focus_painter()); |
| } |
| void ToggleButton::NotifyClick(const ui::Event& event) { |