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..bd4744b98b37ca7220b327b1dc1329aea44b4a67 100644 |
| --- a/ui/views/controls/button/toggle_button.cc |
| +++ b/ui/views/controls/button/toggle_button.cc |
| @@ -10,9 +10,12 @@ |
| #include "ui/gfx/canvas.h" |
| #include "ui/gfx/color_palette.h" |
| #include "ui/gfx/color_utils.h" |
| +#include "ui/gfx/geometry/rect_conversions.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,11 +27,8 @@ 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 |
| @@ -51,6 +51,13 @@ class ToggleButton::ThumbView : public InkDropHostView { |
| .Offset(gfx::Vector2d(kShadowOffsetX, kShadowOffsetY)); |
| } |
| + protected: |
| + // views::View: |
| + bool CanProcessEventsWithinSubtree() const override { |
| + // Make the thumb behave as part of the parent for tooltip event handling. |
| + return false; |
| + } |
| + |
| private: |
| static const int kShadowOffsetX = 0; |
| static const int kShadowOffsetY = 1; |
| @@ -108,8 +115,6 @@ ToggleButton::ToggleButton(ButtonListener* listener) |
| thumb_view_(new ThumbView()) { |
| 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 +141,31 @@ void ToggleButton::SetIsOn(bool is_on, bool animate) { |
| } |
| } |
| +void ToggleButton::set_focus_painter(std::unique_ptr<Painter> focus_painter) { |
| + focus_painter_ = std::move(focus_painter); |
| +} |
| + |
| +void ToggleButton::OnFocus() { |
| + CustomButton::OnFocus(); |
| + if (focus_painter_) |
| + SchedulePaint(); |
| +} |
| + |
| +void ToggleButton::OnBlur() { |
| + CustomButton::OnBlur(); |
| + if (focus_painter_) |
| + SchedulePaint(); |
| +} |
| + |
| +gfx::RectF ToggleButton::GetTrackBounds() const { |
|
Evan Stade
2016/11/17 22:32:22
we can only lay out in DIP, so this should return
varkha
2016/11/17 22:51:54
Done.
|
| + gfx::RectF track_bounds((gfx::SizeF(size()))); |
|
Evan Stade
2016/11/17 22:32:22
too many parens
varkha
2016/11/17 22:51:54
I would also think so. Compiler disagreed though:
|
| + track_bounds.ClampToCenteredSize(gfx::SizeF(kTrackWidth, kTrackHeight)); |
| + return track_bounds; |
| +} |
| + |
| gfx::Rect ToggleButton::GetThumbBounds() const { |
| - gfx::Rect thumb_bounds = GetLocalBounds(); |
| - thumb_bounds.Inset(gfx::Insets(kThumbVerticalMargin, kThumbHorizontalMargin)); |
| + gfx::Rect thumb_bounds(gfx::ToEnclosingRect(GetTrackBounds())); |
| + thumb_bounds.Inset(gfx::Insets(-kThumbInset)); |
| thumb_bounds.set_x(thumb_bounds.x() + |
| slide_animation_.GetCurrentValue() * |
| (thumb_bounds.width() - thumb_bounds.height())); |
| @@ -165,6 +192,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 +205,21 @@ 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); |
|
Evan Stade
2016/11/17 22:32:22
Personally I think I'd prefer not indenting and us
varkha
2016/11/17 22:51:54
Done. We use both throughout the code but I've not
Evan Stade
2016/11/17 23:00:43
yes, ScopedCanvas is nice when you already have an
varkha
2016/11/17 23:18:10
Acknowledged.
|
| + float dsf = canvas->UndoDeviceScaleFactor(); |
| + gfx::RectF track_rect(GetTrackBounds()); |
| + 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_.get()); |
| } |
| void ToggleButton::NotifyClick(const ui::Event& event) { |