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' /> 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} 88 89class ProgressInitializeModifier extends ModifierWithKey<ProgressParam> { 90 constructor(value: ProgressParam) { 91 super(value); 92 } 93 static identity: Symbol = Symbol('progressInitialize'); 94 applyPeer(node: KNode, reset: boolean): void { 95 if (reset) { 96 getUINativeModule().progress.resetProgressInitialize(node); 97 } else { 98 getUINativeModule().progress.setProgressInitialize(node, this.value.value, 99 this.value.total, this.value.style, this.value.type); 100 } 101 } 102} 103 104class ProgressValueModifier extends ModifierWithKey<number> { 105 static identity: Symbol = Symbol('value'); 106 applyPeer(node: KNode, reset: boolean): void { 107 if (reset) { 108 getUINativeModule().progress.ResetProgressValue(node); 109 } else { 110 getUINativeModule().progress.SetProgressValue(node, this.value!); 111 } 112 } 113 checkObjectDiff(): boolean { 114 return true; 115 } 116} 117 118class ProgressColorModifier extends ModifierWithKey<ResourceColor | LinearGradient> { 119 static identity: Symbol = Symbol('color'); 120 applyPeer(node: KNode, reset: boolean): void { 121 if (reset) { 122 getUINativeModule().progress.resetProgressColor(node); 123 } else { 124 getUINativeModule().progress.setProgressColor(node, this.value!); 125 } 126 } 127 128 checkObjectDiff(): boolean { 129 return this.stageValue !== this.value; 130 } 131} 132 133class ProgressStyleModifier extends ModifierWithKey<ProgressStyleOptions | CapsuleStyleOptions | 134RingStyleOptions | LinearStyleOptions | ScaleRingStyleOptions | EclipseStyleOptions> { 135 static identity: Symbol = Symbol('style'); 136 applyPeer(node: KNode, reset: boolean): void { 137 if (reset) { 138 getUINativeModule().progress.ResetProgressStyle(node); 139 } else { 140 let strokeWidth = (<ProgressStyleOptions> this.value).strokeWidth; 141 let scaleCount = (<ProgressStyleOptions> this.value).scaleCount; 142 let scaleWidth = (<ProgressStyleOptions> this.value).scaleWidth; 143 let enableSmoothEffect = (<ProgressStyleOptions> this.value).enableSmoothEffect; 144 let borderColor = (<CapsuleStyleOptions> this.value).borderColor; 145 let borderWidth = (<CapsuleStyleOptions> this.value).borderWidth; 146 let content = (<CapsuleStyleOptions> this.value).content; 147 let fontSize; 148 let fontWeight; 149 let fontFamily; 150 let fontStyle; 151 if ((<CapsuleStyleOptions> this.value).font) { 152 fontSize = (<CapsuleStyleOptions> this.value).font.size; 153 fontWeight = (<CapsuleStyleOptions> this.value).font.weight; 154 fontFamily = (<CapsuleStyleOptions> this.value).font.family; 155 fontStyle = (<CapsuleStyleOptions> this.value).font.style; 156 } 157 let fontColor = (<CapsuleStyleOptions> this.value).fontColor; 158 let enableScanEffect = (<CapsuleStyleOptions> this.value).enableScanEffect; 159 let showDefaultPercentage = (<CapsuleStyleOptions> this.value).showDefaultPercentage; 160 let shadow = (<RingStyleOptions> this.value).shadow; 161 let status = (<RingStyleOptions> this.value).status; 162 let strokeRadius = (<LinearStyleOptions> this.value).strokeRadius; 163 getUINativeModule().progress.SetProgressStyle( 164 node, strokeWidth, scaleCount, scaleWidth, enableSmoothEffect, borderColor, 165 borderWidth, content, fontSize, fontWeight, fontFamily, fontStyle, fontColor, 166 enableScanEffect, showDefaultPercentage, shadow, status, strokeRadius 167 ); 168 } 169 } 170 checkObjectDiff(): boolean { 171 return true; 172 } 173} 174 175class ProgressBackgroundColorModifier extends ModifierWithKey<ResourceColor> { 176 static identity: Symbol = Symbol('progressBackgroundColor'); 177 applyPeer(node: KNode, reset: boolean): void { 178 if (reset) { 179 getUINativeModule().progress.resetProgressBackgroundColor(node); 180 } else { 181 getUINativeModule().progress.setProgressBackgroundColor(node, this.value); 182 } 183 } 184 185 checkObjectDiff(): boolean { 186 if (isResource(this.stageValue) && isResource(this.value)) { 187 return !isResourceEqual(this.stageValue, this.value); 188 } else { 189 return true; 190 } 191 } 192} 193 194class ProgressContentModifier extends ModifierWithKey<ContentModifier<ProgressConfiguration>> { 195 constructor(value: ContentModifier<ProgressConfiguration>) { 196 super(value); 197 } 198 static identity: Symbol = Symbol('progressContentModifier'); 199 applyPeer(node: KNode, reset: boolean, component: ArkComponent) { 200 let progressComponent = component as ArkProgressComponent; 201 progressComponent.setContentModifier(this.value); 202 } 203} 204 205// @ts-ignore 206globalThis.Progress.attributeModifier = function (modifier: ArkComponent): void { 207 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 208 return new ArkProgressComponent(nativePtr); 209 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 210 return new modifierJS.ProgressModifier(nativePtr, classType); 211 }); 212}; 213 214// @ts-ignore 215globalThis.Progress.contentModifier = function (modifier) { 216 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 217 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 218 let component = this.createOrGetNode(elmtId, () => { 219 return new ArkProgressComponent(nativeNode); 220 }); 221 component.setContentModifier(modifier); 222};