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 ArkDataPanelComponent extends ArkComponent implements DataPanelAttribute { 18 builder: WrappedBuilder<Object[]> | null = null; 19 dataPanelNode: BuilderNode<[taPanelTrackShado]> | null = null; 20 modifier: ContentModifier<DataPanelConfiguration>; 21 needRebuild: Boolean = false; 22 23 constructor(nativePtr: KNode, classType?: ModifierType) { 24 super(nativePtr, classType); 25 } 26 closeEffect(value: boolean): this { 27 modifierWithKey(this._modifiersWithKeys, DataPanelCloseEffectModifier.identity, DataPanelCloseEffectModifier, value); 28 return this; 29 } 30 valueColors(value: Array<ResourceColor | LinearGradient>): this { 31 modifierWithKey(this._modifiersWithKeys, DataPanelValueColorsModifier.identity, DataPanelValueColorsModifier, value); 32 return this; 33 } 34 trackBackgroundColor(value: any): this { 35 modifierWithKey(this._modifiersWithKeys, DataPanelTrackBackgroundColorModifier.identity, DataPanelTrackBackgroundColorModifier, value); 36 return this; 37 } 38 strokeWidth(value: any): this { 39 modifierWithKey(this._modifiersWithKeys, DataPanelStrokeWidthModifier.identity, DataPanelStrokeWidthModifier, value); 40 return this; 41 } 42 trackShadow(value: DataPanelShadowOptions): this { 43 modifierWithKey(this._modifiersWithKeys, DataPanelTrackShadowModifier.identity, DataPanelTrackShadowModifier, value); 44 return this; 45 } 46 contentModifier(value: ContentModifier<DataPanelConfiguration>): this { 47 modifierWithKey(this._modifiersWithKeys, DataPanelContentModifier.identity, DataPanelContentModifier, value); 48 return this; 49 } 50 setContentModifier(modifier: ContentModifier<DataPanelConfiguration>): this { 51 if (modifier === undefined || modifier === null) { 52 getUINativeModule().datapanel.setContentModifierBuilder(this.nativePtr, false); 53 return; 54 } 55 this.needRebuild = false; 56 if (this.builder !== modifier.applyContent()) { 57 this.needRebuild = true; 58 } 59 this.builder = modifier.applyContent(); 60 this.modifier = modifier; 61 getUINativeModule().dataPanel.setContentModifierBuilder(this.nativePtr, this); 62 } 63 makeContentModifierNode(context: UIContext, datapanelConfig: DataPanelConfiguration): FrameNode | null { 64 datapanelConfig.contentModifier = this.modifier; 65 if (!isUndefined(this.dataPanelNode) || this.needRebuild) { 66 let xNode = globalThis.requireNapi('arkui.node'); 67 this.dataPanelNode = new xNode.BuilderNode(context); 68 this.dataPanelNode.build(this.builder, datapanelConfig); 69 this.needRebuild = false; 70 } else { 71 this.dataPanelNode.update(datapanelConfig); 72 } 73 return this.dataPanelNode.getFrameNode(); 74 } 75} 76 77class DataPanelStrokeWidthModifier extends ModifierWithKey<Length> { 78 static identity: Symbol = Symbol('dataPanelStrokeWidth'); 79 applyPeer(node: KNode, reset: boolean): void { 80 if (reset) { 81 getUINativeModule().dataPanel.resetDataPanelStrokeWidth(node); 82 } else { 83 getUINativeModule().dataPanel.setDataPanelStrokeWidth(node, this.value); 84 } 85 } 86 87 checkObjectDiff(): boolean { 88 return !isBaseOrResourceEqual(this.stageValue, this.value); 89 } 90} 91 92class DataPanelCloseEffectModifier extends ModifierWithKey<boolean> { 93 static identity: Symbol = Symbol('dataPanelCloseEffect'); 94 applyPeer(node: KNode, reset: boolean): void { 95 if (reset) { 96 getUINativeModule().dataPanel.resetCloseEffect(node); 97 } else { 98 getUINativeModule().dataPanel.setCloseEffect(node, this.value); 99 } 100 } 101 102 checkObjectDiff(): boolean { 103 return !isBaseOrResourceEqual(this.stageValue, this.value); 104 } 105} 106 107class DataPanelTrackBackgroundColorModifier extends ModifierWithKey<ResourceColor> { 108 static identity: Symbol = Symbol('dataPanelTrackBackgroundColorModifier'); 109 applyPeer(node: KNode, reset: boolean): void { 110 if (reset) { 111 getUINativeModule().dataPanel.resetDataPanelTrackBackgroundColor(node); 112 } else { 113 getUINativeModule().dataPanel.setDataPanelTrackBackgroundColor(node, this.value); 114 } 115 } 116 117 checkObjectDiff(): boolean { 118 return !isBaseOrResourceEqual(this.stageValue, this.value); 119 } 120} 121class DataPanelTrackShadowModifier extends ModifierWithKey<DataPanelShadowOptions> { 122 static identity = Symbol('dataPanelTrackShadow'); 123 applyPeer(node: KNode, reset: boolean): void { 124 if (reset) { 125 if (this.value === null) { 126 getUINativeModule().dataPanel.setDataPanelTrackShadow(node, null); 127 } 128 getUINativeModule().dataPanel.resetDataPanelTrackShadow(node); 129 } else { 130 getUINativeModule().dataPanel.setDataPanelTrackShadow(node, this.value!); 131 } 132 } 133 checkObjectDiff(): boolean { 134 return true; 135 } 136} 137 138class DataPanelContentModifier extends ModifierWithKey<ContentModifier<DataPanelConfiguration>> { 139 constructor(value: ContentModifier<DataPanelConfiguration>) { 140 super(value); 141 } 142 static identity: Symbol = Symbol('dataPanelContentModifier'); 143 applyPeer(node: KNode, reset: boolean, component: ArkComponent) { 144 let dataPanelComponent = component as ArkDataPanelComponent; 145 dataPanelComponent.setContentModifier(this.value); 146 } 147} 148class DataPanelValueColorsModifier extends ModifierWithKey<Array<ResourceColor | LinearGradient>> { 149 static identity = Symbol('dataPanelValueColors'); 150 applyPeer(node: KNode, reset: boolean): void { 151 if (reset) { 152 getUINativeModule().dataPanel.resetDataPanelValueColors(node); 153 return; 154 } else { 155 getUINativeModule().dataPanel.setDataPanelValueColors(node, this.value!); 156 } 157 } 158 checkObjectDiff(): boolean { 159 return true; 160 } 161} 162// @ts-ignore 163globalThis.DataPanel.attributeModifier = function (modifier: ArkComponent): void { 164 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 165 return new ArkDataPanelComponent(nativePtr); 166 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 167 return new modifierJS.DataPanelModifier(nativePtr, classType); 168 }); 169}; 170// @ts-ignore 171globalThis.DataPanel.contentModifier = function (modifier: ContentModifier<DataPanelConfiguration>) { 172 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 173 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 174 let component = this.createOrGetNode(elmtId, () => { 175 return new ArkDataPanelComponent(nativeNode); 176 }); 177 component.setContentModifier(modifier); 178}; 179