Files: 05d26ec79bdbb7f7583ee169d8ed48689f4f6bad / index.js
5347 bytesRaw
1 | process.on('uncaughtException', function (err) { |
2 | console.log(err) |
3 | process.exit() |
4 | }) |
5 | |
6 | var electron = require('electron') |
7 | var openWindow = require('./lib/window') |
8 | |
9 | var Path = require('path') |
10 | var defaultMenu = require('electron-default-menu') |
11 | var WindowState = require('electron-window-state') |
12 | var Menu = electron.Menu |
13 | var extend = require('xtend') |
14 | var ssbKeys = require('ssb-keys') |
15 | |
16 | var windows = { |
17 | dialogs: new Set() |
18 | } |
19 | var ssbConfig = null |
20 | var quitting = false |
21 | |
22 | /** |
23 | * It's not possible to run two instances of patchwork as it would create two |
24 | * scuttlebot instances that conflict on the same port. Before opening patchwork, |
25 | * we check if it's already running and if it is we focus the existing window |
26 | * rather than opening a new instance. |
27 | */ |
28 | function quitIfAlreadyRunning () { |
29 | var shouldQuit = electron.app.makeSingleInstance(function (commandLine, workingDirectory) { |
30 | // Someone tried to run a second instance, we should focus our window. |
31 | if (windows.main) { |
32 | if (windows.main.isMinimized()) windows.main.restore() |
33 | windows.main.focus() |
34 | } |
35 | }) |
36 | |
37 | if (shouldQuit) { |
38 | electron.app.quit() |
39 | } |
40 | } |
41 | |
42 | quitIfAlreadyRunning() |
43 | |
44 | electron.app.on('ready', () => { |
45 | setupContext('ssb', { |
46 | server: !(process.argv.includes('-g') || process.argv.includes('--use-global-ssb')) |
47 | }, () => { |
48 | |
49 | var browserWindow = openMainWindow(); |
50 | var menu = defaultMenu(electron.app, electron.shell) |
51 | |
52 | menu.splice(4, 0, { |
53 | label: "History", |
54 | submenu: [ |
55 | { |
56 | label: "Forward", |
57 | accelerator: "CmdOrCtrl+]", |
58 | click: () => { |
59 | browserWindow.webContents.send('goForward') |
60 | } |
61 | }, |
62 | { |
63 | label: "Back", |
64 | accelerator: "CmdOrCtrl+[", |
65 | click: () => { |
66 | browserWindow.webContents.send("goBack") |
67 | } |
68 | } |
69 | ] |
70 | }); |
71 | |
72 | |
73 | var view = menu.find(x => x.label === 'View') |
74 | view.submenu = [ |
75 | { role: 'reload' }, |
76 | { role: 'toggledevtools' }, |
77 | { type: 'separator' }, |
78 | { role: 'resetzoom' }, |
79 | { role: 'zoomin' }, |
80 | { role: 'zoomout' }, |
81 | { type: 'separator' }, |
82 | { role: 'togglefullscreen' } |
83 | ] |
84 | var help = menu.find(x => x.label === 'Help') |
85 | help.submenu = [ |
86 | { |
87 | label: 'Learn More', |
88 | click () { require('electron').shell.openExternal('https://scuttlebutt.nz') } |
89 | } |
90 | ] |
91 | if (process.platform === 'darwin') { |
92 | var win = menu.find(x => x.label === 'Window') |
93 | win.submenu = [ |
94 | { role: 'minimize' }, |
95 | { role: 'zoom' }, |
96 | { role: 'close', label: 'Close' }, |
97 | { type: 'separator' }, |
98 | { role: 'front' } |
99 | ] |
100 | } |
101 | |
102 | Menu.setApplicationMenu(Menu.buildFromTemplate(menu)) |
103 | }) |
104 | |
105 | electron.app.on('activate', function (e) { |
106 | if (windows.main) { |
107 | windows.main.show() |
108 | } |
109 | }) |
110 | |
111 | electron.app.on('before-quit', function () { |
112 | quitting = true |
113 | }) |
114 | |
115 | electron.ipcMain.on('open-background-devtools', function (ev, config) { |
116 | if (windows.background) { |
117 | windows.background.webContents.openDevTools({detach: true}) |
118 | } |
119 | }) |
120 | }) |
121 | |
122 | function openMainWindow () { |
123 | if (!windows.main) { |
124 | var windowState = WindowState({ |
125 | defaultWidth: 1024, |
126 | defaultHeight: 768 |
127 | }) |
128 | windows.main = openWindow(ssbConfig, Path.join(__dirname, 'main-window.js'), { |
129 | minWidth: 800, |
130 | x: windowState.x, |
131 | y: windowState.y, |
132 | width: windowState.width, |
133 | height: windowState.height, |
134 | titleBarStyle: 'hidden-inset', |
135 | autoHideMenuBar: true, |
136 | title: 'Patchwork', |
137 | show: true, |
138 | backgroundColor: '#EEE', |
139 | icon: Path.join(__dirname, 'assets/icon.png') |
140 | }) |
141 | windowState.manage(windows.main) |
142 | windows.main.setSheetOffset(40) |
143 | windows.main.on('close', function (e) { |
144 | if (!quitting && process.platform === 'darwin') { |
145 | e.preventDefault() |
146 | windows.main.hide() |
147 | } |
148 | }) |
149 | windows.main.on('closed', function () { |
150 | windows.main = null |
151 | if (process.platform !== 'darwin') electron.app.quit() |
152 | }) |
153 | } |
154 | return windows.main |
155 | } |
156 | |
157 | function setupContext (appName, opts, cb) { |
158 | ssbConfig = require('ssb-config/inject')(appName, extend({ |
159 | port: 8008, |
160 | blobsPort: 8989, // matches ssb-ws |
161 | friends: { |
162 | dunbar: 150, |
163 | hops: 2 // down from 3 |
164 | } |
165 | }, opts)) |
166 | |
167 | console.log(ssbConfig) |
168 | |
169 | ssbConfig.keys = ssbKeys.loadOrCreateSync(Path.join(ssbConfig.path, 'secret')) |
170 | |
171 | // fix offline on windows by specifying 127.0.0.1 instead of localhost (default) |
172 | var id = ssbConfig.keys.id |
173 | ssbConfig.remote = `net:127.0.0.1:${ssbConfig.port}~shs:${id.slice(1).replace('.ed25519', '')}` |
174 | |
175 | if (opts.server === false) { |
176 | cb && cb() |
177 | } else { |
178 | electron.ipcMain.once('server-started', function (ev, config) { |
179 | ssbConfig = config |
180 | cb && cb() |
181 | }) |
182 | windows.background = openWindow(ssbConfig, Path.join(__dirname, 'server-process.js'), { |
183 | connect: false, |
184 | center: true, |
185 | fullscreen: false, |
186 | fullscreenable: false, |
187 | height: 150, |
188 | maximizable: false, |
189 | minimizable: false, |
190 | resizable: false, |
191 | show: false, |
192 | skipTaskbar: true, |
193 | title: 'patchwork-server', |
194 | useContentSize: true, |
195 | width: 150 |
196 | }) |
197 | // windows.background.on('close', (ev) => { |
198 | // ev.preventDefault() |
199 | // windows.background.hide() |
200 | // }) |
201 | } |
202 | } |
203 |
Built with git-ssb-web