1
0
mirror of https://github.com/rhysd/Mstdn.git synced 2025-01-22 21:52:09 +01:00
Mstdn/main/app.ts

53 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-04-15 16:32:44 +09:00
import * as path from 'path';
import {app, Menu, globalShortcut} from 'electron';
2017-04-15 16:32:44 +09:00
import log from './log';
import {Config, Account} from './config';
import AccountSwitcher from './account_switcher';
2017-04-17 18:40:55 +09:00
import defaultMenu from './default_menu';
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
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());
this.switcher = new AccountSwitcher(this.config.accounts);
this.switcher.on('switch', this.onAccountSwitch);
2017-04-17 18:40:55 +09:00
}
start() {
2017-04-18 20:39:47 +09:00
const a = this.switcher.current;
const url = `https://${a.host}${a.default_page}`;
this.win.open(url);
2017-04-18 20:39:47 +09:00
log.debug('Application started', a, url);
2017-04-15 16:32:44 +09:00
}
private onAccountSwitch = (next: Account) => {
this.win.close();
if (this.config.hot_key) {
2017-04-18 20:39:47 +09:00
log.debug('Disable global shortcut for switching account');
globalShortcut.unregister(this.config.hot_key);
}
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);
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) {
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
}