Skip to content

Commit

Permalink
GLSP-1393 Align Interface usage across *Manager classes (#388)
Browse files Browse the repository at this point in the history
- Align interface + symbol usage for *Manager classes
  - `ChangeBoundsManager`, `GridManager`, `DebugManager`
- Use bindAsService to ensure backward compatibility
- Update injects to use the interface instead of the actual manager class

Resolves eclipse-glsp/glsp#1393
  • Loading branch information
ndoschek authored Sep 9, 2024
1 parent 4894f72 commit b01477a
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 48 deletions.
6 changes: 3 additions & 3 deletions examples/workflow-glsp/src/workflow-startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { GridManager, IDiagramStartup } from '@eclipse-glsp/client';
import { MaybePromise } from '@eclipse-glsp/sprotty';
import { IDiagramStartup, IGridManager } from '@eclipse-glsp/client';
import { MaybePromise, TYPES } from '@eclipse-glsp/sprotty';
import { inject, injectable, optional } from 'inversify';

@injectable()
export class WorkflowStartup implements IDiagramStartup {
rank = -1;

@inject(GridManager) @optional() protected gridManager?: GridManager;
@inject(TYPES.IGridManager) @optional() protected gridManager?: IGridManager;

preRequestModel(): MaybePromise<void> {
this.gridManager?.setGridVisible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { GLSPActionDispatcher } from '../../../base/action-dispatcher';
import { SelectionService } from '../../../base/selection-service';
import { Tool } from '../../../base/tool-manager/tool';
import { Grid } from '../../grid/grid';
import { ChangeBoundsManager } from '../../tools/change-bounds/change-bounds-manager';
import { IChangeBoundsManager } from '../../tools/change-bounds/change-bounds-manager';
import { AccessibleKeyShortcutProvider, SetAccessibleKeyShortcutAction } from '../key-shortcut/accessible-key-shortcut';
import { MoveElementAction, MoveViewportAction } from '../move-zoom/move-handler';

Expand All @@ -41,7 +41,7 @@ export class MovementKeyTool implements Tool {
@inject(TYPES.ISnapper) @optional() readonly snapper?: ISnapper;
@inject(TYPES.IActionDispatcher) readonly actionDispatcher: GLSPActionDispatcher;
@inject(TYPES.Grid) @optional() protected grid: Grid;
@inject(ChangeBoundsManager) readonly changeBoundsManager: ChangeBoundsManager;
@inject(TYPES.IChangeBoundsManager) readonly changeBoundsManager: IChangeBoundsManager;

get id(): string {
return MovementKeyTool.ID;
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/features/debug/debug-bounds-decorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
********************************************************************************/
/* eslint-disable max-len */
/** @jsx svg */
import { Bounds, GModelElement, IVNodePostprocessor, Point, isDecoration, isSizeable, setClass, svg } from '@eclipse-glsp/sprotty';
import { Bounds, GModelElement, IVNodePostprocessor, Point, TYPES, isDecoration, isSizeable, setClass, svg } from '@eclipse-glsp/sprotty';
import { inject, injectable, optional } from 'inversify';
import { VNode } from 'snabbdom';
import { GGraph } from '../../model';
import { BoundsAwareModelElement } from '../../utils/gmodel-util';
import { DebugManager } from './debug-manager';
import { IDebugManager } from './debug-manager';

export const CSS_DEBUG_BOUNDS = 'debug-bounds';

@injectable()
export class DebugBoundsDecorator implements IVNodePostprocessor {
@inject(DebugManager) @optional() protected debugManager?: DebugManager;
@inject(TYPES.IDebugManager) @optional() protected debugManager?: IDebugManager;

decorate(vnode: VNode, element: GModelElement): VNode {
if (!this.debugManager?.isDebugEnabled) {
Expand Down
15 changes: 14 additions & 1 deletion packages/client/src/features/debug/debug-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@ import { IFeedbackActionDispatcher } from '../../base/feedback/feedback-action-d
import { FeedbackEmitter } from '../../base/feedback/feedback-emitter';
import { EnableDebugModeAction } from './debug-model';

export interface IDebugManager {
/** Flag to indicate whether the debug mode is enabled. */
readonly isDebugEnabled: boolean;
/** Sets the debug enabled state. */
setDebugEnabled(enabled: boolean): void;
/** Toggles the debug enabled state. */
toggleDebugEnabled(): void;
}

/**
* The default {@link IDebugManager} implementation.
* This class manages the debug mode and provides functionality to enable or disable it.
*/
@injectable()
export class DebugManager implements IActionHandler {
export class DebugManager implements IActionHandler, IDebugManager {
protected _debugEnabled: boolean = false;
protected debugFeedback: FeedbackEmitter;

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/features/debug/debug-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const debugModule = new FeatureModule(

configureCommand(context, EnableDebugModeCommand);

bind(DebugManager).toSelf().inSingletonScope();
bindAsService(context, TYPES.IDebugManager, DebugManager);
configureActionHandler(context, EnableDebugModeAction.KIND, DebugManager);

bindAsService(context, TYPES.IVNodePostprocessor, DebugBoundsDecorator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import { FeedbackEmitter } from '../../base/feedback/feedback-emitter';
import { MoveableElement } from '../../utils/gmodel-util';
import { getAbsolutePosition } from '../../utils/viewpoint-util';
import { FeedbackAwareTool } from '../tools/base-tools';
import { ChangeBoundsManager } from '../tools/change-bounds/change-bounds-manager';
import { IChangeBoundsManager } from '../tools/change-bounds/change-bounds-manager';
import { MoveFinishedEventAction } from '../tools/change-bounds/change-bounds-tool-feedback';
import { ChangeBoundsTracker, TrackedMove } from '../tools/change-bounds/change-bounds-tracker';

export interface PositioningTool extends FeedbackAwareTool {
readonly changeBoundsManager: ChangeBoundsManager;
readonly changeBoundsManager: IChangeBoundsManager;
}

export class MouseTrackingElementPositionListener extends DragAwareMouseListener {
Expand Down
17 changes: 16 additions & 1 deletion packages/client/src/features/grid/grid-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,23 @@ import { ShowGridAction } from './grid-model';

export type GridStyle = Record<string, string> & Partial<PropertiesOfType<CSSStyleDeclaration, string>>;

export interface IGridManager {
/** The grid to manage. */
readonly grid: Grid;
/** Flag to indicate whether the grid is visible. */
readonly isGridVisible: boolean;
/** Sets the visibility of the grid. */
setGridVisible(visible: boolean): void;
/** Toggles the visibility of the grid. */
toggleGridVisible(): void;
}

/**
* The default {@link IGridManager} implementation.
* This class manages the visibility and behavior of a grid in the application.
*/
@injectable()
export class GridManager implements IActionHandler {
export class GridManager implements IActionHandler, IGridManager {
protected _gridVisible: boolean = false;
protected gridFeedback: FeedbackEmitter;

Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/features/grid/grid-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { FeatureModule, TYPES, configureActionHandler, configureCommand } from '@eclipse-glsp/sprotty';
import { FeatureModule, TYPES, bindAsService, configureActionHandler, configureCommand } from '@eclipse-glsp/sprotty';
import '../../../css/grid.css';
import { GridManager } from './grid-manager';
import { ShowGridAction, ShowGridCommand } from './grid-model';
Expand All @@ -28,7 +28,7 @@ export const gridModule = new FeatureModule(

configureCommand(context, ShowGridCommand);

bind(GridManager).toSelf().inSingletonScope();
bindAsService(context, TYPES.IGridManager, GridManager);
configureActionHandler(context, ShowGridAction.KIND, GridManager);

bind(TYPES.ISnapper).to(GridSnapper);
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/features/select/select-mouse-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { Action, BringToFrontAction, GModelElement, SelectAction, SelectMouseListener } from '@eclipse-glsp/sprotty';
import { Action, BringToFrontAction, GModelElement, SelectAction, SelectMouseListener, TYPES } from '@eclipse-glsp/sprotty';
import { inject, injectable, optional } from 'inversify';
import { Ranked } from '../../base/ranked';
import { SelectableElement } from '../../utils/gmodel-util';
import { GResizeHandle } from '../change-bounds/model';
import { ChangeBoundsManager } from '../tools/change-bounds/change-bounds-manager';
import { IChangeBoundsManager } from '../tools/change-bounds/change-bounds-manager';

/**
* Ranked select mouse listener that is executed before default mouse listeners when using the RankedMouseTool.
Expand All @@ -28,7 +28,7 @@ import { ChangeBoundsManager } from '../tools/change-bounds/change-bounds-manage
export class RankedSelectMouseListener extends SelectMouseListener implements Ranked {
rank: number = Ranked.DEFAULT_RANK - 100; /* we want to be executed before all default mouse listeners */

@inject(ChangeBoundsManager) @optional() readonly changeBoundsManager?: ChangeBoundsManager;
@inject(TYPES.IChangeBoundsManager) @optional() readonly changeBoundsManager?: IChangeBoundsManager;

protected override handleSelectTarget(
selectableTarget: SelectableElement,
Expand Down
13 changes: 7 additions & 6 deletions packages/client/src/features/tool-palette/tool-palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
SetContextActions,
SetModelAction,
SetUIExtensionVisibilityAction,
TYPES,
TriggerNodeCreationAction,
UpdateModelAction,
codiconCSSClasses,
Expand All @@ -37,8 +38,8 @@ import { FocusTracker } from '../../base/focus/focus-tracker';
import { IDiagramStartup } from '../../base/model/diagram-loader';
import { EnableDefaultToolsAction, EnableToolsAction } from '../../base/tool-manager/tool';
import { GLSPAbstractUIExtension } from '../../base/ui-extension/ui-extension';
import { DebugManager } from '../debug/debug-manager';
import { GridManager } from '../grid/grid-manager';
import { IDebugManager } from '../debug/debug-manager';
import { IGridManager } from '../grid/grid-manager';
import { MouseDeleteTool } from '../tools/deletion/delete-tool';
import { MarqueeMouseTool } from '../tools/marquee-selection/marquee-mouse-tool';
import { OriginViewportAction } from '../viewport/origin-viewport';
Expand Down Expand Up @@ -77,13 +78,13 @@ export class ToolPalette extends GLSPAbstractUIExtension implements IActionHandl
@inject(FocusTracker)
protected focusTracker: FocusTracker;

@inject(GridManager)
@inject(TYPES.IGridManager)
@optional()
protected gridManager?: GridManager;
protected gridManager?: IGridManager;

@inject(DebugManager)
@inject(TYPES.IDebugManager)
@optional()
protected debugManager?: DebugManager;
protected debugManager?: IDebugManager;

protected paletteItems: PaletteItem[];
protected paletteItemsCopy: PaletteItem[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
movementRestrictionFeedback,
removeMovementRestrictionFeedback
} from '../../change-bounds/movement-restrictor';
import { GridManager } from '../../grid/grid-manager';
import { IGridManager } from '../../grid/grid-manager';
import { IHelperLineManager } from '../../helper-lines/helper-line-manager';
import { InsertIndicator } from '../node-creation/insert-indicator';
import { ChangeBoundsTracker, TrackedElementMove, TrackedElementResize, TrackedMove, TrackedResize } from './change-bounds-tracker';
Expand All @@ -55,14 +55,140 @@ export const CSS_RESIZE_MODE = 'resize-mode';
export const CSS_RESTRICTED_RESIZE = 'resize-not-allowed';
export const CSS_ACTIVE_HANDLE = 'active';

export interface IChangeBoundsManager {
/**
* Unsnap the modifier used for changing bounds.
* @returns The unsnapped keyboard modifier, or undefined if no modifier was snapped.
*/
unsnapModifier(): KeyboardModifier | undefined;

/**
* Determine whether to use position snap for changing bounds.
* @param arg - The event argument.
* @returns True if position snap should be used, false otherwise.
*/
usePositionSnap(arg: MouseEvent | KeyboardEvent | any): boolean;

/**
* Snap the position of an element.
* @param element - The element to snap.
* @param position - The position to snap.
* @returns The snapped position.
*/
snapPosition(element: GModelElement, position: Point): Point;

/**
* Check if an element is valid for changing bounds.
* @param element - The element to check.
* @returns True if the element is valid, false otherwise.
*/
isValid(element: GModelElement): boolean;

/**
* Check if an element has a valid position for changing bounds.
* @param element - The element to check.
* @param position - The position to check.
* @returns True if the element has a valid position, false otherwise.
*/
hasValidPosition(element: GModelElement, position?: Point): boolean;

/**
* Check if an element has a valid size for changing bounds.
* @param element - The element to check.
* @param size - The size to check.
* @returns True if the element has a valid size, false otherwise.
*/
hasValidSize(element: GModelElement, size?: Dimension): boolean;

/**
* Get the minimum size of an element for changing bounds.
* @param element - The element to get the minimum size for.
* @returns The minimum size of the element.
*/
getMinimumSize(element: GModelElement): Dimension;

/**
* Determine whether to use movement restriction for changing bounds.
* @param arg - The event argument.
* @returns True if movement restriction should be used, false otherwise.
*/
useMovementRestriction(arg: MouseEvent | KeyboardEvent | any): boolean;

/**
* Restrict the movement of an element.
* @param element - The element to restrict movement for.
* @param movement - The movement to restrict.
* @returns The restricted movement.
*/
restrictMovement(element: GModelElement, movement: Movement): Movement;

/**
* Add move feedback for changing bounds.
* @param feedback - The feedback emitter.
* @param trackedMove - The tracked move.
* @param ctx - The context element. (optional)
* @param event - The mouse event. (optional)
* @returns The feedback emitter.
*/
addMoveFeedback(feedback: FeedbackEmitter, trackedMove: TrackedMove, ctx?: GModelElement, event?: MouseEvent): FeedbackEmitter;

/**
* Add resize feedback for changing bounds.
* @param feedback - The feedback emitter.
* @param resize - The tracked resize.
* @param ctx - The context element. (optional)
* @param event - The mouse event. (optional)
* @returns The feedback emitter.
*/
addResizeFeedback(feedback: FeedbackEmitter, resize: TrackedResize, ctx?: GModelElement, event?: MouseEvent): FeedbackEmitter;

/**
* Add move restriction feedback for changing bounds.
* @param feedback - The feedback emitter.
* @param change - The tracked element resize or move.
* @param ctx - The context element. (optional)
* @param event - The mouse event. (optional)
* @returns The feedback emitter.
*/
addMoveRestrictionFeedback(
feedback: FeedbackEmitter,
change: TrackedElementResize | TrackedElementMove,
ctx?: GModelElement,
event?: MouseEvent
): FeedbackEmitter;

/**
* Get the default resize locations for changing bounds.
* @returns The default resize handle locations.
*/
defaultResizeLocations(): ResizeHandleLocation[];

/**
* Determine whether to use symmetric resize for changing bounds.
* @param arg - The event argument.
* @returns True if symmetric resize should be used, false otherwise.
*/
useSymmetricResize(arg: MouseEvent | KeyboardEvent | any): boolean;

/**
* Create a tracker for changing bounds.
* @returns The change bounds tracker.
*/
createTracker(): ChangeBoundsTracker;
}

/**
* The default {@link IChangeBoundsManager} implementation. It is responsible for managing
* the change of bounds for {@link GModelElement}s.
*/
@injectable()
export class ChangeBoundsManager {
export class ChangeBoundsManager implements IChangeBoundsManager {
constructor(
@inject(MousePositionTracker) readonly positionTracker: MousePositionTracker,
@optional() @inject(TYPES.IMovementRestrictor) readonly movementRestrictor?: IMovementRestrictor,
@optional() @inject(TYPES.ISnapper) readonly snapper?: ISnapper,
@optional() @inject(TYPES.IHelperLineManager) readonly helperLineManager?: IHelperLineManager,
@optional() @inject(GridManager) protected gridManager?: GridManager
@optional() @inject(TYPES.IGridManager) protected gridManager?: IGridManager
) {}

unsnapModifier(): KeyboardModifier | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { FeedbackCommand } from '../../../base/feedback/feedback-command';
import { OptionalAction } from '../../../base/model/glsp-model-source';
import { forEachElement } from '../../../utils/gmodel-util';
import { ResizeHandleLocation, addResizeHandles, isResizable, removeResizeHandles } from '../../change-bounds/model';
import { ChangeBoundsManager } from './change-bounds-manager';
import { IChangeBoundsManager } from './change-bounds-manager';

export interface ShowChangeBoundsToolResizeFeedbackAction extends Action {
kind: typeof ShowChangeBoundsToolResizeFeedbackAction.KIND;
Expand Down Expand Up @@ -71,7 +71,7 @@ export class ShowChangeBoundsToolResizeFeedbackCommand extends FeedbackCommand {
static readonly KIND = ShowChangeBoundsToolResizeFeedbackAction.KIND;

@inject(TYPES.Action) protected action: ShowChangeBoundsToolResizeFeedbackAction;
@inject(ChangeBoundsManager) protected changeBoundsManager: ChangeBoundsManager;
@inject(TYPES.IChangeBoundsManager) protected changeBoundsManager: IChangeBoundsManager;

execute(context: CommandExecutionContext): CommandReturn {
const index = context.root.index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { GResizeHandleView } from './view';
export const changeBoundsToolModule = new FeatureModule(
(bind, unbind, isBound, rebind) => {
const context = { bind, unbind, isBound, rebind };
bind(ChangeBoundsManager).toSelf().inSingletonScope();
bindAsService(context, TYPES.IChangeBoundsManager, ChangeBoundsManager);
bindAsService(context, TYPES.IDefaultTool, ChangeBoundsTool);
configureCommand(context, ShowChangeBoundsToolResizeFeedbackCommand);
configureCommand(context, HideChangeBoundsToolResizeFeedbackCommand);
Expand Down
Loading

0 comments on commit b01477a

Please sign in to comment.