blob: 4d735aef8b4fae9dd5d354763878ee80016cf848 [file] [log] [blame]
// Copyright 2015 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include <map>
#include "base/basictypes.h"
#include "base/optional.h"
#include "base/time.h"
#include "cobalt/cssom/css_transition.h"
#include "cobalt/cssom/property_definitions.h"
namespace cobalt {
namespace cssom {
class CSSComputedStyleData;
class PropertyValue;
// Maintains a mapping from the set of all unique CSS style properties to
// active transitions. Not all style properties may have active transitions.
// This class is used to maintain persistance of active transitions and is
// expected to be modified over time as events such as CSS style modifications
// occur.
class TransitionSet {
// An EventHandler object can be supplied to the TransitionSet upon
// construction to have TransitionSet update a caller as it internally updates
// its set of active transitions on each call to UpdateTransitions(). This
// is useful as it can provide an external observer (e.g. such as an adapter
// from CSS Transitions to the Web Animations API) the opportunity to take
// action based on transition start/end events.
class EventHandler {
virtual ~EventHandler() {}
// Called when a transition is starting, either freshly introduced or
// replacing an existing transition.
virtual void OnTransitionStarted(const Transition& transition) = 0;
// Called when a transition is removed, either because it has finished, or
// because it was replaced (in which case OnTransitionStarted() will be
// called with the replacement immediately after this).
virtual void OnTransitionRemoved(const Transition& transition) = 0;
explicit TransitionSet(EventHandler* event_handler = NULL);
// Helper function to get a const reference to a global empty transition set.
static const TransitionSet* EmptyTransitionSet();
// Given a source and destination CSS Style Declarations (presumably applied
// to an HTML element), updates all current transitions to reflect the new
// state. This may mean removing transitions that were canceled or
// completed, updating existing transitions with new values, or creating new
// transitions. The implementation of this method is defined here:
void UpdateTransitions(const base::TimeDelta& current_time,
const CSSComputedStyleData& source,
const CSSComputedStyleData& destination);
// Given the name of a property, returns the active transition corresponding
// to it. If no transition currently exists for this property, this method
// returns NULL.
const Transition* GetTransitionForProperty(PropertyKey property) const;
// Returns true if there are no animations in this animation set.
bool empty() const { return transitions_.IsEmpty(); }
// Clears out all currently running transitions.
void Clear();
void UpdateTransitionForProperty(
PropertyKey property, const base::TimeDelta& current_time,
const scoped_refptr<PropertyValue>& source_value,
const scoped_refptr<PropertyValue>& destination_value,
const CSSComputedStyleData& transition_style);
// We define a TransitionMap (from CSS property to Transition object) here to
// host our set of active transitions, and additionally manage the firing
// of transition begin/end events.
class TransitionMap {
explicit TransitionMap(EventHandler* event_handler);
const Transition* GetTransitionForProperty(PropertyKey property) const;
bool IsEmpty() const { return transitions_.empty(); }
// Adds or replaces an existing transition in the map.
void UpdateTransitionForProperty(PropertyKey property,
const Transition& transition);
// Removes an existing transition from the map. The transition must
// already exist in the map.
void RemoveTransitionForProperty(PropertyKey property);
// Clears all entries from the map, as if RemoveTransitionForProperty()
// had been called on each property.
void Clear();
typedef std::map<PropertyKey, Transition> InternalTransitionMap;
const InternalTransitionMap& GetInternalMap() const { return transitions_; }
EventHandler* event_handler_;
InternalTransitionMap transitions_;
TransitionMap transitions_;
} // namespace cssom
} // namespace cobalt