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 if (isResource(this.stageValue) && isResource(this.value)) { 89 return !isResourceEqual(this.stageValue, this.value); 90 } else { 91 return true; 92 } 93 } 94} 95 96class DataPanelCloseEffectModifier extends ModifierWithKey<boolean> { 97 static identity: Symbol = Symbol('dataPanelCloseEffect'); 98 applyPeer(node: KNode, reset: boolean): void { 99 if (reset) { 100 getUINativeModule().dataPanel.resetCloseEffect(node); 101 } else { 102 getUINativeModule().dataPanel.setCloseEffect(node, this.value); 103 } 104 } 105 106 checkObjectDiff(): boolean { 107 if (isResource(this.stageValue) && isResource(this.value)) { 108 return !isResourceEqual(this.stageValue, this.value); 109 } else { 110 return true; 111 } 112 } 113} 114 115class DataPanelTrackBackgroundColorModifier extends ModifierWithKey<ResourceColor> { 116 static identity: Symbol = Symbol('dataPanelTrackBackgroundColorModifier'); 117 applyPeer(node: KNode, reset: boolean): void { 118 if (reset) { 119 getUINativeModule().dataPanel.resetDataPanelTrackBackgroundColor(node); 120 } else { 121 getUINativeModule().dataPanel.setDataPanelTrackBackgroundColor(node, this.value); 122 } 123 } 124 125 checkObjectDiff(): boolean { 126 if (isResource(this.stageValue) && isResource(this.value)) { 127 return !isResourceEqual(this.stageValue, this.value); 128 } else { 129 return true; 130 } 131 } 132} 133class DataPanelTrackShadowModifier extends ModifierWithKey<DataPanelShadowOptions> { 134 static identity = Symbol('dataPanelTrackShadow'); 135 applyPeer(node: KNode, reset: boolean): void { 136 if (reset) { 137 if (this.value === null) { 138 getUINativeModule().dataPanel.setDataPanelTrackShadow(node, null); 139 } 140 getUINativeModule().dataPanel.resetDataPanelTrackShadow(node); 141 } else { 142 getUINativeModule().dataPanel.setDataPanelTrackShadow(node, this.value!); 143 } 144 } 145 checkObjectDiff(): boolean { 146 return true; 147 } 148} 149 150class DataPanelContentModifier extends ModifierWithKey<ContentModifier<DataPanelConfiguration>> { 151 constructor(value: ContentModifier<DataPanelConfiguration>) { 152 super(value); 153 } 154 static identity: Symbol = Symbol('dataPanelContentModifier'); 155 applyPeer(node: KNode, reset: boolean, component: ArkComponent) { 156 let dataPanelComponent = component as ArkDataPanelComponent; 157 dataPanelComponent.setContentModifier(this.value); 158 } 159} 160class DataPanelValueColorsModifier extends ModifierWithKey<Array<ResourceColor | LinearGradient>> { 161 static identity = Symbol('dataPanelValueColors'); 162 applyPeer(node: KNode, reset: boolean): void { 163 if (reset) { 164 getUINativeModule().dataPanel.resetDataPanelValueColors(node); 165 return; 166 } else { 167 getUINativeModule().dataPanel.setDataPanelValueColors(node, this.value!); 168 } 169 } 170 checkObjectDiff(): boolean { 171 return true; 172 } 173} 174// @ts-ignore 175globalThis.DataPanel.attributeModifier = function (modifier: ArkComponent): void { 176 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 177 return new ArkDataPanelComponent(nativePtr); 178 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 179 return new modifierJS.DataPanelModifier(nativePtr, classType); 180 }); 181}; 182// @ts-ignore 183globalThis.DataPanel.contentModifier = function (modifier: ContentModifier<DataPanelConfiguration>) { 184 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 185 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 186 let component = this.createOrGetNode(elmtId, () => { 187 return new ArkDataPanelComponent(nativeNode); 188 }); 189 component.setContentModifier(modifier); 190}; 191