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/// <reference path="./ArkComponent.ts" /> 18const FontWeightMap = { 19 0: 'lighter', 20 1: 'normal', 21 2: 'regular', 22 3: 'medium', 23 4: 'bold', 24 5: 'bolder', 25 100: '100', 26 200: '200', 27 300: '300', 28 400: '400', 29 500: '500', 30 600: '600', 31 700: '700', 32 800: '800', 33 900: '900', 34}; 35 36class ArkButtonComponent extends ArkComponent implements ButtonAttribute { 37 builder: WrappedBuilder<Object[]> | null = null; 38 buttonNode: BuilderNode<[ButtonConfiguration]> | null = null; 39 modifier: ContentModifier<ButtonConfiguration>; 40 needRebuild: boolean = false; 41 applyContent: WrappedBuilder<[ButtonConfiguration]>; 42 constructor(nativePtr: KNode, classType?: ModifierType) { 43 super(nativePtr, classType); 44 this._needDiff = false; 45 } 46 allowChildCount(): number { 47 return 1; 48 } 49 initialize(value: Object[]): this { 50 if (value.length >= 1 && (isResource(value[0]) || isString(value[0]))) { 51 modifierWithKey(this._modifiersWithKeys, ButtonCreateTypeModifier.identity, ButtonCreateTypeModifier, true); 52 } else { 53 modifierWithKey(this._modifiersWithKeys, ButtonCreateTypeModifier.identity, ButtonCreateTypeModifier, false); 54 } 55 if (value.length === 1) { 56 if (!isNull(value[0]) && isObject(value[0])) { 57 modifierWithKey(this._modifiersWithKeys, ButtonOptionsModifier.identity, ButtonOptionsModifier, value[0]); 58 } else if (isResource(value[0]) || isString(value[0])) { 59 modifierWithKey(this._modifiersWithKeys, ButtonLabelModifier.identity, ButtonLabelModifier, value[0]); 60 } else { 61 modifierWithKey(this._modifiersWithKeys, ButtonLabelModifier.identity, ButtonLabelModifier, undefined); 62 modifierWithKey(this._modifiersWithKeys, ButtonOptionsModifier.identity, ButtonOptionsModifier, undefined); 63 } 64 } else if (value.length === 2) { 65 modifierWithKey(this._modifiersWithKeys, ButtonLabelModifier.identity, ButtonLabelModifier, value[0]); 66 modifierWithKey(this._modifiersWithKeys, ButtonOptionsModifier.identity, ButtonOptionsModifier, value[1]); 67 } else { 68 modifierWithKey(this._modifiersWithKeys, ButtonLabelModifier.identity, ButtonLabelModifier, undefined); 69 modifierWithKey(this._modifiersWithKeys, ButtonOptionsModifier.identity, ButtonOptionsModifier, undefined); 70 } 71 return this; 72 } 73 backgroundColor(value: ResourceColor): this { 74 modifierWithKey(this._modifiersWithKeys, ButtonBackgroundColorModifier.identity, ButtonBackgroundColorModifier, value); 75 return this; 76 } 77 type(value: ButtonType): this { 78 modifierWithKey(this._modifiersWithKeys, ButtonTypeModifier.identity, ButtonTypeModifier, value); 79 return this; 80 } 81 stateEffect(value: boolean): this { 82 modifierWithKey(this._modifiersWithKeys, ButtonStateEffectModifier.identity, ButtonStateEffectModifier, value); 83 return this; 84 } 85 fontColor(value: ResourceColor): this { 86 modifierWithKey(this._modifiersWithKeys, ButtonFontColorModifier.identity, ButtonFontColorModifier, value); 87 return this; 88 } 89 fontSize(value: Length): this { 90 modifierWithKey(this._modifiersWithKeys, ButtonFontSizeModifier.identity, ButtonFontSizeModifier, value); 91 return this; 92 } 93 fontWeight(value: string | number | FontWeight): this { 94 modifierWithKey(this._modifiersWithKeys, ButtonFontWeightModifier.identity, ButtonFontWeightModifier, value); 95 return this; 96 } 97 fontStyle(value: FontStyle): this { 98 modifierWithKey(this._modifiersWithKeys, ButtonFontStyleModifier.identity, ButtonFontStyleModifier, value); 99 return this; 100 } 101 fontFamily(value: string | Resource): this { 102 modifierWithKey(this._modifiersWithKeys, ButtonFontFamilyModifier.identity, ButtonFontFamilyModifier, value); 103 return this; 104 } 105 labelStyle(value: LabelStyle): this { 106 modifierWithKey(this._modifiersWithKeys, ButtonLabelStyleModifier.identity, ButtonLabelStyleModifier, value); 107 return this; 108 } 109 borderRadius(value: Length | BorderRadiuses): this { 110 modifierWithKey(this._modifiersWithKeys, ButtonBorderRadiusModifier.identity, ButtonBorderRadiusModifier, value); 111 return this; 112 } 113 border(value: BorderOptions): this { 114 modifierWithKey(this._modifiersWithKeys, ButtonBorderModifier.identity, ButtonBorderModifier, value); 115 return this; 116 } 117 size(value: SizeOptions): this { 118 modifierWithKey(this._modifiersWithKeys, ButtonSizeModifier.identity, ButtonSizeModifier, value); 119 return this; 120 } 121 contentModifier(value: ContentModifier<ButtonConfiguration>): this { 122 modifierWithKey(this._modifiersWithKeys, ButtonContentModifier.identity, ButtonContentModifier, value); 123 return this; 124 } 125 setContentModifier(modifier: ContentModifier<ButtonConfiguration>): this { 126 if (modifier === undefined || modifier === null) { 127 getUINativeModule().button.setContentModifierBuilder(this.nativePtr, false); 128 return; 129 } 130 this.needRebuild = false; 131 this.applyContent = modifier.applyContent(); 132 if (this.builder !== this.applyContent) { 133 this.needRebuild = true; 134 } 135 this.builder = this.applyContent; 136 this.modifier = modifier; 137 getUINativeModule().button.setContentModifierBuilder(this.nativePtr, this); 138 } 139 makeContentModifierNode(context: UIContext, buttonConfiguration: ButtonConfiguration): FrameNode | null { 140 buttonConfiguration.contentModifier = this.modifier; 141 if (isUndefined(this.buttonNode) || this.needRebuild) { 142 const xNode = globalThis.requireNapi('arkui.node'); 143 this.buttonNode = new xNode.BuilderNode(context); 144 this.buttonNode.build(this.builder, buttonConfiguration); 145 this.needRebuild = false; 146 } else { 147 this.buttonNode.update(buttonConfiguration); 148 } 149 return this.buttonNode.getFrameNode(); 150 } 151 role(value: ButtonRole): this { 152 modifierWithKey(this._modifiersWithKeys, ButtonRoleModifier.identity, ButtonRoleModifier, value); 153 return this; 154 } 155 buttonStyle(value: ButtonStyleMode): this { 156 modifierWithKey(this._modifiersWithKeys, ButtonStyleModifier.identity, ButtonStyleModifier, value); 157 return this; 158 } 159 controlSize(value: ControlSize): this { 160 modifierWithKey(this._modifiersWithKeys, ButtonControlSizeModifier.identity, ButtonControlSizeModifier, value); 161 return this; 162 } 163 minFontScale(value: number | Resource): this { 164 modifierWithKey(this._modifiersWithKeys, ButtonMinFontScaleModifier.identity, ButtonMinFontScaleModifier, value); 165 return this; 166 } 167 maxFontScale(value: number | Resource): this { 168 modifierWithKey(this._modifiersWithKeys, ButtonMaxFontScaleModifier.identity, ButtonMaxFontScaleModifier, value); 169 return this; 170 } 171} 172class ButtonBackgroundColorModifier extends ModifierWithKey<ResourceColor> { 173 constructor(value: ResourceColor) { 174 super(value); 175 } 176 static identity: Symbol = Symbol('buttonBackgroundColor'); 177 applyPeer(node: KNode, reset: boolean): void { 178 if (reset) { 179 getUINativeModule().button.resetBackgroundColor(node); 180 } else { 181 getUINativeModule().button.setBackgroundColor(node, this.value); 182 } 183 } 184 185 checkObjectDiff(): boolean { 186 return !isBaseOrResourceEqual(this.stageValue, this.value); 187 } 188} 189class ButtonStateEffectModifier extends ModifierWithKey<boolean> { 190 constructor(value: boolean) { 191 super(value); 192 } 193 static identity: Symbol = Symbol('buttonStateEffect'); 194 applyPeer(node: KNode, reset: boolean): void { 195 if (reset) { 196 getUINativeModule().button.resetStateEffect(node); 197 } else { 198 getUINativeModule().button.setStateEffect(node, this.value); 199 } 200 } 201} 202class ButtonFontStyleModifier extends ModifierWithKey<number> { 203 constructor(value: number) { 204 super(value); 205 } 206 static identity: Symbol = Symbol('buttonFontStyle'); 207 applyPeer(node: KNode, reset: boolean): void { 208 if (reset) { 209 getUINativeModule().button.resetFontStyle(node); 210 } else { 211 getUINativeModule().button.setFontStyle(node, this.value); 212 } 213 } 214} 215class ButtonFontFamilyModifier extends ModifierWithKey<string | Resource> { 216 constructor(value: string | Resource) { 217 super(value); 218 } 219 static identity: Symbol = Symbol('buttonFontFamily'); 220 applyPeer(node: KNode, reset: boolean): void { 221 if (reset) { 222 getUINativeModule().button.resetFontFamily(node); 223 } else { 224 getUINativeModule().button.setFontFamily(node, this.value); 225 } 226 } 227 checkObjectDiff(): boolean { 228 return !isBaseOrResourceEqual(this.stageValue, this.value); 229 } 230} 231class ButtonLabelStyleModifier extends ModifierWithKey<LabelStyle> { 232 constructor(value: LabelStyle) { 233 super(value); 234 } 235 static identity: Symbol = Symbol('buttonLabelStyle'); 236 applyPeer(node: KNode, reset: boolean): void { 237 if (reset) { 238 getUINativeModule().button.resetLabelStyle(node); 239 } else { 240 let textOverflow = this.value.overflow; // number(enum) -> Ace::TextOverflow 241 let maxLines = this.value.maxLines; // number -> uint32_t 242 let minFontSize = this.value.minFontSize; // number | string | Resource -> Dimension 243 let maxFontSize = this.value.maxFontSize; // number | string | Resource -> Dimension 244 let heightAdaptivePolicy = this.value.heightAdaptivePolicy; // number(enum) -> Ace::TextHeightAdaptivePolicy 245 let fontSize; // number | string | Resource -> Dimension 246 let fontWeight; // number | string | Ace::FontWeight -> string -> Ace::FontWeight 247 let fontStyle; // number(enum) -> Ace::FontStyle 248 let fontFamily; // string -> std::vector<std::string> 249 if (isObject(this.value.font)) { 250 fontSize = this.value.font.size; 251 fontStyle = this.value.font.style; 252 fontFamily = this.value.font.family; 253 fontWeight = this.value.font.weight; 254 } 255 getUINativeModule().button.setLabelStyle(node, textOverflow, maxLines, minFontSize, maxFontSize, 256 heightAdaptivePolicy, fontSize, fontWeight, fontStyle, fontFamily); 257 } 258 } 259 checkObjectDiff(): boolean { 260 if (isResource(this.stageValue) && isResource(this.value)) { 261 return !isResourceEqual(this.stageValue, this.value); 262 } else if (!isResource(this.stageValue) && !isResource(this.value)) { 263 return !(this.value.overflow === this.stageValue.overflow && 264 this.value.maxLines === this.stageValue.maxLines && 265 this.value.minFontSize === this.stageValue.minFontSize && 266 this.value.maxFontSize === this.stageValue.maxFontSize && 267 this.value.heightAdaptivePolicy === this.stageValue.heightAdaptivePolicy && 268 this.value.font === this.stageValue.font); 269 } else { 270 return true; 271 } 272 } 273} 274class ButtonTypeModifier extends ModifierWithKey<number> { 275 constructor(value: number) { 276 super(value); 277 } 278 static identity: Symbol = Symbol('buttonType'); 279 applyStage(node: KNode, component?: ArkComponent): boolean { 280 if (this.stageValue === undefined || this.stageValue === null) { 281 this.value = this.stageValue; 282 this.applyPeer(node, true, component); 283 return true; 284 } 285 this.value = this.stageValue; 286 this.applyPeer(node, false, component); 287 return false; 288 } 289 applyPeer(node: KNode, reset: boolean): void { 290 if (reset) { 291 getUINativeModule().button.resetType(node); 292 } else { 293 getUINativeModule().button.setType(node, this.value); 294 } 295 } 296} 297class ButtonFontColorModifier extends ModifierWithKey<ResourceColor> { 298 constructor(value: ResourceColor) { 299 super(value); 300 } 301 static identity: Symbol = Symbol('buttonFontColor'); 302 applyPeer(node: KNode, reset: boolean): void { 303 if (reset) { 304 getUINativeModule().button.resetFontColor(node); 305 } else { 306 getUINativeModule().button.setFontColor(node, this.value); 307 } 308 } 309 310 checkObjectDiff(): boolean { 311 return !isBaseOrResourceEqual(this.stageValue, this.value); 312 } 313} 314class ButtonFontSizeModifier extends ModifierWithKey<Length> { 315 constructor(value: Length) { 316 super(value); 317 } 318 static identity: Symbol = Symbol('buttonFontSize'); 319 applyPeer(node: KNode, reset: boolean): void { 320 if (reset) { 321 getUINativeModule().button.resetFontSize(node); 322 } else { 323 getUINativeModule().button.setFontSize(node, this.value); 324 } 325 } 326 327 checkObjectDiff(): boolean { 328 return !isBaseOrResourceEqual(this.stageValue, this.value); 329 } 330} 331class ButtonFontWeightModifier extends ModifierWithKey<string | number | FontWeight> { 332 constructor(value: string | number | FontWeight) { 333 super(value); 334 } 335 static identity: Symbol = Symbol('buttonFontWeight'); 336 applyPeer(node: KNode, reset: boolean): void { 337 if (reset) { 338 getUINativeModule().button.resetFontWeight(node); 339 } else { 340 getUINativeModule().button.setFontWeight(node, this.value); 341 } 342 } 343} 344 345class ButtonBorderRadiusModifier extends ModifierWithKey<Length | BorderRadiuses> { 346 constructor(value: Length | BorderRadiuses) { 347 super(value); 348 } 349 static identity: Symbol = Symbol('buttonBorderRadius'); 350 applyPeer(node: KNode, reset: boolean): void { 351 if (reset) { 352 getUINativeModule().button.resetButtonBorderRadius(node); 353 } else { 354 if (isNumber(this.value) || isString(this.value) || isResource(this.value)) { 355 getUINativeModule().button.setButtonBorderRadius(node, this.value, this.value, this.value, this.value); 356 } else { 357 getUINativeModule().button.setButtonBorderRadius(node, 358 (this.value as BorderRadiuses).topLeft, 359 (this.value as BorderRadiuses).topRight, 360 (this.value as BorderRadiuses).bottomLeft, 361 (this.value as BorderRadiuses).bottomRight); 362 } 363 } 364 } 365 366 checkObjectDiff(): boolean { 367 if (isResource(this.stageValue) && isResource(this.value)) { 368 return !isResourceEqual(this.stageValue, this.value); 369 } else if (!isResource(this.stageValue) && !isResource(this.value)) { 370 return !((this.stageValue as BorderRadiuses).topLeft === (this.value as BorderRadiuses).topLeft && 371 (this.stageValue as BorderRadiuses).topRight === (this.value as BorderRadiuses).topRight && 372 (this.stageValue as BorderRadiuses).bottomLeft === (this.value as BorderRadiuses).bottomLeft && 373 (this.stageValue as BorderRadiuses).bottomRight === (this.value as BorderRadiuses).bottomRight); 374 } else { 375 return true; 376 } 377 } 378} 379 380class ButtonBorderModifier extends ModifierWithKey<BorderOptions> { 381 constructor(value: BorderOptions) { 382 super(value); 383 } 384 static identity: Symbol = Symbol('buttonBorder'); 385 applyPeer(node: KNode, reset: boolean): void { 386 if (reset) { 387 getUINativeModule().button.resetButtonBorder(node); 388 } else { 389 let widthLeft; 390 let widthRight; 391 let widthTop; 392 let widthBottom; 393 if (!isUndefined(this.value.width) && this.value.width !== null) { 394 if (isNumber(this.value.width) || isString(this.value.width) || isResource(this.value.width)) { 395 widthLeft = this.value.width; 396 widthRight = this.value.width; 397 widthTop = this.value.width; 398 widthBottom = this.value.width; 399 } else { 400 widthLeft = (this.value.width as EdgeWidths).left; 401 widthRight = (this.value.width as EdgeWidths).right; 402 widthTop = (this.value.width as EdgeWidths).top; 403 widthBottom = (this.value.width as EdgeWidths).bottom; 404 } 405 } 406 let leftColor; 407 let rightColor; 408 let topColor; 409 let bottomColor; 410 if (!isUndefined(this.value.color) && this.value.color !== null) { 411 if (isNumber(this.value.color) || isString(this.value.color) || isResource(this.value.color)) { 412 leftColor = this.value.color; 413 rightColor = this.value.color; 414 topColor = this.value.color; 415 bottomColor = this.value.color; 416 } else { 417 leftColor = (this.value.color as EdgeColors).left; 418 rightColor = (this.value.color as EdgeColors).right; 419 topColor = (this.value.color as EdgeColors).top; 420 bottomColor = (this.value.color as EdgeColors).bottom; 421 } 422 } 423 let topLeft; 424 let topRight; 425 let bottomLeft; 426 let bottomRight; 427 if (!isUndefined(this.value.radius) && this.value.radius !== null) { 428 if (isNumber(this.value.radius) || isString(this.value.radius) || isResource(this.value.radius)) { 429 topLeft = this.value.radius; 430 topRight = this.value.radius; 431 bottomLeft = this.value.radius; 432 bottomRight = this.value.radius; 433 } else { 434 topLeft = (this.value.radius as BorderRadiuses).topLeft; 435 topRight = (this.value.radius as BorderRadiuses).topRight; 436 bottomLeft = (this.value.radius as BorderRadiuses).bottomLeft; 437 bottomRight = (this.value.radius as BorderRadiuses).bottomRight; 438 } 439 } 440 let styleTop; 441 let styleRight; 442 let styleBottom; 443 let styleLeft; 444 if (!isUndefined(this.value.style) && this.value.style !== null) { 445 if (isNumber(this.value.style) || isString(this.value.style) || isResource(this.value.style)) { 446 styleTop = this.value.style; 447 styleRight = this.value.style; 448 styleBottom = this.value.style; 449 styleLeft = this.value.style; 450 } else { 451 styleTop = (this.value.style as EdgeStyles).top; 452 styleRight = (this.value.style as EdgeStyles).right; 453 styleBottom = (this.value.style as EdgeStyles).bottom; 454 styleLeft = (this.value.style as EdgeStyles).left; 455 } 456 } 457 getUINativeModule().button.setButtonBorder( 458 node, 459 widthLeft, 460 widthRight, 461 widthTop, 462 widthBottom, 463 leftColor, 464 rightColor, 465 topColor, 466 bottomColor, 467 topLeft, 468 topRight, 469 bottomLeft, 470 bottomRight, 471 styleTop, 472 styleRight, 473 styleBottom, 474 styleLeft 475 ); 476 } 477 } 478 479 checkObjectDiff(): boolean { 480 return !isBaseOrResourceEqual(this.stageValue.width, this.value.width) || 481 !isBaseOrResourceEqual(this.stageValue.color, this.value.color) || 482 !isBaseOrResourceEqual(this.stageValue.radius, this.value.radius) || 483 !isBaseOrResourceEqual(this.stageValue.style, this.value.style); 484 } 485} 486 487class ButtonSizeModifier extends ModifierWithKey<SizeOptions> { 488 constructor(value: SizeOptions) { 489 super(value); 490 } 491 static identity: Symbol = Symbol('buttonSize'); 492 applyPeer(node: KNode, reset: boolean): void { 493 if (reset) { 494 getUINativeModule().button.resetButtonSize(node); 495 } else { 496 getUINativeModule().button.setButtonSize(node, this.value.width, this.value.height); 497 } 498 } 499 500 checkObjectDiff(): boolean { 501 return ( 502 !isBaseOrResourceEqual(this.stageValue.width, this.value.width) || 503 !isBaseOrResourceEqual(this.stageValue.height, this.value.height) 504 ); 505 } 506} 507 508class ButtonRoleModifier extends ModifierWithKey<ButtonRole> { 509 constructor(value: ButtonRole) { 510 super(value); 511 } 512 static identity: Symbol = Symbol('buttonRole'); 513 applyPeer(node: KNode, reset: boolean): void { 514 if (reset) { 515 getUINativeModule().button.resetButtonRole(node); 516 } else { 517 getUINativeModule().button.setButtonRole(node, this.value); 518 } 519 } 520 521 checkObjectDiff(): boolean { 522 return ( 523 !isBaseOrResourceEqual(this.stageValue, this.value) 524 ); 525 } 526} 527 528class ButtonStyleModifier extends ModifierWithKey<ButtonStyleMode> { 529 constructor(value: ButtonStyleMode) { 530 super(value); 531 } 532 static identity: Symbol = Symbol('buttonStyle'); 533 applyPeer(node: KNode, reset: boolean): void { 534 if (reset) { 535 getUINativeModule().button.resetButtonStyle(node); 536 } else { 537 getUINativeModule().button.setButtonStyle(node, this.value); 538 } 539 } 540 541 checkObjectDiff(): boolean { 542 return ( 543 !isBaseOrResourceEqual(this.stageValue, this.value) 544 ); 545 } 546} 547 548class ButtonControlSizeModifier extends ModifierWithKey<ControlSize> { 549 constructor(value: ControlSize) { 550 super(value); 551 } 552 static identity: Symbol = Symbol('buttonControlSize'); 553 applyPeer(node: KNode, reset: boolean): void { 554 if (reset) { 555 getUINativeModule().button.resetControlSize(node); 556 } else { 557 getUINativeModule().button.setControlSize(node, this.value); 558 } 559 } 560 561 checkObjectDiff(): boolean { 562 return ( 563 !isBaseOrResourceEqual(this.stageValue, this.value) 564 ); 565 } 566} 567 568class ButtonLabelModifier extends ModifierWithKey<ResourceStr> { 569 constructor(value: ResourceStr) { 570 super(value); 571 } 572 static identity: Symbol = Symbol('buttonLabel'); 573 applyPeer(node: KNode, reset: boolean): void { 574 if (reset) { 575 getUINativeModule().button.resetLabel(node); 576 } else { 577 getUINativeModule().button.setLabel(node, this.value); 578 } 579 } 580 checkObjectDiff(): boolean { 581 return !isBaseOrResourceEqual(this.stageValue, this.value); 582 } 583} 584 585class ButtonOptionsModifier extends ModifierWithKey<ButtonOptions> { 586 static identity: Symbol = Symbol('buttonOptions'); 587 applyPeer(node: KNode, reset: boolean): void { 588 if (reset) { 589 getUINativeModule().button.resetOptions(node); 590 } else { 591 getUINativeModule().button.setOptions(node, this.value.type, this.value.stateEffect, 592 this.value.buttonStyle, this.value.controlSize, this.value.role); 593 } 594 } 595 checkObjectDiff(): boolean { 596 return ( 597 !isBaseOrResourceEqual(this.stageValue.type, this.value.type) || 598 !isBaseOrResourceEqual(this.stageValue.stateEffect, this.value.stateEffect) || 599 !isBaseOrResourceEqual(this.stageValue.buttonStyle, this.value.buttonStyle) || 600 !isBaseOrResourceEqual(this.stageValue.controlSize, this.value.controlSize) || 601 !isBaseOrResourceEqual(this.stageValue.role, this.value.role) 602 ); 603 } 604} 605 606class ButtonCreateTypeModifier extends ModifierWithKey<boolean> { 607 static identity: Symbol = Symbol('buttonCreateType'); 608 applyPeer(node: KNode, reset: boolean): void { 609 if (!reset) { 610 getUINativeModule().button.setCreateWithLabel(node, this.value); 611 } 612 } 613 checkObjectDiff(): boolean { 614 return !isBaseOrResourceEqual(this.stageValue, this.value); 615 } 616} 617class ButtonContentModifier extends ModifierWithKey<ContentModifier<ButtonConfiguration>> { 618 constructor(value: ContentModifier<ButtonConfiguration>) { 619 super(value); 620 } 621 static identity: Symbol = Symbol('buttonContentModifier'); 622 applyPeer(node: KNode, reset: boolean, component: ArkComponent) { 623 let buttonComponent = component as ArkButtonComponent; 624 buttonComponent.setContentModifier(this.value); 625 } 626} 627 628class ButtonMinFontScaleModifier extends ModifierWithKey<number | Resource> { 629 constructor(value: number | Resource) { 630 super(value); 631 } 632 static identity: Symbol = Symbol('buttonMinFontScale'); 633 applyPeer(node: KNode, reset: boolean): void { 634 if (reset) { 635 getUINativeModule().button.resetMinFontScale(node); 636 } else if (!isNumber(this.value) && !isResource(this.value)) { 637 getUINativeModule().button.resetMinFontScale(node); 638 } else { 639 getUINativeModule().button.setMinFontScale(node, this.value); 640 } 641 } 642 643 checkObjectDiff(): boolean { 644 return !isBaseOrResourceEqual(this.stageValue, this.value); 645 } 646} 647 648class ButtonMaxFontScaleModifier extends ModifierWithKey<number | Resource> { 649 constructor(value: number | Resource) { 650 super(value); 651 } 652 static identity: Symbol = Symbol('buttonMaxFontScale'); 653 applyPeer(node: KNode, reset: boolean): void { 654 if (reset) { 655 getUINativeModule().button.resetMaxFontScale(node); 656 } else if (!isNumber(this.value) && !isResource(this.value)) { 657 getUINativeModule().button.resetMaxFontScale(node); 658 } else { 659 getUINativeModule().button.setMaxFontScale(node, this.value); 660 } 661 } 662 663 checkObjectDiff(): boolean { 664 return !isBaseOrResourceEqual(this.stageValue, this.value); 665 } 666} 667 668// @ts-ignore 669globalThis.Button.attributeModifier = function (modifier: ArkComponent): void { 670 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 671 return new ArkButtonComponent(nativePtr); 672 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 673 return new modifierJS.ButtonModifier(nativePtr, classType); 674 }); 675}; 676 677// @ts-ignore 678globalThis.Button.contentModifier = function (modifier: ContentModifier<ButtonConfiguration>): void { 679 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 680 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 681 let component = this.createOrGetNode(elmtId, () => { 682 return new ArkButtonComponent(nativeNode); 683 }); 684 component.setContentModifier(modifier); 685}; 686