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

Unified Diff: ui/views/controls/animated_icon_view.cc

Issue 2892563004: Use animated vector icon for app menu notification animation. (Closed)
Patch Set: git add Created 3 years, 7 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/animated_icon_view.cc
diff --git a/ui/views/controls/animated_icon_view.cc b/ui/views/controls/animated_icon_view.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7ace43691778530addb8f73c854795d2d9b604c6
--- /dev/null
+++ b/ui/views/controls/animated_icon_view.cc
@@ -0,0 +1,75 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/views/controls/animated_icon_view.h"
+
+#include "ui/compositor/compositor.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/color_palette.h"
+#include "ui/gfx/paint_vector_icon.h"
+#include "ui/views/widget/widget.h"
+
+namespace views {
+
+AnimatedIconView::AnimatedIconView(const gfx::VectorIcon& icon)
+ : icon_(icon),
+ color_(gfx::kPlaceholderColor),
+ duration_(gfx::GetDurationOfAnimation(icon)) {
+ set_can_process_events_within_subtree(false);
sadrul 2017/05/23 18:01:14 Why? Shouldn't this be something the creator of An
Evan Stade 2017/05/23 23:38:42 I don't think there will be any instances of this
sadrul 2017/05/24 17:35:14 When you create a View, the default expectation sh
Evan Stade 2017/05/24 17:47:53 ok, I will move this to AppMenuButton.
+ UpdateStaticImage();
+}
+
+AnimatedIconView::~AnimatedIconView() {}
+
+void AnimatedIconView::Animate(State target) {
+ SetState(target);
+ start_time_ = base::TimeTicks::Now();
+ if (!IsAnimating())
+ GetWidget()->GetCompositor()->AddAnimationObserver(this);
+}
+
+void AnimatedIconView::SetState(State state) {
+ state_ = state;
+ UpdateStaticImage();
+}
+
+void AnimatedIconView::OnPaint(gfx::Canvas* canvas) {
+ if (!IsAnimating()) {
+ views::ImageView::OnPaint(canvas);
sadrul 2017/05/23 18:01:14 How does this work when the animation ends? (i.e.
Evan Stade 2017/05/23 23:38:42 Animate() calls SetState() calls UpdateStaticImage
sadrul 2017/05/24 17:35:14 Who is calling Animate(END)? Shouldn't that happen
Evan Stade 2017/05/24 17:47:53 The client is calling it. In this case, AppMenuBut
sadrul 2017/05/26 19:44:47 The client having to track when the animation ends
Evan Stade 2017/05/26 20:48:52 The client doesn't have to do anything when the an
sadrul 2017/05/30 17:06:51 I see. So I guess I don't quite understand how OnP
Evan Stade 2017/05/30 18:53:45 You would have called Animate(END), not Animate(ST
sadrul 2017/05/30 19:48:33 Aah, yes. That makes sense now. Thanks for explain
+ return;
+ }
+
+ auto timestamp = base::TimeTicks::Now();
+ base::TimeDelta elapsed = timestamp - start_time_;
+ if (state_ == END)
+ elapsed = start_time_ + duration_ - timestamp;
+
+ gfx::Rect target_bounds = GetContentsBounds();
+ target_bounds.ClampToCenteredSize(GetImage().size());
sadrul 2017/05/23 18:01:14 Can this use GetImageBounds()?
Evan Stade 2017/05/23 23:38:42 seems so, thanks!
+ canvas->Translate(gfx::Vector2d(target_bounds.OffsetFromOrigin()));
sadrul 2017/05/23 18:01:14 OffsetFromOrigin() already returns a Vector2d, rig
Evan Stade 2017/05/23 23:38:42 Done.
+ gfx::PaintVectorIcon(canvas, icon_, color_, elapsed);
+}
+
+void AnimatedIconView::OnAnimationStep(base::TimeTicks timestamp) {
+ base::TimeDelta elapsed = timestamp - start_time_;
+ if (elapsed > duration_)
+ GetWidget()->GetCompositor()->RemoveAnimationObserver(this);
+
+ SchedulePaint();
+}
+
+void AnimatedIconView::OnCompositingShuttingDown(ui::Compositor* compositor) {}
+
+bool AnimatedIconView::IsAnimating() {
+ return GetWidget()->GetCompositor()->HasAnimationObserver(this);
sadrul 2017/05/23 18:01:14 Can we maintain a flag instead?
Evan Stade 2017/05/23 23:38:42 how's this?
+}
+
+void AnimatedIconView::UpdateStaticImage() {
+ gfx::IconDescription description(
+ icon_, 0, color_, state_ == START ? base::TimeDelta() : duration_,
+ gfx::kNoneIcon);
+ SetImage(gfx::CreateVectorIcon(description));
+}
+
+} // namespace views
« ui/views/controls/animated_icon_view.h ('K') | « ui/views/controls/animated_icon_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698