From e38d62da47b2c0b8c1787923f2ccb1128d87f2c2 Mon Sep 17 00:00:00 2001 From: pyrox18 Date: Wed, 20 Dec 2017 13:22:11 +0800 Subject: [PATCH] Add workspace elapsed time feature (#19) * Add workspace elapsed time feature * Clean up workspaceElapsedTime setting check --- package.json | 5 +++++ src/extension.ts | 14 +++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index daf77a6..5dec16a 100644 --- a/package.json +++ b/package.json @@ -108,6 +108,11 @@ "type": "boolean", "default": false, "description": "Decides if error messages are shown to the user" + }, + "discord.workspaceElapsedTime": { + "type": "boolean", + "default": false, + "description": "Decides whether to display elapsed time for a workspace or a single file" } } } diff --git a/src/extension.ts b/src/extension.ts index 03783d1..cd88e8c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -93,9 +93,10 @@ function initRPC(clientID: string): void { setActivity(); // Set the activity once on ready setTimeout(() => rpc.setActivity(activity), 500); - eventHandlers.add(workspace.onDidChangeTextDocument(() => setActivity())) - .add(workspace.onDidOpenTextDocument(() => setActivity())) - .add(workspace.onDidCloseTextDocument(() => setActivity())) + const workspaceElapsedTime = Boolean(config.get('workspaceElapsedTime')); + eventHandlers.add(workspace.onDidChangeTextDocument(() => setActivity(workspaceElapsedTime))) + .add(workspace.onDidOpenTextDocument(() => setActivity(workspaceElapsedTime))) + .add(workspace.onDidCloseTextDocument(() => setActivity(workspaceElapsedTime))) .add(debug.onDidChangeActiveDebugSession(() => setActivity())) .add(debug.onDidStartDebugSession(() => setActivity())) .add(debug.onDidTerminateDebugSession(() => setActivity())); @@ -151,7 +152,7 @@ function destroyRPC(): void { } // This function updates the activity (The Client's Rich Presence status). -function setActivity(): void { +function setActivity(workspaceElapsedTime: boolean = false): void { // Do not continue if RPC isn't initalized. if (!rpc) return; if (window.activeTextEditor && window.activeTextEditor.document.fileName === lastKnownFileName) return; @@ -168,11 +169,14 @@ function setActivity(): void { })] : 'vscode-big'; + // Get the previous activity start timestamp (if available) to preserve workspace elapsed time + let previousTimestamp = null; + if (activity) previousTimestamp = activity['startTimestamp']; // Create a JSON Object with the user's activity information. activity = { details: generateDetails('detailsDebugging', 'detailsEditing', 'detailsIdle'), state: generateDetails('lowerDetailsDebugging', 'lowerDetailsEditing', 'lowerDetailsIdle'), - startTimestamp: new Date().getTime() / 1000, + startTimestamp: !previousTimestamp && !workspaceElapsedTime ? new Date().getTime() / 1000 : previousTimestamp, largeImageKey: largeImageKey ? largeImageKey.image || largeImageKey