• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) {
19    super(nativePtr);
20  }
21  onGestureJudgeBegin(callback: (gestureInfo: GestureInfo, event: BaseGestureEvent) => GestureJudgeResult): this {
22    throw new Error('Method not implemented.');
23  }
24  defaultPickerItemHeight(value: string | number): this {
25    modifierWithKey(
26      this._modifiersWithKeys, TextpickerDefaultPickerItemHeightModifier.identity, TextpickerDefaultPickerItemHeightModifier, value);
27    return this;
28  }
29  canLoop(value: boolean): this {
30    modifierWithKey(
31      this._modifiersWithKeys, TextpickerCanLoopModifier.identity, TextpickerCanLoopModifier, value);
32    return this;
33  }
34  disappearTextStyle(value: PickerTextStyle): this {
35    modifierWithKey(
36      this._modifiersWithKeys, TextpickerDisappearTextStyleModifier.identity, TextpickerDisappearTextStyleModifier, value);
37    return this;
38  }
39  textStyle(value: PickerTextStyle): this {
40    modifierWithKey(
41      this._modifiersWithKeys, TextpickerTextStyleModifier.identity, TextpickerTextStyleModifier, value);
42    return this;
43  }
44  selectedTextStyle(value: PickerTextStyle): this {
45    modifierWithKey(
46      this._modifiersWithKeys, TextpickerSelectedTextStyleModifier.identity, TextpickerSelectedTextStyleModifier, value);
47    return this;
48  }
49  onAccept(callback: (value: string, index: number) => void): this {
50    throw new Error('Method not implemented.');
51  }
52  onCancel(callback: () => void): this {
53    throw new Error('Method not implemented.');
54  }
55  onChange(callback: (value: string | string[], index: number | number[]) => void): this {
56    throw new Error('Method not implemented.');
57  }
58  selectedIndex(value: number | number[]): this {
59    modifierWithKey(
60      this._modifiersWithKeys, TextpickerSelectedIndexModifier.identity, TextpickerSelectedIndexModifier, value);
61    return this;
62  }
63}
64
65class TextpickerCanLoopModifier extends ModifierWithKey<boolean> {
66  constructor(value: boolean) {
67    super(value);
68  }
69  static identity: Symbol = Symbol('textpickerCanLoop');
70  applyPeer(node: KNode, reset: boolean): void {
71    if (reset) {
72      getUINativeModule().textpicker.resetCanLoop(node);
73    } else {
74      getUINativeModule().textpicker.setCanLoop(node, this.value);
75    }
76  }
77}
78
79class TextpickerSelectedIndexModifier extends ModifierWithKey<number | number[]> {
80  constructor(value: number | number[]) {
81    super(value);
82  }
83  static identity: Symbol = Symbol('textpickerSelectedIndex');
84  applyPeer(node: KNode, reset: boolean): void {
85    if (reset) {
86      getUINativeModule().textpicker.resetSelectedIndex(node);
87    } else {
88      getUINativeModule().textpicker.setSelectedIndex(node, this.value);
89    }
90  }
91
92  checkObjectDiff(): boolean {
93    if (Array.isArray(this.stageValue) && Array.isArray(this.value)) {
94      return !deepCompareArrays(this.stageValue, this.value);
95    } else if (Array.isArray(this.stageValue) || Array.isArray(this.value)) {
96      return true;
97    } else {
98      return this.stageValue !== this.value;
99    }
100  }
101
102}
103
104class TextpickerTextStyleModifier extends ModifierWithKey<PickerTextStyle> {
105  constructor(value: PickerTextStyle) {
106    super(value);
107  }
108  static identity: Symbol = Symbol('textpickerTextStyle');
109  applyPeer(node: KNode, reset: boolean): void {
110    if (reset) {
111      getUINativeModule().textpicker.resetTextStyle(node);
112    } else {
113      getUINativeModule().textpicker.setTextStyle(node, this.value?.color ?? undefined,
114        this.value?.font?.size ?? undefined,
115        this.value?.font?.weight ?? undefined,
116        this.value?.font?.family ?? undefined,
117        this.value?.font?.style ?? undefined);
118    }
119  }
120
121  checkObjectDiff(): boolean {
122    if (!(this.stageValue?.font?.weight === this.value?.font?.weight &&
123      this.stageValue?.font?.style === this.value?.font?.style)) {
124      return true;
125    } else {
126      return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) ||
127        !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) ||
128        !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family);
129    }
130  }
131}
132
133class TextpickerSelectedTextStyleModifier extends ModifierWithKey<PickerTextStyle> {
134  constructor(value: PickerTextStyle) {
135    super(value);
136  }
137  static identity: Symbol = Symbol('textpickerSelectedTextStyle');
138  applyPeer(node: KNode, reset: boolean): void {
139    if (reset) {
140      getUINativeModule().textpicker.resetSelectedTextStyle(node);
141    } else {
142      getUINativeModule().textpicker.setSelectedTextStyle(node, this.value?.color ?? undefined,
143        this.value?.font?.size ?? undefined,
144        this.value?.font?.weight ?? undefined,
145        this.value?.font?.family ?? undefined,
146        this.value?.font?.style ?? undefined);
147    }
148  }
149
150  checkObjectDiff(): boolean {
151    if (!(this.stageValue?.font?.weight === this.value?.font?.weight &&
152      this.stageValue?.font?.style === this.value?.font?.style)) {
153      return true;
154    } else {
155      return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) ||
156        !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) ||
157        !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family);
158    }
159  }
160}
161
162class TextpickerDisappearTextStyleModifier extends ModifierWithKey<PickerTextStyle> {
163  constructor(value: PickerTextStyle) {
164    super(value);
165  }
166  static identity: Symbol = Symbol('textpickerDisappearTextStyle');
167  applyPeer(node: KNode, reset: boolean): void {
168    if (reset) {
169      getUINativeModule().textpicker.resetDisappearTextStyle(node);
170    } else {
171      getUINativeModule().textpicker.setDisappearTextStyle(node, this.value?.color ?? undefined,
172        this.value?.font?.size ?? undefined,
173        this.value?.font?.weight ?? undefined,
174        this.value?.font?.family ?? undefined,
175        this.value?.font?.style ?? undefined);
176    }
177  }
178
179  checkObjectDiff(): boolean {
180    if (!(this.stageValue?.font?.weight === this.value?.font?.weight &&
181      this.stageValue?.font?.style === this.value?.font?.style)) {
182      return true;
183    } else {
184      return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) ||
185        !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size) ||
186        !isBaseOrResourceEqual(this.stageValue?.font?.family, this.value?.font?.family);
187    }
188  }
189}
190
191class TextpickerDefaultPickerItemHeightModifier extends ModifierWithKey<number | string> {
192  constructor(value: number | string) {
193    super(value);
194  }
195  static identity: Symbol = Symbol('textpickerDefaultPickerItemHeight');
196  applyPeer(node: KNode, reset: boolean): void {
197    if (reset) {
198      getUINativeModule().textpicker.resetDefaultPickerItemHeight(node);
199    } else {
200      getUINativeModule().textpicker.setDefaultPickerItemHeight(node, this.value);
201    }
202  }
203}
204
205// @ts-ignore
206globalThis.TextPicker.attributeModifier = function (modifier) {
207  const elmtId = ViewStackProcessor.GetElmtIdToAccountFor();
208  let nativeNode = getUINativeModule().getFrameNodeById(elmtId);
209  let component = this.createOrGetNode(elmtId, () => {
210    return new ArkTextPickerComponent(nativeNode);
211  });
212  applyUIAttributes(modifier, nativeNode, component);
213  component.applyModifierPatch();
214};
215