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 ArkTextTimerComponent extends ArkComponent implements TextTimerAttribute { 18 builder: WrappedBuilder<Object[]> | null = null; 19 textTimerNode: BuilderNode<[TextTimerConfiguration]> | null = null; 20 modifier: ContentModifier<TextTimerConfiguration>; 21 constructor(nativePtr: KNode, classType?: ModifierType) { 22 super(nativePtr, classType); 23 } 24 allowChildCount(): number { 25 return 0; 26 } 27 initialize(value: Object[]): this { 28 if (value.length === 1 && isObject(value[0])) { 29 modifierWithKey(this._modifiersWithKeys, TextTimerOptionsModifier.identity, TextTimerOptionsModifier, value[0]); 30 } else { 31 modifierWithKey(this._modifiersWithKeys, TextTimerOptionsModifier.identity, TextTimerOptionsModifier, undefined); 32 } 33 return this; 34 } 35 fontColor(value: any): this { 36 modifierWithKey(this._modifiersWithKeys, TextTimerFontColorModifier.identity, TextTimerFontColorModifier, value); 37 return this; 38 } 39 40 fontSize(value: any): this { 41 modifierWithKey(this._modifiersWithKeys, TextTimerFontSizeModifier.identity, TextTimerFontSizeModifier, value); 42 return this; 43 } 44 45 fontWeight(value: number | FontWeight | string): this { 46 modifierWithKey(this._modifiersWithKeys, TextTimerFontWeightModifier.identity, TextTimerFontWeightModifier, value); 47 return this; 48 } 49 50 fontStyle(value: FontStyle): this { 51 modifierWithKey(this._modifiersWithKeys, TextTimerFontStyleModifier.identity, TextTimerFontStyleModifier, value); 52 return this; 53 } 54 55 fontFamily(value: string | Resource): this { 56 modifierWithKey(this._modifiersWithKeys, TextTimerFontFamilyModifier.identity, TextTimerFontFamilyModifier, value); 57 return this; 58 } 59 60 format(value: string): this { 61 modifierWithKey(this._modifiersWithKeys, TextTimerFormatModifier.identity, TextTimerFormatModifier, value); 62 return this; 63 } 64 65 textShadow(value: ShadowOptions): this { 66 modifierWithKey(this._modifiersWithKeys, TextTimerTextShadowModifier.identity, TextTimerTextShadowModifier, value); 67 return this; 68 } 69 70 contentModifier(value: ContentModifier<TextTimerConfiguration>): this { 71 modifierWithKey(this._modifiersWithKeys, TextTimerContentModifier.identity, TextTimerContentModifier, value); 72 return this; 73 } 74 75 setContentModifier(modifier: ContentModifier<TextTimerConfiguration>): this { 76 if (modifier === undefined || modifier === null) { 77 getUINativeModule().textTimer.setContentModifierBuilder(this.nativePtr, false); 78 return; 79 } 80 this.builder = modifier.applyContent(); 81 this.modifier = modifier; 82 getUINativeModule().textTimer.setContentModifierBuilder(this.nativePtr, this); 83 } 84 85 makeContentModifierNode(context: UIContext, textTimerConfiguration: TextTimerConfiguration): FrameNode | null { 86 textTimerConfiguration.contentModifier = this.modifier; 87 if (isUndefined(this.textTimerNode)) { 88 const xNode = globalThis.requireNapi('arkui.node'); 89 this.textTimerNode = new xNode.BuilderNode(context); 90 this.textTimerNode.build(this.builder, textTimerConfiguration); 91 } else { 92 this.textTimerNode.update(textTimerConfiguration); 93 } 94 return this.textTimerNode.getFrameNode(); 95 } 96 97 98 onTimer(event: (utc: number, elapsedTime: number) => void): this { 99 modifierWithKey(this._modifiersWithKeys, TextTimerOnTimerModifier.identity, TextTimerOnTimerModifier, event); 100 return this; 101 } 102} 103 104class TextTimerOnTimerModifier extends ModifierWithKey<(utc: number, elapsedTime: number) => void> { 105 constructor(value: (utc: number, elapsedTime: number) => void) { 106 super(value); 107 } 108 static identity: Symbol = Symbol('textTimerOnTimer'); 109 applyPeer(node: KNode, reset: boolean): void { 110 if (reset) { 111 getUINativeModule().textTimer.resetTextTimerOnTimer(node); 112 } else { 113 getUINativeModule().textTimer.setTextTimerOnTimer(node, this.value); 114 } 115 } 116} 117 118class TextTimerOptionsModifier extends ModifierWithKey<TextTimerOptions> { 119 constructor(value: TextTimerOptions) { 120 super(value); 121 } 122 static identity: Symbol = Symbol('textTimerOptions'); 123 applyPeer(node: KNode, reset: boolean): void { 124 if (reset) { 125 getUINativeModule().textTimer.setTextTimerOptions(node, undefined, undefined, undefined); 126 } else { 127 getUINativeModule().textTimer.setTextTimerOptions(node, this.value?.isCountDown, this.value?.count, this.value?.controller); 128 } 129 } 130 checkObjectDiff(): boolean { 131 return !isBaseOrResourceEqual(this.stageValue?.isCountDown, this.value?.isCountDown) || 132 !isBaseOrResourceEqual(this.stageValue?.count, this.value?.count) || 133 !isBaseOrResourceEqual(this.stageValue?.controller, this.value?.controller); 134 } 135} 136 137class TextTimerFontColorModifier extends ModifierWithKey<ResourceColor> { 138 static identity: Symbol = Symbol('fontColor'); 139 applyPeer(node: KNode, reset: boolean): void { 140 if (reset) { 141 getUINativeModule().textTimer.resetFontColor(node); 142 } else { 143 getUINativeModule().textTimer.setFontColor(node, this.value); 144 } 145 } 146 checkObjectDiff(): boolean { 147 return !isBaseOrResourceEqual(this.stageValue, this.value); 148 } 149} 150 151class TextTimerFontSizeModifier extends ModifierWithKey<Length> { 152 static identity: Symbol = Symbol('fontSize'); 153 applyPeer(node: KNode, reset: boolean): void { 154 if (reset) { 155 getUINativeModule().textTimer.resetFontSize(node); 156 } else { 157 getUINativeModule().textTimer.setFontSize(node, this.value); 158 } 159 } 160 checkObjectDiff(): boolean { 161 return !isBaseOrResourceEqual(this.stageValue, this.value); 162 } 163} 164 165class TextTimerFontWeightModifier extends ModifierWithKey<number | FontWeight | string> { 166 static identity: Symbol = Symbol('fontWeight'); 167 applyPeer(node: KNode, reset: boolean): void { 168 if (reset) { 169 getUINativeModule().textTimer.resetFontWeight(node); 170 } else { 171 getUINativeModule().textTimer.setFontWeight(node, this.value); 172 } 173 } 174} 175 176class TextTimerFontStyleModifier extends ModifierWithKey<FontStyle> { 177 static identity: Symbol = Symbol('fontStyle'); 178 applyPeer(node: KNode, reset: boolean): void { 179 if (reset) { 180 getUINativeModule().textTimer.resetFontStyle(node); 181 } else { 182 getUINativeModule().textTimer.setFontStyle(node, this.value); 183 } 184 } 185 checkObjectDiff(): boolean { 186 return !isBaseOrResourceEqual(this.stageValue, this.value); 187 } 188} 189 190class TextTimerFontFamilyModifier extends ModifierWithKey<string | Resource> { 191 static identity: Symbol = Symbol('fontFamily'); 192 applyPeer(node: KNode, reset: boolean): void { 193 if (reset) { 194 getUINativeModule().textTimer.resetFontFamily(node); 195 } else { 196 getUINativeModule().textTimer.setFontFamily(node, this.value); 197 } 198 } 199 checkObjectDiff(): boolean { 200 return !isBaseOrResourceEqual(this.stageValue, this.value); 201 } 202} 203 204class TextTimerFormatModifier extends ModifierWithKey<string> { 205 static identity: Symbol = Symbol('textTimerFormat'); 206 applyPeer(node: KNode, reset: boolean): void { 207 if (reset) { 208 getUINativeModule().textTimer.resetFormat(node); 209 } else { 210 getUINativeModule().textTimer.setFormat(node, this.value); 211 } 212 } 213} 214 215class TextTimerContentModifier extends ModifierWithKey<ContentModifier<TextTimerConfiguration>> { 216 constructor(value: ContentModifier<TextTimerConfiguration>) { 217 super(value); 218 } 219 static identity: Symbol = Symbol('textTimerContentModifier'); 220 applyPeer(node: KNode, reset: boolean, component: ArkComponent) { 221 let textTimerComponent = component as ArkTextTimerComponent; 222 textTimerComponent.setContentModifier(this.value); 223 } 224} 225 226class TextTimerTextShadowModifier extends ModifierWithKey<ShadowOptions | Array<ShadowOptions>> { 227 constructor(value: ShadowOptions | Array<ShadowOptions>) { 228 super(value); 229 } 230 static identity: Symbol = Symbol('textTimerTextShadow'); 231 applyPeer(node: KNode, reset: boolean): void { 232 if (reset) { 233 getUINativeModule().textTimer.resetTextShadow(node); 234 } else { 235 getUINativeModule().textTimer.setTextShadow(node, this.value!); 236 } 237 } 238 239 checkObjectDiff(): boolean { 240 let checkDiff = true; 241 let arkShadow = new ArkShadowInfoToArray(); 242 if (Object.getPrototypeOf(this.stageValue).constructor === Object && 243 Object.getPrototypeOf(this.value).constructor === Object) { 244 checkDiff = arkShadow.checkDiff(<ShadowOptions> this.stageValue, <ShadowOptions> this.value); 245 } else if (Object.getPrototypeOf(this.stageValue).constructor === Array && 246 Object.getPrototypeOf(this.value).constructor === Array && 247 (<Array<ShadowOptions>> this.stageValue).length === (<Array<ShadowOptions>> this.value).length) { 248 let isDiffItem = false; 249 for (let i: number = 0; i < (<Array<ShadowOptions>> this.value).length; i++) { 250 if (arkShadow.checkDiff(this.stageValue[i], this.value[1])) { 251 isDiffItem = true; 252 break; 253 } 254 } 255 if (!isDiffItem) { 256 checkDiff = false; 257 } 258 } 259 return checkDiff; 260 } 261} 262 263// @ts-ignore 264globalThis.TextTimer.attributeModifier = function (modifier: ArkComponent): void { 265 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 266 return new ArkTextTimerComponent(nativePtr); 267 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 268 return new modifierJS.TextTimerModifier(nativePtr, classType); 269 }); 270}; 271 272// @ts-ignore 273globalThis.TextTimer.contentModifier = function (modifier) { 274 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 275 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 276 let component = this.createOrGetNode(elmtId, () => { 277 return new ArkTextTimerComponent(nativeNode); 278 }); 279 component.setContentModifier(modifier); 280};