Skip to content

Commit

Permalink
[sigma] Clears FakeSigmaMouseEvent related code
Browse files Browse the repository at this point in the history
After re-discovering these fake events (mentionned in #1292), I tested
them as much as I could, and I'm pretty sure they are never actually
caught from the mouse captor.

Also, this commit always uses `this.getNodeAtPosition(e)` to find what
node is under the cursor, and never uses `hoveredNode`.
  • Loading branch information
jacomyal committed Oct 30, 2024
1 parent f9e150d commit 56b8b47
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 35 deletions.
33 changes: 1 addition & 32 deletions packages/sigma/src/core/captors/touch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export const DEFAULT_TOUCH_SETTINGS = TOUCH_SETTINGS_KEYS.reduce(
{},
) as TouchSettings;

export type FakeSigmaMouseEvent = MouseEvent & { isFakeSigmaMouseEvent?: true };

/**
* Event types.
*/
Expand Down Expand Up @@ -88,27 +86,10 @@ export default class TouchCaptor<
};
}

dispatchRelatedMouseEvent(type: string, e: TouchEvent, touch?: Touch, emitter?: EventTarget): void {
const mousePosition = touch || e.touches[0];
const mouseEvent = new MouseEvent(type, {
clientX: mousePosition.clientX,
clientY: mousePosition.clientY,
altKey: e.altKey,
ctrlKey: e.ctrlKey,
});

(mouseEvent as FakeSigmaMouseEvent).isFakeSigmaMouseEvent = true;

(emitter || this.container).dispatchEvent(mouseEvent);
}

handleStart(e: TouchEvent): void {
if (!this.enabled) return;

// Prevent default to avoid default browser behaviors...
e.preventDefault();
// ...but simulate mouse behavior anyway, to get the MouseCaptor working as well:
if (e.touches.length === 1) this.dispatchRelatedMouseEvent("mousedown", e);

const touches = getTouchesArray(e.touches);
this.touchMode = touches.length;
Expand All @@ -131,16 +112,7 @@ export default class TouchCaptor<
handleLeave(e: TouchEvent): void {
if (!this.enabled) return;

// Prevent default to avoid default browser behaviors...
e.preventDefault();
// ...but simulate mouse behavior anyway, to get the MouseCaptor working as well:
if (e.touches.length === 0 && this.lastTouches && this.lastTouches.length) {
this.dispatchRelatedMouseEvent("mouseup", e, this.lastTouches[0], document);
// ... and only click if no move was made
if (!this.hasMoved) {
this.dispatchRelatedMouseEvent("click", e, this.lastTouches[0]);
}
}

if (this.movingTimeout) {
this.isMoving = false;
Expand Down Expand Up @@ -191,17 +163,14 @@ export default class TouchCaptor<
handleMove(e: TouchEvent): void {
if (!this.enabled) return;

// Prevent default to avoid default browser behaviors...
e.preventDefault();
// ...but simulate mouse behavior anyway, to get the MouseCaptor working as well:
if (e.touches.length === 1) this.dispatchRelatedMouseEvent("mousemove", e);

const touches = getTouchesArray(e.touches);
const touchesPositions = touches.map((touch) => getPosition(touch, this.container));
this.lastTouches = touches;
this.lastTouchesPositions = touchesPositions;

// If a move was initiated at some point, and we get back to startpoint,
// If a move was initiated at some point, and we get back to start point,
// we should still consider that we did move (which also happens after a
// multiple touch when only one touch remains in which case handleStart
// is recalled within handleLeave).
Expand Down
5 changes: 2 additions & 3 deletions packages/sigma/src/sigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Graph, { Attributes } from "graphology-types";

import Camera from "./core/camera";
import MouseCaptor from "./core/captors/mouse";
import TouchCaptor, { FakeSigmaMouseEvent } from "./core/captors/touch";
import TouchCaptor from "./core/captors/touch";
import { LabelGrid, edgeLabelsToDisplayFromNodes } from "./core/labels";
import { AbstractEdgeProgram, AbstractNodeProgram, EdgeProgramType, NodeProgramType } from "./rendering";
import { Settings, resolveSettings, validateSettings } from "./settings";
Expand Down Expand Up @@ -493,8 +493,7 @@ export default class Sigma<
},
};

const isFakeSigmaMouseEvent = (e.original as FakeSigmaMouseEvent).isFakeSigmaMouseEvent;
const nodeAtPosition = isFakeSigmaMouseEvent ? this.getNodeAtPosition(e) : this.hoveredNode;
const nodeAtPosition = this.getNodeAtPosition(e);

if (nodeAtPosition)
return this.emit(`${eventType}Node`, {
Expand Down

0 comments on commit 56b8b47

Please sign in to comment.