Skip to content

Commit d1e78fa

Browse files
committed
2 parents 6a7f2c1 + 864721d commit d1e78fa

File tree

2 files changed

+73
-31
lines changed

2 files changed

+73
-31
lines changed

src/webview/newMicroPythonProjectPanel.mts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import which from "which";
2323
import { existsSync } from "fs";
2424
import { join } from "path";
2525
import { PythonExtension } from "@vscode/python-extension";
26+
import { unknownErrorToString } from "../utils/errorHelper.mjs";
2627

2728
interface SubmitMessageValue {
2829
projectName: string;
@@ -77,10 +78,16 @@ export class NewMicroPythonProjectPanel {
7778
if (!settings) {
7879
panel.dispose();
7980

80-
// TODO: maybe add restart button
81-
void window.showErrorMessage(
82-
"Failed to load settings. Please restart VSCode."
83-
);
81+
void window
82+
.showErrorMessage(
83+
"Failed to load settings. Please restart VS Code or reload the window.",
84+
"Reload Window"
85+
)
86+
.then(selected => {
87+
if (selected === "Reload Window") {
88+
commands.executeCommand("workbench.action.reloadWindow");
89+
}
90+
});
8491

8592
return;
8693
}
@@ -354,11 +361,9 @@ print("Finished.")\r\n`;
354361
await new Promise(resolve => setTimeout(resolve, 2000));
355362

356363
// open and call initialise
357-
void commands.executeCommand(
358-
"vscode.openFolder",
359-
Uri.file(projectFolder),
360-
(workspace.workspaceFolders?.length ?? 0) > 0
361-
);
364+
void commands.executeCommand("vscode.openFolder", Uri.file(projectFolder), {
365+
forceNewWindow: (workspace.workspaceFolders?.length ?? 0) > 0,
366+
});
362367
}
363368

364369
private async _update(): Promise<void> {
@@ -375,7 +380,18 @@ print("Finished.")\r\n`;
375380
const html = await this._getHtmlForWebview(this._panel.webview);
376381

377382
if (html !== "") {
378-
this._panel.webview.html = html;
383+
try {
384+
this._panel.webview.html = html;
385+
} catch (error) {
386+
this._logger.error(
387+
"Failed to set webview html. Webview might have been disposed. Error: ",
388+
unknownErrorToString(error)
389+
);
390+
// properly dispose panel
391+
this.dispose();
392+
393+
return;
394+
}
379395
await this._updateTheme();
380396
} else {
381397
void window.showErrorMessage(
@@ -386,14 +402,23 @@ print("Finished.")\r\n`;
386402
}
387403

388404
private async _updateTheme(): Promise<void> {
389-
await this._panel.webview.postMessage({
390-
command: "setTheme",
391-
theme:
392-
window.activeColorTheme.kind === ColorThemeKind.Dark ||
393-
window.activeColorTheme.kind === ColorThemeKind.HighContrast
394-
? "dark"
395-
: "light",
396-
});
405+
try {
406+
await this._panel.webview.postMessage({
407+
command: "setTheme",
408+
theme:
409+
window.activeColorTheme.kind === ColorThemeKind.Dark ||
410+
window.activeColorTheme.kind === ColorThemeKind.HighContrast
411+
? "dark"
412+
: "light",
413+
});
414+
} catch (error) {
415+
this._logger.error(
416+
"Failed to update theme in webview. Webview might have been disposed. Error:",
417+
unknownErrorToString(error)
418+
);
419+
// properly dispose panel
420+
this.dispose();
421+
}
397422
}
398423

399424
public dispose(): void {

src/webview/newProjectPanel.mts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,16 @@ export class NewProjectPanel {
384384
if (settings === undefined) {
385385
panel.dispose();
386386

387-
// TODO: maybe add restart button
388-
void window.showErrorMessage(
389-
"Failed to load settings. Please restart VSCode."
390-
);
387+
void window
388+
.showErrorMessage(
389+
"Failed to load settings. Please restart VS Code or reload the window.",
390+
"Reload Window"
391+
)
392+
.then(selected => {
393+
if (selected === "Reload Window") {
394+
commands.executeCommand("workbench.action.reloadWindow");
395+
}
396+
});
391397

392398
return;
393399
}
@@ -1227,14 +1233,23 @@ export class NewProjectPanel {
12271233
}
12281234

12291235
private async _updateTheme(): Promise<void> {
1230-
await this._panel.webview.postMessage({
1231-
command: "setTheme",
1232-
theme:
1233-
window.activeColorTheme.kind === ColorThemeKind.Dark ||
1234-
window.activeColorTheme.kind === ColorThemeKind.HighContrast
1235-
? "dark"
1236-
: "light",
1237-
});
1236+
try {
1237+
await this._panel.webview.postMessage({
1238+
command: "setTheme",
1239+
theme:
1240+
window.activeColorTheme.kind === ColorThemeKind.Dark ||
1241+
window.activeColorTheme.kind === ColorThemeKind.HighContrast
1242+
? "dark"
1243+
: "light",
1244+
});
1245+
} catch (error) {
1246+
this._logger.error(
1247+
"Failed to update theme in webview. Webview might have been disposed. Error:",
1248+
unknownErrorToString(error)
1249+
);
1250+
// properly dispose panel
1251+
this.dispose();
1252+
}
12381253
}
12391254

12401255
public dispose(): void {
@@ -2230,7 +2245,9 @@ export class NewProjectPanel {
22302245
? options.projectRoot
22312246
: join(options.projectRoot, projectName)
22322247
),
2233-
(workspace.workspaceFolders?.length ?? 0) > 0
2248+
{
2249+
forceNewWindow: (workspace.workspaceFolders?.length ?? 0) > 0,
2250+
}
22342251
);
22352252

22362253
// restart the extension if the folder was already open cause then vscode won't

0 commit comments

Comments
 (0)