From ae20cc238a7a2ba2d2dc06c3185e64c0df1a54e6 Mon Sep 17 00:00:00 2001 From: Daniel Andrei Date: Tue, 12 Feb 2019 22:12:58 +0200 Subject: [PATCH] Add `launchAtLogin` and `launchAsHidden` menu options --- source/index.ts | 10 ++++++++++ source/menu.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/source/index.ts b/source/index.ts index 6fff952f0..03c157198 100644 --- a/source/index.ts +++ b/source/index.ts @@ -48,6 +48,16 @@ app.setAppUserModelId('com.sindresorhus.caprine'); // https://github.com/electron/electron/issues/9671 app.commandLine.appendSwitch('disable-color-correct-rendering'); +const appFolder = path.dirname(process.execPath); +const updateExe = path.resolve(appFolder, '..', 'Update.exe'); + +const {openAtLogin, openAsHidden, wasOpenedAsHidden} = app.getLoginItemSettings({ + path: updateExe +}); + +config.set('openAtLogin', openAtLogin); +config.set('openAsHidden', openAsHidden || wasOpenedAsHidden); + if (!config.get('hardwareAcceleration')) { app.disableHardwareAcceleration(); } diff --git a/source/menu.ts b/source/menu.ts index ce13bfa36..187318a1c 100644 --- a/source/menu.ts +++ b/source/menu.ts @@ -13,6 +13,20 @@ import config from './config'; import {sendAction, showRestartDialog} from './util'; import {generateSubmenu as generateEmojiSubmenu} from './emoji'; +function setLoginSettings(): void { + const appFolder = path.dirname(process.execPath); + const updateExe = path.resolve(appFolder, '..', 'Update.exe'); + + const openAtLogin = config.get('openAtLogin'); + const openAsHidden = config.get('openAsHidden'); + + app.setLoginItemSettings({ + openAtLogin, + openAsHidden, + path: updateExe + }); +} + export default function updateMenu(): Menu { const newConversationItem: MenuItemConstructorOptions = { label: 'New Conversation', @@ -190,6 +204,26 @@ Press Command/Ctrl+R in Caprine to see your changes. focusedWindow.setAlwaysOnTop(item.checked); } }, + { + type: 'checkbox', + label: 'Launch at login', + checked: config.get('openAtLogin'), + click(item) { + config.set('openAtLogin', item.checked); + setLoginSettings(); + updateMenu(); + } + }, + { + type: 'checkbox', + label: 'Launch as hidden', + checked: config.get('openAsHidden'), + enabled: config.get('openAtLogin'), + click(item) { + config.set('openAsHidden', item.checked); + setLoginSettings(); + } + }, { label: 'Auto Hide Menu Bar', visible: !is.macos,