Files: 6e309f8eeaa5f795ea26b439be0f4fcf4c4103a4 / index.js
5331 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 | var browserWindow = openMainWindow() |
49 | var menu = defaultMenu(electron.app, electron.shell) |
50 | |
51 | menu.splice(4, 0, { |
52 | label: 'History', |
53 | submenu: [ |
54 | { |
55 | label: 'Back', |
56 | accelerator: 'CmdOrCtrl+[', |
57 | click: () => { |
58 | browserWindow.webContents.send('goBack') |
59 | } |
60 | }, |
61 | { |
62 | label: 'Forward', |
63 | accelerator: 'CmdOrCtrl+]', |
64 | click: () => { |
65 | browserWindow.webContents.send('goForward') |
66 | } |
67 | } |
68 | ] |
69 | }) |
70 | |
71 | var view = menu.find(x => x.label === 'View') |
72 | view.submenu = [ |
73 | { role: 'reload' }, |
74 | { role: 'toggledevtools' }, |
75 | { type: 'separator' }, |
76 | { role: 'resetzoom' }, |
77 | { role: 'zoomin' }, |
78 | { role: 'zoomout' }, |
79 | { type: 'separator' }, |
80 | { role: 'togglefullscreen' } |
81 | ] |
82 | var help = menu.find(x => x.label === 'Help') |
83 | help.submenu = [ |
84 | { |
85 | label: 'Learn More', |
86 | click () { require('electron').shell.openExternal('https://scuttlebutt.nz') } |
87 | } |
88 | ] |
89 | if (process.platform === 'darwin') { |
90 | var win = menu.find(x => x.label === 'Window') |
91 | win.submenu = [ |
92 | { role: 'minimize' }, |
93 | { role: 'zoom' }, |
94 | { role: 'close', label: 'Close' }, |
95 | { type: 'separator' }, |
96 | { role: 'front' } |
97 | ] |
98 | } |
99 | |
100 | Menu.setApplicationMenu(Menu.buildFromTemplate(menu)) |
101 | }) |
102 | |
103 | electron.app.on('activate', function (e) { |
104 | if (windows.main) { |
105 | windows.main.show() |
106 | } |
107 | }) |
108 | |
109 | electron.app.on('before-quit', function () { |
110 | quitting = true |
111 | }) |
112 | |
113 | electron.ipcMain.on('open-background-devtools', function (ev, config) { |
114 | if (windows.background) { |
115 | windows.background.webContents.openDevTools({detach: true}) |
116 | } |
117 | }) |
118 | }) |
119 | |
120 | function openMainWindow () { |
121 | if (!windows.main) { |
122 | var windowState = WindowState({ |
123 | defaultWidth: 1024, |
124 | defaultHeight: 768 |
125 | }) |
126 | windows.main = openWindow(ssbConfig, Path.join(__dirname, 'main-window.js'), { |
127 | minWidth: 800, |
128 | x: windowState.x, |
129 | y: windowState.y, |
130 | width: windowState.width, |
131 | height: windowState.height, |
132 | titleBarStyle: 'hidden-inset', |
133 | autoHideMenuBar: true, |
134 | title: 'Patchwork', |
135 | show: true, |
136 | backgroundColor: '#EEE', |
137 | icon: Path.join(__dirname, 'assets/icon.png') |
138 | }) |
139 | windowState.manage(windows.main) |
140 | windows.main.setSheetOffset(40) |
141 | windows.main.on('close', function (e) { |
142 | if (!quitting && process.platform === 'darwin') { |
143 | e.preventDefault() |
144 | windows.main.hide() |
145 | } |
146 | }) |
147 | windows.main.on('closed', function () { |
148 | windows.main = null |
149 | if (process.platform !== 'darwin') electron.app.quit() |
150 | }) |
151 | } |
152 | return windows.main |
153 | } |
154 | |
155 | function setupContext (appName, opts, cb) { |
156 | ssbConfig = require('ssb-config/inject')(appName, extend({ |
157 | port: 8008, |
158 | blobsPort: 8989, // matches ssb-ws |
159 | friends: { |
160 | dunbar: 150, |
161 | hops: 2 // down from 3 |
162 | } |
163 | }, opts)) |
164 | |
165 | console.log(ssbConfig) |
166 | |
167 | ssbConfig.keys = ssbKeys.loadOrCreateSync(Path.join(ssbConfig.path, 'secret')) |
168 | |
169 | // fix offline on windows by specifying 127.0.0.1 instead of localhost (default) |
170 | var id = ssbConfig.keys.id |
171 | ssbConfig.remote = `net:127.0.0.1:${ssbConfig.port}~shs:${id.slice(1).replace('.ed25519', '')}` |
172 | |
173 | if (opts.server === false) { |
174 | cb && cb() |
175 | } else { |
176 | electron.ipcMain.once('server-started', function (ev, config) { |
177 | ssbConfig = config |
178 | cb && cb() |
179 | }) |
180 | windows.background = openWindow(ssbConfig, Path.join(__dirname, 'server-process.js'), { |
181 | connect: false, |
182 | center: true, |
183 | fullscreen: false, |
184 | fullscreenable: false, |
185 | height: 150, |
186 | maximizable: false, |
187 | minimizable: false, |
188 | resizable: false, |
189 | show: false, |
190 | skipTaskbar: true, |
191 | title: 'patchwork-server', |
192 | useContentSize: true, |
193 | width: 150 |
194 | }) |
195 | // windows.background.on('close', (ev) => { |
196 | // ev.preventDefault() |
197 | // windows.background.hide() |
198 | // }) |
199 | } |
200 | } |
201 |
Built with git-ssb-web