summaryrefslogtreecommitdiffstats
path: root/components/view_manager/animation_runner.h
blob: 02f9f2cbc5dba96be9830f37f3e1c8cf6f499352 (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
113
114
// Copyright 2014 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 COMPONENTS_VIEW_MANAGER_ANIMATION_RUNNER_H_
#define COMPONENTS_VIEW_MANAGER_ANIMATION_RUNNER_H_

#include <algorithm>
#include <map>
#include <set>
#include <vector>

#include "base/containers/scoped_ptr_hash_map.h"
#include "base/observer_list.h"
#include "base/time/time.h"

namespace mojo {
class AnimationGroup;
}

namespace view_manager {

class AnimationRunnerObserver;
class ScheduledAnimationGroup;
class ServerView;

// AnimationRunner is responsible for maintaing and running a set of animations.
// The animations are represented as a set of AnimationGroups. New animations
// are scheduled by way of Schedule(). A |view| may only have one animation
// running at a time. Schedule()ing a new animation for a view already animating
// implicitly cancels the current animation for the view. Animations progress
// by way of the Tick() function.
class AnimationRunner {
 public:
  using AnimationId = uint32_t;
  using ViewAndAnimationPair =
      std::pair<ServerView*, const mojo::AnimationGroup*>;

  explicit AnimationRunner(base::TimeTicks now);
  ~AnimationRunner();

  void AddObserver(AnimationRunnerObserver* observer);
  void RemoveObserver(AnimationRunnerObserver* observer);

  // Schedules animations. If any of the groups are not valid no animations are
  // scheuled and 0 is returned. If there is an existing animation in progress
  // for any of the views it is canceled and any properties that were animating
  // but are no longer animating are set to their target value.
  AnimationId Schedule(const std::vector<ViewAndAnimationPair>& views,
                       base::TimeTicks now);

  // Cancels an animation scheduled by an id that was previously returned from
  // Schedule().
  void CancelAnimation(AnimationId id);

  // Cancels the animation scheduled for |view|. Does nothing if there is no
  // animation scheduled for |view|. This does not change |view|. That is, any
  // in progress animations are stopped.
  void CancelAnimationForView(ServerView* view);

  // Advance the animations updating values appropriately.
  void Tick(base::TimeTicks time);

  // Returns true if there are animations currently scheduled.
  bool HasAnimations() const { return !view_to_animation_map_.empty(); }

  // Returns true if the animation identified by |id| is valid and animating.
  bool IsAnimating(AnimationId id) const {
    return id_to_views_map_.count(id) > 0;
  }

  // Returns the views that are currently animating for |id|. Returns an empty
  // set if |id| does not identify a valid animation.
  std::set<ServerView*> GetViewsAnimating(AnimationId id) {
    return IsAnimating(id) ? id_to_views_map_.find(id)->second
                           : std::set<ServerView*>();
  }

 private:
  enum CancelSource {
    // Cancel is the result of scheduling another animation for the view.
    CANCEL_SOURCE_SCHEDULE,

    // Cancel originates from an explicit call to cancel.
    CANCEL_SOURCE_CANCEL,
  };

  using ViewToAnimationMap =
      base::ScopedPtrHashMap<ServerView*, ScheduledAnimationGroup>;
  using IdToViewsMap = std::map<AnimationId, std::set<ServerView*>>;

  void CancelAnimationForViewImpl(ServerView* view, CancelSource source);

  // Removes |view| from both |view_to_animation_map_| and |id_to_views_map_|.
  // Returns true if there are no more views animating with the animation id
  // the view is associated with.
  bool RemoveViewFromMaps(ServerView* view);

  AnimationId next_id_;

  base::TimeTicks last_tick_time_;

  ObserverList<AnimationRunnerObserver> observers_;

  ViewToAnimationMap view_to_animation_map_;

  IdToViewsMap id_to_views_map_;

  DISALLOW_COPY_AND_ASSIGN(AnimationRunner);
};

}  // namespace view_manager

#endif  // COMPONENTS_VIEW_MANAGER_ANIMATION_RUNNER_H_