// Modules to control application life and create native browser window const { app, dialog, BrowserWindow, Menu, Tray, ipcMain } = require('electron') const path = require('path') const fs = require('fs'); var result = [{ label: "title", sublabel: "title", type: "submenu", submenu: [ { label: "切换到该任务分支", }, ] } ]; let tray = null function createMenu() { const iconPath = path.join(__dirname, 'windows-icon.png') tray = new Tray(iconPath) tray.setToolTip('This is my application.') var menu_data = ""; var project_map_data = ""; tray.on("right-click", function () { // tray.popUpContextMenu(Menu.buildFromTemplate(result.concat({ // type: "separator" // }, { // label: "退出", // click: function () { // for (var i in BrowserWindow.getAllWindows()) { // BrowserWindow.getAllWindows()[i].close() // } // app.quit() // } // }))) // return console.log("right-click") if (!fs.existsSync("task.json") || !fs.existsSync("project-map.json")) { return } // if (!fs.existsSync("project-map.json")) fs.writeFileSync("project-map.json", JSON.stringify({})) var project_map_str = fs.readFileSync("project-map.json"); var project_map = JSON.parse(fs.readFileSync("project-map.json")); var data = fs.readFileSync("task.json") + ""; if (data != menu_data && menu_data != "") { tray.displayBalloon({ title: "你有一个新任务", content: "你有一个新任务,请及时查看" }) } project_map_data = project_map_str menu_data = data data = JSON.parse(data); result = []; for (var i in data) { let submenu = []; for (var k in data[i]) { submenu.push({ label: data[i][k]["title"], type: "submenu", submenu: [ { label: "切换到该任务分支", click: (function () { i, data[i][k]["title"] return function () { } })() }, ] }); } let project_dir = project_map[i] if (!project_dir) { project_dir = "" } else { project_dir = `(${project_dir})` } let item = { label: i, type: "submenu", submenu: submenu.concat([ { type: "separator" }, { label: "关联项目路径" + project_dir, click: (function (i) { return function (menuItem, browserWindow, event) { let project_map = JSON.parse(fs.readFileSync("project-map.json")); let a = dialog.showOpenDialogSync({ properties: ['openDirectory'] }) if (a) { project_map[i] = a[0] fs.writeFileSync("project-map.json", JSON.stringify(project_map)) } } })(i) }, ]) } result.push(item); } tray.popUpContextMenu(Menu.buildFromTemplate(result.concat({ type: "separator" }, { label: "退出", click: function () { for (var i in BrowserWindow.getAllWindows()) { BrowserWindow.getAllWindows()[i].close() } app.quit() } }))) }) } function createWindow() { // Create the browser window. const mainWindow = new BrowserWindow({ width: 300, height: 600, webPreferences: { preload: path.join(__dirname, 'preload.js') } }) // and load the index.html of the app. mainWindow.loadURL('http://oa.ouxuan.net/?d=we&m=login') mainWindow.webContents.on('crashed', function () { const options = { type: 'info', title: '渲染器进程崩溃', message: '这个进程已经崩溃.', buttons: ['重载', '关闭'] } dialog.showMessageBox(options, function (index) { if (index === 0) mainWindow.reload() else mainWindow.close() }) }) mainWindow.on('minimize', function (event) { event.preventDefault(); mainWindow.hide(); }); // Open the DevTools. // mainWindow.webContents.openDevTools() } // 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.whenReady().then(function () { createWindow() createMenu() }) // Quit when all windows are closed. app.on('window-all-closed', function () { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q for (var i in BrowserWindow.getAllWindows()) { BrowserWindow.getAllWindows()[i].close() } if (process.platform !== 'darwin') app.quit() }) app.on('activate', function () { // 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() }) // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here.