Add workspace elapsed time feature (#19)

* Add workspace elapsed time feature

* Clean up workspaceElapsedTime setting check
This commit is contained in:
pyrox18 2017-12-20 13:22:11 +08:00 committed by Crawl
parent 47e57f9d81
commit e38d62da47
2 changed files with 14 additions and 5 deletions

View file

@ -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"
}
}
}

View file

@ -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