diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2018-07-10 20:22:41 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2018-07-16 11:38:07 +0200 |
| commit | 0bc0f175ebdf6d0464cd1501c83b2f0c10816801 (patch) | |
| tree | 0c9383f6e2d13bf3d3dc7dd2c95546b34cfe8234 | |
| parent | fb6cb3737ed2b1d59e712b02f05f521f25343e4a (diff) | |
| download | mullvadvpn-0bc0f175ebdf6d0464cd1501c83b2f0c10816801.tar.xz mullvadvpn-0bc0f175ebdf6d0464cd1501c83b2f0c10816801.zip | |
Add helper to check/add app to auto start
| -rw-r--r-- | app/lib/platform.android.js | 10 | ||||
| -rw-r--r-- | app/lib/platform.js | 33 |
2 files changed, 41 insertions, 2 deletions
diff --git a/app/lib/platform.android.js b/app/lib/platform.android.js index 1dc450b359..5b3abb4496 100644 --- a/app/lib/platform.android.js +++ b/app/lib/platform.android.js @@ -9,6 +9,14 @@ const getAppVersion = () => { return version; }; +const getOpenAtLogin = () => { + throw new Error('Not implemented'); +}; + +const setOpenAtLogin = (_autoStart: boolean) => { + throw new Error('Not implemented'); +}; + const exit = () => { BackHandler.exitApp(); }; @@ -21,4 +29,4 @@ const openItem = (path: string) => { MobileAppBridge.openItem(path); }; -export { log, exit, openLink, openItem, getAppVersion }; +export { log, exit, openLink, openItem, getAppVersion, getOpenAtLogin, setOpenAtLogin }; diff --git a/app/lib/platform.js b/app/lib/platform.js index ecbd32a27a..0e46341149 100644 --- a/app/lib/platform.js +++ b/app/lib/platform.js @@ -1,6 +1,10 @@ // @flow import { remote, shell } from 'electron'; import electronLog from 'electron-log'; +import { execFile } from 'child_process'; +import { promisify } from 'util'; + +const execFileAsync = promisify(execFile); const log = electronLog; @@ -8,6 +12,33 @@ const getAppVersion = () => { return remote.app.getVersion(); }; +const getOpenAtLogin = () => { + return remote.app.getLoginItemSettings().openAtLogin; +}; + +const setOpenAtLogin = async (openAtLogin: boolean) => { + // setLoginItemSettings is broken on macOS and cannot delete login items. + // Issue: https://github.com/electron/electron/issues/10880 + if (process.platform === 'darwin' && openAtLogin === false) { + // process.execPath in renderer process points to the sub-bundle of Electron Helper. + // This regular expression extracts the path to the app bundle, which is the first occurrence of + // file with .app extension. + const matches = process.execPath.match(/([a-z0-9 ]+)\.app/i); + if (matches && matches.length > 1) { + const bundleName = matches[1]; + const appleScript = `on run argv + set itemName to item 1 of argv + tell application "System Events" to delete login item itemName + end run`; + await execFileAsync('osascript', ['-e', appleScript, bundleName]); + } else { + log.error(`Cannot extract the app bundle name from ${process.execPath}`); + } + } else { + remote.app.setLoginItemSettings({ openAtLogin }); + } +}; + const exit = () => { remote.app.quit(); }; @@ -20,4 +51,4 @@ const openItem = (path: string) => { shell.openItem(path); }; -export { log, exit, openLink, openItem, getAppVersion }; +export { log, exit, openLink, openItem, getAppVersion, getOpenAtLogin, setOpenAtLogin }; |
