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} 71 72class TextpickerCanLoopModifier extends ModifierWithKey<boolean> { 73 constructor(value: boolean) { 74 super(value); 75 } 76 static identity: Symbol = Symbol('textpickerCanLoop'); 77 applyPeer(node: KNode, reset: boolean): void { 78 if (reset) { 79 getUINativeModule().textpicker.resetCanLoop(node); 80 } else { 81 getUINativeModule().textpicker.setCanLoop(node, this.value); 82 } 83 } 84} 85 86class TextpickerSelectedIndexModifier extends ModifierWithKey<number | number[]> { 87 constructor(value: number | number[]) { 88 super(value); 89 } 90 static identity: Symbol = Symbol('textpickerSelectedIndex'); 91 applyPeer(node: KNode, reset: boolean): void { 92 if (reset) { 93 getUINativeModule().textpicker.resetSelectedIndex(node); 94 } else { 95 getUINativeModule().textpicker.setSelectedIndex(node, this.value); 96 } 97 } 98 99 checkObjectDiff(): boolean { 100 if (Array.isArray(this.stageValue) && Array.isArray(this.value)) { 101 return !deepCompareArrays(this.stageValue, this.value); 102 } else if (Array.isArray(this.stageValue) || Array.isArray(this.value)) { 103 return true; 104 } else { 105 return this.stageValue !== this.value; 106 } 107 } 108 109} 110 111class TextpickerDividerModifier extends ModifierWithKey<DividerOptions | null> { 112 constructor(value: DividerOptions | null) { 113 super(value); 114 } 115 static identity: Symbol = Symbol('textpickerDivider'); 116 applyPeer(node: KNode, reset: boolean): void { 117 if (reset) { 118 getUINativeModule().textpicker.resetDivider(node, this.value); 119 } else { 120 getUINativeModule().textpicker.setDivider(node, this.value?.strokeWidth, this.value?.color, this.value?.startMargin, this.value?.endMargin); 121 } 122 } 123 124 checkObjectDiff(): boolean { 125 return !(this.stageValue?.strokeWidth === this.value?.strokeWidth && 126 this.stageValue?.color === this.value?.color && 127 this.stageValue?.startMargin === this.value?.startMargin && 128 this.stageValue?.endMargin === this.value?.endMargin); 129 } 130} 131 132class TextpickerGradientHeightModifier extends ModifierWithKey<Dimension> { 133 constructor(value: Dimension) { 134 super(value); 135 } 136 static identity: Symbol = Symbol('textpickerGradientHeight'); 137 applyPeer(node: KNode, reset: boolean): void { 138 if (reset) { 139 getUINativeModule().textpicker.resetGradientHeight(node); 140 } else { 141 getUINativeModule().textpicker.setGradientHeight(node, this.value); 142 } 143 } 144 checkObjectDiff(): boolean { 145 return !isBaseOrResourceEqual(this.stageValue, this.value); 146 } 147} 148 149class TextpickerTextStyleModifier extends ModifierWithKey<PickerTextStyle> { 150 constructor(value: PickerTextStyle) { 151 super(value); 152 } 153 static identity: Symbol = Symbol('textpickerTextStyle'); 154 applyPeer(node: KNode, reset: boolean): void { 155 if (reset) { 156 getUINativeModule().textpicker.resetTextStyle(node); 157 } else { 158 getUINativeModule().textpicker.setTextStyle(node, this.value?.color ?? undefined, 159 this.value?.font?.size ?? undefined, 160 this.value?.font?.weight ?? undefined, 161 this.value?.font?.family ?? undefined, 162 this.value?.font?.style ?? undefined); 163 } 164 } 165 166 checkObjectDiff(): boolean { 167 if (!(this.stageValue?.font?.weight === this.value?.font?.weight && 168 this.stageValue?.font?.style === this.value?.font?.style)) { 169 return true; 170 } else { 171 return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) || 172 !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) || 173 !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family); 174 } 175 } 176} 177 178class TextpickerSelectedTextStyleModifier extends ModifierWithKey<PickerTextStyle> { 179 constructor(value: PickerTextStyle) { 180 super(value); 181 } 182 static identity: Symbol = Symbol('textpickerSelectedTextStyle'); 183 applyPeer(node: KNode, reset: boolean): void { 184 if (reset) { 185 getUINativeModule().textpicker.resetSelectedTextStyle(node); 186 } else { 187 getUINativeModule().textpicker.setSelectedTextStyle(node, this.value?.color ?? undefined, 188 this.value?.font?.size ?? undefined, 189 this.value?.font?.weight ?? undefined, 190 this.value?.font?.family ?? undefined, 191 this.value?.font?.style ?? undefined); 192 } 193 } 194 195 checkObjectDiff(): boolean { 196 if (!(this.stageValue?.font?.weight === this.value?.font?.weight && 197 this.stageValue?.font?.style === this.value?.font?.style)) { 198 return true; 199 } else { 200 return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) || 201 !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) || 202 !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family); 203 } 204 } 205} 206 207class TextpickerDisappearTextStyleModifier extends ModifierWithKey<PickerTextStyle> { 208 constructor(value: PickerTextStyle) { 209 super(value); 210 } 211 static identity: Symbol = Symbol('textpickerDisappearTextStyle'); 212 applyPeer(node: KNode, reset: boolean): void { 213 if (reset) { 214 getUINativeModule().textpicker.resetDisappearTextStyle(node); 215 } else { 216 getUINativeModule().textpicker.setDisappearTextStyle(node, this.value?.color ?? undefined, 217 this.value?.font?.size ?? undefined, 218 this.value?.font?.weight ?? undefined, 219 this.value?.font?.family ?? undefined, 220 this.value?.font?.style ?? undefined); 221 } 222 } 223 224 checkObjectDiff(): boolean { 225 if (!(this.stageValue?.font?.weight === this.value?.font?.weight && 226 this.stageValue?.font?.style === this.value?.font?.style)) { 227 return true; 228 } else { 229 return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) || 230 !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) || 231 !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family); 232 } 233 } 234} 235 236class TextpickerDefaultPickerItemHeightModifier extends ModifierWithKey<number | string> { 237 constructor(value: number | string) { 238 super(value); 239 } 240 static identity: Symbol = Symbol('textpickerDefaultPickerItemHeight'); 241 applyPeer(node: KNode, reset: boolean): void { 242 if (reset) { 243 getUINativeModule().textpicker.resetDefaultPickerItemHeight(node); 244 } else { 245 getUINativeModule().textpicker.setDefaultPickerItemHeight(node, this.value); 246 } 247 } 248} 249 250// @ts-ignore 251globalThis.TextPicker.attributeModifier = function (modifier: ArkComponent): void { 252 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 253 return new ArkTextPickerComponent(nativePtr); 254 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 255 return new modifierJS.TextPickerModifier(nativePtr, classType); 256 }); 257}; 258