Skip to content

Commit

Permalink
fix eclipse-theia#7170: default to last visible editor if no last foc…
Browse files Browse the repository at this point in the history
…used editor

Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed May 4, 2020
1 parent dcb9c9c commit 57eb460
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 25 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/browser/shell/application-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,15 @@ export class ApplicationShell extends Widget {
return [...this.tracker.widgets];
}

getWidgetById(id: string): Widget | undefined {
for (const widget of this.tracker.widgets) {
if (widget.id === id) {
return widget;
}
}
return undefined;
}

canToggleMaximized(): boolean {
const area = this.currentWidget && this.getAreaFor(this.currentWidget);
return area === 'main' || area === 'bottom';
Expand Down
37 changes: 35 additions & 2 deletions packages/editor/src/browser/editor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,40 @@ export class EditorManager extends NavigatableWidgetOpenHandler<EditorWidget> {
super.init();
this.shell.activeChanged.connect(() => this.updateActiveEditor());
this.shell.currentChanged.connect(() => this.updateCurrentEditor());
this.onCreated(widget => widget.disposed.connect(() => this.updateCurrentEditor()));
this.onCreated(widget => {
widget.onDidChangeVisibility(() => {
if (widget.isVisible) {
this.addRecentlyVisible(widget);
this.updateCurrentEditor();
}
});
widget.disposed.connect(() => {
this.removeRecentlyVisible(widget);
this.updateCurrentEditor();
});
});
for (const widget of this.all) {
if (widget.isVisible) {
this.addRecentlyVisible(widget);
}
}
this.updateCurrentEditor();
}

protected readonly recentlyVisibleIds: string[] = [];
protected get recentlyVisible(): EditorWidget | undefined {
const id = this.recentlyVisibleIds[0];
return id && this.all.find(w => w.id === id) || undefined;
}
protected addRecentlyVisible(widget: EditorWidget): void {
this.removeRecentlyVisible(widget);
this.recentlyVisibleIds.unshift(widget.id);
}
protected removeRecentlyVisible(widget: EditorWidget): void {
const index = this.recentlyVisibleIds.indexOf(widget.id);
if (index !== -1) {
this.recentlyVisibleIds.splice(index, 1);
}
}

protected _activeEditor: EditorWidget | undefined;
Expand Down Expand Up @@ -93,7 +126,7 @@ export class EditorManager extends NavigatableWidgetOpenHandler<EditorWidget> {
if (widget instanceof EditorWidget) {
this.setCurrentEditor(widget);
} else if (!this._currentEditor || !this._currentEditor.isVisible) {
this.setCurrentEditor(undefined);
this.setCurrentEditor(this.recentlyVisible);
}
}

Expand Down
11 changes: 8 additions & 3 deletions packages/markers/src/browser/marker-tree-label-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ let disableJSDOM = enableJSDOM();
import URI from '@theia/core/lib/common/uri';
import { expect } from 'chai';
import { Container } from 'inversify';
import { ContributionProvider } from '@theia/core/lib/common';
import { ContributionProvider, Event } from '@theia/core/lib/common';
import { FileStat, FileSystem } from '@theia/filesystem/lib/common';
import { LabelProvider, LabelProviderContribution, DefaultUriLabelProviderContribution, ApplicationShell } from '@theia/core/lib/browser';
import { LabelProvider, LabelProviderContribution, DefaultUriLabelProviderContribution, ApplicationShell, WidgetManager } from '@theia/core/lib/browser';
import { MarkerInfoNode } from './marker-tree';
import { MarkerTreeLabelProvider } from './marker-tree-label-provider';
import { Signal } from '@phosphor/signaling';
Expand All @@ -46,7 +46,12 @@ before(() => {
testContainer.bind(WorkspaceService).toConstantValue(workspaceService);
testContainer.bind(WorkspaceVariableContribution).toSelf().inSingletonScope();
testContainer.bind(ApplicationShell).toConstantValue({
currentChanged: new Signal({})
currentChanged: new Signal({}),
widgets: () => []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
testContainer.bind(WidgetManager).toConstantValue({
onDidCreateWidget: Event.None
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
testContainer.bind(FileSystem).to(MockFilesystem).inSingletonScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { expect } from 'chai';
import * as sinon from 'sinon';
import { Container } from 'inversify';
import { Signal } from '@phosphor/signaling';
import { ApplicationShell } from '@theia/core/lib/browser';
import { Event } from '@theia/core/lib/common/event';
import { ApplicationShell, WidgetManager } from '@theia/core/lib/browser';
import { FileStat, FileSystem } from '@theia/filesystem/lib/common/filesystem';
import { MockFilesystem } from '@theia/filesystem/lib/common/test';
import { DefaultUriLabelProviderContribution } from '@theia/core/lib/browser/label-provider';
Expand All @@ -44,7 +45,12 @@ beforeEach(() => {

container = new Container();
container.bind(ApplicationShell).toConstantValue({
currentChanged: new Signal({})
currentChanged: new Signal({}),
widgets: () => []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
container.bind(WidgetManager).toConstantValue({
onDidCreateWidget: Event.None
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
const workspaceService = new WorkspaceService();
Expand Down
65 changes: 47 additions & 18 deletions packages/workspace/src/browser/workspace-variable-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import { injectable, inject, postConstruct } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { Path } from '@theia/core/lib/common/path';
import { FileSystem } from '@theia/filesystem/lib/common';
import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
import { ApplicationShell, NavigatableWidget } from '@theia/core/lib/browser';
import { ApplicationShell, NavigatableWidget, WidgetManager } from '@theia/core/lib/browser';
import { VariableContribution, VariableRegistry, Variable } from '@theia/variable-resolver/lib/browser';
import { WorkspaceService } from './workspace-service';

Expand All @@ -32,31 +31,62 @@ export class WorkspaceVariableContribution implements VariableContribution {
protected readonly shell: ApplicationShell;
@inject(FileSystem)
protected readonly fileSystem: FileSystem;
@inject(WidgetManager)
protected readonly widgetManager: WidgetManager;

protected currentWidget: NavigatableWidget | undefined;

@postConstruct()
protected init(): void {
this.updateCurrentWidget();
this.shell.currentChanged.connect(() => this.updateCurrentWidget());
this.widgetManager.onDidCreateWidget(({ widget }) => {
if (NavigatableWidget.is(widget)) {
widget.onDidChangeVisibility(() => {
if (widget.isVisible) {
this.addRecentlyVisible(widget);
this.updateCurrentWidget();
}
});
widget.onDidDispose(() => {
this.removeRecentlyVisible(widget);
this.updateCurrentWidget();
});
}
});
for (const widget of this.shell.widgets) {
if (NavigatableWidget.is(widget) && widget.isVisible) {
this.addRecentlyVisible(widget);
}
}
this.updateCurrentWidget();
}
protected updateCurrentWidget(): void {
const { currentWidget } = this.shell;
if (NavigatableWidget.is(currentWidget)) {
this.setCurrentWidget(currentWidget);

protected readonly recentlyVisibleIds: string[] = [];
protected get recentlyVisible(): NavigatableWidget | undefined {
const id = this.recentlyVisibleIds[0];
const widget = id && this.shell.getWidgetById(id) || undefined;
if (NavigatableWidget.is(widget)) {
return widget;
}
return undefined;
}
protected addRecentlyVisible(widget: NavigatableWidget): void {
this.removeRecentlyVisible(widget);
this.recentlyVisibleIds.unshift(widget.id);
}
protected removeRecentlyVisible(widget: NavigatableWidget): void {
const index = this.recentlyVisibleIds.indexOf(widget.id);
if (index !== -1) {
this.recentlyVisibleIds.splice(index, 1);
}
}

protected readonly toDisposeOnUpdateCurrentWidget = new DisposableCollection();
protected setCurrentWidget(currentWidget: NavigatableWidget | undefined): void {
this.toDisposeOnUpdateCurrentWidget.dispose();
this.currentWidget = currentWidget;
if (currentWidget) {
const resetCurrentWidget = () => this.setCurrentWidget(undefined);
currentWidget.disposed.connect(resetCurrentWidget);
this.toDisposeOnUpdateCurrentWidget.push(Disposable.create(() =>
currentWidget.disposed.disconnect(resetCurrentWidget)
));
protected updateCurrentWidget(): void {
const { currentWidget } = this.shell;
if (NavigatableWidget.is(currentWidget)) {
this.currentWidget = currentWidget;
} else if (!this.currentWidget || !this.currentWidget.isVisible) {
this.currentWidget = this.recentlyVisible;
}
}

Expand Down Expand Up @@ -178,7 +208,6 @@ export class WorkspaceVariableContribution implements VariableContribution {
}

getResourceUri(): URI | undefined {
// TODO replace with ResourceContextKey.get?
return this.currentWidget && this.currentWidget.getResourceUri();
}

Expand Down

0 comments on commit 57eb460

Please sign in to comment.