-
-
Notifications
You must be signed in to change notification settings - Fork 91
Show webview about missing installation dependencies #2720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
04639c0
Started working on installation dependences
AndreasArvidsson 9bc38a3
Added don't show again button
AndreasArvidsson bd511c1
Added command description
AndreasArvidsson 9f8a704
Added command to nevin
AndreasArvidsson f4b266d
Update packages/cursorless-vscode/resources/installationDependencies.js
AndreasArvidsson f01d307
Update packages/cursorless-vscode/resources/installationDependencies.js
AndreasArvidsson 309f31a
Use html template file
AndreasArvidsson 80a9acf
Restructure html file
AndreasArvidsson 6e14405
Update packages/cursorless-vscode/resources/installationDependencies.…
AndreasArvidsson 0e0c84a
Update packages/cursorless-vscode/resources/installationDependencies.…
AndreasArvidsson 3556418
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,6 @@ | |
}, | ||
"pnpm": { | ||
"patchedDependencies": { | ||
"@docusaurus/theme-search-algolia": "patches/@[email protected]", | ||
"@types/[email protected]": "patches/@[email protected]", | ||
"[email protected]": "patches/[email protected]" | ||
}, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/cursorless-vscode/resources/installationDependencies.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const vscode = acquireVsCodeApi(); | ||
|
||
const root = document.getElementById("root"); | ||
|
||
const header = document.createElement("h2"); | ||
header.textContent = "Cursorless extension is now running!"; | ||
|
||
const description = document.createElement("p"); | ||
const aDocs = link( | ||
"https://www.cursorless.org/docs/user/installation", | ||
"Click here to learn how to install Cursorless", | ||
); | ||
description.innerHTML = `<p>Lets check if all dependencies are installed.</p>${aDocs}`; | ||
|
||
const messages = document.createElement("div"); | ||
|
||
root.append(header, description, messages); | ||
|
||
const keyboardUserMessage = | ||
"<p><i>If you're using Cursorless by keyboard you can ignore this message.</i></p>"; | ||
AndreasArvidsson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
window.addEventListener("message", (event) => { | ||
const { dontShow, dependencies } = event.data; | ||
const children = []; | ||
|
||
if (!dependencies.talon) { | ||
const a = link("https://talonvoice.com", "talonvoice.com"); | ||
children.push( | ||
getChild( | ||
"Talon not installed", | ||
`Cursorless requires Talon to function by voice.</br>You can download Talon from ${a}.${keyboardUserMessage}`, | ||
), | ||
); | ||
} else if (!dependencies.cursorlessTalon) { | ||
const a = link( | ||
"https://github.com/cursorless-dev/cursorless-talon", | ||
"github.com/cursorless-dev/cursorless-talon", | ||
); | ||
children.push( | ||
getChild( | ||
"Cursorless Talon scripts missing", | ||
`Cursorless requires Talon user scripts to function by voice.</br>The scripts are available at ${a}.`, | ||
), | ||
); | ||
} | ||
|
||
if (!dependencies.commandServer) { | ||
const a = link( | ||
"https://marketplace.visualstudio.com/items?itemName=pokey.command-server", | ||
"vscode marketplace", | ||
); | ||
children.push( | ||
getChild( | ||
"Command server extension not installed", | ||
`Cursorless requires the command server extension to function by voice.</br>The extension is available at the ${a}.${keyboardUserMessage}`, | ||
), | ||
); | ||
} | ||
|
||
if (children.length === 0) { | ||
children.push(getChild("All dependencies are installed!")); | ||
} | ||
|
||
const dontShowCheckbox = getDontShowCheckbox(dontShow); | ||
|
||
messages.replaceChildren(...children, dontShowCheckbox); | ||
}); | ||
|
||
function link(href, text) { | ||
return `<a href="${href}">${text}</a>`; | ||
} | ||
|
||
function getChild(title, body) { | ||
const child = document.createElement("div"); | ||
const titleElement = document.createElement("h4"); | ||
titleElement.textContent = title; | ||
child.append(titleElement); | ||
if (body != null) { | ||
const bodyElement = document.createElement("p"); | ||
bodyElement.innerHTML = body; | ||
child.append(bodyElement); | ||
} | ||
return child; | ||
} | ||
|
||
function getDontShowCheckbox(dontShow) { | ||
const checkbox = document.createElement("input"); | ||
checkbox.type = "checkbox"; | ||
checkbox.checked = dontShow; | ||
checkbox.onchange = () => { | ||
const command = { type: "dontShow", checked: checkbox.checked }; | ||
vscode.postMessage(command); | ||
}; | ||
const label = document.createElement("label"); | ||
label.style.marginTop = "1rem"; | ||
const text = document.createTextNode("Don't show again"); | ||
label.append(checkbox, text); | ||
return label; | ||
} |
141 changes: 141 additions & 0 deletions
141
packages/cursorless-vscode/src/InstallationDependencies.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { COMMAND_SERVER_EXTENSION_ID } from "@cursorless/vscode-common"; | ||
import { globSync } from "glob"; | ||
import * as fs from "node:fs"; | ||
import * as os from "node:os"; | ||
import * as path from "node:path"; | ||
import * as vscode from "vscode"; | ||
|
||
const STATE_KEY = "dontShowInstallationDependencies"; | ||
|
||
export class InstallationDependencies { | ||
private panel: vscode.WebviewPanel | undefined; | ||
|
||
constructor(private extensionContext: vscode.ExtensionContext) { | ||
this.show = this.show.bind(this); | ||
this.maybeShow = this.maybeShow.bind(this); | ||
} | ||
|
||
/** | ||
* Shows the installation dependencies webview. | ||
*/ | ||
show() { | ||
this.createWebview(); | ||
} | ||
|
||
/** | ||
* Shows the installation dependencies webview if there are missing dependencies. | ||
*/ | ||
maybeShow() { | ||
const state = this.getState(); | ||
if (!state.dontShow && hasMissingDependencies(state.dependencies)) { | ||
this.createWebview(); | ||
} | ||
} | ||
|
||
private getState() { | ||
return { | ||
dontShow: !!this.extensionContext.globalState.get<boolean>(STATE_KEY), | ||
dependencies: getDependencies(), | ||
}; | ||
} | ||
|
||
private createWebview() { | ||
if (this.panel != null) { | ||
this.panel.reveal(); | ||
return; | ||
} | ||
|
||
this.panel = vscode.window.createWebviewPanel( | ||
"cursorless.installationDependencies", | ||
"Cursorless dependencies", | ||
{ | ||
viewColumn: vscode.ViewColumn.Active, | ||
}, | ||
{ | ||
enableScripts: true, | ||
}, | ||
); | ||
|
||
const jsUri = this.panel.webview.asWebviewUri( | ||
vscode.Uri.joinPath( | ||
this.extensionContext.extensionUri, | ||
"resources", | ||
"installationDependencies.js", | ||
), | ||
); | ||
|
||
this.panel.webview.html = getWebviewContent(this.panel.webview, jsUri); | ||
|
||
const updateWebview = () => { | ||
this.panel?.webview.postMessage(this.getState()); | ||
}; | ||
|
||
this.panel.onDidChangeViewState(updateWebview); | ||
|
||
this.panel.webview.onDidReceiveMessage((message) => { | ||
if (message.type === "dontShow") { | ||
const checked = message.checked; | ||
this.extensionContext.globalState.update(STATE_KEY, checked); | ||
} else { | ||
console.error(`Unknown message: ${message}`); | ||
} | ||
}); | ||
|
||
const interval = setInterval(updateWebview, 5000); | ||
|
||
this.panel.onDidDispose(() => { | ||
clearInterval(interval); | ||
this.panel = undefined; | ||
}); | ||
|
||
this.panel.webview.postMessage(this.getState()); | ||
} | ||
} | ||
|
||
function getDependencies(): Record<string, boolean> { | ||
return { | ||
talon: talonHomeExists(), | ||
cursorlessTalon: cursorlessTalonExists(), | ||
commandServer: commandServerInstalled(), | ||
}; | ||
} | ||
|
||
function hasMissingDependencies(dependencies: Record<string, boolean>) { | ||
return Object.values(dependencies).some((value) => !value); | ||
} | ||
|
||
function talonHomeExists() { | ||
return fs.existsSync(getTalonHomePath()); | ||
} | ||
|
||
function cursorlessTalonExists() { | ||
const talonUserPath = path.join(getTalonHomePath(), "user"); | ||
const files = globSync("*/src/cursorless.talon", { cwd: talonUserPath }); | ||
AndreasArvidsson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return files.length > 0; | ||
} | ||
|
||
function commandServerInstalled() { | ||
const extension = vscode.extensions.getExtension(COMMAND_SERVER_EXTENSION_ID); | ||
return extension != null; | ||
} | ||
|
||
function getTalonHomePath() { | ||
return os.platform() === "win32" | ||
? `${os.homedir()}\\AppData\\Roaming\\talon` | ||
: `${os.homedir()}/.talon`; | ||
} | ||
|
||
function getWebviewContent(webview: vscode.Webview, jsUri: vscode.Uri) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta http-equiv="Content-Security-Policy" content="script-src ${webview.cspSource};" /> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script src="${jsUri}"></script> | ||
</body> | ||
</html>`; | ||
} | ||
AndreasArvidsson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.