libvesktop: native dbus module for autostart & app badge (#1180)
Both setAppBadge and autoStart at system boot now use dbus calls. This means that autoStart will work in Flatpak
This commit is contained in:
+55
-14
@@ -5,30 +5,32 @@
|
||||
*/
|
||||
|
||||
import { app } from "electron";
|
||||
import { existsSync, mkdirSync, renameSync, rmSync, writeFileSync } from "fs";
|
||||
import { existsSync, mkdirSync, rmSync, writeFileSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { stripIndent } from "shared/utils/text";
|
||||
|
||||
import { IS_FLATPAK } from "./constants";
|
||||
import { requestBackground } from "./dbus";
|
||||
import { Settings, State } from "./settings";
|
||||
import { escapeDesktopFileArgument } from "./utils/desktopFileEscape";
|
||||
|
||||
interface AutoStart {
|
||||
isEnabled(): boolean;
|
||||
enable(): void;
|
||||
disable(): void;
|
||||
}
|
||||
|
||||
function makeAutoStartLinux(): AutoStart {
|
||||
function getEscapedCommandLine() {
|
||||
const args = process.argv.map(escapeDesktopFileArgument);
|
||||
if (Settings.store.autoStartMinimized) args.push("--start-minimized");
|
||||
return args;
|
||||
}
|
||||
|
||||
function makeAutoStartLinuxDesktop(): AutoStart {
|
||||
const configDir = process.env.XDG_CONFIG_HOME || join(process.env.HOME!, ".config");
|
||||
const dir = join(configDir, "autostart");
|
||||
const file = join(dir, "vesktop.desktop");
|
||||
|
||||
// IM STUPID
|
||||
const legacyName = join(dir, "vencord.desktop");
|
||||
if (existsSync(legacyName)) renameSync(legacyName, file);
|
||||
|
||||
// "Quoting must be done by enclosing the argument between double quotes and escaping the double quote character,
|
||||
// backtick character ("`"), dollar sign ("$") and backslash character ("\") by preceding it with an additional backslash character"
|
||||
// https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables
|
||||
const commandLine = process.argv.map(arg => '"' + arg.replace(/["$`\\]/g, "\\$&") + '"').join(" ");
|
||||
|
||||
return {
|
||||
isEnabled: () => existsSync(file),
|
||||
enable() {
|
||||
@@ -37,7 +39,7 @@ function makeAutoStartLinux(): AutoStart {
|
||||
Type=Application
|
||||
Name=Vesktop
|
||||
Comment=Vesktop autostart script
|
||||
Exec=${commandLine}
|
||||
Exec=${getEscapedCommandLine().join(" ")}
|
||||
StartupNotify=false
|
||||
Terminal=false
|
||||
Icon=vesktop
|
||||
@@ -50,10 +52,49 @@ function makeAutoStartLinux(): AutoStart {
|
||||
};
|
||||
}
|
||||
|
||||
function makeAutoStartLinuxPortal() {
|
||||
return {
|
||||
isEnabled: () => State.store.linuxAutoStartEnabled === true,
|
||||
enable() {
|
||||
const success = requestBackground(true, getEscapedCommandLine());
|
||||
if (success) {
|
||||
State.store.linuxAutoStartEnabled = true;
|
||||
}
|
||||
return success;
|
||||
},
|
||||
disable() {
|
||||
const success = requestBackground(false, []);
|
||||
if (success) {
|
||||
State.store.linuxAutoStartEnabled = false;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const autoStartWindowsMac: AutoStart = {
|
||||
isEnabled: () => app.getLoginItemSettings().openAtLogin,
|
||||
enable: () => app.setLoginItemSettings({ openAtLogin: true }),
|
||||
enable: () =>
|
||||
app.setLoginItemSettings({
|
||||
openAtLogin: true,
|
||||
args: Settings.store.autoStartMinimized ? ["--start-minimized"] : []
|
||||
}),
|
||||
disable: () => app.setLoginItemSettings({ openAtLogin: false })
|
||||
};
|
||||
|
||||
export const autoStart = process.platform === "linux" ? makeAutoStartLinux() : autoStartWindowsMac;
|
||||
// The portal call uses the app id by default, which is org.chromium.Chromium, even in packaged Vesktop.
|
||||
// This leads to an autostart entry named "Chromium" instead of "Vesktop".
|
||||
// Thus, only use the portal inside Flatpak, where the app is actually correct.
|
||||
// Maybe there is a way to fix it outside of flatpak, but I couldn't figure it out.
|
||||
export const autoStart =
|
||||
process.platform !== "linux"
|
||||
? autoStartWindowsMac
|
||||
: IS_FLATPAK
|
||||
? makeAutoStartLinuxPortal()
|
||||
: makeAutoStartLinuxDesktop();
|
||||
|
||||
Settings.addChangeListener("autoStartMinimized", () => {
|
||||
if (!autoStart.isEnabled()) return;
|
||||
|
||||
autoStart.enable();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user