2017-04-15 16:32:44 +09:00
|
|
|
import * as path from 'path';
|
2017-04-18 15:48:50 +09:00
|
|
|
import {app, Menu} from 'electron';
|
2017-04-15 16:32:44 +09:00
|
|
|
import log from './log';
|
2017-04-17 18:40:55 +09:00
|
|
|
import {Config} from './config';
|
2017-04-18 15:48:50 +09:00
|
|
|
import AccountSwitcher from './account_switcher';
|
2017-04-17 18:40:55 +09:00
|
|
|
import defaultMenu from './default_menu';
|
2017-04-18 15:48:50 +09:00
|
|
|
import Window from './window';
|
2017-04-15 16:32:44 +09:00
|
|
|
|
|
|
|
const IS_DARWIN = process.platform === 'darwin';
|
2017-04-16 22:07:13 +09:00
|
|
|
const APP_ICON = path.join(__dirname, '..', 'resources', 'icon', 'icon.png');
|
2017-04-15 16:32:44 +09:00
|
|
|
|
|
|
|
export class App {
|
2017-04-17 18:40:55 +09:00
|
|
|
private switcher: AccountSwitcher;
|
2017-04-15 16:32:44 +09:00
|
|
|
|
2017-04-18 15:48:50 +09:00
|
|
|
constructor(private win: Window, private config: Config) {
|
2017-04-15 16:32:44 +09:00
|
|
|
if (config.accounts.length === 0) {
|
|
|
|
throw new Error('No account found. Please check the config.');
|
|
|
|
}
|
2017-04-17 18:40:55 +09:00
|
|
|
if (IS_DARWIN) {
|
|
|
|
app.dock.setIcon(APP_ICON);
|
|
|
|
}
|
|
|
|
Menu.setApplicationMenu(defaultMenu());
|
2017-04-18 15:48:50 +09:00
|
|
|
this.switcher = new AccountSwitcher(win.browser, this.config.accounts);
|
2017-04-17 18:40:55 +09:00
|
|
|
this.switcher.on('did-switch', () => this.open());
|
|
|
|
}
|
|
|
|
|
|
|
|
open() {
|
2017-04-18 15:48:50 +09:00
|
|
|
const url = `https://${this.switcher.current.host}${this.switcher.current.default_page}`;
|
|
|
|
this.win.open(url);
|
|
|
|
log.debug('Open URL: ', url);
|
2017-04-15 16:32:44 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 12:44:53 +09:00
|
|
|
export default function startApp(config: Config) {
|
2017-04-18 15:48:50 +09:00
|
|
|
return Window.create(config).then(win => new App(win, config));
|
2017-04-15 16:32:44 +09:00
|
|
|
}
|