summaryrefslogtreecommitdiffstats
path: root/app/animation_container.h
blob: 98abf289606598df0090c1f6d3493bd2ca93d221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2010 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.

#ifndef APP_ANIMATION_CONTAINER_H_
#define APP_ANIMATION_CONTAINER_H_

#include <set>

#include "base/ref_counted.h"
#include "base/time.h"
#include "base/timer.h"

// AnimationContainer is used by Animation to manage the underlying timer.
// Internally each Animation creates a single AnimationContainer. You can
// group a set of Animations into the same AnimationContainer by way of
// Animation::SetContainer. Grouping a set of Animations into the same
// AnimationContainer ensures they all update and start at the same time.
//
// AnimationContainer is ref counted. Each Animation contained within the
// AnimationContainer own it.
class AnimationContainer : public base::RefCounted<AnimationContainer> {
 public:
  // The observer is notified after every update of the animations managed by
  // the container.
  class Observer {
   public:
    // Invoked on every tick of the timer managed by the container and after
    // all the animations have updated.
    virtual void AnimationContainerProgressed(
        AnimationContainer* container) = 0;

    // Invoked when no more animations are being managed by this container.
    virtual void AnimationContainerEmpty(AnimationContainer* container) = 0;
  };

  // Interface for the elements the AnimationContainer contains. This is
  // implemented by Animation.
  class Element {
   public:
    // Sets the start of the animation. This is invoked from
    // AnimationContainer::Start.
    virtual void SetStartTime(base::TimeTicks start_time) = 0;

    // Invoked when the animation is to progress.
    virtual void Step(base::TimeTicks time_now) = 0;

    // Returns the time interval of the animation. If an Element needs to change
    // this it should first invoke Stop, then Start.
    virtual base::TimeDelta GetTimerInterval() const = 0;

   protected:
    virtual ~Element() {}
  };

  AnimationContainer();

  // Invoked by Animation when it needs to start. Starts the timer if necessary.
  // NOTE: This is invoked by Animation for you, you shouldn't invoke this
  // directly.
  void Start(Element* animation);

  // Invoked by Animation when it needs to stop. If there are no more animations
  // running the timer stops.
  // NOTE: This is invoked by Animation for you, you shouldn't invoke this
  // directly.
  void Stop(Element* animation);

  void set_observer(Observer* observer) { observer_ = observer; }

  // The time the last animation ran at.
  base::TimeTicks last_tick_time() const { return last_tick_time_; }

  // Are there any timers running?
  bool is_running() const { return !elements_.empty(); }

 private:
  friend class base::RefCounted<AnimationContainer>;

  typedef std::set<Element*> Elements;

  ~AnimationContainer();

  // Timer callback method.
  void Run();

  // Sets min_timer_interval_ and restarts the timer.
  void SetMinTimerInterval(base::TimeDelta delta);

  // Returns the min timer interval of all the timers.
  base::TimeDelta GetMinInterval();

  // Represents one of two possible values:
  // . If only a single animation has been started and the timer hasn't yet
  //   fired this is the time the animation was added.
  // . The time the last animation ran at (::Run was invoked).
  base::TimeTicks last_tick_time_;

  // Set of elements (animations) being managed.
  Elements elements_;

  // Minimum interval the timers run at.
  base::TimeDelta min_timer_interval_;

  base::RepeatingTimer<AnimationContainer> timer_;

  Observer* observer_;

  DISALLOW_COPY_AND_ASSIGN(AnimationContainer);
};

#endif  // APP_ANIMATION_CONTAINER_H_