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 ArkGaugeComponent extends ArkComponent implements GaugeAttribute { 18 builder: WrappedBuilder<Object[]> | null = null; 19 gaugeNode: BuilderNode<[GaugeConfiguration]> | null = null; 20 modifier: ContentModifier<GaugeConfiguration>; 21 needRebuild: boolean = false; 22 constructor(nativePtr: KNode, classType?: ModifierType) { 23 super(nativePtr, classType); 24 } 25 value(value: number): this { 26 modifierWithKey(this._modifiersWithKeys, GaugeVauleModifier.identity, GaugeVauleModifier, value); 27 return this; 28 } 29 startAngle(angle: number): this { 30 modifierWithKey(this._modifiersWithKeys, GaugeStartAngleModifier.identity, GaugeStartAngleModifier, angle); 31 return this; 32 } 33 endAngle(angle: number): this { 34 modifierWithKey(this._modifiersWithKeys, GaugeEndAngleModifier.identity, GaugeEndAngleModifier, angle); 35 return this; 36 } 37 colors(colors: ResourceColor | LinearGradient | Array<[ResourceColor | LinearGradient, number]>): this { 38 modifierWithKey(this._modifiersWithKeys, GaugeColorsModifier.identity, GaugeColorsModifier, colors); 39 return this; 40 } 41 strokeWidth(length: any): this { 42 modifierWithKey(this._modifiersWithKeys, GaugeStrokeWidthModifier.identity, GaugeStrokeWidthModifier, length); 43 return this; 44 } 45 description(value: CustomBuilder): this { 46 throw new Error('Method not implemented.'); 47 } 48 trackShadow(value: GaugeShadowOptions): this { 49 modifierWithKey(this._modifiersWithKeys, GaugeTrackShadowModifier.identity, GaugeTrackShadowModifier, value); 50 return this; 51 } 52 indicator(value: GaugeIndicatorOptions): this { 53 modifierWithKey(this._modifiersWithKeys, GaugeIndicatorModifier.identity, GaugeIndicatorModifier, value); 54 return this; 55 } 56 contentModifier(value: ContentModifier<GaugeConfiguration>): this { 57 modifierWithKey(this._modifiersWithKeys, GaugeContentModifier.identity, GaugeContentModifier, value); 58 return this; 59 } 60 setContentModifier(modifier: ContentModifier<GaugeConfiguration>): this { 61 if (modifier === undefined || modifier === null) { 62 getUINativeModule().gauge.setContentModifierBuilder(this.nativePtr, false); 63 return; 64 } 65 this.needRebuild = false; 66 if (this.builder !== modifier.applyContent()) { 67 this.needRebuild = true; 68 } 69 this.builder = modifier.applyContent(); 70 this.modifier = modifier; 71 getUINativeModule().gauge.setContentModifierBuilder(this.nativePtr, this); 72 } 73 makeContentModifierNode(context: UIContext, gaugeConfiguration: GaugeConfiguration): FrameNode | null { 74 gaugeConfiguration.contentModifier = this.modifier; 75 if (isUndefined(this.gaugeNode) || this.needRebuild) { 76 let xNode = globalThis.requireNapi('arkui.node'); 77 this.gaugeNode = new xNode.BuilderNode(context); 78 this.gaugeNode.build(this.builder, gaugeConfiguration); 79 this.needRebuild = false; 80 } else { 81 this.gaugeNode.update(gaugeConfiguration); 82 } 83 return this.gaugeNode.getFrameNode(); 84 } 85} 86 87class GaugeIndicatorModifier extends ModifierWithKey<GaugeIndicatorOptions> { 88 static identity: Symbol = Symbol('gaugeIndicator'); 89 applyPeer(node: KNode, reset: boolean): void { 90 if (reset) { 91 getUINativeModule().gauge.resetGaugeIndicator(node, this.value); 92 } else { 93 getUINativeModule().gauge.setGaugeIndicator(node, this.value.icon, this.value.space); 94 } 95 } 96 97 checkObjectDiff(): boolean { 98 return !isBaseOrResourceEqual(this.stageValue.icon, this.value.icon) || 99 !isBaseOrResourceEqual(this.stageValue.space, this.value.space); 100 } 101} 102 103class GaugeContentModifier extends ModifierWithKey<ContentModifier<GaugeConfiguration>> { 104 constructor(value: ContentModifier<GaugeConfiguration>) { 105 super(value); 106 } 107 static identity: Symbol = Symbol('gaugeContentModifier'); 108 applyPeer(node: KNode, reset: boolean, component: ArkComponent) { 109 let gaugeComponent = component as ArkGaugeComponent; 110 gaugeComponent.setContentModifier(this.value); 111 } 112} 113 114class GaugeColorsModifier extends ModifierWithKey<ResourceColor | LinearGradient | Array<[ResourceColor | LinearGradient, number]>> { 115 static identity = Symbol('gaugeColors'); 116 applyPeer(node: KNode, reset: boolean): void { 117 if (reset) { 118 getUINativeModule().gauge.resetGaugeColors(node); 119 } else { 120 getUINativeModule().gauge.setGaugeColors(node, this.value!); 121 } 122 } 123 checkObjectDiff(): boolean { 124 return true; 125 } 126} 127 128class GaugeVauleModifier extends ModifierWithKey<number> { 129 static identity: Symbol = Symbol('gaugeVaule'); 130 applyPeer(node: KNode, reset: boolean): void { 131 if (reset) { 132 getUINativeModule().gauge.resetGaugeVaule(node); 133 } else { 134 getUINativeModule().gauge.setGaugeVaule(node, this.value); 135 } 136 } 137 138 checkObjectDiff(): boolean { 139 if (isResource(this.stageValue) && isResource(this.value)) { 140 return !isResourceEqual(this.stageValue, this.value); 141 } else { 142 return true; 143 } 144 } 145} 146 147class GaugeStartAngleModifier extends ModifierWithKey<number> { 148 static identity: Symbol = Symbol('gaugeStartAngle'); 149 applyPeer(node: KNode, reset: boolean): void { 150 if (reset) { 151 getUINativeModule().gauge.resetGaugeStartAngle(node); 152 } else { 153 getUINativeModule().gauge.setGaugeStartAngle(node, this.value); 154 } 155 } 156 157 checkObjectDiff(): boolean { 158 if (isResource(this.stageValue) && isResource(this.value)) { 159 return !isResourceEqual(this.stageValue, this.value); 160 } else { 161 return true; 162 } 163 } 164} 165 166class GaugeEndAngleModifier extends ModifierWithKey<number> { 167 static identity: Symbol = Symbol('gaugeEndAngle'); 168 applyPeer(node: KNode, reset: boolean): void { 169 if (reset) { 170 getUINativeModule().gauge.resetGaugeEndAngle(node); 171 } else { 172 getUINativeModule().gauge.setGaugeEndAngle(node, this.value); 173 } 174 } 175 176 checkObjectDiff(): boolean { 177 if (isResource(this.stageValue) && isResource(this.value)) { 178 return !isResourceEqual(this.stageValue, this.value); 179 } else { 180 return true; 181 } 182 } 183} 184 185class GaugeStrokeWidthModifier extends ModifierWithKey<Length> { 186 static identity: Symbol = Symbol('gaugeStrokeWidth'); 187 applyPeer(node: KNode, reset: boolean): void { 188 if (reset) { 189 getUINativeModule().gauge.resetGaugeStrokeWidth(node); 190 } else { 191 getUINativeModule().gauge.setGaugeStrokeWidth(node, this.value); 192 } 193 } 194 195 checkObjectDiff(): boolean { 196 if (isResource(this.stageValue) && isResource(this.value)) { 197 return !isResourceEqual(this.stageValue, this.value); 198 } else { 199 return true; 200 } 201 } 202} 203 204class GaugeTrackShadowModifier extends ModifierWithKey<GaugeShadowOptions> { 205 static identity: Symbol = Symbol('gaugeTrackShadow'); 206 applyPeer(node: KNode, reset: boolean): void { 207 if (reset) { 208 getUINativeModule().gauge.resetGaugeTrackShadow(node); 209 } else { 210 getUINativeModule().gauge.setGaugeTrackShadow(node, this.value, this.value.radius, this.value.offsetX, this.value.offsetY); 211 } 212 } 213 214 checkObjectDiff(): boolean { 215 if (isResource(this.stageValue) && isResource(this.value)) { 216 return !isResourceEqual(this.stageValue, this.value); 217 } else { 218 return true; 219 } 220 } 221} 222 223// @ts-ignore 224globalThis.Gauge.attributeModifier = function (modifier: ArkComponent): void { 225 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 226 return new ArkGaugeComponent(nativePtr); 227 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 228 return new modifierJS.GaugeModifier(nativePtr, classType); 229 }); 230}; 231// @ts-ignore 232globalThis.Gauge.contentModifier = function (modifier) { 233 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 234 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 235 let component = this.createOrGetNode(elmtId, () => { 236 return new ArkGaugeComponent(nativeNode); 237 }); 238 component.setContentModifier(modifier); 239}; 240