|
| 1 | +const electron = require('electron'); |
| 2 | +const { app, Menu, Tray, shell} = require('electron') |
| 3 | +const ipc = electron.ipcMain; |
| 4 | +let tray; |
| 5 | +let mainWindow; |
| 6 | +const iconFile = `${__dirname}/images/256x256.png`; |
| 7 | + |
| 8 | +const title = 'P3X OneNote'; |
| 9 | + |
| 10 | + |
| 11 | +const BrowserWindow = electron.BrowserWindow; |
| 12 | +const configstore = require('configstore'); |
| 13 | +const pkg = require('../../package.json'); |
| 14 | +const conf = new configstore(pkg.name); |
| 15 | + |
| 16 | + |
| 17 | +const action = { |
| 18 | + restart: () => { |
| 19 | + mainWindow.webContents.send('action', { |
| 20 | + action: 'restart' |
| 21 | + }) |
| 22 | + }, |
| 23 | + home: () => { |
| 24 | + mainWindow.show(); |
| 25 | + mainWindow.webContents.send('action', { |
| 26 | + action: 'home' |
| 27 | + }) |
| 28 | + }, |
| 29 | + toggleVisible: () => { |
| 30 | + if (mainWindow === undefined) { |
| 31 | + return; |
| 32 | + } |
| 33 | + setVisible(!mainWindow.isVisible()); |
| 34 | + }, |
| 35 | + quit: function () { |
| 36 | + app.isQuiting = true; |
| 37 | + app.quit(); |
| 38 | + }, |
| 39 | + github: () => { |
| 40 | + shell.openExternal('https://github.com/patrikx3/onenote') |
| 41 | + }, |
| 42 | + patrik: () => { |
| 43 | + shell.openExternal('https://patrikx3.tk') |
| 44 | + }, |
| 45 | + p3x: () => { |
| 46 | + shell.openExternal('https://github.com/patrikx3') |
| 47 | + }, |
| 48 | + corifeus: () => { |
| 49 | + shell.openExternal('https://corifeus.tk') |
| 50 | + }, |
| 51 | + npm: () => { |
| 52 | + shell.openExternal('https://www.npmjs.com/~patrikx3') |
| 53 | + }, |
| 54 | + download: () => { |
| 55 | + shell.openExternal('https://github.com/patrikx3/onenote/releases') |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | +const menus = { |
| 60 | + default: () => { |
| 61 | + |
| 62 | + let visible = false; |
| 63 | + if (mainWindow !== undefined) { |
| 64 | + visible = mainWindow.isVisible() ? true : false; |
| 65 | + } |
| 66 | + return [ |
| 67 | + { |
| 68 | + label: 'Home', |
| 69 | + click: action.home |
| 70 | + }, |
| 71 | + { |
| 72 | + label: 'Restart / Logout', |
| 73 | + tooltip: 'You logout and can login again', |
| 74 | + click: action.restart |
| 75 | + }, |
| 76 | + { |
| 77 | + label: visible ? 'Hide' : 'Show', |
| 78 | + click: action.toggleVisible |
| 79 | + }, |
| 80 | + { |
| 81 | + label: 'Download', |
| 82 | + click: action.download |
| 83 | + }, |
| 84 | + { |
| 85 | + label: 'Quit', |
| 86 | + click: action.quit |
| 87 | + } |
| 88 | + ] |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function createMenu() { |
| 93 | + const template = [ |
| 94 | + { |
| 95 | + label: title, |
| 96 | + submenu: menus.default(), |
| 97 | + }, |
| 98 | + { |
| 99 | + label: 'Edit', |
| 100 | + submenu: [ |
| 101 | + {role: 'undo'}, |
| 102 | + {role: 'redo'}, |
| 103 | + {type: 'separator'}, |
| 104 | + {role: 'cut'}, |
| 105 | + {role: 'copy'}, |
| 106 | + {role: 'paste'}, |
| 107 | + {role: 'pasteandmatchstyle'}, |
| 108 | + {role: 'delete'}, |
| 109 | + {role: 'selectall'} |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + label: 'View', |
| 114 | + submenu: [ |
| 115 | + {role: 'reload'}, |
| 116 | + {role: 'forcereload'}, |
| 117 | + {role: 'toggledevtools'}, |
| 118 | + {type: 'separator'}, |
| 119 | + {role: 'resetzoom'}, |
| 120 | + {role: 'zoomin'}, |
| 121 | + {role: 'zoomout'}, |
| 122 | + {type: 'separator'}, |
| 123 | + {role: 'togglefullscreen'} |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + role: 'help', |
| 128 | + submenu: [ |
| 129 | + { |
| 130 | + label: 'Download', |
| 131 | + click: action.download |
| 132 | + }, |
| 133 | + { |
| 134 | + label: 'GitHub', |
| 135 | + click: action.github |
| 136 | + }, |
| 137 | + { |
| 138 | + label: 'Patrik Laszlo', |
| 139 | + click: action.patrik |
| 140 | + }, |
| 141 | + { |
| 142 | + label: 'P3X', |
| 143 | + click: action.p3x |
| 144 | + }, |
| 145 | + { |
| 146 | + label: 'Corifeus', |
| 147 | + click: action.corifeus |
| 148 | + }, |
| 149 | + { |
| 150 | + label: 'Npm', |
| 151 | + click: action.npm |
| 152 | + }, |
| 153 | + ] |
| 154 | + } |
| 155 | + ] |
| 156 | + |
| 157 | + const menu = Menu.buildFromTemplate(template) |
| 158 | + Menu.setApplicationMenu(menu) |
| 159 | +} |
| 160 | + |
| 161 | +function createTray() { |
| 162 | + if (tray === undefined) { |
| 163 | + tray = new Tray(iconFile) |
| 164 | + tray.setToolTip(title) |
| 165 | + tray.on('click', action.toggleVisible) |
| 166 | + } |
| 167 | + const contextMenu = Menu.buildFromTemplate(menus.default()) |
| 168 | + tray.setContextMenu(contextMenu) |
| 169 | +} |
| 170 | + |
| 171 | +function setVisible(visible = true) { |
| 172 | + if (visible === null) { |
| 173 | + visible = true; |
| 174 | + } |
| 175 | + if (mainWindow !== undefined) { |
| 176 | + if (visible) { |
| 177 | + mainWindow.show(); |
| 178 | + } else { |
| 179 | + mainWindow.hide(); |
| 180 | + } |
| 181 | + } |
| 182 | + conf.set('visible', visible); |
| 183 | + createMenu(); |
| 184 | + createTray() |
| 185 | +} |
| 186 | + |
| 187 | + |
| 188 | +function createWindow() { |
| 189 | + |
| 190 | + mainWindow = new BrowserWindow({ |
| 191 | + icon: iconFile, |
| 192 | + toolbar: false, |
| 193 | + }); |
| 194 | + |
| 195 | + |
| 196 | + setVisible(conf.get('visible')); |
| 197 | + |
| 198 | + mainWindow.loadURL('file://' + __dirname + '/index.html'); |
| 199 | + |
| 200 | + mainWindow.on('minimize', function (event) { |
| 201 | + event.preventDefault() |
| 202 | + setVisible(false); |
| 203 | + }); |
| 204 | + |
| 205 | + mainWindow.on('close', function (event) { |
| 206 | + if (!app.isQuiting) { |
| 207 | + event.preventDefault() |
| 208 | + setVisible(false); |
| 209 | + } |
| 210 | + return false; |
| 211 | + }); |
| 212 | + |
| 213 | + const windowBounds = conf.get('windowBounds'); |
| 214 | + if (windowBounds !== null && windowBounds !== undefined) { |
| 215 | + mainWindow.setBounds(windowBounds); |
| 216 | + } |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | +} |
| 221 | +ipc.on('did-finish-load', function () { |
| 222 | + const hostData = conf.get('toHost'); |
| 223 | +// console.log('Loading data', hostData); |
| 224 | + if (hostData !== undefined && hostData !== null) { |
| 225 | + mainWindow.webContents.send('onload-user', hostData); |
| 226 | + } |
| 227 | +}); |
| 228 | + |
| 229 | +ipc.on('save', function (event, data) { |
| 230 | +// console.log('Save', data) |
| 231 | + conf.set('toHost', data); |
| 232 | + conf.set('windowBounds', mainWindow.getBounds()); |
| 233 | +}) |
| 234 | + |
| 235 | +app.on('ready', createWindow); |
| 236 | + |
| 237 | +app.on('window-all-closed', function () { |
| 238 | + |
| 239 | + if (process.platform !== 'darwin') { |
| 240 | + app.quit(); |
| 241 | + } |
| 242 | +}); |
| 243 | + |
| 244 | +app.on('activate', function () { |
| 245 | + if (mainWindow === null) { |
| 246 | + createWindow(); |
| 247 | + } |
| 248 | +}); |
| 249 | + |
| 250 | + |
0 commit comments