From 15bc264fd5b1e70442036f3c984ce61b4f3c34f2 Mon Sep 17 00:00:00 2001 From: Yuya Ochiai Date: Tue, 7 Aug 2018 23:47:43 +0900 Subject: [PATCH 1/2] Keep autostart config in sync with config.json --- src/browser/components/SettingsPage.jsx | 38 ++------------------ src/common/config/defaultPreferences.js | 1 + src/main.js | 10 ++++++ src/main/AutoLauncher.js | 48 +++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 36 deletions(-) create mode 100644 src/main/AutoLauncher.js diff --git a/src/browser/components/SettingsPage.jsx b/src/browser/components/SettingsPage.jsx index 0d540183..7fd2c7fe 100644 --- a/src/browser/components/SettingsPage.jsx +++ b/src/browser/components/SettingsPage.jsx @@ -8,7 +8,6 @@ import ReactDOM from 'react-dom'; import {Button, Checkbox, Col, FormGroup, Grid, HelpBlock, Navbar, Radio, Row} from 'react-bootstrap'; import {ipcRenderer, remote} from 'electron'; -import AutoLaunch from 'auto-launch'; import {debounce} from 'underscore'; import buildConfig from '../../common/config/buildConfig'; @@ -17,11 +16,6 @@ import settings from '../../common/settings'; import TeamList from './TeamList.jsx'; import AutoSaveIndicator from './AutoSaveIndicator.jsx'; -const appLauncher = new AutoLaunch({ - name: remote.app.getName(), - isHidden: true, -}); - function backToIndex(index) { const target = typeof index === 'undefined' ? 0 : index; const indexURL = remote.getGlobal('isDev') ? 'http://localhost:8080/browser/index.html' : `file://${remote.app.getAppPath()}/browser/index.html`; @@ -58,14 +52,6 @@ const SettingsPage = createReactClass({ return initialState; }, componentDidMount() { - if (process.platform === 'win32' || process.platform === 'linux') { - var self = this; - appLauncher.isEnabled().then((enabled) => { - self.setState({ - autostart: enabled, - }); - }); - } ipcRenderer.on('add-server', () => { this.setState({ showAddTeamForm: true, @@ -140,6 +126,7 @@ const SettingsPage = createReactClass({ useSpellChecker: this.state.useSpellChecker, spellCheckerLocale: this.state.spellCheckerLocale, enableHardwareAcceleration: this.state.enableHardwareAcceleration, + autostart: this.state.autostart, }; settings.writeFile(this.props.configFile, config, (err) => { @@ -149,31 +136,10 @@ const SettingsPage = createReactClass({ } ipcRenderer.send('update-menu', config); ipcRenderer.send('update-config'); - if (process.platform === 'win32' || process.platform === 'linux') { - const autostart = this.state.autostart; - this.saveAutoStart(autostart, callback); - } else { - callback(); - } + callback(); }); }, - saveAutoStart(autostart, callback) { - appLauncher.isEnabled().then((enabled) => { - if (enabled && !autostart) { - appLauncher.disable().then(() => { - callback(); - }).catch(callback); - } else if (!enabled && autostart) { - appLauncher.enable().then(() => { - callback(); - }).catch(callback); - } else { - callback(); - } - }).catch(callback); - }, - handleCancel() { backToIndex(); }, diff --git a/src/common/config/defaultPreferences.js b/src/common/config/defaultPreferences.js index 3c0ca30b..6cd9dc40 100644 --- a/src/common/config/defaultPreferences.js +++ b/src/common/config/defaultPreferences.js @@ -20,6 +20,7 @@ const defaultPreferences = { showUnreadBadge: true, useSpellChecker: true, enableHardwareAcceleration: true, + autostart: false, }; export default defaultPreferences; diff --git a/src/main.js b/src/main.js index e6cfd1ea..42466a58 100644 --- a/src/main.js +++ b/src/main.js @@ -24,6 +24,7 @@ import {parse as parseArgv} from 'yargs'; import {protocols} from '../electron-builder.json'; import squirrelStartup from './main/squirrelStartup'; +import AutoLauncher from './main/AutoLauncher'; import CriticalErrorHandler from './main/CriticalErrorHandler'; const criticalErrorHandler = new CriticalErrorHandler(); @@ -102,6 +103,15 @@ if (config.enableHardwareAcceleration === false) { ipcMain.on('update-config', () => { const configFile = app.getPath('userData') + '/config.json'; config = settings.readFileSync(configFile); + if (process.platform === 'win32' || process.platform === 'linux') { + const appLauncher = new AutoLauncher(); + const autoStartTask = config.autostart ? appLauncher.enable() : appLauncher.disable(); + autoStartTask.then(() => { + console.log('config.autostart has been configured:', config.autostart); + }).catch((err) => { + console.log('error:', err); + }); + } const trustedURLs = settings.mergeDefaultTeams(config.teams).map((team) => team.url); permissionManager.setTrustedURLs(trustedURLs); ipcMain.emit('update-dict', true, config.spellCheckerLocale); diff --git a/src/main/AutoLauncher.js b/src/main/AutoLauncher.js new file mode 100644 index 00000000..2251c7c7 --- /dev/null +++ b/src/main/AutoLauncher.js @@ -0,0 +1,48 @@ +// Copyright (c) 2015-2016 Yuya Ochiai +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import AutoLaunch from 'auto-launch'; +import {app} from 'electron'; +import isDev from 'electron-is-dev'; + +export default class AutoLauncher { + constructor() { + this.appLauncher = new AutoLaunch({ + name: app.getName(), + isHidden: true, + }); + } + + isEnabled() { + return this.appLauncher.isEnabled(); + } + + async blankPromise() { + return null; + } + + async enable() { + if (isDev) { + console.log('In development mode, autostart config never effects'); + return this.blankPromise(); + } + const enabled = await this.isEnabled(); + if (!enabled) { + return this.appLauncher.enable(); + } + return this.blankPromise(); + } + + async disable() { + if (isDev) { + console.log('In development mode, autostart config never effects'); + return this.blankPromise(); + } + const enabled = await this.isEnabled(); + if (enabled) { + return this.appLauncher.disable(); + } + return this.blankPromise(); + } +} From ac8c692c45e343eda9a576fb40f29317be92bf52 Mon Sep 17 00:00:00 2001 From: Yuya Ochiai Date: Fri, 10 Aug 2018 21:40:53 +0900 Subject: [PATCH 2/2] Set "app start on login" preference to default on --- src/common/config/defaultPreferences.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/config/defaultPreferences.js b/src/common/config/defaultPreferences.js index 6cd9dc40..8a26665f 100644 --- a/src/common/config/defaultPreferences.js +++ b/src/common/config/defaultPreferences.js @@ -20,7 +20,7 @@ const defaultPreferences = { showUnreadBadge: true, useSpellChecker: true, enableHardwareAcceleration: true, - autostart: false, + autostart: true, }; export default defaultPreferences;