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