1/* 2 * Copyright (c) 2024 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 18class ClipContentModifier extends ModifierWithKey<ContentClipMode | RectShape> { 19 constructor(value: ContentClipMode | RectShape) { 20 super(value); 21 } 22 static identity: Symbol = Symbol('clipContent'); 23 applyPeer(node: KNode, reset: boolean): void { 24 if (reset) { 25 getUINativeModule().scrollable.resetContentClip(node); 26 } else { 27 getUINativeModule().scrollable.setContentClip(node, this.value); 28 } 29 } 30} 31 32class EdgeEffectModifier extends ModifierWithKey<ArkEdgeEffect> { 33 constructor(value: ArkEdgeEffect) { 34 super(value); 35 } 36 static identity: Symbol = Symbol('edgeEffect'); 37 applyPeer(node: KNode, reset: boolean): void { 38 if (reset) { 39 getUINativeModule().scrollable.resetEdgeEffect(node); 40 } else { 41 getUINativeModule().scrollable.setEdgeEffect(node, this.value?.value, this.value.options?.alwaysEnabled, 42 this.value.options?.effectEdge); 43 } 44 } 45 46 checkObjectDiff(): boolean { 47 return !((this.stageValue.value === this.value.value) && 48 (this.stageValue.options === this.value.options)); 49 } 50} 51 52class ScrollableFadingEdgeModifier extends ModifierWithKey<ArkFadingEdge> { 53 constructor(value: ArkFadingEdge) { 54 super(value); 55 } 56 static identity: Symbol = Symbol('scrollableFadingEdge'); 57 applyPeer(node: KNode, reset: boolean): void { 58 if (reset) { 59 getUINativeModule().scrollable.resetFadingEdge(node); 60 } else { 61 getUINativeModule().scrollable.setFadingEdge(node, this.value.value!, this.value.options?.fadingEdgeLength); 62 } 63 } 64 checkObjectDiff(): boolean { 65 return !((this.stageValue.value === this.value.value) && 66 (this.stageValue.options === this.value.options)); 67 } 68} 69 70class OnReachStartModifier extends ModifierWithKey<() => void> { 71 constructor(value: () => void) { 72 super(value); 73 } 74 static identity: Symbol = Symbol('onReachStart'); 75 applyPeer(node: KNode, reset: boolean): void { 76 if (reset) { 77 getUINativeModule().scrollable.resetOnReachStart(node); 78 } else { 79 getUINativeModule().scrollable.setOnReachStart(node, this.value); 80 } 81 } 82} 83 84class BackToTopModifier extends ModifierWithKey<boolean> { 85 constructor(value: boolean) { 86 super(value); 87 } 88 static identity: Symbol = Symbol('backToTop'); 89 applyPeer(node: KNode, reset: boolean): void { 90 if (reset) { 91 getUINativeModule().scrollable.resetBackToTop(node); 92 } else { 93 getUINativeModule().scrollable.setBackToTop(node, this.value); 94 } 95 } 96} 97 98class ScrollBarMarginModifier extends ModifierWithKey<ScrollBarMargin> { 99 constructor(value: ScrollBarMargin) { 100 super(value); 101 } 102 static identity: Symbol = Symbol('scrollBarMargin'); 103 applyPeer(node: KNode, reset: boolean): void { 104 if (reset) { 105 getUINativeModule().scrollable.resetScrollBarMargin(node); 106 } else { 107 getUINativeModule().scrollable.setScrollBarMargin(node, this.value.start, this.value.end); 108 } 109 } 110} 111 112class onWillStopDraggingModifier extends ModifierWithKey<(velocity: number) => void> { 113 constructor(value: (velocity: number) => void) { 114 super(value); 115 } 116 static identity: Symbol = Symbol('onWillStopDragging'); 117 applyPeer(node: KNode, reset: boolean): void { 118 if (reset) { 119 getUINativeModule().scrollable.resetOnWillStopDragging(node); 120 } else { 121 getUINativeModule().scrollable.setOnWillStopDragging(node, this.value); 122 } 123 } 124} 125 126class OnReachEndModifier extends ModifierWithKey<() => void> { 127 constructor(value: () => void) { 128 super(value); 129 } 130 static identity: Symbol = Symbol('onReachEnd'); 131 applyPeer(node: KNode, reset: boolean): void { 132 if (reset) { 133 getUINativeModule().scrollable.resetOnReachEnd(node); 134 } else { 135 getUINativeModule().scrollable.setOnReachEnd(node, this.value); 136 } 137 } 138} 139 140/** 141 * base class of Grid, Scroll, List, and WaterFlow. 142 */ 143export class ArkScrollable<T> extends ArkComponent implements ScrollableCommonMethod<T> { 144 constructor(nativePtr: KNode, classType?: ModifierType) { 145 super(nativePtr, classType); 146 } 147 clipContent(clip: ContentClipMode | RectShape): T { 148 modifierWithKey(this._modifiersWithKeys, ClipContentModifier.identity, ClipContentModifier, clip); 149 return this; 150 } 151 edgeEffect(value: EdgeEffect, options?: EdgeEffectOptions | undefined): T { 152 let effect: ArkEdgeEffect = new ArkEdgeEffect(); 153 effect.value = value; 154 effect.options = options; 155 modifierWithKey(this._modifiersWithKeys, EdgeEffectModifier.identity, EdgeEffectModifier, effect); 156 return this; 157 } 158 fadingEdge(value: boolean, options?: FadingEdgeOptions | undefined): T { 159 let fadingEdge: ArkFadingEdge = new ArkFadingEdge(); 160 fadingEdge.value = value; 161 fadingEdge.options = options; 162 modifierWithKey(this._modifiersWithKeys, ScrollableFadingEdgeModifier.identity, ScrollableFadingEdgeModifier, fadingEdge); 163 return this; 164 } 165 onReachStart(event: () => void): this { 166 modifierWithKey(this._modifiersWithKeys, OnReachStartModifier.identity, OnReachStartModifier, event); 167 return this; 168 } 169 170 onReachEnd(event: () => void): this { 171 modifierWithKey(this._modifiersWithKeys, OnReachEndModifier.identity, OnReachEndModifier, event); 172 return this; 173 } 174 backToTop(value: boolean): this { 175 modifierWithKey(this._modifiersWithKeys, BackToTopModifier.identity, BackToTopModifier, value); 176 return this; 177 } 178 scrollBarMargin(margin: ScrollBarMargin): T { 179 modifierWithKey(this._modifiersWithKeys, ScrollBarMarginModifier.identity, ScrollBarMarginModifier, margin); 180 return this; 181 } 182 onWillStopDragging(callback: (velocity: number) => void) : this { 183 modifierWithKey(this._modifiersWithKeys, OnWillStopDraggingModifier.identity, OnWillStopDraggingModifier, callback); 184 return this; 185 } 186}