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

GLSP-1423 Add MovementBehavior to control invalid multi element movements #397

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,18 @@ export class FeedbackMoveMouseListener extends DragAwareMouseListener {
if (!this.tracker.isTracking()) {
return [];
}
// only reset the move of invalid elements, the others will be handled by the change bounds tool itself
this.getElementsToMove(target)
.filter(element => this.tool.changeBoundsManager.isValid(element))
.forEach(element => this.elementId2startPos.delete(element.id));
const elementToMove = this.getElementsToMove(target);
ivy-lli marked this conversation as resolved.
Show resolved Hide resolved
if (!this.tool.movementBehavior.allElementsNeedToBeValid) {
// only reset the move of invalid elements, the others will be handled by the change bounds tool itself
elementToMove
.filter(element => this.tool.changeBoundsManager.isValid(element))
.forEach(element => this.elementId2startPos.delete(element.id));
} else {
if (elementToMove.find(element => !this.tool.changeBoundsManager.isValid(element)) === undefined) {
ivy-lli marked this conversation as resolved.
Show resolved Hide resolved
// do not reset any element as all are valid
this.elementId2startPos.clear();
}
}
this.dispose();
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ import {
import { FeedbackMoveMouseListener } from './change-bounds-tool-move-feedback';
import { ChangeBoundsTracker, TrackedElementResize, TrackedResize } from './change-bounds-tracker';

export interface IMovementBehavior {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little bit on the fence about whether it should be called IMovementBehavior (as we have with the IMarqueeBehavior) or IMovementOptions (as we have with the IHelperLineOptions). I think both are probably fine, just wanted to note that down somewhere. We should probably unify that at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I named it indeed after the MarqueeBehavior (as I invented this too 🙈), however, I think I like Options more, so I will call it options.

readonly allElementsNeedToBeValid: boolean;
ivy-lli marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* The change bounds tool has the license to move multiple elements or resize a single element by implementing the ChangeBounds operation.
* In contrast to Sprotty's implementation this tool only sends a `ChangeBoundsOperationAction` when an operation has finished and does not
Expand All @@ -84,6 +88,7 @@ export class ChangeBoundsTool extends BaseEditTool {
@inject(EdgeRouterRegistry) @optional() readonly edgeRouterRegistry?: EdgeRouterRegistry;
@inject(TYPES.IMovementRestrictor) @optional() readonly movementRestrictor?: IMovementRestrictor;
@inject(TYPES.IChangeBoundsManager) readonly changeBoundsManager: IChangeBoundsManager;
@inject(TYPES.IMovementBehavior) @optional() readonly movementBehavior: IMovementBehavior = { allElementsNeedToBeValid: true };

get id(): string {
return ChangeBoundsTool.ID;
Expand Down Expand Up @@ -260,7 +265,11 @@ export class ChangeBoundsListener extends DragAwareMouseListener implements ISel
protected getElementsToMove(target: GModelElement): SelectableBoundsAware[] {
const selectedElements = getMatchingElements(target.index, isNonRoutableSelectedMovableBoundsAware);
const selectionSet: Set<BoundsAwareModelElement> = new Set(selectedElements);
return selectedElements.filter(element => this.isValidMove(element, selectionSet));
const elementsToMove = selectedElements.filter(element => this.isValidMove(element, selectionSet));
if (this.tool.movementBehavior.allElementsNeedToBeValid && elementsToMove.length !== selectionSet.size) {
return [];
}
return elementsToMove;
}

protected handleMoveElementsOnServer(elementsToMove: SelectableBoundsAware[]): Operation[] {
Expand Down
1 change: 1 addition & 0 deletions packages/glsp-sprotty/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const TYPES = {
IToolFactory: Symbol('Factory<Tool>'),
ITypeHintProvider: Symbol('ITypeHintProvider'),
IMovementRestrictor: Symbol('IMovementRestrictor'),
IMovementBehavior: Symbol('IMovementBehavior'),
ISelectionListener: Symbol('ISelectionListener'),
/** @deprecated Use {@link TYPES.IGModelRootListener} instead */
// eslint-disable-next-line deprecation/deprecation
Expand Down
Loading