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 constructor(nativePtr: KNode) { 38 super(nativePtr); 39 } 40 onGestureJudgeBegin(callback: (gestureInfo: GestureInfo, event: BaseGestureEvent) => GestureJudgeResult): this { 41 throw new Error('Method not implemented.'); 42 } 43 backgroundColor(value: ResourceColor): this { 44 modifierWithKey(this._modifiersWithKeys, ButtonBackgroundColorModifier.identity, ButtonBackgroundColorModifier, value); 45 return this; 46 } 47 type(value: ButtonType): this { 48 modifierWithKey(this._modifiersWithKeys, ButtonTypeModifier.identity, ButtonTypeModifier, value); 49 return this; 50 } 51 stateEffect(value: boolean): this { 52 modifierWithKey(this._modifiersWithKeys, ButtonStateEffectModifier.identity, ButtonStateEffectModifier, value); 53 return this; 54 } 55 fontColor(value: ResourceColor): this { 56 modifierWithKey(this._modifiersWithKeys, ButtonFontColorModifier.identity, ButtonFontColorModifier, value); 57 return this; 58 } 59 fontSize(value: Length): this { 60 modifierWithKey(this._modifiersWithKeys, ButtonFontSizeModifier.identity, ButtonFontSizeModifier, value); 61 return this; 62 } 63 fontWeight(value: string | number | FontWeight): this { 64 modifierWithKey(this._modifiersWithKeys, ButtonFontWeightModifier.identity, ButtonFontWeightModifier, value); 65 return this; 66 } 67 fontStyle(value: FontStyle): this { 68 modifierWithKey(this._modifiersWithKeys, ButtonFontStyleModifier.identity, ButtonFontStyleModifier, value); 69 return this; 70 } 71 fontFamily(value: string | Resource): this { 72 modifierWithKey(this._modifiersWithKeys, ButtonFontFamilyModifier.identity, ButtonFontFamilyModifier, value); 73 return this; 74 } 75 labelStyle(value: LabelStyle): this { 76 modifierWithKey(this._modifiersWithKeys, ButtonLabelStyleModifier.identity, ButtonLabelStyleModifier, value); 77 return this; 78 } 79 borderRadius(value: Length | BorderRadiuses): this { 80 modifierWithKey(this._modifiersWithKeys, ButtonBorderRadiusModifier.identity, ButtonBorderRadiusModifier, value); 81 return this; 82 } 83 border(value: BorderOptions): this { 84 modifierWithKey(this._modifiersWithKeys, ButtonBorderModifier.identity, ButtonBorderModifier, value); 85 return this; 86 } 87 size(value: SizeOptions): this { 88 modifierWithKey(this._modifiersWithKeys, ButtonSizeModifier.identity, ButtonSizeModifier, value); 89 return this; 90 } 91} 92class ButtonBackgroundColorModifier extends ModifierWithKey<ResourceColor> { 93 constructor(value: ResourceColor) { 94 super(value); 95 } 96 static identity: Symbol = Symbol('buttonBackgroundColor'); 97 applyPeer(node: KNode, reset: boolean): void { 98 if (reset) { 99 getUINativeModule().button.resetBackgroundColor(node); 100 } else { 101 getUINativeModule().button.setBackgroundColor(node, this.value); 102 } 103 } 104 105 checkObjectDiff(): boolean { 106 return !isBaseOrResourceEqual(this.stageValue, this.value); 107 } 108} 109class ButtonStateEffectModifier extends ModifierWithKey<boolean> { 110 constructor(value: boolean) { 111 super(value); 112 } 113 static identity: Symbol = Symbol('buttonStateEffect'); 114 applyPeer(node: KNode, reset: boolean): void { 115 if (reset) { 116 getUINativeModule().button.resetStateEffect(node); 117 } else { 118 getUINativeModule().button.setStateEffect(node, this.value); 119 } 120 } 121} 122class ButtonFontStyleModifier extends ModifierWithKey<number> { 123 constructor(value: number) { 124 super(value); 125 } 126 static identity: Symbol = Symbol('buttonFontStyle'); 127 applyPeer(node: KNode, reset: boolean): void { 128 if (reset) { 129 getUINativeModule().button.resetFontStyle(node); 130 } else { 131 getUINativeModule().button.setFontStyle(node, this.value); 132 } 133 } 134} 135class ButtonFontFamilyModifier extends ModifierWithKey<string | Resource> { 136 constructor(value: string | Resource) { 137 super(value); 138 } 139 static identity: Symbol = Symbol('buttonFontFamily'); 140 applyPeer(node: KNode, reset: boolean): void { 141 if (reset) { 142 getUINativeModule().button.resetFontFamily(node); 143 } else { 144 getUINativeModule().button.setFontFamily(node, this.value); 145 } 146 } 147 checkObjectDiff(): boolean { 148 return !isBaseOrResourceEqual(this.stageValue, this.value); 149 } 150} 151class ButtonLabelStyleModifier extends ModifierWithKey<LabelStyle> { 152 constructor(value: LabelStyle) { 153 super(value); 154 } 155 static identity: Symbol = Symbol('buttonLabelStyle'); 156 applyPeer(node: KNode, reset: boolean): void { 157 if (reset) { 158 getUINativeModule().button.resetLabelStyle(node); 159 } else { 160 let textOverflow = this.value.overflow; // number(enum) -> Ace::TextOverflow 161 let maxLines = this.value.maxLines; // number -> uint32_t 162 let minFontSize = this.value.minFontSize; // number | string | Resource -> Dimension 163 let maxFontSize = this.value.maxFontSize; // number | string | Resource -> Dimension 164 let heightAdaptivePolicy = this.value.heightAdaptivePolicy; // number(enum) -> Ace::TextHeightAdaptivePolicy 165 let fontSize; // number | string | Resource -> Dimension 166 let fontWeight; // number | string | Ace::FontWeight -> string -> Ace::FontWeight 167 let fontStyle; // number(enum) -> Ace::FontStyle 168 let fontFamily; // string -> std::vector<std::string> 169 if (isObject(this.value.font)) { 170 fontSize = this.value.font.size; 171 fontStyle = this.value.font.style; 172 fontFamily = this.value.font.family; 173 fontWeight = this.value.font.weight; 174 } 175 getUINativeModule().button.setLabelStyle(node, textOverflow, maxLines, minFontSize, maxFontSize, 176 heightAdaptivePolicy, fontSize, fontWeight, fontStyle, fontFamily); 177 } 178 } 179 checkObjectDiff(): boolean { 180 if (isResource(this.stageValue) && isResource(this.value)) { 181 return !isResourceEqual(this.stageValue, this.value); 182 } else if (!isResource(this.stageValue) && !isResource(this.value)) { 183 return !(this.value.overflow === this.stageValue.overflow && 184 this.value.maxLines === this.stageValue.maxLines && 185 this.value.minFontSize === this.stageValue.minFontSize && 186 this.value.maxFontSize === this.stageValue.maxFontSize && 187 this.value.heightAdaptivePolicy === this.stageValue.heightAdaptivePolicy && 188 this.value.font === this.stageValue.font); 189 } else { 190 return true; 191 } 192 } 193} 194class ButtonTypeModifier extends ModifierWithKey<number> { 195 constructor(value: number) { 196 super(value); 197 } 198 static identity: Symbol = Symbol('buttonType'); 199 applyPeer(node: KNode, reset: boolean): void { 200 if (reset) { 201 getUINativeModule().button.resetType(node); 202 } else { 203 getUINativeModule().button.setType(node, this.value); 204 } 205 } 206} 207class ButtonFontColorModifier extends ModifierWithKey<ResourceColor> { 208 constructor(value: ResourceColor) { 209 super(value); 210 } 211 static identity: Symbol = Symbol('buttonFontColor'); 212 applyPeer(node: KNode, reset: boolean): void { 213 if (reset) { 214 getUINativeModule().button.resetFontColor(node); 215 } else { 216 getUINativeModule().button.setFontColor(node, this.value); 217 } 218 } 219 220 checkObjectDiff(): boolean { 221 return !isBaseOrResourceEqual(this.stageValue, this.value); 222 } 223} 224class ButtonFontSizeModifier extends ModifierWithKey<Length> { 225 constructor(value: Length) { 226 super(value); 227 } 228 static identity: Symbol = Symbol('buttonFontSize'); 229 applyPeer(node: KNode, reset: boolean): void { 230 if (reset) { 231 getUINativeModule().button.resetFontSize(node); 232 } else { 233 getUINativeModule().button.setFontSize(node, this.value); 234 } 235 } 236 237 checkObjectDiff(): boolean { 238 return !isBaseOrResourceEqual(this.stageValue, this.value); 239 } 240} 241class ButtonFontWeightModifier extends ModifierWithKey<string | number | FontWeight> { 242 constructor(value: string | number | FontWeight) { 243 super(value); 244 } 245 static identity: Symbol = Symbol('buttonFontWeight'); 246 applyPeer(node: KNode, reset: boolean): void { 247 if (reset) { 248 getUINativeModule().button.resetFontWeight(node); 249 } else { 250 getUINativeModule().button.setFontWeight(node, this.value); 251 } 252 } 253} 254 255class ButtonBorderRadiusModifier extends ModifierWithKey<Length | BorderRadiuses> { 256 constructor(value: Length | BorderRadiuses) { 257 super(value); 258 } 259 static identity: Symbol = Symbol('buttonBorderRadius'); 260 applyPeer(node: KNode, reset: boolean): void { 261 if (reset) { 262 getUINativeModule().button.resetButtonBorderRadius(node); 263 } else { 264 if (isNumber(this.value) || isString(this.value) || isResource(this.value)) { 265 getUINativeModule().button.setButtonBorderRadius(node, this.value, this.value, this.value, this.value); 266 } else { 267 getUINativeModule().button.setButtonBorderRadius(node, 268 (this.value as BorderRadiuses).topLeft, 269 (this.value as BorderRadiuses).topRight, 270 (this.value as BorderRadiuses).bottomLeft, 271 (this.value as BorderRadiuses).bottomRight); 272 } 273 } 274 } 275 276 checkObjectDiff(): boolean { 277 if (isResource(this.stageValue) && isResource(this.value)) { 278 return !isResourceEqual(this.stageValue, this.value); 279 } else if (!isResource(this.stageValue) && !isResource(this.value)) { 280 return !((this.stageValue as BorderRadiuses).topLeft === (this.value as BorderRadiuses).topLeft && 281 (this.stageValue as BorderRadiuses).topRight === (this.value as BorderRadiuses).topRight && 282 (this.stageValue as BorderRadiuses).bottomLeft === (this.value as BorderRadiuses).bottomLeft && 283 (this.stageValue as BorderRadiuses).bottomRight === (this.value as BorderRadiuses).bottomRight); 284 } else { 285 return true; 286 } 287 } 288} 289 290class ButtonBorderModifier extends ModifierWithKey<BorderOptions> { 291 constructor(value: BorderOptions) { 292 super(value); 293 } 294 static identity: Symbol = Symbol('buttonBorder'); 295 applyPeer(node: KNode, reset: boolean): void { 296 if (reset) { 297 getUINativeModule().button.resetButtonBorder(node); 298 } else { 299 let widthLeft; 300 let widthRight; 301 let widthTop; 302 let widthBottom; 303 if (!isUndefined(this.value.width) && this.value.width != null) { 304 if (isNumber(this.value.width) || isString(this.value.width) || isResource(this.value.width)) { 305 widthLeft = this.value.width; 306 widthRight = this.value.width; 307 widthTop = this.value.width; 308 widthBottom = this.value.width; 309 } else { 310 widthLeft = (this.value.width as EdgeWidths).left; 311 widthRight = (this.value.width as EdgeWidths).right; 312 widthTop = (this.value.width as EdgeWidths).top; 313 widthBottom = (this.value.width as EdgeWidths).bottom; 314 } 315 } 316 let leftColor; 317 let rightColor; 318 let topColor; 319 let bottomColor; 320 if (!isUndefined(this.value.color) && this.value.color != null) { 321 if (isNumber(this.value.color) || isString(this.value.color) || isResource(this.value.color)) { 322 leftColor = this.value.color; 323 rightColor = this.value.color; 324 topColor = this.value.color; 325 bottomColor = this.value.color; 326 } else { 327 leftColor = (this.value.color as EdgeColors).left; 328 rightColor = (this.value.color as EdgeColors).right; 329 topColor = (this.value.color as EdgeColors).top; 330 bottomColor = (this.value.color as EdgeColors).bottom; 331 } 332 } 333 let topLeft; 334 let topRight; 335 let bottomLeft; 336 let bottomRight; 337 if (!isUndefined(this.value.radius) && this.value.radius != null) { 338 if (isNumber(this.value.radius) || isString(this.value.radius) || isResource(this.value.radius)) { 339 topLeft = this.value.radius; 340 topRight = this.value.radius; 341 bottomLeft = this.value.radius; 342 bottomRight = this.value.radius; 343 } else { 344 topLeft = (this.value.radius as BorderRadiuses).topLeft; 345 topRight = (this.value.radius as BorderRadiuses).topRight; 346 bottomLeft = (this.value.radius as BorderRadiuses).bottomLeft; 347 bottomRight = (this.value.radius as BorderRadiuses).bottomRight; 348 } 349 } 350 let styleTop; 351 let styleRight; 352 let styleBottom; 353 let styleLeft; 354 if (!isUndefined(this.value.style) && this.value.style != null) { 355 if (isNumber(this.value.style) || isString(this.value.style) || isResource(this.value.style)) { 356 styleTop = this.value.style; 357 styleRight = this.value.style; 358 styleBottom = this.value.style; 359 styleLeft = this.value.style; 360 } else { 361 styleTop = (this.value.style as EdgeStyles).top; 362 styleRight = (this.value.style as EdgeStyles).right; 363 styleBottom = (this.value.style as EdgeStyles).bottom; 364 styleLeft = (this.value.style as EdgeStyles).left; 365 } 366 } 367 getUINativeModule().button.setButtonBorder( 368 node, 369 widthLeft, 370 widthRight, 371 widthTop, 372 widthBottom, 373 leftColor, 374 rightColor, 375 topColor, 376 bottomColor, 377 topLeft, 378 topRight, 379 bottomLeft, 380 bottomRight, 381 styleTop, 382 styleRight, 383 styleBottom, 384 styleLeft 385 ); 386 } 387 } 388 389 checkObjectDiff(): boolean { 390 return ( 391 !isBaseOrResourceEqual(this.stageValue.width, this.value.width) || 392 !isBaseOrResourceEqual(this.stageValue.color, this.value.color) || 393 !isBaseOrResourceEqual(this.stageValue.radius, this.value.radius) || 394 !isBaseOrResourceEqual(this.stageValue.style, this.value.style) 395 ); 396 } 397} 398 399class ButtonSizeModifier extends ModifierWithKey<SizeOptions> { 400 constructor(value: SizeOptions) { 401 super(value); 402 } 403 static identity: Symbol = Symbol('buttonSize'); 404 applyPeer(node: KNode, reset: boolean): void { 405 if (reset) { 406 getUINativeModule().button.resetButtonSize(node); 407 } else { 408 getUINativeModule().button.setButtonSize(node, this.value.width, this.value.height); 409 } 410 } 411 412 checkObjectDiff(): boolean { 413 return ( 414 !isBaseOrResourceEqual(this.stageValue.width, this.value.width) || 415 !isBaseOrResourceEqual(this.stageValue.height, this.value.height) 416 ); 417 } 418} 419 420// @ts-ignore 421globalThis.Button.attributeModifier = function (modifier) { 422 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 423 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 424 let component = this.createOrGetNode(elmtId, () => { 425 return new ArkButtonComponent(nativeNode); 426 }); 427 applyUIAttributes(modifier, nativeNode, component); 428 component.applyModifierPatch(); 429}; 430