Skip to content

Commit

Permalink
fix(core): ScrollView event wiring sequencing improvement (#10178)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWalker committed Jan 19, 2023
1 parent 7993de5 commit 75821ea
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 74 deletions.
1 change: 1 addition & 0 deletions apps/toolbox/src/main-page.xml
Expand Up @@ -16,6 +16,7 @@
<Button text="labels" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="list-page" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="root-layout" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="scroll-view" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="switch" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="touch-animations" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="vector-image" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
Expand Down
26 changes: 26 additions & 0 deletions apps/toolbox/src/pages/scroll-view.ts
@@ -0,0 +1,26 @@
import { Observable, EventData, Page, ScrollEventData } from '@nativescript/core';

let page: Page;

export function navigatingTo(args: EventData) {
page = <Page>args.object;
page.bindingContext = new ScrollViewModel();
}

export class ScrollViewModel extends Observable {
longText = `The standard Lorem Ipsum passage, used since the 1500s
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
1914 translation by H. Rackham
"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
Section 1.10.33 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."`;

scroll(args: ScrollEventData) {
console.log('scroll:', args.scrollY);
}
}
14 changes: 14 additions & 0 deletions apps/toolbox/src/pages/scroll-view.xml
@@ -0,0 +1,14 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
<Page.actionBar>
<ActionBar title="ScrollView" icon="" class="action-bar">
</ActionBar>
</Page.actionBar>

<ScrollView scroll="{{ scroll }}">
<StackLayout>

<Label text="{{longText}}" textWrap="true" />

</StackLayout>
</ScrollView>
</Page>
69 changes: 19 additions & 50 deletions packages/core/ui/scroll-view/index.android.ts
Expand Up @@ -118,65 +118,29 @@ export class ScrollView extends ScrollViewBase {
this.nativeViewProtected.setId(this._androidViewId);
}

protected addNativeListener() {
if (!this.nativeViewProtected) {
return;
public _onOrientationChanged() {
if (this.nativeViewProtected) {
const parent = this.parent;
if (parent) {
parent._removeView(this);
parent._addView(this);
}
}
const that = new WeakRef(this);
if (this.orientation === 'vertical') {
this.scrollChangeHandler = new androidx.core.widget.NestedScrollView.OnScrollChangeListener({
onScrollChange(view, scrollX, scrollY) {
const owner: ScrollView = that?.get();
if (owner) {
owner.notify({
object: owner,
eventName: ScrollView.scrollEvent,
scrollX: layout.toDeviceIndependentPixels(scrollX),
scrollY: layout.toDeviceIndependentPixels(scrollY),
});
}
},
});
this.nativeViewProtected.setOnScrollChangeListener(this.scrollChangeHandler);
} else {
}

protected attachNative() {
if (!this.handler) {
const that = new WeakRef(this);
this.handler = new android.view.ViewTreeObserver.OnScrollChangedListener({
onScrollChanged: function () {
const owner: ScrollView = that?.get();
const owner: ScrollView = that.get();
if (owner) {
owner._onScrollChanged();
}
},
});
this.nativeViewProtected.getViewTreeObserver().addOnScrollChangedListener(this.handler);
}
}

protected removeNativeListener() {
if (!this.nativeViewProtected) {
return;
}
if (this.handler) {
this.nativeViewProtected?.getViewTreeObserver().removeOnScrollChangedListener(this.handler);
this.handler = null;
}
if (this.scrollChangeHandler) {
this.nativeView?.setOnScrollChangeListener(null);
this.scrollChangeHandler = null;
}
}

disposeNativeView() {
this.removeNativeListener();
super.disposeNativeView();
}

public _onOrientationChanged() {
if (this.nativeViewProtected) {
const parent = this.parent;
if (parent) {
parent._removeView(this);
parent._addView(this);
}
this.nativeViewProtected.getViewTreeObserver().addOnScrollChangedListener(this.handler);
}
}

Expand All @@ -201,6 +165,11 @@ export class ScrollView extends ScrollViewBase {
}
}
}

protected dettachNative() {
this.nativeViewProtected.getViewTreeObserver().removeOnScrollChangedListener(this.handler);
this.handler = null;
}
}

ScrollView.prototype.recycleNativeView = 'never';
24 changes: 11 additions & 13 deletions packages/core/ui/scroll-view/index.ios.ts
Expand Up @@ -50,32 +50,30 @@ export class ScrollView extends ScrollViewBase {
}

disposeNativeView() {
this.removeNativeListener();
this.dettachNative();
this._delegate = null;
super.disposeNativeView();
}

protected addNativeListener() {
_setNativeClipToBounds() {
if (!this.nativeViewProtected) {
return;
}
this._delegate = UIScrollViewDelegateImpl.initWithOwner(new WeakRef(this));
this.nativeViewProtected.delegate = this._delegate;
// Always set clipsToBounds for scroll-view
this.nativeViewProtected.clipsToBounds = true;
}

protected removeNativeListener() {
if (!this.nativeViewProtected) {
return;
protected attachNative() {
if (!this._delegate) {
this._delegate = UIScrollViewDelegateImpl.initWithOwner(new WeakRef(this));
this.nativeViewProtected.delegate = this._delegate;
}
this.nativeViewProtected.delegate = null;
}

_setNativeClipToBounds() {
if (!this.nativeViewProtected) {
return;
protected dettachNative() {
if (this.nativeViewProtected) {
this.nativeViewProtected.delegate = null;
}
// Always set clipsToBounds for scroll-view
this.nativeViewProtected.clipsToBounds = true;
}

protected updateScrollBarVisibility(value) {
Expand Down
46 changes: 35 additions & 11 deletions packages/core/ui/scroll-view/scroll-view-common.ts
Expand Up @@ -9,7 +9,7 @@ import { CoreTypes } from '../../core-types';

@CSSType('ScrollView')
export abstract class ScrollViewBase extends ContentView implements ScrollViewDefinition {
private _addedScrollEvent = false;
private _scrollChangeCount = 0;
public static scrollEvent = 'scroll';

public orientation: CoreTypes.OrientationType;
Expand All @@ -19,27 +19,51 @@ export abstract class ScrollViewBase extends ContentView implements ScrollViewDe
public addEventListener(arg: string, callback: any, thisArg?: any) {
super.addEventListener(arg, callback, thisArg);

if (arg === ScrollViewBase.scrollEvent && !this._addedScrollEvent) {
this._addedScrollEvent = true;
this.addNativeListener();
if (arg === ScrollViewBase.scrollEvent) {
this._scrollChangeCount++;
this.attach();
}
}

public removeEventListener(arg: string, callback: any, thisArg?: any) {
super.removeEventListener(arg, callback, thisArg);

if (arg === ScrollViewBase.scrollEvent && this._addedScrollEvent) {
this._addedScrollEvent = false;
this.removeNativeListener();
if (arg === ScrollViewBase.scrollEvent) {
this._scrollChangeCount--;
this.dettach();
}
}

protected addNativeListener() {
// implemented per platform
@profile
public onLoaded() {
super.onLoaded();

this.attach();
}

public disposeNativeView() {
this.dettach();
super.disposeNativeView();
}

private attach() {
if (this._scrollChangeCount > 0 && this.isLoaded) {
this.attachNative();
}
}

private dettach() {
if (this._scrollChangeCount === 0 && this.isLoaded) {
this.dettachNative();
}
}

protected attachNative() {
//
}

protected removeNativeListener() {
// implemented per platform
protected dettachNative() {
//
}

get horizontalOffset(): number {
Expand Down

0 comments on commit 75821ea

Please sign in to comment.