• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2024 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' />
17interface ProgressParam {
18  value: number;
19  total?: number;
20  style?: ProgressStyle
21  type?: Type
22}
23class ArkProgressComponent extends ArkComponent implements ProgressAttribute {
24  constructor(nativePtr: KNode, classType?: ModifierType) {
25    super(nativePtr, classType);
26  }
27  builder: WrappedBuilder<Object[]> | null = null;
28  modifier: ContentModifier<ProgressConfiguration> | null = null;
29  progressNode: BuilderNode<[ProgressConfiguration]> | null = null;
30  initialize(value: Object[]): ProgressAttribute {
31    if (value[0] !== undefined) {
32      modifierWithKey(this._modifiersWithKeys, ProgressInitializeModifier.identity,
33        ProgressInitializeModifier, (value[0] as ProgressParam));
34    }
35    return this;
36  }
37  allowChildCount(): number {
38    return 0;
39  }
40  value(value: number): ProgressAttribute<keyof ProgressStyleMap, LinearStyleOptions |
41  ProgressStyleOptions | RingStyleOptions | EclipseStyleOptions | ScaleRingStyleOptions |
42  CapsuleStyleOptions> {
43    modifierWithKey(this._modifiersWithKeys, ProgressValueModifier.identity, ProgressValueModifier, value);
44    return this;
45  }
46  color(value: ResourceColor | LinearGradient): ProgressAttribute<keyof ProgressStyleMap, LinearStyleOptions |
47  ProgressStyleOptions | RingStyleOptions | EclipseStyleOptions | ScaleRingStyleOptions |
48  CapsuleStyleOptions> {
49    modifierWithKey(this._modifiersWithKeys, ProgressColorModifier.identity, ProgressColorModifier, value);
50    return this;
51  }
52  style(value: LinearStyleOptions | ProgressStyleOptions | RingStyleOptions | EclipseStyleOptions |
53  ScaleRingStyleOptions | CapsuleStyleOptions):
54    ProgressAttribute<keyof ProgressStyleMap, LinearStyleOptions | ProgressStyleOptions |
55    RingStyleOptions | EclipseStyleOptions | ScaleRingStyleOptions | CapsuleStyleOptions> {
56    modifierWithKey(this._modifiersWithKeys, ProgressStyleModifier.identity, ProgressStyleModifier, value);
57    return this;
58  }
59  backgroundColor(value: ResourceColor): this {
60    modifierWithKey(this._modifiersWithKeys, ProgressBackgroundColorModifier.identity, ProgressBackgroundColorModifier, value);
61    return this;
62  }
63  contentModifier(value: ContentModifier<ProgressConfiguration>): this {
64    modifierWithKey(this._modifiersWithKeys, ProgressContentModifier.identity, ProgressContentModifier, value);
65    return this;
66  }
67  setContentModifier(modifier: ContentModifier<ProgressConfiguration>): this {
68    if (modifier === undefined || modifier === null) {
69      getUINativeModule().progress.setContentModifierBuilder(this.nativePtr, false);
70      return;
71    }
72    this.builder = modifier.applyContent();
73    this.modifier = modifier;
74    getUINativeModule().progress.setContentModifierBuilder(this.nativePtr, this);
75  }
76  makeContentModifierNode(context: UIContext, progressConfig: ProgressConfiguration): FrameNode | null {
77    progressConfig.contentModifier = this.modifier;
78    if (isUndefined(this.progressNode)) {
79      const xNode = globalThis.requireNapi('arkui.node');
80      this.progressNode = new xNode.BuilderNode(context);
81      this.progressNode.build(this.builder, progressConfig);
82    } else {
83      this.progressNode.update(progressConfig);
84    }
85    return this.progressNode.getFrameNode();
86  }
87  privacySensitive(value: boolean): this {
88    modifierWithKey(this._modifiersWithKeys, ProgressPrivacySensitiveModifier.identity, ProgressPrivacySensitiveModifier, value);
89    return this;
90  }
91}
92
93class ProgressPrivacySensitiveModifier extends ModifierWithKey<boolean> {
94  constructor(value: boolean) {
95    super(value);
96  }
97  static identity: Symbol = Symbol('progressPrivacySensitive');
98  applyPeer(node: KNode, reset: boolean): void {
99    if (reset) {
100      getUINativeModule().progress.resetProgressPrivacySensitive(node);
101    } else {
102      getUINativeModule().progress.setProgressPrivacySensitive(node, this.value);
103    }
104  }
105}
106
107class ProgressInitializeModifier extends ModifierWithKey<ProgressParam> {
108  constructor(value: ProgressParam) {
109    super(value);
110  }
111  static identity: Symbol = Symbol('progressInitialize');
112  applyPeer(node: KNode, reset: boolean): void {
113    if (reset) {
114      getUINativeModule().progress.resetProgressInitialize(node);
115    } else {
116      getUINativeModule().progress.setProgressInitialize(node, this.value.value,
117        this.value.total, this.value.style, this.value.type);
118    }
119  }
120}
121
122class ProgressValueModifier extends ModifierWithKey<number> {
123  static identity: Symbol = Symbol('value');
124  applyPeer(node: KNode, reset: boolean): void {
125    if (reset) {
126      getUINativeModule().progress.ResetProgressValue(node);
127    } else {
128      getUINativeModule().progress.SetProgressValue(node, this.value!);
129    }
130  }
131  checkObjectDiff(): boolean {
132    return true;
133  }
134}
135
136class ProgressColorModifier extends ModifierWithKey<ResourceColor | LinearGradient> {
137  static identity: Symbol = Symbol('color');
138  applyPeer(node: KNode, reset: boolean): void {
139    if (reset) {
140      getUINativeModule().progress.resetProgressColor(node);
141    } else {
142      getUINativeModule().progress.setProgressColor(node, this.value!);
143    }
144  }
145
146  checkObjectDiff(): boolean {
147    return this.stageValue !== this.value;
148  }
149}
150
151class ProgressStyleModifier extends ModifierWithKey<ProgressStyleOptions | CapsuleStyleOptions |
152RingStyleOptions | LinearStyleOptions | ScaleRingStyleOptions | EclipseStyleOptions> {
153  static identity: Symbol = Symbol('style');
154  applyPeer(node: KNode, reset: boolean): void {
155    if (reset) {
156      getUINativeModule().progress.ResetProgressStyle(node);
157    } else {
158      let strokeWidth = (<ProgressStyleOptions> this.value).strokeWidth;
159      let scaleCount = (<ProgressStyleOptions> this.value).scaleCount;
160      let scaleWidth = (<ProgressStyleOptions> this.value).scaleWidth;
161      let enableSmoothEffect = (<ProgressStyleOptions> this.value).enableSmoothEffect;
162      let borderColor = (<CapsuleStyleOptions> this.value).borderColor;
163      let borderWidth = (<CapsuleStyleOptions> this.value).borderWidth;
164      let content = (<CapsuleStyleOptions> this.value).content;
165      let fontSize;
166      let fontWeight;
167      let fontFamily;
168      let fontStyle;
169      if ((<CapsuleStyleOptions> this.value).font) {
170        fontSize = (<CapsuleStyleOptions> this.value).font.size;
171        fontWeight = (<CapsuleStyleOptions> this.value).font.weight;
172        fontFamily = (<CapsuleStyleOptions> this.value).font.family;
173        fontStyle = (<CapsuleStyleOptions> this.value).font.style;
174      }
175      let fontColor = (<CapsuleStyleOptions> this.value).fontColor;
176      let enableScanEffect = (<CapsuleStyleOptions> this.value).enableScanEffect;
177      let showDefaultPercentage = (<CapsuleStyleOptions> this.value).showDefaultPercentage;
178      let shadow = (<RingStyleOptions> this.value).shadow;
179      let status = (<RingStyleOptions> this.value).status;
180      let strokeRadius = (<LinearStyleOptions> this.value).strokeRadius;
181      let borderRadius = (<CapsuleStyleOptions> this.value).borderRadius;
182      getUINativeModule().progress.SetProgressStyle(
183        node, strokeWidth, scaleCount, scaleWidth, enableSmoothEffect, borderColor,
184        borderWidth, content, fontSize, fontWeight, fontFamily, fontStyle, fontColor,
185        enableScanEffect, showDefaultPercentage, shadow, status, strokeRadius, borderRadius
186      );
187    }
188  }
189  checkObjectDiff(): boolean {
190    return true;
191  }
192}
193
194class ProgressBackgroundColorModifier extends ModifierWithKey<ResourceColor> {
195  static identity: Symbol = Symbol('progressBackgroundColor');
196  applyPeer(node: KNode, reset: boolean): void {
197    if (reset) {
198      getUINativeModule().progress.resetProgressBackgroundColor(node);
199    } else {
200      getUINativeModule().progress.setProgressBackgroundColor(node, this.value);
201    }
202  }
203
204  checkObjectDiff(): boolean {
205    return !isBaseOrResourceEqual(this.stageValue, this.value);
206  }
207}
208
209class ProgressContentModifier extends ModifierWithKey<ContentModifier<ProgressConfiguration>> {
210  constructor(value: ContentModifier<ProgressConfiguration>) {
211    super(value);
212  }
213  static identity: Symbol = Symbol('progressContentModifier');
214  applyPeer(node: KNode, reset: boolean, component: ArkComponent) {
215    let progressComponent = component as ArkProgressComponent;
216    progressComponent.setContentModifier(this.value);
217  }
218}
219
220// @ts-ignore
221globalThis.Progress.attributeModifier = function (modifier: ArkComponent): void {
222  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
223    return new ArkProgressComponent(nativePtr);
224  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
225    return new modifierJS.ProgressModifier(nativePtr, classType);
226  });
227};
228
229// @ts-ignore
230globalThis.Progress.contentModifier = function (modifier) {
231  const elmtId = ViewStackProcessor.GetElmtIdToAccountFor();
232  let nativeNode = getUINativeModule().getFrameNodeById(elmtId);
233  let component = this.createOrGetNode(elmtId, () => {
234    return new ArkProgressComponent(nativeNode);
235  });
236  component.setContentModifier(modifier);
237};