Skip to content

Commit afab6bf

Browse files
committed
Open all closed workspaces on startup
1 parent 1d88263 commit afab6bf

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

arduino-ide-extension/src/browser/arduino-workspace-resolver.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class ArduinoWorkspaceRootResolver {
6161
// - https://github.com/eclipse-theia/theia/blob/8196e9dcf9c8de8ea0910efeb5334a974f426966/packages/workspace/src/browser/workspace-service.ts#L423
6262
protected hashToUri(hash: string | undefined): string | undefined {
6363
if (hash && hash.length > 1 && hash.startsWith('#')) {
64-
const path = hash.slice(1); // Trim the leading `#`.
64+
const path = decodeURI(hash.slice(1)).replace(/\\/g, '/'); // Trim the leading `#`, decode the URI and replace Windows separators
6565
return new URI(
6666
toUnix(path.slice(isWindows && hash.startsWith('/') ? 1 : 0))
6767
)

arduino-ide-extension/src/electron-main/theia/electron-main-application.ts

+43
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ import { ElectronSecurityToken } from '@theia/core/lib/electron-common/electron-
1414
import { FrontendApplicationConfig } from '@theia/application-package/lib/application-props';
1515
import {
1616
ElectronMainApplication as TheiaElectronMainApplication,
17+
ElectronMainExecutionParams,
1718
TheiaBrowserWindowOptions,
1819
} from '@theia/core/lib/electron-main/electron-main-application';
1920
import { SplashServiceImpl } from '../splash/splash-service-impl';
2021
import { ipcMain } from '@theia/core/shared/electron';
22+
import { URI } from '@theia/core/shared/vscode-uri';
2123

2224
app.commandLine.appendSwitch('disable-http-cache');
2325

26+
const WORKSPACES = 'workspaces';
27+
2428
@injectable()
2529
export class ElectronMainApplication extends TheiaElectronMainApplication {
2630
protected _windows: BrowserWindow[] = [];
@@ -36,6 +40,17 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
3640
return super.start(config);
3741
}
3842

43+
protected async launch(params: ElectronMainExecutionParams): Promise<void> {
44+
const workspaces: string[] | undefined = this.electronStore.get(WORKSPACES);
45+
if (workspaces && workspaces.length > 0) {
46+
// Setting `secondInstance=true` allows us to pass workspace URIs to the frontend
47+
Object.assign(params, { secondInstance: true });
48+
await Promise.all(workspaces.map(file => this.handleMainCommand(params, { file })));
49+
} else {
50+
super.launch(params);
51+
}
52+
}
53+
3954
protected getTitleBarStyle(): 'native' | 'custom' {
4055
return 'native';
4156
}
@@ -148,6 +163,7 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
148163
}
149164
}
150165
});
166+
this.attachClosedWorkspace(electronWindow);
151167
this.attachReadyToShow(electronWindow);
152168
this.attachSaveWindowState(electronWindow);
153169
this.attachGlobalShortcuts(electronWindow);
@@ -218,6 +234,33 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
218234
}
219235
}
220236

237+
protected closedWorkspaces: { workspace: string, time: number }[] = [];
238+
239+
protected attachClosedWorkspace(window: BrowserWindow): void {
240+
// Since the `before-quit` event is only fired when closing the *last* window
241+
// We need to keep track of recently closed windows/workspaces manually
242+
window.on('close', () => {
243+
const url = window.webContents.getURL();
244+
const workspace = URI.parse(url).fragment;
245+
if (workspace) {
246+
const workspaceUri = URI.file(workspace);
247+
this.closedWorkspaces.push({
248+
workspace: workspaceUri.fsPath,
249+
time: Date.now()
250+
})
251+
}
252+
});
253+
}
254+
255+
protected onWillQuit(event: Electron.Event): void {
256+
// Only add workspaces which were closed within the last second (1000 milliseconds)
257+
const threshold = Date.now() - 1000;
258+
const workspaces = this.closedWorkspaces.filter(e => e.time > threshold).map(e => e.workspace).sort();
259+
this.electronStore.set(WORKSPACES, workspaces);
260+
261+
super.onWillQuit(event);
262+
}
263+
221264
get windows(): BrowserWindow[] {
222265
return this._windows.slice();
223266
}

0 commit comments

Comments
 (0)