Bug 1354947 - Expose FindMatchingKeyframe and make it reusable for nsTimingFunction. r?birtles
MozReview-Commit-ID: Jjcrk5AhtUH
--- a/layout/style/nsAnimationManager.cpp
+++ b/layout/style/nsAnimationManager.cpp
@@ -880,76 +880,46 @@ GeckoCSSAnimationBuilder::GetKeyframePro
result.AppendElement(Move(pair));
aAnimatedProperties.AddProperty(prop);
}
return result;
}
-// Utility function to walk through |aIter| to find the Keyframe with
-// matching offset and timing function but stopping as soon as the offset
-// differs from |aOffset| (i.e. it assumes a sorted iterator).
-//
-// If a matching Keyframe is found,
-// Returns true and sets |aIndex| to the index of the matching Keyframe
-// within |aIter|.
-//
-// If no matching Keyframe is found,
-// Returns false and sets |aIndex| to the index in the iterator of the
-// first Keyframe with an offset differing to |aOffset| or, if the end
-// of the iterator is reached, sets |aIndex| to the index after the last
-// Keyframe.
-template <class IterType>
-static bool
-FindMatchingKeyframe(
- IterType&& aIter,
- double aOffset,
- const Maybe<ComputedTimingFunction>& aTimingFunctionToMatch,
- size_t& aIndex)
-{
- aIndex = 0;
- for (Keyframe& keyframe : aIter) {
- if (keyframe.mOffset.value() != aOffset) {
- break;
- }
- if (keyframe.mTimingFunction == aTimingFunctionToMatch) {
- return true;
- }
- ++aIndex;
- }
- return false;
-}
-
void
GeckoCSSAnimationBuilder::FillInMissingKeyframeValues(
nsCSSPropertyIDSet aAnimatedProperties,
nsCSSPropertyIDSet aPropertiesSetAtStart,
nsCSSPropertyIDSet aPropertiesSetAtEnd,
const Maybe<ComputedTimingFunction>& aInheritedTimingFunction,
nsTArray<Keyframe>& aKeyframes)
{
static const size_t kNotSet = static_cast<size_t>(-1);
// Find/create the keyframe to add start values to
size_t startKeyframeIndex = kNotSet;
if (!aAnimatedProperties.Equals(aPropertiesSetAtStart) &&
- !FindMatchingKeyframe(aKeyframes, 0.0, aInheritedTimingFunction,
- startKeyframeIndex)) {
+ !nsAnimationManager::FindMatchingKeyframe(aKeyframes,
+ 0.0,
+ aInheritedTimingFunction,
+ startKeyframeIndex)) {
Keyframe newKeyframe;
newKeyframe.mOffset.emplace(0.0);
newKeyframe.mTimingFunction = aInheritedTimingFunction;
aKeyframes.InsertElementAt(startKeyframeIndex, Move(newKeyframe));
}
// Find/create the keyframe to add end values to
size_t endKeyframeIndex = kNotSet;
if (!aAnimatedProperties.Equals(aPropertiesSetAtEnd)) {
- if (!FindMatchingKeyframe(Reversed(aKeyframes), 1.0,
- aInheritedTimingFunction, endKeyframeIndex)) {
+ if (!nsAnimationManager::FindMatchingKeyframe(Reversed(aKeyframes),
+ 1.0,
+ aInheritedTimingFunction,
+ endKeyframeIndex)) {
Keyframe newKeyframe;
newKeyframe.mOffset.emplace(1.0);
newKeyframe.mTimingFunction = aInheritedTimingFunction;
aKeyframes.AppendElement(Move(newKeyframe));
endKeyframeIndex = aKeyframes.Length() - 1;
} else {
// endKeyframeIndex is currently a count from the end of the array
// so we need to reverse it.
--- a/layout/style/nsAnimationManager.h
+++ b/layout/style/nsAnimationManager.h
@@ -5,16 +5,17 @@
#ifndef nsAnimationManager_h_
#define nsAnimationManager_h_
#include "mozilla/Attributes.h"
#include "mozilla/ContentEvents.h"
#include "mozilla/EventForwards.h"
#include "AnimationCommon.h"
#include "mozilla/dom/Animation.h"
+#include "mozilla/Keyframe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/TimeStamp.h"
class nsIGlobalObject;
class nsStyleContext;
struct nsStyleDisplay;
struct ServoComputedValues;
@@ -352,16 +353,49 @@ public:
void DispatchEvents()
{
RefPtr<nsAnimationManager> kungFuDeathGrip(this);
mEventDispatcher.DispatchEvents(mPresContext);
}
void SortEvents() { mEventDispatcher.SortEvents(); }
void ClearEventQueue() { mEventDispatcher.ClearEventQueue(); }
+ // Utility function to walk through |aIter| to find the Keyframe with
+ // matching offset and timing function but stopping as soon as the offset
+ // differs from |aOffset| (i.e. it assumes a sorted iterator).
+ //
+ // If a matching Keyframe is found,
+ // Returns true and sets |aIndex| to the index of the matching Keyframe
+ // within |aIter|.
+ //
+ // If no matching Keyframe is found,
+ // Returns false and sets |aIndex| to the index in the iterator of the
+ // first Keyframe with an offset differing to |aOffset| or, if the end
+ // of the iterator is reached, sets |aIndex| to the index after the last
+ // Keyframe.
+ template <class IterType, class TimingFunctionType>
+ static bool FindMatchingKeyframe(
+ IterType&& aIter,
+ double aOffset,
+ const TimingFunctionType& aTimingFunctionToMatch,
+ size_t& aIndex)
+ {
+ aIndex = 0;
+ for (mozilla::Keyframe& keyframe : aIter) {
+ if (keyframe.mOffset.value() != aOffset) {
+ break;
+ }
+ if (keyframe.mTimingFunction == aTimingFunctionToMatch) {
+ return true;
+ }
+ ++aIndex;
+ }
+ return false;
+ }
+
protected:
~nsAnimationManager() override = default;
private:
template<class BuilderType>
void DoUpdateAnimations(
const mozilla::NonOwningAnimationTarget& aTarget,
const nsStyleDisplay& aStyleDisplay,