Skip to content

Commit

Permalink
GLSP-1399: Fix MousePositionTracker (eclipse-glsp#391)
Browse files Browse the repository at this point in the history
Ensure that the `GLSPMousePositionTracker` correctly calculates the
diagram local mouse position
  • Loading branch information
tortmayr authored Sep 10, 2024
1 parent b01477a commit d472c7a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Changes

- [diagram] Ensure that `GLSPMousePositionTracker` correctly calculates the current position in diagram local coordinates [#391](https://github.com/eclipse-glsp/glsp-client/pull/391)

### Potentially breaking changes

## [v2.2.1 - 22/07/2024](https://github.com/eclipse-glsp/glsp-client/releases/tag/v2.2.1)
Expand Down
20 changes: 4 additions & 16 deletions packages/client/src/base/mouse-position-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,21 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import {
Action,
DOMHelper,
GModelElement,
MousePositionTracker,
Point,
TYPES,
ViewerOptions,
getAbsoluteClientBounds
} from '@eclipse-glsp/sprotty';
import { inject, injectable } from 'inversify';
import { Action, GModelElement, MousePositionTracker, Point } from '@eclipse-glsp/sprotty';
import { injectable } from 'inversify';
import { Ranked } from './ranked';

@injectable()
export class GLSPMousePositionTracker extends MousePositionTracker implements Ranked {
/* we want to be executed before all default mouse listeners since we are just tracking the position and others may need it */
rank = Ranked.DEFAULT_RANK - 200;

@inject(TYPES.ViewerOptions) protected viewerOptions: ViewerOptions;
@inject(TYPES.DOMHelper) protected domHelper: DOMHelper;

override mouseMove(target: GModelElement, event: MouseEvent): (Action | Promise<Action>)[] {
// we cannot simply use the offsetX and offsetY properties of the event since they also consider nested HTML elements
// such as foreignObjects or the projection bars divs. Instead, we manually translate the client coordinates to the diagram
const clientToRoot = getAbsoluteClientBounds(target.root, this.domHelper, this.viewerOptions);
const globalOrigin = target.root.canvasBounds;
const clientToPosition = { x: event.clientX, y: event.clientY };
const rootToPosition = Point.subtract(clientToPosition, clientToRoot);
const rootToPosition = Point.subtract(clientToPosition, globalOrigin);
const positionOnDiagram = target.root.parentToLocal(rootToPosition);
this.lastPosition = positionOnDiagram;
return [];
Expand Down

0 comments on commit d472c7a

Please sign in to comment.