import { app, protocol, BrowserWindow, Menu, globalShortcut, ipcMain } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
])
let defaultWinow = null
let printWindow = null
async function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
width: 1920,
height: 1080,
frame: false,
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
webviewTag: true
},
resizable: false
})
defaultWinow = win
Menu.setApplicationMenu(null)
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await defaultWinow.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
defaultWinow.loadURL('app://./index.html')
}
globalShortcut.register('F11', () => {
if (defaultWinow.isFullScreen()) {
defaultWinow.setFullScreen(false);
} else {
defaultWinow.setFullScreen(true);
}
});
ipcMain.on('get-printer-list', (event) => {
const list = win.webContents.getPrinters();
//通过webContents发送事件到渲染线程,同时将打印机列表也传过去
defaultWinow.webContents.send('get-printer-list', list);
});
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
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 (BrowserWindow.getAllWindows().length === 0) createWindow()
})
app.on('ready', async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
// await installExtension(VUEJS_DEVTOOLS)
const { default: installExtension } = await import('electron-devtools-installer')
var vue_devtools_beta = { id: "ljjemllljcmogpfapbkkighbhhppjdbg", electron: ">=1.2.1" }
var result = await installExtension(vue_devtools_beta)
if (result) {
console.log("success load : " + result)
}
} catch (e) {
console.error('Vue Devtools failed to install:', e.toString())
}
}
createWindow()
createPrintWindow()
})
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
app.quit()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
})
}
}
const createPrintWindow = () => {
const _printWindow = new BrowserWindow({
show: false,
title: '打印页面',
width: 800,
height: 600,
})
printWindow = _printWindow
ipcMain.on('startPrint', async (event, { deviceName, data }) => {
printWindow.loadURL(`data:text/html;charset=utf-8, ${encodeURI(data.html)}`)
printWindow.webContents.once('did-finish-load', () => {
printWindow.webContents.print({
silent: true,
printBackground: false,
deviceName: deviceName
}, res => {
});
})
})
}