Skip to content

Commit

Permalink
Merge pull request #225 from markuskreukniet/check-isDiscordRunning
Browse files Browse the repository at this point in the history
isDiscordRunning check
  • Loading branch information
hmlendea authored Mar 14, 2024
2 parents 22f09de + 962dc6d commit 0e847e9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
20 changes: 17 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "geforcenow-electron",
"appId": "com.github.hmlendea.${name}",
"productName": "GeForce NOW",
"version": "2.0.1",
"version": "2.0.2",
"description": "A Linux desktop web app for GeForce NOW",
"main": "scripts/main.js",
"scripts": {
Expand Down Expand Up @@ -37,6 +37,7 @@
"electron-builder": "^24.9.1"
},
"dependencies": {
"discord-rich-presence": "^0.0.8"
"discord-rich-presence": "^0.0.8",
"find-process": "^1.4.7"
}
}
33 changes: 29 additions & 4 deletions scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { app, globalShortcut, BrowserWindow, session } = require('electron');
const findProcess = require('find-process');
const path = require('path');
const { DiscordRPC } = require('./rpc.js');
const { switchFullscreenState } = require('./windowManager.js');
Expand Down Expand Up @@ -52,10 +53,17 @@ async function createWindow() {
*/
}

let discordIsRunning = false;

app.whenReady().then(async () => {
// Ensure isDiscordRunning is called before createWindow to prevent the 'browser-window-created' event from triggering before the Discord check is complete.
discordIsRunning = await isDiscordRunning();

createWindow();

DiscordRPC('GeForce NOW');
if (discordIsRunning) {
DiscordRPC('GeForce NOW');
}

app.on('activate', async function () {
if (BrowserWindow.getAllWindows().length === 0) {
Expand Down Expand Up @@ -129,9 +137,11 @@ app.on('browser-window-created', async function (e, window) {
BrowserWindow.getAllWindows()[0].loadURL(url);
});

window.on('page-title-updated', async function (e, title) {
DiscordRPC(title);
});
if (discordIsRunning) {
window.on('page-title-updated', async function (e, title) {
DiscordRPC(title);
});
}
});

app.on('will-quit', async () => {
Expand All @@ -143,3 +153,18 @@ app.on('window-all-closed', async function () {
app.quit();
}
});

function isDiscordRunning() {
return new Promise(resolve => {
findProcess('name', 'Discord').then(list => {
if (list.length > 0) {
resolve(true);
} else {
resolve(false);
}
}).catch(error => {
console.log('Error checking Discord process:', error);
resolve(false);
});
});
}

0 comments on commit 0e847e9

Please sign in to comment.