Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(536)

Unified Diff: ui/views/controls/button/toggle_button.cc

Issue 2396133005: [ash-md] Animates ToggleButton highlight to move it in sync with the thumb (Closed)
Patch Set: [ash-md] Animates ToggleButton highlight to move it in sync with the thumb (comments) Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 30bf562a54887ce6a3b62c24af0408b80104c884..f8be8c0d464b01f41a8af37b33dd2a230af2352b 100644
--- a/ui/views/controls/button/toggle_button.cc
+++ b/ui/views/controls/button/toggle_button.cc
@@ -34,8 +34,71 @@ const SkColor kTrackOffColor =
} // namespace
+// Class representing the thumb. When the thumb is clicked it is separated into
+// its own layer and the ink drop layer is made a child of the thumb layer
+// allowing the two to animate in sync.
+class ToggleButton::ThumbView : public views::View {
+ public:
+ ThumbView(views::View* parent) : color_ratio_(0.) {
sadrul 2016/10/13 03:30:23 explicit Or may be better: AddChild() from the ca
varkha 2016/10/13 17:04:56 I like your second choice more. Done.
+ parent->AddChildView(this);
+ }
+ ~ThumbView() override {}
+
+ void AddInkDropLayer(ui::Layer* ink_drop_layer) {
+ SetPaintToLayer(true);
+ layer()->SetFillsBoundsOpaquely(false);
+ layer()->Add(ink_drop_layer);
+ }
+
+ void RemoveInkDropLayer(ui::Layer* ink_drop_layer) {
+ layer()->Remove(ink_drop_layer);
+ SetPaintToLayer(false);
+ }
+
+ void Update(const gfx::Rect& bounds, double color_ratio) {
+ SetBoundsRect(bounds);
+ color_ratio_ = color_ratio;
+ SchedulePaint();
+ }
+
+ private:
+ // views::View:
+ const char* GetClassName() const override {
+ return "ToggleButton::ThumbView";
+ }
+
+ void OnPaint(gfx::Canvas* canvas) override {
+ std::vector<gfx::ShadowValue> shadows;
+ shadows.emplace_back(gfx::Vector2d(0, 1), 4.f,
+ SkColorSetA(SK_ColorBLACK, 0x99));
+ SkPaint thumb_paint;
+ thumb_paint.setLooper(gfx::CreateShadowDrawLooperCorrectBlur(shadows));
+ thumb_paint.setStyle(SkPaint::kFill_Style);
+ thumb_paint.setAntiAlias(true);
+ const SkColor thumb_on_color = GetNativeTheme()->GetSystemColor(
+ ui::NativeTheme::kColorId_ProminentButtonColor);
+ // TODO(estade): get this color from the theme?
+ const SkColor thumb_off_color = SK_ColorWHITE;
+ const SkAlpha blend = static_cast<SkAlpha>(SK_AlphaOPAQUE * color_ratio_);
+ thumb_paint.setColor(
+ color_utils::AlphaBlend(thumb_on_color, thumb_off_color, blend));
+ gfx::Rect thumb_bounds = GetLocalBounds();
+ thumb_bounds.Inset(gfx::Insets(kThumbVerticalMargin));
+ canvas->DrawCircle(gfx::RectF(thumb_bounds).CenterPoint(),
+ thumb_bounds.height() / 2.f, thumb_paint);
+ }
+
+ // Color ratio between 0 and 1 that controls the thumb color.
+ double color_ratio_;
+
+ DISALLOW_COPY_AND_ASSIGN(ThumbView);
+};
+
ToggleButton::ToggleButton(ButtonListener* listener)
- : CustomButton(listener), is_on_(false), slide_animation_(this) {
+ : CustomButton(listener),
+ is_on_(false),
+ slide_animation_(this),
+ thumb_view_(new ToggleButton::ThumbView(this)) {
slide_animation_.SetSlideDuration(80 /* ms */);
slide_animation_.SetTweenType(gfx::Tween::LINEAR);
SetBorder(Border::CreateEmptyBorder(
@@ -44,7 +107,9 @@ ToggleButton::ToggleButton(ButtonListener* listener)
set_has_ink_drop_action_on_click(true);
}
-ToggleButton::~ToggleButton() {}
+ToggleButton::~ToggleButton() {
+ thumb_view_.reset();
+}
void ToggleButton::SetIsOn(bool is_on, bool animate) {
if (is_on_ == is_on)
@@ -66,9 +131,13 @@ gfx::Size ToggleButton::GetPreferredSize() const {
return rect.size();
}
+const char* ToggleButton::GetClassName() const {
+ return "ToggleButton";
+}
+
void ToggleButton::OnPaint(gfx::Canvas* canvas) {
- SkAlpha blend =
- static_cast<SkAlpha>(SK_AlphaOPAQUE * slide_animation_.GetCurrentValue());
+ const double color_ratio = slide_animation_.GetCurrentValue();
+ SkAlpha blend = static_cast<SkAlpha>(SK_AlphaOPAQUE * color_ratio);
// Track.
gfx::RectF track_rect(GetContentsBounds());
@@ -81,24 +150,6 @@ void ToggleButton::OnPaint(gfx::Canvas* canvas) {
track_paint.setColor(
color_utils::AlphaBlend(track_on_color, kTrackOffColor, blend));
canvas->DrawRoundRect(track_rect, track_rect.height() / 2, track_paint);
-
- // Thumb.
- gfx::Rect thumb_bounds = GetThumbBounds();
- SkPaint thumb_paint;
- std::vector<gfx::ShadowValue> shadows;
- shadows.emplace_back(gfx::Vector2d(0, 1), 4.f,
- SkColorSetA(SK_ColorBLACK, 0x99));
- thumb_paint.setLooper(gfx::CreateShadowDrawLooperCorrectBlur(shadows));
- thumb_paint.setStyle(SkPaint::kFill_Style);
- thumb_paint.setAntiAlias(true);
- const SkColor thumb_on_color = GetNativeTheme()->GetSystemColor(
- ui::NativeTheme::kColorId_ProminentButtonColor);
- // TODO(estade): get this color from the theme?
- const SkColor thumb_off_color = SK_ColorWHITE;
- thumb_paint.setColor(
- color_utils::AlphaBlend(thumb_on_color, thumb_off_color, blend));
- canvas->DrawCircle(gfx::RectF(thumb_bounds).CenterPoint(),
- thumb_bounds.height() / 2.f, thumb_paint);
}
void ToggleButton::NotifyClick(const ui::Event& event) {
@@ -106,12 +157,28 @@ void ToggleButton::NotifyClick(const ui::Event& event) {
CustomButton::NotifyClick(event);
}
+void ToggleButton::OnBoundsChanged(const gfx::Rect& previous_bounds) {
+ UpdateThumb();
+}
+
void ToggleButton::OnNativeThemeChanged(const ui::NativeTheme* theme) {
SchedulePaint();
}
+void ToggleButton::AddInkDropLayer(ui::Layer* ink_drop_layer) {
+ thumb_view_->AddInkDropLayer(ink_drop_layer);
+ UpdateThumb();
+ SchedulePaint();
+}
+
+void ToggleButton::RemoveInkDropLayer(ui::Layer* ink_drop_layer) {
+ thumb_view_->RemoveInkDropLayer(ink_drop_layer);
+ SchedulePaint();
+}
+
std::unique_ptr<InkDropRipple> ToggleButton::CreateInkDropRipple() const {
- return CreateDefaultInkDropRipple(GetThumbBounds().CenterPoint());
+ const int radius = (kTrackHeight + kTrackVerticalMargin * 2) / 2;
+ return CreateDefaultInkDropRipple(gfx::Point(radius, radius));
}
SkColor ToggleButton::GetInkDropBaseColor() const {
@@ -126,10 +193,12 @@ bool ToggleButton::ShouldShowInkDropHighlight() const {
}
void ToggleButton::AnimationProgressed(const gfx::Animation* animation) {
- if (animation == &slide_animation_)
+ if (animation == &slide_animation_) {
+ UpdateThumb();
SchedulePaint();
sadrul 2016/10/13 03:30:23 Now that the thumb is a separate View, it would be
varkha 2016/10/13 17:04:56 Acknowledged. Added a TODO for a possible followup
- else
- CustomButton::AnimationProgressed(animation);
+ return;
+ }
+ CustomButton::AnimationProgressed(animation);
}
gfx::Rect ToggleButton::GetThumbBounds() const {
@@ -144,4 +213,10 @@ gfx::Rect ToggleButton::GetThumbBounds() const {
return thumb_bounds;
}
+void ToggleButton::UpdateThumb() {
+ gfx::Rect thumb_bounds = GetThumbBounds();
+ thumb_bounds.Inset(gfx::Insets(-kThumbVerticalMargin));
+ thumb_view_->Update(thumb_bounds, slide_animation_.GetCurrentValue());
+}
+
} // namespace views
« ui/views/controls/button/toggle_button.h ('K') | « ui/views/controls/button/toggle_button.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698