Skip to content

Commit

Permalink
No save dialog if another widget shares identical model
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-grant-work committed Jan 11, 2022
1 parent e2d8dc4 commit 8f9d392
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 12 additions & 5 deletions packages/core/src/browser/saveable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export namespace Saveable {
await saveable.save(options);
}
}
export function apply(widget: Widget): SaveableWidget | undefined {
export function apply(widget: Widget, getOtherSaveables?: () => Array<Widget & (Saveable | SaveableSource)>): SaveableWidget | undefined {
if (SaveableWidget.is(widget)) {
return widget;
}
Expand All @@ -104,8 +104,8 @@ export namespace Saveable {
setDirty(widget, saveable.dirty);
saveable.onDirtyChanged(() => setDirty(widget, saveable.dirty));
const closeWidget = widget.close.bind(widget);
const closeWithoutSaving: SaveableWidget['closeWithoutSaving'] = async () => {
if (saveable.dirty && saveable.revert) {
const closeWithoutSaving: SaveableWidget['closeWithoutSaving'] = async (doRevert = true) => {
if (doRevert && saveable.dirty && saveable.revert) {
await saveable.revert();
}
closeWidget();
Expand All @@ -119,6 +119,10 @@ export namespace Saveable {
closing = true;
try {
const result = await shouldSave(saveable, () => {
const notLastWithDocument = getOtherSaveables?.().some(otherWidget => otherWidget !== widget && Saveable.get(otherWidget) === saveable);
if (notLastWithDocument) {
return closeWithoutSaving(false).then(() => undefined);
}
if (options && options.shouldSave) {
return options.shouldSave();
}
Expand All @@ -128,7 +132,7 @@ export namespace Saveable {
if (result) {
await Saveable.save(widget);
}
await closeWithoutSaving();
await closeWithoutSaving(result);
}
} finally {
closing = false;
Expand All @@ -154,7 +158,10 @@ export namespace Saveable {
}

export interface SaveableWidget extends Widget {
closeWithoutSaving(): Promise<void>;
/**
* @param doRevert whether the saveable should be reverted before being saved. Defaults to `true`.
*/
closeWithoutSaving(doRevert?: boolean): Promise<void>;
closeWithSaving(options?: SaveableWidget.CloseOptions): Promise<void>;
}
export namespace SaveableWidget {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/browser/shell/application-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Message } from '@phosphor/messaging';
import { IDragEvent } from '@phosphor/dragdrop';
import { RecursivePartial, Event as CommonEvent, DisposableCollection, Disposable, environment } from '../../common';
import { animationFrame } from '../browser';
import { Saveable, SaveableWidget, SaveOptions } from '../saveable';
import { Saveable, SaveableWidget, SaveOptions, SaveableSource } from '../saveable';
import { StatusBarImpl, StatusBarEntry, StatusBarAlignment } from '../status-bar/status-bar';
import { TheiaDockPanel, BOTTOM_AREA_ID, MAIN_AREA_ID } from './theia-dock-panel';
import { SidePanelHandler, SidePanel, SidePanelHandlerFactory } from './side-panel-handler';
Expand Down Expand Up @@ -1025,7 +1025,7 @@ export class ApplicationShell extends Widget {
}
this.tracker.add(widget);
this.checkActivation(widget);
Saveable.apply(widget);
Saveable.apply(widget, () => this.widgets.filter((maybeSaveable): maybeSaveable is Widget & SaveableSource => !!Saveable.get(maybeSaveable)));
if (ApplicationShell.TrackableWidgetProvider.is(widget)) {
for (const toTrack of widget.getTrackableWidgets()) {
this.track(toTrack);
Expand Down

0 comments on commit 8f9d392

Please sign in to comment.