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