Skip to content

Commit

Permalink
Get last window on Linux/Windows reviving process
Browse files Browse the repository at this point in the history
Part of #131634
  • Loading branch information
Tyriar committed Sep 20, 2021
1 parent 3a1c692 commit bd7a0d4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,13 @@ export class RemoteTerminalService extends Disposable implements IRemoteTerminal
if (serializedState) {
try {
await this._remoteTerminalChannel.reviveTerminalProcesses(serializedState);
this._storageService.remove(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
// If reviving processes, send the terminal layout info back to the pty host as it
// will not have been persisted on application exit
const layoutInfo = this._storageService.get(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
if (layoutInfo) {
await this._remoteTerminalChannel.setTerminalLayoutInfo(JSON.parse(layoutInfo));
this._storageService.remove(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
}
} catch {
// no-op
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ export interface ITerminalService extends ITerminalInstanceHost {

getDefaultProfileName(): string;
resolveLocation(location?: ITerminalLocationOptions): TerminalLocation | undefined
setNativeDelegate(nativeCalls: ITerminalServiceNativeDelegate): void;
}

export interface ITerminalServiceNativeDelegate {
getWindowCount(): Promise<number>;
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/vs/workbench/contrib/terminal/browser/terminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { ColorScheme } from 'vs/platform/theme/common/theme';
import { IThemeService, Themable, ThemeIcon } from 'vs/platform/theme/common/themeService';
import { VirtualWorkspaceContext } from 'vs/workbench/browser/contextkeys';
import { IEditableData, IViewsService } from 'vs/workbench/common/views';
import { ICreateTerminalOptions, IRemoteTerminalService, IRequestAddInstanceToGroupEvent, ITerminalEditorService, ITerminalExternalLinkProvider, ITerminalFindHost, ITerminalGroup, ITerminalGroupService, ITerminalInstance, ITerminalInstanceHost, ITerminalInstanceService, ITerminalLocationOptions, ITerminalProfileProvider, ITerminalService, TerminalConnectionState, TerminalEditorLocation } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ICreateTerminalOptions, IRemoteTerminalService, IRequestAddInstanceToGroupEvent, ITerminalEditorService, ITerminalExternalLinkProvider, ITerminalFindHost, ITerminalGroup, ITerminalGroupService, ITerminalInstance, ITerminalInstanceHost, ITerminalInstanceService, ITerminalLocationOptions, ITerminalProfileProvider, ITerminalService, ITerminalServiceNativeDelegate, TerminalConnectionState, TerminalEditorLocation } from 'vs/workbench/contrib/terminal/browser/terminal';
import { refreshTerminalActions } from 'vs/workbench/contrib/terminal/browser/terminalActions';
import { TerminalConfigHelper } from 'vs/workbench/contrib/terminal/browser/terminalConfigHelper';
import { TerminalEditor } from 'vs/workbench/contrib/terminal/browser/terminalEditor';
Expand Down Expand Up @@ -75,6 +75,8 @@ export class TerminalService implements ITerminalService {
private _remoteTerminalsInitPromise: Promise<void> | undefined;
private _localTerminalsInitPromise: Promise<void> | undefined;
private _connectionState: TerminalConnectionState;
private _nativeDelegate?: ITerminalServiceNativeDelegate;
private _shutdownWindowCount?: number;

private _editable: { instance: ITerminalInstance, data: IEditableData } | undefined;

Expand Down Expand Up @@ -543,6 +545,7 @@ export class TerminalService implements ITerminalService {

// Persist terminal _buffer state_, note that even if this happens the dirty terminal prompt
// still shows as that cannot be revived
this._shutdownWindowCount = await this._nativeDelegate?.getWindowCount();
const shouldReviveProcesses = this._shouldReviveProcesses(reason);
if (shouldReviveProcesses) {
await this._localTerminalService?.persistTerminalState();
Expand All @@ -565,12 +568,22 @@ export class TerminalService implements ITerminalService {
return false;
}

setNativeDelegate(nativeDelegate: ITerminalServiceNativeDelegate): void {
this._nativeDelegate = nativeDelegate;
}

private _shouldReviveProcesses(reason: ShutdownReason): boolean {
if (!this._configHelper.config.enablePersistentSessions) {
return false;
}
switch (this.configHelper.config.persistentSessionReviveProcess) {
case 'onExit': return reason === ShutdownReason.LOAD || reason === ShutdownReason.QUIT;
case 'onExit': {
// Allow on close if it's the last window on Windows or Linux
if (reason === ShutdownReason.CLOSE && (this._shutdownWindowCount === 1 && !isMacintosh)) {
return true;
}
return reason === ShutdownReason.LOAD || reason === ShutdownReason.QUIT;
}
case 'onExitAndWindowClose': return reason !== ShutdownReason.RELOAD;
default: return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,13 @@ export class LocalTerminalService extends Disposable implements ILocalTerminalSe
if (serializedState) {
try {
await this._localPtyService.reviveTerminalProcesses(serializedState);
this._storageService.remove(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
// If reviving processes, send the terminal layout info back to the pty host as it
// will not have been persisted on application exit
const layoutInfo = this._storageService.get(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
if (layoutInfo) {
await this._localPtyService.setTerminalLayoutInfo(JSON.parse(layoutInfo));
this._storageService.remove(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
}
} catch {
// no-op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class TerminalNativeContribution extends Disposable implements IWorkbench
ipcRenderer.on('vscode:openFiles', (_: unknown, request: INativeOpenFileRequest) => this._onOpenFileRequest(request));
this._register(nativeHostService.onDidResumeOS(() => this._onOsResume()));

this._terminalService.setNativeDelegate({
getWindowCount: () => nativeHostService.getWindowCount()
});

const connection = remoteAgentService.getConnection();
if (connection && connection.remoteAuthority) {
registerRemoteContributions();
Expand Down

0 comments on commit bd7a0d4

Please sign in to comment.