From 428d22d7038ceda614735c5050def0cd0c7b502b Mon Sep 17 00:00:00 2001 From: FOG_Yamato Date: Fri, 24 Nov 2017 01:53:16 +0200 Subject: [PATCH] feat: enable/disable commands (#1) --- .vscode/launch.json | 13 +------------ package.json | 19 ++++++++++++++++++- src/extension.ts | 33 ++++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 43fcc54..d1c69dd 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,6 +1,6 @@ // A launch configuration that compiles the extension and then opens it inside a new window { - "version": "0.1.0", + "version": "0.5.0", "configurations": [ { "name": "Launch Extension", @@ -12,17 +12,6 @@ "sourceMaps": true, "outFiles": [ "${workspaceRoot}/out/src/**/*.js" ], "preLaunchTask": "npm" - }, - { - "name": "Launch Tests", - "type": "extensionHost", - "request": "launch", - "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ], - "stopOnEntry": false, - "sourceMaps": true, - "outFiles": [ "${workspaceRoot}/out/test/**/*.js" ], - "preLaunchTask": "npm" } ] } diff --git a/package.json b/package.json index 5ece226..c9c525c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "discord-vscode", "displayName": "Discord Presence", - "version": "0.4.0", + "version": "0.5.0", "description": "Update your discord status with the newly added rich presence.", "author": { "name": "iCrawl", @@ -21,6 +21,18 @@ "*" ], "contributes": { + "commands": [ + { + "command": "discord.enable", + "title": "Enable Visual Studio Code Discord in the current workspace", + "category": "VS-Discord" + }, + { + "command": "discord.disable", + "title": "Disable Visual Studio Code Discord in the current workspace", + "category": "VS-Discord" + } + ], "configuration": [ { "title": "Visual Studio Code Discord Configuration", @@ -30,6 +42,11 @@ "type": "string", "default": "383226320970055681", "description": "Only modify this if you know what you are doing (most of you don't)." + }, + "discord.enabled": { + "type": "boolean", + "default": true, + "description": "Controls if Visual Studio Code Discord is active" } } } diff --git a/src/extension.ts b/src/extension.ts index 2e905e8..0651db3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,20 +1,26 @@ import { Client } from 'discord-rpc'; import { basename, extname } from 'path'; import { ExtensionContext, commands, window, workspace, Uri, TextDocumentChangeEvent, TextDocument } from 'vscode'; +import { setInterval, clearInterval } from 'timers'; let rpc: Client; export function activate(context: ExtensionContext) { - rpc = new Client({ transport: 'ipc' }); const config = workspace.getConfiguration('discord'); - rpc.once('ready', () => { - setActivity(); - workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity()); + if (config.get('enabled')) { + initRPC(config.get('clientID')); + } + const enabler = commands.registerCommand('discord.enable', () => { + config.update('enabled', true); + initRPC(config.get('clientID')); }); - rpc.login(config.get('clientID')).catch(error => - window.showErrorMessage(`Could not connect to discord via rpc: ${error.message}`) - ); + const disabler = commands.registerCommand('discord.disable', () => { + config.update('enabled', false); + rpc.setActivity({}); + }); + + context.subscriptions.push(enabler, disabler); } export function deactivate(context: ExtensionContext) { @@ -45,3 +51,16 @@ function setActivity(): void { }; rpc.setActivity(activity); } + +function initRPC(clientID: string): void { + rpc = new Client({ transport: 'ipc' }); + rpc.once('ready', () => { + setActivity(); + workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity()); + }); + rpc.login(clientID).catch(error => + error.message.includes('ENOENT') + ? window.showErrorMessage('No Discord Client detected!') + : window.showErrorMessage(`Could not connect to discord via rpc: ${error.message}`) + ); +}