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