Skip to content

Commit

Permalink
Replace auto-launch with native autostart
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Dec 13, 2023
1 parent 3deafca commit 7d44507
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 84 deletions.
67 changes: 5 additions & 62 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@
"@buttercup/importer": "^3.1.0",
"@buttercup/secure-file-host": "^0.3.0",
"@electron/remote": "^2.0.8",
"auto-launch": "^5.0.6",
"buttercup": "^7.4.0",
"debounce": "^1.2.1",
"debounce-promise": "^3.1.2",
Expand Down
2 changes: 1 addition & 1 deletion source/main/actions/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { getOSLocale } from "../services/locale";
import { changeLanguage } from "../../shared/i18n/trans";
import { getLanguage } from "../../shared/library/i18n";
import { startFileHost, stopFileHost } from "../services/fileHost";
import { setStartWithSession } from "../services/launch";
import { start as startBrowserAPI, stop as stopBrowserAPI } from "../services/browser/index";
import { setStartWithSession } from "../services/config";
import { Preferences } from "../types";

export async function handleConfigUpdate(preferences: Preferences) {
Expand Down
11 changes: 7 additions & 4 deletions source/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { initialise } from "./services/init";
import { openMainWindow } from "./services/windows";
import { handleProtocolCall } from "./services/protocol";
import { getConfigValue } from "./services/config";
import { shouldShowMainWindow } from "./services/arguments";
import { shouldShowMainWindow, wasAutostarted } from "./services/arguments";
import { logErr, logInfo } from "./library/log";
import { BUTTERCUP_PROTOCOL } from "./symbols";
import { AppStartMode } from "./types";
Expand Down Expand Up @@ -74,9 +74,12 @@ app.whenReady()
})
.then(async () => {
const preferences = await getConfigValue("preferences");
const hideInTray = preferences.startMode === AppStartMode.HiddenAlways;
if (!shouldShowMainWindow() || hideInTray) {
logInfo("Opening initial window disabled by CL or preferences");
const autostarted = wasAutostarted();
if (!shouldShowMainWindow() || preferences.startMode === AppStartMode.HiddenAlways) {
logInfo("Not opening initial window: disabled by CL or preferences");
return;
} else if (autostarted && preferences.startMode === AppStartMode.HiddenOnBoot) {
logInfo("Not opening initial window: disabled for autostart");
return;
}
openMainWindow();
Expand Down
10 changes: 9 additions & 1 deletion source/main/services/arguments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { app } from "electron";

let __showMainWindow = true;
let __autostarted = false,
__showMainWindow = true;

export function processCLFlags() {
const cl = app.commandLine;
Expand All @@ -11,8 +12,15 @@ export function processCLFlags() {
if (cl.hasSwitch("hidden")) {
__showMainWindow = false;
}
if (cl.hasSwitch("autostart")) {
__autostarted = true;
}
}

export function shouldShowMainWindow(): boolean {
return __showMainWindow;
}

export function wasAutostarted(): boolean {
return __autostarted;
}
17 changes: 2 additions & 15 deletions source/main/services/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import AutoLaunch from "auto-launch";
import fs from "fs/promises";
import { VaultSourceID } from "buttercup";
import { getConfigStorage, getVaultSettingsPath, getVaultSettingsStorage } from "./storage";
import { naiveClone } from "../../shared/library/clone";
import { logErr, logInfo } from "../library/log";
import { PREFERENCES_DEFAULT, VAULT_SETTINGS_DEFAULT } from "../../shared/symbols";
import { AppStartMode, Config, Preferences, VaultSettingsLocal } from "../types";
import { runConfigMigrations } from "./migration";
import { PREFERENCES_DEFAULT, VAULT_SETTINGS_DEFAULT } from "../../shared/symbols";
import { Config, VaultSettingsLocal } from "../types";

const DEFAULT_CONFIG: Config = {
browserClients: {},
Expand Down Expand Up @@ -81,15 +80,3 @@ export async function setVaultSettings(
const storage = getVaultSettingsStorage(sourceID);
await storage.setValues(settings);
}

export async function setStartWithSession(enable: boolean): Promise<void> {
const autoLauncher = new AutoLaunch({
name: "Buttercup"
});
const isEnabled = await autoLauncher.isEnabled();
if (enable && !isEnabled) {
await autoLauncher.enable();
} else if (!enable && isEnabled) {
await autoLauncher.disable();
}
}
16 changes: 16 additions & 0 deletions source/main/services/launch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { app } from "electron";

export async function setStartWithSession(enable: boolean): Promise<void> {
const isEnabled = app.getLoginItemSettings().openAtLogin;
if (enable && !isEnabled) {
app.setLoginItemSettings({
openAtLogin: true,
args: ["--autostart"]
});
} else if (!enable && isEnabled) {
app.setLoginItemSettings({
openAtLogin: false,
args: []
});
}
}

0 comments on commit 7d44507

Please sign in to comment.