SVGAnimatedEnumeration.h (4014B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef DOM_SVG_SVGANIMATEDENUMERATION_H_ 8 #define DOM_SVG_SVGANIMATEDENUMERATION_H_ 9 10 #include "DOMSVGAnimatedEnumeration.h" 11 #include "mozilla/SMILAttr.h" 12 #include "mozilla/UniquePtr.h" 13 #include "mozilla/dom/SVGElement.h" 14 #include "nsCycleCollectionParticipant.h" 15 #include "nsError.h" 16 17 class nsAtom; 18 19 namespace mozilla { 20 21 class SMILValue; 22 23 namespace dom { 24 class SVGAnimationElement; 25 } // namespace dom 26 27 using SVGEnumValue = uint8_t; 28 29 struct SVGEnumMapping { 30 nsStaticAtom* const mKey; 31 const SVGEnumValue mVal; 32 }; 33 34 class SVGAnimatedEnumeration { 35 public: 36 friend class AutoChangeEnumNotifier; 37 using SVGElement = dom::SVGElement; 38 39 void Init(uint8_t aAttrEnum, uint16_t aValue) { 40 mAnimVal = mBaseVal = uint8_t(aValue); 41 mAttrEnum = aAttrEnum; 42 mIsAnimated = false; 43 mIsBaseSet = false; 44 } 45 46 // Returns whether aValue corresponded to a key in our mapping (in which case 47 // we actually set the base value) or not (in which case we did not). 48 bool SetBaseValueAtom(const nsAtom* aValue, SVGElement* aSVGElement); 49 nsAtom* GetBaseValueAtom(SVGElement* aSVGElement); 50 void SetBaseValue(uint16_t aValue, SVGElement* aSVGElement, ErrorResult& aRv); 51 uint16_t GetBaseValue() const { return mBaseVal; } 52 53 void SetAnimValue(uint16_t aValue, SVGElement* aSVGElement); 54 uint16_t GetAnimValue() const { return mAnimVal; } 55 bool IsExplicitlySet() const { return mIsAnimated || mIsBaseSet; } 56 57 already_AddRefed<dom::DOMSVGAnimatedEnumeration> ToDOMAnimatedEnum( 58 SVGElement* aSVGElement); 59 60 UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement); 61 62 private: 63 SVGEnumValue mAnimVal; 64 SVGEnumValue mBaseVal; 65 uint8_t mAttrEnum; // element specified tracking for attribute 66 bool mIsAnimated; 67 bool mIsBaseSet; 68 69 const SVGEnumMapping* GetMapping(SVGElement* aSVGElement); 70 71 public: 72 // DOM wrapper class for the (DOM)SVGAnimatedEnumeration interface where the 73 // wrapped class is SVGAnimatedEnumeration. 74 struct DOMAnimatedEnum final : public dom::DOMSVGAnimatedEnumeration { 75 DOMAnimatedEnum(SVGAnimatedEnumeration* aVal, SVGElement* aSVGElement) 76 : dom::DOMSVGAnimatedEnumeration(aSVGElement), mVal(aVal) {} 77 virtual ~DOMAnimatedEnum(); 78 79 SVGAnimatedEnumeration* mVal; // kept alive because it belongs to content 80 81 using dom::DOMSVGAnimatedEnumeration::SetBaseVal; 82 uint16_t BaseVal() override { return mVal->GetBaseValue(); } 83 void SetBaseVal(uint16_t aBaseVal, ErrorResult& aRv) override { 84 mVal->SetBaseValue(aBaseVal, mSVGElement, aRv); 85 } 86 uint16_t AnimVal() override { 87 // Script may have modified animation parameters or timeline -- DOM 88 // getters need to flush any resample requests to reflect these 89 // modifications. 90 mSVGElement->FlushAnimations(); 91 return mVal->GetAnimValue(); 92 } 93 }; 94 95 struct SMILEnum : public SMILAttr { 96 public: 97 SMILEnum(SVGAnimatedEnumeration* aVal, SVGElement* aSVGElement) 98 : mVal(aVal), mSVGElement(aSVGElement) {} 99 100 // These will stay alive because a SMILAttr only lives as long 101 // as the Compositing step, and DOM elements don't get a chance to 102 // die during that. 103 SVGAnimatedEnumeration* mVal; 104 SVGElement* mSVGElement; 105 106 // SMILAttr methods 107 nsresult ValueFromString(const nsAString& aStr, 108 const dom::SVGAnimationElement* aSrcElement, 109 SMILValue& aValue, 110 bool& aPreventCachingOfSandwich) const override; 111 SMILValue GetBaseValue() const override; 112 void ClearAnimValue() override; 113 nsresult SetAnimValue(const SMILValue& aValue) override; 114 }; 115 }; 116 117 } // namespace mozilla 118 119 #endif // DOM_SVG_SVGANIMATEDENUMERATION_H_