Skip to content

Commit

Permalink
Ensure selection forwarder restores selection of active GLSP widget
Browse files Browse the repository at this point in the history
- Improve customizability of GlspSelectionDataService
  • Loading branch information
martin-fleck-at committed Dec 3, 2023
1 parent 969047f commit 4701f1f
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import {
hasStringProp
} from '@eclipse-glsp/client';
import { SelectionService } from '@theia/core';
import { inject, injectable, optional } from '@theia/core/shared/inversify';
import { ApplicationShell } from '@theia/core/lib/browser';
import { inject, injectable, optional, postConstruct } from '@theia/core/shared/inversify';
import { getDiagramWidget } from '../../glsp-diagram-widget';

export interface GlspSelection {
additionalSelectionData?: GlspSelectionData;
Expand Down Expand Up @@ -61,7 +63,7 @@ export interface GlspSelectionData {
*/
@injectable()
export abstract class GlspSelectionDataService {
abstract getSelectionData(selectedElementIds: string[]): Promise<GlspSelectionData>;
abstract getSelectionData(root: Readonly<GModelRoot>, selectedElementIds: string[]): Promise<GlspSelectionData>;
}

/**
Expand All @@ -85,8 +87,25 @@ export class TheiaGLSPSelectionForwarder implements ISelectionListener {
@inject(TYPES.ModelSourceProvider)
protected modelSourceProvider: () => Promise<ModelSource>;

@inject(ApplicationShell)
protected shell: ApplicationShell;

protected sourceUri?: string;

@postConstruct()
protected init(): void {
this.shell.onDidChangeActiveWidget(() => {
const activeDiagramWidget = getDiagramWidget(this.shell);
if (activeDiagramWidget) {
// restore selection from diagram to the global scope
this.selectionChanged(
activeDiagramWidget.editorContext.modelRoot,
activeDiagramWidget.editorContext.selectedElements.map(element => element.id)
);
}
});
}

protected async getSourceUri(): Promise<string | undefined> {
if (!this.sourceUri) {
const modelSource = await this.modelSourceProvider();
Expand All @@ -97,13 +116,13 @@ export class TheiaGLSPSelectionForwarder implements ISelectionListener {
return this.sourceUri;
}

selectionChanged(_root: Readonly<GModelRoot>, selectedElements: string[]): void {
this.handleSelectionChanged(selectedElements);
selectionChanged(root: Readonly<GModelRoot>, selectedElements: string[]): void {
this.handleSelectionChanged(root, selectedElements);
}

async handleSelectionChanged(selectedElementsIDs: string[]): Promise<void> {
async handleSelectionChanged(root: Readonly<GModelRoot>, selectedElementsIDs: string[]): Promise<void> {
const sourceUri = await this.getSourceUri();
const additionalSelectionData = (await this.selectionDataService?.getSelectionData(selectedElementsIDs)) ?? undefined;
const additionalSelectionData = (await this.selectionDataService?.getSelectionData(root, selectedElementsIDs)) ?? undefined;
const glspSelection: GlspSelection = {
selectedElementsIDs,
additionalSelectionData,
Expand Down

0 comments on commit 4701f1f

Please sign in to comment.