diff --git a/src/extension.ts b/src/extension.ts index 1136f02..d65467c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -8,7 +8,7 @@ import { Logging, setDebug } from './logging'; import { Manager } from './manager'; import type { SSHPseudoTerminal } from './pseudoTerminal'; import { ConfigTreeProvider, ConnectionTreeProvider } from './treeViewManager'; -import { pickComplex, PickComplexOptions, pickConnection, setAsAbsolutePath } from './ui-utils'; +import { pickComplex, PickComplexOptions, pickConnection, setAsAbsolutePath, setupWhenClauseContexts } from './ui-utils'; function getVersion(): string | undefined { const ext = vscode.extensions.getExtension('Kelvin.vscode-sshfs'); @@ -59,6 +59,8 @@ export function activate(context: vscode.ExtensionContext) { subscribe(vscode.tasks.registerTaskProvider('ssh-shell', manager)); subscribe(vscode.window.registerTerminalLinkProvider(manager)); + setupWhenClauseContexts(manager.connectionManager); + function registerCommandHandler(name: string, handler: CommandHandler) { const callback = async (arg?: string | FileSystemConfig | Connection | SSHPseudoTerminal | vscode.Uri) => { if (handler.promptOptions && (!arg || typeof arg === 'string')) { diff --git a/src/ui-utils.ts b/src/ui-utils.ts index 1eccf9d..d3e9828 100644 --- a/src/ui-utils.ts +++ b/src/ui-utils.ts @@ -1,7 +1,7 @@ import * as vscode from 'vscode'; import { getConfigs } from './config'; -import type { Connection } from './connection'; +import type { Connection, ConnectionManager } from './connection'; import { parseConnectionString, FileSystemConfig } from './fileSystemConfig'; import type { Manager } from './manager'; import type { SSHPseudoTerminal } from './pseudoTerminal'; @@ -18,6 +18,25 @@ export function formatAddress(config: FileSystemConfig): string { return `${username ? `${username}@` : ''}${host}${port ? `:${port}` : ''}`; } +export function setWhenClauseContext(key: string, value: any) { + return vscode.commands.executeCommand('setContext', `sshfs.${key}`, value); +} + +export function setupWhenClauseContexts(connectionManager: ConnectionManager): Promise { + async function refresh() { + const active = connectionManager.getActiveConnections(); + const pending = connectionManager.getPendingConnections(); + await setWhenClauseContext('openConnections', active.length + pending.length); + await setWhenClauseContext('openTerminals', active.reduce((tot, con) => tot + con.terminals.length, 0)); + await setWhenClauseContext('openFileSystems', active.reduce((tot, con) => tot + con.filesystems.length, 0)); + } + connectionManager.onConnectionAdded(refresh); + connectionManager.onConnectionRemoved(refresh); + connectionManager.onConnectionUpdated(refresh); + connectionManager.onPendingChanged(refresh); + return refresh(); +} + export let asAbsolutePath: vscode.ExtensionContext['asAbsolutePath'] | undefined; export const setAsAbsolutePath = (value: typeof asAbsolutePath) => asAbsolutePath = value;