Files: 2d3be0b71fb114fe7815f6d65ae8c25ba8337ae4 / index.js
3891 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 | }, opts)) |
111 | |
112 | ssbConfig.keys = ssbKeys.loadOrCreateSync(Path.join(ssbConfig.path, 'secret')) |
113 | |
114 | // fix offline on windows by specifying 127.0.0.1 instead of localhost (default) |
115 | var id = ssbConfig.keys.id |
116 | ssbConfig.remote = `net:127.0.0.1:${ssbConfig.port}~shs:${id.slice(1).replace('.ed25519', '')}` |
117 | |
118 | if (opts.server === false) { |
119 | cb && cb() |
120 | } else { |
121 | electron.ipcMain.once('server-started', function (ev, config) { |
122 | ssbConfig = config |
123 | cb && cb() |
124 | }) |
125 | windows.background = openWindow(ssbConfig, Path.join(__dirname, 'server-process.js'), { |
126 | connect: false, |
127 | center: true, |
128 | fullscreen: false, |
129 | fullscreenable: false, |
130 | height: 150, |
131 | maximizable: false, |
132 | minimizable: false, |
133 | resizable: false, |
134 | show: false, |
135 | skipTaskbar: true, |
136 | title: 'patchwork-server', |
137 | useContentSize: true, |
138 | width: 150 |
139 | }) |
140 | // windows.background.on('close', (ev) => { |
141 | // ev.preventDefault() |
142 | // windows.background.hide() |
143 | // }) |
144 | } |
145 | } |
146 |
Built with git-ssb-web