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