diff --git a/src/webview/newMicroPythonProjectPanel.mts b/src/webview/newMicroPythonProjectPanel.mts index c53de36..353a554 100644 --- a/src/webview/newMicroPythonProjectPanel.mts +++ b/src/webview/newMicroPythonProjectPanel.mts @@ -23,6 +23,7 @@ import which from "which"; import { existsSync } from "fs"; import { join } from "path"; import { PythonExtension } from "@vscode/python-extension"; +import { unknownErrorToString } from "../utils/errorHelper.mjs"; interface SubmitMessageValue { projectName: string; @@ -77,10 +78,16 @@ export class NewMicroPythonProjectPanel { if (!settings) { panel.dispose(); - // TODO: maybe add restart button - void window.showErrorMessage( - "Failed to load settings. Please restart VSCode." - ); + void window + .showErrorMessage( + "Failed to load settings. Please restart VS Code or reload the window.", + "Reload Window" + ) + .then(selected => { + if (selected === "Reload Window") { + commands.executeCommand("workbench.action.reloadWindow"); + } + }); return; } @@ -354,11 +361,9 @@ print("Finished.")\r\n`; await new Promise(resolve => setTimeout(resolve, 2000)); // open and call initialise - void commands.executeCommand( - "vscode.openFolder", - Uri.file(projectFolder), - (workspace.workspaceFolders?.length ?? 0) > 0 - ); + void commands.executeCommand("vscode.openFolder", Uri.file(projectFolder), { + forceNewWindow: (workspace.workspaceFolders?.length ?? 0) > 0, + }); } private async _update(): Promise { @@ -375,7 +380,18 @@ print("Finished.")\r\n`; const html = await this._getHtmlForWebview(this._panel.webview); if (html !== "") { - this._panel.webview.html = html; + try { + this._panel.webview.html = html; + } catch (error) { + this._logger.error( + "Failed to set webview html. Webview might have been disposed. Error: ", + unknownErrorToString(error) + ); + // properly dispose panel + this.dispose(); + + return; + } await this._updateTheme(); } else { void window.showErrorMessage( @@ -386,14 +402,23 @@ print("Finished.")\r\n`; } private async _updateTheme(): Promise { - await this._panel.webview.postMessage({ - command: "setTheme", - theme: - window.activeColorTheme.kind === ColorThemeKind.Dark || - window.activeColorTheme.kind === ColorThemeKind.HighContrast - ? "dark" - : "light", - }); + try { + await this._panel.webview.postMessage({ + command: "setTheme", + theme: + window.activeColorTheme.kind === ColorThemeKind.Dark || + window.activeColorTheme.kind === ColorThemeKind.HighContrast + ? "dark" + : "light", + }); + } catch (error) { + this._logger.error( + "Failed to update theme in webview. Webview might have been disposed. Error:", + unknownErrorToString(error) + ); + // properly dispose panel + this.dispose(); + } } public dispose(): void { diff --git a/src/webview/newProjectPanel.mts b/src/webview/newProjectPanel.mts index 834df9b..bad45cf 100644 --- a/src/webview/newProjectPanel.mts +++ b/src/webview/newProjectPanel.mts @@ -384,10 +384,16 @@ export class NewProjectPanel { if (settings === undefined) { panel.dispose(); - // TODO: maybe add restart button - void window.showErrorMessage( - "Failed to load settings. Please restart VSCode." - ); + void window + .showErrorMessage( + "Failed to load settings. Please restart VS Code or reload the window.", + "Reload Window" + ) + .then(selected => { + if (selected === "Reload Window") { + commands.executeCommand("workbench.action.reloadWindow"); + } + }); return; } @@ -1227,14 +1233,23 @@ export class NewProjectPanel { } private async _updateTheme(): Promise { - await this._panel.webview.postMessage({ - command: "setTheme", - theme: - window.activeColorTheme.kind === ColorThemeKind.Dark || - window.activeColorTheme.kind === ColorThemeKind.HighContrast - ? "dark" - : "light", - }); + try { + await this._panel.webview.postMessage({ + command: "setTheme", + theme: + window.activeColorTheme.kind === ColorThemeKind.Dark || + window.activeColorTheme.kind === ColorThemeKind.HighContrast + ? "dark" + : "light", + }); + } catch (error) { + this._logger.error( + "Failed to update theme in webview. Webview might have been disposed. Error:", + unknownErrorToString(error) + ); + // properly dispose panel + this.dispose(); + } } public dispose(): void { @@ -2230,7 +2245,9 @@ export class NewProjectPanel { ? options.projectRoot : join(options.projectRoot, projectName) ), - (workspace.workspaceFolders?.length ?? 0) > 0 + { + forceNewWindow: (workspace.workspaceFolders?.length ?? 0) > 0, + } ); // restart the extension if the folder was already open cause then vscode won't