Skip to content

Commit

Permalink
[Web] Migrate to get and set (#3263)
Browse files Browse the repository at this point in the history
In this PR I've migrated our handlers into `get`/`set` functions instead of traditional getters and setters convention (like in `Java`).

Run web example
  • Loading branch information
m-bert committed Dec 20, 2024
1 parent 61aaef9 commit 8f34660
Show file tree
Hide file tree
Showing 22 changed files with 368 additions and 367 deletions.
4 changes: 2 additions & 2 deletions src/RNGestureHandlerModule.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default {
handlerTag,
new GestureClass(new GestureHandlerWebDelegate())
);
InteractionManager.getInstance().configureInteractions(
InteractionManager.instance.configureInteractions(
NodeManager.getHandler(handlerTag),
config as unknown as Config
);
Expand Down Expand Up @@ -93,7 +93,7 @@ export default {
if (isNewWebImplementationEnabled()) {
NodeManager.getHandler(handlerTag).updateGestureConfig(newConfig);

InteractionManager.getInstance().configureInteractions(
InteractionManager.instance.configureInteractions(
NodeManager.getHandler(handlerTag),
newConfig
);
Expand Down
44 changes: 22 additions & 22 deletions src/web/detectors/RotationGestureDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export default class RotationGestureDetector
private previousTime = 0;

private previousAngle = 0;
private rotation = 0;
private _rotation = 0;

private anchorX = 0;
private anchorY = 0;
private _anchorX = 0;
private _anchorY = 0;

private isInProgress = false;

Expand All @@ -45,28 +45,28 @@ export default class RotationGestureDetector
const vectorX: number = secondPointerCoords.x - firstPointerCoords.x;
const vectorY: number = secondPointerCoords.y - firstPointerCoords.y;

this.anchorX = (firstPointerCoords.x + secondPointerCoords.x) / 2;
this.anchorY = (firstPointerCoords.y + secondPointerCoords.y) / 2;
this._anchorX = (firstPointerCoords.x + secondPointerCoords.x) / 2;
this._anchorY = (firstPointerCoords.y + secondPointerCoords.y) / 2;

// Angle diff should be positive when rotating in clockwise direction
const angle: number = -Math.atan2(vectorY, vectorX);

this.rotation = Number.isNaN(this.previousAngle)
this._rotation = Number.isNaN(this.previousAngle)
? 0
: this.previousAngle - angle;

this.previousAngle = angle;

if (this.rotation > Math.PI) {
this.rotation -= Math.PI;
this._rotation -= Math.PI;
} else if (this.rotation < -Math.PI) {
this.rotation += Math.PI;
this._rotation += Math.PI;
}

if (this.rotation > Math.PI / 2) {
this.rotation -= Math.PI;
this._rotation -= Math.PI;
} else if (this.rotation < -Math.PI / 2) {
this.rotation += Math.PI;
this._rotation += Math.PI;
}
}

Expand All @@ -85,7 +85,7 @@ export default class RotationGestureDetector
return;
}

const pointerIDs: IterableIterator<number> = tracker.getData().keys();
const pointerIDs: IterableIterator<number> = tracker.trackedPointers.keys();

this.keyPointers[0] = pointerIDs.next().value as number;
this.keyPointers[1] = pointerIDs.next().value as number;
Expand Down Expand Up @@ -143,24 +143,24 @@ export default class RotationGestureDetector
return true;
}

public getTimeDelta(): number {
return this.currentTime + this.previousTime;
public reset(): void {
this.keyPointers = [NaN, NaN];
this.isInProgress = false;
}

public getAnchorX(): number {
return this.anchorX;
public get anchorX() {
return this._anchorX;
}

public getAnchorY(): number {
return this.anchorY;
public get anchorY() {
return this._anchorY;
}

public getRotation(): number {
return this.rotation;
public get rotation() {
return this._rotation;
}

public reset(): void {
this.keyPointers = [NaN, NaN];
this.isInProgress = false;
public get timeDelta() {
return this.currentTime + this.previousTime;
}
}
48 changes: 24 additions & 24 deletions src/web/detectors/ScaleGestureDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
public onScale: (detector: ScaleGestureDetector) => boolean;
public onScaleEnd: (detector: ScaleGestureDetector) => void;

private focusX!: number;
private focusY!: number;
private _focusX!: number;
private _focusY!: number;

private currentSpan!: number;
private _currentSpan!: number;
private prevSpan!: number;
private initialSpan!: number;

Expand All @@ -42,7 +42,7 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
this.currentTime = event.time;

const action: EventTypes = event.eventType;
const numOfPointers = tracker.getTrackedPointersCount();
const numOfPointers = tracker.trackedPointersCount;

const streamComplete: boolean =
action === EventTypes.UP ||
Expand Down Expand Up @@ -86,7 +86,7 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
let devSumX = 0;
let devSumY = 0;

tracker.getData().forEach((value, key) => {
tracker.trackedPointers.forEach((value, key) => {
if (key === ignoredPointer) {
return;
}
Expand All @@ -105,8 +105,8 @@ export default class ScaleGestureDetector implements ScaleGestureListener {

// Begin/end events
const wasInProgress: boolean = this.inProgress;
this.focusX = focusX;
this.focusY = focusY;
this._focusX = focusX;
this._focusY = focusY;

if (this.inProgress && (span < this.minSpan || configChanged)) {
this.onScaleEnd(this);
Expand All @@ -115,15 +115,15 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
}

if (configChanged) {
this.initialSpan = this.prevSpan = this.currentSpan = span;
this.initialSpan = this.prevSpan = this._currentSpan = span;
}

if (
!this.inProgress &&
span >= this.minSpan &&
(wasInProgress || Math.abs(span - this.initialSpan) > this.spanSlop)
) {
this.prevSpan = this.currentSpan = span;
this.prevSpan = this._currentSpan = span;
this.prevTime = this.currentTime;
this.inProgress = this.onScaleBegin(this);
}
Expand All @@ -133,7 +133,7 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
return true;
}

this.currentSpan = span;
this._currentSpan = span;

if (this.inProgress && !this.onScale(this)) {
return true;
Expand All @@ -145,27 +145,27 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
return true;
}

public getCurrentSpan(): number {
return this.currentSpan;
}
public calculateScaleFactor(numOfPointers: number): number {
if (numOfPointers < 2) {
return 1;
}

public getFocusX(): number {
return this.focusX;
return this.prevSpan > 0 ? this.currentSpan / this.prevSpan : 1;
}

public getFocusY(): number {
return this.focusY;
public get currentSpan() {
return this._currentSpan;
}

public getTimeDelta(): number {
return this.currentTime - this.prevTime;
public get focusX() {
return this._focusX;
}

public getScaleFactor(numOfPointers: number): number {
if (numOfPointers < 2) {
return 1;
}
public get focusY() {
return this._focusY;
}

return this.prevSpan > 0 ? this.currentSpan / this.prevSpan : 1;
public get timeDelta() {
return this.currentTime - this.prevTime;
}
}
13 changes: 6 additions & 7 deletions src/web/handlers/FlingGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,29 +121,28 @@ export default class FlingGestureHandler extends GestureHandler {
}

private newPointerAction(): void {
if (this.currentState === State.UNDETERMINED) {
if (this.state === State.UNDETERMINED) {
this.startFling();
}

if (this.currentState !== State.BEGAN) {
if (this.state !== State.BEGAN) {
return;
}

this.tryEndFling();

if (
this.tracker.getTrackedPointersCount() >
this.maxNumberOfPointersSimultaneously
this.tracker.trackedPointersCount > this.maxNumberOfPointersSimultaneously
) {
this.maxNumberOfPointersSimultaneously =
this.tracker.getTrackedPointersCount();
this.tracker.trackedPointersCount;
}
}

private pointerMoveAction(event: AdaptedEvent): void {
this.tracker.track(event);

if (this.currentState !== State.BEGAN) {
if (this.state !== State.BEGAN) {
return;
}

Expand Down Expand Up @@ -173,7 +172,7 @@ export default class FlingGestureHandler extends GestureHandler {
}

private onUp(event: AdaptedEvent): void {
if (this.currentState === State.BEGAN) {
if (this.state === State.BEGAN) {
this.endFling();
}

Expand Down
Loading

0 comments on commit 8f34660

Please sign in to comment.