• 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 ArkLoadingProgressComponent extends ArkComponent implements LoadingProgressAttribute {
18  builder: WrappedBuilder<Object[]> | null = null;
19  loadingProgressNode: BuilderNode<[LoadingProgressConfiguration]> | null = null;
20  modifier: ContentModifier<LoadingProgressConfiguration>;
21  needRebuild: boolean = false;
22  constructor(nativePtr: KNode, classType?: ModifierType) {
23    super(nativePtr, classType);
24  }
25  initialize(value: Object[]): LoadingProgressAttribute {
26    return this;
27  }
28  allowChildCount(): number {
29    return 0;
30  }
31  color(value: ResourceColor): this {
32    modifierWithKey(this._modifiersWithKeys, LoadingProgressColorModifier.identity, LoadingProgressColorModifier, value);
33    return this;
34  }
35  enableLoading(value: boolean): this {
36    modifierWithKey(this._modifiersWithKeys, LoadingProgressEnableLoadingModifier.identity, LoadingProgressEnableLoadingModifier, value);
37    return this;
38  }
39  foregroundColor(value: ResourceColor): this {
40    modifierWithKey(this._modifiersWithKeys, LoadingProgressForegroundColorModifier.identity,
41      LoadingProgressForegroundColorModifier, value);
42    return this;
43  }
44  contentModifier(value: ContentModifier<LoadingProgressConfiguration>): this {
45    modifierWithKey(this._modifiersWithKeys, LoadingProgressContentModifier.identity, LoadingProgressContentModifier, value);
46    return this;
47  }
48  setContentModifier(modifier: ContentModifier<LoadingProgressConfiguration>): this {
49    if (modifier === undefined || modifier === null) {
50      getUINativeModule().loadingProgress.setContentModifierBuilder(this.nativePtr, false);
51      return;
52    }
53    this.needRebuild = false;
54    if (this.builder !== modifier.applyContent()) {
55      this.needRebuild = true;
56    }
57    this.builder = modifier.applyContent();
58    this.modifier = modifier;
59    getUINativeModule().loadingProgress.setContentModifierBuilder(this.nativePtr, this);
60  }
61  makeContentModifierNode(context: UIContext, loadingProgressConfiguration: LoadingProgressConfiguration): FrameNode | null {
62    loadingProgressConfiguration.contentModifier = this.modifier;
63    if (isUndefined(this.loadingProgressNode) || this.needRebuild) {
64      const xNode = globalThis.requireNapi('arkui.node');
65      this.loadingProgressNode = new xNode.BuilderNode(context);
66      this.loadingProgressNode.build(this.builder, loadingProgressConfiguration);
67      this.needRebuild = false;
68    } else {
69      this.loadingProgressNode.update(loadingProgressConfiguration);
70    }
71    return this.loadingProgressNode.getFrameNode();
72  }
73}
74
75class LoadingProgressColorModifier extends ModifierWithKey<ResourceColor> {
76  constructor(value: ResourceColor) {
77    super(value);
78  }
79  static identity: Symbol = Symbol('loadingProgressColor');
80  applyPeer(node: KNode, reset: boolean): void {
81    if (reset) {
82      getUINativeModule().loadingProgress.resetColor(node);
83    } else {
84      getUINativeModule().loadingProgress.setColor(node, this.value);
85    }
86  }
87
88  checkObjectDiff(): boolean {
89    return !isBaseOrResourceEqual(this.stageValue, this.value);
90  }
91}
92
93class LoadingProgressForegroundColorModifier extends ModifierWithKey<ResourceColor> {
94  constructor(value: ResourceColor) {
95    super(value);
96  }
97  static identity: Symbol = Symbol('loadingProgressForegroundColor');
98  applyPeer(node: KNode, reset: boolean): void {
99    if (reset) {
100      getUINativeModule().loadingProgress.resetForegroundColor(node);
101    } else {
102      getUINativeModule().loadingProgress.setForegroundColor(node, this.value);
103    }
104  }
105  checkObjectDiff(): boolean {
106    return !isBaseOrResourceEqual(this.stageValue, this.value);
107  }
108}
109
110class LoadingProgressContentModifier extends ModifierWithKey<ContentModifier<LoadingProgressConfiguration>> {
111  constructor(value: ContentModifier<LoadingProgressConfiguration>) {
112    super(value);
113  }
114  static identity: Symbol = Symbol('loadingProgressContentModifier');
115  applyPeer(node: KNode, reset: boolean, component: ArkComponent): void {
116    let loadingProgressComponent = component as ArkLoadingProgressComponent;
117    loadingProgressComponent.setContentModifier(this.value);
118  }
119}
120
121class LoadingProgressEnableLoadingModifier extends ModifierWithKey<boolean> {
122  constructor(value: boolean) {
123    super(value);
124  }
125  static identity: Symbol = Symbol('loadingProgressEnableLoading');
126  applyPeer(node: KNode, reset: boolean): void {
127    if (reset) {
128      getUINativeModule().loadingProgress.resetEnableLoading(node);
129    } else {
130      getUINativeModule().loadingProgress.setEnableLoading(node, this.value);
131    }
132  }
133}
134
135// @ts-ignore
136globalThis.LoadingProgress.attributeModifier = function (modifier: ArkComponent): void {
137  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
138    return new ArkLoadingProgressComponent(nativePtr);
139  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
140    return new modifierJS.LoadingProgressModifier(nativePtr, classType);
141  });
142};
143
144
145// @ts-ignore
146globalThis.LoadingProgress.contentModifier = function (modifier) {
147  const elmtId = ViewStackProcessor.GetElmtIdToAccountFor();
148  let nativeNode = getUINativeModule().getFrameNodeById(elmtId);
149  let component = this.createOrGetNode(elmtId, () => {
150    return new ArkLoadingProgressComponent(nativeNode);
151  });
152  component.setContentModifier(modifier);
153};
154