2019-04-05 19:40:00 +11:00
|
|
|
'use strict'
|
|
|
|
|
2019-04-06 02:38:20 +11:00
|
|
|
import path from 'path'
|
2019-04-06 22:38:17 +11:00
|
|
|
import {
|
|
|
|
app,
|
|
|
|
protocol,
|
|
|
|
shell,
|
|
|
|
BrowserWindow,
|
|
|
|
Menu
|
|
|
|
} from 'electron'
|
|
|
|
import ContextMenu from 'electron-context-menu'
|
2019-04-05 19:40:00 +11:00
|
|
|
import {
|
|
|
|
createProtocol,
|
|
|
|
installVueDevtools
|
|
|
|
} from 'vue-cli-plugin-electron-builder/lib'
|
2019-04-08 01:55:27 +10:00
|
|
|
import localShortcut from 'electron-localshortcut'
|
|
|
|
|
2019-04-08 00:21:26 +10:00
|
|
|
import { bugs } from '../package.json'
|
|
|
|
import thedeskInfo from '../info.json'
|
2019-04-06 02:38:20 +11:00
|
|
|
|
2019-04-05 19:40:00 +11:00
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production'
|
|
|
|
|
2019-04-08 01:55:27 +10:00
|
|
|
// イベントリスナや`createWindow`関数が参照するグローバル変数
|
|
|
|
let createdAppProtocol = false
|
|
|
|
, windows = {}
|
2019-04-05 19:40:00 +11:00
|
|
|
|
2019-04-06 22:38:17 +11:00
|
|
|
ContextMenu()
|
2019-04-05 23:19:58 +11:00
|
|
|
|
2019-04-08 01:55:27 +10:00
|
|
|
// Standard schemeはreadyの前に登録する必要がある
|
2019-04-05 19:40:00 +11:00
|
|
|
protocol.registerStandardSchemes(['app'], { secure: true })
|
2019-04-06 22:38:17 +11:00
|
|
|
|
2019-04-08 01:55:27 +10:00
|
|
|
// Windowを作る
|
|
|
|
async function createWindow(windowName, loadPath, windowOptions, singleton, lastAction, openDevTools) {
|
|
|
|
if (typeof windows[windowName] !== 'undefined') {
|
|
|
|
windows[windowName].show()
|
|
|
|
windows[windowName].focus()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 引数のバリデーション
|
|
|
|
if (typeof windowOptions !== 'object') windowOptions = {}
|
|
|
|
if (typeof lastAction !== 'function') lastAction = () => {}
|
2019-04-05 19:40:00 +11:00
|
|
|
|
2019-04-08 01:55:27 +10:00
|
|
|
let win = new BrowserWindow(windowOptions)
|
|
|
|
|
|
|
|
// ページの表示が完了するまで非表示にする
|
|
|
|
win.hide()
|
|
|
|
win.webContents.on('did-finish-load', () => {
|
|
|
|
windows[windowName].show()
|
|
|
|
})
|
|
|
|
|
|
|
|
win.on('closed', () => {
|
|
|
|
windows[windowName] = undefined
|
|
|
|
})
|
2019-04-06 22:38:17 +11:00
|
|
|
|
2019-04-05 19:40:00 +11:00
|
|
|
if (process.env.WEBPACK_DEV_SERVER_URL) {
|
2019-04-08 01:55:27 +10:00
|
|
|
// `electron:serve`で起動した時の読み込み
|
|
|
|
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL + loadPath)
|
|
|
|
if (openDevTools) win.webContents.openDevTools()
|
2019-04-05 19:40:00 +11:00
|
|
|
} else {
|
2019-04-08 01:55:27 +10:00
|
|
|
// ビルドしたアプリでの読み込み
|
|
|
|
if (!createdAppProtocol) {
|
|
|
|
createProtocol('app')
|
|
|
|
createdAppProtocol = true
|
|
|
|
}
|
|
|
|
win.loadURL(`app://./${loadPath}`)
|
2019-04-05 19:40:00 +11:00
|
|
|
}
|
|
|
|
|
2019-04-08 01:55:27 +10:00
|
|
|
localShortcut.register(win, 'F5', () => windows[windowName].reload())
|
|
|
|
lastAction(win)
|
|
|
|
|
|
|
|
windows[windowName] = win
|
|
|
|
}
|
|
|
|
|
|
|
|
function openMainWindow() {
|
|
|
|
const winOpts = {
|
|
|
|
icon: path.join(__static, 'icon.png'),
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
}
|
|
|
|
createWindow('main', 'index.html', winOpts, true, null, !process.env.IS_TEST)
|
2019-04-05 19:40:00 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Quit when all windows are closed.
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
// On macOS it is common for applications and their menu bar
|
|
|
|
// to stay active until the user quits explicitly with Cmd + Q
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
// On macOS it's common to re-create a window in the app when the
|
|
|
|
// dock icon is clicked and there are no other windows open.
|
2019-04-08 01:55:27 +10:00
|
|
|
if (typeof windows.main === 'undefined') {
|
|
|
|
openMainWindow()
|
2019-04-05 19:40:00 +11:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// This method will be called when Electron has finished
|
|
|
|
// initialization and is ready to create browser windows.
|
|
|
|
// Some APIs can only be used after this event occurs.
|
|
|
|
app.on('ready', async () => {
|
|
|
|
if (isDevelopment && !process.env.IS_TEST) {
|
|
|
|
// Install Vue Devtools
|
|
|
|
try {
|
|
|
|
await installVueDevtools()
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Vue Devtools failed to install:', e.toString())
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 01:55:27 +10:00
|
|
|
openMainWindow()
|
2019-04-05 19:40:00 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
// Exit cleanly on request from parent process in development mode.
|
|
|
|
if (isDevelopment) {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
process.on('message', data => {
|
|
|
|
if (data === 'graceful-exit') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
app.quit()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-04-06 22:38:17 +11:00
|
|
|
|
|
|
|
const template = [
|
|
|
|
{
|
|
|
|
label: app.getName(),
|
|
|
|
submenu: [
|
|
|
|
{ role: 'about' },
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Edit',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'undo' },
|
|
|
|
{ role: 'redo' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'cut' },
|
|
|
|
{ role: 'copy' },
|
|
|
|
{ role: 'paste' },
|
|
|
|
{ role: 'pasteandmatchstyle' },
|
|
|
|
{ role: 'delete' },
|
2019-04-07 07:43:55 +10:00
|
|
|
{ role: 'selectall' },
|
2019-04-06 22:38:17 +11:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'reload' },
|
|
|
|
{ role: 'forcereload' },
|
|
|
|
{ role: 'toggledevtools' },
|
|
|
|
{ type: 'separator' },
|
2019-04-07 07:43:55 +10:00
|
|
|
{ role: 'togglefullscreen' },
|
2019-04-06 22:38:17 +11:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'Window',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'minimize' },
|
2019-04-07 07:43:55 +10:00
|
|
|
{ role: 'close' },
|
2019-04-06 22:38:17 +11:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'help',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Report an issue',
|
2019-04-08 00:21:26 +10:00
|
|
|
click: () => shell.openExternal(`${bugs.url}/new`),
|
2019-04-06 22:38:17 +11:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Learn More',
|
2019-04-08 00:21:26 +10:00
|
|
|
click: () => shell.openExternal(thedeskInfo.documentURL),
|
2019-04-06 22:38:17 +11:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
template[0].submenu.push(
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'services' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'hide' },
|
|
|
|
{ role: 'hideothers' },
|
|
|
|
{ role: 'unhide' },
|
|
|
|
{ type: 'separator' },
|
2019-04-07 07:43:55 +10:00
|
|
|
{ role: 'quit' },
|
2019-04-06 22:38:17 +11:00
|
|
|
)
|
|
|
|
|
|
|
|
template[1].submenu.push(
|
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
|
|
|
label: 'Speech',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'startspeaking' },
|
2019-04-07 07:43:55 +10:00
|
|
|
{ role: 'stopspeaking' },
|
2019-04-06 22:38:17 +11:00
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
template[3].submenu = [
|
|
|
|
{ role: 'close' },
|
|
|
|
{ role: 'minimize' },
|
|
|
|
{ role: 'zoom' },
|
|
|
|
{ type: 'separator' },
|
2019-04-07 07:43:55 +10:00
|
|
|
{ role: 'front' },
|
2019-04-06 22:38:17 +11:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
|
|
Menu.setApplicationMenu(menu)
|