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 ArkTextPickerComponent extends ArkComponent implements TextPickerAttribute { 18 constructor(nativePtr: KNode, classType?: ModifierType) { 19 super(nativePtr, classType); 20 } 21 defaultPickerItemHeight(value: string | number): this { 22 modifierWithKey( 23 this._modifiersWithKeys, TextpickerDefaultPickerItemHeightModifier.identity, TextpickerDefaultPickerItemHeightModifier, value); 24 return this; 25 } 26 canLoop(value: boolean): this { 27 modifierWithKey( 28 this._modifiersWithKeys, TextpickerCanLoopModifier.identity, TextpickerCanLoopModifier, value); 29 return this; 30 } 31 disappearTextStyle(value: PickerTextStyle): this { 32 modifierWithKey( 33 this._modifiersWithKeys, TextpickerDisappearTextStyleModifier.identity, TextpickerDisappearTextStyleModifier, value); 34 return this; 35 } 36 textStyle(value: PickerTextStyle): this { 37 modifierWithKey( 38 this._modifiersWithKeys, TextpickerTextStyleModifier.identity, TextpickerTextStyleModifier, value); 39 return this; 40 } 41 selectedTextStyle(value: PickerTextStyle): this { 42 modifierWithKey( 43 this._modifiersWithKeys, TextpickerSelectedTextStyleModifier.identity, TextpickerSelectedTextStyleModifier, value); 44 return this; 45 } 46 onAccept(callback: (value: string, index: number) => void): this { 47 throw new Error('Method not implemented.'); 48 } 49 onCancel(callback: () => void): this { 50 throw new Error('Method not implemented.'); 51 } 52 onChange(callback: (value: string | string[], index: number | number[]) => void): this { 53 throw new Error('Method not implemented.'); 54 } 55 selectedIndex(value: number | number[]): this { 56 modifierWithKey( 57 this._modifiersWithKeys, TextpickerSelectedIndexModifier.identity, TextpickerSelectedIndexModifier, value); 58 return this; 59 } 60 divider(value: DividerOptions | null): this { 61 modifierWithKey( 62 this._modifiersWithKeys, TextpickerDividerModifier.identity, TextpickerDividerModifier, value); 63 return this; 64 } 65 gradientHeight(value: Dimension): this { 66 modifierWithKey( 67 this._modifiersWithKeys, TextpickerGradientHeightModifier.identity, TextpickerGradientHeightModifier, value); 68 return this; 69 } 70 disableTextStyleAnimation(value: boolean): this { 71 modifierWithKey( 72 this._modifiersWithKeys, TextpickerDisableTextStyleAnimationModifier.identity, TextpickerDisableTextStyleAnimationModifier, value); 73 return this; 74 } 75 defaultTextStyle(value: PickerTextStyle): this { 76 modifierWithKey( 77 this._modifiersWithKeys, TextpickerDefaultTextStyleModifier.identity, TextpickerDefaultTextStyleModifier, value); 78 return this; 79 } 80} 81 82class TextpickerCanLoopModifier extends ModifierWithKey<boolean> { 83 constructor(value: boolean) { 84 super(value); 85 } 86 static identity: Symbol = Symbol('textpickerCanLoop'); 87 applyPeer(node: KNode, reset: boolean): void { 88 if (reset) { 89 getUINativeModule().textpicker.resetCanLoop(node); 90 } else { 91 getUINativeModule().textpicker.setCanLoop(node, this.value); 92 } 93 } 94} 95 96class TextpickerSelectedIndexModifier extends ModifierWithKey<number | number[]> { 97 constructor(value: number | number[]) { 98 super(value); 99 } 100 static identity: Symbol = Symbol('textpickerSelectedIndex'); 101 applyPeer(node: KNode, reset: boolean): void { 102 if (reset) { 103 getUINativeModule().textpicker.resetSelectedIndex(node); 104 } else { 105 getUINativeModule().textpicker.setSelectedIndex(node, this.value); 106 } 107 } 108 109 checkObjectDiff(): boolean { 110 if (Array.isArray(this.stageValue) && Array.isArray(this.value)) { 111 return !deepCompareArrays(this.stageValue, this.value); 112 } else if (Array.isArray(this.stageValue) || Array.isArray(this.value)) { 113 return true; 114 } else { 115 return this.stageValue !== this.value; 116 } 117 } 118 119} 120 121class TextpickerDividerModifier extends ModifierWithKey<DividerOptions | null> { 122 constructor(value: DividerOptions | null) { 123 super(value); 124 } 125 static identity: Symbol = Symbol('textpickerDivider'); 126 applyPeer(node: KNode, reset: boolean): void { 127 if (reset) { 128 getUINativeModule().textpicker.resetDivider(node, this.value); 129 } else { 130 getUINativeModule().textpicker.setDivider(node, this.value?.strokeWidth, this.value?.color, this.value?.startMargin, this.value?.endMargin); 131 } 132 } 133 134 checkObjectDiff(): boolean { 135 return !(this.stageValue?.strokeWidth === this.value?.strokeWidth && 136 this.stageValue?.color === this.value?.color && 137 this.stageValue?.startMargin === this.value?.startMargin && 138 this.stageValue?.endMargin === this.value?.endMargin); 139 } 140} 141 142class TextpickerGradientHeightModifier extends ModifierWithKey<Dimension> { 143 constructor(value: Dimension) { 144 super(value); 145 } 146 static identity: Symbol = Symbol('textpickerGradientHeight'); 147 applyPeer(node: KNode, reset: boolean): void { 148 if (reset) { 149 getUINativeModule().textpicker.resetGradientHeight(node); 150 } else { 151 getUINativeModule().textpicker.setGradientHeight(node, this.value); 152 } 153 } 154 checkObjectDiff(): boolean { 155 return !isBaseOrResourceEqual(this.stageValue, this.value); 156 } 157} 158 159class TextpickerDisableTextStyleAnimationModifier extends ModifierWithKey<boolean> { 160 constructor(value: boolean) { 161 super(value); 162 } 163 static identity: Symbol = Symbol('textpickerDisableTextStyleAnimation'); 164 applyPeer(node: KNode, reset: boolean): void { 165 if (reset) { 166 getUINativeModule().textpicker.resetDisableTextStyleAnimation(node); 167 } else { 168 getUINativeModule().textpicker.setDisableTextStyleAnimation(node, this.value); 169 } 170 } 171} 172 173class TextpickerDefaultTextStyleModifier extends ModifierWithKey<PickerTextStyle> { 174 constructor(value: PickerTextStyle) { 175 super(value); 176 } 177 static identity: Symbol = Symbol('textpickerDefaultTextStyle'); 178 applyPeer(node: KNode, reset: boolean): void { 179 if (reset) { 180 getUINativeModule().textpicker.resetDefaultTextStyle(node); 181 } else { 182 getUINativeModule().textpicker.setDefaultTextStyle(node, this.value?.color ?? undefined, 183 this.value?.font?.size ?? undefined, 184 this.value?.font?.weight ?? undefined, 185 this.value?.font?.family ?? undefined, 186 this.value?.font?.style ?? undefined, 187 this.value?.minFontSize ?? undefined, 188 this.value?.maxFontSize ?? undefined, 189 this.value?.overflow ?? undefined); 190 } 191 } 192 193 checkObjectDiff(): boolean { 194 if (!(this.stageValue?.font?.weight === this.value?.font?.weight && 195 this.stageValue?.font?.style === this.value?.font?.style && 196 this.stageValue?.overflow === this.value?.overflow)) { 197 return true; 198 } else { 199 return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) || 200 !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) || 201 !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family) || 202 !isBaseOrResourceEqual(this.stageValue?.minFontSize, this.value?.minFontSize) || 203 !isBaseOrResourceEqual(this.stageValue?.maxFontSize, this.value?.maxFontSize); 204 } 205 } 206} 207 208class TextpickerTextStyleModifier extends ModifierWithKey<PickerTextStyle> { 209 constructor(value: PickerTextStyle) { 210 super(value); 211 } 212 static identity: Symbol = Symbol('textpickerTextStyle'); 213 applyPeer(node: KNode, reset: boolean): void { 214 if (reset) { 215 getUINativeModule().textpicker.resetTextStyle(node); 216 } else { 217 getUINativeModule().textpicker.setTextStyle(node, this.value?.color ?? undefined, 218 this.value?.font?.size ?? undefined, 219 this.value?.font?.weight ?? undefined, 220 this.value?.font?.family ?? undefined, 221 this.value?.font?.style ?? undefined); 222 } 223 } 224 225 checkObjectDiff(): boolean { 226 if (!(this.stageValue?.font?.weight === this.value?.font?.weight && 227 this.stageValue?.font?.style === this.value?.font?.style)) { 228 return true; 229 } else { 230 return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) || 231 !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) || 232 !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family); 233 } 234 } 235} 236 237class TextpickerSelectedTextStyleModifier extends ModifierWithKey<PickerTextStyle> { 238 constructor(value: PickerTextStyle) { 239 super(value); 240 } 241 static identity: Symbol = Symbol('textpickerSelectedTextStyle'); 242 applyPeer(node: KNode, reset: boolean): void { 243 if (reset) { 244 getUINativeModule().textpicker.resetSelectedTextStyle(node); 245 } else { 246 getUINativeModule().textpicker.setSelectedTextStyle(node, this.value?.color ?? undefined, 247 this.value?.font?.size ?? undefined, 248 this.value?.font?.weight ?? undefined, 249 this.value?.font?.family ?? undefined, 250 this.value?.font?.style ?? undefined); 251 } 252 } 253 254 checkObjectDiff(): boolean { 255 if (!(this.stageValue?.font?.weight === this.value?.font?.weight && 256 this.stageValue?.font?.style === this.value?.font?.style)) { 257 return true; 258 } else { 259 return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) || 260 !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) || 261 !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family); 262 } 263 } 264} 265 266class TextpickerDisappearTextStyleModifier extends ModifierWithKey<PickerTextStyle> { 267 constructor(value: PickerTextStyle) { 268 super(value); 269 } 270 static identity: Symbol = Symbol('textpickerDisappearTextStyle'); 271 applyPeer(node: KNode, reset: boolean): void { 272 if (reset) { 273 getUINativeModule().textpicker.resetDisappearTextStyle(node); 274 } else { 275 getUINativeModule().textpicker.setDisappearTextStyle(node, this.value?.color ?? undefined, 276 this.value?.font?.size ?? undefined, 277 this.value?.font?.weight ?? undefined, 278 this.value?.font?.family ?? undefined, 279 this.value?.font?.style ?? undefined); 280 } 281 } 282 283 checkObjectDiff(): boolean { 284 if (!(this.stageValue?.font?.weight === this.value?.font?.weight && 285 this.stageValue?.font?.style === this.value?.font?.style)) { 286 return true; 287 } else { 288 return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) || 289 !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) || 290 !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family); 291 } 292 } 293} 294 295class TextpickerDefaultPickerItemHeightModifier extends ModifierWithKey<number | string> { 296 constructor(value: number | string) { 297 super(value); 298 } 299 static identity: Symbol = Symbol('textpickerDefaultPickerItemHeight'); 300 applyPeer(node: KNode, reset: boolean): void { 301 if (reset) { 302 getUINativeModule().textpicker.resetDefaultPickerItemHeight(node); 303 } else { 304 getUINativeModule().textpicker.setDefaultPickerItemHeight(node, this.value); 305 } 306 } 307} 308 309// @ts-ignore 310globalThis.TextPicker.attributeModifier = function (modifier: ArkComponent): void { 311 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 312 return new ArkTextPickerComponent(nativePtr); 313 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 314 return new modifierJS.TextPickerModifier(nativePtr, classType); 315 }); 316}; 317