armcord/src/tray.ts
Ritiek Malhotra d9d24d9473
Improve multi-instance behaviour (#604)
* Experiment with two windows

* Improve multi-instance behaviour

Previously, ArmCord would attempt to launch up a completely new
instance each time when multi-instances setting were enabled. This
doesn't work well as Electron doesn't support running multiple instances
of the same app pointing to the same user data directory
(which by default on GNU/Linux is `~/.config/ArmCord`). Doing so would
result in this error:
> Failed to open LevelDB database" "file currently in use"

It's possible to workaround this behaviour by passing in a parameter to
a different user data directory when launching subsequent instances of
armcord, like so:
```shell
$ armcord --user-data-directory=$HOME/.config/ArmCord-2
```

However, this method ends up taking disk storage in the multiples of the
number of armcord instances that are simultaneously running, which isn't
ideal.

Looking into this more, it looks like Electron can do multiple windows
fine with the same user data directory. I gave this a try and it seems
to be working nice. With this PR, running any subsequent instances of
armcord will open up a new window in the original armcord instance. This
should also help with better resource utilization when compared to
running multiple full blown instances of armcord.

* Fix lints
2024-06-15 10:20:44 +02:00

129 lines
4.8 KiB
TypeScript

import fs from "fs";
import {Menu, MessageBoxOptions, Tray, app, dialog, nativeImage} from "electron";
import {createInviteWindow, mainWindows} from "./discord/window.js";
import path from "path";
import {createSettingsWindow} from "./settings/main.js";
import {getConfig, getConfigLocation, setConfig} from "./common/config.js";
import {getDisplayVersion} from "./common/version.js";
export let tray: Tray;
let trayIcon = "ac_plug_colored";
void app.whenReady().then(async () => {
// REVIEW - app will hang at startup if line above is awaited.
const finishedSetup = getConfig("doneSetup");
if (getConfig("trayIcon") != "default") {
trayIcon = getConfig("trayIcon");
}
let trayPath = nativeImage.createFromPath(path.join(import.meta.dirname, "../", `/assets/${trayIcon}.png`));
const trayVerIcon = function () {
if (process.platform == "win32") {
return trayPath.resize({height: 16});
} else if (process.platform == "darwin") {
return trayPath.resize({height: 18});
} else if (process.platform == "linux") {
return trayPath.resize({height: 24});
}
return undefined;
};
if (process.platform == "darwin" && trayPath.getSize().height > 22) trayPath = trayPath.resize({height: 22});
if (getConfig("tray")) {
const clientName = getConfig("clientName") ?? "ArmCord";
tray = new Tray(trayPath);
if (finishedSetup == false) {
const contextMenu = Menu.buildFromTemplate([
{
label: `Finish the setup first!`,
enabled: false
},
{
label: `Quit ${clientName}`,
click() {
fs.unlink(getConfigLocation(), (err) => {
if (err) throw err;
console.log('Closed during setup. "settings.json" was deleted');
app.quit();
});
}
}
]);
tray.setContextMenu(contextMenu);
} else {
const contextMenu = Menu.buildFromTemplate([
// REVIEW - Awaiting any window creation will fail silently
{
label: `${clientName} ${getDisplayVersion()}`,
icon: trayVerIcon(),
enabled: false
},
{
type: "separator"
},
{
label: `Open ${clientName}`,
click() {
mainWindows.forEach((mainWindow) => {
mainWindow.show();
});
}
},
{
label: "Open Settings",
click() {
void createSettingsWindow();
}
},
{
label: "Support Discord Server",
click() {
void createInviteWindow("TnhxcqynZ2");
}
},
{
type: "separator"
},
{
label: `Quit ${clientName}`,
click() {
// NOTE - Temporary fix for app not actually quitting
app.exit();
}
}
]);
tray.setContextMenu(contextMenu);
}
tray.setToolTip(clientName);
tray.on("click", function () {
mainWindows.forEach((mainWindow) => {
mainWindow.show();
});
});
} else {
if (getConfig("tray") == undefined) {
if (process.platform == "linux") {
const options: MessageBoxOptions = {
type: "question",
buttons: ["Yes, please", "No, I don't"],
defaultId: 1,
title: "Tray icon choice",
message: `Do you want to use tray icons?`,
detail: "Linux may not work well with tray icons. Depending on your system configuration, you may not be able to see the tray icon. Enable at your own risk. Can be changed later."
};
await dialog.showMessageBox(mainWindows[0], options).then(({response}) => {
if (response == 0) {
setConfig("tray", true);
} else {
setConfig("tray", false);
}
app.relaunch();
app.exit();
});
} else {
setConfig("tray", true);
app.relaunch();
app.exit();
}
}
}
});