Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
will-v-pi committed Sep 17, 2024
2 parents 6a7f2c1 + 864721d commit d1e78fa
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 31 deletions.
61 changes: 43 additions & 18 deletions src/webview/newMicroPythonProjectPanel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<void> {
Expand All @@ -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(
Expand All @@ -386,14 +402,23 @@ print("Finished.")\r\n`;
}

private async _updateTheme(): Promise<void> {
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 {
Expand Down
43 changes: 30 additions & 13 deletions src/webview/newProjectPanel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1227,14 +1233,23 @@ export class NewProjectPanel {
}

private async _updateTheme(): Promise<void> {
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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d1e78fa

Please sign in to comment.