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 ArkCheckboxComponent extends ArkComponent implements CheckboxAttribute { 18 constructor(nativePtr: KNode) { 19 super(nativePtr); 20 } 21 shape(value: CheckBoxShape): this { 22 throw new Error('Method not implemented.'); 23 } 24 width(value: Length): this { 25 modifierWithKey( 26 this._modifiersWithKeys, CheckboxWidthModifier.identity, CheckboxWidthModifier, value); 27 return this; 28 } 29 height(value: Length): this { 30 modifierWithKey( 31 this._modifiersWithKeys, CheckboxHeightModifier.identity, CheckboxHeightModifier, value); 32 return this; 33 } 34 select(value: boolean): this { 35 modifierWithKey( 36 this._modifiersWithKeys, CheckboxSelectModifier.identity, CheckboxSelectModifier, value); 37 return this; 38 } 39 selectedColor(value: ResourceColor): this { 40 modifierWithKey( 41 this._modifiersWithKeys, CheckboxSelectedColorModifier.identity, CheckboxSelectedColorModifier, value); 42 43 return this; 44 } 45 unselectedColor(value: ResourceColor): this { 46 modifierWithKey( 47 this._modifiersWithKeys, CheckboxUnselectedColorModifier.identity, CheckboxUnselectedColorModifier, value); 48 return this; 49 } 50 mark(value: MarkStyle): this { 51 modifierWithKey( 52 this._modifiersWithKeys, CheckboxMarkModifier.identity, CheckboxMarkModifier, value); 53 return this; 54 } 55 padding(value: Padding | Length): this { 56 let arkValue = new ArkPadding(); 57 if (value !== null && value !== undefined) { 58 if (isLengthType(value) || isResource(value)) { 59 arkValue.top = <Length>value; 60 arkValue.right = <Length>value; 61 arkValue.bottom = <Length>value; 62 arkValue.left = <Length>value; 63 } else { 64 arkValue.top = (<Padding>value).top; 65 arkValue.right = (<Padding>value).right; 66 arkValue.bottom = (<Padding>value).bottom; 67 arkValue.left = (<Padding>value).left; 68 } 69 modifierWithKey(this._modifiersWithKeys, CheckBoxPaddingModifier.identity, CheckBoxPaddingModifier, arkValue); 70 } else { 71 modifierWithKey(this._modifiersWithKeys, CheckBoxPaddingModifier.identity, CheckBoxPaddingModifier, undefined); 72 } 73 return this; 74 } 75 size(value: SizeOptions): this { 76 modifierWithKey(this._modifiersWithKeys, CheckBoxSizeModifier.identity, CheckBoxSizeModifier, value); 77 return this; 78 } 79 responseRegion(value: Array<Rectangle> | Rectangle): this { 80 modifierWithKey( 81 this._modifiersWithKeys, CheckBoxResponseRegionModifier.identity, CheckBoxResponseRegionModifier, value); 82 return this; 83 } 84 onChange(callback: (value: boolean) => void): this { 85 throw new Error('Method not implemented.'); 86 } 87} 88 89class CheckBoxResponseRegionModifier extends ModifierWithKey<Array<Rectangle> | Rectangle> { 90 constructor(value: Array<Rectangle> | Rectangle) { 91 super(value); 92 } 93 static identity = Symbol('responseRegion'); 94 applyPeer(node: KNode, reset: boolean): void { 95 if (reset) { 96 getUINativeModule().checkbox.resetCheckboxResponseRegion(node); 97 } else { 98 let responseRegion: (number | string | Resource)[] = []; 99 if (Array.isArray(this.value)) { 100 for (let i = 0; i < this.value.length; i++) { 101 responseRegion.push(this.value[i].x ?? 'PLACEHOLDER'); 102 responseRegion.push(this.value[i].y ?? 'PLACEHOLDER'); 103 responseRegion.push(this.value[i].width ?? 'PLACEHOLDER'); 104 responseRegion.push(this.value[i].height ?? 'PLACEHOLDER'); 105 } 106 } else { 107 responseRegion.push(this.value.x ?? 'PLACEHOLDER'); 108 responseRegion.push(this.value.y ?? 'PLACEHOLDER'); 109 responseRegion.push(this.value.width ?? 'PLACEHOLDER'); 110 responseRegion.push(this.value.height ?? 'PLACEHOLDER'); 111 } 112 getUINativeModule().checkbox.setCheckboxResponseRegion(node, responseRegion, responseRegion.length); 113 } 114 } 115 116 checkObjectDiff(): boolean { 117 if (Array.isArray(this.value) && Array.isArray(this.stageValue)) { 118 if (this.value.length !== this.stageValue.length) { 119 return true; 120 } else { 121 for (let i = 0; i < this.value.length; i++) { 122 if (!(isBaseOrResourceEqual(this.stageValue[i].x, this.value[i].x) && 123 isBaseOrResourceEqual(this.stageValue[i].y, this.value[i].y) && 124 isBaseOrResourceEqual(this.stageValue[i].width, this.value[i].width) && 125 isBaseOrResourceEqual(this.stageValue[i].height, this.value[i].height) 126 )) { 127 return true; 128 } 129 } 130 return false; 131 } 132 } else if (!Array.isArray(this.value) && !Array.isArray(this.stageValue)) { 133 return (!(isBaseOrResourceEqual(this.stageValue.x, this.value.x) && 134 isBaseOrResourceEqual(this.stageValue.y, this.value.y) && 135 isBaseOrResourceEqual(this.stageValue.width, this.value.width) && 136 isBaseOrResourceEqual(this.stageValue.height, this.value.height) 137 )); 138 } else { 139 return true; 140 } 141 } 142} 143 144class CheckBoxSizeModifier extends ModifierWithKey<SizeOptions> { 145 constructor(value: SizeOptions) { 146 super(value); 147 } 148 static identity: Symbol = Symbol('size'); 149 applyPeer(node: KNode, reset: boolean): void { 150 if (reset) { 151 getUINativeModule().checkbox.resetCheckboxSize(node); 152 } else { 153 getUINativeModule().checkbox.setCheckboxSize(node, this.value.width, this.value.height); 154 } 155 } 156 157 checkObjectDiff(): boolean { 158 return !isBaseOrResourceEqual(this.stageValue.width, this.value.width) || 159 !isBaseOrResourceEqual(this.stageValue.height, this.value.height); 160 } 161} 162 163class CheckBoxPaddingModifier extends ModifierWithKey<ArkPadding> { 164 constructor(value: ArkPadding) { 165 super(value); 166 } 167 static identity: Symbol = Symbol('padding'); 168 applyPeer(node: KNode, reset: boolean): void { 169 if (reset) { 170 getUINativeModule().checkbox.resetCheckboxPadding(node); 171 } else { 172 getUINativeModule().checkbox.setCheckboxPadding(node, this.value.top, 173 this.value.right, this.value.bottom, this.value.left); 174 } 175 } 176 177 checkObjectDiff(): boolean { 178 return !isBaseOrResourceEqual(this.stageValue.top, this.value.top) || 179 !isBaseOrResourceEqual(this.stageValue.right, this.value.right) || 180 !isBaseOrResourceEqual(this.stageValue.bottom, this.value.bottom) || 181 !isBaseOrResourceEqual(this.stageValue.left, this.value.left); 182 } 183} 184 185class CheckboxMarkModifier extends ModifierWithKey<MarkStyle> { 186 constructor(value: MarkStyle) { 187 super(value); 188 } 189 static identity: Symbol = Symbol('checkboxMark'); 190 applyPeer(node: KNode, reset: boolean): void { 191 if (reset) { 192 getUINativeModule().checkbox.resetMark(node); 193 } else { 194 getUINativeModule().checkbox.setMark(node, this.value?.strokeColor, this.value?.size, this.value?.strokeWidth); 195 } 196 } 197 198 checkObjectDiff(): boolean { 199 let colorEQ = isBaseOrResourceEqual(this.stageValue.strokeColor, this.value.strokeColor); 200 let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size); 201 let widthEQ = isBaseOrResourceEqual(this.stageValue.strokeWidth, this.value.strokeWidth); 202 return !colorEQ || !sizeEQ || !widthEQ; 203 } 204} 205 206class CheckboxSelectModifier extends ModifierWithKey<boolean> { 207 constructor(value: boolean) { 208 super(value); 209 } 210 static identity: Symbol = Symbol('checkboxSelect'); 211 applyPeer(node: KNode, reset: boolean): void { 212 if (reset) { 213 getUINativeModule().checkbox.resetSelect(node); 214 } else { 215 getUINativeModule().checkbox.setSelect(node, this.value); 216 } 217 } 218 219 checkObjectDiff(): boolean { 220 return this.stageValue !== this.value; 221 } 222} 223 224class CheckboxHeightModifier extends ModifierWithKey<ResourceColor> { 225 constructor(value: ResourceColor) { 226 super(value); 227 } 228 static identity: Symbol = Symbol('checkboxHeight'); 229 applyPeer(node: KNode, reset: boolean): void { 230 if (reset) { 231 getUINativeModule().checkbox.resetHeight(node); 232 } else { 233 getUINativeModule().checkbox.setHeight(node, this.value); 234 } 235 } 236 237 checkObjectDiff(): boolean { 238 return !isBaseOrResourceEqual(this.stageValue, this.value); 239 } 240} 241 242class CheckboxWidthModifier extends ModifierWithKey<Length> { 243 constructor(value: Length) { 244 super(value); 245 } 246 static identity: Symbol = Symbol('checkboxWidth'); 247 applyPeer(node: KNode, reset: boolean): void { 248 if (reset) { 249 getUINativeModule().checkbox.resetWidth(node); 250 } else { 251 getUINativeModule().checkbox.setWidth(node, this.value); 252 } 253 } 254 255 checkObjectDiff(): boolean { 256 return !isBaseOrResourceEqual(this.stageValue, this.value); 257 } 258} 259 260class CheckboxSelectedColorModifier extends ModifierWithKey<ResourceColor> { 261 constructor(value: ResourceColor) { 262 super(value); 263 } 264 static identity: Symbol = Symbol('checkboxSelectedColor'); 265 applyPeer(node: KNode, reset: boolean): void { 266 if (reset) { 267 getUINativeModule().checkbox.resetSelectedColor(node); 268 } else { 269 getUINativeModule().checkbox.setSelectedColor(node, this.value); 270 } 271 } 272 273 checkObjectDiff(): boolean { 274 return !isBaseOrResourceEqual(this.stageValue, this.value); 275 } 276} 277 278class CheckboxUnselectedColorModifier extends ModifierWithKey<ResourceColor> { 279 constructor(value: ResourceColor) { 280 super(value); 281 } 282 static identity: Symbol = Symbol('checkboxUnselectedColor'); 283 applyPeer(node: KNode, reset: boolean): void { 284 if (reset) { 285 getUINativeModule().checkbox.resetUnSelectedColor(node); 286 } else { 287 getUINativeModule().checkbox.setUnSelectedColor(node, this.value); 288 } 289 } 290 291 checkObjectDiff(): boolean { 292 return !isBaseOrResourceEqual(this.stageValue, this.value); 293 } 294} 295 296// @ts-ignore 297globalThis.Checkbox.attributeModifier = function (modifier) { 298 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 299 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 300 let component = this.createOrGetNode(elmtId, () => { 301 return new ArkCheckboxComponent(nativeNode); 302 }); 303 applyUIAttributes(modifier, nativeNode, component); 304 component.applyModifierPatch(); 305}; 306