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 static identity: Symbol = Symbol('itemConstraintSize'); 22 applyPeer(node: KNode, reset: boolean): void { 23 if (reset) { 24 getUINativeModule().waterFlow.resetItemConstraintSize(node); 25 } else { 26 getUINativeModule().waterFlow.setItemConstraintSize(node, this.value.minWidth, 27 this.value.maxWidth, this.value.minHeight, this.value.maxHeight); 28 } 29 } 30 checkObjectDiff(): boolean { 31 return !isBaseOrResourceEqual(this.stageValue.minWidth, this.value.minWidth) || 32 !isBaseOrResourceEqual(this.stageValue.maxWidth, this.value.maxWidth) || 33 !isBaseOrResourceEqual(this.stageValue.minHeight, this.value.minHeight) || 34 !isBaseOrResourceEqual(this.stageValue.maxHeight, this.value.maxHeight); 35 } 36} 37 38class ColumnsTemplateModifier extends ModifierWithKey<string> { 39 constructor(value: string) { 40 super(value); 41 } 42 static identity: Symbol = Symbol('columnsTemplate'); 43 applyPeer(node: KNode, reset: boolean): void { 44 if (reset) { 45 getUINativeModule().waterFlow.resetColumnsTemplate(node); 46 } else { 47 getUINativeModule().waterFlow.setColumnsTemplate(node, this.value); 48 } 49 } 50} 51 52class RowsTemplateModifier extends ModifierWithKey<string> { 53 constructor(value: string) { 54 super(value); 55 } 56 static identity: Symbol = Symbol('rowsTemplate'); 57 applyPeer(node: KNode, reset: boolean): void { 58 if (reset) { 59 getUINativeModule().waterFlow.resetRowsTemplate(node); 60 } else { 61 getUINativeModule().waterFlow.setRowsTemplate(node, this.value); 62 } 63 } 64} 65 66class EnableScrollInteractionModifier extends ModifierWithKey<boolean> { 67 constructor(value: boolean) { 68 super(value); 69 } 70 static identity = Symbol('enableScrollInteraction'); 71 applyPeer(node: KNode, reset: boolean): void { 72 if (reset) { 73 getUINativeModule().waterFlow.resetEnableScrollInteraction(node); 74 } else { 75 getUINativeModule().waterFlow.setEnableScrollInteraction(node, this.value); 76 } 77 } 78} 79 80class WaterFlowClipModifier extends ModifierWithKey<boolean | object> { 81 constructor(value: boolean | object) { 82 super(value); 83 } 84 static identity: Symbol = Symbol('waterFlowclip'); 85 applyPeer(node: KNode, reset: boolean): void { 86 if (reset) { 87 getUINativeModule().common.resetClipWithEdge(node); 88 } else { 89 getUINativeModule().common.setClipWithEdge(node, this.value); 90 } 91 } 92 93 checkObjectDiff(): boolean { 94 return true; 95 } 96} 97 98class RowsGapModifier extends ModifierWithKey<number | string> { 99 static identity: Symbol = Symbol('rowsGap'); 100 applyPeer(node: KNode, reset: boolean): void { 101 if (reset) { 102 getUINativeModule().waterFlow.resetRowsGap(node); 103 } else { 104 getUINativeModule().waterFlow.setRowsGap(node, this.value); 105 } 106 } 107 checkObjectDiff(): boolean { 108 return !isBaseOrResourceEqual(this.stageValue, this.value); 109 } 110} 111 112class ColumnsGapModifier extends ModifierWithKey<number | string> { 113 static identity: Symbol = Symbol('columnsGap'); 114 applyPeer(node: KNode, reset: boolean): void { 115 if (reset) { 116 getUINativeModule().waterFlow.resetColumnsGap(node); 117 } else { 118 getUINativeModule().waterFlow.setColumnsGap(node, this.value); 119 } 120 } 121 checkObjectDiff(): boolean { 122 return !isBaseOrResourceEqual(this.stageValue, this.value); 123 } 124} 125 126class LayoutDirectionModifier extends ModifierWithKey<number> { 127 constructor(value: number) { 128 super(value); 129 } 130 static identity: Symbol = Symbol('layoutDirection'); 131 applyPeer(node: KNode, reset: boolean): void { 132 if (reset) { 133 getUINativeModule().waterFlow.resetLayoutDirection(node); 134 } else { 135 getUINativeModule().waterFlow.setLayoutDirection(node, this.value); 136 } 137 } 138} 139 140class NestedScrollModifier extends ModifierWithKey<ArkNestedScrollOptions> { 141 constructor(value: ArkNestedScrollOptions) { 142 super(value); 143 } 144 static identity: Symbol = Symbol('nestedScroll'); 145 applyPeer(node: KNode, reset: boolean): void { 146 if (reset) { 147 getUINativeModule().waterFlow.resetNestedScroll(node); 148 } else { 149 getUINativeModule().waterFlow.setNestedScroll(node, this.value.scrollForward, this.value.scrollBackward); 150 } 151 } 152} 153 154class FrictionModifier extends ModifierWithKey<number | Resource> { 155 static identity: Symbol = Symbol('friction'); 156 applyPeer(node: KNode, reset: boolean): void { 157 if (reset) { 158 getUINativeModule().waterFlow.resetFriction(node); 159 } else { 160 getUINativeModule().waterFlow.setFriction(node, this.value); 161 } 162 } 163 checkObjectDiff(): boolean { 164 return !isBaseOrResourceEqual(this.stageValue, this.value); 165 } 166} 167 168class WaterFlowEdgeEffectModifier extends ModifierWithKey<ArkWaterFlowEdgeEffect> { 169 constructor(value: ArkWaterFlowEdgeEffect) { 170 super(value); 171 } 172 static identity: Symbol = Symbol('waterFlowEdgeEffect'); 173 applyPeer(node: KNode, reset: boolean): void { 174 if (reset) { 175 getUINativeModule().waterFlow.resetEdgeEffect(node); 176 } else { 177 getUINativeModule().waterFlow.setEdgeEffect(node, this.value?.value, this.value.options?.alwaysEnabled); 178 } 179 } 180 181 checkObjectDiff(): boolean { 182 return !((this.stageValue.value === this.value.value) && 183 (this.stageValue.options === this.value.options)); 184 } 185} 186 187class WaterFlowFadingEdgeModifier extends ModifierWithKey<ArkFadingEdge> { 188 constructor(value: ArkFadingEdge) { 189 super(value); 190 } 191 static identity: Symbol = Symbol('waterFlowFadingEdge'); 192 applyPeer(node: KNode, reset: boolean): void { 193 if (reset) { 194 getUINativeModule().waterFlow.resetFadingEdge(node); 195 } else { 196 getUINativeModule().waterFlow.setFadingEdge(node, this.value.value!, this.value.options?.fadingEdgeLength); 197 } 198 } 199 checkObjectDiff(): boolean { 200 return !((this.stageValue.value === this.value.value) && 201 (this.stageValue.options === this.value.options)); 202 } 203} 204 205 206class WaterFlowScrollBarWidthModifier extends ModifierWithKey<string | number> { 207 constructor(value: string | number) { 208 super(value); 209 } 210 static identity: Symbol = Symbol('waterFlowScrollBarWidth'); 211 applyPeer(node: KNode, reset: boolean): void { 212 if (reset) { 213 getUINativeModule().waterFlow.resetScrollBarWidth(node); 214 } else { 215 getUINativeModule().waterFlow.setScrollBarWidth(node, this.value); 216 } 217 } 218} 219 220class WaterFlowScrollBarModifier extends ModifierWithKey<BarState> { 221 constructor(value: BarState) { 222 super(value); 223 } 224 static identity: Symbol = Symbol('waterFlowScrollBar'); 225 applyPeer(node: KNode, reset: boolean): void { 226 if (reset) { 227 getUINativeModule().waterFlow.resetScrollBar(node); 228 } else { 229 getUINativeModule().waterFlow.setScrollBar(node, this.value); 230 } 231 } 232} 233 234class WaterFlowScrollBarColorModifier extends ModifierWithKey<string | number | Color> { 235 constructor(value: string | number | Color) { 236 super(value); 237 } 238 static identity: Symbol = Symbol('waterFlowScrollBarColor'); 239 applyPeer(node: KNode, reset: boolean): void { 240 if (reset) { 241 getUINativeModule().waterFlow.resetScrollBarColor(node); 242 } else { 243 getUINativeModule().waterFlow.setScrollBarColor(node, this.value); 244 } 245 } 246} 247 248class WaterFlowCachedCountModifier extends ModifierWithKey<ArkScrollableCacheOptions> { 249 constructor(value: ArkScrollableCacheOptions) { 250 super(value); 251 } 252 static identity: Symbol = Symbol('waterFlowCachedCount'); 253 applyPeer(node: KNode, reset: boolean): void { 254 if (reset) { 255 getUINativeModule().waterFlow.resetCachedCount(node); 256 } else { 257 getUINativeModule().waterFlow.setCachedCount(node, this.value.count, this.value.show); 258 } 259 } 260} 261 262class WaterFlowFlingSpeedLimitModifier extends ModifierWithKey<number> { 263 constructor(value: number) { 264 super(value); 265 } 266 static identity: Symbol = Symbol('waterFlowFlingSpeedLimit'); 267 applyPeer(node: KNode, reset: boolean): void { 268 if (reset) { 269 getUINativeModule().waterFlow.resetFlingSpeedLimit(node); 270 } else { 271 getUINativeModule().waterFlow.setFlingSpeedLimit(node, this.value); 272 } 273 } 274} 275 276class WaterFlowInitializeModifier extends ModifierWithKey<WaterFlowParam> { 277 constructor(value: WaterFlowParam) { 278 super(value); 279 } 280 static identity: Symbol = Symbol('waterFlowInitialize'); 281 applyPeer(node: KNode, reset: boolean): void { 282 if (reset) { 283 getUINativeModule().waterFlow.resetWaterFlowInitialize(node); 284 } else { 285 getUINativeModule().waterFlow.setWaterFlowInitialize(node, 286 this.value?.scroller, this.value?.sections, this.value?.layoutMode); 287 } 288 } 289} 290 291interface WaterFlowParam { 292 scroller?: Scroller; 293 sections?: WaterFlowSections; 294 layoutMode?: WaterFlowLayoutMode; 295} 296 297class ArkWaterFlowComponent extends ArkScrollable<WaterFlowAttribute> implements WaterFlowAttribute { 298 constructor(nativePtr: KNode, classType?: ModifierType) { 299 super(nativePtr, classType); 300 } 301 columnsTemplate(value: string): this { 302 modifierWithKey(this._modifiersWithKeys, ColumnsTemplateModifier.identity, ColumnsTemplateModifier, value); 303 return this; 304 } 305 rowsTemplate(value: string): this { 306 modifierWithKey(this._modifiersWithKeys, RowsTemplateModifier.identity, RowsTemplateModifier, value); 307 return this; 308 } 309 itemConstraintSize(value: ConstraintSizeOptions): this { 310 if (!value) { 311 modifierWithKey( 312 this._modifiersWithKeys, ItemConstraintSizeModifier.identity, ItemConstraintSizeModifier, undefined); 313 return this; 314 } 315 let arkValue: ArkConstraintSizeOptions = new ArkConstraintSizeOptions(); 316 arkValue.minWidth = value.minWidth; 317 arkValue.maxWidth = value.maxWidth; 318 arkValue.minHeight = value.minHeight; 319 arkValue.maxHeight = value.maxHeight; 320 modifierWithKey(this._modifiersWithKeys, ItemConstraintSizeModifier.identity, ItemConstraintSizeModifier, arkValue); 321 return this; 322 } 323 columnsGap(value: Length): this { 324 modifierWithKey(this._modifiersWithKeys, ColumnsGapModifier.identity, ColumnsGapModifier, value); 325 return this; 326 } 327 rowsGap(value: Length): this { 328 modifierWithKey(this._modifiersWithKeys, RowsGapModifier.identity, RowsGapModifier, value); 329 return this; 330 } 331 layoutDirection(value: FlexDirection): this { 332 modifierWithKey(this._modifiersWithKeys, LayoutDirectionModifier.identity, LayoutDirectionModifier, value); 333 return this; 334 } 335 nestedScroll(value: NestedScrollOptions): this { 336 let options = new ArkNestedScrollOptions(); 337 if (value) { 338 if (value.scrollForward) { 339 options.scrollForward = value.scrollForward; 340 } 341 if (value.scrollBackward) { 342 options.scrollBackward = value.scrollBackward; 343 } 344 modifierWithKey(this._modifiersWithKeys, NestedScrollModifier.identity, NestedScrollModifier, options); 345 } 346 return this; 347 } 348 enableScrollInteraction(value: boolean): this { 349 modifierWithKey( 350 this._modifiersWithKeys, EnableScrollInteractionModifier.identity, EnableScrollInteractionModifier, value); 351 return this; 352 } 353 friction(value: number | Resource): this { 354 modifierWithKey(this._modifiersWithKeys, FrictionModifier.identity, FrictionModifier, value); 355 return this; 356 } 357 cachedCount(count: number, show?: boolean): WaterFlowAttribute { 358 let opt = new ArkScrollableCacheOptions(count, show ? show : false); 359 modifierWithKey(this._modifiersWithKeys, WaterFlowCachedCountModifier.identity, WaterFlowCachedCountModifier, opt); 360 return this; 361 } 362 onReachStart(event: () => void): this { 363 throw new Error('Method not implemented.'); 364 } 365 onReachEnd(event: () => void): this { 366 throw new Error('Method not implemented.'); 367 } 368 onScrollFrameBegin(event: (offset: number, state: ScrollState) => { offsetRemain: number; }): this { 369 throw new Error('Method not implemented.'); 370 } 371 clip(value: boolean | CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute): this { 372 modifierWithKey(this._modifiersWithKeys, WaterFlowClipModifier.identity, WaterFlowClipModifier, value); 373 return this; 374 } 375 edgeEffect(value: EdgeEffect, options?: EdgeEffectOptions | undefined): this { 376 let effect: ArkWaterFlowEdgeEffect = new ArkWaterFlowEdgeEffect(); 377 effect.value = value; 378 effect.options = options; 379 modifierWithKey(this._modifiersWithKeys, WaterFlowEdgeEffectModifier.identity, WaterFlowEdgeEffectModifier, effect); 380 return this; 381 } 382 fadingEdge(value: boolean, options?: FadingEdgeOptions | undefined): this { 383 let fadingEdge: ArkFadingEdge = new ArkFadingEdge(); 384 fadingEdge.value = value; 385 fadingEdge.options = options; 386 modifierWithKey(this._modifiersWithKeys, WaterFlowFadingEdgeModifier.identity, WaterFlowFadingEdgeModifier, fadingEdge); 387 return this; 388 } 389 scrollBarWidth(value: string | number): this { 390 modifierWithKey(this._modifiersWithKeys, WaterFlowScrollBarWidthModifier.identity, WaterFlowScrollBarWidthModifier, value); 391 return this; 392 } 393 scrollBarColor(value: string | number | Color): this { 394 modifierWithKey(this._modifiersWithKeys, WaterFlowScrollBarColorModifier.identity, WaterFlowScrollBarColorModifier, value); 395 return this; 396 } 397 scrollBar(value: BarState): this { 398 modifierWithKey(this._modifiersWithKeys, WaterFlowScrollBarModifier.identity, WaterFlowScrollBarModifier, value); 399 return this; 400 } 401 flingSpeedLimit(value: number): this { 402 modifierWithKey(this._modifiersWithKeys, WaterFlowFlingSpeedLimitModifier.identity, WaterFlowFlingSpeedLimitModifier, value); 403 return this; 404 } 405 initialize(value: Object[]): this { 406 if (value[0] !== undefined) { 407 modifierWithKey(this._modifiersWithKeys, WaterFlowInitializeModifier.identity, 408 WaterFlowInitializeModifier, (value[0] as WaterFlowParam)); 409 } else { 410 modifierWithKey(this._modifiersWithKeys, WaterFlowInitializeModifier.identity, 411 WaterFlowInitializeModifier, undefined); 412 } 413 return this; 414 } 415 allowChildTypes(): string[] { 416 return ['FlowItem']; 417 } 418} 419 420// @ts-ignore 421globalThis.WaterFlow.attributeModifier = function (modifier: ArkComponent): void { 422 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 423 return new ArkWaterFlowComponent(nativePtr); 424 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 425 return new modifierJS.WaterFlowModifier(nativePtr, classType); 426 }); 427}; 428