Merge pull request #846 from yuya-oc/startup-via-config-json

Startup via config json
This commit is contained in:
Yuya Ochiai
2018-09-12 23:23:31 +09:00
committed by GitHub
4 changed files with 61 additions and 37 deletions

View File

@@ -10,7 +10,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';
@@ -19,11 +18,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,7 +52,6 @@ export default class SettingsPage extends React.Component {
this.didSaveConfig = this.didSaveConfig.bind(this);
this.handleTeamsChange = this.handleTeamsChange.bind(this);
this.saveConfig = this.saveConfig.bind(this);
this.saveAutoStart = this.saveAutoStart.bind(this);
this.handleCancel = this.handleCancel.bind(this);
this.handleChangeShowTrayIcon = this.handleChangeShowTrayIcon.bind(this);
this.handleChangeTrayIconTheme = this.handleChangeTrayIconTheme.bind(this);
@@ -77,14 +70,6 @@ export default class SettingsPage extends React.Component {
}
componentDidMount() {
if (process.platform === 'win32' || process.platform === 'linux') {
const self = this;
appLauncher.isEnabled().then((enabled) => {
self.setState({
autostart: enabled,
});
});
}
ipcRenderer.on('add-server', () => {
this.setState({
showAddTeamForm: true,
@@ -159,6 +144,7 @@ export default class SettingsPage extends React.Component {
useSpellChecker: this.state.useSpellChecker,
spellCheckerLocale: this.state.spellCheckerLocale,
enableHardwareAcceleration: this.state.enableHardwareAcceleration,
autostart: this.state.autostart,
};
settings.writeFile(this.props.configFile, config, (err) => {
@@ -168,31 +154,10 @@ export default class SettingsPage extends React.Component {
}
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();
}

View File

@@ -20,6 +20,7 @@ const defaultPreferences = {
showUnreadBadge: true,
useSpellChecker: true,
enableHardwareAcceleration: true,
autostart: true,
};
export default defaultPreferences;

View File

@@ -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();
@@ -99,6 +100,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);

48
src/main/AutoLauncher.js Normal file
View File

@@ -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();
}
}