thedesk/src/background.js

189 lines
4.1 KiB
JavaScript
Raw Normal View History

'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'
import {
createProtocol,
installVueDevtools
} from 'vue-cli-plugin-electron-builder/lib'
import { bugs } from '../package.json'
import thedeskInfo from '../info.json'
2019-04-06 02:38:20 +11:00
const isDevelopment = process.env.NODE_ENV !== 'production'
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
2019-04-06 22:38:17 +11:00
ContextMenu()
2019-04-05 23:19:58 +11:00
// Standard scheme must be registered before the app is ready
protocol.registerStandardSchemes(['app'], { secure: true })
2019-04-06 22:38:17 +11:00
function createWindow () {
// Create the browser window.
2019-04-06 02:38:20 +11:00
win = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__static, 'icon.png')
})
2019-04-06 22:38:17 +11:00
win.setMenuBarVisibility(true)
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
createProtocol('app')
// Load the index.html when not in development
win.loadURL('app://./index.html')
}
win.on('closed', () => {
win = null
})
}
// 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.
if (win === null) {
createWindow()
}
})
// 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())
}
}
createWindow()
})
// 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',
click: () => shell.openExternal(`${bugs.url}/new`),
2019-04-06 22:38:17 +11:00
},
{
label: 'Learn More',
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)