• 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' />
17
18import { ArkScrollable } from "./ArkScrollable";
19
20class ScrollNestedScrollModifier extends ModifierWithKey<ArkNestedScrollOptions> {
21  static identity: symbol = Symbol('nestedScroll');
22  applyPeer(node: KNode, reset: boolean): void {
23    if (reset) {
24      getUINativeModule().scroll.resetNestedScroll(node);
25    } else {
26      getUINativeModule().scroll.setNestedScroll(node, this.value.scrollForward, this.value.scrollBackward);
27    }
28  }
29
30  checkObjectDiff(): boolean {
31    return !isBaseOrResourceEqual(this.stageValue.scrollForward, this.value.scrollForward) ||
32      !isBaseOrResourceEqual(this.stageValue.scrollBackward, this.value.scrollBackward);
33  }
34}
35
36class ScrollEnableScrollInteractionModifier extends ModifierWithKey<boolean> {
37  static identity: symbol = Symbol('enableScrollInteraction');
38  applyPeer(node: KNode, reset: boolean): void {
39    if (reset) {
40      getUINativeModule().scroll.resetEnableScroll(node);
41    } else {
42      getUINativeModule().scroll.setEnableScroll(node, this.value);
43    }
44  }
45
46  checkObjectDiff(): boolean {
47    return !isBaseOrResourceEqual(this.stageValue, this.value);
48  }
49}
50
51class ScrollFrictionModifier extends ModifierWithKey<number | Resource> {
52  static identity: symbol = Symbol('friction');
53  applyPeer(node: KNode, reset: boolean): void {
54    if (reset) {
55      getUINativeModule().scroll.resetFriction(node);
56    } else {
57      getUINativeModule().scroll.setFriction(node, this.value);
58    }
59  }
60
61  checkObjectDiff(): boolean {
62    return !isBaseOrResourceEqual(this.stageValue, this.value);
63  }
64}
65
66class ScrollScrollSnapModifier extends ModifierWithKey<ArkScrollSnapOptions> {
67  static identity: symbol = Symbol('scrollSnap');
68  applyPeer(node: KNode, reset: boolean): void {
69    if (reset) {
70      getUINativeModule().scroll.resetScrollSnap(node);
71    } else {
72      let snapPagination = [];
73      let isArray = true;
74      if (Array.isArray(this.value.snapPagination)) {
75        for (let i = 0; i < this.value.snapPagination.length; i++) {
76          let item = this.value.snapPagination[i];
77          snapPagination.push(item);
78        }
79      } else {
80        isArray = false;
81      }
82
83      if (isArray) {
84        getUINativeModule().scroll.setScrollSnap(node, this.value.snapAlign, snapPagination, this.value.enableSnapToStart, this.value.enableSnapToEnd);
85      } else {
86        getUINativeModule().scroll.setScrollSnap(node, this.value.snapAlign,
87          this.value.snapPagination, this.value.enableSnapToStart, this.value.enableSnapToEnd);
88      }
89    }
90  }
91
92  checkObjectDiff(): boolean {
93    return !((this.stageValue.snapAlign === this.value.snapAlign) &&
94      (this.stageValue.enableSnapToStart === this.value.enableSnapToStart) &&
95      (this.stageValue.enableSnapToEnd === this.value.enableSnapToEnd) &&
96      (this.stageValue.snapPagination === this.value.snapPagination));
97  }
98}
99
100class ScrollScrollBarModifier extends ModifierWithKey<number> {
101  static identity: symbol = Symbol('scrollBar');
102  applyPeer(node: KNode, reset: boolean): void {
103    if (reset) {
104      getUINativeModule().scroll.resetScrollBar(node);
105    } else {
106      getUINativeModule().scroll.setScrollBar(node, this.value);
107    }
108  }
109  checkObjectDiff(): boolean {
110    return !isBaseOrResourceEqual(this.stageValue, this.value);
111  }
112}
113
114class ScrollScrollableModifier extends ModifierWithKey<ScrollDirection> {
115  constructor(value: ScrollDirection) {
116    super(value);
117  }
118  static identity: Symbol = Symbol('scrollable');
119  applyPeer(node: KNode, reset: boolean): void {
120    if (reset) {
121      getUINativeModule().scroll.resetScrollable(node);
122    } else {
123      getUINativeModule().scroll.setScrollable(node, this.value);
124    }
125  }
126
127  checkObjectDiff(): boolean {
128    return this.stageValue !== this.value;
129  }
130}
131
132class ScrollEdgeEffectModifier extends ModifierWithKey<ArkScrollEdgeEffect> {
133  constructor(value: ArkScrollEdgeEffect) {
134    super(value);
135  }
136  static identity: Symbol = Symbol('edgeEffect');
137  applyPeer(node: KNode, reset: boolean): void {
138    if (reset) {
139      getUINativeModule().scroll.resetEdgeEffect(node);
140    } else {
141      getUINativeModule().scroll.setEdgeEffect(node, this.value.value, this.value.options?.alwaysEnabled);
142    }
143  }
144
145  checkObjectDiff(): boolean {
146    return !((this.stageValue.value === this.value.value) &&
147      (this.stageValue.options === this.value.options));
148  }
149}
150
151class ScrollFadingEdgeModifier extends ModifierWithKey<ArkFadingEdge> {
152  constructor(value: ArkFadingEdge) {
153    super(value);
154  }
155  static identity: Symbol = Symbol('scrollFadingEdge');
156  applyPeer(node: KNode, reset: boolean): void {
157    if (reset) {
158      getUINativeModule().scroll.resetFadingEdge(node);
159    } else {
160      getUINativeModule().scroll.setFadingEdge(node, this.value.value!, this.value.options?.fadingEdgeLength);
161    }
162  }
163  checkObjectDiff(): boolean {
164    return !((this.stageValue.value === this.value.value) &&
165      (this.stageValue.options === this.value.options));
166  }
167}
168
169class ScrollScrollBarWidthModifier extends ModifierWithKey<string | number> {
170  constructor(value: string | number) {
171    super(value);
172  }
173  static identity: Symbol = Symbol('scrollBarWidth');
174  applyPeer(node: KNode, reset: boolean): void {
175    if (reset) {
176      getUINativeModule().scroll.resetScrollBarWidth(node);
177    } else {
178      getUINativeModule().scroll.setScrollBarWidth(node, this.value);
179    }
180  }
181
182  checkObjectDiff(): boolean {
183    return this.stageValue !== this.value;
184  }
185}
186
187class ScrollScrollBarColorModifier extends ModifierWithKey<ResourceColor> {
188  constructor(value: ResourceColor) {
189    super(value);
190  }
191  static identity: Symbol = Symbol('scrollBarColor');
192  applyPeer(node: KNode, reset: boolean): void {
193    if (reset) {
194      getUINativeModule().scroll.resetScrollBarColor(node);
195    } else {
196      getUINativeModule().scroll.setScrollBarColor(node, this.value);
197    }
198  }
199
200  checkObjectDiff(): boolean {
201    return !isBaseOrResourceEqual(this.stageValue, this.value);
202  }
203}
204
205class ScrollEnablePagingModifier extends ModifierWithKey<boolean> {
206  constructor(value: boolean) {
207    super(value);
208  }
209  static identity: Symbol = Symbol('scrollEnablePaging');
210  applyPeer(node: KNode, reset: boolean): void {
211    if (reset) {
212      getUINativeModule().scroll.resetEnablePaging(node);
213    } else {
214      getUINativeModule().scroll.setEnablePaging(node, this.value);
215    }
216  }
217}
218
219class ScrollClipModifier extends ModifierWithKey<boolean | object> {
220  constructor(value: boolean | object) {
221    super(value);
222  }
223  static identity: Symbol = Symbol('scrollClip');
224  applyPeer(node: KNode, reset: boolean): void {
225    if (reset) {
226      getUINativeModule().common.resetClipWithEdge(node);
227    } else {
228      getUINativeModule().common.setClipWithEdge(node, this.value);
229    }
230  }
231
232  checkObjectDiff(): boolean {
233    return true;
234  }
235}
236
237class ScrollInitialOffsetModifier extends ModifierWithKey<ArkScrollOffsetOptions> {
238  static identity: Symbol = Symbol('initialOffset');
239  applyPeer(node: KNode, reset: boolean): void {
240    if (reset) {
241      getUINativeModule().scroll.resetInitialOffset(node);
242    } else {
243      getUINativeModule().scroll.setInitialOffset(node, this.value.xOffset, this.value.yOffset);
244    }
245  }
246
247  checkObjectDiff(): boolean {
248    return !((this.stageValue.xOffset === this.value.xOffset) &&
249    (this.stageValue.yOffset === this.value.yOffset));
250  }
251}
252
253class ScrollFlingSpeedLimitModifier extends ModifierWithKey<number> {
254  static identity: symbol = Symbol('flingSpeedLimit');
255  applyPeer(node: KNode, reset: boolean): void {
256    if (reset) {
257      getUINativeModule().scroll.resetFlingSpeedLimit(node);
258    } else {
259      getUINativeModule().scroll.setFlingSpeedLimit(node, this.value);
260    }
261  }
262  checkObjectDiff(): boolean {
263    return !isBaseOrResourceEqual(this.stageValue, this.value);
264  }
265}
266
267class ScrollInitializeModifier extends ModifierWithKey<Scroller> {
268  constructor(value: Scroller) {
269    super(value);
270  }
271  static identity: Symbol = Symbol('scrollInitialize');
272  applyPeer(node: KNode, reset: boolean): void {
273    if (reset) {
274      getUINativeModule().scroll.resetScrollInitialize(node);
275    } else {
276      getUINativeModule().scroll.setScrollInitialize(node, this.value);
277    }
278  }
279}
280
281class ScrollOnScrollStartModifier extends ModifierWithKey<() => void> {
282  constructor(value: () => void) {
283    super(value);
284  }
285  static identity: Symbol = Symbol('scrollOnScrollStart');
286  applyPeer(node: KNode, reset: boolean): void {
287    if (reset) {
288      getUINativeModule().scroll.resetScrollOnScrollStart(node);
289    } else {
290      getUINativeModule().scroll.setScrollOnScrollStart(node, this.value);
291    }
292  }
293}
294
295class ScrollOnScrollEndModifier extends ModifierWithKey<() => void> {
296  constructor(value: () => void) {
297    super(value);
298  }
299  static identity: Symbol = Symbol('scrollOnScrollEnd');
300  applyPeer(node: KNode, reset: boolean): void {
301    if (reset) {
302      getUINativeModule().scroll.resetScrollOnScrollEnd(node);
303    } else {
304      getUINativeModule().scroll.setScrollOnScrollEnd(node, this.value);
305    }
306  }
307}
308
309class ScrollOnScrollStopModifier extends ModifierWithKey<() => void> {
310  constructor(value: () => void) {
311    super(value);
312  }
313  static identity: Symbol = Symbol('scrollOnScrollStop');
314  applyPeer(node: KNode, reset: boolean): void {
315    if (reset) {
316      getUINativeModule().scroll.resetScrollOnScrollStop(node);
317    } else {
318      getUINativeModule().scroll.setScrollOnScrollStop(node, this.value);
319    }
320  }
321}
322
323class ScrollOnScrollModifier extends ModifierWithKey<(xOffset: number, yOffset: number) => void> {
324  constructor(value: (xOffset: number, yOffset: number) => void) {
325    super(value);
326  }
327  static identity: Symbol = Symbol('scrollOnScroll');
328  applyPeer(node: KNode, reset: boolean): void {
329    if (reset) {
330      getUINativeModule().scroll.resetScrollOnScrollModifier(node);
331    } else {
332      getUINativeModule().scroll.setScrollOnScrollModifier(node, this.value);
333    }
334  }
335}
336
337class ScrollOnScrollEdgeModifier extends ModifierWithKey<(side: Edge) => void> {
338  constructor(value: (side: Edge) => void) {
339    super(value);
340  }
341  static identity: Symbol = Symbol('scrollOnScrollEdge');
342  applyPeer(node: KNode, reset: boolean): void {
343    if (reset) {
344      getUINativeModule().scroll.resetScrollOnScrollEdge(node);
345    } else {
346      getUINativeModule().scroll.setScrollOnScrollEdge(node, this.value);
347    }
348  }
349}
350
351class ScrollOnDidScrollModifier extends ModifierWithKey<(xOffset: number,
352                                                         yOffset: number, scrollState: ScrollState) => void> {
353  constructor(value: (xOffset: number, yOffset: number, scrollState: ScrollState) => void) {
354    super(value);
355  }
356  static identity: Symbol = Symbol('scrollOnDidScroll');
357  applyPeer(node: KNode, reset: boolean): void {
358    if (reset) {
359      getUINativeModule().scroll.resetScrollOnDidScroll(node);
360    } else {
361      getUINativeModule().scroll.setScrollOnDidScroll(node, this.value);
362    }
363  }
364}
365
366class ScrollOnWillScrollModifier extends ModifierWithKey<(xOffset: number, yOffset: number,
367  scrollState: ScrollState, scrollSource: ScrollSource) => void | OffsetResult> {
368  constructor(value: (xOffset: number, yOffset: number,
369    scrollState: ScrollState, scrollSource: ScrollSource) => void | OffsetResult) {
370    super(value);
371  }
372  static identity: Symbol = Symbol('scrollOnWillScroll');
373  applyPeer(node: KNode, reset: boolean): void {
374    if (reset) {
375      getUINativeModule().scroll.resetScrollOnWillScroll(node);
376    } else {
377      getUINativeModule().scroll.setScrollOnWillScroll(node, this.value);
378    }
379  }
380}
381
382class ScrollOnScrollFrameBeginModifier extends ModifierWithKey<(offset: number, state: ScrollState) =>
383  { offsetRemain: number }> {
384  constructor(value: (offset: number, state: ScrollState) =>
385    { offsetRemain: number }) {
386    super(value);
387  }
388  static identity: Symbol = Symbol('scrollOnScrollFrameBegin');
389  applyPeer(node: KNode, reset: boolean): void {
390    if (reset) {
391      getUINativeModule().scroll.resetScrollOnScrollFrameBegin(node);
392    } else {
393      getUINativeModule().scroll.setScrollOnScrollFrameBegin(node, this.value);
394    }
395  }
396}
397
398class ArkScrollComponent extends ArkScrollable<ScrollAttribute> implements ScrollAttribute {
399  constructor(nativePtr: KNode, classType?: ModifierType) {
400    super(nativePtr, classType);
401  }
402  initialize(value: Object[]): ScrollAttribute {
403    if (value[0] !== undefined) {
404      modifierWithKey(this._modifiersWithKeys, ScrollInitializeModifier.identity, ScrollInitializeModifier, value[0]);
405    }
406    return this;
407  }
408  allowChildCount(): number {
409    return 1;
410  }
411  scrollable(value: ScrollDirection): this {
412    modifierWithKey(this._modifiersWithKeys, ScrollScrollableModifier.identity, ScrollScrollableModifier, value);
413    return this;
414  }
415  onScroll(event: (xOffset: number, yOffset: number) => void): this {
416    modifierWithKey(this._modifiersWithKeys, ScrollOnScrollModifier.identity, ScrollOnScrollModifier, event);
417    return this;
418  }
419  onScrollEdge(event: (side: Edge) => void): this {
420    modifierWithKey(this._modifiersWithKeys, ScrollOnScrollEdgeModifier.identity, ScrollOnScrollEdgeModifier, event);
421    return this;
422  }
423  onScrollStart(event: () => void): this {
424    modifierWithKey(this._modifiersWithKeys, ScrollOnScrollStartModifier.identity, ScrollOnScrollStartModifier, event);
425    return this;
426  }
427  onScrollEnd(event: () => void): this {
428    modifierWithKey(this._modifiersWithKeys, ScrollOnScrollEndModifier.identity, ScrollOnScrollEndModifier, event);
429    return this;
430  }
431  onScrollStop(event: () => void): this {
432    modifierWithKey(this._modifiersWithKeys, ScrollOnScrollStopModifier.identity, ScrollOnScrollStopModifier, event);
433    return this;
434  }
435  enablePaging(value: boolean): this {
436    modifierWithKey(this._modifiersWithKeys, ScrollEnablePagingModifier.identity, ScrollEnablePagingModifier, value);
437    return this;
438  }
439  scrollBar(value: BarState): this {
440    if (value in BarState) {
441      modifierWithKey(this._modifiersWithKeys, ScrollScrollBarModifier.identity, ScrollScrollBarModifier, value);
442    } else {
443      modifierWithKey(this._modifiersWithKeys, ScrollScrollBarModifier.identity, ScrollScrollBarModifier, undefined);
444    }
445    return this;
446  }
447  scrollBarColor(color: ResourceColor): this {
448    modifierWithKey(this._modifiersWithKeys, ScrollScrollBarColorModifier.identity, ScrollScrollBarColorModifier, color);
449    return this;
450  }
451  scrollBarWidth(value: string | number): this {
452    modifierWithKey(this._modifiersWithKeys, ScrollScrollBarWidthModifier.identity, ScrollScrollBarWidthModifier, value);
453    return this;
454  }
455  edgeEffect(value: EdgeEffect, options?: EdgeEffectOptions): this {
456    let effect: ArkScrollEdgeEffect = new ArkScrollEdgeEffect();
457    effect.value = value;
458    effect.options = options;
459    modifierWithKey(this._modifiersWithKeys, ScrollEdgeEffectModifier.identity, ScrollEdgeEffectModifier, effect);
460    return this;
461  }
462  fadingEdge(value: boolean, options?: FadingEdgeOptions | undefined): this {
463    let fadingEdge: ArkFadingEdge = new ArkFadingEdge();
464    fadingEdge.value = value;
465    fadingEdge.options = options;
466    modifierWithKey(this._modifiersWithKeys, ScrollFadingEdgeModifier.identity, ScrollFadingEdgeModifier, fadingEdge);
467    return this;
468  }
469  onScrollFrameBegin(callback: (offset: number, state: ScrollState) => { offsetRemain: number }): this {
470    modifierWithKey(this._modifiersWithKeys, ScrollOnScrollFrameBeginModifier.identity, ScrollOnScrollFrameBeginModifier, callback);
471    return this;
472  }
473  onWillScroll(callback: (xOffset: number, yOffset: number,
474    scrollState: ScrollState, scrollSource: ScrollSource) => void | OffsetResult): this {
475    modifierWithKey(this._modifiersWithKeys, ScrollOnWillScrollModifier.identity, ScrollOnWillScrollModifier, callback);
476    return this;
477  }
478
479  onDidScroll(callback: (xOffset: number, yOffset: number, scrollState: ScrollState) => void): this {
480    modifierWithKey(this._modifiersWithKeys, ScrollOnDidScrollModifier.identity, ScrollOnDidScrollModifier, callback);
481    return this;
482  }
483
484  nestedScroll(value: NestedScrollOptions): ScrollAttribute {
485    let options = new ArkNestedScrollOptions();
486    if (value) {
487      if (value.scrollForward) {
488        options.scrollForward = value.scrollForward;
489      }
490      if (value.scrollBackward) {
491        options.scrollBackward = value.scrollBackward;
492      }
493      modifierWithKey(this._modifiersWithKeys, ScrollNestedScrollModifier.identity, ScrollNestedScrollModifier, options);
494    }
495    return this;
496  }
497  enableScrollInteraction(value: boolean): ScrollAttribute {
498    modifierWithKey(this._modifiersWithKeys, ScrollEnableScrollInteractionModifier.identity, ScrollEnableScrollInteractionModifier, value);
499    return this;
500  }
501  friction(value: number | Resource): ScrollAttribute {
502    modifierWithKey(this._modifiersWithKeys, ScrollFrictionModifier.identity, ScrollFrictionModifier, value);
503    return this;
504  }
505  scrollSnap(value: ScrollSnapOptions): ScrollAttribute {
506    let options = new ArkScrollSnapOptions();
507    if (value) {
508      if (value.snapAlign) {
509        options.snapAlign = value.snapAlign;
510      }
511      if (value.snapPagination) {
512        options.snapPagination = value.snapPagination;
513      }
514      if (value.enableSnapToStart) {
515        options.enableSnapToStart = value.enableSnapToStart;
516      }
517      if (value.enableSnapToEnd) {
518        options.enableSnapToEnd = value.enableSnapToEnd;
519      }
520      modifierWithKey(this._modifiersWithKeys, ScrollScrollSnapModifier.identity, ScrollScrollSnapModifier, options);
521    }
522    return this;
523  }
524  clip(value: boolean | CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute): this {
525    modifierWithKey(this._modifiersWithKeys, ScrollClipModifier.identity, ScrollClipModifier, value);
526    return this;
527  }
528  initialOffset(value: OffsetOptions): ScrollAttribute {
529    let options = new ArkScrollOffsetOptions();
530    if (value) {
531      if (value.xOffset) {
532        options.xOffset = value.xOffset;
533      }
534      if (value.yOffset) {
535        options.yOffset = value.yOffset;
536      }
537      modifierWithKey(this._modifiersWithKeys, ScrollInitialOffsetModifier.identity, ScrollInitialOffsetModifier, options);
538    }
539    return this;
540  }
541  flingSpeedLimit(value: number): this {
542    modifierWithKey(this._modifiersWithKeys, ScrollFlingSpeedLimitModifier.identity, ScrollFlingSpeedLimitModifier, value);
543    return this;
544  }
545}
546// @ts-ignore
547globalThis.Scroll.attributeModifier = function (modifier: ArkComponent): void {
548  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
549    return new ArkScrollComponent(nativePtr);
550  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
551    return new modifierJS.ScrollModifier(nativePtr, classType);
552  });
553};
554