Skip to content

Commit

Permalink
Fix #1440 autorotate: stuck when using keyboard zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Sep 23, 2024
1 parent bbd8114 commit fa7ef61
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
15 changes: 9 additions & 6 deletions packages/core/src/services/EventsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class EventsHandler extends AbstractService {
};

private readonly step = new Step();
private readonly keyHandler = new PressHandler();
private readonly keyHandler = new PressHandler<ACTIONS>();
private readonly resizeObserver = new ResizeObserver(throttle(() => this.viewer.autoSize(), 50));
private readonly moveThreshold = MOVE_THRESHOLD * SYSTEM.pixelRatio;

Expand Down Expand Up @@ -207,7 +207,7 @@ export class EventsHandler extends AbstractService {
case ACTIONS.ZOOM_OUT: this.viewer.dynamics.zoom.roll(true); break;
}

this.keyHandler.down();
this.keyHandler.down(action);
e.preventDefault();
}
}
Expand All @@ -222,10 +222,13 @@ export class EventsHandler extends AbstractService {
return;
}

this.keyHandler.up(() => {
this.viewer.dynamics.position.stop();
this.viewer.dynamics.zoom.stop();
this.viewer.resetIdleTimer();
this.keyHandler.up((action) => {
if (action === ACTIONS.ZOOM_IN || action === ACTIONS.ZOOM_OUT) {
this.viewer.dynamics.zoom.stop();
} else {
this.viewer.dynamics.position.stop();
this.viewer.resetIdleTimer();
}
});
}

Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/utils/PressHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* When the pressed thing goes up and was not pressed long enough, wait a bit more before execution
* @internal
*/
export class PressHandler {
export class PressHandler<TData = never> {
private time = 0;
private timeout: ReturnType<typeof setTimeout>;
private data: TData;

get pending() {
return this.time !== 0;
Expand All @@ -15,30 +16,33 @@ export class PressHandler {
this.delay = delay;
}

down() {
down(data?: TData) {
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = undefined;
}

this.time = new Date().getTime();
this.data = data;
}

up(cb: () => void) {
up(cb: (data: TData) => void) {
if (!this.time) {
return;
}

const elapsed = Date.now() - this.time;
if (elapsed < this.delay) {
this.timeout = setTimeout(() => {
cb();
cb(this.data);
this.timeout = undefined;
this.time = 0;
this.data = undefined;
}, this.delay);
} else {
cb();
cb(this.data);
this.time = 0;
this.data = undefined;
}
}
}

0 comments on commit fa7ef61

Please sign in to comment.