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