const { app, BrowserWindow, globalShortcut, Tray, Menu, ipcMain, dialog } = require('electron')
const path = require('path')
const url = require('url')
const pathName = path.join(__dirname, 'demo/javascript/src/NiuniuCapture.exe')
let mainWindow
let tray = null;
var count = 0, timer = null;
var count1 = 0, timer1 = null;
app.on('ready', function () {
globalShortcut.register('ctrl+alt+o', function () {
mainWindow.webContents.send('main-process-messages', 'StartCapture')
});
});
function createWindow() {
mainWindow = new BrowserWindow({
width: 1024, height: 700, minWidth: 712, minHeight: 505, maximizable: true, icon: '/icon.ico', backgroundColor: '#ffffff', frame: false, show: false
})
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
mainWindow.webContents.openDevTools()
mainWindow.setMenu(null)
mainWindow.on('closed', function (e) {
e.preventDefault()
mainWindow = null
})
mainWindow.on('close', (event) => {
mainWindow.hide();
mainWindow.setSkipTaskbar(true);
event.preventDefault();
});
mainWindow.on('show', () => {
tray.setHighlightMode('always');
})
mainWindow.on('restore', () => {
clearInterval(timer1);
clearInterval(timer);
tray.setImage(path.join(__dirname, './icon.ico'))
})
mainWindow.on('hide', () => {
tray.setHighlightMode('always')
})
tray = new Tray(path.join(__dirname, './icon.ico'));
const contextMenu = Menu.buildFromTemplate([
{
label: '退出', click: () => {
mainWindow.webContents.send('main-process-messages', 'exit');
setTimeout(function () {
mainWindow.destroy()
}, 300)
}
},
])
tray.setToolTip('视界');
tray.setContextMenu(contextMenu);
tray.on('click', () => {
if (timer) {
tray.setImage(path.join(__dirname, './icon.ico'))
clearInterval(timer)
}
if (timer1) {
tray.setImage(path.join(__dirname, './icon.ico'))
clearInterval(timer1)
}
if (mainWindow.isVisible()) {
if (mainWindow.isMinimized()) {
mainWindow.show();
clearInterval(timer);
clearInterval(timer1);
}
} else {
mainWindow.show();
mainWindow.setSkipTaskbar(false);
clearInterval(timer);
clearInterval(timer1);
}
});
}
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (mainWindow) {
mainWindow.setSkipTaskbar(false);
mainWindow.show();
if (mainWindow.isMinimized()) {
mainWindow.restore();
mainWindow.focus();
mainWindow.setSkipTaskbar(false);
}
}
});
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
ipcMain.on("open-error-dialog", function (event) {
clearInterval(timer1);
clearInterval(timer);
if (!mainWindow.isFocused()) {
mainWindow.flashFrame(true);
timer1 = setInterval(function () {
count1++;
if (count1 % 2 == 0) {
tray.setImage(path.join(__dirname, "./empty.ico"));
} else {
tray.setImage(path.join(__dirname, "./icon.ico"));
}
}, 500);
} else {
clearInterval(timer);
clearInterval(timer1);
if (mainWindow.isVisible()) {
clearInterval(timer);
} else {
timer = setInterval(function () {
count++;
if (count % 2 == 0) {
tray.setImage(path.join(__dirname, "./empty.ico"));
} else {
tray.setImage(path.join(__dirname, "./icon.ico"));
}
}, 500);
}
if (mainWindow.isMinimized()) {
timer1 = setInterval(function () {
count1++;
if (count1 % 2 == 0) {
tray.setImage(path.join(__dirname, "./empty.ico"));
} else {
tray.setImage(path.join(__dirname, "./icon.ico"));
}
}, 500);
} else if(!mainWindow.isMinimized()){
clearInterval(timer1);
}
}
});