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