• 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 ItemConstraintSizeModifier extends ModifierWithKey<ArkConstraintSizeOptions> {
21  constructor(value: ArkConstraintSizeOptions) {
22    super(value);
23  }
24  static identity: Symbol = Symbol('itemConstraintSize');
25  applyPeer(node: KNode, reset: boolean): void {
26    if (reset) {
27      getUINativeModule().waterFlow.resetItemConstraintSize(node);
28    } else {
29      getUINativeModule().waterFlow.setItemConstraintSize(node, this.value.minWidth,
30        this.value.maxWidth, this.value.minHeight, this.value.maxHeight);
31    }
32  }
33  checkObjectDiff(): boolean {
34    return !isBaseOrResourceEqual(this.stageValue.minWidth, this.value.minWidth) ||
35      !isBaseOrResourceEqual(this.stageValue.maxWidth, this.value.maxWidth) ||
36      !isBaseOrResourceEqual(this.stageValue.minHeight, this.value.minHeight) ||
37      !isBaseOrResourceEqual(this.stageValue.maxHeight, this.value.maxHeight);
38  }
39}
40
41class ColumnsTemplateModifier extends ModifierWithKey<string> {
42  constructor(value: string) {
43    super(value);
44  }
45  static identity: Symbol = Symbol('columnsTemplate');
46  applyPeer(node: KNode, reset: boolean): void {
47    if (reset) {
48      getUINativeModule().waterFlow.resetColumnsTemplate(node);
49    } else {
50      getUINativeModule().waterFlow.setColumnsTemplate(node, this.value);
51    }
52  }
53}
54
55class RowsTemplateModifier extends ModifierWithKey<string> {
56  constructor(value: string) {
57    super(value);
58  }
59  static identity: Symbol = Symbol('rowsTemplate');
60  applyPeer(node: KNode, reset: boolean): void {
61    if (reset) {
62      getUINativeModule().waterFlow.resetRowsTemplate(node);
63    } else {
64      getUINativeModule().waterFlow.setRowsTemplate(node, this.value);
65    }
66  }
67}
68
69class EnableScrollInteractionModifier extends ModifierWithKey<boolean> {
70  constructor(value: boolean) {
71    super(value);
72  }
73  static identity = Symbol('enableScrollInteraction');
74  applyPeer(node: KNode, reset: boolean): void {
75    if (reset) {
76      getUINativeModule().waterFlow.resetEnableScrollInteraction(node);
77    } else {
78      getUINativeModule().waterFlow.setEnableScrollInteraction(node, this.value);
79    }
80  }
81}
82
83class WaterFlowClipModifier extends ModifierWithKey<boolean | object> {
84  constructor(value: boolean | object) {
85    super(value);
86  }
87  static identity: Symbol = Symbol('waterFlowclip');
88  applyPeer(node: KNode, reset: boolean): void {
89    if (reset) {
90      getUINativeModule().common.resetClipWithEdge(node);
91    } else {
92      getUINativeModule().common.setClipWithEdge(node, this.value);
93    }
94  }
95
96  checkObjectDiff(): boolean {
97    return true;
98  }
99}
100
101class RowsGapModifier extends ModifierWithKey<number | string> {
102  constructor(value: number | string) {
103    super(value);
104  }
105  static identity: Symbol = Symbol('rowsGap');
106  applyPeer(node: KNode, reset: boolean): void {
107    if (reset) {
108      getUINativeModule().waterFlow.resetRowsGap(node);
109    } else {
110      getUINativeModule().waterFlow.setRowsGap(node, this.value);
111    }
112  }
113  checkObjectDiff(): boolean {
114    return !isBaseOrResourceEqual(this.stageValue, this.value);
115  }
116}
117
118class ColumnsGapModifier extends ModifierWithKey<number | string> {
119  constructor(value: number | string) {
120    super(value);
121  }
122  static identity: Symbol = Symbol('columnsGap');
123  applyPeer(node: KNode, reset: boolean): void {
124    if (reset) {
125      getUINativeModule().waterFlow.resetColumnsGap(node);
126    } else {
127      getUINativeModule().waterFlow.setColumnsGap(node, this.value);
128    }
129  }
130  checkObjectDiff(): boolean {
131    return !isBaseOrResourceEqual(this.stageValue, this.value);
132  }
133}
134
135class LayoutDirectionModifier extends ModifierWithKey<number> {
136  constructor(value: number) {
137    super(value);
138  }
139  static identity: Symbol = Symbol('layoutDirection');
140  applyPeer(node: KNode, reset: boolean): void {
141    if (reset) {
142      getUINativeModule().waterFlow.resetLayoutDirection(node);
143    } else {
144      getUINativeModule().waterFlow.setLayoutDirection(node, this.value);
145    }
146  }
147}
148
149class NestedScrollModifier extends ModifierWithKey<ArkNestedScrollOptions> {
150  constructor(value: ArkNestedScrollOptions) {
151    super(value);
152  }
153  static identity: Symbol = Symbol('nestedScroll');
154  applyPeer(node: KNode, reset: boolean): void {
155    if (reset) {
156      getUINativeModule().waterFlow.resetNestedScroll(node);
157    } else {
158      getUINativeModule().waterFlow.setNestedScroll(node, this.value.scrollForward, this.value.scrollBackward);
159    }
160  }
161}
162
163class FrictionModifier extends ModifierWithKey<number | Resource> {
164  constructor(value: number | Resource) {
165    super(value);
166  }
167  static identity: Symbol = Symbol('friction');
168  applyPeer(node: KNode, reset: boolean): void {
169    if (reset) {
170      getUINativeModule().waterFlow.resetFriction(node);
171    } else {
172      getUINativeModule().waterFlow.setFriction(node, this.value);
173    }
174  }
175  checkObjectDiff(): boolean {
176    return !isBaseOrResourceEqual(this.stageValue, this.value);
177  }
178}
179
180class WaterFlowScrollBarWidthModifier extends ModifierWithKey<string | number> {
181  constructor(value: string | number) {
182    super(value);
183  }
184  static identity: Symbol = Symbol('waterFlowScrollBarWidth');
185  applyPeer(node: KNode, reset: boolean): void {
186    if (reset) {
187      getUINativeModule().waterFlow.resetScrollBarWidth(node);
188    } else {
189      getUINativeModule().waterFlow.setScrollBarWidth(node, this.value);
190    }
191  }
192}
193
194class WaterFlowScrollBarModifier extends ModifierWithKey<BarState> {
195  constructor(value: BarState) {
196    super(value);
197  }
198  static identity: Symbol = Symbol('waterFlowScrollBar');
199  applyPeer(node: KNode, reset: boolean): void {
200    if (reset) {
201      getUINativeModule().waterFlow.resetScrollBar(node);
202    } else {
203      getUINativeModule().waterFlow.setScrollBar(node, this.value);
204    }
205  }
206}
207
208class WaterFlowScrollBarColorModifier extends ModifierWithKey<string | number | Color> {
209  constructor(value: string | number | Color) {
210    super(value);
211  }
212  static identity: Symbol = Symbol('waterFlowScrollBarColor');
213  applyPeer(node: KNode, reset: boolean): void {
214    if (reset) {
215      getUINativeModule().waterFlow.resetScrollBarColor(node);
216    } else {
217      getUINativeModule().waterFlow.setScrollBarColor(node, this.value);
218    }
219  }
220}
221
222class WaterFlowCachedCountModifier extends ModifierWithKey<ArkScrollableCacheOptions> {
223  constructor(value: ArkScrollableCacheOptions) {
224    super(value);
225  }
226  static identity: Symbol = Symbol('waterFlowCachedCount');
227  applyPeer(node: KNode, reset: boolean): void {
228    if (reset) {
229      getUINativeModule().waterFlow.resetCachedCount(node);
230    } else {
231      getUINativeModule().waterFlow.setCachedCount(node, this.value.count, this.value.show);
232    }
233  }
234}
235
236class WaterFlowFlingSpeedLimitModifier extends ModifierWithKey<number> {
237  constructor(value: number) {
238    super(value);
239  }
240  static identity: Symbol = Symbol('waterFlowFlingSpeedLimit');
241  applyPeer(node: KNode, reset: boolean): void {
242    if (reset) {
243      getUINativeModule().waterFlow.resetFlingSpeedLimit(node);
244    } else {
245      getUINativeModule().waterFlow.setFlingSpeedLimit(node, this.value);
246    }
247  }
248}
249
250class WaterFlowOnScrollFrameBeginModifier extends ModifierWithKey<(offset: number, state: ScrollState) => { offsetRemain: number }> {
251  constructor(value: (offset: number, state: ScrollState) => { offsetRemain: number; }) {
252    super(value);
253  }
254  static identity: Symbol = Symbol('waterFlowOnScrollFrameBegin');
255  applyPeer(node: KNode, reset: boolean): void {
256    if (reset) {
257      getUINativeModule().waterFlow.resetOnScrollFrameBegin(node);
258    } else {
259      getUINativeModule().waterFlow.setOnScrollFrameBegin(node, this.value);
260    }
261  }
262}
263
264class WaterFlowOnWillScrollModifier extends ModifierWithKey<(xOffset: number, yOffset: number,
265  scrollState: ScrollState, scrollSource: ScrollSource) => void | OffsetResult> {
266  constructor(value: (xOffset: number, yOffset: number,
267    scrollState: ScrollState, scrollSource: ScrollSource) => void | OffsetResult) {
268    super(value);
269  }
270  static identity: Symbol = Symbol('waterFlowOnWillScroll');
271  applyPeer(node: KNode, reset: boolean): void {
272    if (reset) {
273      getUINativeModule().waterFlow.resetOnWillScroll(node);
274    } else {
275      getUINativeModule().waterFlow.setOnWillScroll(node, this.value);
276    }
277  }
278}
279
280class WaterFlowOnDidScrollModifier extends ModifierWithKey<(xOffset: number, yOffset: number, scrollState: ScrollState) => void> {
281  constructor(value: (xOffset: number, yOffset: number, scrollState: ScrollState) => void) {
282    super(value);
283  }
284  static identity: Symbol = Symbol('waterFlowOnDidScroll');
285  applyPeer(node: KNode, reset: boolean): void {
286    if (reset) {
287      getUINativeModule().waterFlow.resetOnDidScroll(node);
288    } else {
289      getUINativeModule().waterFlow.setOnDidScroll(node, this.value);
290    }
291  }
292}
293
294class WaterFlowOnReachStartModifier extends ModifierWithKey<() => void> {
295  constructor(value: () => void) {
296    super(value);
297  }
298  static identity: Symbol = Symbol('waterFlowOnReachStart');
299  applyPeer(node: KNode, reset: boolean): void {
300    if (reset) {
301      getUINativeModule().waterFlow.resetOnReachStart(node);
302    } else {
303      getUINativeModule().waterFlow.setOnReachStart(node, this.value);
304    }
305  }
306}
307
308class WaterFlowOnReachEndModifier extends ModifierWithKey<() => void> {
309  constructor(value: () => void) {
310    super(value);
311  }
312  static identity: Symbol = Symbol('waterFlowOnReachEnd');
313  applyPeer(node: KNode, reset: boolean): void {
314    if (reset) {
315      getUINativeModule().waterFlow.resetOnReachEnd(node);
316    } else {
317      getUINativeModule().waterFlow.setOnReachEnd(node, this.value);
318    }
319  }
320}
321
322class WaterFlowOnScrollStartModifier extends ModifierWithKey<() => void> {
323  constructor(value: () => void) {
324    super(value);
325  }
326  static identity: Symbol = Symbol('waterFlowOnScrollStart');
327  applyPeer(node: KNode, reset: boolean): void {
328    if (reset) {
329      getUINativeModule().waterFlow.resetOnScrollStart(node);
330    } else {
331      getUINativeModule().waterFlow.setOnScrollStart(node, this.value);
332    }
333  }
334}
335
336class WaterFlowOnScrollStopModifier extends ModifierWithKey<() => void> {
337  constructor(value: () => void) {
338    super(value);
339  }
340  static identity: Symbol = Symbol('waterFlowOnScrollStop');
341  applyPeer(node: KNode, reset: boolean): void {
342    if (reset) {
343      getUINativeModule().waterFlow.resetOnScrollStop(node);
344    } else {
345      getUINativeModule().waterFlow.setOnScrollStop(node, this.value);
346    }
347  }
348}
349
350class WaterFlowOnScrollIndexModifier extends ModifierWithKey<(first: number, last: number) => void> {
351  constructor(value: (first: number, last: number) => void) {
352    super(value);
353  }
354  static identity: Symbol = Symbol('waterFlowOnScrollIndex');
355  applyPeer(node: KNode, reset: boolean): void {
356    if (reset) {
357      getUINativeModule().waterFlow.resetOnScrollIndex(node);
358    } else {
359      getUINativeModule().waterFlow.setOnScrollIndex(node, this.value);
360    }
361  }
362}
363
364class WaterFlowInitializeModifier extends ModifierWithKey<WaterFlowParam> {
365  constructor(value: WaterFlowParam) {
366    super(value);
367  }
368  static identity: Symbol = Symbol('waterFlowInitialize');
369  applyPeer(node: KNode, reset: boolean): void {
370    if (reset) {
371      getUINativeModule().waterFlow.resetWaterFlowInitialize(node);
372    } else {
373      getUINativeModule().waterFlow.setWaterFlowInitialize(node,
374        this.value?.scroller, this.value?.sections, this.value?.layoutMode, this.value?.footerContent);
375    }
376  }
377}
378
379interface WaterFlowParam {
380  scroller?: Scroller;
381  sections?: WaterFlowSections;
382  layoutMode?: WaterFlowLayoutMode;
383  footerContent?: ComponentContent;
384}
385
386class ArkWaterFlowComponent extends ArkScrollable<WaterFlowAttribute> implements WaterFlowAttribute {
387  constructor(nativePtr: KNode, classType?: ModifierType) {
388    super(nativePtr, classType);
389  }
390  columnsTemplate(value: string): this {
391    modifierWithKey(this._modifiersWithKeys, ColumnsTemplateModifier.identity, ColumnsTemplateModifier, value);
392    return this;
393  }
394  rowsTemplate(value: string): this {
395    modifierWithKey(this._modifiersWithKeys, RowsTemplateModifier.identity, RowsTemplateModifier, value);
396    return this;
397  }
398  itemConstraintSize(value: ConstraintSizeOptions): this {
399    if (!value) {
400      modifierWithKey(
401        this._modifiersWithKeys, ItemConstraintSizeModifier.identity, ItemConstraintSizeModifier, undefined);
402      return this;
403    }
404    let arkValue: ArkConstraintSizeOptions = new ArkConstraintSizeOptions();
405    arkValue.minWidth = value.minWidth;
406    arkValue.maxWidth = value.maxWidth;
407    arkValue.minHeight = value.minHeight;
408    arkValue.maxHeight = value.maxHeight;
409    modifierWithKey(this._modifiersWithKeys, ItemConstraintSizeModifier.identity, ItemConstraintSizeModifier, arkValue);
410    return this;
411  }
412  columnsGap(value: Length): this {
413    modifierWithKey(this._modifiersWithKeys, ColumnsGapModifier.identity, ColumnsGapModifier, value);
414    return this;
415  }
416  rowsGap(value: Length): this {
417    modifierWithKey(this._modifiersWithKeys, RowsGapModifier.identity, RowsGapModifier, value);
418    return this;
419  }
420  layoutDirection(value: FlexDirection): this {
421    modifierWithKey(this._modifiersWithKeys, LayoutDirectionModifier.identity, LayoutDirectionModifier, value);
422    return this;
423  }
424  nestedScroll(value: NestedScrollOptions): this {
425    let options = new ArkNestedScrollOptions();
426    if (value) {
427      if (value.scrollForward) {
428        options.scrollForward = value.scrollForward;
429      }
430      if (value.scrollBackward) {
431        options.scrollBackward = value.scrollBackward;
432      }
433      modifierWithKey(this._modifiersWithKeys, NestedScrollModifier.identity, NestedScrollModifier, options);
434    }
435    return this;
436  }
437  enableScrollInteraction(value: boolean): this {
438    modifierWithKey(
439      this._modifiersWithKeys, EnableScrollInteractionModifier.identity, EnableScrollInteractionModifier, value);
440    return this;
441  }
442  friction(value: number | Resource): this {
443    modifierWithKey(this._modifiersWithKeys, FrictionModifier.identity, FrictionModifier, value);
444    return this;
445  }
446  cachedCount(count: number, show?: boolean): WaterFlowAttribute {
447    let opt = new ArkScrollableCacheOptions(count, show ? show : false);
448    modifierWithKey(this._modifiersWithKeys, WaterFlowCachedCountModifier.identity, WaterFlowCachedCountModifier, opt);
449    return this;
450  }
451  onReachStart(event: () => void): this {
452    modifierWithKey(this._modifiersWithKeys, WaterFlowOnReachStartModifier.identity, WaterFlowOnReachStartModifier, event);
453    return this;
454  }
455  onReachEnd(event: () => void): this {
456    modifierWithKey(this._modifiersWithKeys, WaterFlowOnReachEndModifier.identity, WaterFlowOnReachEndModifier, event);
457    return this;
458  }
459  onScrollFrameBegin(event: (offset: number, state: ScrollState) => { offsetRemain: number; }): this {
460    modifierWithKey(this._modifiersWithKeys, WaterFlowOnScrollFrameBeginModifier.identity, WaterFlowOnScrollFrameBeginModifier, event);
461    return this;
462  }
463  clip(value: boolean | CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute): this {
464    modifierWithKey(this._modifiersWithKeys, WaterFlowClipModifier.identity, WaterFlowClipModifier, value);
465    return this;
466  }
467  scrollBarWidth(value: string | number): this {
468    modifierWithKey(this._modifiersWithKeys, WaterFlowScrollBarWidthModifier.identity, WaterFlowScrollBarWidthModifier, value);
469    return this;
470  }
471  scrollBarColor(value: string | number | Color): this {
472    modifierWithKey(this._modifiersWithKeys, WaterFlowScrollBarColorModifier.identity, WaterFlowScrollBarColorModifier, value);
473    return this;
474  }
475  scrollBar(value: BarState): this {
476    modifierWithKey(this._modifiersWithKeys, WaterFlowScrollBarModifier.identity, WaterFlowScrollBarModifier, value);
477    return this;
478  }
479  flingSpeedLimit(value: number): this {
480    modifierWithKey(this._modifiersWithKeys, WaterFlowFlingSpeedLimitModifier.identity, WaterFlowFlingSpeedLimitModifier, value);
481    return this;
482  }
483  onWillScroll(callback: (xOffset: number, yOffset: number,
484    scrollState: ScrollState, scrollSource: ScrollSource) => void | OffsetResult): this {
485    modifierWithKey(this._modifiersWithKeys, WaterFlowOnWillScrollModifier.identity, WaterFlowOnWillScrollModifier, callback);
486    return this;
487  }
488  onDidScroll(callback: (xOffset: number, yOffset: number, scrollState: ScrollState) => void): this {
489    modifierWithKey(this._modifiersWithKeys, WaterFlowOnDidScrollModifier.identity, WaterFlowOnDidScrollModifier, callback);
490    return this;
491  }
492  onScrollStart(event: () => void): this {
493    modifierWithKey(this._modifiersWithKeys, WaterFlowOnScrollStartModifier.identity, WaterFlowOnScrollStartModifier, event);
494    return this;
495  }
496  onScrollStop(event: () => void): this {
497    modifierWithKey(this._modifiersWithKeys, WaterFlowOnScrollStopModifier.identity, WaterFlowOnScrollStopModifier, event);
498    return this;
499  }
500  onScrollIndex(event: (first: number, last: number) => void): this {
501    modifierWithKey(this._modifiersWithKeys, WaterFlowOnScrollIndexModifier.identity, WaterFlowOnScrollIndexModifier, event);
502    return this;
503  }
504  initialize(value: Object[]): this {
505    if (value[0] !== undefined) {
506      modifierWithKey(this._modifiersWithKeys, WaterFlowInitializeModifier.identity,
507        WaterFlowInitializeModifier, (value[0] as WaterFlowParam));
508    } else {
509      modifierWithKey(this._modifiersWithKeys, WaterFlowInitializeModifier.identity,
510        WaterFlowInitializeModifier, undefined);
511    }
512    return this;
513  }
514  allowChildTypes(): string[] {
515    return ['FlowItem'];
516  }
517}
518
519// @ts-ignore
520globalThis.WaterFlow.attributeModifier = function (modifier: ArkComponent): void {
521  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
522    return new ArkWaterFlowComponent(nativePtr);
523  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
524    return new modifierJS.WaterFlowModifier(nativePtr, classType);
525  });
526};
527