This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 333
/
index.js
330 lines (299 loc) · 9.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
process.on('uncaughtException', function (err) {
console.log(err)
process.exit()
})
const electron = require('electron')
const openWindow = require('./lib/window')
const Path = require('path')
const defaultMenu = require('electron-default-menu')
const WindowState = require('electron-window-state')
const Menu = electron.Menu
const extend = require('xtend')
const ssbKeys = require('ssb-keys')
const windows = {
dialogs: new Set()
}
let ssbConfig = null
let quitting = false
/**
* It's not possible to run two instances of patchwork as it would create two
* ssb-server instances that conflict on the same port. Before opening patchwork,
* we check if it's already running and if it is we focus the existing window
* rather than opening a new instance.
*/
function quitIfAlreadyRunning () {
if (!electron.app.requestSingleInstanceLock()) {
console.log('Patchwork is already running!')
console.log('Please close the existing instance before starting a new one.')
return electron.app.quit()
}
electron.app.on('second-instance', () => {
// Someone tried to run a second instance, we should focus our window.
if (windows.main) {
if (windows.main.isMinimized()) windows.main.restore()
windows.main.focus()
}
})
}
const config = {
server: !(process.argv.includes('-g') || process.argv.includes('--use-global-ssb'))
}
// a flag so we don't start git-ssb-web if a custom path is passed in
if (process.argv.includes('--path')) {
config.customPath = true
}
quitIfAlreadyRunning()
electron.app.on('ready', () => {
setupContext(process.env.ssb_appname || 'ssb', {
server: !(process.argv.includes('-g') || process.argv.includes('--use-global-ssb'))
}, () => {
const browserWindow = openMainWindow()
browserWindow.on('app-command', (e, cmd) => {
switch (cmd) {
case 'browser-backward': {
browserWindow.webContents.send('goBack')
break
}
case 'browser-forward': {
browserWindow.webContents.send('goForward')
break
}
}
})
const menu = defaultMenu(electron.app, electron.shell)
menu.splice(4, 0, {
label: 'Navigation',
submenu: [
{
label: 'Activate Search Field',
accelerator: 'CmdOrCtrl+L',
click: () => {
browserWindow.webContents.send('activateSearch')
}
},
{
label: 'Back',
accelerator: 'CmdOrCtrl+[',
click: () => {
browserWindow.webContents.send('goBack')
}
},
{
label: 'Forward',
accelerator: 'CmdOrCtrl+]',
click: () => {
browserWindow.webContents.send('goForward')
}
},
{
type: 'separator'
},
{
label: 'Settings',
accelerator: 'CmdOrCtrl+,',
click: () => {
browserWindow.webContents.send('goToSettings')
}
},
{
label: 'Status',
accelerator: 'CmdOrCtrl+.',
click: () => {
browserWindow.webContents.send('goToStatus')
}
}
]
})
const view = menu.find(x => x.label === 'View')
view.submenu = [
{ role: 'reload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin', accelerator: 'CmdOrCtrl+=' },
{ role: 'zoomout', accelerator: 'CmdOrCtrl+-' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
const help = menu.find(x => x.label === 'Help')
help.submenu = [
{
label: 'Learn More',
click () { require('electron').shell.openExternal('https://scuttlebutt.nz') }
}
]
if (process.platform === 'darwin') {
const win = menu.find(x => x.label === 'Window')
win.submenu = [
{ role: 'minimize' },
{ role: 'zoom' },
{ role: 'close', label: 'Close' },
{ type: 'separator' },
{ role: 'front' }
]
}
Menu.setApplicationMenu(Menu.buildFromTemplate(menu))
})
electron.app.on('activate', function () {
if (windows.main) {
windows.main.show()
}
})
electron.app.on('before-quit', function () {
quitting = true
})
electron.ipcMain.handle('navigation-menu-popup', (event, data) => {
const {items, x, y} = data
const window = event.sender
const factor = event.sender.zoomFactor
const menuItems = buildMenu(items, window)
const menu = electron.Menu.buildFromTemplate(menuItems);
menu.popup({
window,
x: Math.round(x * factor),
y: Math.round(y * factor) + 4,
});
})
electron.ipcMain.handle('setSpellcheckLangs', (ev, params) => {
if (!windows.main) { return }
const { langs, enabled } = params
windows.main.webContents.session.setSpellCheckerLanguages(enabled ? langs : []);
})
electron.ipcMain.handle('consoleLog', (ev, o) => console.log(o))
electron.ipcMain.handle('consoleError', (ev, o) => console.error(o))
electron.ipcMain.handle('badgeCount', (ev, count) => {
electron.app.badgeCount = count;
});
electron.ipcMain.on('exit', (ev, code) => process.exit(code))
})
function openServerDevTools () {
if (windows.background) {
windows.background.webContents.openDevTools({ mode: 'detach' })
}
}
function buildMenu(items, window) {
const result = []
for (let item of items) {
switch (item.type) {
case 'separator':
result.push(item)
break
case 'submenu':
result.push({
...item,
submenu: buildMenu(item.submenu, window),
})
break
case 'normal':
result.push({
...item,
click: () => navigateTo(item.target)
})
break
default:
throw Error(`Unknown menu item of type "${item.type}": ${JSON.stringify(item, null, 2)}`);
}
}
return result
}
function navigateTo(target) {
if (windows?.main) {
windows.main.send('navigate-to', target)
}
}
function openMainWindow () {
if (!windows.main) {
const windowState = WindowState({
defaultWidth: 1024,
defaultHeight: 768
})
windows.main = openWindow(ssbConfig, Path.join(__dirname, 'lib', 'main-window.js'), {
minWidth: 800,
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
titleBarStyle: 'hiddenInset',
autoHideMenuBar: true,
title: 'Patchwork',
show: true,
backgroundColor: '#EEE',
icon: Path.join(__dirname, 'assets/icon.png'),
},
openServerDevTools,
navigateTo,
)
windowState.manage(windows.main)
windows.main.setSheetOffset(40)
windows.main.on('close', function (e) {
if (!quitting && process.platform === 'darwin') {
e.preventDefault()
windows.main.hide()
}
})
windows.main.on('closed', function () {
windows.main = null
if (process.platform !== 'darwin') electron.app.quit()
})
}
return windows.main
}
function setupContext (appName, opts, cb) {
ssbConfig = require('ssb-config/inject')(appName, extend({
port: 8008,
blobsPort: 8989, // matches ssb-ws
friends: { // not using ssb-friends (sbot/contacts fixes hops at 2, so this setting won't do anything)
dunbar: 150,
hops: 2 // down from 3
}
}, opts))
// disable gossip auto-population from {type: 'pub'} messages as we handle this manually in sbot/index.js
if (!ssbConfig.gossip) ssbConfig.gossip = {}
ssbConfig.gossip.autoPopulate = false
ssbConfig.keys = ssbKeys.loadOrCreateSync(Path.join(ssbConfig.path, 'secret'))
const keys = ssbConfig.keys
const pubkey = keys.id.slice(1).replace(`.${keys.curve}`, '')
if (process.platform === 'win32') {
// fix offline on windows by specifying 127.0.0.1 instead of localhost (default)
ssbConfig.remote = `net:127.0.0.1:${ssbConfig.port}~shs:${pubkey}`
} else {
const socketPath = Path.join(ssbConfig.path, 'socket')
ssbConfig.connections.incoming.unix = [{ scope: 'device', transform: 'noauth' }]
ssbConfig.remote = `unix:${socketPath}:~noauth:${pubkey}`
}
// Support rooms
ssbConfig.connections.incoming.tunnel = [{ scope: 'public', transform: 'shs' }]
ssbConfig.connections.outgoing.tunnel = [{ transform: 'shs' }]
// Support DHT invites (only as a client, for now)
ssbConfig.connections.outgoing.dht = [{ transform: 'shs' }]
const redactedConfig = JSON.parse(JSON.stringify(ssbConfig))
redactedConfig.keys.private = null
console.dir(redactedConfig, { depth: null })
if (opts.server === false) {
cb && cb()
} else {
electron.ipcMain.once('server-started', function (ev, config) {
ssbConfig = config
cb && cb()
})
windows.background = openWindow(ssbConfig, Path.join(__dirname, 'lib', 'server-process.js'), {
connect: false,
center: true,
fullscreen: false,
fullscreenable: false,
height: 150,
maximizable: false,
minimizable: false,
resizable: false,
show: false,
skipTaskbar: true,
title: 'patchwork-server',
useContentSize: true,
width: 150
})
// windows.background.on('close', (ev) => {
// ev.preventDefault()
// windows.background.hide()
// })
}
}