2017-04-19 15:51:41 +09:00
|
|
|
import {app, Menu, globalShortcut, Tray} from 'electron';
|
2017-04-15 16:32:44 +09:00
|
|
|
import log from './log';
|
2017-04-18 20:29:07 +09:00
|
|
|
import {Config, Account} 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-19 16:13:33 +09:00
|
|
|
import {IS_DARWIN, APP_ICON, trayIcon} from './common';
|
2017-04-19 15:51:41 +09:00
|
|
|
|
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 20:29:07 +09:00
|
|
|
this.switcher = new AccountSwitcher(this.config.accounts);
|
|
|
|
this.switcher.on('switch', this.onAccountSwitch);
|
2017-04-19 15:51:41 +09:00
|
|
|
|
2017-04-19 16:16:55 +09:00
|
|
|
this.setupTray();
|
|
|
|
this.setupHotkey();
|
|
|
|
}
|
|
|
|
|
2017-04-19 17:19:01 +09:00
|
|
|
start() {
|
|
|
|
const a = this.switcher.current;
|
|
|
|
const url = `https://${a.host}${a.default_page}`;
|
|
|
|
this.win.open(url);
|
|
|
|
log.debug('Application started', a, url);
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:16:55 +09:00
|
|
|
private setupHotkey() {
|
|
|
|
if (!this.config.hot_key) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.win.menubar) {
|
|
|
|
globalShortcut.register(this.config.hot_key, () => {
|
|
|
|
const mb = this.win.menubar!;
|
|
|
|
if (mb.window.isFocused()) {
|
2017-04-19 15:51:41 +09:00
|
|
|
log.debug('Toggle window: shown -> hidden');
|
2017-04-19 16:16:55 +09:00
|
|
|
mb.hideWindow();
|
2017-04-19 15:51:41 +09:00
|
|
|
} else {
|
|
|
|
log.debug('Toggle window: hidden -> shown');
|
2017-04-19 16:16:55 +09:00
|
|
|
mb.showWindow();
|
2017-04-19 15:51:41 +09:00
|
|
|
}
|
2017-04-19 16:16:55 +09:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
globalShortcut.register(this.config.hot_key, this.toggleNormalWindow);
|
2017-04-19 15:51:41 +09:00
|
|
|
}
|
2017-04-19 16:16:55 +09:00
|
|
|
log.debug('Hot key was set to:', this.config.hot_key);
|
2017-04-17 18:40:55 +09:00
|
|
|
}
|
|
|
|
|
2017-04-19 16:16:55 +09:00
|
|
|
private setupTray() {
|
|
|
|
if (this.win.menubar) {
|
|
|
|
// Note:
|
|
|
|
// If it's menubar window, tray will be put in menubar().
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const icon = trayIcon(this.config.icon_color);
|
|
|
|
const tray = new Tray(icon);
|
|
|
|
tray.on('click', this.toggleNormalWindow);
|
|
|
|
tray.on('double-click', this.toggleNormalWindow);
|
|
|
|
if (IS_DARWIN) {
|
|
|
|
tray.setHighlightMode('never');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private toggleNormalWindow = () => {
|
|
|
|
const win = this.win.browser;
|
|
|
|
if (win.isFocused()) {
|
|
|
|
log.debug('Toggle window: shown -> hidden');
|
|
|
|
if (IS_DARWIN) {
|
|
|
|
app.hide();
|
|
|
|
} else {
|
|
|
|
win.hide();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.debug('Toggle window: hidden -> shown');
|
|
|
|
win.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 20:29:07 +09:00
|
|
|
private onAccountSwitch = (next: Account) => {
|
|
|
|
this.win.close();
|
2017-04-19 16:16:55 +09:00
|
|
|
Window.create(next, this.config, this.win.menubar).then(win => {
|
2017-04-18 20:39:47 +09:00
|
|
|
log.debug('Window was recreated again', next);
|
2017-04-18 20:29:07 +09:00
|
|
|
this.win = win;
|
|
|
|
this.start();
|
|
|
|
});
|
|
|
|
}
|
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 20:29:07 +09:00
|
|
|
const default_account = config.accounts[0];
|
|
|
|
return Window.create(default_account, config)
|
|
|
|
.then(win => new App(win, config));
|
2017-04-15 16:32:44 +09:00
|
|
|
}
|