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 ArkTextClockComponent extends ArkComponent implements TextClockAttribute { 18 builder: WrappedBuilder<Object[]> | null = null; 19 textClockNode: BuilderNode<[TextClockConfiguration]> | null = null; 20 modifier: ContentModifier<TextClockConfiguration>; 21 needRebuild: Boolean = false; 22 23 constructor(nativePtr: KNode, classType?: ModifierType) { 24 super(nativePtr, classType); 25 } 26 allowChildCount(): number { 27 return 0; 28 } 29 initialize(value: Object[]): this { 30 if (value.length === 1 && isObject(value[0])) { 31 if (value[0]?.timeZoneOffset !== undefined) { 32 modifierWithKey(this._modifiersWithKeys, TextClockTimeZoneOffsetModifier.identity, TextClockTimeZoneOffsetModifier, value[0].timeZoneOffset); 33 } else { 34 modifierWithKey(this._modifiersWithKeys, TextClockTimeZoneOffsetModifier.identity, TextClockTimeZoneOffsetModifier, undefined); 35 } 36 if (value[0]?.controller !== undefined) { 37 modifierWithKey(this._modifiersWithKeys, TextClockControllerModifier.identity, TextClockControllerModifier, value[0].controller); 38 } else { 39 modifierWithKey(this._modifiersWithKeys, TextClockControllerModifier.identity, TextClockControllerModifier, undefined); 40 } 41 } else { 42 modifierWithKey(this._modifiersWithKeys, TextClockTimeZoneOffsetModifier.identity, TextClockTimeZoneOffsetModifier, undefined); 43 modifierWithKey(this._modifiersWithKeys, TextClockControllerModifier.identity, TextClockControllerModifier, undefined); 44 } 45 return this; 46 } 47 format(value: string): this { 48 modifierWithKey(this._modifiersWithKeys, TextClockFormatModifier.identity, TextClockFormatModifier, value); 49 return this; 50 } 51 onDateChange(event: (value: number) => void): this { 52 modifierWithKey(this._modifiersWithKeys, TextClockOnDateChangeModifier.identity, TextClockOnDateChangeModifier, event); 53 return this; 54 } 55 fontColor(value: ResourceColor): this { 56 modifierWithKey(this._modifiersWithKeys, TextClockFontColorModifier.identity, TextClockFontColorModifier, value); 57 return this; 58 } 59 fontSize(value: Length): this { 60 modifierWithKey(this._modifiersWithKeys, TextClockFontSizeModifier.identity, TextClockFontSizeModifier, value); 61 return this; 62 } 63 fontStyle(value: FontStyle): this { 64 modifierWithKey(this._modifiersWithKeys, TextClockFontStyleModifier.identity, TextClockFontStyleModifier, value); 65 return this; 66 } 67 fontWeight(value: string | number | FontWeight): this { 68 modifierWithKey(this._modifiersWithKeys, TextClockFontWeightModifier.identity, TextClockFontWeightModifier, value); 69 return this; 70 } 71 fontFamily(value: ResourceStr): this { 72 modifierWithKey(this._modifiersWithKeys, TextClockFontFamilyModifier.identity, TextClockFontFamilyModifier, value); 73 return this; 74 } 75 textShadow(value: ShadowOptions): this { 76 modifierWithKey(this._modifiersWithKeys, TextClockTextShadowModifier.identity, TextClockTextShadowModifier, value); 77 return this; 78 } 79 fontFeature(value: string): this { 80 modifierWithKey(this._modifiersWithKeys, TextClockFontFeatureModifier.identity, TextClockFontFeatureModifier, value); 81 return this; 82 } 83 contentModifier(value: ContentModifier<TextClockConfiguration>): this { 84 modifierWithKey(this._modifiersWithKeys, TextClockContentModifier.identity, TextClockContentModifier, value); 85 return this; 86 } 87 setContentModifier(modifier: ContentModifier<TextClockConfiguration>): this { 88 if (modifier === undefined || modifier === null) { 89 getUINativeModule().textClock.setContentModifierBuilder(this.nativePtr, false); 90 return; 91 } 92 this.needRebuild = false; 93 if (this.builder !== modifier.applyContent()) { 94 this.needRebuild = true; 95 } 96 this.builder = modifier.applyContent(); 97 this.modifier = modifier; 98 getUINativeModule().textClock.setContentModifierBuilder(this.nativePtr, this); 99 } 100 makeContentModifierNode(context: UIContext, textClockConfiguration: TextClockConfiguration): FrameNode | null { 101 textClockConfiguration.contentModifier = this.modifier; 102 if (isUndefined(this.textClockNode) || this.needRebuild) { 103 const xNode = globalThis.requireNapi('arkui.node'); 104 this.textClockNode = new xNode.BuilderNode(context); 105 this.textClockNode.build(this.builder, textClockConfiguration); 106 this.needRebuild = false; 107 } else { 108 this.textClockNode.update(textClockConfiguration); 109 } 110 return this.textClockNode.getFrameNode(); 111 } 112 dateTimeOptions(value: DateTimeOptions): this { 113 modifierWithKey(this._modifiersWithKeys, TextClockDateTimeOptionsModifier.identity, 114 TextClockDateTimeOptionsModifier, value); 115 return this; 116 } 117} 118 119class TextClockTimeZoneOffsetModifier extends ModifierWithKey<number> { 120 constructor(value: number) { 121 super(value); 122 } 123 static identity: Symbol = Symbol('textClockTimeZoneOffset'); 124 applyPeer(node: KNode, reset: boolean): void { 125 if (reset) { 126 getUINativeModule().textClock.setTextClockTimeZoneOffset(node, undefined); 127 } else { 128 getUINativeModule().textClock.setTextClockTimeZoneOffset(node, this.value); 129 } 130 } 131 checkObjectDiff(): boolean { 132 return !isBaseOrResourceEqual(this.stageValue, this.value); 133 } 134} 135 136class TextClockControllerModifier extends ModifierWithKey<TextClockController> { 137 constructor(value: TextClockController) { 138 super(value); 139 } 140 static identity: Symbol = Symbol('textClockController'); 141 applyPeer(node: KNode, reset: boolean): void { 142 if (reset) { 143 getUINativeModule().textClock.setTextClockController(node, undefined); 144 } else { 145 getUINativeModule().textClock.setTextClockController(node, this.value); 146 } 147 } 148 checkObjectDiff(): boolean { 149 return !isBaseOrResourceEqual(this.stageValue, this.value); 150 } 151} 152 153class TextClockFormatModifier extends ModifierWithKey<string> { 154 constructor(value: string) { 155 super(value); 156 } 157 static identity: Symbol = Symbol('textClockFormat'); 158 applyPeer(node: KNode, reset: boolean): void { 159 if (reset) { 160 getUINativeModule().textClock.resetFormat(node); 161 } else { 162 getUINativeModule().textClock.setFormat(node, this.value); 163 } 164 } 165} 166 167class TextClockFontColorModifier extends ModifierWithKey<ResourceColor> { 168 constructor(value: ResourceColor) { 169 super(value); 170 } 171 static identity: Symbol = Symbol('textClockFontColor'); 172 applyPeer(node: KNode, reset: boolean): void { 173 if (reset) { 174 getUINativeModule().textClock.resetFontColor(node); 175 } else { 176 getUINativeModule().textClock.setFontColor(node, this.value); 177 } 178 } 179 180 checkObjectDiff(): boolean { 181 return !isBaseOrResourceEqual(this.stageValue, this.value); 182 } 183} 184 185class TextClockFontSizeModifier extends ModifierWithKey<Length> { 186 constructor(value: Length) { 187 super(value); 188 } 189 static identity: Symbol = Symbol('textClockFontSize'); 190 applyPeer(node: KNode, reset: boolean): void { 191 if (reset) { 192 getUINativeModule().textClock.resetFontSize(node); 193 } else { 194 getUINativeModule().textClock.setFontSize(node, this.value); 195 } 196 } 197 checkObjectDiff(): boolean { 198 return !isBaseOrResourceEqual(this.stageValue, this.value); 199 } 200} 201 202class TextClockFontStyleModifier extends ModifierWithKey<FontStyle> { 203 constructor(value: FontStyle) { 204 super(value); 205 } 206 static identity: Symbol = Symbol('textClockFontStyle'); 207 applyPeer(node: KNode, reset: boolean): void { 208 if (reset) { 209 getUINativeModule().textClock.resetFontStyle(node); 210 } else { 211 getUINativeModule().textClock.setFontStyle(node, this.value!); 212 } 213 } 214} 215 216class TextClockFontWeightModifier extends ModifierWithKey<number | FontWeight | string> { 217 constructor(value: number | FontWeight | string) { 218 super(value); 219 } 220 static identity: Symbol = Symbol('textClockFontWeight'); 221 applyPeer(node: KNode, reset: boolean): void { 222 if (reset) { 223 getUINativeModule().textClock.resetFontWeight(node); 224 } else { 225 getUINativeModule().textClock.setFontWeight(node, this.value!); 226 } 227 } 228} 229 230class TextClockTextShadowModifier extends ModifierWithKey<ShadowOptions | Array<ShadowOptions>> { 231 constructor(value: ShadowOptions | Array<ShadowOptions>) { 232 super(value); 233 } 234 static identity: Symbol = Symbol('textClockTextShadow'); 235 applyPeer(node: KNode, reset: boolean): void { 236 if (reset) { 237 getUINativeModule().textClock.resetTextShadow(node); 238 } else { 239 getUINativeModule().textClock.setTextShadow(node, this.value!); 240 } 241 } 242 243 checkObjectDiff(): boolean { 244 let checkDiff = true; 245 let arkShadow = new ArkShadowInfoToArray(); 246 if (Object.getPrototypeOf(this.stageValue).constructor === Object && 247 Object.getPrototypeOf(this.value).constructor === Object) { 248 checkDiff = arkShadow.checkDiff(<ShadowOptions> this.stageValue, <ShadowOptions> this.value); 249 } else if (Object.getPrototypeOf(this.stageValue).constructor === Array && 250 Object.getPrototypeOf(this.value).constructor === Array && 251 (<Array<ShadowOptions>> this.stageValue).length === (<Array<ShadowOptions>> this.value).length) { 252 let isDiffItem = false; 253 for (let i: number = 0; i < (<Array<ShadowOptions>> this.value).length; i++) { 254 if (arkShadow.checkDiff(this.stageValue[i], this.value[1])) { 255 isDiffItem = true; 256 break; 257 } 258 } 259 if (!isDiffItem) { 260 checkDiff = false; 261 } 262 } 263 return checkDiff; 264 } 265} 266 267class TextClockFontFeatureModifier extends ModifierWithKey<FontFeature> { 268 constructor(value: FontFeature) { 269 super(value); 270 } 271 static identity: Symbol = Symbol('textClockFontFeature'); 272 applyPeer(node: KNode, reset: boolean): void { 273 if (reset) { 274 getUINativeModule().textClock.resetFontFeature(node); 275 } else { 276 getUINativeModule().textClock.setFontFeature(node, this.value!); 277 } 278 } 279 checkObjectDiff(): boolean { 280 return !isBaseOrResourceEqual(this.stageValue, this.value); 281 } 282} 283 284class TextClockContentModifier extends ModifierWithKey<ContentModifier<TextClockConfiguration>> { 285 constructor(value: ContentModifier<TextClockConfiguration>) { 286 super(value); 287 } 288 static identity: Symbol = Symbol('textClockContentModifier'); 289 applyPeer(node: KNode, reset: boolean, component: ArkComponent) { 290 let textClockComponent = component as ArkTextClockComponent; 291 textClockComponent.setContentModifier(this.value); 292 } 293} 294 295class TextClockFontFamilyModifier extends ModifierWithKey<ResourceStr> { 296 constructor(value: ResourceStr) { 297 super(value); 298 } 299 static identity: Symbol = Symbol('textClockFontFamily'); 300 applyPeer(node: KNode, reset: boolean): void { 301 if (reset) { 302 getUINativeModule().textClock.resetFontFamily(node); 303 } else { 304 getUINativeModule().textClock.setFontFamily(node, this.value!); 305 } 306 } 307 308 checkObjectDiff(): boolean { 309 return !isBaseOrResourceEqual(this.stageValue, this.value); 310 } 311} 312 313class TextClockDateTimeOptionsModifier extends ModifierWithKey<DateTimeOptions> { 314 constructor(value: DateTimeOptions) { 315 super(value); 316 } 317 static identity: Symbol = Symbol('textClockDateTimeOptions'); 318 applyPeer(node: KNode, reset: boolean): void { 319 if (reset) { 320 getUINativeModule().textClock.resetDateTimeOptions(node); 321 } else { 322 getUINativeModule().textClock.setDateTimeOptions(node, this.value.hour); 323 } 324 } 325 326 checkObjectDiff(): boolean { 327 return !isBaseOrResourceEqual(this.stageValue, this.value); 328 } 329} 330 331declare type OnDateChangeCallback = (value: number) => void; 332class TextClockOnDateChangeModifier extends ModifierWithKey<OnDateChangeCallback> { 333 constructor(value: OnDateChangeCallback) { 334 super(value); 335 } 336 static identity: Symbol = Symbol('textClockOnDateChange'); 337 applyPeer(node: KNode, reset: boolean): void { 338 if (reset) { 339 getUINativeModule().textClock.resetTextClockOnDateChange(node); 340 } else { 341 getUINativeModule().textClock.setTextClockOnDateChange(node, this.value); 342 } 343 } 344} 345// @ts-ignore 346globalThis.TextClock.attributeModifier = function (modifier: ArkComponent): void { 347 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 348 return new ArkTextClockComponent(nativePtr); 349 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 350 return new modifierJS.TextClockModifier(nativePtr, classType); 351 }); 352}; 353 354// @ts-ignore 355globalThis.TextClock.contentModifier = function (modifier: ContentModifier<TextClockConfiguration>) { 356 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 357 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 358 let component = this.createOrGetNode(elmtId, () => { 359 return new ArkTextClockComponent(nativeNode); 360 }); 361 component.setContentModifier(modifier); 362}; 363