1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/// <reference path='./import.ts' /> 17class ImageAnimatorImagesModifier extends ModifierWithKey<Array<ImageFrameInfo>> { 18 constructor(value: Array<ImageFrameInfo>) { 19 super(value); 20 } 21 static identity: Symbol = Symbol('imageAnimatorImages'); 22 applyPeer(node: KNode, reset: boolean): void { 23 if (reset) { 24 getUINativeModule().imageAnimator.resetImages(node); 25 } else { 26 let arkImageFrame: ArkImageFrameInfoToArray = this.convertImageFrames(this.value); 27 if (!arkImageFrame) { 28 getUINativeModule().imageAnimator.resetImages(node); 29 } else { 30 getUINativeModule().imageAnimator.setImages(node, arkImageFrame.arrSrc, 31 arkImageFrame.arrWidth, arkImageFrame.arrHeight, arkImageFrame.arrTop, arkImageFrame.arrLeft, 32 arkImageFrame.arrDuration, arkImageFrame.arrSrc.length); 33 } 34 } 35 } 36 37 checkObjectDiff(): boolean { 38 let checkDiff = true; 39 if (this.value && this.value.length > 0 && 40 this.stageValue && this.stageValue.length > 0 && 41 this.value.length === this.stageValue.length) { 42 let checkItemEqual: Boolean = false; 43 44 for (let i: number = 0; i < this.value.length; i++) { 45 checkItemEqual = this.isEqual(this.stageValue[i], this.value[i]); 46 if (!checkItemEqual) { 47 checkDiff = !checkItemEqual; 48 break; 49 } 50 } 51 } 52 return checkDiff; 53 } 54 55 isEqual(one: ImageFrameInfo, another: ImageFrameInfo): boolean { 56 if (!(one.width === another.width && 57 one.height === another.height && 58 one.top === another.top && 59 one.left === another.left && 60 one.duration === another.duration)) { 61 return true; 62 } else { 63 return !isBaseOrResourceEqual(one.src, another.src); 64 } 65 } 66 67 convertImageFrames(value: Array<ImageFrameInfo>): ArkImageFrameInfoToArray { 68 if (value && value.length > 0) { 69 let isFlag: Boolean = true; 70 for (let item of value) { 71 if (item.src === undefined || item.src === null) { 72 isFlag = false; 73 break; 74 } 75 } 76 if (isFlag) { 77 let array: ArkImageFrameInfoToArray = new ArkImageFrameInfoToArray(); 78 for (let item of value) { 79 array.arrSrc.push(<string>item.src); 80 array.arrWidth.push((item.width === undefined || item.width === null) ? 0 : item.width); 81 array.arrHeight.push((item.height === undefined || item.height === null) ? 0 : item.height); 82 array.arrTop.push((item.top === undefined || item.top === null) ? 0 : item.top); 83 array.arrLeft.push((item.left === undefined || item.left === null) ? 0 : item.left); 84 array.arrDuration.push((item.duration === undefined || item.duration === null) ? 0 : item.duration); 85 } 86 return array; 87 } else { 88 return undefined; 89 } 90 } else { 91 return undefined; 92 } 93 } 94} 95 96class ImageAnimatorDurationModifier extends ModifierWithKey<number> { 97 constructor(value: number) { 98 super(value); 99 } 100 static identity: Symbol = Symbol('imageAnimatorDuration'); 101 applyPeer(node: KNode, reset: boolean): void { 102 if (reset) { 103 getUINativeModule().imageAnimator.resetDuration(node); 104 } else { 105 getUINativeModule().imageAnimator.setDuration(node, this.value); 106 } 107 } 108 checkObjectDiff(): boolean { 109 return this.stageValue !== this.value; 110 } 111} 112 113class ImageAnimatorReverseModifier extends ModifierWithKey<boolean> { 114 constructor(value: boolean) { 115 super(value); 116 } 117 static identity: Symbol = Symbol('imageAnimatorReverse'); 118 applyPeer(node: KNode, reset: boolean): void { 119 if (reset) { 120 getUINativeModule().imageAnimator.resetReverse(node); 121 } else { 122 getUINativeModule().imageAnimator.setReverse(node, this.value); 123 } 124 } 125 checkObjectDiff(): boolean { 126 return this.stageValue !== this.value; 127 } 128} 129 130class ImageAnimatorStateModifier extends ModifierWithKey<AnimationStatus> { 131 constructor(value: AnimationStatus) { 132 super(value); 133 } 134 static identity: Symbol = Symbol('imageAnimatorState'); 135 applyPeer(node: KNode, reset: boolean): void { 136 if (reset) { 137 getUINativeModule().imageAnimator.resetState(node); 138 } else { 139 getUINativeModule().imageAnimator.setState(node, this.value); 140 } 141 } 142 checkObjectDiff(): boolean { 143 return this.stageValue !== this.value; 144 } 145} 146 147class ImageAnimatorFixedSizeModifier extends ModifierWithKey<boolean> { 148 constructor(value: boolean) { 149 super(value); 150 } 151 static identity: Symbol = Symbol('imageAnimatorFixedSize'); 152 applyPeer(node: KNode, reset: boolean): void { 153 if (reset) { 154 getUINativeModule().imageAnimator.resetFixedSize(node); 155 } else { 156 getUINativeModule().imageAnimator.setFixedSize(node, this.value); 157 } 158 } 159 checkObjectDiff(): boolean { 160 return this.stageValue !== this.value; 161 } 162} 163 164class ImageAnimatorFillModeModifier extends ModifierWithKey<FillMode> { 165 constructor(value: FillMode) { 166 super(value); 167 } 168 static identity: Symbol = Symbol('imageAnimatorFillMode'); 169 applyPeer(node: KNode, reset: boolean): void { 170 if (reset) { 171 getUINativeModule().imageAnimator.resetFillMode(node); 172 } else { 173 getUINativeModule().imageAnimator.setFillMode(node, this.value); 174 } 175 } 176 checkObjectDiff(): boolean { 177 return this.stageValue !== this.value; 178 } 179} 180 181class ImageAnimatorIterationsModeModifier extends ModifierWithKey<number> { 182 constructor(value: number) { 183 super(value); 184 } 185 static identity: Symbol = Symbol('imageAnimatorIterationsMode'); 186 applyPeer(node: KNode, reset: boolean): void { 187 if (reset) { 188 getUINativeModule().imageAnimator.resetIterations(node); 189 } else { 190 getUINativeModule().imageAnimator.setIterations(node, this.value); 191 } 192 } 193 checkObjectDiff(): boolean { 194 return this.stageValue !== this.value; 195 } 196} 197 198class ArkImageAnimatorComponent extends ArkComponent implements CommonMethod<ImageAnimatorAttribute> { 199 constructor(nativePtr: KNode) { 200 super(nativePtr); 201 } 202 images(value: Array<ImageFrameInfo>): ImageAnimatorAttribute { 203 modifierWithKey(this._modifiersWithKeys, ImageAnimatorImagesModifier.identity, 204 ImageAnimatorImagesModifier, value); 205 return this; 206 } 207 state(value: AnimationStatus): ImageAnimatorAttribute { 208 modifierWithKey(this._modifiersWithKeys, ImageAnimatorStateModifier.identity, 209 ImageAnimatorStateModifier, value); 210 return this; 211 } 212 duration(value: number): ImageAnimatorAttribute { 213 modifierWithKey(this._modifiersWithKeys, ImageAnimatorDurationModifier.identity, 214 ImageAnimatorDurationModifier, value); 215 return this; 216 } 217 reverse(value: boolean): ImageAnimatorAttribute { 218 modifierWithKey(this._modifiersWithKeys, ImageAnimatorReverseModifier.identity, 219 ImageAnimatorReverseModifier, value); 220 return this; 221 } 222 fixedSize(value: boolean): ImageAnimatorAttribute { 223 modifierWithKey(this._modifiersWithKeys, ImageAnimatorFixedSizeModifier.identity, 224 ImageAnimatorFixedSizeModifier, value); 225 return this; 226 } 227 preDecode(value: number): ImageAnimatorAttribute { 228 throw new Error('Method not implemented.'); 229 } 230 fillMode(value: FillMode): ImageAnimatorAttribute { 231 modifierWithKey(this._modifiersWithKeys, ImageAnimatorFillModeModifier.identity, 232 ImageAnimatorFillModeModifier, value); 233 return this; 234 } 235 iterations(value: number): ImageAnimatorAttribute { 236 modifierWithKey(this._modifiersWithKeys, ImageAnimatorIterationsModeModifier.identity, 237 ImageAnimatorIterationsModeModifier, value); 238 return this; 239 } 240 onStart(event: () => void): ImageAnimatorAttribute { 241 throw new Error('Method not implemented.'); 242 } 243 onPause(event: () => void): ImageAnimatorAttribute { 244 throw new Error('Method not implemented.'); 245 } 246 onRepeat(event: () => void): ImageAnimatorAttribute { 247 throw new Error('Method not implemented.'); 248 } 249 onCancel(event: () => void): ImageAnimatorAttribute { 250 throw new Error('Method not implemented.'); 251 } 252 onFinish(event: () => void): ImageAnimatorAttribute { 253 throw new Error('Method not implemented.'); 254 } 255} 256// @ts-ignore 257globalThis.ImageAnimator.attributeModifier = function (modifier) { 258 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 259 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 260 let component = this.createOrGetNode(elmtId, () => { 261 return new ArkImageAnimatorComponent(nativeNode); 262 }); 263 applyUIAttributes(modifier, nativeNode, component); 264 component.applyModifierPatch(); 265}; 266