Skip to content

Commit

Permalink
Terminal close dialog
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Grant <[email protected]>
  • Loading branch information
Colin Grant committed Nov 17, 2021
1 parent 5bc3ba1 commit 680a8f1
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions packages/terminal/src/browser/terminal-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
} from '@theia/core/lib/common';
import {
ApplicationShell, KeybindingContribution, KeyCode, Key, WidgetManager,
KeybindingRegistry, Widget, LabelProvider, WidgetOpenerOptions, StorageService, QuickInputService, codicon, CommonCommands, FrontendApplicationContribution, OnWillStopAction
KeybindingRegistry, Widget, LabelProvider, WidgetOpenerOptions, StorageService,
QuickInputService, codicon, CommonCommands, FrontendApplicationContribution, OnWillStopAction, Dialog, ConfirmDialog
} from '@theia/core/lib/browser';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { TERMINAL_WIDGET_FACTORY_ID, TerminalWidgetFactoryOptions, TerminalWidgetImpl } from './terminal-widget-impl';
Expand Down Expand Up @@ -206,17 +207,39 @@ export class TerminalFrontendContribution implements FrontendApplicationContribu
}

onWillStop(): OnWillStopAction | undefined {
if (this.terminalPreferences['terminal.integrated.confirmOnExit'] !== 'never') {
const preferenceValue = this.terminalPreferences['terminal.integrated.confirmOnExit'];
if (preferenceValue !== 'never') {
const allTerminals = this.widgetManager.getWidgets(TERMINAL_WIDGET_FACTORY_ID) as TerminalWidget[];
if (allTerminals.length) {
return {
action: () => Promise.all(allTerminals.map(widget => widget.hasChildProcesses())).then(hasChildProcesses => hasChildProcesses.some(hasChild => hasChild)),
action: async () => {
if (preferenceValue === 'always') {
return this.confirmExitWithActiveTerminals(allTerminals.length);
} else {
const activeTerminals = await Promise.all(allTerminals.map(widget => widget.hasChildProcesses()))
.then(hasChildProcesses => hasChildProcesses.filter(hasChild => hasChild));
return activeTerminals.length === 0 || this.confirmExitWithActiveTerminals(activeTerminals.length);
}
},
reason: 'Active integrated terminal',
};
}
}
}

protected async confirmExitWithActiveTerminals(activeTerminalCount: number): Promise<boolean> {
const msg = activeTerminalCount === 1
? nls.localizeByDefault('Do you want to terminate the active terminal session?')
: nls.localizeByDefault('Do you want to terminate the {0} active terminal sessions?', activeTerminalCount);
const safeToExit = await new ConfirmDialog({
title: '',
msg,
ok: nls.localizeByDefault('Terminate'),
cancel: Dialog.CANCEL,
}).open();
return safeToExit === true;
}

protected _currentTerminal: TerminalWidget | undefined;
get currentTerminal(): TerminalWidget | undefined {
return this._currentTerminal;
Expand Down

0 comments on commit 680a8f1

Please sign in to comment.