thedesk/src/main/Application.ts

87 lines
2.2 KiB
TypeScript
Raw Normal View History

import {
app,
2019-04-24 20:59:30 +09:00
ipcMain,
2019-04-22 19:08:23 +09:00
shell,
2019-04-24 20:59:30 +09:00
systemPreferences,
Event,
Menu,
} from 'electron'
import {
createProtocol,
installVueDevtools,
} from 'vue-cli-plugin-electron-builder/lib'
2019-04-22 19:08:23 +09:00
import Window from './Window'
import Timeline from './Timeline'
2019-04-24 05:13:32 +09:00
import Streaming from './Streaming'
const isDevelopment = process.env.NODE_ENV !== 'production'
export default class Application {
2019-04-22 18:39:16 +09:00
private static _instance: Application
public static get shared(): Application {
// Do you need arguments? Make it a regular static method instead.
2019-04-22 18:39:16 +09:00
return this._instance || (this._instance = new this())
}
2019-04-24 20:59:30 +09:00
public isDarkMode: boolean
private constructor() {
2019-04-24 20:59:30 +09:00
this.isDarkMode = systemPreferences.isDarkMode()
app.on('window-all-closed', () => this.onWindowAllClosed())
2019-04-22 18:39:16 +09:00
app.on('ready', () => this.onReady())
app.on('activate', () => this.onActivated())
2019-04-24 20:59:30 +09:00
systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', () => {
this.isDarkMode = systemPreferences.isDarkMode()
Window.windowMap.forEach(win => {
win.webContents.send('change-color-theme')
})
})
}
public setApplicationMenu(menu: Menu) {
Menu.setApplicationMenu(menu)
}
private onWindowAllClosed() {
if (process.platform !== 'darwin') {
app.quit()
}
}
private async onReady() {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
await installVueDevtools()
} catch (e) {
2019-04-22 13:35:19 +09:00
console.error('Vue Devtools failed to install:', e.toString())
}
}
if (!process.env.WEBPACK_DEV_SERVER_URL) createProtocol('app')
Timeline.ready()
2019-04-24 05:13:32 +09:00
Streaming.ready()
2019-04-24 20:59:30 +09:00
ipcMain.on('dark-theme', (e: Event) => e.returnValue = this.isDarkMode)
Window.Main()
}
private onActivated() {
2019-04-24 02:56:47 +09:00
if (!Window.windowMap.has('main')) {
Window.Main()
}
}
2019-04-22 19:08:23 +09:00
public static openUrl(event: Event, url: string) {
shell.openExternal(url, {
activate: false
}, (err) => {
if (err) console.log(err)
})
}
}