Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.
/ Linux Public archive

Commit a3e46d3

Browse files
committed
✨ Electron 7, Discord state and auto-launch
1 parent 4918a7f commit a3e46d3

File tree

8 files changed

+136
-93
lines changed

8 files changed

+136
-93
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"productName": "PreMiD",
44
"author": "Jack 'DooMLorD' W. <doomlordcs@gmail.com> (https://github.com/doomlerd)",
55
"description": "Discord Rich Presence for web services",
6-
"version": "2.2.15",
6+
"version": "2.2.16",
77
"repository": {
88
"type": "git",
99
"url": "git://github.com/PreMiD/Linux.git"
@@ -58,12 +58,12 @@
5858
"devDependencies": {
5959
"@timeraa/devscript": "Timeraa/DevScript",
6060
"@types/auto-launch": "5.0.1",
61-
"@types/discord-rpc": "3.0.2",
62-
"@types/node": "13.11.1",
61+
"@types/discord-rpc": "3.0.3",
62+
"@types/node": "^12.12.6",
6363
"@types/socket.io": "2.1.4",
64-
"@typescript-eslint/eslint-plugin": "2.27.0",
65-
"@typescript-eslint/parser": "2.27.0",
66-
"electron": "8.2.1",
64+
"@typescript-eslint/eslint-plugin": "2.28.0",
65+
"@typescript-eslint/parser": "2.28.0",
66+
"electron": "^7.1.11",
6767
"electron-builder": "22.4.1",
6868
"eslint": "6.8.0",
6969
"typescript": "3.8.3"

src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ let singleInstanceLock: boolean;
1111
if (app.isPackaged) singleInstanceLock = app.requestSingleInstanceLock();
1212
export let trayManager: TrayManager;
1313

14-
//* When app is ready
15-
export let updateCheckerInterval = null;
16-
app.whenReady().then(async () => {
17-
if (!singleInstanceLock) return app.exit();
14+
app.on("ready", async () => {
15+
if (!singleInstanceLock && app.isPackaged) return app.exit();
1816

1917
trayManager = new TrayManager();
2018

@@ -23,7 +21,7 @@ app.whenReady().then(async () => {
2321

2422
if (app.isPackaged && app.name.includes("Portable")) {
2523
await checkForUpdate(true);
26-
updateCheckerInterval = setInterval(() => {
24+
setInterval(() => {
2725
checkForUpdate(true);
2826
}, 15 * 1000 * 60);
2927
}

src/managers/discordManager.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Client } from "discord-rpc";
22
import { app } from "electron";
3+
import { trayManager } from "..";
34

45
//* Import custom types
56
/* eslint-disable no-unused-vars */
@@ -9,6 +10,12 @@ import PresenceData from "../../@types/PreMiD/PresenceData";
910
//* Define Presence array
1011
export let rpcClients: Array<RPCClient> = [];
1112

13+
//* States :
14+
//* - Disconnected
15+
//* - Connected
16+
//* - Connecting
17+
export let connectionState: String = "Connecting";
18+
1219
class RPCClient {
1320
clientId: string;
1421
currentPresence: PresenceData;
@@ -24,21 +31,25 @@ class RPCClient {
2431
});
2532

2633
this.client.once("ready", () => {
34+
updateTray("Connected");
2735
this.clientReady = true;
2836
this.setActivity();
2937
});
3038

3139
this.client.once(
3240
// @ts-ignore
3341
"disconnected",
34-
() =>
35-
(rpcClients = rpcClients.filter(
42+
() => {
43+
updateTray("Disconnected");
44+
rpcClients = rpcClients.filter(
3645
(client) => client.clientId !== this.clientId
37-
))
46+
);
47+
}
3848
);
3949

4050
this.client.login({ clientId: this.clientId }).catch(() => this.destroy());
4151

52+
updateTray("Connected");
4253
console.log(`Create RPC client (${this.clientId})`);
4354
}
4455

@@ -54,6 +65,8 @@ class RPCClient {
5465
)
5566
presenceData.presenceData.largeImageText = `PreMiD 🐧 v${app.getVersion()}`;
5667

68+
updateTray("Connected");
69+
5770
this.client
5871
.setActivity(presenceData.presenceData)
5972
.catch(() => this.destroy());
@@ -113,6 +126,7 @@ export function clearActivity(clientId: string = undefined) {
113126
}
114127

115128
export async function getDiscordUser() {
129+
updateTray("Connecting");
116130
return new Promise((resolve, reject) => {
117131
const c = new Client({ transport: "ipc" });
118132

@@ -128,3 +142,10 @@ app.once(
128142
"will-quit",
129143
async () => await Promise.all(rpcClients.map((c) => c.destroy()))
130144
);
145+
146+
function updateTray(reason: string = "Connecting") {
147+
if (!connectionState || (connectionState && connectionState !== reason)) {
148+
connectionState = reason;
149+
if (trayManager) trayManager.update();
150+
}
151+
}

src/managers/launchManager.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,8 @@ let autoLaunch = new AutoLaunch({
1414
export function update() {
1515
//* If app not packaged return
1616
//* Either enable/disable autolaunch
17-
if (!app.isPackaged || (app.isPackaged && app.name.includes("Portable"))) {
18-
//* Return
19-
console.log(
20-
(app.isPackaged
21-
? "Portable version detected,"
22-
: "Debug version detected,") + " skipping autoLaunch"
23-
);
17+
if (!app.isPackaged || (app.isPackaged && app.name.includes("Portable")))
2418
return;
25-
}
2619
//* Enable if disabled and vice versa
2720
settings.get("autoLaunch") ? autoLaunch.enable() : autoLaunch.disable();
2821
}

src/managers/socketManager.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import { trayManager } from "..";
1616
export let io: socketIo.Server;
1717
export let socket: socketIo.Socket;
1818
export let server: Server;
19-
export let connected: boolean = false;
19+
20+
//* States :
21+
//* - Disconnected
22+
//* - Connected
23+
//* - Connecting
24+
export let connectionState: String = "Connecting";
2025

2126
export function init() {
2227
return new Promise((resolve) => {
@@ -58,14 +63,14 @@ function socketConnection(cSocket: socketIo.Socket) {
5863
socket.emit("receiveVersion", app.getVersion().replace(/[\D]/g, ""))
5964
);
6065
socket.once("disconnect", () => {
61-
connected = false;
62-
if(trayManager && trayManager.tray) trayManager.update();
66+
updateTray("Disconnected");
67+
if (trayManager && trayManager.tray) trayManager.update();
6368
//* Destroy all open RPC connections
6469
console.log("Socket disconnected.");
6570
rpcClients.forEach((c) => c.destroy());
6671
});
67-
connected = true;
68-
if(trayManager && trayManager.tray) trayManager.update();
72+
updateTray("Connected");
73+
if (trayManager && trayManager.tray) trayManager.update();
6974
}
7075

7176
app.on("quit", () => {
@@ -76,6 +81,7 @@ app.on("quit", () => {
7681
async function socketError(e: any) {
7782
//* If port in use
7883
if (e.code === "EADDRINUSE") {
84+
updateTray("Disconnected");
7985
//* Focus app
8086
//* Show error dialog
8187
//* Exit app afterwards
@@ -89,3 +95,10 @@ async function socketError(e: any) {
8995
console.log(`Socket error :\n${e.message}`);
9096
}
9197
}
98+
99+
function updateTray(reason: string = "Connecting") {
100+
if (!connectionState || (connectionState && connectionState !== reason)) {
101+
connectionState = reason;
102+
if (trayManager) trayManager.update();
103+
}
104+
}

src/managers/trayManager.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Menu, Tray, app, shell } from "electron";
22
import { join } from "path";
3-
import { trayManager } from "..";
43
import { checkForUpdate, updateProcess } from "../util/updateChecker";
5-
import { connected } from "./socketManager";
4+
import { connectionState as socketState } from "./socketManager";
5+
import { connectionState as discordState } from "./discordManager";
6+
import { settings } from "./settingsManager";
7+
import { update as updateAutoLaunch } from "./launchManager";
68

79
export class TrayManager {
810
tray: Tray;
@@ -14,6 +16,7 @@ export class TrayManager {
1416

1517
this.update();
1618
this.tray.on("click", () => this.update());
19+
this.tray.on("right-click", () => this.update());
1720
}
1821

1922
update() {
@@ -24,20 +27,32 @@ export class TrayManager {
2427
enabled: false
2528
},
2629
{
27-
id: "connectInfo",
28-
label: `Extension - ${connected ? "Connected" : "Not connected"}`,
29-
enabled: false
30+
type: "separator"
3031
},
3132
{
32-
type: "separator"
33+
label: `Extension - ${socketState ? socketState : "Connecting"}`,
34+
enabled: false
3335
},
3436
{
35-
label: "Presence Store",
36-
click: () => shell.openExternal("https://premid.app/store")
37+
label: `Discord - ${discordState ? socketState : "Disconnected"}`,
38+
enabled: false
3739
},
3840
{
3941
type: "separator"
4042
},
43+
{
44+
label: "Auto launch",
45+
type: "checkbox",
46+
click: () => {
47+
settings.get("autoLaunch")
48+
? settings.set("autoLaunch", false)
49+
: settings.set("autoLaunch", true);
50+
updateAutoLaunch();
51+
},
52+
checked: settings.get("autoLaunch") == true,
53+
enabled: app.isPackaged,
54+
visible: !app.name.includes("Portable")
55+
},
4156
{
4257
label: "Check for updates",
4358
click: () => checkForUpdate(),
@@ -64,6 +79,13 @@ export class TrayManager {
6479
updateProcess &&
6580
updateProcess === "checking"
6681
},
82+
{
83+
type: "separator"
84+
},
85+
{
86+
label: "Presence Store",
87+
click: () => shell.openExternal("https://premid.app/store")
88+
},
6789
{
6890
label: "Acknowledgments",
6991
click: () => shell.openExternal("https://premid.app/contributors")
@@ -72,16 +94,10 @@ export class TrayManager {
7294
type: "separator"
7395
},
7496
{
75-
label: `Quit ${app.name}`,
97+
label: `Quit`,
7698
role: "quit"
7799
}
78100
])
79101
);
80102
}
81103
}
82-
83-
app.once("quit", () => {
84-
if (trayManager && trayManager.tray) {
85-
trayManager.tray.destroy();
86-
}
87-
});

src/util/updateChecker.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ autoUpdater.on("error", (error: any) => {
4444
errHandler(error);
4545
});
4646

47-
function updateTray(reason: string) {
48-
updateProcess = reason;
49-
trayManager.update();
47+
function updateTray(reason: string = "standby") {
48+
if (!updateProcess || (updateProcess && updateProcess !== reason)) {
49+
updateProcess = reason;
50+
if (trayManager) trayManager.update();
51+
}
5052
}
5153

5254
// Temporarily

0 commit comments

Comments
 (0)