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