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 builder: WrappedBuilder<Object[]> | null = null; 19 checkboxNode: BuilderNode<[CheckBoxConfiguration]> | null = null; 20 modifier: ContentModifier<CheckBoxConfiguration>; 21 needRebuild: boolean = false; 22 constructor(nativePtr: KNode, classType?: ModifierType) { 23 super(nativePtr, classType); 24 } 25 shape(value: CheckBoxShape): this { 26 modifierWithKey(this._modifiersWithKeys, CheckBoxShapeModifier.identity, CheckBoxShapeModifier, value); 27 return this; 28 } 29 width(value: Length): this { 30 modifierWithKey( 31 this._modifiersWithKeys, CheckboxWidthModifier.identity, CheckboxWidthModifier, value); 32 return this; 33 } 34 height(value: Length): this { 35 modifierWithKey( 36 this._modifiersWithKeys, CheckboxHeightModifier.identity, CheckboxHeightModifier, value); 37 return this; 38 } 39 select(value: boolean): this { 40 modifierWithKey( 41 this._modifiersWithKeys, CheckboxSelectModifier.identity, CheckboxSelectModifier, value); 42 return this; 43 } 44 selectedColor(value: ResourceColor): this { 45 modifierWithKey( 46 this._modifiersWithKeys, CheckboxSelectedColorModifier.identity, CheckboxSelectedColorModifier, value); 47 48 return this; 49 } 50 unselectedColor(value: ResourceColor): this { 51 modifierWithKey( 52 this._modifiersWithKeys, CheckboxUnselectedColorModifier.identity, CheckboxUnselectedColorModifier, value); 53 return this; 54 } 55 mark(value: MarkStyle): this { 56 modifierWithKey( 57 this._modifiersWithKeys, CheckboxMarkModifier.identity, CheckboxMarkModifier, value); 58 return this; 59 } 60 padding(value: Padding | Length): this { 61 let arkValue = new ArkPadding(); 62 if (value !== null && value !== undefined) { 63 if (isLengthType(value) || isResource(value)) { 64 arkValue.top = <Length>value; 65 arkValue.right = <Length>value; 66 arkValue.bottom = <Length>value; 67 arkValue.left = <Length>value; 68 } else { 69 arkValue.top = (<Padding>value).top; 70 arkValue.right = (<Padding>value).right; 71 arkValue.bottom = (<Padding>value).bottom; 72 arkValue.left = (<Padding>value).left; 73 } 74 modifierWithKey(this._modifiersWithKeys, CheckBoxPaddingModifier.identity, CheckBoxPaddingModifier, arkValue); 75 } else { 76 modifierWithKey(this._modifiersWithKeys, CheckBoxPaddingModifier.identity, CheckBoxPaddingModifier, undefined); 77 } 78 return this; 79 } 80 size(value: SizeOptions): this { 81 modifierWithKey(this._modifiersWithKeys, CheckBoxSizeModifier.identity, CheckBoxSizeModifier, value); 82 return this; 83 } 84 responseRegion(value: Array<Rectangle> | Rectangle): this { 85 modifierWithKey( 86 this._modifiersWithKeys, CheckBoxResponseRegionModifier.identity, CheckBoxResponseRegionModifier, value); 87 return this; 88 } 89 contentModifier(value: ContentModifier<CheckBoxConfiguration>): this { 90 modifierWithKey(this._modifiersWithKeys, CheckBoxContentModifier.identity, CheckBoxContentModifier, value); 91 return this; 92 } 93 setContentModifier(modifier: ContentModifier<CheckBoxConfiguration>): this { 94 if (modifier === undefined || modifier === null) { 95 getUINativeModule().checkbox.setContentModifierBuilder(this.nativePtr, false); 96 return; 97 } 98 this.needRebuild = false; 99 if (this.builder !== modifier.applyContent()) { 100 this.needRebuild = true; 101 } 102 this.builder = modifier.applyContent(); 103 this.modifier = modifier; 104 getUINativeModule().checkbox.setContentModifierBuilder(this.nativePtr, this); 105 } 106 makeContentModifierNode(context: UIContext, checkBoxConfiguration: CheckBoxConfiguration): FrameNode | null { 107 checkBoxConfiguration.contentModifier = this.modifier; 108 if (isUndefined(this.checkboxNode) || this.needRebuild) { 109 const xNode = globalThis.requireNapi('arkui.node'); 110 this.checkboxNode = new xNode.BuilderNode(context); 111 this.checkboxNode.build(this.builder, checkBoxConfiguration); 112 this.needRebuild = false; 113 } else { 114 this.checkboxNode.update(checkBoxConfiguration); 115 } 116 return this.checkboxNode.getFrameNode(); 117 } 118 onChange(callback: (value: boolean) => void): this { 119 throw new Error('Method not implemented.'); 120 } 121} 122 123class CheckBoxResponseRegionModifier extends ModifierWithKey<Array<Rectangle> | Rectangle> { 124 constructor(value: Array<Rectangle> | Rectangle) { 125 super(value); 126 } 127 static identity = Symbol('responseRegion'); 128 applyPeer(node: KNode, reset: boolean): void { 129 if (reset) { 130 getUINativeModule().checkbox.resetCheckboxResponseRegion(node); 131 } else { 132 let responseRegion: (number | string | Resource)[] = []; 133 if (Array.isArray(this.value)) { 134 for (let i = 0; i < this.value.length; i++) { 135 responseRegion.push(this.value[i].x ?? 'PLACEHOLDER'); 136 responseRegion.push(this.value[i].y ?? 'PLACEHOLDER'); 137 responseRegion.push(this.value[i].width ?? 'PLACEHOLDER'); 138 responseRegion.push(this.value[i].height ?? 'PLACEHOLDER'); 139 } 140 } else { 141 responseRegion.push(this.value.x ?? 'PLACEHOLDER'); 142 responseRegion.push(this.value.y ?? 'PLACEHOLDER'); 143 responseRegion.push(this.value.width ?? 'PLACEHOLDER'); 144 responseRegion.push(this.value.height ?? 'PLACEHOLDER'); 145 } 146 getUINativeModule().checkbox.setCheckboxResponseRegion(node, responseRegion, responseRegion.length); 147 } 148 } 149 150 checkObjectDiff(): boolean { 151 if (Array.isArray(this.value) && Array.isArray(this.stageValue)) { 152 if (this.value.length !== this.stageValue.length) { 153 return true; 154 } else { 155 for (let i = 0; i < this.value.length; i++) { 156 if (!(isBaseOrResourceEqual(this.stageValue[i].x, this.value[i].x) && 157 isBaseOrResourceEqual(this.stageValue[i].y, this.value[i].y) && 158 isBaseOrResourceEqual(this.stageValue[i].width, this.value[i].width) && 159 isBaseOrResourceEqual(this.stageValue[i].height, this.value[i].height) 160 )) { 161 return true; 162 } 163 } 164 return false; 165 } 166 } else if (!Array.isArray(this.value) && !Array.isArray(this.stageValue)) { 167 return (!(isBaseOrResourceEqual(this.stageValue.x, this.value.x) && 168 isBaseOrResourceEqual(this.stageValue.y, this.value.y) && 169 isBaseOrResourceEqual(this.stageValue.width, this.value.width) && 170 isBaseOrResourceEqual(this.stageValue.height, this.value.height) 171 )); 172 } else { 173 return true; 174 } 175 } 176} 177 178class CheckBoxContentModifier extends ModifierWithKey<ContentModifier<CheckBoxConfiguration>> { 179 constructor(value: ContentModifier<CheckBoxConfiguration>) { 180 super(value); 181 } 182 static identity: Symbol = Symbol('checkBoxContentModifier'); 183 applyPeer(node: KNode, reset: boolean, component: ArkComponent): void { 184 let checkboxComponent = component as ArkCheckboxComponent; 185 checkboxComponent.setContentModifier(this.value); 186 } 187} 188 189class CheckBoxShapeModifier extends ModifierWithKey<CheckBoxShape> { 190 constructor(value: CheckBoxShape) { 191 super(value); 192 } 193 static identity: Symbol = Symbol('checkboxShape'); 194 applyPeer(node: KNode, reset: boolean): void { 195 if (reset) { 196 getUINativeModule().checkbox.resetCheckboxShape(node); 197 } else { 198 getUINativeModule().checkbox.setCheckboxShape(node, this.value); 199 } 200 } 201 202 checkObjectDiff(): boolean { 203 return !isBaseOrResourceEqual(this.stageValue, this.value); 204 } 205} 206 207class CheckBoxSizeModifier extends ModifierWithKey<SizeOptions> { 208 constructor(value: SizeOptions) { 209 super(value); 210 } 211 static identity: Symbol = Symbol('size'); 212 applyPeer(node: KNode, reset: boolean): void { 213 if (reset) { 214 getUINativeModule().checkbox.resetCheckboxSize(node); 215 } else { 216 getUINativeModule().checkbox.setCheckboxSize(node, this.value.width, this.value.height); 217 } 218 } 219 220 checkObjectDiff(): boolean { 221 return !isBaseOrResourceEqual(this.stageValue.width, this.value.width) || 222 !isBaseOrResourceEqual(this.stageValue.height, this.value.height); 223 } 224} 225 226class CheckBoxPaddingModifier extends ModifierWithKey<ArkPadding> { 227 constructor(value: ArkPadding) { 228 super(value); 229 } 230 static identity: Symbol = Symbol('padding'); 231 applyPeer(node: KNode, reset: boolean): void { 232 if (reset) { 233 getUINativeModule().checkbox.resetCheckboxPadding(node); 234 } else { 235 getUINativeModule().checkbox.setCheckboxPadding(node, this.value.top, 236 this.value.right, this.value.bottom, this.value.left); 237 } 238 } 239 240 checkObjectDiff(): boolean { 241 return !isBaseOrResourceEqual(this.stageValue.top, this.value.top) || 242 !isBaseOrResourceEqual(this.stageValue.right, this.value.right) || 243 !isBaseOrResourceEqual(this.stageValue.bottom, this.value.bottom) || 244 !isBaseOrResourceEqual(this.stageValue.left, this.value.left); 245 } 246} 247 248class CheckboxMarkModifier extends ModifierWithKey<MarkStyle> { 249 constructor(value: MarkStyle) { 250 super(value); 251 } 252 static identity: Symbol = Symbol('checkboxMark'); 253 applyPeer(node: KNode, reset: boolean): void { 254 if (reset) { 255 getUINativeModule().checkbox.resetMark(node); 256 } else { 257 getUINativeModule().checkbox.setMark(node, this.value?.strokeColor, this.value?.size, this.value?.strokeWidth); 258 } 259 } 260 261 checkObjectDiff(): boolean { 262 let colorEQ = isBaseOrResourceEqual(this.stageValue.strokeColor, this.value.strokeColor); 263 let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size); 264 let widthEQ = isBaseOrResourceEqual(this.stageValue.strokeWidth, this.value.strokeWidth); 265 return !colorEQ || !sizeEQ || !widthEQ; 266 } 267} 268 269class CheckboxSelectModifier extends ModifierWithKey<boolean> { 270 constructor(value: boolean) { 271 super(value); 272 } 273 static identity: Symbol = Symbol('checkboxSelect'); 274 applyPeer(node: KNode, reset: boolean): void { 275 if (reset) { 276 getUINativeModule().checkbox.resetSelect(node); 277 } else { 278 getUINativeModule().checkbox.setSelect(node, this.value); 279 } 280 } 281 282 checkObjectDiff(): boolean { 283 return this.stageValue !== this.value; 284 } 285} 286 287class CheckboxHeightModifier extends ModifierWithKey<ResourceColor> { 288 constructor(value: ResourceColor) { 289 super(value); 290 } 291 static identity: Symbol = Symbol('checkboxHeight'); 292 applyPeer(node: KNode, reset: boolean): void { 293 if (reset) { 294 getUINativeModule().checkbox.resetHeight(node); 295 } else { 296 getUINativeModule().checkbox.setHeight(node, this.value); 297 } 298 } 299 300 checkObjectDiff(): boolean { 301 return !isBaseOrResourceEqual(this.stageValue, this.value); 302 } 303} 304 305class CheckboxWidthModifier extends ModifierWithKey<Length> { 306 constructor(value: Length) { 307 super(value); 308 } 309 static identity: Symbol = Symbol('checkboxWidth'); 310 applyPeer(node: KNode, reset: boolean): void { 311 if (reset) { 312 getUINativeModule().checkbox.resetWidth(node); 313 } else { 314 getUINativeModule().checkbox.setWidth(node, this.value); 315 } 316 } 317 318 checkObjectDiff(): boolean { 319 return !isBaseOrResourceEqual(this.stageValue, this.value); 320 } 321} 322 323class CheckboxSelectedColorModifier extends ModifierWithKey<ResourceColor> { 324 constructor(value: ResourceColor) { 325 super(value); 326 } 327 static identity: Symbol = Symbol('checkboxSelectedColor'); 328 applyPeer(node: KNode, reset: boolean): void { 329 if (reset) { 330 getUINativeModule().checkbox.resetSelectedColor(node); 331 } else { 332 getUINativeModule().checkbox.setSelectedColor(node, this.value); 333 } 334 } 335 336 checkObjectDiff(): boolean { 337 return !isBaseOrResourceEqual(this.stageValue, this.value); 338 } 339} 340 341class CheckboxUnselectedColorModifier extends ModifierWithKey<ResourceColor> { 342 constructor(value: ResourceColor) { 343 super(value); 344 } 345 static identity: Symbol = Symbol('checkboxUnselectedColor'); 346 applyPeer(node: KNode, reset: boolean): void { 347 if (reset) { 348 getUINativeModule().checkbox.resetUnSelectedColor(node); 349 } else { 350 getUINativeModule().checkbox.setUnSelectedColor(node, this.value); 351 } 352 } 353 354 checkObjectDiff(): boolean { 355 return !isBaseOrResourceEqual(this.stageValue, this.value); 356 } 357} 358 359// @ts-ignore 360globalThis.Checkbox.attributeModifier = function (modifier: ArkComponent): void { 361 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 362 return new ArkCheckboxComponent(nativePtr); 363 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 364 return new modifierJS.CheckboxModifier(nativePtr, classType); 365 }); 366}; 367 368// @ts-ignore 369globalThis.Checkbox.contentModifier = function (modifier: ContentModifier<CheckBoxConfiguration>): void { 370 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 371 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 372 let component = this.createOrGetNode(elmtId, () => { 373 return new ArkCheckboxComponent(nativeNode); 374 }); 375 component.setContentModifier(modifier); 376}; 377