• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    throw new Error('Method not implemented.');
100  }
101}
102
103class TextTimerOptionsModifier extends ModifierWithKey<TextTimerOptions> {
104  constructor(value: TextTimerOptions) {
105    super(value);
106  }
107  static identity: Symbol = Symbol('textTimerOptions');
108  applyPeer(node: KNode, reset: boolean): void {
109    if (reset) {
110      getUINativeModule().textTimer.setTextTimerOptions(node, undefined, undefined, undefined);
111    } else {
112      getUINativeModule().textTimer.setTextTimerOptions(node, this.value?.isCountDown, this.value?.count, this.value?.controller);
113    }
114  }
115  checkObjectDiff(): boolean {
116    return !isBaseOrResourceEqual(this.stageValue?.isCountDown, this.value?.isCountDown) ||
117          !isBaseOrResourceEqual(this.stageValue?.count, this.value?.count) ||
118          !isBaseOrResourceEqual(this.stageValue?.controller, this.value?.controller);
119  }
120}
121
122class TextTimerFontColorModifier extends ModifierWithKey<ResourceColor> {
123  static identity: Symbol = Symbol('fontColor');
124  applyPeer(node: KNode, reset: boolean): void {
125    if (reset) {
126      getUINativeModule().textTimer.resetFontColor(node);
127    } else {
128      getUINativeModule().textTimer.setFontColor(node, this.value);
129    }
130  }
131  checkObjectDiff(): boolean {
132    return !isBaseOrResourceEqual(this.stageValue, this.value);
133  }
134}
135
136class TextTimerFontSizeModifier extends ModifierWithKey<Length> {
137  static identity: Symbol = Symbol('fontSize');
138  applyPeer(node: KNode, reset: boolean): void {
139    if (reset) {
140      getUINativeModule().textTimer.resetFontSize(node);
141    } else {
142      getUINativeModule().textTimer.setFontSize(node, this.value);
143    }
144  }
145  checkObjectDiff(): boolean {
146    return !isBaseOrResourceEqual(this.stageValue, this.value);
147  }
148}
149
150class TextTimerFontWeightModifier extends ModifierWithKey<number | FontWeight | string> {
151  static identity: Symbol = Symbol('fontWeight');
152  applyPeer(node: KNode, reset: boolean): void {
153    if (reset) {
154      getUINativeModule().textTimer.resetFontWeight(node);
155    } else {
156      getUINativeModule().textTimer.setFontWeight(node, this.value);
157    }
158  }
159}
160
161class TextTimerFontStyleModifier extends ModifierWithKey<FontStyle> {
162  static identity: Symbol = Symbol('fontStyle');
163  applyPeer(node: KNode, reset: boolean): void {
164    if (reset) {
165      getUINativeModule().textTimer.resetFontStyle(node);
166    } else {
167      getUINativeModule().textTimer.setFontStyle(node, this.value);
168    }
169  }
170  checkObjectDiff(): boolean {
171    return !isBaseOrResourceEqual(this.stageValue, this.value);
172  }
173}
174
175class TextTimerFontFamilyModifier extends ModifierWithKey<string | Resource> {
176  static identity: Symbol = Symbol('fontFamily');
177  applyPeer(node: KNode, reset: boolean): void {
178    if (reset) {
179      getUINativeModule().textTimer.resetFontFamily(node);
180    } else {
181      getUINativeModule().textTimer.setFontFamily(node, this.value);
182    }
183  }
184  checkObjectDiff(): boolean {
185    return !isBaseOrResourceEqual(this.stageValue, this.value);
186  }
187}
188
189class TextTimerFormatModifier extends ModifierWithKey<string> {
190  static identity: Symbol = Symbol('textTimerFormat');
191  applyPeer(node: KNode, reset: boolean): void {
192    if (reset) {
193      getUINativeModule().textTimer.resetFormat(node);
194    } else {
195      getUINativeModule().textTimer.setFormat(node, this.value);
196    }
197  }
198}
199
200class TextTimerContentModifier extends ModifierWithKey<ContentModifier<TextTimerConfiguration>> {
201  constructor(value: ContentModifier<TextTimerConfiguration>) {
202    super(value);
203  }
204  static identity: Symbol = Symbol('textTimerContentModifier');
205  applyPeer(node: KNode, reset: boolean, component: ArkComponent) {
206    let textTimerComponent = component as ArkTextTimerComponent;
207    textTimerComponent.setContentModifier(this.value);
208  }
209}
210
211class TextTimerTextShadowModifier extends ModifierWithKey<ShadowOptions | Array<ShadowOptions>> {
212  constructor(value: ShadowOptions | Array<ShadowOptions>) {
213    super(value);
214  }
215  static identity: Symbol = Symbol('textTimerTextShadow');
216  applyPeer(node: KNode, reset: boolean): void {
217    if (reset) {
218      getUINativeModule().textTimer.resetTextShadow(node);
219    } else {
220      getUINativeModule().textTimer.setTextShadow(node, this.value!);
221    }
222  }
223
224  checkObjectDiff(): boolean {
225    let checkDiff = true;
226    let arkShadow = new ArkShadowInfoToArray();
227    if (Object.getPrototypeOf(this.stageValue).constructor === Object &&
228      Object.getPrototypeOf(this.value).constructor === Object) {
229      checkDiff = arkShadow.checkDiff(<ShadowOptions> this.stageValue, <ShadowOptions> this.value);
230    } else if (Object.getPrototypeOf(this.stageValue).constructor === Array &&
231      Object.getPrototypeOf(this.value).constructor === Array &&
232      (<Array<ShadowOptions>> this.stageValue).length === (<Array<ShadowOptions>> this.value).length) {
233      let isDiffItem = false;
234      for (let i: number = 0; i < (<Array<ShadowOptions>> this.value).length; i++) {
235        if (arkShadow.checkDiff(this.stageValue[i], this.value[1])) {
236          isDiffItem = true;
237          break;
238        }
239      }
240      if (!isDiffItem) {
241        checkDiff = false;
242      }
243    }
244    return checkDiff;
245  }
246}
247
248// @ts-ignore
249globalThis.TextTimer.attributeModifier = function (modifier: ArkComponent): void {
250  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
251    return new ArkTextTimerComponent(nativePtr);
252  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
253    return new modifierJS.TextTimerModifier(nativePtr, classType);
254  });
255};
256
257// @ts-ignore
258globalThis.TextTimer.contentModifier = function (modifier) {
259  const elmtId = ViewStackProcessor.GetElmtIdToAccountFor();
260  let nativeNode = getUINativeModule().getFrameNodeById(elmtId);
261  let component = this.createOrGetNode(elmtId, () => {
262    return new ArkTextTimerComponent(nativeNode);
263  });
264  component.setContentModifier(modifier);
265};