Skip to content

Commit

Permalink
feat: Add shiny.previewType configuration setting (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Cheng <[email protected]>
  • Loading branch information
gadenbuie and jcheng5 authored Apr 26, 2024
1 parent 2608eed commit e1237e3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- The extension now supports Shiny for R apps. (#30)
- The [Python VS Code extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) is now a soft dependency and is no longer installed by default with the Shiny for VS Code extension. (#30)
- Added a new setting, `shiny.previewType`, to control where the Shiny app preview should be opened. (#40)

## 0.1.6

Expand Down
20 changes: 19 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@
"configuration": {
"title": "Shiny",
"properties": {
"shiny.previewType": {
"scope": "window",
"type": "string",
"description": "Where should the Shiny app preview open?",
"enum": [
"internal",
"simpleBrowser",
"external",
"none"
],
"default": "internal",
"markdownEnumDescriptions": [
"Preview using a preview panel inside VS Code",
"Preview using a Simple Browser inside VS Code",
"Preview using an external web browser",
"Don't automatically launch the app after starting"
]
},
"shiny.python.port": {
"type": "integer",
"default": 0,
Expand Down Expand Up @@ -132,4 +150,4 @@
"fs": "^0.0.1-security",
"winreg": "^1.2.5"
}
}
}
24 changes: 24 additions & 0 deletions src/extension-api-utils/extensionHostPreview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as vscode from "vscode";

declare const globalThis: {
[key: string]: any;
};

export interface HostWebviewPanel extends vscode.Disposable {
readonly webview: vscode.Webview;
readonly visible: boolean;
reveal(viewColumn?: vscode.ViewColumn, preserveFocus?: boolean): void;
readonly onDidChangeViewState: vscode.Event<any>;
readonly onDidDispose: vscode.Event<void>;
}

export function getExtensionHostPreview(): void | ((url: string) => HostWebviewPanel) {
if (
"acquirePositronApi" in globalThis &&
typeof globalThis.acquirePositronApi === "function"
) {
const pst = globalThis.acquirePositronApi();
if (!pst) { return; }
return (url: string) => pst.window.previewUrl(vscode.Uri.parse(url));
}
}
36 changes: 28 additions & 8 deletions src/net-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AddressInfo } from "net";
import * as vscode from "vscode";
import { getRemoteSafeUrl } from "./extension-api-utils/getRemoteSafeUrl";
import { retryUntilTimeout } from "./retry-utils";
import { getExtensionHostPreview } from "./extension-api-utils/extensionHostPreview";

/**
* Tests if a port is open on a host, by trying to connect to it with a TCP
Expand Down Expand Up @@ -63,14 +64,33 @@ export async function openBrowserWhenReady(
}

export async function openBrowser(url: string): Promise<void> {
// if (process.env["CODESPACES"] === "true") {
// vscode.env.openExternal(vscode.Uri.parse(url));
// } else {
await vscode.commands.executeCommand("simpleBrowser.api.open", url, {
preserveFocus: true,
viewColumn: vscode.ViewColumn.Beside,
});
// }
const previewType = vscode.workspace
.getConfiguration()
.get("shiny.previewType");

switch (previewType) {
case "none":
return;
case "external": {
vscode.env.openExternal(vscode.Uri.parse(url));
return;
}
// @ts-ignore-next-line
case "internal": {
const hostPreview = getExtensionHostPreview();
if (hostPreview) {
hostPreview(url);
return;
}
// fallthrough to simpleBrowser default if no hostPreview feature
}
default: {
await vscode.commands.executeCommand("simpleBrowser.api.open", url, {
preserveFocus: true,
viewColumn: vscode.ViewColumn.Beside,
});
}
}
}

export async function suggestPort(): Promise<number> {
Expand Down

0 comments on commit e1237e3

Please sign in to comment.