/
change-bounds-tool.ts
442 lines (392 loc) · 18.5 KB
/
change-bounds-tool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/********************************************************************************
* Copyright (c) 2019 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { inject, injectable, optional } from "inversify";
import {
Action,
Bounds,
BoundsAware,
Dimension,
EdgeRouterRegistry,
ElementAndBounds,
findParentByFeature,
ISnapper,
isSelected,
isViewport,
MouseListener,
Point,
SConnectableElement,
SetBoundsAction,
SModelElement,
SModelRoot,
SParentElement,
TYPES
} from "sprotty";
import { DragAwareMouseListener } from "../../base/drag-aware-mouse-listener";
import {
ChangeBoundsOperation,
ChangeRoutingPointsOperation,
CompoundOperation,
ElementAndRoutingPoints,
Operation
} from "../../base/operations/operation";
import { GLSP_TYPES } from "../../base/types";
import { isValidMove, isValidSize, WriteablePoint } from "../../utils/layout-utils";
import {
forEachElement,
isNonRoutableSelectedMovableBoundsAware,
toElementAndBounds,
toElementAndRoutingPoints
} from "../../utils/smodel-util";
import { isBoundsAwareMoveable, isResizable, Resizable, ResizeHandleLocation, SResizeHandle } from "../change-bounds/model";
import {
createMovementRestrictionFeedback,
IMovementRestrictor,
removeMovementRestrictionFeedback
} from "../change-bounds/movement-restrictor";
import { SelectionListener, SelectionService } from "../select/selection-service";
import {
FeedbackMoveMouseListener,
HideChangeBoundsToolResizeFeedbackAction,
ShowChangeBoundsToolResizeFeedbackAction
} from "../tool-feedback/change-bounds-tool-feedback";
import { applyCssClasses, CursorCSS, cursorFeedbackAction, deleteCssClasses } from "../tool-feedback/css-feedback";
import { BaseGLSPTool } from "./base-glsp-tool";
/**
* 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
* provide client-side live updates to improve performance.
*
* | Operation | Client Update | Server Update
* +-----------+------------------+----------------------------
* | Move | MoveAction | ChangeBoundsOperationAction
* | Resize | SetBoundsAction | ChangeBoundsOperationAction
*
* To provide a visual client updates during move we install the `FeedbackMoveMouseListener` and to provide visual client updates during resize
* and send the server updates we install the `ChangeBoundsListener`.
*/
@injectable()
export class ChangeBoundsTool extends BaseGLSPTool {
static ID = "glsp.change-bounds-tool";
readonly id = ChangeBoundsTool.ID;
@inject(GLSP_TYPES.SelectionService) protected selectionService: SelectionService;
@inject(EdgeRouterRegistry) @optional() readonly edgeRouterRegistry?: EdgeRouterRegistry;
@inject(TYPES.ISnapper) @optional() readonly snapper?: ISnapper;
@inject(GLSP_TYPES.IMovementRestrictor) @optional() readonly movementRestrictor?: IMovementRestrictor;
protected feedbackMoveMouseListener: MouseListener;
protected changeBoundsListener: MouseListener & SelectionListener;
enable() {
// install feedback move mouse listener for client-side move updates
this.feedbackMoveMouseListener = this.createMoveMouseListener();
this.mouseTool.register(this.feedbackMoveMouseListener);
// install change bounds listener for client-side resize updates and server-side updates
this.changeBoundsListener = this.createChangeBoundsListener();
this.mouseTool.register(this.changeBoundsListener);
this.selectionService.register(this.changeBoundsListener);
}
protected createMoveMouseListener(): MouseListener {
return new FeedbackMoveMouseListener(this);
}
protected createChangeBoundsListener(): MouseListener & SelectionListener {
return new ChangeBoundsListener(this);
}
disable() {
this.mouseTool.deregister(this.changeBoundsListener);
this.selectionService.deregister(this.changeBoundsListener);
this.mouseTool.deregister(this.feedbackMoveMouseListener);
this.deregisterFeedback([], this.feedbackMoveMouseListener);
this.deregisterFeedback([new HideChangeBoundsToolResizeFeedbackAction], this.changeBoundsListener);
}
}
export class ChangeBoundsListener extends DragAwareMouseListener implements SelectionListener {
static readonly CSS_CLASS_ACTIVE = 'active';
// members for calculating the correct position change
protected initialBounds: Bounds | undefined;
protected lastDragPosition?: Point;
protected positionDelta: WriteablePoint = { x: 0, y: 0 };
// members for resize mode
protected activeResizeElement?: SModelElement;
protected activeResizeHandle?: SResizeHandle;
constructor(protected tool: ChangeBoundsTool) {
super();
}
mouseDown(target: SModelElement, event: MouseEvent): Action[] {
super.mouseDown(target, event);
if (event.button !== 0) {
return [];
}
// check if we have a resize handle (only single-selection)
if (this.activeResizeElement && target instanceof SResizeHandle) {
this.activeResizeHandle = target;
} else {
this.setActiveResizeElement(target);
}
if (this.activeResizeElement) {
this.initPosition(event);
} else {
this.reset();
}
return [];
}
mouseMove(target: SModelElement, event: MouseEvent): Action[] {
super.mouseMove(target, event);
if (this.isMouseDrag && this.activeResizeHandle) {
// rely on the FeedbackMoveMouseListener to update the element bounds of selected elements
// consider resize handles ourselves
const actions: Action[] = [cursorFeedbackAction(CursorCSS.RESIZE), applyCssClasses(this.activeResizeHandle, ChangeBoundsListener.CSS_CLASS_ACTIVE)];
const positionUpdate = this.updatePosition(target, event);
if (positionUpdate) {
const resizeActions = this.handleResizeOnClient(positionUpdate);
actions.push(...resizeActions);
}
return actions;
}
return [];
}
draggingMouseUp(target: SModelElement, event: MouseEvent): Action[] {
if (this.lastDragPosition === undefined) {
this.resetPosition();
return [];
}
const actions: Action[] = [];
if (this.activeResizeHandle) {
// Resize
actions.push(...this.handleResize(this.activeResizeHandle));
} else {
// Move
actions.push(...this.handleMoveOnServer(target));
}
this.resetPosition();
return actions;
}
protected handleMoveOnServer(target: SModelElement): Action[] {
const operations: Operation[] = [];
operations.push(...this.handleMoveElementsOnServer(target));
operations.push(...this.handleMoveRoutingPointsOnServer(target));
if (operations.length > 0) {
return [new CompoundOperation(operations)];
}
return operations;
}
protected handleMoveElementsOnServer(target: SModelElement): Action[] {
const result: Operation[] = [];
const newBounds: ElementAndBounds[] = [];
forEachElement(target, isNonRoutableSelectedMovableBoundsAware, element => {
this.createElementAndBounds(element).forEach(bounds => newBounds.push(bounds));
});
if (newBounds.length > 0) {
result.push(new ChangeBoundsOperation(newBounds));
}
return result;
}
protected handleMoveRoutingPointsOnServer(target: SModelElement): Action[] {
const result: Operation[] = [];
const newRoutingPoints: ElementAndRoutingPoints[] = [];
forEachElement(target, isNonRoutableSelectedMovableBoundsAware, element => {
// If client routing is enabled -> delegate routingpoints of connected edges to server
if (this.tool.edgeRouterRegistry && element instanceof SConnectableElement) {
element.incomingEdges.map(toElementAndRoutingPoints).forEach(ear => newRoutingPoints.push(ear));
element.outgoingEdges.map(toElementAndRoutingPoints).forEach(ear => newRoutingPoints.push(ear));
}
});
if (newRoutingPoints.length > 0) {
result.push(new ChangeRoutingPointsOperation(newRoutingPoints));
}
return result;
}
protected handleResize(activeResizeHandle: SResizeHandle): Action[] {
const actions: Action[] = [];
actions.push(cursorFeedbackAction(CursorCSS.DEFAULT));
actions.push(deleteCssClasses(activeResizeHandle, ChangeBoundsListener.CSS_CLASS_ACTIVE));
const resizeElement = findParentByFeature(activeResizeHandle, isResizable);
if (this.isActiveResizeElement(resizeElement)) {
this.createChangeBoundsAction(resizeElement).forEach(action => actions.push(action));
}
return actions;
}
selectionChanged(root: SModelRoot, selectedElements: string[]): void {
if (this.activeResizeElement) {
if (selectedElements.includes(this.activeResizeElement.id)) {
// our active element is still selected, nothing to do
return;
}
// try to find some other selected element and mark that active
for (const elementId of selectedElements.reverse()) {
const element = root.index.getById(elementId);
if (element && this.setActiveResizeElement(element)) {
return;
}
}
this.reset();
}
}
protected setActiveResizeElement(target: SModelElement): boolean {
// check if we have a selected, moveable element (multi-selection allowed)
const moveableElement = findParentByFeature(target, isBoundsAwareMoveable);
if (isSelected(moveableElement)) {
// only allow one element to have the element resize handles
this.activeResizeElement = moveableElement;
if (isResizable(this.activeResizeElement)) {
this.tool.dispatchFeedback([new ShowChangeBoundsToolResizeFeedbackAction(this.activeResizeElement.id)], this);
}
return true;
}
return false;
}
protected isActiveResizeElement(element?: SModelElement): element is SParentElement & BoundsAware {
return element !== undefined && this.activeResizeElement !== undefined && element.id === this.activeResizeElement.id;
}
protected initPosition(event: MouseEvent) {
this.lastDragPosition = { x: event.pageX, y: event.pageY };
if (this.activeResizeHandle) {
const resizeElement = findParentByFeature(this.activeResizeHandle, isResizable);
this.initialBounds = { x: resizeElement!.bounds.x, y: resizeElement!.bounds.y, width: resizeElement!.bounds.width, height: resizeElement!.bounds.height };
}
}
protected updatePosition(target: SModelElement, event: MouseEvent): Point | undefined {
if (this.lastDragPosition) {
const newDragPosition = { x: event.pageX, y: event.pageY };
const viewport = findParentByFeature(target, isViewport);
const zoom = viewport ? viewport.zoom : 1;
const dx = (event.pageX - this.lastDragPosition.x) / zoom;
const dy = (event.pageY - this.lastDragPosition.y) / zoom;
const deltaToLastPosition = { x: dx, y: dy };
this.lastDragPosition = newDragPosition;
// update position delta with latest delta
this.positionDelta.x += deltaToLastPosition.x;
this.positionDelta.y += deltaToLastPosition.y;
// snap our delta and only send update if the position actually changes
// otherwise accumulate delta until we do snap to an update
const positionUpdate = this.snap(this.positionDelta, target, !event.shiftKey);
if (positionUpdate.x === 0 && positionUpdate.y === 0) {
return undefined;
}
// we update our position so we need to reset our delta
this.positionDelta.x = 0;
this.positionDelta.y = 0;
return positionUpdate;
}
return undefined;
}
protected reset() {
if (this.activeResizeElement && isResizable(this.activeResizeElement)) {
this.tool.dispatchFeedback([new HideChangeBoundsToolResizeFeedbackAction()], this);
}
this.tool.dispatchActions([cursorFeedbackAction(CursorCSS.DEFAULT)]);
this.resetPosition();
}
protected resetPosition() {
this.activeResizeHandle = undefined;
this.lastDragPosition = undefined;
this.positionDelta = { x: 0, y: 0 };
}
protected handleResizeOnClient(positionUpdate: Point): Action[] {
if (!this.activeResizeHandle) {
return [];
}
const resizeElement = findParentByFeature(this.activeResizeHandle, isResizable);
if (this.isActiveResizeElement(resizeElement)) {
switch (this.activeResizeHandle.location) {
case ResizeHandleLocation.TopLeft:
return this.handleTopLeftResize(resizeElement, positionUpdate);
case ResizeHandleLocation.TopRight:
return this.handleTopRightResize(resizeElement, positionUpdate);
case ResizeHandleLocation.BottomLeft:
return this.handleBottomLeftResize(resizeElement, positionUpdate);
case ResizeHandleLocation.BottomRight:
return this.handleBottomRightResize(resizeElement, positionUpdate);
}
}
return [];
}
protected handleTopLeftResize(resizeElement: SParentElement & Resizable, positionUpdate: Point): Action[] {
return this.createSetBoundsAction(resizeElement,
resizeElement.bounds.x + positionUpdate.x,
resizeElement.bounds.y + positionUpdate.y,
resizeElement.bounds.width - positionUpdate.x,
resizeElement.bounds.height - positionUpdate.y);
}
protected handleTopRightResize(resizeElement: SParentElement & Resizable, positionUpdate: Point): Action[] {
return this.createSetBoundsAction(resizeElement,
resizeElement.bounds.x,
resizeElement.bounds.y + positionUpdate.y,
resizeElement.bounds.width + positionUpdate.x,
resizeElement.bounds.height - positionUpdate.y);
}
protected handleBottomLeftResize(resizeElement: SParentElement & Resizable, positionUpdate: Point): Action[] {
return this.createSetBoundsAction(resizeElement,
resizeElement.bounds.x + positionUpdate.x,
resizeElement.bounds.y,
resizeElement.bounds.width - positionUpdate.x,
resizeElement.bounds.height + positionUpdate.y);
}
protected handleBottomRightResize(resizeElement: SParentElement & Resizable, positionUpdate: Point): Action[] {
return this.createSetBoundsAction(resizeElement,
resizeElement.bounds.x,
resizeElement.bounds.y,
resizeElement.bounds.width + positionUpdate.x,
resizeElement.bounds.height + positionUpdate.y);
}
protected createChangeBoundsAction(element: SModelElement & BoundsAware): Action[] {
if (this.isValidBoundChange(element, element.bounds, element.bounds)) {
return [new ChangeBoundsOperation([toElementAndBounds(element)])];
} else if (this.initialBounds) {
const actions: Action[] = [];
if (this.tool.movementRestrictor) {
actions.push(removeMovementRestrictionFeedback(element, this.tool.movementRestrictor));
}
actions.push(new SetBoundsAction([{ elementId: element.id, newPosition: this.initialBounds, newSize: this.initialBounds }]));
return actions;
}
return [];
}
protected createElementAndBounds(element: SModelElement & BoundsAware): ElementAndBounds[] {
if (this.isValidBoundChange(element, element.bounds, element.bounds)) {
return [toElementAndBounds(element)];
}
return [];
}
protected createSetBoundsAction(element: SModelElement & BoundsAware, x: number, y: number, width: number, height: number): Action[] {
const newPosition = { x, y };
const newSize = { width, height };
const result: Action[] = [];
if (this.isValidBoundChange(element, newPosition, newSize)) {
if (this.tool.movementRestrictor) {
result.push(removeMovementRestrictionFeedback(element, this.tool.movementRestrictor));
}
result.push(new SetBoundsAction([{ elementId: element.id, newPosition, newSize }]));
} else if (this.isValidSize(element, newSize)) {
if (this.tool.movementRestrictor) {
result.push(createMovementRestrictionFeedback(element, this.tool.movementRestrictor));
}
result.push(new SetBoundsAction([{ elementId: element.id, newPosition, newSize }]));
}
return result;
}
protected snap(position: Point, element: SModelElement, isSnap: boolean): Point {
return isSnap && this.tool.snapper
? this.tool.snapper.snap(position, element)
: { x: position.x, y: position.y };
}
protected isValidBoundChange(element: SModelElement & BoundsAware, newPosition: Point, newSize: Dimension): boolean {
return this.isValidSize(element, newSize) && this.isValidMove(element, newPosition);
}
protected isValidSize(element: SModelElement & BoundsAware, size: Dimension) {
return isValidSize(element, size);
}
protected isValidMove(element: SModelElement & BoundsAware, newPosition: Point) {
return isValidMove(element, newPosition, this.tool.movementRestrictor);
}
}