|
1 | 1 | 'use strict'; |
2 | 2 | const path = require('path'); |
3 | 3 | const fs = require('fs'); |
4 | | -const app = require('app'); |
5 | | -const BrowserWindow = require('browser-window'); |
6 | | -const Dialog = require('dialog'); |
7 | | -const shell = require('shell'); |
| 4 | +const electron = require('electron'); |
| 5 | +const config = require('./config') |
| 6 | + |
| 7 | +const app = electron.app; |
8 | 8 |
|
9 | 9 | require('electron-debug')(); |
10 | | -require('crash-reporter').start(); |
| 10 | +require('electron-dl')(); |
| 11 | +require('electron-context-menu')(); |
11 | 12 |
|
12 | 13 | let mainWindow; |
| 14 | +let isQuitting = false; |
13 | 15 |
|
14 | 16 | function updateBadge(title) { |
15 | | - if (!app.dock) { |
16 | | - return; |
17 | | - } |
| 17 | + if (!app.dock) { |
| 18 | + return; |
| 19 | + } |
18 | 20 |
|
19 | | - if (title.indexOf('Messenger') === -1) { |
20 | | - return; |
21 | | - } |
| 21 | + if (title.indexOf('Messenger') === -1) { |
| 22 | + return; |
| 23 | + } |
22 | 24 |
|
23 | | - const messageCount = (/\(([0-9]+)\)/).exec(title); |
24 | | - app.dock.setBadge(messageCount ? messageCount[1] : ''); |
| 25 | + const messageCount = (/\(([0-9]+)\)/).exec(title); |
| 26 | + app.dock.setBadge(messageCount ? messageCount[1] : ''); |
25 | 27 | } |
26 | 28 |
|
27 | 29 | function createMainWindow() { |
28 | | - const win = new BrowserWindow({ |
29 | | - 'title': app.getName(), |
30 | | - 'show': false, |
31 | | - 'width': 800, |
32 | | - 'height': 600, |
33 | | - 'icon': process.platform === 'linux' && path.join(__dirname, 'media', 'Icon.png'), |
34 | | - 'min-width': 400, |
35 | | - 'min-height': 200, |
36 | | - 'title-bar-style': 'hidden-inset', |
37 | | - 'web-preferences': { |
38 | | - // fails without this because of CommonJS script detection |
39 | | - 'node-integration': false, |
40 | | - |
41 | | - 'preload': path.join(__dirname, 'browser.js'), |
42 | | - |
43 | | - // required for Facebook active ping thingy |
44 | | - 'web-security': false, |
45 | | - |
46 | | - 'plugins': true |
| 30 | + const lastWindowState = config.get('lastWindowState'); |
| 31 | + const win = new electron.BrowserWindow({ |
| 32 | + title: app.getName(), |
| 33 | + show: false, |
| 34 | + x: lastWindowState.x, |
| 35 | + y: lastWindowState.y, |
| 36 | + width: lastWindowState.width, |
| 37 | + height: lastWindowState.height, |
| 38 | + icon: process.platform === 'linux' && path.join(__dirname, 'static', 'Icon.png'), |
| 39 | + minWidth: 400, |
| 40 | + minHeight: 200, |
| 41 | + titleBarStyle: 'hidden-inset', |
| 42 | + autoHideMenuBar: true, |
| 43 | + webPreferences: { |
| 44 | + nodeIntegration: false, |
| 45 | + preload: path.join(__dirname, 'browser.js'), |
| 46 | + plugins: true |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + if (process.platform === 'darwin') { |
| 51 | + win.setSheetOffset(40); |
| 52 | + } |
| 53 | + |
| 54 | + win.loadURL('https://trello.com/login'); |
| 55 | + win.on('close', e => { |
| 56 | + if (!isQuitting) { |
| 57 | + e.preventDefault(); |
| 58 | + |
| 59 | + if (process.platform === 'darwin') { |
| 60 | + app.hide(); |
| 61 | + } else { |
| 62 | + win.hide(); |
| 63 | + } |
47 | 64 | } |
48 | 65 | }); |
| 66 | + //win.on('page-title-updated', (e, title) => updateBadge(title)); |
49 | 67 |
|
50 | | - win.loadUrl('https://trello.com/login'); |
51 | | - win.on('closed', app.quit); |
52 | | - //win.on('page-title-updated', (e, title) => updateBadge(title)); |
53 | | - |
54 | | - return win; |
| 68 | + return win; |
55 | 69 | } |
56 | 70 |
|
57 | 71 | app.on('ready', () => { |
58 | 72 |
|
59 | | - mainWindow = createMainWindow(); |
60 | | - |
61 | | - const page = mainWindow.webContents; |
62 | | - |
63 | | - page.on('dom-ready', () => { |
64 | | - page.insertCSS(fs.readFileSync(path.join(__dirname, 'browser.css'), 'utf8')); |
65 | | - mainWindow.show(); |
66 | | - }); |
67 | | - |
68 | | - page.on('new-window', (e, url) => { |
69 | | - e.preventDefault(); |
70 | | - shell.openExternal(url); |
71 | | - }); |
| 73 | + mainWindow = createMainWindow(); |
| 74 | + |
| 75 | + const page = mainWindow.webContents; |
| 76 | + |
| 77 | + page.on('dom-ready', () => { |
| 78 | + page.insertCSS(fs.readFileSync(path.join(__dirname, 'browser.css'), 'utf8')); |
| 79 | + mainWindow.show(); |
| 80 | + }); |
| 81 | + |
| 82 | + page.on('new-window', (e, url) => { |
| 83 | + e.preventDefault(); |
| 84 | + shell.openExternal(url); |
| 85 | + }); |
| 86 | + |
| 87 | + mainWindow.webContents.session.on('will-download', (event, item) => { |
| 88 | + const totalBytes = item.getTotalBytes(); |
| 89 | + |
| 90 | + item.on('updated', () => { |
| 91 | + mainWindow.setProgressBar(item.getReceivedBytes() / totalBytes); |
| 92 | + }); |
| 93 | + |
| 94 | + item.on('done', (e, state) => { |
| 95 | + mainWindow.setProgressBar(-1); |
| 96 | + |
| 97 | + if (state === 'interrupted') { |
| 98 | + Dialog.showErrorBox('Download error', 'The download was interrupted'); |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + var template = [{ |
| 104 | + label: "Application", |
| 105 | + submenu: [ |
| 106 | + { label: "About Application", selector: "orderFrontStandardAboutPanel:" }, |
| 107 | + { type: "separator" }, |
| 108 | + { label: "Quit", accelerator: "Command+Q", click: function() { app.quit(); }} |
| 109 | + ]}, { |
| 110 | + label: "Edit", |
| 111 | + submenu: [ |
| 112 | + { label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" }, |
| 113 | + { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" }, |
| 114 | + { type: "separator" }, |
| 115 | + { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" }, |
| 116 | + { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" }, |
| 117 | + { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" }, |
| 118 | + { label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" } |
| 119 | + ]} |
| 120 | + ]; |
| 121 | + |
| 122 | + electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate(template)); |
| 123 | +}); |
72 | 124 |
|
73 | | - mainWindow.webContents.session.on('will-download', (event, item) => { |
74 | | - const totalBytes = item.getTotalBytes(); |
| 125 | +app.on("window-all-closed", function(){ |
| 126 | + app.quit(); |
| 127 | +}); |
75 | 128 |
|
76 | | - item.on('updated', () => { |
77 | | - mainWindow.setProgressBar(item.getReceivedBytes() / totalBytes); |
78 | | - }); |
| 129 | +app.on('activate', () => { |
| 130 | + mainWindow.show(); |
| 131 | +}); |
79 | 132 |
|
80 | | - item.on('done', (e, state) => { |
81 | | - mainWindow.setProgressBar(-1); |
| 133 | +app.on('before-quit', () => { |
| 134 | + isQuitting = true; |
82 | 135 |
|
83 | | - if (state === 'interrupted') { |
84 | | - Dialog.showErrorBox('Download error', 'The download was interrupted'); |
85 | | - } |
86 | | - }); |
87 | | - }); |
| 136 | + if (!mainWindow.isFullScreen()) { |
| 137 | + config.set('lastWindowState', mainWindow.getBounds()); |
| 138 | + } |
88 | 139 | }); |
0 commit comments