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 ArkSelectComponent extends ArkComponent implements SelectAttribute { 18 constructor(nativePtr: KNode) { 19 super(nativePtr); 20 } 21 onGestureJudgeBegin(callback: (gestureInfo: GestureInfo, event: BaseGestureEvent) => GestureJudgeResult): this { 22 throw new Error('Method not implemented.'); 23 } 24 optionWidth(value: Dimension | OptionWidthMode): this { 25 modifierWithKey( 26 this._modifiersWithKeys, SelectOptionWidthModifier.identity, SelectOptionWidthModifier, value); 27 return this; 28 } 29 optionHeight(value: Dimension): this { 30 modifierWithKey( 31 this._modifiersWithKeys, SelectOptionHeightModifier.identity, SelectOptionHeightModifier, value); 32 return this; 33 } 34 width(value: Length): this { 35 modifierWithKey( 36 this._modifiersWithKeys, SelectWidthModifier.identity, SelectWidthModifier, value); 37 return this; 38 } 39 height(value: Length): this { 40 modifierWithKey( 41 this._modifiersWithKeys, SelectHeightModifier.identity, SelectHeightModifier, value); 42 return this; 43 } 44 size(value: SizeOptions): this { 45 modifierWithKey( 46 this._modifiersWithKeys, SelectSizeModifier.identity, SelectSizeModifier, value); 47 return this; 48 } 49 selected(value: number | Resource): this { 50 modifierWithKey( 51 this._modifiersWithKeys, SelectedModifier.identity, SelectedModifier, value); 52 return this; 53 } 54 value(value: ResourceStr): this { 55 modifierWithKey( 56 this._modifiersWithKeys, ValueModifier.identity, ValueModifier, value); 57 return this; 58 } 59 font(value: Font): this { 60 modifierWithKey( 61 this._modifiersWithKeys, FontModifier.identity, FontModifier, value); 62 return this; 63 } 64 fontColor(value: ResourceColor): this { 65 modifierWithKey( 66 this._modifiersWithKeys, SelectFontColorModifier.identity, SelectFontColorModifier, value); 67 return this; 68 } 69 selectedOptionBgColor(value: ResourceColor): this { 70 modifierWithKey( 71 this._modifiersWithKeys, SelectedOptionBgColorModifier.identity, SelectedOptionBgColorModifier, value); 72 return this; 73 } 74 selectedOptionFont(value: Font): this { 75 modifierWithKey( 76 this._modifiersWithKeys, SelectedOptionFontModifier.identity, SelectedOptionFontModifier, value); 77 return this; 78 } 79 selectedOptionFontColor(value: ResourceColor): this { 80 modifierWithKey( 81 this._modifiersWithKeys, SelectedOptionFontColorModifier.identity, SelectedOptionFontColorModifier, value); 82 return this; 83 } 84 optionBgColor(value: ResourceColor): this { 85 modifierWithKey( 86 this._modifiersWithKeys, OptionBgColorModifier.identity, OptionBgColorModifier, value); 87 return this; 88 } 89 optionFont(value: Font): this { 90 modifierWithKey( 91 this._modifiersWithKeys, OptionFontModifier.identity, OptionFontModifier, value); 92 return this; 93 } 94 optionFontColor(value: ResourceColor): this { 95 modifierWithKey( 96 this._modifiersWithKeys, OptionFontColorModifier.identity, OptionFontColorModifier, value); 97 return this; 98 } 99 onSelect(callback: (index: number, value: string) => void): this { 100 throw new Error('Method not implemented.'); 101 } 102 space(value: Length): this { 103 modifierWithKey( 104 this._modifiersWithKeys, SpaceModifier.identity, SpaceModifier, value); 105 return this; 106 } 107 arrowPosition(value: ArrowPosition): this { 108 modifierWithKey( 109 this._modifiersWithKeys, ArrowPositionModifier.identity, ArrowPositionModifier, value); 110 return this; 111 } 112 menuAlign(alignType: MenuAlignType, offset?: Offset): this { 113 let menuAlign = new ArkMenuAlignType(alignType, offset); 114 modifierWithKey( 115 this._modifiersWithKeys, MenuAlignModifier.identity, MenuAlignModifier, menuAlign); 116 return this; 117 } 118} 119 120class FontModifier extends ModifierWithKey<Font> { 121 constructor(value: Font) { 122 super(value); 123 } 124 static identity: Symbol = Symbol('selectFont'); 125 applyPeer(node: KNode, reset: boolean): void { 126 if (reset) { 127 getUINativeModule().select.resetFont(node); 128 } else { 129 getUINativeModule().select.setFont(node, this.value.size, this.value.weight, this.value.family, this.value.style); 130 } 131 } 132 133 checkObjectDiff(): boolean { 134 let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size); 135 let weightEQ = this.stageValue.weight === this.value.weight; 136 let familyEQ = isBaseOrResourceEqual(this.stageValue.family, this.value.family); 137 let styleEQ = this.stageValue.style === this.value.style; 138 return !sizeEQ || !weightEQ || !familyEQ || !styleEQ; 139 } 140} 141 142class OptionFontModifier extends ModifierWithKey<Font> { 143 constructor(value: Font) { 144 super(value); 145 } 146 static identity: Symbol = Symbol('selectOptionFont'); 147 applyPeer(node: KNode, reset: boolean): void { 148 if (reset) { 149 getUINativeModule().select.resetOptionFont(node); 150 } else { 151 getUINativeModule().select.setOptionFont(node, this.value.size, this.value.weight, this.value.family, this.value.style); 152 } 153 } 154 155 checkObjectDiff(): boolean { 156 let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size); 157 let weightEQ = this.stageValue.weight === this.value.weight; 158 let familyEQ = isBaseOrResourceEqual(this.stageValue.family, this.value.family); 159 let styleEQ = this.stageValue.style === this.value.style; 160 return !sizeEQ || !weightEQ || !familyEQ || !styleEQ; 161 } 162} 163 164class SelectedOptionFontModifier extends ModifierWithKey<Font> { 165 constructor(value: Font) { 166 super(value); 167 } 168 static identity: Symbol = Symbol('selectSelectedOptionFont'); 169 applyPeer(node: KNode, reset: boolean): void { 170 if (reset) { 171 getUINativeModule().select.resetSelectedOptionFont(node); 172 } else { 173 getUINativeModule().select.setSelectedOptionFont(node, this.value.size, this.value.weight, this.value.family, this.value.style); 174 } 175 } 176 177 checkObjectDiff(): boolean { 178 let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size); 179 let weightEQ = this.stageValue.weight === this.value.weight; 180 let familyEQ = isBaseOrResourceEqual(this.stageValue.family, this.value.family); 181 let styleEQ = this.stageValue.style === this.value.style; 182 return !sizeEQ || !weightEQ || !familyEQ || !styleEQ; 183 } 184} 185 186class MenuAlignModifier extends ModifierWithKey<ArkMenuAlignType> { 187 constructor(value: ArkMenuAlignType) { 188 super(value); 189 } 190 static identity: Symbol = Symbol('selectMenuAlign'); 191 applyPeer(node: KNode, reset: boolean): void { 192 if (reset) { 193 getUINativeModule().select.resetMenuAlign(node); 194 } else { 195 getUINativeModule().select.setMenuAlign(node, this.value.alignType, this.value.dx, this.value.dy); 196 } 197 } 198 199 checkObjectDiff(): boolean { 200 let alignTypeEQ = this.stageValue.alignType === this.value.alignType; 201 let dxEQ = isBaseOrResourceEqual(this.stageValue, this.value); 202 let dyEQ = isBaseOrResourceEqual(this.stageValue, this.value); 203 204 return !alignTypeEQ || !dxEQ || !dyEQ; 205 } 206 207 private isEqual(stageValue: Length, value: Length) { 208 if ((!isUndefined(stageValue) && isResource(stageValue)) && 209 (!isUndefined(value) && isResource(value))) { 210 return !isResourceEqual(stageValue, value); 211 } else { 212 return stageValue !== value; 213 } 214 } 215} 216 217class ArrowPositionModifier extends ModifierWithKey<ArrowPosition> { 218 constructor(value: ArrowPosition) { 219 super(value); 220 } 221 static identity: Symbol = Symbol('selectArrowPosition'); 222 applyPeer(node: KNode, reset: boolean): void { 223 if (reset) { 224 getUINativeModule().select.resetArrowPosition(node); 225 } else { 226 getUINativeModule().select.setArrowPosition(node, this.value); 227 } 228 } 229 230 checkObjectDiff(): boolean { 231 return this.stageValue !== this.value; 232 } 233} 234class SpaceModifier extends ModifierWithKey<Length> { 235 constructor(value: Length) { 236 super(value); 237 } 238 static identity: Symbol = Symbol('selectSpace'); 239 240 applyPeer(node: KNode, reset: boolean): void { 241 if (reset) { 242 getUINativeModule().select.resetSpace(node); 243 } else { 244 getUINativeModule().select.setSpace(node, this.value); 245 } 246 } 247 248 checkObjectDiff(): boolean { 249 return !isBaseOrResourceEqual(this.stageValue, this.value); 250 } 251} 252class ValueModifier extends ModifierWithKey<ResourceStr> { 253 constructor(value: ResourceStr) { 254 super(value); 255 } 256 static identity: Symbol = Symbol('selectValue'); 257 258 applyPeer(node: KNode, reset: boolean): void { 259 if (reset) { 260 getUINativeModule().select.resetValue(node); 261 } else { 262 getUINativeModule().select.setValue(node, this.value); 263 } 264 } 265 266 checkObjectDiff(): boolean { 267 return !isBaseOrResourceEqual(this.stageValue, this.value); 268 } 269} 270class SelectedModifier extends ModifierWithKey<number | Resource> { 271 constructor(value: number | Resource) { 272 super(value); 273 } 274 static identity: Symbol = Symbol('selectSelected'); 275 276 applyPeer(node: KNode, reset: boolean): void { 277 if (reset) { 278 getUINativeModule().select.resetSelected(node); 279 } else { 280 getUINativeModule().select.setSelected(node, this.value); 281 } 282 } 283 284 checkObjectDiff(): boolean { 285 return !isBaseOrResourceEqual(this.stageValue, this.value); 286 } 287} 288class SelectFontColorModifier extends ModifierWithKey<ResourceColor> { 289 constructor(value: ResourceColor) { 290 super(value); 291 } 292 static identity: Symbol = Symbol('selectFontColor'); 293 294 applyPeer(node: KNode, reset: boolean): void { 295 if (reset) { 296 getUINativeModule().select.resetFontColor(node); 297 } else { 298 getUINativeModule().select.setFontColor(node, this.value); 299 } 300 } 301 checkObjectDiff(): boolean { 302 return !isBaseOrResourceEqual(this.stageValue, this.value); 303 } 304} 305class SelectedOptionBgColorModifier extends ModifierWithKey<ResourceColor> { 306 constructor(value: ResourceColor) { 307 super(value); 308 } 309 static identity: Symbol = Symbol('selectSelectedOptionBgColor'); 310 311 applyPeer(node: KNode, reset: boolean): void { 312 if (reset) { 313 getUINativeModule().select.resetSelectedOptionBgColor(node); 314 } else { 315 getUINativeModule().select.setSelectedOptionBgColor(node, this.value); 316 } 317 } 318 checkObjectDiff(): boolean { 319 return !isBaseOrResourceEqual(this.stageValue, this.value); 320 } 321} 322class OptionBgColorModifier extends ModifierWithKey<ResourceColor> { 323 constructor(value: ResourceColor) { 324 super(value); 325 } 326 static identity: Symbol = Symbol('selectOptionBgColor'); 327 328 applyPeer(node: KNode, reset: boolean): void { 329 if (reset) { 330 getUINativeModule().select.resetOptionBgColor(node); 331 } else { 332 getUINativeModule().select.setOptionBgColor(node, this.value); 333 } 334 } 335 checkObjectDiff(): boolean { 336 return !isBaseOrResourceEqual(this.stageValue, this.value); 337 } 338} 339class OptionFontColorModifier extends ModifierWithKey<ResourceColor> { 340 constructor(value: ResourceColor) { 341 super(value); 342 } 343 static identity: Symbol = Symbol('selectOptionFontColor'); 344 345 applyPeer(node: KNode, reset: boolean): void { 346 if (reset) { 347 getUINativeModule().select.resetOptionFontColor(node); 348 } else { 349 getUINativeModule().select.setOptionFontColor(node, this.value); 350 } 351 } 352 checkObjectDiff(): boolean { 353 return !isBaseOrResourceEqual(this.stageValue, this.value); 354 } 355} 356class SelectedOptionFontColorModifier extends ModifierWithKey<ResourceColor> { 357 constructor(value: ResourceColor) { 358 super(value); 359 } 360 static identity: Symbol = Symbol('selectSelectedOptionFontColor'); 361 362 applyPeer(node: KNode, reset: boolean): void { 363 if (reset) { 364 getUINativeModule().select.resetSelectedOptionFontColor(node); 365 } else { 366 getUINativeModule().select.setSelectedOptionFontColor(node, this.value); 367 } 368 } 369 checkObjectDiff(): boolean { 370 return !isBaseOrResourceEqual(this.stageValue, this.value); 371 } 372} 373 374class SelectOptionWidthModifier extends ModifierWithKey<Dimension | OptionWidthMode> { 375 constructor(value: Dimension | OptionWidthMode) { 376 super(value); 377 } 378 static identity: Symbol = Symbol('selectOptionWidth'); 379 380 applyPeer(node: KNode, reset: boolean): void { 381 if (reset) { 382 getUINativeModule().select.resetOptionWidth(node); 383 } else { 384 getUINativeModule().select.setOptionWidth(node, this.value); 385 } 386 } 387 388 checkObjectDiff(): boolean { 389 return !isBaseOrResourceEqual(this.stageValue, this.value); 390 } 391} 392 393class SelectOptionHeightModifier extends ModifierWithKey<Dimension> { 394 constructor(value: Dimension) { 395 super(value); 396 } 397 static identity: Symbol = Symbol('selectOptionHeight'); 398 399 applyPeer(node: KNode, reset: boolean): void { 400 if (reset) { 401 getUINativeModule().select.resetOptionHeight(node); 402 } else { 403 getUINativeModule().select.setOptionHeight(node, this.value); 404 } 405 } 406 407 checkObjectDiff(): boolean { 408 return !isBaseOrResourceEqual(this.stageValue, this.value); 409 } 410} 411 412class SelectWidthModifier extends ModifierWithKey<Length> { 413 constructor(value: Length) { 414 super(value); 415 } 416 static identity: Symbol = Symbol('selectWidth'); 417 418 applyPeer(node: KNode, reset: boolean): void { 419 if (reset) { 420 getUINativeModule().select.resetWidth(node); 421 } else { 422 getUINativeModule().select.setWidth(node, this.value); 423 } 424 } 425 426 checkObjectDiff(): boolean { 427 return !isBaseOrResourceEqual(this.stageValue, this.value); 428 } 429} 430 431class SelectHeightModifier extends ModifierWithKey<Length> { 432 constructor(value: Length) { 433 super(value); 434 } 435 static identity: Symbol = Symbol('selectHeight'); 436 437 applyPeer(node: KNode, reset: boolean): void { 438 if (reset) { 439 getUINativeModule().select.resetHeight(node); 440 } else { 441 getUINativeModule().select.setHeight(node, this.value); 442 } 443 } 444 445 checkObjectDiff(): boolean { 446 return !isBaseOrResourceEqual(this.stageValue, this.value); 447 } 448} 449 450class SelectSizeModifier extends ModifierWithKey<SizeOptions> { 451 constructor(value: SizeOptions) { 452 super(value); 453 } 454 static identity: Symbol = Symbol('selectSize'); 455 456 applyPeer(node: KNode, reset: boolean): void { 457 if (reset) { 458 getUINativeModule().select.resetSize(node); 459 } else { 460 getUINativeModule().select.setSize(node, this.value.width, this.value.height); 461 } 462 } 463 464 checkObjectDiff(): boolean { 465 return !isBaseOrResourceEqual(this.stageValue.width, this.value.width) || 466 !isBaseOrResourceEqual(this.stageValue.height, this.value.height); 467 } 468} 469 470// @ts-ignore 471globalThis.Select.attributeModifier = function (modifier) { 472 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 473 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 474 let component = this.createOrGetNode(elmtId, () => { 475 return new ArkSelectComponent(nativeNode); 476 }); 477 applyUIAttributes(modifier, nativeNode, component); 478 component.applyModifierPatch(); 479}; 480