mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 02:54:11 +08:00

* chore: move tns-core-modules to nativescript-core * chore: preparing compat generate script * chore: add missing definitions * chore: no need for http-request to be private * chore: packages chore * test: generate tests for tns-core-modules * chore: add anroid module for consistency * chore: add .npmignore * chore: added privateModulesWhitelist * chore(webpack): added bundle-entry-points * chore: scripts * chore: tests changed to use @ns/core * test: add scoped-packages test project * test: fix types * test: update test project * chore: build scripts * chore: update build script * chore: npm scripts cleanup * chore: make the compat pgk work with old wp config * test: generate diff friendly tests * chore: create barrel exports * chore: move files after rebase * chore: typedoc config * chore: compat mode * chore: review of barrels * chore: remove tns-core-modules import after rebase * chore: dev workflow setup * chore: update developer-workflow * docs: experiment with API extractor * chore: api-extractor and barrel exports * chore: api-extractor configs * chore: generate d.ts rollup with api-extractor * refactor: move methods inside Frame * chore: fic tests to use Frame static methods * refactor: create Builder class * refactor: use Builder class in tests * refactor: include Style in ui barrel * chore: separate compat build script * chore: fix tslint errors * chore: update NATIVESCRIPT_CORE_ARGS * chore: fix compat pack * chore: fix ui-test-app build with linked modules * chore: Application, ApplicationSettings, Connectivity and Http * chore: export Trace, Profiling and Utils * refactor: Static create methods for ImageSource * chore: fix deprecated usages of ImageSource * chore: move Span and FormattedString to ui * chore: add events-args and ImageSource to index files * chore: check for CLI >= 6.2 when building for IOS * chore: update travis build * chore: copy Pod file to compat package * chore: update error msg ui-tests-app * refactor: Apply suggestions from code review Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com> * chore: typings and refs * chore: add missing d.ts files for public API * chore: adress code review FB * chore: update api-report * chore: dev-workflow for other apps * chore: api update * chore: update api-report
168 lines
5.7 KiB
TypeScript
168 lines
5.7 KiB
TypeScript
import { ScrollEventData } from ".";
|
|
import {
|
|
ScrollViewBase, layout, scrollBarIndicatorVisibleProperty,
|
|
isUserInteractionEnabledProperty, isScrollEnabledProperty
|
|
} from "./scroll-view-common";
|
|
|
|
export * from "./scroll-view-common";
|
|
|
|
export class ScrollView extends ScrollViewBase {
|
|
nativeViewProtected: org.nativescript.widgets.VerticalScrollView | org.nativescript.widgets.HorizontalScrollView;
|
|
private _androidViewId: number = -1;
|
|
private handler: android.view.ViewTreeObserver.OnScrollChangedListener;
|
|
|
|
get horizontalOffset(): number {
|
|
const nativeView = this.nativeViewProtected;
|
|
if (!nativeView) {
|
|
return 0;
|
|
}
|
|
|
|
return nativeView.getScrollX() / layout.getDisplayDensity();
|
|
}
|
|
|
|
get verticalOffset(): number {
|
|
const nativeView = this.nativeViewProtected;
|
|
if (!nativeView) {
|
|
return 0;
|
|
}
|
|
|
|
return nativeView.getScrollY() / layout.getDisplayDensity();
|
|
}
|
|
|
|
get scrollableWidth(): number {
|
|
const nativeView = this.nativeViewProtected;
|
|
if (!nativeView || this.orientation !== "horizontal") {
|
|
return 0;
|
|
}
|
|
|
|
return nativeView.getScrollableLength() / layout.getDisplayDensity();
|
|
}
|
|
|
|
get scrollableHeight(): number {
|
|
const nativeView = this.nativeViewProtected;
|
|
if (!nativeView || this.orientation !== "vertical") {
|
|
return 0;
|
|
}
|
|
|
|
return nativeView.getScrollableLength() / layout.getDisplayDensity();
|
|
}
|
|
|
|
[isUserInteractionEnabledProperty.setNative](value: boolean) {
|
|
// NOTE: different behavior on iOS & Android:
|
|
// iOS disables user interaction recursively for all subviews as well
|
|
this.nativeViewProtected.setClickable(value);
|
|
this.nativeViewProtected.setFocusable(value);
|
|
this.nativeViewProtected.setScrollEnabled(value);
|
|
}
|
|
|
|
[isScrollEnabledProperty.getDefault](): boolean {
|
|
return this.nativeViewProtected.getScrollEnabled();
|
|
}
|
|
[isScrollEnabledProperty.setNative](value: boolean) {
|
|
this.nativeViewProtected.setScrollEnabled(value);
|
|
}
|
|
|
|
[scrollBarIndicatorVisibleProperty.getDefault](): boolean {
|
|
return true;
|
|
}
|
|
[scrollBarIndicatorVisibleProperty.setNative](value: boolean) {
|
|
if (this.orientation === "horizontal") {
|
|
this.nativeViewProtected.setHorizontalScrollBarEnabled(value);
|
|
} else {
|
|
this.nativeViewProtected.setVerticalScrollBarEnabled(value);
|
|
}
|
|
}
|
|
|
|
public scrollToVerticalOffset(value: number, animated: boolean) {
|
|
const nativeView = this.nativeViewProtected;
|
|
if (nativeView && this.orientation === "vertical" && this.isScrollEnabled) {
|
|
value *= layout.getDisplayDensity();
|
|
|
|
if (animated) {
|
|
nativeView.smoothScrollTo(0, value);
|
|
} else {
|
|
nativeView.scrollTo(0, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
public scrollToHorizontalOffset(value: number, animated: boolean) {
|
|
const nativeView = this.nativeViewProtected;
|
|
if (nativeView && this.orientation === "horizontal" && this.isScrollEnabled) {
|
|
value *= layout.getDisplayDensity();
|
|
|
|
if (animated) {
|
|
nativeView.smoothScrollTo(value, 0);
|
|
} else {
|
|
nativeView.scrollTo(value, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
public createNativeView() {
|
|
return this.orientation === "horizontal" ? new org.nativescript.widgets.HorizontalScrollView(this._context) : new org.nativescript.widgets.VerticalScrollView(this._context);
|
|
}
|
|
|
|
public initNativeView(): void {
|
|
super.initNativeView();
|
|
if (this._androidViewId < 0) {
|
|
this._androidViewId = android.view.View.generateViewId();
|
|
}
|
|
|
|
this.nativeViewProtected.setId(this._androidViewId);
|
|
}
|
|
|
|
public _onOrientationChanged() {
|
|
if (this.nativeViewProtected) {
|
|
const parent = this.parent;
|
|
if (parent) {
|
|
parent._removeView(this);
|
|
parent._addView(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected attachNative() {
|
|
const that = new WeakRef(this);
|
|
this.handler = new android.view.ViewTreeObserver.OnScrollChangedListener({
|
|
onScrollChanged: function () {
|
|
const owner: ScrollView = that.get();
|
|
if (owner) {
|
|
owner._onScrollChanged();
|
|
}
|
|
}
|
|
});
|
|
|
|
this.nativeViewProtected.getViewTreeObserver().addOnScrollChangedListener(this.handler);
|
|
}
|
|
|
|
private _lastScrollX: number = -1;
|
|
private _lastScrollY: number = -1;
|
|
private _onScrollChanged() {
|
|
const nativeView = this.nativeViewProtected;
|
|
if (nativeView) {
|
|
// Event is only raised if the scroll values differ from the last time in order to wokraround a native Android bug.
|
|
// https://github.com/NativeScript/NativeScript/issues/2362
|
|
let newScrollX = nativeView.getScrollX();
|
|
let newScrollY = nativeView.getScrollY();
|
|
if (newScrollX !== this._lastScrollX || newScrollY !== this._lastScrollY) {
|
|
this.notify(<ScrollEventData>{
|
|
object: this,
|
|
eventName: ScrollView.scrollEvent,
|
|
scrollX: newScrollX / layout.getDisplayDensity(),
|
|
scrollY: newScrollY / layout.getDisplayDensity()
|
|
});
|
|
this._lastScrollX = newScrollX;
|
|
this._lastScrollY = newScrollY;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected dettachNative() {
|
|
this.nativeViewProtected.getViewTreeObserver().removeOnScrollChangedListener(this.handler);
|
|
this.handler = null;
|
|
}
|
|
}
|
|
|
|
ScrollView.prototype.recycleNativeView = "never";
|