Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interaction handler keyboard handlers fix #3881

Merged
merged 9 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cvat-canvas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Canvas itself handles:
cancel(): void;
configure(configuration: Configuration): void;
isAbleToChangeFrame(): boolean;
destroy(): void;

readonly geometry: Geometry;
}
Expand Down Expand Up @@ -189,6 +190,7 @@ Standard JS events are used.
- canvas.resizeshape => {id: number}
- canvas.contextmenu => { mouseEvent: MouseEvent, objectState: ObjectState, pointID: number }
- canvas.error => { exception: Error }
- canvas.destroy
```

### WEB
Expand Down Expand Up @@ -239,6 +241,7 @@ canvas.draw({
| bitmap() | + | + | + | + | + | + | + | + | + | + | + |
| setZLayer() | + | + | + | + | + | + | + | + | + | + | + |
| setupReviewROIs() | + | + | + | + | + | + | + | + | + | + | + |
| destroy() | + | + | + | + | + | + | + | + | + | + | + |

<!--lint enable maximum-line-length-->

Expand Down
4 changes: 2 additions & 2 deletions cvat-canvas/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cvat-canvas/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cvat-canvas",
"version": "2.8.0",
"version": "2.9.0",
"description": "Part of Computer Vision Annotation Tool which presents its canvas library",
"main": "src/canvas.ts",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions cvat-canvas/src/typescript/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ interface Canvas {
cancel(): void;
configure(configuration: Configuration): void;
isAbleToChangeFrame(): boolean;
destroy(): void;

readonly geometry: Geometry;
}
Expand Down Expand Up @@ -163,6 +164,10 @@ class CanvasImpl implements Canvas {
public get geometry(): Geometry {
return this.model.geometry;
}

public destroy(): void {
this.view.destroy();
}
}

export type InteractionData = _InteractionData;
Expand Down
13 changes: 13 additions & 0 deletions cvat-canvas/src/typescript/canvasView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {

export interface CanvasView {
html(): HTMLDivElement;
destroy(): void;
}

export class CanvasViewImpl implements CanvasView, Listener {
Expand Down Expand Up @@ -1369,6 +1370,18 @@ export class CanvasViewImpl implements CanvasView, Listener {
return this.canvas;
}

public destroy(): void {
this.canvas.dispatchEvent(
new CustomEvent('canvas.destroy', {
bubbles: false,
cancelable: true,
}),
);
// We can't call namespaced svgjs event
// see - https://svgjs.dev/docs/2.7/events/
bsekachev marked this conversation as resolved.
Show resolved Hide resolved
this.adoptedContent.fire('destroy');
}

private redrawBitmap(): void {
const width = +this.background.style.width.slice(0, -2);
const height = +this.background.style.height.slice(0, -2);
Expand Down
44 changes: 26 additions & 18 deletions cvat-canvas/src/typescript/interactionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,27 @@ export class InteractionHandlerImpl implements InteractionHandler {
return false;
}

private onKeyUp = (e:KeyboardEvent):void => {
klakhov marked this conversation as resolved.
Show resolved Hide resolved
if (this.interactionData.enabled && e.keyCode === 17) {
if (this.interactionData.onChangeToolsBlockerState && !this.thresholdWasModified) {
this.interactionData.onChangeToolsBlockerState('keyup');
}
if (this.shouldRaiseEvent(false)) {
// 17 is ctrl
this.onInteraction(this.prepareResult(), true, false);
}
}
};

private onKeyDown = (e:KeyboardEvent):void => {
klakhov marked this conversation as resolved.
Show resolved Hide resolved
if (!e.repeat && this.interactionData.enabled && e.keyCode === 17) {
if (this.interactionData.onChangeToolsBlockerState && !this.thresholdWasModified) {
this.interactionData.onChangeToolsBlockerState('keydown');
}
this.thresholdWasModified = false;
}
};

public constructor(
onInteraction: (
shapes: InteractionResult[] | null,
Expand Down Expand Up @@ -452,25 +473,12 @@ export class InteractionHandlerImpl implements InteractionHandler {
}
});

window.addEventListener('keyup', (e: KeyboardEvent): void => {
if (this.interactionData.enabled && e.keyCode === 17) {
if (this.interactionData.onChangeToolsBlockerState && !this.thresholdWasModified) {
this.interactionData.onChangeToolsBlockerState('keyup');
}
if (this.shouldRaiseEvent(false)) {
// 17 is ctrl
this.onInteraction(this.prepareResult(), true, false);
}
}
});
window.addEventListener('keyup', this.onKeyUp);
window.addEventListener('keydown', this.onKeyDown);

window.addEventListener('keydown', (e: KeyboardEvent): void => {
if (!e.repeat && this.interactionData.enabled && e.keyCode === 17) {
if (this.interactionData.onChangeToolsBlockerState && !this.thresholdWasModified) {
this.interactionData.onChangeToolsBlockerState('keydown');
}
this.thresholdWasModified = false;
}
this.canvas.on('destroy.canvas', ():void => {
window.removeEventListener('keyup', this.onKeyUp);
window.removeEventListener('keydown', this.onKeyDown);
});
}

Expand Down
5 changes: 5 additions & 0 deletions cvat-canvas3d/src/typescript/canvas3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface Canvas3d {
fitCanvas(): void;
fit(): void;
group(groupData: GroupData): void;
destroy(): void;
}

class Canvas3dImpl implements Canvas3d {
Expand Down Expand Up @@ -104,6 +105,10 @@ class Canvas3dImpl implements Canvas3d {
public fitCanvas(): void {
this.model.fit();
}

public destroy(): void {
this.view.destroy();
}
}

export {
Expand Down
55 changes: 32 additions & 23 deletions cvat-canvas3d/src/typescript/canvas3dView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Canvas3dView {
html(): ViewsDOM;
render(): void;
keyControls(keys: KeyboardEvent): void;
destroy(): void;
}

export enum CameraAction {
Expand Down Expand Up @@ -287,6 +288,7 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
(_state: any): boolean => _state.clientID === Number(intersects[0].object.name),
);
if (item.length !== 0) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.model.data.groupData.grouped = this.model.data.groupData.grouped.filter(
(_state: any): boolean => _state.clientID !== Number(intersects[0].object.name),
Expand Down Expand Up @@ -543,9 +545,9 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
this.action.rotation.screenInit = { x: diffX, y: diffY };
this.action.rotation.screenMove = { x: diffX, y: diffY };
if (
this.model.data.selected
&& !this.model.data.selected.perspective.userData.lock
&& !this.model.data.selected.perspective.userData.hidden
this.model.data.selected &&
!this.model.data.selected.perspective.userData.lock &&
!this.model.data.selected.perspective.userData.hidden
) {
this.action.scan = view;
this.model.mode = Mode.EDIT;
Expand Down Expand Up @@ -698,8 +700,8 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
cuboid.setOpacity(opacity);

if (
this.model.data.activeElement.clientID === clientID
&& ![Mode.DRAG_CANVAS, Mode.GROUP].includes(this.mode)
this.model.data.activeElement.clientID === clientID &&
![Mode.DRAG_CANVAS, Mode.GROUP].includes(this.mode)
) {
cuboid.setOpacity(selectedOpacity);
if (!object.lock) {
Expand Down Expand Up @@ -964,12 +966,12 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
const sphereCenter = points.geometry.boundingSphere.center;
const { radius } = points.geometry.boundingSphere;
if (!this.views.perspective.camera) return;
const xRange = -radius / 2 < this.views.perspective.camera.position.x - sphereCenter.x
&& radius / 2 > this.views.perspective.camera.position.x - sphereCenter.x;
const yRange = -radius / 2 < this.views.perspective.camera.position.y - sphereCenter.y
&& radius / 2 > this.views.perspective.camera.position.y - sphereCenter.y;
const zRange = -radius / 2 < this.views.perspective.camera.position.z - sphereCenter.z
&& radius / 2 > this.views.perspective.camera.position.z - sphereCenter.z;
const xRange = -radius / 2 < this.views.perspective.camera.position.x - sphereCenter.x &&
radius / 2 > this.views.perspective.camera.position.x - sphereCenter.x;
const yRange = -radius / 2 < this.views.perspective.camera.position.y - sphereCenter.y &&
radius / 2 > this.views.perspective.camera.position.y - sphereCenter.y;
const zRange = -radius / 2 < this.views.perspective.camera.position.z - sphereCenter.z &&
radius / 2 > this.views.perspective.camera.position.z - sphereCenter.z;
let newX = 0;
let newY = 0;
let newZ = 0;
Expand Down Expand Up @@ -1085,10 +1087,10 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {

private positionAllViews(x: number, y: number, z: number, animation: boolean): void {
if (
this.views.perspective.controls
&& this.views.top.controls
&& this.views.side.controls
&& this.views.front.controls
this.views.perspective.controls &&
this.views.top.controls &&
this.views.side.controls &&
this.views.front.controls
) {
this.views.perspective.controls.setLookAt(x - 8, y - 8, z + 3, x, y, z, animation);
this.views.top.camera.position.set(x, y, z + 8);
Expand Down Expand Up @@ -1266,8 +1268,8 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {

private renderTranslateAction(view: ViewType, viewType: any): void {
if (
this.action.translation.helper.x === this.views[view].rayCaster.mouseVector.x
&& this.action.translation.helper.y === this.views[view].rayCaster.mouseVector.y
this.action.translation.helper.x === this.views[view].rayCaster.mouseVector.x &&
this.action.translation.helper.y === this.views[view].rayCaster.mouseVector.y
) {
return;
}
Expand Down Expand Up @@ -1332,8 +1334,8 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
}

if (
this.action.resize.recentMouseVector.x === currentPosX
&& this.action.resize.recentMouseVector.y === currentPosY
this.action.resize.recentMouseVector.x === currentPosX &&
this.action.resize.recentMouseVector.y === currentPosY
) {
return;
}
Expand Down Expand Up @@ -1736,15 +1738,15 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
y: canvas.offsetTop + canvas.offsetHeight / 2,
};
if (
this.action.rotation.screenInit.x === this.action.rotation.screenMove.x
&& this.action.rotation.screenInit.y === this.action.rotation.screenMove.y
this.action.rotation.screenInit.x === this.action.rotation.screenMove.x &&
this.action.rotation.screenInit.y === this.action.rotation.screenMove.y
) {
return;
}

if (
this.action.rotation.recentMouseVector.x === this.views[view].rayCaster.mouseVector.x
&& this.action.rotation.recentMouseVector.y === this.views[view].rayCaster.mouseVector.y
this.action.rotation.recentMouseVector.x === this.views[view].rayCaster.mouseVector.x &&
this.action.rotation.recentMouseVector.y === this.views[view].rayCaster.mouseVector.y
) {
return;
}
Expand Down Expand Up @@ -1875,4 +1877,11 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
front: this.views.front.renderer.domElement,
};
}

public destroy(): void {
this.dispatchEvent(new CustomEvent('canvas.destroy', {
klakhov marked this conversation as resolved.
Show resolved Hide resolved
bubbles: false,
cancelable: true,
}));
}
}
4 changes: 2 additions & 2 deletions cvat-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cvat-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cvat-ui",
"version": "1.25.0",
"version": "1.25.1",
"description": "CVAT single-page application",
"main": "src/index.tsx",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions cvat-ui/src/actions/annotation-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export enum AnnotationActionTypes {
CONFIRM_CANVAS_READY = 'CONFIRM_CANVAS_READY',
DRAG_CANVAS = 'DRAG_CANVAS',
ZOOM_CANVAS = 'ZOOM_CANVAS',
DESTROY_CANVAS = 'DESTROY_CANVAS',
SELECT_ISSUE_POSITION = 'SELECT_ISSUE_POSITION',
MERGE_OBJECTS = 'MERGE_OBJECTS',
GROUP_OBJECTS = 'GROUP_OBJECTS',
Expand Down Expand Up @@ -1024,6 +1025,8 @@ export function getJobAsync(tid: number, jid: number, initialFrame: number, init

loadJobEvent.close(await jobInfoGenerator(job));

dispatch({ type: AnnotationActionTypes.DESTROY_CANVAS });

const openTime = Date.now();
dispatch({
type: AnnotationActionTypes.GET_JOB_SUCCESS,
Expand Down
3 changes: 2 additions & 1 deletion cvat-ui/src/actions/boundaries-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'utils/redux';
import getCore from 'cvat-core-wrapper';
import { LogType } from 'cvat-logger';
import { computeZRange } from './annotation-actions';
import { AnnotationActionTypes, computeZRange } from './annotation-actions';

const cvat = getCore();

Expand Down Expand Up @@ -43,6 +43,7 @@ export function resetAfterErrorAsync(): ThunkAction {
const state = getState();
const job = state.annotation.job.instance;

dispatch(createAction(AnnotationActionTypes.DESTROY_CANVAS));
ActiveChooN marked this conversation as resolved.
Show resolved Hide resolved
if (job) {
const currentFrame = state.annotation.player.frame.number;
const { showAllInterpolationTracks } = state.settings.workspace;
Expand Down
6 changes: 6 additions & 0 deletions cvat-ui/src/reducers/annotation-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ export default (state = defaultState, action: AnyAction): AnnotationState => {
},
};
}
case AnnotationActionTypes.DESTROY_CANVAS: {
state.canvas.instance.destroy();
return {
...state,
};
}
case AnnotationActionTypes.REMEMBER_CREATED_OBJECT: {
const { payload } = action;

Expand Down