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