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 ArkBadgeComponent extends ArkComponent implements BadgeAttribute { 18 constructor(nativePtr: KNode, classType?: ModifierType) { 19 super(nativePtr, classType); 20 } 21 allowChildCount(): number { 22 return 1; 23 } 24 initialize(value: Object[]): this { 25 if (value.length === 1 && isObject(value[0])) { 26 if (value[0]?.count !== undefined) { 27 modifierWithKey( 28 this._modifiersWithKeys, BadgeParamWithNumberModifier.identity, BadgeParamWithNumberModifier, value[0]); 29 } else if (value[0]?.value !== undefined) { 30 modifierWithKey( 31 this._modifiersWithKeys, BadgeParamWithStringModifier.identity, BadgeParamWithStringModifier, value[0]); 32 } else { 33 modifierWithKey( 34 this._modifiersWithKeys, BadgeParamWithNumberModifier.identity, BadgeParamWithNumberModifier, undefined); 35 } 36 return this; 37 } 38 modifierWithKey( 39 this._modifiersWithKeys, BadgeParamWithNumberModifier.identity, BadgeParamWithNumberModifier, undefined); 40 return this; 41 } 42} 43 44class BadgeParamWithNumberModifier extends ModifierWithKey<BadgeParamWithNumber> { 45 constructor(value: BadgeParamWithNumber) { 46 super(value); 47 } 48 static identity: Symbol = Symbol('BadgeParamWithNumber'); 49 applyPeer(node: KNode, reset: boolean): void { 50 if (reset) { 51 getUINativeModule().badge.setBadgeParamWithNumber(node, undefined, undefined, undefined, 52 undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 0, undefined); 53 } else { 54 if (this.value?.style === undefined) { 55 getUINativeModule().badge.setBadgeParamWithNumber(node, 56 isNumber(this.value?.position) ? this.value.position : undefined, 57 isObject(this.value?.position) ? this.value.position?.x : undefined, 58 isObject(this.value?.position) ? this.value.position?.y : undefined, 59 undefined, undefined, undefined, undefined, undefined, undefined, undefined, 60 isObject(this.value?.position) && !isNull(this.value?.position), this.value?.count, this.value?.maxCount); 61 } else { 62 getUINativeModule().badge.setBadgeParamWithNumber(node, 63 isNumber(this.value?.position) ? this.value.position : undefined, 64 isObject(this.value?.position) ? this.value.position?.x : undefined, 65 isObject(this.value?.position) ? this.value.position?.y : undefined, 66 this.value.style?.badgeColor, this.value.style?.badgeSize, 67 this.value.style?.borderColor, this.value.style?.borderWidth, 68 this.value.style?.color, this.value.style?.fontSize, this.value.style?.fontWeight, 69 isObject(this.value?.position) && !isNull(this.value?.position), this.value?.count, this.value?.maxCount); 70 } 71 } 72 } 73 checkObjectDiff(): boolean { 74 let positionResult: boolean = false; 75 let badgeSizeResult: boolean = false; 76 let borderWidthResult: boolean = false; 77 let badgeColorResult: boolean = false; 78 let borderColorResult: boolean = false; 79 let colorResult: boolean = false; 80 let fontSizeResult: boolean = false; 81 let fontWeightResult: boolean = false; 82 if (isNumber(this.stageValue?.position) && isNumber(this.value?.position)) { 83 positionResult = !isBaseOrResourceEqual(this.stageValue.position, this.value.position); 84 } else if (isObject(this.stageValue?.position) && isObject(this.value?.position)) { 85 positionResult = !isBaseOrResourceEqual(this.stageValue.position?.x, this.value.position?.x) || 86 !isBaseOrResourceEqual(this.stageValue.position?.y, this.value.position?.y); 87 } else if (this.stageValue?.position !== undefined || this.value?.position !== undefined) { 88 positionResult = true; 89 } 90 if (isObject(this.stageValue?.style) && isObject(this.value?.style)) { 91 badgeSizeResult = !isBaseOrResourceEqual(this.stageValue.style?.badgeSize, this.value.style?.badgeSize); 92 borderWidthResult = !isBaseOrResourceEqual(this.stageValue.style?.borderWidth, this.value.style?.borderWidth); 93 badgeColorResult = !isBaseOrResourceEqual(this.stageValue.style?.badgeColor, this.value.style?.badgeColor); 94 borderColorResult = !isBaseOrResourceEqual(this.stageValue.style?.borderColor, this.value.style?.borderColor); 95 colorResult = !isBaseOrResourceEqual(this.stageValue.style?.color, this.value.style?.color); 96 fontSizeResult = !isBaseOrResourceEqual(this.stageValue.style?.fontSize, this.value.style?.fontSize); 97 fontWeightResult = !isBaseOrResourceEqual(this.stageValue.style?.fontWeight, this.value.style?.fontWeight); 98 } else if (this.stageValue?.style !== undefined || this.value?.style !== undefined) { 99 badgeSizeResult = true; 100 borderWidthResult = true; 101 badgeColorResult = true; 102 borderColorResult = true; 103 colorResult = true; 104 fontSizeResult = true; 105 fontWeightResult = true; 106 } 107 return positionResult || badgeSizeResult || borderWidthResult || badgeColorResult || borderColorResult || colorResult || 108 fontSizeResult || fontWeightResult || !isBaseOrResourceEqual(this.stageValue?.count, this.value?.count) || 109 !isBaseOrResourceEqual(this.stageValue?.maxCount, this.value?.maxCount); 110 } 111} 112 113class BadgeParamWithStringModifier extends ModifierWithKey<BadgeParamWithString> { 114 constructor(value: BadgeParamWithString) { 115 super(value); 116 } 117 static identity: Symbol = Symbol('BadgeParamWithString'); 118 applyPeer(node: KNode, reset: boolean): void { 119 if (reset) { 120 getUINativeModule().badge.setBadgeParamWithString(node, undefined, undefined, undefined, 121 undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined); 122 } else { 123 if (this.value?.style === undefined) { 124 getUINativeModule().badge.setBadgeParamWithString(node, 125 isNumber(this.value?.position) ? this.value.position : undefined, 126 isObject(this.value?.position) ? this.value.position?.x : undefined, 127 isObject(this.value?.position) ? this.value.position?.y : undefined, 128 undefined, undefined, undefined, undefined, undefined, undefined, undefined, 129 isObject(this.value?.position) && !isNull(this.value?.position), this.value?.value); 130 } else { 131 getUINativeModule().badge.setBadgeParamWithString(node, 132 isNumber(this.value?.position) ? this.value.position : undefined, 133 isObject(this.value?.position) ? this.value.position?.x : undefined, 134 isObject(this.value?.position) ? this.value.position?.y : undefined, 135 this.value.style?.badgeColor, this.value.style?.badgeSize, 136 this.value.style?.borderColor, this.value.style?.borderWidth, 137 this.value.style?.color, this.value.style?.fontSize, this.value.style?.fontWeight, 138 isObject(this.value?.position) && !isNull(this.value?.position), this.value?.value); 139 } 140 } 141 } 142 checkObjectDiff(): boolean { 143 let positionResult: boolean = false; 144 let badgeSizeResult: boolean = false; 145 let borderWidthResult: boolean = false; 146 let badgeColorResult: boolean = false; 147 let borderColorResult: boolean = false; 148 let colorResult: boolean = false; 149 let fontSizeResult: boolean = false; 150 let fontWeightResult: boolean = false; 151 if (isNumber(this.stageValue?.position) && isNumber(this.value?.position)) { 152 positionResult = !isBaseOrResourceEqual(this.stageValue.position, this.value.position); 153 } else if (isObject(this.stageValue?.position) && isObject(this.value?.position)) { 154 positionResult = !isBaseOrResourceEqual(this.stageValue.position?.x, this.value.position?.x) || 155 !isBaseOrResourceEqual(this.stageValue.position?.y, this.value.position?.y); 156 } else if (this.stageValue?.position !== undefined || this.value?.position !== undefined) { 157 positionResult = true; 158 } 159 if (isObject(this.stageValue?.style) && isObject(this.value?.style)) { 160 badgeSizeResult = !isBaseOrResourceEqual(this.stageValue.style?.badgeSize, this.value.style?.badgeSize); 161 borderWidthResult = !isBaseOrResourceEqual(this.stageValue.style?.borderWidth, this.value.style?.borderWidth); 162 badgeColorResult = !isBaseOrResourceEqual(this.stageValue.style?.badgeColor, this.value.style?.badgeColor); 163 borderColorResult = !isBaseOrResourceEqual(this.stageValue.style?.borderColor, this.value.style?.borderColor); 164 colorResult = !isBaseOrResourceEqual(this.stageValue.style?.color, this.value.style?.color); 165 fontSizeResult = !isBaseOrResourceEqual(this.stageValue.style?.fontSize, this.value.style?.fontSize); 166 fontWeightResult = !isBaseOrResourceEqual(this.stageValue.style?.fontWeight, this.value.style?.fontWeight); 167 } else if (this.stageValue?.style !== undefined || this.value?.style !== undefined) { 168 badgeSizeResult = true; 169 borderWidthResult = true; 170 badgeColorResult = true; 171 borderColorResult = true; 172 colorResult = true; 173 fontSizeResult = true; 174 fontWeightResult = true; 175 } 176 return positionResult || badgeSizeResult || borderWidthResult || badgeColorResult || borderColorResult || colorResult || 177 fontSizeResult || fontWeightResult || !isBaseOrResourceEqual(this.stageValue?.value, this.value?.value); 178 } 179} 180 181// @ts-ignore 182globalThis.Badge.attributeModifier = function (modifier: ArkComponent): void { 183 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 184 return new ArkBadgeComponent(nativePtr); 185 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 186 return new modifierJS.CommonModifier(nativePtr, classType); 187 }); 188}; 189