2019-04-05 19:40:00 +11:00
|
|
|
'use strict'
|
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
import * as path from 'path'
|
|
|
|
import { pick } from 'lodash'
|
2019-04-06 22:38:17 +11:00
|
|
|
import {
|
2019-04-09 01:06:28 +10:00
|
|
|
app,
|
|
|
|
ipcMain,
|
|
|
|
protocol,
|
|
|
|
shell,
|
|
|
|
BrowserWindow,
|
|
|
|
Menu,
|
2019-04-06 22:38:17 +11:00
|
|
|
} from 'electron'
|
2019-04-09 01:06:28 +10:00
|
|
|
// Electron types
|
|
|
|
import {
|
|
|
|
App,
|
|
|
|
BrowserWindowConstructorOptions,
|
|
|
|
Event,
|
|
|
|
MenuItemConstructorOptions,
|
|
|
|
} from 'electron'
|
|
|
|
|
2019-04-06 22:38:17 +11:00
|
|
|
import ContextMenu from 'electron-context-menu'
|
2019-04-05 19:40:00 +11:00
|
|
|
import {
|
2019-04-09 01:06:28 +10:00
|
|
|
createProtocol,
|
|
|
|
installVueDevtools
|
2019-04-05 19:40:00 +11:00
|
|
|
} from 'vue-cli-plugin-electron-builder/lib'
|
2019-04-09 01:06:28 +10:00
|
|
|
// tslint:disable-next-line:no-var-requires
|
|
|
|
const localShortcut = require('electron-localshortcut')
|
2019-04-08 01:55:27 +10:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
export type PackageJson = typeof import('../package.json');
|
2019-04-08 02:14:32 +10:00
|
|
|
import { bugs, homepage } from '../package.json'
|
2019-04-09 01:06:28 +10:00
|
|
|
import TheDeskInfo from '../info.json'
|
|
|
|
export type TheDeskInfoObject = typeof TheDeskInfo;
|
2019-04-06 02:38:20 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
declare const __static: string;
|
2019-04-08 02:14:32 +10:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
ipcMain.on('thedesk-info', (event: Event) => {
|
2019-04-09 01:38:04 +10:00
|
|
|
event.returnValue = Object.assign({
|
2019-04-09 01:06:28 +10:00
|
|
|
productName: app.getName(),
|
|
|
|
homePage: homepage,
|
2019-04-09 01:38:04 +10:00
|
|
|
versions: Object.assign(pick(process.versions, ["chrome", "electron", "node"]), { internal: app.getVersion() }),
|
2019-04-09 01:06:28 +10:00
|
|
|
}, TheDeskInfo)
|
|
|
|
})
|
2019-04-05 19:40:00 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production'
|
2019-04-05 19:40:00 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
if (isDevelopment) {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
process.on('message', data => {
|
|
|
|
if (data === 'graceful-exit') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
app.quit()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 23:19:58 +11:00
|
|
|
|
2019-04-05 19:40:00 +11:00
|
|
|
protocol.registerStandardSchemes(['app'], { secure: true })
|
2019-04-06 22:38:17 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
interface CreateWindowOptions {
|
|
|
|
windowName: string
|
|
|
|
loadPath: string
|
|
|
|
windowOptions?: BrowserWindowConstructorOptions
|
|
|
|
singleton?: boolean
|
|
|
|
lastAction?: (win: BrowserWindow) => void
|
|
|
|
openDevTools?: boolean
|
|
|
|
}
|
2019-04-08 01:55:27 +10:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
class Application {
|
|
|
|
public app: App;
|
|
|
|
public windows: { [key: string]: BrowserWindow } = {};
|
2019-04-05 19:40:00 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
constructor(app: App) {
|
|
|
|
this.app = app;
|
|
|
|
ContextMenu()
|
|
|
|
this.app.on('window-all-closed', () => this.onWindowAllClosed())
|
|
|
|
this.app.on('ready', () => this.onReady());
|
|
|
|
this.app.on('activate', () => this.onActivated());
|
|
|
|
}
|
|
|
|
|
|
|
|
private onWindowAllClosed() {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.app.quit()
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 01:55:27 +10:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
private async onReady() {
|
|
|
|
if (isDevelopment && !process.env.IS_TEST) {
|
|
|
|
// Install Vue Devtools
|
|
|
|
try {
|
|
|
|
await installVueDevtools()
|
|
|
|
} catch (e) {
|
|
|
|
//console.error('Vue Devtools failed to install:', e.toString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!process.env.WEBPACK_DEV_SERVER_URL) createProtocol('app')
|
|
|
|
this.openMainWindow()
|
|
|
|
}
|
2019-04-08 01:55:27 +10:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
private onActivated() {
|
|
|
|
if (typeof this.windows.main === 'undefined') {
|
|
|
|
this.openMainWindow()
|
|
|
|
}
|
|
|
|
}
|
2019-04-06 22:38:17 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
public openMainWindow() {
|
|
|
|
const opts: CreateWindowOptions = {
|
|
|
|
windowName: 'main',
|
|
|
|
loadPath: 'index.html',
|
|
|
|
windowOptions: {
|
|
|
|
icon: path.join(__static, 'icon.png'),
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
},
|
|
|
|
singleton: true,
|
|
|
|
lastAction: (win) => {
|
|
|
|
localShortcut.register(win, 'F5', () => this.windows.main.reload())
|
|
|
|
},
|
|
|
|
openDevTools: !process.env.IS_TEST
|
|
|
|
}
|
|
|
|
this.createWindow(opts)
|
2019-04-08 05:28:54 +10:00
|
|
|
}
|
2019-04-09 01:06:28 +10:00
|
|
|
|
|
|
|
public openAboutWindow() {
|
|
|
|
const opts: CreateWindowOptions = {
|
|
|
|
windowName: 'about',
|
|
|
|
loadPath: 'about.html',
|
|
|
|
windowOptions: {
|
|
|
|
width: 296,
|
|
|
|
height: 432,
|
|
|
|
resizable: false,
|
|
|
|
minimizable: false,
|
|
|
|
maximizable: false,
|
|
|
|
fullscreenable: false,
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
titleBarStyle: 'hiddenInset',
|
|
|
|
},
|
|
|
|
singleton: true,
|
|
|
|
lastAction: (win) => {
|
|
|
|
win.setMenuBarVisibility(false)
|
|
|
|
win.webContents.on('before-input-event', (event, input) => {
|
|
|
|
if (typeof this.windows.about !== 'undefined')
|
|
|
|
this.windows.about.webContents.setIgnoreMenuShortcuts((input.meta || input.control) && input.key !== "R" || input.key === "F5")
|
|
|
|
})
|
|
|
|
localShortcut.register(win, 'Esc', () => this.windows.about.destroy())
|
|
|
|
},
|
|
|
|
openDevTools: !process.env.IS_TEST
|
|
|
|
}
|
|
|
|
this.createWindow(opts)
|
2019-04-08 01:55:27 +10:00
|
|
|
}
|
2019-04-05 19:40:00 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
private async createWindow(options: CreateWindowOptions) {
|
|
|
|
if (typeof this.windows[options.windowName] !== 'undefined') {
|
|
|
|
this.windows[options.windowName].show()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let win = new BrowserWindow(options.windowOptions)
|
|
|
|
win.hide()
|
|
|
|
win.webContents.on('did-finish-load', () => {
|
|
|
|
this.windows[options.windowName].show()
|
|
|
|
})
|
2019-04-08 05:28:54 +10:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
win.on('closed', () => {
|
|
|
|
delete this.windows[options.windowName]
|
|
|
|
})
|
2019-04-08 01:55:27 +10:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
let openUrl = (event: Event, url: string) => {
|
2019-04-09 01:38:04 +10:00
|
|
|
if (isDevelopment && url === process.env.WEBPACK_DEV_SERVER_URL + options.loadPath) {
|
2019-04-09 01:06:28 +10:00
|
|
|
return
|
|
|
|
}
|
|
|
|
event.preventDefault()
|
|
|
|
shell.openExternal(url, {
|
|
|
|
activate: false
|
|
|
|
}, (err) => {
|
|
|
|
//if (err) console.log(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
win.webContents.on('will-navigate', openUrl)
|
|
|
|
win.webContents.on('new-window', openUrl)
|
|
|
|
|
|
|
|
if (process.env.WEBPACK_DEV_SERVER_URL) {
|
|
|
|
// `electron:serve`で起動した時の読み込み
|
|
|
|
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL + options.loadPath)
|
|
|
|
} else {
|
|
|
|
// ビルドしたアプリでの読み込み
|
|
|
|
win.loadURL(`app://./${options.loadPath}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDevelopment && options.openDevTools) win.webContents.openDevTools()
|
2019-04-08 01:55:27 +10:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
if (typeof options.lastAction === 'function') options.lastAction(win)
|
|
|
|
|
|
|
|
this.windows[options.windowName] = win
|
|
|
|
}
|
2019-04-05 19:40:00 +11:00
|
|
|
}
|
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
class ApplicationMenu {
|
|
|
|
private app: Application;
|
2019-04-05 19:40:00 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
// Mac only menu. prefix `macOnly`. First Item always separator
|
|
|
|
private macOnlyAppMenu: MenuItemConstructorOptions[] = [
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'services' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'hide' },
|
|
|
|
{ role: 'hideOthers' },
|
|
|
|
{ role: 'unhide' },
|
|
|
|
];
|
|
|
|
private macOnlyEditMenu: MenuItemConstructorOptions[] = [
|
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
|
|
|
label: 'Speech',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'startspeaking' },
|
|
|
|
{ role: 'stopspeaking' },
|
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
2019-04-05 19:40:00 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
private get aboutMenuItem(): MenuItemConstructorOptions {
|
|
|
|
return {
|
|
|
|
label: process.platform !== 'darwin' ? 'About' : `About ${this.app.app.getName()}`,
|
|
|
|
click: () => this.app.openAboutWindow(),
|
|
|
|
}
|
2019-04-05 19:40:00 +11:00
|
|
|
}
|
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
constructor(app: Application) {
|
|
|
|
this.app = app;
|
|
|
|
}
|
2019-04-06 22:38:17 +11:00
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
public setApplicationMenu() {
|
|
|
|
const menu = Menu.buildFromTemplate(this.buildTemplate(process.platform === 'darwin'))
|
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildTemplate(isMac: boolean): MenuItemConstructorOptions[] {
|
|
|
|
return [
|
|
|
|
this.AppMenu(isMac),
|
|
|
|
this.EditMenu(isMac),
|
|
|
|
this.ViewMenu(),
|
|
|
|
this.WindowMenu(isMac),
|
|
|
|
this.HelpMenu(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
private AppMenu(isMac: boolean): MenuItemConstructorOptions {
|
|
|
|
let appMenu: MenuItemConstructorOptions[] = [
|
|
|
|
this.aboutMenuItem,
|
|
|
|
...(isMac ? this.macOnlyAppMenu : []),
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'quit' },
|
|
|
|
]
|
|
|
|
return {
|
|
|
|
label: this.app.app.getName(),
|
|
|
|
submenu: appMenu,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private EditMenu(isMac: boolean): MenuItemConstructorOptions {
|
|
|
|
let editMenu: MenuItemConstructorOptions[] = [
|
|
|
|
{ role: 'undo' },
|
|
|
|
{ role: 'redo' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'cut' },
|
|
|
|
{ role: 'copy' },
|
|
|
|
{ role: 'paste' },
|
|
|
|
{ role: 'pasteandmatchstyle' },
|
|
|
|
{ role: 'delete' },
|
|
|
|
{ role: 'selectall' },
|
|
|
|
...(isMac ? this.macOnlyEditMenu : []),
|
|
|
|
]
|
|
|
|
return {
|
|
|
|
label: 'Edit',
|
|
|
|
submenu: editMenu,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ViewMenu(): MenuItemConstructorOptions {
|
|
|
|
let viewMenu: MenuItemConstructorOptions[] = [
|
|
|
|
{ role: 'reload' },
|
|
|
|
{ role: 'forcereload' },
|
|
|
|
{ role: 'toggledevtools' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'togglefullscreen' },
|
|
|
|
]
|
|
|
|
return {
|
|
|
|
label: 'View',
|
|
|
|
submenu: viewMenu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private WindowMenu(isMac: boolean): MenuItemConstructorOptions {
|
|
|
|
let windowMenu: MenuItemConstructorOptions[] = isMac ? [
|
|
|
|
{ role: 'close' },
|
|
|
|
{ role: 'minimize' },
|
|
|
|
{ role: 'zoom' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'front' },
|
|
|
|
] : [
|
2019-04-09 01:38:04 +10:00
|
|
|
{ role: 'minimize' },
|
|
|
|
{ role: 'close' },
|
|
|
|
]
|
2019-04-09 01:06:28 +10:00
|
|
|
return {
|
|
|
|
label: 'Window',
|
|
|
|
submenu: windowMenu,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private HelpMenu(): MenuItemConstructorOptions {
|
|
|
|
let helpMenu: MenuItemConstructorOptions[] = [
|
|
|
|
{
|
|
|
|
label: 'Report an issue',
|
|
|
|
click: () => shell.openExternal(`${bugs.url}/new`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Learn More',
|
|
|
|
click: () => shell.openExternal(TheDeskInfo.documentURL),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
return {
|
|
|
|
label: 'Help',
|
|
|
|
submenu: helpMenu,
|
2019-04-08 02:14:32 +10:00
|
|
|
}
|
2019-04-06 22:38:17 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 01:06:28 +10:00
|
|
|
const TheDeskVueApp: Application = new Application(app)
|
|
|
|
const MainMenu: ApplicationMenu = new ApplicationMenu(TheDeskVueApp)
|
|
|
|
MainMenu.setApplicationMenu()
|
|
|
|
|