Skip to content

Commit

Permalink
feat: show discord rpc connection status in tray
Browse files Browse the repository at this point in the history
  • Loading branch information
N0chteil committed Jul 29, 2023
1 parent cdadd32 commit 5fbd5d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/managers/discord.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client, Presence, register, User } from "discord-rpc";
import { app } from "electron";

import { appDependencies } from "../index";
import { appDependencies, trayManager } from "../index";

import { config, getConfig, setConfig } from "./store";
import { Browser } from "./browser";
Expand All @@ -26,6 +26,7 @@ export class Discord {

public activity: Presence = {};
public isLive: boolean = false;
public isConnected: boolean = false;
public currentTrack: currentTrack;
public isSupporter: boolean = null;
public songData: SongData = new SongData();
Expand Down Expand Up @@ -147,8 +148,11 @@ export class Discord {
}

this.isReady = true;
this.isConnected = true;
this.startUp = false;

trayManager.discordConnectionUpdate(true);

this.triggerAfterReady.forEach((func) => func());
this.triggerAfterReady = [];
})
Expand All @@ -167,6 +171,9 @@ export class Discord {
log.info("[DISCORD]", "Client disconnected");

this.isReady = false;
this.isConnected = false;

trayManager.discordConnectionUpdate(false);

this.connect();
});
Expand Down
29 changes: 26 additions & 3 deletions src/managers/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export class TrayManager {
private tray: Tray;
private i18n = i18n.getLangStrings();

private isDiscordConnected = false;

constructor() {
this.tray = new Tray(
path.join(
app.getAppPath(),
process.platform === "darwin"
? "assets/statusBarLogo.png"
? "assets/statusBarIcon.png"
: "assets/trayLogo@32.png"
)
);
Expand All @@ -29,7 +31,7 @@ export class TrayManager {

i18n.onLanguageUpdate(() => {
this.i18n = i18n.getLangStrings();
this.tray.setContextMenu(this.createContextMenu());
this.update();
});
}

Expand All @@ -51,6 +53,12 @@ export class TrayManager {
: "Apple Music",
enabled: false
},
{
label: `Discord${
this.isDiscordConnected ? " " : " not "
}connected`,
enabled: false
},
{ type: "separator" },
{
label: this.i18n?.tray?.reportProblem ?? "Report a Problem",
Expand All @@ -62,7 +70,7 @@ export class TrayManager {
{
label: "Settings",
click() {
new Browser()
new Browser();
}
},
{ type: "separator" },
Expand Down Expand Up @@ -103,4 +111,19 @@ export class TrayManager {
update() {
this.tray.setContextMenu(this.createContextMenu());
}

public discordConnectionUpdate(isConnected: boolean) {
this.isDiscordConnected = isConnected;

if (process.platform === "darwin") {
this.tray.setImage(
path.join(
app.getAppPath(),
`assets/statusBarIcon${isConnected ? "" : "Error"}.png`
)
);
}

this.update();
}
}

0 comments on commit 5fbd5d4

Please sign in to comment.