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' /> 17 18class ArkRadioComponent extends ArkComponent implements RadioAttribute { 19 constructor(nativePtr: KNode) { 20 super(nativePtr); 21 } 22 onGestureJudgeBegin(callback: (gestureInfo: GestureInfo, event: BaseGestureEvent) => GestureJudgeResult): this { 23 throw new Error('Method not implemented.'); 24 } 25 checked(value: boolean): this { 26 modifierWithKey(this._modifiersWithKeys, RadioCheckedModifier.identity, RadioCheckedModifier, value); 27 return this; 28 } 29 onChange(callback: (isChecked: boolean) => void): this { 30 throw new Error('Method not implemented.'); 31 } 32 radioStyle(value: RadioStyle): this { 33 modifierWithKey(this._modifiersWithKeys, RadioStyleModifier.identity, RadioStyleModifier, value); 34 return this; 35 } 36 width(value: Length): this { 37 modifierWithKey(this._modifiersWithKeys, RadioWidthModifier.identity, RadioWidthModifier, value); 38 return this; 39 } 40 height(value: Length): this { 41 modifierWithKey(this._modifiersWithKeys, RadioHeightModifier.identity, RadioHeightModifier, value); 42 return this; 43 } 44 size(value: { width: Length; height: Length }): this { 45 modifierWithKey(this._modifiersWithKeys, RadioSizeModifier.identity, RadioSizeModifier, value); 46 return this; 47 } 48 hoverEffect(value: HoverEffect): this { 49 modifierWithKey(this._modifiersWithKeys, RadioHoverEffectModifier.identity, RadioHoverEffectModifier, value); 50 return this; 51 } 52 padding(value: Padding | Length): this { 53 modifierWithKey(this._modifiersWithKeys, RadioPaddingModifier.identity, RadioPaddingModifier, value); 54 return this; 55 } 56 responseRegion(value: Array<Rectangle> | Rectangle): this { 57 modifierWithKey(this._modifiersWithKeys, RadioResponseRegionModifier.identity, 58 RadioResponseRegionModifier, value); 59 return this; 60 } 61} 62 63class RadioCheckedModifier extends ModifierWithKey<boolean> { 64 constructor(value: boolean) { 65 super(value); 66 } 67 static identity: Symbol = Symbol('radioChecked'); 68 applyPeer(node: KNode, reset: boolean): void { 69 if (reset) { 70 getUINativeModule().radio.resetRadioChecked(node); 71 } else { 72 getUINativeModule().radio.setRadioChecked(node, this.value!); 73 } 74 } 75} 76 77class RadioStyleModifier extends ModifierWithKey<RadioStyle> { 78 constructor(value: RadioStyle) { 79 super(value); 80 } 81 static identity: Symbol = Symbol('radioStyle'); 82 applyPeer(node: KNode, reset: boolean): void { 83 if (reset) { 84 getUINativeModule().radio.resetRadioStyle(node); 85 } else { 86 getUINativeModule().radio.setRadioStyle( 87 node, this.value.checkedBackgroundColor, this.value.uncheckedBorderColor, this.value.indicatorColor); 88 } 89 } 90 91 checkObjectDiff(): boolean { 92 let checkedBackgroundColorEQ = 93 isBaseOrResourceEqual(this.stageValue.checkedBackgroundColor, 94 this.value.checkedBackgroundColor); 95 let uncheckedBorderColorEQ = 96 isBaseOrResourceEqual(this.stageValue.uncheckedBorderColor, 97 this.value.uncheckedBorderColor); 98 let indicatorColorEQ = 99 isBaseOrResourceEqual(this.stageValue.indicatorColor, 100 this.value.indicatorColor); 101 return !checkedBackgroundColorEQ || 102 !uncheckedBorderColorEQ || 103 !indicatorColorEQ; 104 } 105} 106 107class RadioWidthModifier extends ModifierWithKey<Length> { 108 constructor(value: Length) { 109 super(value); 110 } 111 static identity: Symbol = Symbol('radioWidth'); 112 applyPeer(node: KNode, reset: boolean): void { 113 if (reset) { 114 getUINativeModule().radio.resetRadioWidth(node); 115 } else { 116 getUINativeModule().radio.setRadioWidth(node, this.value); 117 } 118 } 119 120 checkObjectDiff(): boolean { 121 return !isBaseOrResourceEqual(this.stageValue, this.value); 122 } 123} 124 125class RadioHeightModifier extends ModifierWithKey<Length> { 126 constructor(value: Length) { 127 super(value); 128 } 129 static identity: Symbol = Symbol('radioHeight'); 130 applyPeer(node: KNode, reset: boolean): void { 131 if (reset) { 132 getUINativeModule().radio.resetRadioHeight(node); 133 } else { 134 getUINativeModule().radio.setRadioHeight(node, this.value); 135 } 136 } 137 138 checkObjectDiff(): boolean { 139 return !isBaseOrResourceEqual(this.stageValue, this.value); 140 } 141} 142 143class RadioSizeModifier extends ModifierWithKey<{ width: Length; height: Length }> { 144 constructor(value: { width: Length; height: Length }) { 145 super(value); 146 } 147 static identity: Symbol = Symbol('radioSize'); 148 applyPeer(node: KNode, reset: boolean): void { 149 if (reset) { 150 getUINativeModule().radio.resetRadioSize(node); 151 } else { 152 getUINativeModule().radio.setRadioSize(node, this.value.width, this.value.height); 153 } 154 } 155 156 checkObjectDiff(): boolean { 157 return !isBaseOrResourceEqual(this.stageValue.width, this.value.width) || 158 !isBaseOrResourceEqual(this.stageValue.height, this.value.height); 159 } 160} 161 162class RadioHoverEffectModifier extends ModifierWithKey<HoverEffect> { 163 constructor(value: HoverEffect) { 164 super(value); 165 } 166 static identity: Symbol = Symbol('radioHoverEffect'); 167 applyPeer(node: KNode, reset: boolean): void { 168 if (reset) { 169 getUINativeModule().radio.resetRadioHoverEffect(node); 170 } else { 171 getUINativeModule().radio.setRadioHoverEffect(node, this.value); 172 } 173 } 174 175 checkObjectDiff(): boolean { 176 return !isBaseOrResourceEqual(this.stageValue, this.value); 177 } 178} 179 180class RadioPaddingModifier extends ModifierWithKey<Padding | Length> { 181 constructor(value: Padding | Length) { 182 super(value); 183 } 184 static identity: Symbol = Symbol('radioPadding'); 185 applyPeer(node: KNode, reset: boolean): void { 186 if (reset) { 187 getUINativeModule().radio.resetRadioPadding(node); 188 } else { 189 let paddingTop: Length; 190 let paddingRight: Length; 191 let paddingBottom: Length; 192 let paddingLeft: Length; 193 if (this.value !== null && this.value !== undefined) { 194 if (isLengthType(this.value) || isResource(this.value)) { 195 paddingTop = <Length> this.value; 196 paddingRight = <Length> this.value; 197 paddingBottom = <Length> this.value; 198 paddingLeft = <Length> this.value; 199 } else { 200 paddingTop = (<Padding> this.value).top; 201 paddingRight = (<Padding> this.value).right; 202 paddingBottom = (<Padding> this.value).bottom; 203 paddingLeft = (<Padding> this.value).left; 204 } 205 } 206 getUINativeModule().radio.setRadioPadding(node, paddingTop, paddingRight, paddingBottom, paddingLeft); 207 } 208 } 209 210 checkObjectDiff(): boolean { 211 if (isResource(this.stageValue) && isResource(this.value)) { 212 return !isResourceEqual(this.stageValue, this.value); 213 } else if (!isResource(this.stageValue) && !isResource(this.value)) { 214 return !((this.stageValue as Padding).left === (this.value as Padding).left && 215 (this.stageValue as Padding).right === (this.value as Padding).right && 216 (this.stageValue as Padding).top === (this.value as Padding).top && 217 (this.stageValue as Padding).bottom === (this.value as Padding).bottom); 218 } else { 219 return true; 220 } 221 } 222} 223 224class RadioResponseRegionModifier extends ModifierWithKey<Array<Rectangle> | Rectangle> { 225 constructor(value: Array<Rectangle> | Rectangle) { 226 super(value); 227 } 228 static identity = Symbol('radioResponseRegion'); 229 applyPeer(node: KNode, reset: boolean): void { 230 if (reset) { 231 getUINativeModule().radio.resetRadioResponseRegion(node); 232 } else { 233 let responseRegion: (number | string | Resource)[] = []; 234 if (Array.isArray(this.value)) { 235 for (let i = 0; i < this.value.length; i++) { 236 responseRegion.push(this.value[i].x ?? 'PLACEHOLDER'); 237 responseRegion.push(this.value[i].y ?? 'PLACEHOLDER'); 238 responseRegion.push(this.value[i].width ?? 'PLACEHOLDER'); 239 responseRegion.push(this.value[i].height ?? 'PLACEHOLDER'); 240 } 241 } else { 242 responseRegion.push(this.value.x ?? 'PLACEHOLDER'); 243 responseRegion.push(this.value.y ?? 'PLACEHOLDER'); 244 responseRegion.push(this.value.width ?? 'PLACEHOLDER'); 245 responseRegion.push(this.value.height ?? 'PLACEHOLDER'); 246 } 247 getUINativeModule().radio.setRadioResponseRegion(node, responseRegion, responseRegion.length); 248 } 249 } 250 251 checkObjectDiff(): boolean { 252 if (Array.isArray(this.value) && Array.isArray(this.stageValue)) { 253 if (this.value.length !== this.stageValue.length) { 254 return true; 255 } else { 256 for (let i = 0; i < this.value.length; i++) { 257 if (!(isBaseOrResourceEqual(this.stageValue[i].x, this.value[i].x) && 258 isBaseOrResourceEqual(this.stageValue[i].y, this.value[i].y) && 259 isBaseOrResourceEqual(this.stageValue[i].width, this.value[i].width) && 260 isBaseOrResourceEqual(this.stageValue[i].height, this.value[i].height) 261 )) { 262 return true; 263 } 264 } 265 return false; 266 } 267 } else if (!Array.isArray(this.value) && !Array.isArray(this.stageValue)) { 268 return (!(isBaseOrResourceEqual(this.stageValue.x, this.value.x) && 269 isBaseOrResourceEqual(this.stageValue.y, this.value.y) && 270 isBaseOrResourceEqual(this.stageValue.width, this.value.width) && 271 isBaseOrResourceEqual(this.stageValue.height, this.value.height) 272 )); 273 } else { 274 return true; 275 } 276 } 277} 278// @ts-ignore 279globalThis.Radio.attributeModifier = function (modifier) { 280 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 281 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 282 let component = this.createOrGetNode(elmtId, () => { 283 return new ArkRadioComponent(nativeNode); 284 }); 285 applyUIAttributes(modifier, nativeNode, component); 286 component.applyModifierPatch(); 287};